pt_core.c 518 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include <linux/suspend.h>
  33. #include "pt_regs.h"
  34. #include <linux/soc/qcom/panel_event_notifier.h>
  35. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  36. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  37. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  38. #define FT_VTG_MIN_UV 2800000
  39. #define FT_VTG_MAX_UV 2800000
  40. #define FT_I2C_VTG_MIN_UV 1800000
  41. #define FT_I2C_VTG_MAX_UV 1800000
  42. #define PWR_SUSPEND_LOAD_UA 165
  43. #define I2C_SUSPEND_LOAD_UA 100
  44. #define PWR_ACTIVE_LOAD_MA 12000
  45. #define I2C_ACTIVE_LOAD_MA 30000
  46. #define PT_CORE_STARTUP_RETRY_COUNT 3
  47. #define PT_STATUS_STR_LEN (50)
  48. #if defined(CONFIG_DRM)
  49. static struct drm_panel *active_panel;
  50. #endif
  51. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  52. #define ENABLE_I2C_REG_ONLY
  53. #ifdef ENABLE_I2C_REG_ONLY
  54. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  55. #endif
  56. static const char *pt_driver_core_name = PT_CORE_NAME;
  57. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  58. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  59. struct pt_hid_field {
  60. int report_count;
  61. int report_size;
  62. int size; /* report_count * report_size */
  63. int offset;
  64. int data_type;
  65. int logical_min;
  66. int logical_max;
  67. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  68. u32 usage_page;
  69. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  70. struct pt_hid_report *report;
  71. bool record_field;
  72. };
  73. struct pt_hid_report {
  74. u8 id;
  75. u8 type;
  76. int size;
  77. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  78. int num_fields;
  79. int record_field_index;
  80. int header_size;
  81. int record_size;
  82. u32 usage_page;
  83. };
  84. struct atten_node {
  85. struct list_head node;
  86. char *id;
  87. struct device *dev;
  88. int (*func)(struct device *dev);
  89. int mode;
  90. };
  91. struct param_node {
  92. struct list_head node;
  93. u8 id;
  94. u32 value;
  95. u8 size;
  96. };
  97. struct module_node {
  98. struct list_head node;
  99. struct pt_module *module;
  100. void *data;
  101. };
  102. struct pt_hid_cmd {
  103. u8 opcode;
  104. u8 report_type;
  105. union {
  106. u8 report_id;
  107. u8 power_state;
  108. };
  109. u8 has_data_register;
  110. size_t write_length;
  111. u8 *write_buf;
  112. u8 *read_buf;
  113. u8 wait_interrupt;
  114. u8 reset_cmd;
  115. u16 timeout_ms;
  116. };
  117. struct pt_hid_output {
  118. u8 cmd_type;
  119. u16 length;
  120. u8 command_code;
  121. size_t write_length;
  122. u8 *write_buf;
  123. u8 novalidate;
  124. u8 reset_expected;
  125. u16 timeout_ms;
  126. };
  127. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  128. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  129. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  130. #define CREATE_PIP1_FW_CMD(command) \
  131. .cmd_type = PIP1_CMD_TYPE_FW, \
  132. .command_code = command
  133. #define CREATE_PIP1_BL_CMD(command) \
  134. .cmd_type = PIP1_CMD_TYPE_BL, \
  135. .command_code = command
  136. #define PT_MAX_PR_BUF_SIZE 2048
  137. /*******************************************************************************
  138. * FUNCTION: pt_pr_buf
  139. *
  140. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  141. *
  142. * RETURN: Void
  143. *
  144. * PARAMETERS:
  145. * *dev - pointer to Device structure
  146. * debug_level - requested debug level to print at
  147. * *buf - pointer to buffer to print
  148. * buf_len - size of buf
  149. * *data_name - Descriptive name of data prefixed to data
  150. ******************************************************************************/
  151. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  152. u16 buf_len, const char *data_name)
  153. {
  154. struct pt_core_data *cd = dev_get_drvdata(dev);
  155. int i;
  156. ssize_t pr_buf_index = 0;
  157. int max_size;
  158. /* only proceed if valid debug level and there is data to print */
  159. if (debug_level <= cd->debug_level && buf_len > 0) {
  160. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  161. if (!pr_buf)
  162. return;
  163. /*
  164. * With a space each printed char takes 3 bytes, subtract
  165. * the length of the data_name prefix as well as 11 bytes
  166. * for the " [0..xxx]: " printed before the data.
  167. */
  168. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  169. /* Ensure pr_buf_index stays within the 1018 size */
  170. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  171. data_name);
  172. for (i = 0; i < buf_len && i < max_size; i++)
  173. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  174. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  175. pt_debug(dev, debug_level, "%s\n", pr_buf);
  176. kfree(pr_buf);
  177. }
  178. }
  179. EXPORT_SYMBOL_GPL(pt_pr_buf);
  180. #ifdef TTHE_TUNER_SUPPORT
  181. /*******************************************************************************
  182. * FUNCTION: tthe_print
  183. *
  184. * SUMMARY: Format data name and time stamp as the header and format the
  185. * content of input buffer with hex base to "tthe_buf". And then wake up event
  186. * semaphore for tthe debugfs node.
  187. *
  188. * RETURN:
  189. * 0 = success
  190. * !0 = failure
  191. *
  192. * PARAMETERS:
  193. * *cd - pointer to core data
  194. * *buf - pointer to input buffer
  195. * buf_len - size of input buffer
  196. * *data_name - pointer to data name
  197. ******************************************************************************/
  198. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  199. const u8 *data_name)
  200. {
  201. int name_len = strlen(data_name);
  202. int i, n;
  203. u8 *p;
  204. int remain;
  205. u8 data_name_with_time_stamp[100];
  206. /* Prepend timestamp, if requested, to data_name */
  207. if (cd->show_timestamp) {
  208. scnprintf(data_name_with_time_stamp,
  209. sizeof(data_name_with_time_stamp),
  210. "[%u] %s", pt_get_time_stamp(), data_name);
  211. data_name = data_name_with_time_stamp;
  212. name_len = strlen(data_name);
  213. }
  214. mutex_lock(&cd->tthe_lock);
  215. if (!cd->tthe_buf)
  216. goto exit;
  217. /* Add 1 due to the '\n' that is appended at the end */
  218. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  219. goto exit;
  220. if (name_len + buf_len == 0)
  221. goto exit;
  222. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  223. if (remain < name_len)
  224. name_len = remain;
  225. p = cd->tthe_buf + cd->tthe_buf_len;
  226. memcpy(p, data_name, name_len);
  227. cd->tthe_buf_len += name_len;
  228. p += name_len;
  229. remain -= name_len;
  230. *p = 0;
  231. for (i = 0; i < buf_len; i++) {
  232. n = scnprintf(p, remain, "%02X ", buf[i]);
  233. if (n <= 0)
  234. break;
  235. p += n;
  236. remain -= n;
  237. cd->tthe_buf_len += n;
  238. }
  239. n = scnprintf(p, remain, "\n");
  240. cd->tthe_buf_len += n;
  241. exit:
  242. wake_up(&cd->wait_q);
  243. mutex_unlock(&cd->tthe_lock);
  244. return 0;
  245. }
  246. /*******************************************************************************
  247. * FUNCTION: _pt_request_tthe_print
  248. *
  249. * SUMMARY: Function pointer included in core_cmds to allow other modules
  250. * to request to print data to the "tthe_buffer".
  251. *
  252. * RETURN:
  253. * 0 = success
  254. * !0 = failure
  255. *
  256. * PARAMETERS:
  257. * *dev - pointer to device structure
  258. ******************************************************************************/
  259. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  260. int buf_len, const u8 *data_name)
  261. {
  262. struct pt_core_data *cd = dev_get_drvdata(dev);
  263. return tthe_print(cd, buf, buf_len, data_name);
  264. }
  265. #endif
  266. /*******************************************************************************
  267. * FUNCTION: pt_platform_detect_read
  268. *
  269. * SUMMARY: To be passed to platform dectect function to perform a read
  270. * operation.
  271. *
  272. * RETURN:
  273. * 0 = success
  274. * !0 = failure
  275. *
  276. * PARAMETERS:
  277. * *dev - pointer to Device structure
  278. * *buf - pointer to buffer where the data read will be stored
  279. * size - size to be read
  280. ******************************************************************************/
  281. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  282. {
  283. struct pt_core_data *cd = dev_get_drvdata(dev);
  284. return pt_adap_read_default(cd, buf, size);
  285. }
  286. /*******************************************************************************
  287. * FUNCTION: pt_add_parameter
  288. *
  289. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  290. * On every reset of the DUT this linked list is traversed and all
  291. * parameters in it are restored to the DUT.
  292. *
  293. * RETURN:
  294. * 0 = success
  295. * !0 = failure
  296. *
  297. * PARAMETERS:
  298. * *cd - pointer to core data
  299. * param_id - parameter ID to add
  300. * param_value - Value corresponding to the ID
  301. * param_size - Size of param_value
  302. ******************************************************************************/
  303. static int pt_add_parameter(struct pt_core_data *cd,
  304. u8 param_id, u32 param_value, u8 param_size)
  305. {
  306. struct param_node *param, *param_new;
  307. /* Check if parameter already exists in the list */
  308. spin_lock(&cd->spinlock);
  309. list_for_each_entry(param, &cd->param_list, node) {
  310. if (param->id == param_id) {
  311. /* Update parameter */
  312. param->value = param_value;
  313. pt_debug(cd->dev, DL_INFO,
  314. "%s: Update parameter id:%d value:%d size:%d\n",
  315. __func__, param_id, param_value, param_size);
  316. goto exit_unlock;
  317. }
  318. }
  319. spin_unlock(&cd->spinlock);
  320. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  321. if (!param_new)
  322. return -ENOMEM;
  323. param_new->id = param_id;
  324. param_new->value = param_value;
  325. param_new->size = param_size;
  326. pt_debug(cd->dev, DL_INFO,
  327. "%s: Add parameter id:%d value:%d size:%d\n",
  328. __func__, param_id, param_value, param_size);
  329. spin_lock(&cd->spinlock);
  330. list_add(&param_new->node, &cd->param_list);
  331. exit_unlock:
  332. spin_unlock(&cd->spinlock);
  333. return 0;
  334. }
  335. #ifdef TTDL_DIAGNOSTICS
  336. /*******************************************************************************
  337. * FUNCTION: pt_erase_parameter_list
  338. *
  339. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  340. * pairs. In some test cases this functionality is needed to ensure DUT
  341. * returns to a virgin state after a reset and no parameters are restored.
  342. *
  343. * RETURN:
  344. * 0 = success
  345. * !0 = failure
  346. *
  347. * PARAMETERS:
  348. * *cd - pointer to core data
  349. ******************************************************************************/
  350. static int pt_erase_parameter_list(struct pt_core_data *cd)
  351. {
  352. struct param_node *pos, *temp;
  353. spin_lock(&cd->spinlock);
  354. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  355. pt_debug(cd->dev, DL_INFO,
  356. "%s: Parameter Restore List - remove 0x%02x\n",
  357. __func__, pos->id);
  358. list_del(&pos->node);
  359. kfree(pos);
  360. }
  361. spin_unlock(&cd->spinlock);
  362. return 0;
  363. }
  364. /*******************************************************************************
  365. * FUNCTION: pt_count_parameter_list
  366. *
  367. * SUMMARY: Count the items in the RAM parameter restor list
  368. *
  369. * RETURN:
  370. * 0 = success
  371. * !0 = failure
  372. *
  373. * PARAMETERS:
  374. * *cd - pointer to core data
  375. ******************************************************************************/
  376. static int pt_count_parameter_list(struct pt_core_data *cd)
  377. {
  378. struct param_node *pos, *temp;
  379. int entries = 0;
  380. spin_lock(&cd->spinlock);
  381. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  382. entries++;
  383. spin_unlock(&cd->spinlock);
  384. return entries;
  385. }
  386. #endif /* TTDL_DIAGNOSTICS */
  387. /*******************************************************************************
  388. * FUNCTION: request_exclusive
  389. *
  390. * SUMMARY: Request exclusive access to the DUT
  391. *
  392. * RETURN:
  393. * 0 = success
  394. * !0 = failure
  395. *
  396. * PARAMETERS:
  397. * *cd - pointer to core data
  398. * *ownptr - pointer to device
  399. * timeout_ms - Timeout value
  400. ******************************************************************************/
  401. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  402. int timeout_ms)
  403. {
  404. int t = msecs_to_jiffies(timeout_ms);
  405. bool with_timeout = (timeout_ms != 0);
  406. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  407. __func__, timeout_ms);
  408. mutex_lock(&cd->system_lock);
  409. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  410. cd->exclusive_dev = ownptr;
  411. goto exit;
  412. }
  413. cd->exclusive_waits++;
  414. wait:
  415. mutex_unlock(&cd->system_lock);
  416. if (with_timeout) {
  417. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  418. if (IS_TMO(t)) {
  419. pt_debug(cd->dev, DL_ERROR,
  420. "%s: tmo waiting exclusive access\n", __func__);
  421. return -ETIME;
  422. }
  423. } else {
  424. wait_event(cd->wait_q, !cd->exclusive_dev);
  425. }
  426. mutex_lock(&cd->system_lock);
  427. if (cd->exclusive_dev)
  428. goto wait;
  429. cd->exclusive_dev = ownptr;
  430. cd->exclusive_waits--;
  431. exit:
  432. mutex_unlock(&cd->system_lock);
  433. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  434. __func__, ownptr);
  435. return 0;
  436. }
  437. /*******************************************************************************
  438. * FUNCTION: release_exclusive_
  439. *
  440. * SUMMARY: Release exclusive access to the DUT
  441. *
  442. * RETURN:
  443. * 0 = success
  444. *
  445. * PARAMETERS:
  446. * *cd - pointer to core data
  447. * *ownptr - pointer to device
  448. ******************************************************************************/
  449. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  450. {
  451. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  452. __func__);
  453. if (cd->exclusive_dev != ownptr)
  454. return -EINVAL;
  455. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  456. __func__, cd->exclusive_dev);
  457. cd->exclusive_dev = NULL;
  458. wake_up(&cd->wait_q);
  459. return 0;
  460. }
  461. /*******************************************************************************
  462. * FUNCTION: release_exclusive
  463. *
  464. * SUMMARY: Protected wrapper to release_exclusive_()
  465. *
  466. * RETURN:
  467. * 0 = success
  468. * !0 = failure
  469. *
  470. * PARAMETERS:
  471. * *cd - pointer to core data
  472. * *ownptr - pointer to device
  473. ******************************************************************************/
  474. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  475. {
  476. int rc;
  477. mutex_lock(&cd->system_lock);
  478. rc = release_exclusive_(cd, ownptr);
  479. mutex_unlock(&cd->system_lock);
  480. return rc;
  481. }
  482. /*******************************************************************************
  483. * FUNCTION: pt_hid_exec_cmd_
  484. *
  485. * SUMMARY: Send the HID command to the DUT
  486. *
  487. * RETURN:
  488. * 0 = success
  489. * !0 = failure
  490. *
  491. * PARAMETERS:
  492. * *cd - pointer to core data
  493. * *hid_cmd - pointer to the HID command to send
  494. ******************************************************************************/
  495. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  496. struct pt_hid_cmd *hid_cmd)
  497. {
  498. int rc = 0;
  499. u8 *cmd;
  500. u16 cmd_length;
  501. u8 cmd_offset = 0;
  502. cmd_length = 2 /* command register */
  503. + 2 /* command */
  504. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  505. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  506. + hid_cmd->write_length; /* Data length */
  507. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  508. return -EPROTO;
  509. cmd = kzalloc(cmd_length, GFP_KERNEL);
  510. if (!cmd)
  511. return -ENOMEM;
  512. /* Set Command register */
  513. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  514. sizeof(cd->hid_desc.command_register));
  515. cmd_offset += sizeof(cd->hid_desc.command_register);
  516. /* Set Command */
  517. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  518. if (hid_cmd->report_id >= 0XF)
  519. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  520. else
  521. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  522. cmd_offset++;
  523. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  524. cmd_offset++;
  525. if (hid_cmd->report_id >= 0XF) {
  526. cmd[cmd_offset] = hid_cmd->report_id;
  527. cmd_offset++;
  528. }
  529. /* Set Data register */
  530. if (hid_cmd->has_data_register) {
  531. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  532. sizeof(cd->hid_desc.data_register));
  533. cmd_offset += sizeof(cd->hid_desc.data_register);
  534. }
  535. /* Set Data */
  536. if (hid_cmd->write_length && hid_cmd->write_buf) {
  537. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  538. hid_cmd->write_length);
  539. cmd_offset += hid_cmd->write_length;
  540. }
  541. pt_debug(cd->dev, DL_INFO,
  542. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  543. __func__, cmd_length, hid_cmd->report_id);
  544. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  545. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  546. hid_cmd->read_buf);
  547. if (rc)
  548. pt_debug(cd->dev, DL_ERROR,
  549. "%s: Fail pt_adap_transfer\n", __func__);
  550. kfree(cmd);
  551. return rc;
  552. }
  553. #ifdef TTDL_DIAGNOSTICS
  554. /*******************************************************************************
  555. * FUNCTION: pt_toggle_err_gpio
  556. *
  557. * SUMMARY: Toggles the pre-defined error GPIO
  558. *
  559. * RETURN: n/a
  560. *
  561. * PARAMETERS:
  562. * *cd - pointer to core data
  563. * type - type of err that occurred
  564. ******************************************************************************/
  565. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  566. {
  567. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  568. __func__, type);
  569. if (cd->err_gpio && type == cd->err_gpio_type) {
  570. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  571. gpio_direction_output(cd->err_gpio,
  572. !gpio_get_value(cd->err_gpio));
  573. }
  574. }
  575. /*******************************************************************************
  576. * FUNCTION: _pt_request_toggle_err_gpio
  577. *
  578. * SUMMARY: Function pointer included in core_cmds to allow other modules
  579. * to request to toggle the err_gpio
  580. *
  581. * RETURN: n/a
  582. *
  583. * PARAMETERS:
  584. * *cd - pointer to core data
  585. * type - type of err that occurred
  586. ******************************************************************************/
  587. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  588. {
  589. struct pt_core_data *cd = dev_get_drvdata(dev);
  590. pt_toggle_err_gpio(cd, type);
  591. }
  592. #endif /* TTDL_DIAGNOSTICS */
  593. /*******************************************************************************
  594. * FUNCTION: pt_hid_exec_cmd_and_wait_
  595. *
  596. * SUMMARY: Send the HID command to the DUT and wait for the response
  597. *
  598. * RETURN:
  599. * 0 = success
  600. * !0 = failure
  601. *
  602. * PARAMETERS:
  603. * *cd - pointer to core data
  604. * *hid_cmd - pointer to the HID command to send
  605. ******************************************************************************/
  606. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  607. struct pt_hid_cmd *hid_cmd)
  608. {
  609. int rc = 0;
  610. int t;
  611. u16 timeout_ms;
  612. int *cmd_state;
  613. if (hid_cmd->reset_cmd)
  614. cmd_state = &cd->hid_reset_cmd_state;
  615. else
  616. cmd_state = &cd->hid_cmd_state;
  617. if (hid_cmd->wait_interrupt) {
  618. mutex_lock(&cd->system_lock);
  619. *cmd_state = 1;
  620. mutex_unlock(&cd->system_lock);
  621. }
  622. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  623. if (rc) {
  624. if (hid_cmd->wait_interrupt)
  625. goto error;
  626. goto exit;
  627. }
  628. if (!hid_cmd->wait_interrupt)
  629. goto exit;
  630. if (hid_cmd->timeout_ms)
  631. timeout_ms = hid_cmd->timeout_ms;
  632. else
  633. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  634. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  635. msecs_to_jiffies(timeout_ms));
  636. if (IS_TMO(t)) {
  637. #ifdef TTDL_DIAGNOSTICS
  638. cd->bus_transmit_error_count++;
  639. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  640. #endif /* TTDL_DIAGNOSTICS */
  641. pt_debug(cd->dev, DL_ERROR,
  642. "%s: HID output cmd execution timed out\n",
  643. __func__);
  644. rc = -ETIME;
  645. goto error;
  646. }
  647. goto exit;
  648. error:
  649. mutex_lock(&cd->system_lock);
  650. *cmd_state = 0;
  651. mutex_unlock(&cd->system_lock);
  652. exit:
  653. return rc;
  654. }
  655. /*******************************************************************************
  656. * FUNCTION: pt_hid_cmd_reset_
  657. *
  658. * SUMMARY: Send the HID RESET command to the DUT
  659. *
  660. * RETURN:
  661. * 0 = success
  662. * !0 = failure
  663. *
  664. * PARAMETERS:
  665. * *cd - pointer to core data
  666. ******************************************************************************/
  667. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  668. {
  669. struct pt_hid_cmd hid_cmd = {
  670. .opcode = HID_CMD_RESET,
  671. .wait_interrupt = 1,
  672. .reset_cmd = 1,
  673. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  674. };
  675. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  676. }
  677. /*******************************************************************************
  678. * FUNCTION: pt_hid_cmd_reset
  679. *
  680. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  681. * access.
  682. *
  683. * RETURN:
  684. * 0 = success
  685. * !0 = failure
  686. *
  687. * PARAMETERS:
  688. * *cd - pointer to core data
  689. ******************************************************************************/
  690. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  691. {
  692. int rc = 0;
  693. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  694. if (rc < 0) {
  695. pt_debug(cd->dev, DL_ERROR,
  696. "%s: fail get exclusive ex=%p own=%p\n",
  697. __func__, cd->exclusive_dev, cd->dev);
  698. return rc;
  699. }
  700. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  701. rc = pt_hid_cmd_reset_(cd);
  702. if (release_exclusive(cd, cd->dev) < 0)
  703. pt_debug(cd->dev, DL_ERROR,
  704. "%s: fail to release exclusive\n", __func__);
  705. return rc;
  706. }
  707. /*******************************************************************************
  708. * FUNCTION: pt_hid_cmd_set_power_
  709. *
  710. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  711. *
  712. * RETURN:
  713. * 0 = success
  714. * !0 = failure
  715. *
  716. * PARAMETERS:
  717. * *cd - pointer to core data
  718. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  719. ******************************************************************************/
  720. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  721. u8 power_state)
  722. {
  723. int rc = 0;
  724. struct pt_hid_cmd hid_cmd = {
  725. .opcode = HID_CMD_SET_POWER,
  726. .wait_interrupt = 1,
  727. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  728. };
  729. hid_cmd.power_state = power_state;
  730. /* The chip won't give response if goes to Deep Standby */
  731. if (power_state == HID_POWER_STANDBY) {
  732. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  733. if (rc)
  734. pt_debug(cd->dev, DL_ERROR,
  735. "%s: Failed to set power to state:%d\n",
  736. __func__, power_state);
  737. else
  738. cd->fw_sys_mode_in_standby_state = true;
  739. return rc;
  740. }
  741. cd->fw_sys_mode_in_standby_state = false;
  742. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  743. if (rc) {
  744. pt_debug(cd->dev, DL_ERROR,
  745. "%s: Failed to set power to state:%d\n",
  746. __func__, power_state);
  747. return rc;
  748. }
  749. /* validate */
  750. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  751. || ((cd->response_buf[3] & 0x3) != power_state)
  752. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  753. rc = -EINVAL;
  754. return rc;
  755. }
  756. /*******************************************************************************
  757. * FUNCTION: pt_hid_cmd_set_power
  758. *
  759. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  760. * exclusive access.
  761. *
  762. * RETURN:
  763. * 0 = success
  764. * !0 = failure
  765. *
  766. * PARAMETERS:
  767. * *cd - pointer to core data
  768. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  769. ******************************************************************************/
  770. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  771. u8 power_state)
  772. {
  773. int rc;
  774. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  775. if (rc < 0) {
  776. pt_debug(cd->dev, DL_ERROR,
  777. "%s: fail get exclusive ex=%p own=%p\n",
  778. __func__, cd->exclusive_dev, cd->dev);
  779. return rc;
  780. }
  781. rc = pt_hid_cmd_set_power_(cd, power_state);
  782. if (release_exclusive(cd, cd->dev) < 0)
  783. pt_debug(cd->dev, DL_ERROR,
  784. "%s: fail to release exclusive\n", __func__);
  785. return rc;
  786. }
  787. static const u16 crc_table[16] = {
  788. 0x0000, 0x1021, 0x2042, 0x3063,
  789. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  790. 0x8108, 0x9129, 0xa14a, 0xb16b,
  791. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  792. };
  793. /*******************************************************************************
  794. * FUNCTION: _pt_compute_crc
  795. *
  796. * SUMMARY: Calculate CRC by CRC table.
  797. *
  798. * RETURN:
  799. * CRC calculation result
  800. *
  801. * PARAMETERS:
  802. * *buf - pointer to the data array to be calculated
  803. * size - size of data array
  804. ******************************************************************************/
  805. static u16 _pt_compute_crc(u8 *buf, u32 size)
  806. {
  807. u16 remainder = 0xFFFF;
  808. u16 xor_mask = 0x0000;
  809. u32 index;
  810. u32 byte_value;
  811. u32 table_index;
  812. u32 crc_bit_width = sizeof(u16) * 8;
  813. /* Divide the message by polynomial, via the table. */
  814. for (index = 0; index < size; index++) {
  815. byte_value = buf[index];
  816. table_index = ((byte_value >> 4) & 0x0F)
  817. ^ (remainder >> (crc_bit_width - 4));
  818. remainder = crc_table[table_index] ^ (remainder << 4);
  819. table_index = (byte_value & 0x0F)
  820. ^ (remainder >> (crc_bit_width - 4));
  821. remainder = crc_table[table_index] ^ (remainder << 4);
  822. }
  823. /* Perform the final remainder CRC. */
  824. return remainder ^ xor_mask;
  825. }
  826. u16 ccitt_Table[] = {
  827. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  828. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  829. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  830. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  831. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  832. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  833. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  834. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  835. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  836. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  837. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  838. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  839. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  840. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  841. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  842. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  843. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  844. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  845. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  846. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  847. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  848. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  849. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  850. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  851. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  852. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  853. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  854. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  855. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  856. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  857. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  858. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  859. };
  860. /*******************************************************************************
  861. * FUNCTION: crc_ccitt_calculate
  862. *
  863. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  864. *
  865. * RETURN:
  866. * CRC calculation result
  867. *
  868. * PARAMETERS:
  869. * *q - pointer to the data array to be calculated
  870. * len - size of data array
  871. ******************************************************************************/
  872. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  873. {
  874. unsigned short crc = 0xffff;
  875. while (len-- > 0)
  876. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  877. return crc;
  878. }
  879. /*******************************************************************************
  880. * FUNCTION: pt_pip2_cmd_calculate_crc
  881. *
  882. * SUMMARY: Calculate the CRC of a command packet
  883. *
  884. * RETURN: void
  885. *
  886. * PARAMETERS:
  887. * *cmd - pointer to command data
  888. * extra_bytes - Extra bytes included in command length
  889. ******************************************************************************/
  890. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  891. u8 extra_bytes)
  892. {
  893. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  894. unsigned short crc;
  895. buf[0] = cmd->len & 0xff;
  896. buf[1] = (cmd->len & 0xff00) >> 8;
  897. buf[2] = cmd->seq;
  898. buf[3] = cmd->id;
  899. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  900. /* Calculate the CRC for the first 4 bytes above and the data payload */
  901. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  902. cmd->crc[0] = (crc & 0xff00) >> 8;
  903. cmd->crc[1] = (crc & 0xff);
  904. }
  905. /*******************************************************************************
  906. * FUNCTION: pt_pip2_get_next_cmd_seq
  907. *
  908. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  909. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  910. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  911. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  912. * increment, just use the forced value.
  913. *
  914. * RETURN: Next command sequence number [0x08-0x0F]
  915. *
  916. * PARAMETERS:
  917. * *cd - pointer to core data
  918. ******************************************************************************/
  919. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  920. {
  921. #ifdef TTDL_DIAGNOSTICS
  922. if (cd->force_pip2_seq <= 0x07) {
  923. cd->pip2_cmd_tag_seq++;
  924. if (cd->pip2_cmd_tag_seq > 0x0F)
  925. cd->pip2_cmd_tag_seq = 0x08;
  926. } else {
  927. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  928. }
  929. #else
  930. cd->pip2_cmd_tag_seq++;
  931. if (cd->pip2_cmd_tag_seq > 0x0F)
  932. cd->pip2_cmd_tag_seq = 0x08;
  933. #endif
  934. return cd->pip2_cmd_tag_seq;
  935. }
  936. /*
  937. * Following macros are to define the response time (the interval between PIP2
  938. * command finishes sending and INT pin falls). The unit is in microsecond.
  939. * It has different time settings between the solution GPIO polling and Bus
  940. * polling due to the considration for system load.
  941. */
  942. #ifdef PT_POLL_RESP_BY_BUS
  943. #define POLL_RETRY_DEFAULT_INTERVAL 50
  944. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  945. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  946. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  947. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  948. #else
  949. #define POLL_RETRY_DEFAULT_INTERVAL 20
  950. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  951. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  952. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  953. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  954. #endif
  955. /*
  956. * id: the command id defined in PIP2
  957. * response_len: the (maximum) length of response.
  958. * response_time_min: minimum response time in microsecond
  959. * response_time_max: maximum response time in microsecond
  960. */
  961. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  962. {.id = PIP2_CMD_ID_PING,
  963. .response_len = 255,
  964. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  965. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  966. {.id = PIP2_CMD_ID_STATUS,
  967. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  968. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  969. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  970. {.id = PIP2_CMD_ID_CTRL,
  971. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  972. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  973. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  974. {.id = PIP2_CMD_ID_CONFIG,
  975. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_CLEAR,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_RESET,
  983. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  984. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  985. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  986. {.id = PIP2_CMD_ID_VERSION,
  987. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_FILE_OPEN,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  992. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_FILE_CLOSE,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  998. {.id = PIP2_CMD_ID_FILE_READ,
  999. .response_len = 255,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_FILE_WRITE,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1004. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_FLASH_INFO,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_EXECUTE,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1023. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1024. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1025. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1026. {.id = PIP2_CMD_ID_READ_GPIO,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_EXECUTE_SCAN,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_SET_PARAMETER,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_GET_PARAMETER,
  1039. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1042. {.id = PIP2_CMD_SET_DDI_REG,
  1043. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1044. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1045. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1046. {.id = PIP2_CMD_GET_DDI_REG,
  1047. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1048. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1049. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1050. {.id = PIP2_CMD_ID_END,
  1051. .response_len = 255,
  1052. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1053. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1054. };
  1055. /*******************************************************************************
  1056. * FUNCTION: pt_pip2_get_cmd_response_len
  1057. *
  1058. * SUMMARY: Gets the expected response length based on the command ID
  1059. *
  1060. * RETURN: Expected response length
  1061. *
  1062. * PARAMETERS:
  1063. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1064. ******************************************************************************/
  1065. static int pt_pip2_get_cmd_response_len(u8 id)
  1066. {
  1067. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1068. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1069. p++;
  1070. if (p->id != PIP2_CMD_ID_END)
  1071. return p->response_len;
  1072. else
  1073. return -EPERM;
  1074. }
  1075. /*******************************************************************************
  1076. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1077. *
  1078. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1079. * finishes sending and INT pin falls) based on the command ID
  1080. *
  1081. * RETURN: Estimated minimum response time in microsecond
  1082. *
  1083. * PARAMETERS:
  1084. * id - Command ID
  1085. ******************************************************************************/
  1086. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1087. {
  1088. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1089. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1090. p++;
  1091. if (p->id != PIP2_CMD_ID_END)
  1092. return p->response_time_min;
  1093. else
  1094. return PIP2_RESP_DEFAULT_TIME_MIN;
  1095. }
  1096. /*******************************************************************************
  1097. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1098. *
  1099. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1100. * finishes sending and INT pin falls) based on the command ID
  1101. *
  1102. * RETURN: Estimated maximum response time in microsecond
  1103. *
  1104. * PARAMETERS:
  1105. * id - Command ID
  1106. ******************************************************************************/
  1107. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1108. {
  1109. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1110. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1111. p++;
  1112. if (p->id != PIP2_CMD_ID_END)
  1113. return p->response_time_max;
  1114. else
  1115. return PIP2_RESP_DEFAULT_TIME_MAX;
  1116. }
  1117. /*******************************************************************************
  1118. * FUNCTION: pt_pip2_validate_response
  1119. *
  1120. * SUMMARY: Validate the response of PIP2 command.
  1121. *
  1122. * RETURN:
  1123. * 0 = success
  1124. * !0 = failure
  1125. *
  1126. * PARAMETERS:
  1127. * *cd - pointer to core data
  1128. * *pip2_cmd - pointer to PIP2 command to send
  1129. * *read_buf - pointer to response buffer
  1130. * actual_read_len - actual read length of the response
  1131. ******************************************************************************/
  1132. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1133. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1134. u16 actual_read_len)
  1135. {
  1136. int rc = 0;
  1137. u8 response_seq = 0;
  1138. u8 reserved_bits = 0;
  1139. u8 cmd_id = 0;
  1140. u8 response_bit = 0;
  1141. unsigned short calc_crc = 0;
  1142. unsigned short resp_crc = 0;
  1143. /* Verify the length of response buffer */
  1144. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1145. pt_debug(cd->dev, DL_ERROR,
  1146. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1147. __func__, pip2_cmd->id, actual_read_len);
  1148. rc = -EINVAL;
  1149. goto exit;
  1150. }
  1151. /* Verify the CRC */
  1152. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1153. resp_crc = read_buf[actual_read_len - 2] << 8;
  1154. resp_crc |= read_buf[actual_read_len - 1];
  1155. if (resp_crc != calc_crc) {
  1156. pt_debug(cd->dev, DL_ERROR,
  1157. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1158. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1159. #ifdef TTDL_DIAGNOSTICS
  1160. cd->pip2_crc_error_count++;
  1161. #endif /* TTDL_DIAGNOSTICS */
  1162. rc = -EINVAL;
  1163. goto exit;
  1164. }
  1165. /* Verify the response bit is set */
  1166. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1167. if (!response_bit) {
  1168. pt_debug(cd->dev, DL_ERROR,
  1169. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1170. __func__, pip2_cmd->id, response_bit);
  1171. rc = -EINVAL;
  1172. goto exit;
  1173. }
  1174. /* Verify the command ID matches from command to response */
  1175. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1176. if (cmd_id != pip2_cmd->id) {
  1177. pt_debug(cd->dev, DL_ERROR,
  1178. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1179. __func__, pip2_cmd->id, cmd_id);
  1180. rc = -EINVAL;
  1181. goto exit;
  1182. }
  1183. /* Verify the SEQ number matches from command to response */
  1184. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1185. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1186. pt_debug(cd->dev, DL_ERROR,
  1187. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1188. __func__, pip2_cmd->id,
  1189. pip2_cmd->seq, response_seq);
  1190. rc = -EINVAL;
  1191. goto exit;
  1192. }
  1193. /* Verify the reserved bits are 0 */
  1194. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1195. if (reserved_bits)
  1196. pt_debug(cd->dev, DL_WARN,
  1197. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1198. __func__, pip2_cmd->id, reserved_bits);
  1199. exit:
  1200. if (rc)
  1201. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1202. "PIP RSP:");
  1203. return rc;
  1204. }
  1205. /*******************************************************************************
  1206. * FUNCTION: pt_hid_output_validate_bl_response
  1207. *
  1208. * SUMMARY: Validate the response of bootloader command.
  1209. *
  1210. * RETURN:
  1211. * 0 = success
  1212. * !0 = failure
  1213. *
  1214. * PARAMETERS:
  1215. * *cd - pointer to core data
  1216. * *hid_output - pointer to hid output data structure
  1217. ******************************************************************************/
  1218. static int pt_hid_output_validate_bl_response(
  1219. struct pt_core_data *cd,
  1220. struct pt_hid_output *hid_output)
  1221. {
  1222. u16 size;
  1223. u16 crc;
  1224. u8 status;
  1225. size = get_unaligned_le16(&cd->response_buf[0]);
  1226. if (hid_output->reset_expected && !size)
  1227. return 0;
  1228. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1229. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1230. pt_debug(cd->dev, DL_ERROR,
  1231. "%s: BL output response, wrong report_id\n", __func__);
  1232. return -EPROTO;
  1233. }
  1234. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1235. pt_debug(cd->dev, DL_ERROR,
  1236. "%s: BL output response, wrong SOP\n", __func__);
  1237. return -EPROTO;
  1238. }
  1239. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1240. pt_debug(cd->dev, DL_ERROR,
  1241. "%s: BL output response, wrong EOP\n", __func__);
  1242. return -EPROTO;
  1243. }
  1244. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1245. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1246. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1247. pt_debug(cd->dev, DL_ERROR,
  1248. "%s: BL output response, wrong CRC 0x%X\n",
  1249. __func__, crc);
  1250. return -EPROTO;
  1251. }
  1252. status = cd->response_buf[5];
  1253. if (status) {
  1254. pt_debug(cd->dev, DL_ERROR,
  1255. "%s: BL output response, ERROR:%d\n",
  1256. __func__, status);
  1257. return -EPROTO;
  1258. }
  1259. return 0;
  1260. }
  1261. /*******************************************************************************
  1262. * FUNCTION: pt_hid_output_validate_app_response
  1263. *
  1264. * SUMMARY: Validate the response of application command.
  1265. *
  1266. * RETURN:
  1267. * 0 = success
  1268. * !0 = failure
  1269. *
  1270. * PARAMETERS:
  1271. * *cd - pointer to core data
  1272. * *hid_output - pointer to hid output data structure
  1273. ******************************************************************************/
  1274. static int pt_hid_output_validate_app_response(
  1275. struct pt_core_data *cd,
  1276. struct pt_hid_output *hid_output)
  1277. {
  1278. int command_code;
  1279. u16 size;
  1280. size = get_unaligned_le16(&cd->response_buf[0]);
  1281. if (hid_output->reset_expected && !size)
  1282. return 0;
  1283. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1284. != PT_PIP_NON_HID_RESPONSE_ID) {
  1285. pt_debug(cd->dev, DL_ERROR,
  1286. "%s: APP output response, wrong report_id\n", __func__);
  1287. return -EPROTO;
  1288. }
  1289. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1290. & PIP1_RESP_COMMAND_ID_MASK;
  1291. if (command_code != hid_output->command_code) {
  1292. pt_debug(cd->dev, DL_ERROR,
  1293. "%s: APP output response, wrong command_code:%X\n",
  1294. __func__, command_code);
  1295. return -EPROTO;
  1296. }
  1297. return 0;
  1298. }
  1299. /*******************************************************************************
  1300. * FUNCTION: pt_check_set_parameter
  1301. *
  1302. * SUMMARY: Check command input and response for Set Parameter command.And
  1303. * store the parameter to the list for resume work if pass the check.
  1304. *
  1305. * PARAMETERS:
  1306. * *cd - pointer to core data
  1307. * *hid_output - pointer to hid output data structure
  1308. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1309. ******************************************************************************/
  1310. static void pt_check_set_parameter(struct pt_core_data *cd,
  1311. struct pt_hid_output *hid_output, bool raw)
  1312. {
  1313. u8 *param_buf;
  1314. u32 param_value = 0;
  1315. u8 param_size;
  1316. u8 param_id;
  1317. int i = 0;
  1318. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1319. return;
  1320. /* Check command input for Set Parameter command */
  1321. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1322. && !memcmp(&hid_output->write_buf[0],
  1323. &cd->hid_desc.output_register,
  1324. sizeof(cd->hid_desc.output_register))
  1325. && hid_output->write_buf[4] ==
  1326. PT_PIP_NON_HID_COMMAND_ID
  1327. && hid_output->write_buf[6] ==
  1328. PIP1_CMD_ID_SET_PARAM)
  1329. param_buf = &hid_output->write_buf[7];
  1330. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1331. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1332. && hid_output->write_length >= 3
  1333. && hid_output->write_length <= 6)
  1334. param_buf = &hid_output->write_buf[0];
  1335. else
  1336. return;
  1337. /* Get parameter ID, size and value */
  1338. param_id = param_buf[0];
  1339. param_size = param_buf[1];
  1340. if (param_size > 4) {
  1341. pt_debug(cd->dev, DL_ERROR,
  1342. "%s: Invalid parameter size\n", __func__);
  1343. return;
  1344. }
  1345. param_buf = &param_buf[2];
  1346. while (i < param_size)
  1347. param_value += *(param_buf++) << (8 * i++);
  1348. /* Check command response for Set Parameter command */
  1349. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1350. || (cd->response_buf[4] &
  1351. PIP1_RESP_COMMAND_ID_MASK) !=
  1352. PIP1_CMD_ID_SET_PARAM
  1353. || cd->response_buf[5] != param_id
  1354. || cd->response_buf[6] != param_size) {
  1355. pt_debug(cd->dev, DL_ERROR,
  1356. "%s: Set Parameter command not successful\n",
  1357. __func__);
  1358. return;
  1359. }
  1360. pt_add_parameter(cd, param_id, param_value, param_size);
  1361. }
  1362. /*******************************************************************************
  1363. * FUNCTION: pt_check_command
  1364. *
  1365. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1366. * called here to check output command and store parameter to the list.
  1367. *
  1368. * PARAMETERS:
  1369. * *cd - pointer to core data
  1370. * *hid_output - pointer to hid output data structure
  1371. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1372. ******************************************************************************/
  1373. static void pt_check_command(struct pt_core_data *cd,
  1374. struct pt_hid_output *hid_output, bool raw)
  1375. {
  1376. pt_check_set_parameter(cd, hid_output, raw);
  1377. }
  1378. /*******************************************************************************
  1379. * FUNCTION: pt_hid_output_validate_response
  1380. *
  1381. * SUMMARY: Validate the response of application or bootloader command.
  1382. *
  1383. * RETURN:
  1384. * 0 = success
  1385. * !0 = failure
  1386. *
  1387. * PARAMETERS:
  1388. * *cd - pointer to core data
  1389. * *hid_output - pointer to hid output data structure
  1390. ******************************************************************************/
  1391. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1392. struct pt_hid_output *hid_output)
  1393. {
  1394. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1395. return pt_hid_output_validate_bl_response(cd, hid_output);
  1396. return pt_hid_output_validate_app_response(cd, hid_output);
  1397. }
  1398. /*******************************************************************************
  1399. * FUNCTION: pt_hid_send_output_user_
  1400. *
  1401. * SUMMARY: Blindly send user data to the DUT.
  1402. *
  1403. * RETURN:
  1404. * 0 = success
  1405. * !0 = failure
  1406. *
  1407. * PARAMETERS:
  1408. * *cd - pointer to core data
  1409. * *hid_output - pointer to the command to send
  1410. ******************************************************************************/
  1411. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1412. struct pt_hid_output *hid_output)
  1413. {
  1414. int rc = 0;
  1415. int cmd;
  1416. if (!hid_output->length || !hid_output->write_buf)
  1417. return -EINVAL;
  1418. if (cd->pip2_prot_active) {
  1419. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1420. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1421. } else
  1422. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1423. pt_debug(cd->dev, DL_INFO,
  1424. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1425. __func__, hid_output->length, cmd);
  1426. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1427. hid_output->length, ">>> User CMD");
  1428. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1429. hid_output->write_buf, NULL);
  1430. if (rc)
  1431. pt_debug(cd->dev, DL_ERROR,
  1432. "%s: Fail pt_adap_transfer\n", __func__);
  1433. return rc;
  1434. }
  1435. /*******************************************************************************
  1436. * FUNCTION: pt_hid_send_output_user_and_wait_
  1437. *
  1438. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1439. *
  1440. * RETURN:
  1441. * 0 = success
  1442. * !0 = failure
  1443. *
  1444. * PARAMETERS:
  1445. * *cd - pointer to core data
  1446. * *hid_output - pointer to the command to send
  1447. ******************************************************************************/
  1448. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1449. struct pt_hid_output *hid_output)
  1450. {
  1451. int rc = 0;
  1452. int t;
  1453. mutex_lock(&cd->system_lock);
  1454. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1455. mutex_unlock(&cd->system_lock);
  1456. rc = pt_hid_send_output_user_(cd, hid_output);
  1457. if (rc)
  1458. goto error;
  1459. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1460. msecs_to_jiffies(cd->pip_cmd_timeout));
  1461. if (IS_TMO(t)) {
  1462. #ifdef TTDL_DIAGNOSTICS
  1463. cd->bus_transmit_error_count++;
  1464. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1465. #endif /* TTDL_DIAGNOSTICS */
  1466. pt_debug(cd->dev, DL_ERROR,
  1467. "%s: HID output cmd execution timed out\n",
  1468. __func__);
  1469. rc = -ETIME;
  1470. goto error;
  1471. }
  1472. pt_check_command(cd, hid_output, true);
  1473. goto exit;
  1474. error:
  1475. mutex_lock(&cd->system_lock);
  1476. cd->hid_cmd_state = 0;
  1477. mutex_unlock(&cd->system_lock);
  1478. exit:
  1479. return rc;
  1480. }
  1481. /*******************************************************************************
  1482. * FUNCTION: pt_check_irq_asserted
  1483. *
  1484. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1485. * the FW can hold the INT line low ~150us after the read is complete.
  1486. * NOTE: if irq_stat is not defined this function will return false
  1487. *
  1488. * RETURN:
  1489. * true = IRQ asserted
  1490. * false = IRQ not asserted
  1491. *
  1492. * PARAMETERS:
  1493. * *cd - pointer to core data
  1494. ******************************************************************************/
  1495. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1496. {
  1497. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1498. /*
  1499. * Workaround for FW defect, CDT165308
  1500. * bl_launch app creates a glitch in IRQ line
  1501. */
  1502. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1503. && cd->cpdata->irq_stat) {
  1504. /*
  1505. * in X1S panel and GC1546 panel, the width for the INT
  1506. * glitch is about 4us,the normal INT width of response
  1507. * will last more than 200us, so use 10us delay
  1508. * for distinguish the glitch the normal INT is enough.
  1509. */
  1510. udelay(10);
  1511. }
  1512. #endif
  1513. if (cd->cpdata->irq_stat) {
  1514. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1515. == PT_IRQ_ASSERTED_VALUE) {
  1516. /* Debounce to allow FW to release INT */
  1517. usleep_range(100, 200);
  1518. }
  1519. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1520. == PT_IRQ_ASSERTED_VALUE)
  1521. return true;
  1522. else
  1523. return false;
  1524. }
  1525. return true;
  1526. }
  1527. /*******************************************************************************
  1528. * FUNCTION: pt_flush_bus
  1529. *
  1530. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1531. * Used if IRQ is found to be stuck low
  1532. *
  1533. * RETURN: Length of bytes read from bus
  1534. *
  1535. * PARAMETERS:
  1536. * *cd - pointer to core data
  1537. * flush_type - type of flush
  1538. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1539. * - PT_FLUSH_BUS_FULL_256_READ
  1540. * *read_buf - pointer to store read data
  1541. ******************************************************************************/
  1542. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1543. u8 flush_type, u8 *read_buf)
  1544. {
  1545. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1546. u16 pip_len;
  1547. int bytes_read;
  1548. int rc = 0;
  1549. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1550. rc = pt_adap_read_default(cd, buf, 2);
  1551. if (rc) {
  1552. bytes_read = 0;
  1553. goto exit;
  1554. }
  1555. pip_len = get_unaligned_le16(&buf[0]);
  1556. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1557. #ifdef TTDL_DIAGNOSTICS
  1558. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1559. #endif
  1560. bytes_read = 2;
  1561. pt_debug(cd->dev, DL_INFO,
  1562. "%s: Empty buf detected - len=0x%04X\n",
  1563. __func__, pip_len);
  1564. } else if (pip_len == 0) {
  1565. bytes_read = 0;
  1566. pt_debug(cd->dev, DL_INFO,
  1567. "%s: Sentinel detected\n", __func__);
  1568. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1569. pt_debug(cd->dev, DL_ERROR,
  1570. "%s: Illegal len=0x%04x, force %d byte read\n",
  1571. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1572. rc = pt_adap_read_default(cd, buf,
  1573. PT_MAX_PIP2_MSG_SIZE);
  1574. if (!rc)
  1575. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1576. else
  1577. bytes_read = 0;
  1578. } else {
  1579. pt_debug(cd->dev, DL_INFO,
  1580. "%s: Flush read of %d bytes...\n",
  1581. __func__, pip_len);
  1582. rc = pt_adap_read_default(cd, buf, pip_len);
  1583. if (!rc)
  1584. bytes_read = pip_len;
  1585. else
  1586. bytes_read = 0;
  1587. }
  1588. } else {
  1589. pt_debug(cd->dev, DL_INFO,
  1590. "%s: Forced flush of max %d bytes...\n",
  1591. __func__, PT_MAX_PIP2_MSG_SIZE);
  1592. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1593. if (!rc)
  1594. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1595. else
  1596. bytes_read = 0;
  1597. }
  1598. if (read_buf && (bytes_read > 3))
  1599. memcpy(read_buf, buf, bytes_read);
  1600. exit:
  1601. return bytes_read;
  1602. }
  1603. /*******************************************************************************
  1604. * FUNCTION: pt_flush_bus_if_irq_asserted
  1605. *
  1606. * SUMMARY: This function will flush the active bus if the INT is found to be
  1607. * asserted.
  1608. *
  1609. * RETURN: bytes cleared from bus
  1610. *
  1611. * PARAMETERS:
  1612. * *cd - pointer the core data structure
  1613. * flush_type - type of flush
  1614. * - PT_FLUSH_BUS_BASED_ON_LEN
  1615. * - PT_FLUSH_BUS_FULL_256_READ
  1616. ******************************************************************************/
  1617. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1618. {
  1619. int count = 0;
  1620. int bytes_read = 0;
  1621. while (pt_check_irq_asserted(cd) && count < 5) {
  1622. count++;
  1623. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1624. if (bytes_read) {
  1625. pt_debug(cd->dev, DL_WARN,
  1626. "%s: Cleared %d bytes off bus\n",
  1627. __func__, bytes_read);
  1628. }
  1629. }
  1630. if (pt_check_irq_asserted(cd)) {
  1631. pt_debug(cd->dev, DL_ERROR,
  1632. "%s: IRQ still asserted, %d bytes read\n",
  1633. __func__, bytes_read);
  1634. } else {
  1635. pt_debug(cd->dev, DL_INFO,
  1636. "%s: IRQ cleared, %d bytes read\n",
  1637. __func__, bytes_read);
  1638. }
  1639. return bytes_read;
  1640. }
  1641. /*******************************************************************************
  1642. * FUNCTION: pt_hid_send_output_
  1643. *
  1644. * SUMMARY: Send a touch application command to the DUT
  1645. *
  1646. * RETURN:
  1647. * 0 = success
  1648. * !0 = failure
  1649. *
  1650. * PARAMETERS:
  1651. * *cd - pointer to core data
  1652. * *hid_output - pointer to the command to send
  1653. ******************************************************************************/
  1654. static int pt_hid_send_output_(struct pt_core_data *cd,
  1655. struct pt_hid_output *hid_output)
  1656. {
  1657. int rc = 0;
  1658. u8 *cmd;
  1659. u16 length;
  1660. u16 crc;
  1661. u8 report_id;
  1662. u8 cmd_offset = 0;
  1663. u8 cmd_allocated = 0;
  1664. switch (hid_output->cmd_type) {
  1665. case PIP1_CMD_TYPE_FW:
  1666. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1667. length = 5;
  1668. break;
  1669. case PIP1_CMD_TYPE_BL:
  1670. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1671. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1672. break;
  1673. default:
  1674. return -EINVAL;
  1675. }
  1676. length += hid_output->write_length;
  1677. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1678. cmd = kzalloc(length + 2, GFP_KERNEL);
  1679. if (!cmd)
  1680. return -ENOMEM;
  1681. cmd_allocated = 1;
  1682. } else {
  1683. cmd = cd->cmd_buf;
  1684. }
  1685. /* Set Output register */
  1686. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1687. sizeof(cd->hid_desc.output_register));
  1688. cmd_offset += sizeof(cd->hid_desc.output_register);
  1689. cmd[cmd_offset++] = LOW_BYTE(length);
  1690. cmd[cmd_offset++] = HI_BYTE(length);
  1691. cmd[cmd_offset++] = report_id;
  1692. cmd[cmd_offset++] = 0x0; /* reserved */
  1693. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1694. cmd[cmd_offset++] = PIP1_BL_SOP;
  1695. cmd[cmd_offset++] = hid_output->command_code;
  1696. /* Set Data Length for bootloader */
  1697. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1698. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1699. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1700. }
  1701. /* Set Data */
  1702. if (hid_output->write_length && hid_output->write_buf) {
  1703. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1704. hid_output->write_length);
  1705. cmd_offset += hid_output->write_length;
  1706. }
  1707. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1708. crc = _pt_compute_crc(&cmd[6],
  1709. hid_output->write_length + 4);
  1710. cmd[cmd_offset++] = LOW_BYTE(crc);
  1711. cmd[cmd_offset++] = HI_BYTE(crc);
  1712. cmd[cmd_offset++] = PIP1_BL_EOP;
  1713. }
  1714. pt_debug(cd->dev, DL_INFO,
  1715. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1716. __func__, length + 2, hid_output->command_code);
  1717. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1718. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1719. if (rc)
  1720. pt_debug(cd->dev, DL_ERROR,
  1721. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1722. if (cmd_allocated)
  1723. kfree(cmd);
  1724. return rc;
  1725. }
  1726. /*******************************************************************************
  1727. * FUNCTION: pt_pip1_send_output_and_wait_
  1728. *
  1729. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1730. *
  1731. * RETURN:
  1732. * 0 = success
  1733. * !0 = failure
  1734. *
  1735. * PARAMETERS:
  1736. * *cd - pointer to core data
  1737. * *hid_output - pointer to the command to send
  1738. ******************************************************************************/
  1739. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1740. struct pt_hid_output *hid_output)
  1741. {
  1742. int rc = 0;
  1743. int t;
  1744. u16 timeout_ms;
  1745. mutex_lock(&cd->system_lock);
  1746. cd->hid_cmd_state = hid_output->command_code + 1;
  1747. mutex_unlock(&cd->system_lock);
  1748. if (hid_output->timeout_ms)
  1749. timeout_ms = hid_output->timeout_ms;
  1750. else
  1751. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1752. rc = pt_hid_send_output_(cd, hid_output);
  1753. if (rc)
  1754. goto error;
  1755. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1756. msecs_to_jiffies(timeout_ms));
  1757. if (IS_TMO(t)) {
  1758. #ifdef TTDL_DIAGNOSTICS
  1759. cd->bus_transmit_error_count++;
  1760. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1761. #endif /* TTDL_DIAGNOSTICS */
  1762. pt_debug(cd->dev, DL_ERROR,
  1763. "%s: HID output cmd execution timed out (%dms)\n",
  1764. __func__, timeout_ms);
  1765. rc = -ETIME;
  1766. goto error;
  1767. }
  1768. if (!hid_output->novalidate)
  1769. rc = pt_hid_output_validate_response(cd, hid_output);
  1770. pt_check_command(cd, hid_output, false);
  1771. goto exit;
  1772. error:
  1773. mutex_lock(&cd->system_lock);
  1774. cd->hid_cmd_state = 0;
  1775. mutex_unlock(&cd->system_lock);
  1776. exit:
  1777. return rc;
  1778. }
  1779. /*******************************************************************************
  1780. * FUNCTION: pt_hid_output_user_cmd_
  1781. *
  1782. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1783. * to the DUT waiting for the response and loading it into the read buffer
  1784. *
  1785. * RETURN:
  1786. * 0 = success
  1787. * !0 = failure
  1788. *
  1789. * PARAMETERS:
  1790. * *cd - pointer to core data
  1791. * read_len - expected read length of the response
  1792. * *read_buf - pointer to where the response will be loaded
  1793. * write_len - length of the write buffer
  1794. * *write_buf - pointer to the write buffer
  1795. * *actual_read_len - pointer to the actual amount of data read back
  1796. ******************************************************************************/
  1797. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1798. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1799. u16 *actual_read_len)
  1800. {
  1801. int rc = 0;
  1802. u16 size;
  1803. struct pt_hid_output hid_output = {
  1804. .length = write_len,
  1805. .write_buf = write_buf,
  1806. };
  1807. #ifdef TTHE_TUNER_SUPPORT
  1808. if (!cd->pip2_send_user_cmd) {
  1809. int command_code = 0;
  1810. int len;
  1811. /* Print up to cmd ID */
  1812. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1813. if (write_len < len)
  1814. len = write_len;
  1815. else
  1816. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1817. & PIP1_CMD_COMMAND_ID_MASK;
  1818. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1819. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1820. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1821. tthe_print(cd, write_buf, len, "CMD=");
  1822. }
  1823. #endif
  1824. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1825. if (rc)
  1826. return rc;
  1827. /* Get the response size from the first 2 bytes in the response */
  1828. size = get_unaligned_le16(&cd->response_buf[0]);
  1829. /* Ensure size is not greater than max buffer size */
  1830. if (size > PT_MAX_PIP2_MSG_SIZE)
  1831. size = PT_MAX_PIP2_MSG_SIZE;
  1832. /* Minimum size to read is the 2 byte len field */
  1833. if (size == 0)
  1834. size = 2;
  1835. if (size > read_len) {
  1836. pt_debug(cd->dev, DL_ERROR,
  1837. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1838. __func__, size, read_len);
  1839. *actual_read_len = 0;
  1840. return -EIO;
  1841. }
  1842. memcpy(read_buf, cd->response_buf, size);
  1843. *actual_read_len = size;
  1844. return 0;
  1845. }
  1846. /*******************************************************************************
  1847. * FUNCTION: pt_hid_output_user_cmd
  1848. *
  1849. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1850. * the DUT.
  1851. *
  1852. * RETURN:
  1853. * 0 = success
  1854. * !0 = failure
  1855. *
  1856. * PARAMETERS:
  1857. * *cd - pointer to core data
  1858. * read_len - length of data to read
  1859. * *read_buf - pointer to store read data
  1860. * write_len - length of data to write
  1861. * *write_buf - pointer to buffer to write
  1862. * *actual_read_len - pointer to store data length actually read
  1863. ******************************************************************************/
  1864. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1865. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1866. u16 *actual_read_len)
  1867. {
  1868. int rc;
  1869. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1870. if (rc < 0) {
  1871. pt_debug(cd->dev, DL_ERROR,
  1872. "%s: fail get exclusive ex=%p own=%p\n",
  1873. __func__, cd->exclusive_dev, cd->dev);
  1874. return rc;
  1875. }
  1876. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1877. write_len, write_buf, actual_read_len);
  1878. if (release_exclusive(cd, cd->dev) < 0)
  1879. pt_debug(cd->dev, DL_ERROR,
  1880. "%s: fail to release exclusive\n", __func__);
  1881. return rc;
  1882. }
  1883. /*******************************************************************************
  1884. * FUNCTION: _pt_request_pip2_send_cmd
  1885. *
  1886. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1887. * interrupt and reads response data to read_buf
  1888. *
  1889. * RETURN:
  1890. * 0 = success
  1891. * !0 = failure
  1892. *
  1893. * PARAMETERS:
  1894. * *dev - pointer to device structure
  1895. * protect - flag to run in protected mode
  1896. * id - ID of PIP command
  1897. * *data - pointer to PIP data payload
  1898. * report_body_len - report length
  1899. * *read_buf - pointer to response buffer
  1900. * *actual_read_len - pointer to response buffer length
  1901. ******************************************************************************/
  1902. static int _pt_request_pip2_send_cmd(struct device *dev,
  1903. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1904. u16 *actual_read_len)
  1905. {
  1906. struct pt_core_data *cd = dev_get_drvdata(dev);
  1907. struct pip2_cmd_structure pip2_cmd;
  1908. int rc = 0;
  1909. u16 i = 0;
  1910. u16 j = 0;
  1911. u16 write_len;
  1912. u8 *write_buf = NULL;
  1913. u16 read_len;
  1914. u8 extra_bytes;
  1915. if (report_body_len > 247)
  1916. return -EPROTO;
  1917. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1918. /* Hard coded register for PIP2.x */
  1919. pip2_cmd.reg[0] = 0x01;
  1920. pip2_cmd.reg[1] = 0x01;
  1921. /*
  1922. * For PIP2.1+ the length field value includes itself:
  1923. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1924. *
  1925. * The overall write length must include only the register:
  1926. * ADD 2: 2 (Register)
  1927. */
  1928. extra_bytes = 6;
  1929. write_len = 2;
  1930. /* PIP2 the CMD ID is a 7bit field */
  1931. if (id > PIP2_CMD_ID_END) {
  1932. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1933. __func__, id);
  1934. rc = -EINVAL;
  1935. goto exit;
  1936. }
  1937. pip2_cmd.len = report_body_len + extra_bytes;
  1938. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1939. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1940. pip2_cmd.data = data;
  1941. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1942. /* Add the command length to the extra bytes based on PIP version */
  1943. write_len += pip2_cmd.len;
  1944. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1945. __func__, pip2_cmd.len, write_len);
  1946. write_buf = kzalloc(write_len, GFP_KERNEL);
  1947. if (write_buf == NULL) {
  1948. rc = -ENOMEM;
  1949. goto exit;
  1950. }
  1951. write_buf[i++] = pip2_cmd.reg[0];
  1952. write_buf[i++] = pip2_cmd.reg[1];
  1953. write_buf[i++] = pip2_cmd.len & 0xff;
  1954. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1955. write_buf[i++] = pip2_cmd.seq;
  1956. write_buf[i++] = pip2_cmd.id;
  1957. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1958. write_buf[j] = pip2_cmd.data[j-i];
  1959. write_buf[j++] = pip2_cmd.crc[0];
  1960. write_buf[j++] = pip2_cmd.crc[1];
  1961. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1962. if (read_len < 0)
  1963. read_len = 255;
  1964. pt_debug(dev, DL_INFO,
  1965. "%s cmd_id[0x%02X] expected response length:%d ",
  1966. __func__, pip2_cmd.id, read_len);
  1967. /*
  1968. * All PIP2 commands come through this function.
  1969. * Set flag for PIP2.x interface to allow response parsing to know
  1970. * how to decode the protocol header.
  1971. */
  1972. mutex_lock(&cd->system_lock);
  1973. cd->pip2_prot_active = true;
  1974. cd->pip2_send_user_cmd = true;
  1975. mutex_unlock(&cd->system_lock);
  1976. if (protect == PT_CORE_CMD_PROTECTED)
  1977. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1978. write_len, write_buf, actual_read_len);
  1979. else {
  1980. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1981. write_len, write_buf, actual_read_len);
  1982. }
  1983. if (rc) {
  1984. pt_debug(dev, DL_ERROR,
  1985. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1986. __func__, rc);
  1987. goto exit;
  1988. }
  1989. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1990. *actual_read_len);
  1991. exit:
  1992. mutex_lock(&cd->system_lock);
  1993. cd->pip2_prot_active = false;
  1994. cd->pip2_send_user_cmd = false;
  1995. mutex_unlock(&cd->system_lock);
  1996. kfree(write_buf);
  1997. return rc;
  1998. }
  1999. /*******************************************************************************
  2000. * FUNCTION: _pt_pip2_send_cmd_no_int
  2001. *
  2002. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2003. * reads response data to read_buf if response is available.
  2004. *
  2005. * NOTE:
  2006. * Interrupt MUST be disabled before to call this function.
  2007. *
  2008. * RETURN:
  2009. * 0 = success
  2010. * !0 = failure
  2011. *
  2012. * PARAMETERS:
  2013. * *dev - pointer to device structure
  2014. * protect - flag to run in protected mode
  2015. * id - ID of PIP command
  2016. * *data - pointer to PIP data payload
  2017. * report_body_len - report length
  2018. * *read_buf - pointer to response buffer
  2019. * *actual_read_len - pointer to response buffer length
  2020. ******************************************************************************/
  2021. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2022. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2023. u16 *actual_read_len)
  2024. {
  2025. int max_retry = 0;
  2026. int retry = 0;
  2027. int rc = 0;
  2028. u16 i = 0;
  2029. u16 j = 0;
  2030. u16 write_len;
  2031. u8 *write_buf = NULL;
  2032. u16 read_len;
  2033. u16 size = 0;
  2034. u8 response_seq = 0;
  2035. u8 extra_bytes;
  2036. u32 retry_interval = 0;
  2037. u32 retry_total_time = 0;
  2038. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2039. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2040. struct pt_core_data *cd = dev_get_drvdata(dev);
  2041. struct pip2_cmd_structure pip2_cmd;
  2042. if (report_body_len > 247)
  2043. return -EPROTO;
  2044. if (protect == PT_CORE_CMD_PROTECTED) {
  2045. rc = request_exclusive(cd,
  2046. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2047. if (rc < 0) {
  2048. pt_debug(cd->dev, DL_ERROR,
  2049. "%s: fail get exclusive ex=%p own=%p\n",
  2050. __func__, cd->exclusive_dev, cd->dev);
  2051. return rc;
  2052. }
  2053. }
  2054. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2055. /* Hard coded register for PIP2.x */
  2056. pip2_cmd.reg[0] = 0x01;
  2057. pip2_cmd.reg[1] = 0x01;
  2058. /*
  2059. * For PIP2.1+ the length field value includes itself:
  2060. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2061. *
  2062. * The overall write length must include only the register:
  2063. * ADD 2: 2 (Register)
  2064. */
  2065. extra_bytes = 6;
  2066. write_len = 2;
  2067. pip2_cmd.len = report_body_len + extra_bytes;
  2068. pip2_cmd.id = id;
  2069. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2070. pip2_cmd.data = data;
  2071. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2072. /* Add the command length to the extra bytes based on PIP version */
  2073. write_len += pip2_cmd.len;
  2074. write_buf = kzalloc(write_len, GFP_KERNEL);
  2075. if (write_buf == NULL) {
  2076. rc = -ENOMEM;
  2077. goto exit;
  2078. }
  2079. write_buf[i++] = pip2_cmd.reg[0];
  2080. write_buf[i++] = pip2_cmd.reg[1];
  2081. write_buf[i++] = pip2_cmd.len & 0xff;
  2082. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2083. write_buf[i++] = pip2_cmd.seq;
  2084. write_buf[i++] = pip2_cmd.id;
  2085. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2086. write_buf[j] = pip2_cmd.data[j-i];
  2087. write_buf[j++] = pip2_cmd.crc[0];
  2088. write_buf[j++] = pip2_cmd.crc[1];
  2089. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2090. if (read_len < 0)
  2091. read_len = 255;
  2092. pt_debug(dev, DL_INFO,
  2093. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2094. __func__, pip2_cmd.id, read_len);
  2095. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2096. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2097. if (rc) {
  2098. pt_debug(dev, DL_ERROR,
  2099. "%s: SPI write Error = %d\n",
  2100. __func__, rc);
  2101. goto exit;
  2102. }
  2103. #ifdef PT_POLL_RESP_BY_BUS
  2104. /*
  2105. * Frequent bus read can increase system load obviously. The expected
  2106. * first bus read should be valid and timely. The tollerance for
  2107. * usleep_range should be limited. The minimum response delay (between
  2108. * command finishes sending and INT pin falls) is less than 50
  2109. * microseconds. So the 10 microseconds should be maximum tollerance
  2110. * with the consideration that the unit to calculate the response delay
  2111. * is 10 microseconds and more precise is not necessary. Every
  2112. * additional 10 microseconds only contribute less than 3 milliseconds
  2113. * for whole BL.
  2114. */
  2115. usleep_range(resp_time_min, resp_time_min+10);
  2116. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2117. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2118. rc = pt_adap_read_default(cd, read_buf, read_len);
  2119. if (rc) {
  2120. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2121. __func__, rc);
  2122. break;
  2123. }
  2124. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2125. size = get_unaligned_le16(&read_buf[0]);
  2126. if ((size <= read_len) &&
  2127. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2128. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2129. break;
  2130. }
  2131. /*
  2132. * To reduce the bus and system load, increase the sleep
  2133. * step gradually:
  2134. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2135. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2136. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2137. */
  2138. retry++;
  2139. if (retry < 20) {
  2140. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2141. usleep_range(retry_interval,
  2142. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2143. } else if (retry < 40) {
  2144. retry_interval += 1000;
  2145. usleep_range(retry_interval, retry_interval + 1000);
  2146. } else {
  2147. retry_interval += 50000;
  2148. msleep(retry_interval/1000);
  2149. }
  2150. retry_total_time += retry_interval;
  2151. }
  2152. #else
  2153. /*
  2154. * Frequent GPIO read will not increase CPU/system load heavily if the
  2155. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2156. * fixed interval: 20 us.
  2157. */
  2158. usleep_range(resp_time_min, resp_time_min+10);
  2159. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2160. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2161. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2162. rc = pt_adap_read_default(cd, read_buf, read_len);
  2163. size = get_unaligned_le16(&read_buf[0]);
  2164. if (rc)
  2165. pt_debug(dev, DL_ERROR,
  2166. "%s: SPI read Error = %d\n",
  2167. __func__, rc);
  2168. else if (size > read_len) {
  2169. pt_debug(cd->dev, DL_ERROR,
  2170. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2171. __func__, size, read_len);
  2172. rc = -EIO;
  2173. }
  2174. break;
  2175. }
  2176. /*
  2177. * Poll GPIO with fixed interval 20 us, and tollerance is
  2178. * limited to 10 us to speed up the process.
  2179. */
  2180. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2181. usleep_range(retry_interval, retry_interval+10);
  2182. retry_total_time += retry_interval;
  2183. }
  2184. #endif
  2185. *actual_read_len = size;
  2186. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2187. pt_debug(dev, DL_ERROR,
  2188. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2189. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2190. *actual_read_len = 0;
  2191. rc = -EINVAL;
  2192. }
  2193. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2194. "<<< NO_INT Read");
  2195. exit:
  2196. kfree(write_buf);
  2197. if (protect == PT_CORE_CMD_PROTECTED) {
  2198. if (release_exclusive(cd, cd->dev) < 0)
  2199. pt_debug(cd->dev, DL_ERROR,
  2200. "%s: fail to release exclusive\n", __func__);
  2201. }
  2202. return rc;
  2203. }
  2204. /*******************************************************************************
  2205. * FUNCTION: pt_pip_null_
  2206. *
  2207. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2208. * This function is used by watchdog to check if the fw corrupts.
  2209. *
  2210. * RETURN:
  2211. * 0 = success
  2212. * !0 = failure
  2213. *
  2214. * PARAMETERS:
  2215. * *cd - pointer to core data
  2216. ******************************************************************************/
  2217. static int pt_pip_null_(struct pt_core_data *cd)
  2218. {
  2219. struct pt_hid_output hid_output = {
  2220. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2221. };
  2222. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2223. }
  2224. /*******************************************************************************
  2225. * FUNCTION: pt_pip_null
  2226. *
  2227. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2228. *
  2229. * RETURN:
  2230. * 0 = success
  2231. * !0 = failure
  2232. *
  2233. * PARAMETERS:
  2234. * *cd - pointer to core data
  2235. ******************************************************************************/
  2236. static int pt_pip_null(struct pt_core_data *cd)
  2237. {
  2238. int rc;
  2239. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2240. if (rc < 0) {
  2241. pt_debug(cd->dev, DL_ERROR,
  2242. "%s: fail get exclusive ex=%p own=%p\n",
  2243. __func__, cd->exclusive_dev, cd->dev);
  2244. return rc;
  2245. }
  2246. rc = pt_pip_null_(cd);
  2247. if (release_exclusive(cd, cd->dev) < 0)
  2248. pt_debug(cd->dev, DL_ERROR,
  2249. "%s: fail to release exclusive\n", __func__);
  2250. return rc;
  2251. }
  2252. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2253. /*******************************************************************************
  2254. * FUNCTION: pt_pip_start_bootloader_
  2255. *
  2256. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2257. *
  2258. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2259. * the WD active could cause this function to fail!
  2260. *
  2261. * RETURN:
  2262. * 0 = success
  2263. * !0 = failure
  2264. *
  2265. * PARAMETERS:
  2266. * *cd - pointer to core data
  2267. ******************************************************************************/
  2268. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2269. {
  2270. int rc = 0;
  2271. struct pt_hid_output hid_output = {
  2272. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2273. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2274. .reset_expected = 1,
  2275. };
  2276. if (cd->watchdog_enabled) {
  2277. pt_debug(cd->dev, DL_WARN,
  2278. "%s: watchdog isn't stopped before enter bl\n",
  2279. __func__);
  2280. goto exit;
  2281. }
  2282. /* Reset startup status after entering BL, new DUT enum required */
  2283. cd->startup_status = STARTUP_STATUS_START;
  2284. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2285. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2286. if (rc) {
  2287. pt_debug(cd->dev, DL_ERROR,
  2288. "%s: Start BL PIP cmd failed. rc = %d\n",
  2289. __func__, rc);
  2290. }
  2291. exit:
  2292. return rc;
  2293. }
  2294. /*******************************************************************************
  2295. * FUNCTION: pt_pip_start_bootloader
  2296. *
  2297. * SUMMARY: Protected function to force DUT to enter the BL
  2298. *
  2299. * RETURN:
  2300. * 0 = success
  2301. * !0 = failure
  2302. *
  2303. * PARAMETERS:
  2304. * *cd - pointer to core data structure
  2305. ******************************************************************************/
  2306. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2307. {
  2308. int rc;
  2309. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2310. if (rc < 0) {
  2311. pt_debug(cd->dev, DL_ERROR,
  2312. "%s: fail get exclusive ex=%p own=%p\n",
  2313. __func__, cd->exclusive_dev, cd->dev);
  2314. return rc;
  2315. }
  2316. rc = pt_pip_start_bootloader_(cd);
  2317. if (release_exclusive(cd, cd->dev) < 0)
  2318. pt_debug(cd->dev, DL_ERROR,
  2319. "%s: fail to release exclusive\n", __func__);
  2320. return rc;
  2321. }
  2322. /*******************************************************************************
  2323. * FUNCTION: _pt_request_pip_start_bl
  2324. *
  2325. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2326. * modules to request the DUT to enter the BL
  2327. *
  2328. * RETURN:
  2329. * 0 = success
  2330. * !0 = failure
  2331. *
  2332. * PARAMETERS:
  2333. * *dev - pointer to device structure
  2334. * protect - flag to run in protected mode
  2335. ******************************************************************************/
  2336. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2337. {
  2338. struct pt_core_data *cd = dev_get_drvdata(dev);
  2339. if (protect)
  2340. return pt_pip_start_bootloader(cd);
  2341. return pt_pip_start_bootloader_(cd);
  2342. }
  2343. /*******************************************************************************
  2344. * FUNCTION: pt_pip2_ver_load_ttdata
  2345. *
  2346. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2347. * command into the core data struct.
  2348. *
  2349. * RETURN: n/a
  2350. *
  2351. * PARAMETERS:
  2352. * *cd - pointer to core data structure
  2353. * len - Length of data in response_buf
  2354. ******************************************************************************/
  2355. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2356. {
  2357. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2358. struct pt_pip2_version_full *full_ver;
  2359. struct pt_pip2_version *ver;
  2360. /*
  2361. * The PIP2 VERSION command can return different lengths of data.
  2362. * The additional LOT fields are included when the packet
  2363. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2364. * NOTE:
  2365. * - The FW would swap the BL and FW versions when reporting
  2366. * the small packet.
  2367. * - Sub Lot bytes 16 and 17 are reserved.
  2368. */
  2369. if (len >= 0x1D) {
  2370. full_ver = (struct pt_pip2_version_full *)
  2371. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2372. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2373. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2374. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2375. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2376. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2377. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2378. /*
  2379. * BL PIP 2.02 and greater the version fields are
  2380. * swapped
  2381. */
  2382. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2383. ttdata->chip_rev =
  2384. get_unaligned_le16(&full_ver->chip_rev);
  2385. ttdata->chip_id =
  2386. get_unaligned_le16(&full_ver->chip_id);
  2387. } else {
  2388. ttdata->chip_rev =
  2389. get_unaligned_le16(&full_ver->chip_id);
  2390. ttdata->chip_id =
  2391. get_unaligned_le16(&full_ver->chip_rev);
  2392. }
  2393. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2394. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2395. sizeof(struct pt_pip2_version_full),
  2396. "PIP2 VERSION FULL");
  2397. } else {
  2398. ver = (struct pt_pip2_version *)
  2399. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2400. ttdata->pip_ver_major = ver->pip2_version_msb;
  2401. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2402. ttdata->bl_ver_major = ver->bl_version_msb;
  2403. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2404. ttdata->fw_ver_major = ver->fw_version_msb;
  2405. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2406. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2407. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2408. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2409. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2410. }
  2411. }
  2412. /*******************************************************************************
  2413. * FUNCTION: pt_si_get_ttdata
  2414. *
  2415. * SUMMARY: Function to load the version information from the system information
  2416. * PIP command into the core data struct.
  2417. *
  2418. * RETURN: n/a
  2419. *
  2420. * PARAMETERS:
  2421. * *cd - pointer to core data structure
  2422. ******************************************************************************/
  2423. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2424. {
  2425. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2426. struct pt_ttdata_dev *ttdata_dev =
  2427. (struct pt_ttdata_dev *)
  2428. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2429. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2430. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2431. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2432. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2433. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2434. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2435. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2436. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2437. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2438. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2439. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2440. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2441. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2442. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2443. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2444. }
  2445. /*******************************************************************************
  2446. * FUNCTION: pt_si_get_sensing_conf_data
  2447. *
  2448. * SUMMARY: Function to load the sensing information from the system information
  2449. * PIP command into the core data struct.
  2450. *
  2451. * RETURN: n/a
  2452. *
  2453. * PARAMETERS:
  2454. * *cd - pointer to core data structure
  2455. ******************************************************************************/
  2456. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2457. {
  2458. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2459. struct pt_sensing_conf_data_dev *scd_dev =
  2460. (struct pt_sensing_conf_data_dev *)
  2461. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2462. scd->electrodes_x = scd_dev->electrodes_x;
  2463. scd->electrodes_y = scd_dev->electrodes_y;
  2464. scd->origin_x = scd_dev->origin_x;
  2465. scd->origin_y = scd_dev->origin_y;
  2466. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2467. if (scd->origin_x & 0x02) {
  2468. scd->tx_num = scd->electrodes_x;
  2469. scd->rx_num = scd->electrodes_y;
  2470. } else {
  2471. scd->tx_num = scd->electrodes_y;
  2472. scd->rx_num = scd->electrodes_x;
  2473. }
  2474. /*
  2475. * When the Panel ID is coming from an XY pin and not a dedicated
  2476. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2477. * other DUTs as the loader will use cd->pid_for_loader to generate
  2478. * the bin file name but will ignore it if pid_for_loader is still
  2479. * set to PANEL_ID_NOT_ENABLED
  2480. */
  2481. if (cd->panel_id_support &
  2482. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2483. mutex_lock(&cd->system_lock);
  2484. cd->pid_for_loader = scd_dev->panel_id;
  2485. mutex_unlock(&cd->system_lock);
  2486. }
  2487. scd->panel_id = scd_dev->panel_id;
  2488. scd->btn = scd_dev->btn;
  2489. scd->scan_mode = scd_dev->scan_mode;
  2490. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2491. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2492. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2493. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2494. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2495. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2496. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2497. sizeof(struct pt_sensing_conf_data_dev),
  2498. "sensing_conf_data");
  2499. }
  2500. /*******************************************************************************
  2501. * FUNCTION: pt_si_setup
  2502. *
  2503. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2504. *
  2505. * RETURN:
  2506. * 0 = success
  2507. * !0 = failure
  2508. *
  2509. * PARAMETERS:
  2510. * *cd - pointer to core data structure
  2511. ******************************************************************************/
  2512. static int pt_si_setup(struct pt_core_data *cd)
  2513. {
  2514. struct pt_sysinfo *si = &cd->sysinfo;
  2515. int max_tch = si->sensing_conf_data.max_tch;
  2516. if (!si->xy_data)
  2517. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2518. GFP_KERNEL);
  2519. if (!si->xy_data)
  2520. return -ENOMEM;
  2521. if (!si->xy_mode)
  2522. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2523. if (!si->xy_mode) {
  2524. kfree(si->xy_data);
  2525. return -ENOMEM;
  2526. }
  2527. return 0;
  2528. }
  2529. /*******************************************************************************
  2530. * FUNCTION: pt_si_get_btn_data
  2531. *
  2532. * SUMMARY: Setup the core data button information based on the response of the
  2533. * System Information PIP command.
  2534. *
  2535. * RETURN:
  2536. * 0 = success
  2537. * !0 = failure
  2538. *
  2539. * PARAMETERS:
  2540. * *cd - pointer to core data structure
  2541. ******************************************************************************/
  2542. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2543. {
  2544. struct pt_sysinfo *si = &cd->sysinfo;
  2545. int num_btns = 0;
  2546. int num_defined_keys;
  2547. u16 *key_table;
  2548. int btn;
  2549. int i;
  2550. int rc = 0;
  2551. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2552. & PIP1_SYSINFO_BTN_MASK;
  2553. size_t btn_keys_size;
  2554. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2555. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2556. if (btns & (1 << i))
  2557. num_btns++;
  2558. }
  2559. si->num_btns = num_btns;
  2560. if (num_btns) {
  2561. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2562. if (!si->btn)
  2563. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2564. if (!si->btn)
  2565. return -ENOMEM;
  2566. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2567. num_defined_keys = 0;
  2568. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2569. num_defined_keys = 0;
  2570. else
  2571. num_defined_keys = cd->cpdata->sett
  2572. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2573. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2574. key_table = (u16 *)cd->cpdata->sett
  2575. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2576. si->btn[btn].key_code = key_table[btn];
  2577. si->btn[btn].enabled = true;
  2578. }
  2579. for (; btn < num_btns; btn++) {
  2580. si->btn[btn].key_code = KEY_RESERVED;
  2581. si->btn[btn].enabled = true;
  2582. }
  2583. return rc;
  2584. }
  2585. kfree(si->btn);
  2586. si->btn = NULL;
  2587. return rc;
  2588. }
  2589. /*******************************************************************************
  2590. * FUNCTION: pt_si_put_log_data
  2591. *
  2592. * SUMMARY: Prints all sys info data to kmsg log
  2593. *
  2594. * RETURN: n/a
  2595. *
  2596. * PARAMETERS:
  2597. * *cd - pointer to core data structure
  2598. ******************************************************************************/
  2599. static void pt_si_put_log_data(struct pt_core_data *cd)
  2600. {
  2601. struct pt_sysinfo *si = &cd->sysinfo;
  2602. struct pt_ttdata *ttdata = &si->ttdata;
  2603. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2604. int i;
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2606. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2608. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2610. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2611. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2612. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2614. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2616. __func__, ttdata->revctrl, ttdata->revctrl);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2618. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2620. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2622. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2624. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2626. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2627. for (i = 0; i < PT_NUM_MFGID; i++)
  2628. pt_debug(cd->dev, DL_DEBUG,
  2629. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2630. __func__, i, ttdata->mfg_id[i],
  2631. ttdata->mfg_id[i]);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2633. __func__, ttdata->post_code, ttdata->post_code);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2635. __func__, scd->electrodes_x, scd->electrodes_x);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2637. __func__, scd->electrodes_y, scd->electrodes_y);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2639. __func__, scd->len_x, scd->len_x);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2641. __func__, scd->len_y, scd->len_y);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2643. __func__, scd->res_x, scd->res_x);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2645. __func__, scd->res_y, scd->res_y);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2647. __func__, scd->max_z, scd->max_z);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2649. __func__, scd->origin_x, scd->origin_x);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2651. __func__, scd->origin_y, scd->origin_y);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2653. __func__, scd->panel_id, scd->panel_id);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2655. __func__, scd->btn, scd->btn);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2657. __func__, scd->scan_mode, scd->scan_mode);
  2658. pt_debug(cd->dev, DL_DEBUG,
  2659. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2660. __func__, scd->max_tch, scd->max_tch);
  2661. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2662. __func__, si->xy_mode);
  2663. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2664. __func__, si->xy_data);
  2665. }
  2666. /*******************************************************************************
  2667. * FUNCTION: pt_get_sysinfo_regs
  2668. *
  2669. * SUMMARY: Setup all the core data System information based on the response
  2670. * of the System Information PIP command.
  2671. *
  2672. * RETURN:
  2673. * 0 = success
  2674. * !0 = failure
  2675. *
  2676. * PARAMETERS:
  2677. * *cd - pointer to core data structure
  2678. ******************************************************************************/
  2679. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2680. {
  2681. struct pt_sysinfo *si = &cd->sysinfo;
  2682. int rc;
  2683. rc = pt_si_get_btn_data(cd);
  2684. if (rc < 0)
  2685. return rc;
  2686. pt_si_get_ttdata(cd);
  2687. pt_si_get_sensing_conf_data(cd);
  2688. pt_si_setup(cd);
  2689. pt_si_put_log_data(cd);
  2690. si->ready = true;
  2691. return rc;
  2692. }
  2693. /*******************************************************************************
  2694. * FUNCTION: pt_free_si_ptrs
  2695. *
  2696. * SUMMARY: Frees all memory associated with the System Information within
  2697. * core data
  2698. *
  2699. * RETURN: n/a
  2700. *
  2701. * PARAMETERS:
  2702. * *cd - pointer to core data structure
  2703. ******************************************************************************/
  2704. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2705. {
  2706. struct pt_sysinfo *si = &cd->sysinfo;
  2707. kfree(si->btn);
  2708. kfree(si->xy_mode);
  2709. kfree(si->xy_data);
  2710. }
  2711. /*******************************************************************************
  2712. * FUNCTION: pt_hid_output_get_sysinfo_
  2713. *
  2714. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2715. * response.
  2716. *
  2717. * RETURN::
  2718. * 0 = success
  2719. * !0 = failure
  2720. *
  2721. * PARAMETERS:
  2722. * *cd - pointer to core data structure
  2723. ******************************************************************************/
  2724. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2725. {
  2726. int rc = 0;
  2727. struct pt_hid_output hid_output = {
  2728. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2729. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2730. };
  2731. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2732. if (rc)
  2733. return rc;
  2734. /* Parse the sysinfo data */
  2735. rc = pt_get_sysinfo_regs(cd);
  2736. if (rc)
  2737. pt_free_si_ptrs(cd);
  2738. return rc;
  2739. }
  2740. /*******************************************************************************
  2741. * FUNCTION: pt_hid_output_get_sysinfo
  2742. *
  2743. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2744. *
  2745. * RETURN::
  2746. * 0 = success
  2747. * !0 = failure
  2748. *
  2749. * PARAMETERS:
  2750. * *cd - pointer to core data structure
  2751. ******************************************************************************/
  2752. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2753. {
  2754. int rc;
  2755. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2756. if (rc < 0) {
  2757. pt_debug(cd->dev, DL_ERROR,
  2758. "%s: fail get exclusive ex=%p own=%p\n",
  2759. __func__, cd->exclusive_dev, cd->dev);
  2760. return rc;
  2761. }
  2762. rc = pt_hid_output_get_sysinfo_(cd);
  2763. if (release_exclusive(cd, cd->dev) < 0)
  2764. pt_debug(cd->dev, DL_ERROR,
  2765. "%s: fail to release exclusive\n", __func__);
  2766. return rc;
  2767. }
  2768. /*******************************************************************************
  2769. * FUNCTION: pt_pip_suspend_scanning_
  2770. *
  2771. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2772. *
  2773. * RETURN::
  2774. * 0 = success
  2775. * !0 = failure
  2776. *
  2777. * PARAMETERS:
  2778. * *cd - pointer to core data structure
  2779. ******************************************************************************/
  2780. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2781. {
  2782. int rc = 0;
  2783. struct pt_hid_output hid_output = {
  2784. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2785. };
  2786. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2787. if (rc) {
  2788. pt_debug(cd->dev, DL_ERROR,
  2789. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2790. __func__, rc);
  2791. }
  2792. return rc;
  2793. }
  2794. /*******************************************************************************
  2795. * FUNCTION: pt_pip_suspend_scanning
  2796. *
  2797. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2798. *
  2799. * RETURN::
  2800. * 0 = success
  2801. * !0 = failure
  2802. *
  2803. * PARAMETERS:
  2804. * *cd - pointer to core data structure
  2805. ******************************************************************************/
  2806. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2807. {
  2808. int rc;
  2809. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2810. if (rc < 0) {
  2811. pt_debug(cd->dev, DL_ERROR,
  2812. "%s: fail get exclusive ex=%p own=%p\n",
  2813. __func__, cd->exclusive_dev, cd->dev);
  2814. return rc;
  2815. }
  2816. rc = pt_pip_suspend_scanning_(cd);
  2817. if (release_exclusive(cd, cd->dev) < 0)
  2818. pt_debug(cd->dev, DL_ERROR,
  2819. "%s: fail to release exclusive\n", __func__);
  2820. return rc;
  2821. }
  2822. /*******************************************************************************
  2823. * FUNCTION: _pt_request_pip_suspend_scanning
  2824. *
  2825. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2826. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2827. *
  2828. * RETURN:
  2829. * 0 = success
  2830. * !0 = failure
  2831. *
  2832. * PARAMETERS:
  2833. * *dev - pointer to device structure
  2834. * protect - 0 = call non-protected function
  2835. * 1 = call protected function
  2836. ******************************************************************************/
  2837. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2838. int protect)
  2839. {
  2840. struct pt_core_data *cd = dev_get_drvdata(dev);
  2841. if (protect)
  2842. return pt_pip_suspend_scanning(cd);
  2843. return pt_pip_suspend_scanning_(cd);
  2844. }
  2845. /*******************************************************************************
  2846. * FUNCTION: pt_pip_resume_scanning_
  2847. *
  2848. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2849. *
  2850. * RETURN::
  2851. * 0 = success
  2852. * !0 = failure
  2853. *
  2854. * PARAMETERS:
  2855. * *cd - pointer to core data structure
  2856. ******************************************************************************/
  2857. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2858. {
  2859. int rc = 0;
  2860. struct pt_hid_output hid_output = {
  2861. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2862. };
  2863. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2864. if (rc) {
  2865. pt_debug(cd->dev, DL_ERROR,
  2866. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2867. __func__, rc);
  2868. }
  2869. return rc;
  2870. }
  2871. /*******************************************************************************
  2872. * FUNCTION: pt_pip_resume_scanning
  2873. *
  2874. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2875. *
  2876. * RETURN::
  2877. * 0 = success
  2878. * !0 = failure
  2879. *
  2880. * PARAMETERS:
  2881. * *cd - pointer to core data structure
  2882. ******************************************************************************/
  2883. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2884. {
  2885. int rc;
  2886. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2887. if (rc < 0) {
  2888. pt_debug(cd->dev, DL_ERROR,
  2889. "%s: fail get exclusive ex=%p own=%p\n",
  2890. __func__, cd->exclusive_dev, cd->dev);
  2891. return rc;
  2892. }
  2893. rc = pt_pip_resume_scanning_(cd);
  2894. if (release_exclusive(cd, cd->dev) < 0)
  2895. pt_debug(cd->dev, DL_ERROR,
  2896. "%s: fail to release exclusive\n", __func__);
  2897. return rc;
  2898. }
  2899. /*******************************************************************************
  2900. * FUNCTION: _pt_request_pip_resume_scanning
  2901. *
  2902. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2903. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2904. *
  2905. * RETURN::
  2906. * 0 = success
  2907. * !0 = failure
  2908. *
  2909. * PARAMETERS:
  2910. * *dev - pointer to device structure
  2911. * protect - 0 = call non-protected function
  2912. * 1 = call protected function
  2913. ******************************************************************************/
  2914. static int _pt_request_pip_resume_scanning(struct device *dev,
  2915. int protect)
  2916. {
  2917. struct pt_core_data *cd = dev_get_drvdata(dev);
  2918. if (protect)
  2919. return pt_pip_resume_scanning(cd);
  2920. return pt_pip_resume_scanning_(cd);
  2921. }
  2922. /*******************************************************************************
  2923. * FUNCTION: pt_pip_get_param_
  2924. *
  2925. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2926. * the 32bit parameter value
  2927. *
  2928. * RETURN:
  2929. * 0 = success
  2930. * !0 = failure
  2931. *
  2932. * PARAMETERS:
  2933. * *cd - pointer to core data
  2934. * param_id - parameter ID to retrieve
  2935. * *value - value of DUT parameter
  2936. ******************************************************************************/
  2937. static int pt_pip_get_param_(struct pt_core_data *cd,
  2938. u8 param_id, u32 *value)
  2939. {
  2940. int write_length = 1;
  2941. u8 param[1] = { param_id };
  2942. u8 read_param_id;
  2943. int param_size;
  2944. u8 *ptr;
  2945. int rc = 0;
  2946. int i;
  2947. struct pt_hid_output hid_output = {
  2948. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2949. .write_length = write_length,
  2950. .write_buf = param,
  2951. };
  2952. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2953. if (rc)
  2954. return rc;
  2955. read_param_id = cd->response_buf[5];
  2956. if (read_param_id != param_id)
  2957. return -EPROTO;
  2958. param_size = cd->response_buf[6];
  2959. ptr = &cd->response_buf[7];
  2960. *value = 0;
  2961. for (i = 0; i < param_size; i++)
  2962. *value += ptr[i] << (i * 8);
  2963. return 0;
  2964. }
  2965. /*******************************************************************************
  2966. * FUNCTION: pt_pip_get_param
  2967. *
  2968. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2969. *
  2970. * RETURN:
  2971. * 0 = success
  2972. * !0 = failure
  2973. *
  2974. * PARAMETERS:
  2975. * *cd - pointer to core data
  2976. * param_id - parameter ID to retrieve
  2977. * *value - value of DUT parameter
  2978. ******************************************************************************/
  2979. static int pt_pip_get_param(struct pt_core_data *cd,
  2980. u8 param_id, u32 *value)
  2981. {
  2982. int rc;
  2983. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2984. if (rc < 0) {
  2985. pt_debug(cd->dev, DL_ERROR,
  2986. "%s: fail get exclusive ex=%p own=%p\n",
  2987. __func__, cd->exclusive_dev, cd->dev);
  2988. return rc;
  2989. }
  2990. rc = pt_pip_get_param_(cd, param_id, value);
  2991. if (release_exclusive(cd, cd->dev) < 0)
  2992. pt_debug(cd->dev, DL_ERROR,
  2993. "%s: fail to release exclusive\n", __func__);
  2994. return rc;
  2995. }
  2996. /*******************************************************************************
  2997. * FUNCTION: _pt_request_pip_get_param
  2998. *
  2999. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3000. * calls to the protected or unprotected call to pt_pip_get_param
  3001. *
  3002. * RETURN:
  3003. * 0 = success
  3004. * !0 = failure
  3005. *
  3006. * PARAMETERS:
  3007. * *dev - pointer to device structure
  3008. * protect - flag to call protected or non protected function
  3009. * param_id - parameter ID to retrieve
  3010. * *value - value of DUT parameter
  3011. ******************************************************************************/
  3012. int _pt_request_pip_get_param(struct device *dev,
  3013. int protect, u8 param_id, u32 *value)
  3014. {
  3015. struct pt_core_data *cd = dev_get_drvdata(dev);
  3016. if (protect)
  3017. return pt_pip_get_param(cd, param_id, value);
  3018. return pt_pip_get_param_(cd, param_id, value);
  3019. }
  3020. /*******************************************************************************
  3021. * FUNCTION: pt_pip_set_param_
  3022. *
  3023. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3024. * passed in value to flash
  3025. *
  3026. * RETURN:
  3027. * 0 = success
  3028. * !0 = failure
  3029. *
  3030. * PARAMETERS:
  3031. * *cd - pointer to core data
  3032. * param_id - parameter ID to set
  3033. * value - value to write
  3034. * size - size to write
  3035. ******************************************************************************/
  3036. static int pt_pip_set_param_(struct pt_core_data *cd,
  3037. u8 param_id, u32 value, u8 size)
  3038. {
  3039. u8 write_buf[6];
  3040. u8 *ptr = &write_buf[2];
  3041. int rc = 0;
  3042. int i;
  3043. struct pt_hid_output hid_output = {
  3044. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3045. .write_buf = write_buf,
  3046. };
  3047. write_buf[0] = param_id;
  3048. write_buf[1] = size;
  3049. for (i = 0; i < size; i++) {
  3050. ptr[i] = value & 0xFF;
  3051. value = value >> 8;
  3052. }
  3053. hid_output.write_length = 2 + size;
  3054. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3055. if (rc)
  3056. return rc;
  3057. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3058. return -EPROTO;
  3059. return 0;
  3060. }
  3061. /*******************************************************************************
  3062. * FUNCTION: pt_pip_set_param
  3063. *
  3064. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3065. *
  3066. * RETURN:
  3067. * 0 = success
  3068. * !0 = failure
  3069. *
  3070. * PARAMETERS:
  3071. * *cd - pointer to core data
  3072. * param_id - parameter ID to set
  3073. * value - value to write
  3074. * size - size to write
  3075. ******************************************************************************/
  3076. static int pt_pip_set_param(struct pt_core_data *cd,
  3077. u8 param_id, u32 value, u8 size)
  3078. {
  3079. int rc;
  3080. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3081. if (rc < 0) {
  3082. pt_debug(cd->dev, DL_ERROR,
  3083. "%s: fail get exclusive ex=%p own=%p\n",
  3084. __func__, cd->exclusive_dev, cd->dev);
  3085. return rc;
  3086. }
  3087. rc = pt_pip_set_param_(cd, param_id, value, size);
  3088. if (release_exclusive(cd, cd->dev) < 0)
  3089. pt_debug(cd->dev, DL_ERROR,
  3090. "%s: fail to release exclusive\n", __func__);
  3091. return rc;
  3092. }
  3093. /*******************************************************************************
  3094. * FUNCTION: _pt_request_pip_set_param
  3095. *
  3096. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3097. * calls to the protected or unprotected call to pt_pip_set_param
  3098. *
  3099. * RETURN:
  3100. * 0 = success
  3101. * !0 = failure
  3102. *
  3103. * PARAMETERS:
  3104. * *dev - pointer to device structure
  3105. * protect - flag to call protected or non-protected
  3106. * param_id - parameter ID to set
  3107. * value - value to write
  3108. * size - size to write
  3109. ******************************************************************************/
  3110. int _pt_request_pip_set_param(struct device *dev, int protect,
  3111. u8 param_id, u32 value, u8 size)
  3112. {
  3113. struct pt_core_data *cd = dev_get_drvdata(dev);
  3114. if (protect)
  3115. return pt_pip_set_param(cd, param_id, value, size);
  3116. return pt_pip_set_param_(cd, param_id, value, size);
  3117. }
  3118. /*******************************************************************************
  3119. * FUNCTION: _pt_pip_enter_easywake_state_
  3120. *
  3121. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3122. *
  3123. * RETURN:
  3124. * 0 = success
  3125. * !0 = failure
  3126. *
  3127. * PARAMETERS:
  3128. * *dev - pointer to device structure
  3129. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3130. * *return_data - return status if easywake was entered
  3131. ******************************************************************************/
  3132. static int pt_hid_output_enter_easywake_state_(
  3133. struct pt_core_data *cd, u8 data, u8 *return_data)
  3134. {
  3135. int write_length = 1;
  3136. u8 param[1] = { data };
  3137. int rc = 0;
  3138. struct pt_hid_output hid_output = {
  3139. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3140. .write_length = write_length,
  3141. .write_buf = param,
  3142. };
  3143. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3144. if (rc)
  3145. return rc;
  3146. *return_data = cd->response_buf[5];
  3147. return rc;
  3148. }
  3149. /*******************************************************************************
  3150. * FUNCTION: pt_pip_verify_config_block_crc_
  3151. *
  3152. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3153. *
  3154. * RETURN:
  3155. * 0 = success
  3156. * !0 = failure
  3157. *
  3158. * PARAMETERS:
  3159. * *cd - pointer the core data structure
  3160. * ebid - enumerated block ID
  3161. * *status - PIP command status
  3162. * calculated_crc - calculated CRC
  3163. * stored_crc - stored CRC in config area
  3164. ******************************************************************************/
  3165. static int pt_pip_verify_config_block_crc_(
  3166. struct pt_core_data *cd, u8 ebid, u8 *status,
  3167. u16 *calculated_crc, u16 *stored_crc)
  3168. {
  3169. int write_length = 1;
  3170. u8 param[1] = { ebid };
  3171. u8 *ptr;
  3172. int rc = 0;
  3173. struct pt_hid_output hid_output = {
  3174. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3175. .write_length = write_length,
  3176. .write_buf = param,
  3177. };
  3178. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3179. if (rc)
  3180. return rc;
  3181. ptr = &cd->response_buf[5];
  3182. *status = ptr[0];
  3183. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3184. *stored_crc = get_unaligned_le16(&ptr[3]);
  3185. return 0;
  3186. }
  3187. /*******************************************************************************
  3188. * FUNCTION: pt_pip_verify_config_block_crc
  3189. *
  3190. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3191. * an exclusive access to the DUT.
  3192. *
  3193. * RETURN:
  3194. * 0 = success
  3195. * !0 = failure
  3196. *
  3197. * PARAMETERS:
  3198. * *cd - pointer the core data structure
  3199. * ebid - enumerated block ID
  3200. * *status - PIP command status
  3201. * calculated_crc - calculated CRC
  3202. * stored_crc - stored CRC in config area
  3203. ******************************************************************************/
  3204. static int pt_pip_verify_config_block_crc(
  3205. struct pt_core_data *cd, u8 ebid, u8 *status,
  3206. u16 *calculated_crc, u16 *stored_crc)
  3207. {
  3208. int rc;
  3209. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3210. if (rc < 0) {
  3211. pt_debug(cd->dev, DL_ERROR,
  3212. "%s: fail get exclusive ex=%p own=%p\n",
  3213. __func__, cd->exclusive_dev, cd->dev);
  3214. return rc;
  3215. }
  3216. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3217. calculated_crc, stored_crc);
  3218. if (release_exclusive(cd, cd->dev) < 0)
  3219. pt_debug(cd->dev, DL_ERROR,
  3220. "%s: fail to release exclusive\n", __func__);
  3221. return rc;
  3222. }
  3223. /*******************************************************************************
  3224. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3225. *
  3226. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3227. * calls to the protected or unprotected call to
  3228. * pt_pip_verify_config_block_crc_
  3229. *
  3230. * RETURN:
  3231. * 0 = success
  3232. * !0 = failure
  3233. *
  3234. * PARAMETERS:
  3235. * *dev - pointer to device structure
  3236. * protect - flag to call protected or non-protected
  3237. * ebid - enumerated block ID
  3238. * *status - PIP command status
  3239. * calculated_crc - calculated CRC
  3240. * stored_crc - stored CRC in config area
  3241. ******************************************************************************/
  3242. static int _pt_request_pip_verify_config_block_crc(
  3243. struct device *dev, int protect, u8 ebid, u8 *status,
  3244. u16 *calculated_crc, u16 *stored_crc)
  3245. {
  3246. struct pt_core_data *cd = dev_get_drvdata(dev);
  3247. if (protect)
  3248. return pt_pip_verify_config_block_crc(cd, ebid,
  3249. status, calculated_crc, stored_crc);
  3250. return pt_pip_verify_config_block_crc_(cd, ebid,
  3251. status, calculated_crc, stored_crc);
  3252. }
  3253. /*******************************************************************************
  3254. * FUNCTION: pt_pip_get_config_row_size_
  3255. *
  3256. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3257. *
  3258. * RETURN:
  3259. * 0 = success
  3260. * !0 = failure
  3261. *
  3262. * PARAMETERS:
  3263. * *cd - pointer to core data
  3264. * protect - flag to call protected or non-protected
  3265. * *row_size - pointer to store the retrieved row size
  3266. ******************************************************************************/
  3267. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3268. u16 *row_size)
  3269. {
  3270. int rc = 0;
  3271. struct pt_hid_output hid_output = {
  3272. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3273. };
  3274. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3275. if (rc)
  3276. return rc;
  3277. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3278. return 0;
  3279. }
  3280. /*******************************************************************************
  3281. * FUNCTION: pt_pip_get_config_row_size
  3282. *
  3283. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3284. * an exclusive access to the DUT.
  3285. *
  3286. * RETURN:
  3287. * 0 = success
  3288. * !0 = failure
  3289. *
  3290. * PARAMETERS:
  3291. * *cd - pointer to core data
  3292. * protect - flag to call protected or non-protected
  3293. * *row_size - pointer to store the retrieved row size
  3294. ******************************************************************************/
  3295. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3296. u16 *row_size)
  3297. {
  3298. int rc;
  3299. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3300. if (rc < 0) {
  3301. pt_debug(cd->dev, DL_ERROR,
  3302. "%s: fail get exclusive ex=%p own=%p\n",
  3303. __func__, cd->exclusive_dev, cd->dev);
  3304. return rc;
  3305. }
  3306. rc = pt_pip_get_config_row_size_(cd, row_size);
  3307. if (release_exclusive(cd, cd->dev) < 0)
  3308. pt_debug(cd->dev, DL_ERROR,
  3309. "%s: fail to release exclusive\n", __func__);
  3310. return rc;
  3311. }
  3312. /*******************************************************************************
  3313. * FUNCTION: _pt_request_pip_get_config_row_size
  3314. *
  3315. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3316. * calls to the protected or unprotected call to
  3317. * pt_pip_get_config_row_size_
  3318. *
  3319. * RETURN:
  3320. * 0 = success
  3321. * !0 = failure
  3322. *
  3323. * PARAMETERS:
  3324. * *dev - pointer to device structure
  3325. * protect - flag to call protected or non-protected
  3326. * *row_size - pointer to store the retrieved row size
  3327. ******************************************************************************/
  3328. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3329. int protect, u16 *row_size)
  3330. {
  3331. struct pt_core_data *cd = dev_get_drvdata(dev);
  3332. if (protect)
  3333. return pt_pip_get_config_row_size(cd, row_size);
  3334. return pt_pip_get_config_row_size_(cd, row_size);
  3335. }
  3336. /*******************************************************************************
  3337. * FUNCTION: pt_pip1_read_data_block_
  3338. *
  3339. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3340. * output data to the "read_buf" and update "crc".
  3341. *
  3342. * RETURN:
  3343. * 0 = success
  3344. * !0 = failure
  3345. *
  3346. * PARAMETERS:
  3347. * *cd - pointer to core data
  3348. * row_number - row number
  3349. * length - length of data to read
  3350. * ebid - block id
  3351. * *actual_read_len - Actual data length read
  3352. * *read_buf - pointer to the buffer to store read data
  3353. * read_buf_size - size of read_buf
  3354. * *crc - pointer to store CRC of row data
  3355. ******************************************************************************/
  3356. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3357. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3358. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3359. {
  3360. int read_ebid;
  3361. int status;
  3362. int rc = 0;
  3363. int write_length = 5;
  3364. u8 write_buf[5];
  3365. u8 cmd_offset = 0;
  3366. u16 calc_crc;
  3367. struct pt_hid_output hid_output = {
  3368. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3369. .write_length = write_length,
  3370. .write_buf = write_buf,
  3371. };
  3372. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3373. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3374. write_buf[cmd_offset++] = LOW_BYTE(length);
  3375. write_buf[cmd_offset++] = HI_BYTE(length);
  3376. write_buf[cmd_offset++] = ebid;
  3377. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3378. if (rc)
  3379. return rc;
  3380. status = cd->response_buf[5];
  3381. if (status)
  3382. return status;
  3383. read_ebid = cd->response_buf[6];
  3384. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3385. return -EPROTO;
  3386. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3387. if (length == 0 || *actual_read_len == 0)
  3388. return 0;
  3389. if (read_buf_size >= *actual_read_len &&
  3390. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3391. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3392. else
  3393. return -EPROTO;
  3394. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3395. /* Validate Row Data CRC */
  3396. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3397. if (*crc == calc_crc) {
  3398. return 0;
  3399. } else {
  3400. pt_debug(cd->dev, DL_ERROR,
  3401. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3402. __func__, *crc, calc_crc);
  3403. return -EPROTO;
  3404. }
  3405. }
  3406. /*******************************************************************************
  3407. * FUNCTION: _pt_request_pip_read_data_block
  3408. *
  3409. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3410. * calls to pt_pip1_read_data_block_
  3411. *
  3412. * RETURN:
  3413. * 0 = success
  3414. * !0 = failure
  3415. *
  3416. * PARAMETERS:
  3417. * *dev - pointer to device structure
  3418. * row_number - row number
  3419. * length - length of data to read
  3420. * ebid - block id
  3421. * *actual_read_len - Actual data length read
  3422. * *read_buf - pointer to the buffer to store read data
  3423. * *crc - pointer to store CRC of row data
  3424. ******************************************************************************/
  3425. static int _pt_request_pip_read_data_block(struct device *dev,
  3426. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3427. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3428. {
  3429. struct pt_core_data *cd = dev_get_drvdata(dev);
  3430. return pt_pip1_read_data_block_(cd, row_number, length,
  3431. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3432. }
  3433. /*******************************************************************************
  3434. * FUNCTION: pt_pip1_write_data_block_
  3435. *
  3436. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3437. * write data to the data block.
  3438. *
  3439. * RETURN:
  3440. * 0 = success
  3441. * !0 = failure
  3442. *
  3443. * PARAMETERS:
  3444. * *cd - pointer to core data
  3445. * row_number - row in config block to write to
  3446. * write_length - length of data to write
  3447. * ebid - enumerated block ID
  3448. * *write_buf - pointer to buffer to write
  3449. * *security_key - pointer to security key to allow write
  3450. * *actual_write_len - pointer to store data length actually written
  3451. ******************************************************************************/
  3452. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3453. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3454. u8 *security_key, u16 *actual_write_len)
  3455. {
  3456. /* row_number + write_len + ebid + security_key + crc */
  3457. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3458. u8 *full_write_buf;
  3459. u8 cmd_offset = 0;
  3460. u16 crc;
  3461. int status;
  3462. int rc = 0;
  3463. int read_ebid;
  3464. u8 *data;
  3465. struct pt_hid_output hid_output = {
  3466. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3467. .write_length = full_write_length,
  3468. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3469. };
  3470. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3471. return -EINVAL;
  3472. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3473. if (!full_write_buf)
  3474. return -ENOMEM;
  3475. hid_output.write_buf = full_write_buf;
  3476. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3477. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3478. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3479. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3480. full_write_buf[cmd_offset++] = ebid;
  3481. data = &full_write_buf[cmd_offset];
  3482. memcpy(data, write_buf, write_length);
  3483. cmd_offset += write_length;
  3484. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3485. cmd_offset += 8;
  3486. crc = _pt_compute_crc(data, write_length);
  3487. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3488. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3489. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3490. if (rc)
  3491. goto exit;
  3492. status = cd->response_buf[5];
  3493. if (status) {
  3494. rc = -EINVAL;
  3495. goto exit;
  3496. }
  3497. read_ebid = cd->response_buf[6];
  3498. if (read_ebid != ebid) {
  3499. rc = -EPROTO;
  3500. goto exit;
  3501. }
  3502. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3503. exit:
  3504. kfree(full_write_buf);
  3505. return rc;
  3506. }
  3507. /*******************************************************************************
  3508. * FUNCTION: _pt_request_pip_write_data_block
  3509. *
  3510. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3511. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3512. *
  3513. * RETURN:
  3514. * 0 = success
  3515. * !0 = failure
  3516. *
  3517. * PARAMETERS:
  3518. * *dev - pointer to device structure
  3519. * protect - flag to call protected or non-protected
  3520. * row_number - row in config block to write to
  3521. * write_length - length of data to write
  3522. * ebid - enumerated block ID
  3523. * *write_buf - pointer to buffer to write
  3524. * *security_key - pointer to security key to allow write
  3525. * *actual_write_len - pointer to store data length actually written
  3526. ******************************************************************************/
  3527. static int _pt_request_pip_write_data_block(struct device *dev,
  3528. u16 row_number, u16 write_length, u8 ebid,
  3529. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3530. {
  3531. struct pt_core_data *cd = dev_get_drvdata(dev);
  3532. return pt_pip1_write_data_block_(cd, row_number,
  3533. write_length, ebid, write_buf, security_key,
  3534. actual_write_len);
  3535. }
  3536. /*******************************************************************************
  3537. * FUNCTION: pt_pip_get_data_structure_
  3538. *
  3539. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3540. * returning a structure of data defined by data_id
  3541. *
  3542. * RETURN:
  3543. * 0 = success
  3544. * !0 = failure
  3545. *
  3546. * PARAMETERS:
  3547. * *cd - pointer to core data
  3548. * read_offset - read pointer offset
  3549. * read_length - length of data to read
  3550. * data_id - data ID to read
  3551. * *status - pointer to store the read response status
  3552. * *data_format - pointer to store format of data read
  3553. * *actual_read_len - pointer to store data length actually read
  3554. * *data - pointer to store data read
  3555. ******************************************************************************/
  3556. static int pt_pip_get_data_structure_(
  3557. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3558. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3559. u8 *data)
  3560. {
  3561. int rc = 0;
  3562. u16 total_read_len = 0;
  3563. u16 read_len;
  3564. u16 off_buf = 0;
  3565. u8 write_buf[5];
  3566. u8 read_data_id;
  3567. struct pt_hid_output hid_output = {
  3568. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3569. .write_length = 5,
  3570. .write_buf = write_buf,
  3571. };
  3572. again:
  3573. write_buf[0] = LOW_BYTE(read_offset);
  3574. write_buf[1] = HI_BYTE(read_offset);
  3575. write_buf[2] = LOW_BYTE(read_length);
  3576. write_buf[3] = HI_BYTE(read_length);
  3577. write_buf[4] = data_id;
  3578. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3579. if (rc)
  3580. return rc;
  3581. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3582. goto set_status;
  3583. read_data_id = cd->response_buf[6];
  3584. if (read_data_id != data_id)
  3585. return -EPROTO;
  3586. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3587. if (read_len && data) {
  3588. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3589. total_read_len += read_len;
  3590. if (read_len < read_length) {
  3591. read_offset += read_len;
  3592. off_buf += read_len;
  3593. read_length -= read_len;
  3594. goto again;
  3595. }
  3596. }
  3597. if (data_format)
  3598. *data_format = cd->response_buf[9];
  3599. if (actual_read_len)
  3600. *actual_read_len = total_read_len;
  3601. set_status:
  3602. if (status)
  3603. *status = cd->response_buf[5];
  3604. return rc;
  3605. }
  3606. /*******************************************************************************
  3607. * FUNCTION: pt_pip_get_data_structure
  3608. *
  3609. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3610. * an exclusive access to the DUT.
  3611. *
  3612. * RETURN:
  3613. * 0 = success
  3614. * !0 = failure
  3615. *
  3616. * PARAMETERS:
  3617. * *cd - pointer to core data
  3618. * read_offset - read pointer offset
  3619. * read_length - length of data to read
  3620. * data_id - data ID to read
  3621. * *status - pointer to store the read response status
  3622. * *data_format - pointer to store format of data read
  3623. * *actual_read_len - pointer to store data length actually read
  3624. * *data - pointer to store data read
  3625. ******************************************************************************/
  3626. static int pt_pip_get_data_structure(
  3627. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3628. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3629. u8 *data)
  3630. {
  3631. int rc;
  3632. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3633. if (rc < 0) {
  3634. pt_debug(cd->dev, DL_ERROR,
  3635. "%s: fail get exclusive ex=%p own=%p\n",
  3636. __func__, cd->exclusive_dev, cd->dev);
  3637. return rc;
  3638. }
  3639. rc = pt_pip_get_data_structure_(cd, read_offset,
  3640. read_length, data_id, status, data_format,
  3641. actual_read_len, data);
  3642. if (release_exclusive(cd, cd->dev) < 0)
  3643. pt_debug(cd->dev, DL_ERROR,
  3644. "%s: fail to release exclusive\n", __func__);
  3645. return rc;
  3646. }
  3647. /*******************************************************************************
  3648. * FUNCTION: _pt_request_pip_get_data_structure
  3649. *
  3650. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3651. * calls to the protected or unprotected call to
  3652. * pt_pip_get_data_structure
  3653. *
  3654. * RETURN:
  3655. * 0 = success
  3656. * !0 = failure
  3657. *
  3658. * PARAMETERS:
  3659. * *dev - pointer to device structure
  3660. * protect - flag to call protected or non-protected
  3661. * read_offset - read pointer offset
  3662. * read_length - length of data to read
  3663. * data_id - data ID to read
  3664. * *status - pointer to store the read response status
  3665. * *data_format - pointer to store format of data read
  3666. * *actual_read_len - pointer to store data length actually read
  3667. * *data - pointer to store data read
  3668. ******************************************************************************/
  3669. static int _pt_request_pip_get_data_structure(struct device *dev,
  3670. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3671. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3672. {
  3673. struct pt_core_data *cd = dev_get_drvdata(dev);
  3674. if (protect)
  3675. return pt_pip_get_data_structure(cd,
  3676. read_offset, read_length, data_id, status,
  3677. data_format, actual_read_len, data);
  3678. return pt_pip_get_data_structure_(cd,
  3679. read_offset, read_length, data_id, status,
  3680. data_format, actual_read_len, data);
  3681. }
  3682. /*******************************************************************************
  3683. * FUNCTION: _pt_manage_local_cal_data
  3684. *
  3685. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3686. * CALIBRATION data. It stores it in a local static array and can be
  3687. * cleared, loaded or used to restore the CAL data back to the running FW.
  3688. * The CAL data is read or restored by use of the PIP1 commands:
  3689. * - READ_DATA_BLOCK (0x22)
  3690. * - WRITE_DATA_BLOCK (0x23)
  3691. *
  3692. * RETURN:
  3693. * 0 = success
  3694. * !0 = failure
  3695. *
  3696. * PARAMETERS:
  3697. * *dev - pointer to device structure
  3698. * action - One of the following actions:
  3699. * - PT_CAL_DATA_SAVE
  3700. * - PT_CAL_DATA_RESTORE
  3701. * - PT_CAL_DATA_CLEAR
  3702. * - PT_CAL_DATA_SIZE
  3703. * *size - pointer to the number of bytes transferred
  3704. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3705. ******************************************************************************/
  3706. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3707. unsigned short *crc)
  3708. {
  3709. struct pt_core_data *cd = dev_get_drvdata(dev);
  3710. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3711. unsigned short calc_id_crc = 0;
  3712. static u8 *cal_cache_data;
  3713. static u16 cal_cache_len;
  3714. static unsigned short cal_cache_chip_id;
  3715. int rc = 0;
  3716. u8 *tmp_data = NULL;
  3717. u8 row_number = 0;
  3718. u8 prefix[20];
  3719. u16 cal_size = 0;
  3720. u16 transfer_size;
  3721. u16 act_trans_len = 0;
  3722. u16 byte_offset = 0;
  3723. u16 cal_blk_size;
  3724. u16 total_rows;
  3725. u16 remain_bytes;
  3726. u16 data_block_crc;
  3727. u16 buf_size = 12;
  3728. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3729. __func__, action);
  3730. switch (action) {
  3731. case PT_CAL_DATA_SAVE:
  3732. /* Read the size of the CAL block and calculate # rows */
  3733. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3734. if (!tmp_data) {
  3735. rc = -ENOMEM;
  3736. goto exit;
  3737. }
  3738. /*
  3739. * Don't check rc as doing a read size will give a false
  3740. * error on the CRC check.
  3741. */
  3742. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3743. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3744. cal_blk_size = act_trans_len;
  3745. kfree(tmp_data);
  3746. pt_debug(dev, DL_INFO,
  3747. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3748. __func__, cal_cache_len, cal_blk_size);
  3749. /* Safety net to ensure we didn't read incorrect size */
  3750. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3751. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3752. __func__);
  3753. rc = 1;
  3754. goto exit;
  3755. }
  3756. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3757. if (cal_blk_size != cal_cache_len) {
  3758. kfree(cal_cache_data);
  3759. cal_cache_data = kzalloc(cal_blk_size + 2,
  3760. GFP_KERNEL);
  3761. if (!cal_cache_data) {
  3762. rc = -ENOMEM;
  3763. goto exit;
  3764. }
  3765. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3766. __func__);
  3767. }
  3768. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3769. /* Calculate how many rows [0-n] (PIP Transactions) */
  3770. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3771. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3772. /* Add row if we have a last partial row */
  3773. if (remain_bytes > 0)
  3774. total_rows++;
  3775. pt_debug(dev, DL_INFO,
  3776. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3777. __func__, cal_blk_size, total_rows, remain_bytes);
  3778. /* Read all rows unless an error occurs */
  3779. rc = 0;
  3780. while (rc == 0 && row_number <= total_rows) {
  3781. act_trans_len = 0;
  3782. if (remain_bytes > 0 && row_number == total_rows)
  3783. transfer_size = remain_bytes;
  3784. else
  3785. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3786. rc = pt_pip1_read_data_block_(cd, row_number,
  3787. transfer_size, PT_CAL_EBID,
  3788. &act_trans_len,
  3789. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3790. &data_block_crc);
  3791. if (rc) {
  3792. /* Error occurred, exit loop */
  3793. cal_size = 0;
  3794. break;
  3795. }
  3796. pt_debug(dev, DL_INFO,
  3797. "%s: CAL read rc=%d actual read len=%d\n",
  3798. __func__, rc, act_trans_len);
  3799. byte_offset += act_trans_len;
  3800. cal_size = byte_offset;
  3801. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3802. pt_pr_buf(dev, DL_INFO,
  3803. &cal_cache_data[byte_offset - act_trans_len],
  3804. act_trans_len, prefix);
  3805. row_number++;
  3806. }
  3807. if (cal_size > 0) {
  3808. /* Save a CRC of the chip info the CAL was saved from */
  3809. calc_id_crc = crc_ccitt_calculate(
  3810. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3811. cal_cache_chip_id = calc_id_crc;
  3812. cal_cache_len = cal_size;
  3813. pt_debug(dev, DL_INFO,
  3814. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3815. __func__, calc_id_crc, cal_size);
  3816. }
  3817. *size = cal_size;
  3818. *crc = calc_id_crc;
  3819. break;
  3820. case PT_CAL_DATA_RESTORE:
  3821. cal_size = cal_cache_len;
  3822. while ((rc == 0) && (byte_offset < cal_size)) {
  3823. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3824. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3825. else
  3826. transfer_size = cal_size - byte_offset;
  3827. rc = pt_pip1_write_data_block_(cd, row_number,
  3828. transfer_size, PT_CAL_EBID,
  3829. &cal_cache_data[byte_offset],
  3830. (u8 *)pt_data_block_security_key,
  3831. &act_trans_len);
  3832. byte_offset += act_trans_len;
  3833. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3834. __func__, byte_offset);
  3835. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3836. pt_pr_buf(dev, DL_INFO,
  3837. &cal_cache_data[byte_offset - act_trans_len],
  3838. act_trans_len, prefix);
  3839. if ((byte_offset > cal_size) ||
  3840. (act_trans_len != transfer_size))
  3841. rc = -EIO;
  3842. row_number++;
  3843. }
  3844. *size = byte_offset;
  3845. *crc = cal_cache_chip_id;
  3846. break;
  3847. case PT_CAL_DATA_CLEAR:
  3848. if (cal_cache_data)
  3849. memset(&cal_cache_data[0], 0, cal_cache_len);
  3850. cal_cache_len = 0;
  3851. cal_cache_chip_id = 0;
  3852. *size = 0;
  3853. *crc = 0;
  3854. break;
  3855. case PT_CAL_DATA_INFO:
  3856. default:
  3857. *size = cal_cache_len;
  3858. *crc = cal_cache_chip_id;
  3859. pt_debug(dev, DL_INFO,
  3860. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3861. __func__, cal_cache_chip_id,
  3862. cal_cache_len);
  3863. break;
  3864. }
  3865. exit:
  3866. pt_debug(dev, DL_INFO,
  3867. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3868. __func__, rc, *crc, *size);
  3869. return rc;
  3870. }
  3871. /*******************************************************************************
  3872. * FUNCTION: pt_pip_run_selftest_
  3873. *
  3874. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3875. * to execute a FW built in self test
  3876. *
  3877. * RETURN:
  3878. * 0 = success
  3879. * !0 = failure
  3880. *
  3881. * PARAMETERS:
  3882. * *cd - pointer to core data
  3883. * test_id - enumerated test ID to run
  3884. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3885. * *status - pointer to store the read response status
  3886. * *summary_results - pointer to store the results summary
  3887. * *results_available - pointer to store if results are available
  3888. *****************************************************************************/
  3889. static int pt_pip_run_selftest_(
  3890. struct pt_core_data *cd, u8 test_id,
  3891. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3892. u8 *results_available)
  3893. {
  3894. int rc = 0;
  3895. u8 write_buf[2];
  3896. struct pt_hid_output hid_output = {
  3897. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3898. .write_length = 2,
  3899. .write_buf = write_buf,
  3900. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3901. };
  3902. write_buf[0] = test_id;
  3903. write_buf[1] = write_idacs_to_flash;
  3904. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3905. hid_output.write_length = 1;
  3906. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3907. if (rc)
  3908. return rc;
  3909. if (status)
  3910. *status = cd->response_buf[5];
  3911. if (summary_result)
  3912. *summary_result = cd->response_buf[6];
  3913. /* results_available only available before PIP 1.03 */
  3914. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3915. if (results_available)
  3916. *results_available = cd->response_buf[7];
  3917. }
  3918. return rc;
  3919. }
  3920. /*******************************************************************************
  3921. * FUNCTION: pt_pip_run_selftest
  3922. *
  3923. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3924. * an exclusive access to the DUT.
  3925. *
  3926. * RETURN:
  3927. * 0 = success
  3928. * !0 = failure
  3929. *
  3930. * PARAMETERS:
  3931. * *cd - pointer to core data
  3932. * test_id - enumerated test ID to run
  3933. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3934. * *status - pointer to store the read response status
  3935. * *summary_results - pointer to store the results summary
  3936. * *results_available - pointer to store if results are available
  3937. ******************************************************************************/
  3938. static int pt_pip_run_selftest(
  3939. struct pt_core_data *cd, u8 test_id,
  3940. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3941. u8 *results_available)
  3942. {
  3943. int rc;
  3944. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3945. if (rc < 0) {
  3946. pt_debug(cd->dev, DL_ERROR,
  3947. "%s: fail get exclusive ex=%p own=%p\n",
  3948. __func__, cd->exclusive_dev, cd->dev);
  3949. return rc;
  3950. }
  3951. rc = pt_pip_run_selftest_(cd, test_id,
  3952. write_idacs_to_flash, status, summary_result,
  3953. results_available);
  3954. if (release_exclusive(cd, cd->dev) < 0)
  3955. pt_debug(cd->dev, DL_ERROR,
  3956. "%s: fail to release exclusive\n", __func__);
  3957. return rc;
  3958. }
  3959. /*******************************************************************************
  3960. * FUNCTION: _pt_request_pip_run_selftest
  3961. *
  3962. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3963. * calls to the protected or unprotected call to pt_pip_run_selftest
  3964. *
  3965. * RETURN:
  3966. * 0 = success
  3967. * !0 = failure
  3968. *
  3969. * PARAMETERS:
  3970. * *dev - pointer to device structure
  3971. * protect - flag to call protected or non-protected
  3972. * test_id - enumerated test ID to run
  3973. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3974. * *status - pointer to store the read response status
  3975. * *summary_results - pointer to store the results summary
  3976. * *results_available - pointer to store if results are available
  3977. ******************************************************************************/
  3978. static int _pt_request_pip_run_selftest(struct device *dev,
  3979. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3980. u8 *summary_result, u8 *results_available)
  3981. {
  3982. struct pt_core_data *cd = dev_get_drvdata(dev);
  3983. if (protect)
  3984. return pt_pip_run_selftest(cd, test_id,
  3985. write_idacs_to_flash, status, summary_result,
  3986. results_available);
  3987. return pt_pip_run_selftest_(cd, test_id,
  3988. write_idacs_to_flash, status, summary_result,
  3989. results_available);
  3990. }
  3991. /*******************************************************************************
  3992. * FUNCTION: _pt_pip_get_selftest_result_
  3993. *
  3994. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3995. * to retrieve the self test results from the self test already executed
  3996. *
  3997. * RETURN:
  3998. * 0 = success
  3999. * !0 = failure
  4000. *
  4001. * PARAMETERS:
  4002. * *cd - pointer to core data
  4003. * read_offset - read pointer offset
  4004. * read_length - length of data to read
  4005. * test_id - enumerated test ID to read selftest results from
  4006. * *status - pointer to store the read response status
  4007. * *actual_read_len - pointer to store data length actually read
  4008. * *status - pointer to where the cmd response statas is stored
  4009. ******************************************************************************/
  4010. static int pt_pip_get_selftest_result_(
  4011. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4012. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4013. {
  4014. int rc = 0;
  4015. u16 total_read_len = 0;
  4016. u16 read_len;
  4017. u16 off_buf = 0;
  4018. u8 write_buf[5];
  4019. u8 read_test_id;
  4020. bool repeat;
  4021. struct pt_hid_output hid_output = {
  4022. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4023. .write_length = 5,
  4024. .write_buf = write_buf,
  4025. };
  4026. /*
  4027. * Do not repeat reading for Auto Shorts test
  4028. * when PIP version < 1.3
  4029. */
  4030. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4031. || test_id != PT_ST_ID_AUTOSHORTS;
  4032. again:
  4033. write_buf[0] = LOW_BYTE(read_offset);
  4034. write_buf[1] = HI_BYTE(read_offset);
  4035. write_buf[2] = LOW_BYTE(read_length);
  4036. write_buf[3] = HI_BYTE(read_length);
  4037. write_buf[4] = test_id;
  4038. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4039. if (rc)
  4040. return rc;
  4041. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4042. goto set_status;
  4043. read_test_id = cd->response_buf[6];
  4044. if (read_test_id != test_id)
  4045. return -EPROTO;
  4046. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4047. if (read_len && data) {
  4048. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4049. total_read_len += read_len;
  4050. if (repeat && read_len < read_length) {
  4051. read_offset += read_len;
  4052. off_buf += read_len;
  4053. read_length -= read_len;
  4054. goto again;
  4055. }
  4056. }
  4057. if (actual_read_len)
  4058. *actual_read_len = total_read_len;
  4059. set_status:
  4060. if (status)
  4061. *status = cd->response_buf[5];
  4062. return rc;
  4063. }
  4064. /*******************************************************************************
  4065. * FUNCTION: _pt_pip_get_selftest_result
  4066. *
  4067. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4068. * access to the DUT
  4069. *
  4070. * RETURN:
  4071. * 0 = success
  4072. * !0 = failure
  4073. *
  4074. * PARAMETERS:
  4075. * *cd - pointer to core data
  4076. * read_offset - read pointer offset
  4077. * read_length - length of data to read
  4078. * test_id - enumerated test ID to read selftest results from
  4079. * *status - pointer to store the read response status
  4080. * *actual_read_len - pointer to store data length actually read
  4081. * *status - pointer to where the cmd response statas is stored
  4082. ******************************************************************************/
  4083. static int pt_pip_get_selftest_result(
  4084. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4085. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4086. {
  4087. int rc;
  4088. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4089. if (rc < 0) {
  4090. pt_debug(cd->dev, DL_ERROR,
  4091. "%s: fail get exclusive ex=%p own=%p\n",
  4092. __func__, cd->exclusive_dev, cd->dev);
  4093. return rc;
  4094. }
  4095. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4096. read_length, test_id, status, actual_read_len, data);
  4097. if (release_exclusive(cd, cd->dev) < 0)
  4098. pt_debug(cd->dev, DL_ERROR,
  4099. "%s: fail to release exclusive\n", __func__);
  4100. return rc;
  4101. }
  4102. /*******************************************************************************
  4103. * FUNCTION: _pt_request_pip_get_selftest_result
  4104. *
  4105. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4106. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4107. *
  4108. * RETURN:
  4109. * 0 = success
  4110. * !0 = failure
  4111. *
  4112. * PARAMETERS:
  4113. * *dev - pointer to device structure
  4114. * protect - flag to call protected or non-protected
  4115. * read_offset - read pointer offset
  4116. * read_length - length of data to read
  4117. * test_id - enumerated test ID to read selftest results from
  4118. * *status - pointer to store the read response status
  4119. * *actual_read_len - pointer to store data length actually read
  4120. * *data - pointer to where the data read is stored
  4121. ******************************************************************************/
  4122. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4123. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4124. u8 *status, u16 *actual_read_len, u8 *data)
  4125. {
  4126. struct pt_core_data *cd = dev_get_drvdata(dev);
  4127. if (protect)
  4128. return pt_pip_get_selftest_result(cd, read_offset,
  4129. read_length, test_id, status, actual_read_len,
  4130. data);
  4131. return pt_pip_get_selftest_result_(cd, read_offset,
  4132. read_length, test_id, status, actual_read_len,
  4133. data);
  4134. }
  4135. /*******************************************************************************
  4136. * FUNCTION: _pt_pip_load_self_test_param
  4137. *
  4138. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4139. * to load parameters needed by a self test
  4140. *
  4141. * RETURN:
  4142. * 0 = success
  4143. * !0 = failure
  4144. *
  4145. * PARAMETERS:
  4146. * *cd - pointer to core data
  4147. * self_test_id - enumerated test ID for which the parmeters belong
  4148. * load_offset - mem offset to where to load parameters
  4149. * load_length - length of parameter data to load
  4150. * *parameters - pointer to list of parameter data
  4151. * *status - pointer to store the response status
  4152. * *ret_test_id - pointer to returned test id the parameters were stored
  4153. * *act_load_len - pointer to store the actual load length that was writen
  4154. ******************************************************************************/
  4155. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4156. u8 self_test_id, u16 load_offset, u16 load_length,
  4157. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4158. {
  4159. int rc = 0;
  4160. int i;
  4161. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4162. struct pt_hid_output hid_output = {
  4163. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4164. .write_length = 5 + load_length,
  4165. .write_buf = write_buf,
  4166. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4167. };
  4168. write_buf[0] = LOW_BYTE(load_offset);
  4169. write_buf[1] = HI_BYTE(load_offset);
  4170. write_buf[2] = LOW_BYTE(load_length);
  4171. write_buf[3] = HI_BYTE(load_length);
  4172. write_buf[4] = self_test_id;
  4173. for (i = 0; i < load_length; i++)
  4174. write_buf[i + 5] = parameters[i];
  4175. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4176. if (rc)
  4177. return rc;
  4178. if (status)
  4179. *status = cd->response_buf[5];
  4180. if (ret_test_id)
  4181. *ret_test_id = cd->response_buf[6];
  4182. if (act_load_len)
  4183. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4184. return rc;
  4185. }
  4186. /*******************************************************************************
  4187. * FUNCTION: pt_pip_load_self_test_param
  4188. *
  4189. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4190. * access to the DUT.
  4191. *
  4192. * RETURN:
  4193. * 0 = success
  4194. * !0 = failure
  4195. *
  4196. * PARAMETERS:
  4197. * *cd - pointer to core data
  4198. * self_test_id - enumerated test ID for which the parmeters belong
  4199. * load_offset - mem offset to where to load parameters
  4200. * load_length - length of parameter data to load
  4201. * *parameters - pointer to list of parameter data
  4202. * *status - pointer to store the response status
  4203. * *ret_test_id - pointer to returned test id the parameters were stored
  4204. * *act_load_len - pointer to store the actual load length that was writen
  4205. ******************************************************************************/
  4206. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4207. u8 self_test_id, u16 load_offset, u16 load_length,
  4208. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4209. {
  4210. int rc;
  4211. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4212. if (rc < 0) {
  4213. pt_debug(cd->dev, DL_ERROR,
  4214. "%s: fail get exclusive ex=%p own=%p\n",
  4215. __func__, cd->exclusive_dev, cd->dev);
  4216. return rc;
  4217. }
  4218. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4219. load_length, parameters, status, ret_test_id, act_load_len);
  4220. if (release_exclusive(cd, cd->dev) < 0)
  4221. pt_debug(cd->dev, DL_ERROR,
  4222. "%s: fail to release exclusive\n", __func__);
  4223. return rc;
  4224. }
  4225. /*******************************************************************************
  4226. * FUNCTION: _pt_request_pip_load_self_test_param
  4227. *
  4228. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4229. * calls to the protected or unprotected call to
  4230. * pt_pip_load_self_test_param
  4231. *
  4232. * RETURN:
  4233. * 0 = success
  4234. * !0 = failure
  4235. *
  4236. * PARAMETERS:
  4237. * *dev - pointer to device structure
  4238. * protect - flag to call protected or non-protected
  4239. * self_test_id - enumerated test ID for which the parmeters belong
  4240. * load_offset - mem offset to where to load parameters
  4241. * load_length - length of parameter data to load
  4242. * *parameters - pointer to list of parameter data
  4243. * *status - pointer to store the response status
  4244. * *ret_test_id - pointer to returned test id the parameters were stored
  4245. * *act_load_len - pointer to store the actual load length that was writen
  4246. ******************************************************************************/
  4247. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4248. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4249. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4250. {
  4251. struct pt_core_data *cd = dev_get_drvdata(dev);
  4252. if (protect)
  4253. return pt_pip_load_self_test_param(cd, self_test_id,
  4254. load_offset, load_length, parameters, status, ret_test_id,
  4255. act_load_len);
  4256. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4257. load_length, parameters, status, ret_test_id, act_load_len);
  4258. }
  4259. /*******************************************************************************
  4260. * FUNCTION: pt_pip_calibrate_ext_
  4261. *
  4262. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4263. * for the response
  4264. *
  4265. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4266. *
  4267. * RETURN:
  4268. * 0 = success
  4269. * !0 = failure
  4270. *
  4271. * PARAMETERS:
  4272. * *cd - pointer to core data
  4273. * *cal_data - pointer to extended calibration data structure
  4274. * *status - pointer to where the command response status is stored
  4275. ******************************************************************************/
  4276. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4277. struct pt_cal_ext_data *cal_data, u8 *status)
  4278. {
  4279. int rc = 0;
  4280. int write_length = 4;
  4281. u8 write_buf[4];
  4282. u16 size = 0;
  4283. unsigned short crc = 0;
  4284. struct pt_hid_output hid_output = {
  4285. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4286. .write_length = write_length,
  4287. .write_buf = write_buf,
  4288. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4289. };
  4290. if (cal_data == NULL)
  4291. return -EINVAL;
  4292. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4293. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4294. if (rc)
  4295. return rc;
  4296. if (status)
  4297. *status = cd->response_buf[5];
  4298. /*
  4299. * When doing a calibration on a flashless DUT, save CAL data in
  4300. * the TTDL cache on any successful calibration
  4301. */
  4302. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4303. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4304. __func__);
  4305. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4306. &size, &crc);
  4307. if (rc)
  4308. pt_debug(cd->dev, DL_ERROR,
  4309. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4310. else
  4311. pt_debug(cd->dev, DL_INFO,
  4312. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4313. __func__, crc, size);
  4314. }
  4315. return 0;
  4316. }
  4317. /*******************************************************************************
  4318. * FUNCTION: pt_pip_calibrate_ext
  4319. *
  4320. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4321. * DUT
  4322. *
  4323. * RETURN:
  4324. * 0 = success
  4325. * !0 = failure
  4326. *
  4327. * PARAMETERS:
  4328. * *cd - pointer to core data
  4329. * *cal_data - pointer to extended calibration data structure
  4330. * *status - pointer to where the command response status is stored
  4331. ******************************************************************************/
  4332. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4333. struct pt_cal_ext_data *cal_data, u8 *status)
  4334. {
  4335. int rc;
  4336. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4337. if (rc < 0) {
  4338. pt_debug(cd->dev, DL_ERROR,
  4339. "%s: fail get exclusive ex=%p own=%p\n",
  4340. __func__, cd->exclusive_dev, cd->dev);
  4341. return rc;
  4342. }
  4343. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4344. if (release_exclusive(cd, cd->dev) < 0)
  4345. pt_debug(cd->dev, DL_ERROR,
  4346. "%s: fail to release exclusive\n", __func__);
  4347. return rc;
  4348. }
  4349. /*******************************************************************************
  4350. * FUNCTION: _pt_request_pip_calibrate_ext
  4351. *
  4352. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4353. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4354. *
  4355. * RETURN:
  4356. * 0 = success
  4357. * !0 = failure
  4358. *
  4359. * PARAMETERS:
  4360. * *dev - pointer to device structure
  4361. * protect - flag to call protected or non-protected
  4362. * *cal_data - pointer to extended calibration data structure
  4363. * *status - pointer to where the command response status is stored
  4364. ******************************************************************************/
  4365. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4366. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4367. {
  4368. struct pt_core_data *cd = dev_get_drvdata(dev);
  4369. if (protect)
  4370. return pt_pip_calibrate_ext(cd, cal_data, status);
  4371. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4372. }
  4373. /*******************************************************************************
  4374. * FUNCTION: pt_pip_calibrate_idacs_
  4375. *
  4376. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4377. * for the response
  4378. *
  4379. * RETURN:
  4380. * 0 = success
  4381. * !0 = failure
  4382. *
  4383. * PARAMETERS:
  4384. * *cd - pointer to core data
  4385. * mode - sense mode to calibrate (0-5)
  4386. * *status - pointer to where the command response status is stored
  4387. ******************************************************************************/
  4388. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4389. u8 mode, u8 *status)
  4390. {
  4391. int rc = 0;
  4392. int write_length = 1;
  4393. u8 write_buf[1];
  4394. u8 cmd_offset = 0;
  4395. struct pt_hid_output hid_output = {
  4396. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4397. .write_length = write_length,
  4398. .write_buf = write_buf,
  4399. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4400. };
  4401. write_buf[cmd_offset++] = mode;
  4402. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4403. if (rc)
  4404. return rc;
  4405. *status = cd->response_buf[5];
  4406. if (*status)
  4407. return -EINVAL;
  4408. return 0;
  4409. }
  4410. /*******************************************************************************
  4411. * FUNCTION: pt_pip_calibrate_idacs
  4412. *
  4413. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4414. * access to the DUT
  4415. *
  4416. * RETURN:
  4417. * 0 = success
  4418. * !0 = failure
  4419. *
  4420. * PARAMETERS:
  4421. * *cd - pointer to core data
  4422. * mode - sense mode to calibrate (0-5)
  4423. * *status - pointer to where the command response status is stored
  4424. ******************************************************************************/
  4425. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4426. u8 mode, u8 *status)
  4427. {
  4428. int rc;
  4429. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4430. if (rc < 0) {
  4431. pt_debug(cd->dev, DL_ERROR,
  4432. "%s: fail get exclusive ex=%p own=%p\n",
  4433. __func__, cd->exclusive_dev, cd->dev);
  4434. return rc;
  4435. }
  4436. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4437. if (release_exclusive(cd, cd->dev) < 0)
  4438. pt_debug(cd->dev, DL_ERROR,
  4439. "%s: fail to release exclusive\n", __func__);
  4440. return rc;
  4441. }
  4442. /*******************************************************************************
  4443. * FUNCTION: _pt_request_pip_calibrate_idacs
  4444. *
  4445. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4446. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4447. *
  4448. * RETURN:
  4449. * 0 = success
  4450. * !0 = failure
  4451. *
  4452. * PARAMETERS:
  4453. * *dev - pointer to device structure
  4454. * protect - flag to call protected or non-protected
  4455. * mode - sense mode to calibrate (0-5)
  4456. * *status - pointer to where the command response status is stored
  4457. ******************************************************************************/
  4458. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4459. int protect, u8 mode, u8 *status)
  4460. {
  4461. struct pt_core_data *cd = dev_get_drvdata(dev);
  4462. if (protect)
  4463. return pt_pip_calibrate_idacs(cd, mode, status);
  4464. return pt_pip_calibrate_idacs_(cd, mode, status);
  4465. }
  4466. /*******************************************************************************
  4467. * FUNCTION: pt_hid_output_initialize_baselines_
  4468. *
  4469. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4470. * waiting for the response.
  4471. *
  4472. * RETURN:
  4473. * 0 = success
  4474. * !0 = failure
  4475. *
  4476. * PARAMETERS:
  4477. * *cd - pointer to core data
  4478. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4479. * each or together with a single command.
  4480. * *status - pointer to where the command response status is stored
  4481. ******************************************************************************/
  4482. static int pt_hid_output_initialize_baselines_(
  4483. struct pt_core_data *cd, u8 test_id, u8 *status)
  4484. {
  4485. int rc = 0;
  4486. int write_length = 1;
  4487. u8 write_buf[1];
  4488. u8 cmd_offset = 0;
  4489. struct pt_hid_output hid_output = {
  4490. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4491. .write_length = write_length,
  4492. .write_buf = write_buf,
  4493. };
  4494. write_buf[cmd_offset++] = test_id;
  4495. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4496. if (rc)
  4497. return rc;
  4498. *status = cd->response_buf[5];
  4499. if (*status)
  4500. return -EINVAL;
  4501. return rc;
  4502. }
  4503. /*******************************************************************************
  4504. * FUNCTION: pt_hid_output_initialize_baselines
  4505. *
  4506. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4507. * access to the DUT
  4508. *
  4509. * RETURN:
  4510. * 0 = success
  4511. * !0 = failure
  4512. *
  4513. * PARAMETERS:
  4514. * *cd - pointer to core data
  4515. * test_id - enumerated ID against which to initialize the baseline
  4516. * *status - pointer to where the command response status is stored
  4517. ******************************************************************************/
  4518. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4519. u8 test_id, u8 *status)
  4520. {
  4521. int rc;
  4522. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4523. if (rc < 0) {
  4524. pt_debug(cd->dev, DL_ERROR,
  4525. "%s: fail get exclusive ex=%p own=%p\n",
  4526. __func__, cd->exclusive_dev, cd->dev);
  4527. return rc;
  4528. }
  4529. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4530. if (release_exclusive(cd, cd->dev) < 0)
  4531. pt_debug(cd->dev, DL_ERROR,
  4532. "%s: fail to release exclusive\n", __func__);
  4533. return rc;
  4534. }
  4535. /*******************************************************************************
  4536. * FUNCTION: _pt_request_pip_initialize_baselines
  4537. *
  4538. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4539. * calls to the protected or unprotected call to
  4540. * pt_pip_initialize_baselines
  4541. *
  4542. * RETURN:
  4543. * 0 = success
  4544. * !0 = failure
  4545. *
  4546. * PARAMETERS:
  4547. * *dev - pointer to device structure
  4548. * protect - flag to call protected or non-protected
  4549. * test_id - enumerated ID against which to initialize the baseline
  4550. * *status - pointer to where the command response status is stored
  4551. ******************************************************************************/
  4552. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4553. int protect, u8 test_id, u8 *status)
  4554. {
  4555. struct pt_core_data *cd = dev_get_drvdata(dev);
  4556. if (protect)
  4557. return pt_hid_output_initialize_baselines(cd, test_id,
  4558. status);
  4559. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4560. }
  4561. /*******************************************************************************
  4562. * FUNCTION: pt_hid_output_exec_panel_scan_
  4563. *
  4564. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4565. * the response
  4566. *
  4567. * RETURN:
  4568. * 0 = success
  4569. * !0 = failure
  4570. *
  4571. * PARAMETERS:
  4572. * *cd - pointer to core data
  4573. ******************************************************************************/
  4574. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4575. {
  4576. struct pt_hid_output hid_output = {
  4577. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4578. };
  4579. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4580. }
  4581. /*******************************************************************************
  4582. * FUNCTION: pt_hid_output_exec_panel_scan
  4583. *
  4584. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4585. * access to the DUT.
  4586. *
  4587. * RETURN:
  4588. * 0 = success
  4589. * !0 = failure
  4590. *
  4591. * PARAMETERS:
  4592. * *cd - pointer to core data
  4593. ******************************************************************************/
  4594. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4595. {
  4596. int rc;
  4597. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4598. if (rc < 0) {
  4599. pt_debug(cd->dev, DL_ERROR,
  4600. "%s: fail get exclusive ex=%p own=%p\n",
  4601. __func__, cd->exclusive_dev, cd->dev);
  4602. return rc;
  4603. }
  4604. rc = pt_hid_output_exec_panel_scan_(cd);
  4605. if (release_exclusive(cd, cd->dev) < 0)
  4606. pt_debug(cd->dev, DL_ERROR,
  4607. "%s: fail to release exclusive\n", __func__);
  4608. return rc;
  4609. }
  4610. /*******************************************************************************
  4611. * FUNCTION: pt_pip2_exec_panel_scan_
  4612. *
  4613. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4614. * the response
  4615. *
  4616. * RETURN:
  4617. * 0 = success
  4618. * !0 = failure
  4619. *
  4620. * PARAMETERS:
  4621. * *cd - pointer to core data
  4622. * scan_type - type of panel scan to perform (PIP2 only)
  4623. ******************************************************************************/
  4624. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4625. {
  4626. int rc = 0;
  4627. u8 data[2];
  4628. u8 read_buf[10];
  4629. u16 actual_read_len;
  4630. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4631. __func__, scan_type);
  4632. data[0] = scan_type;
  4633. rc = _pt_request_pip2_send_cmd(cd->dev,
  4634. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4635. data, 1, read_buf, &actual_read_len);
  4636. if (rc) {
  4637. pt_debug(cd->dev, DL_ERROR,
  4638. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4639. __func__, scan_type, rc);
  4640. }
  4641. return rc;
  4642. }
  4643. /*******************************************************************************
  4644. * FUNCTION: pt_pip2_exec_panel_scan
  4645. *
  4646. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4647. * access to the DUT.
  4648. *
  4649. * RETURN:
  4650. * 0 = success
  4651. * !0 = failure
  4652. *
  4653. * PARAMETERS:
  4654. * *cd - pointer to core data
  4655. * scan_type - type of panel scan to perform (PIP2 only)
  4656. ******************************************************************************/
  4657. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4658. {
  4659. int rc;
  4660. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4661. if (rc < 0) {
  4662. pt_debug(cd->dev, DL_ERROR,
  4663. "%s: fail get exclusive ex=%p own=%p\n",
  4664. __func__, cd->exclusive_dev, cd->dev);
  4665. return rc;
  4666. }
  4667. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4668. if (release_exclusive(cd, cd->dev) < 0)
  4669. pt_debug(cd->dev, DL_ERROR,
  4670. "%s: fail to release exclusive\n", __func__);
  4671. return rc;
  4672. }
  4673. /*******************************************************************************
  4674. * FUNCTION: _pt_request_pip_exec_panel_scan
  4675. *
  4676. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4677. * calls to the protected or unprotected call to
  4678. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4679. *
  4680. * RETURN:
  4681. * 0 = success
  4682. * !0 = failure
  4683. *
  4684. * PARAMETERS:
  4685. * *dev - pointer to device structure
  4686. * protect - flag to call protected or non-protected
  4687. * scan_type - type of panel scan to perform (PIP2 only)
  4688. ******************************************************************************/
  4689. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4690. int protect, u8 scan_type)
  4691. {
  4692. struct pt_core_data *cd = dev_get_drvdata(dev);
  4693. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4694. if (protect)
  4695. return pt_pip2_exec_panel_scan(cd, scan_type);
  4696. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4697. }
  4698. if (protect)
  4699. return pt_hid_output_exec_panel_scan(cd);
  4700. return pt_hid_output_exec_panel_scan_(cd);
  4701. }
  4702. /*******************************************************************************
  4703. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4704. *
  4705. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4706. * to retrieve the specified data type for a the last successful Execute
  4707. * Panel Scan command.
  4708. *
  4709. * RETURN:
  4710. * 0 = success
  4711. * !0 = failure
  4712. *
  4713. * PARAMETERS:
  4714. * *dev - pointer to device structure
  4715. * protect - flag to call protected or non-protected
  4716. * read_offset - read pointer offset
  4717. * read_count - length of data to read
  4718. * data_id - enumerated test ID to read selftest results from
  4719. * *response - pointer to store the read response status
  4720. * *config - pointer to store config data
  4721. * *actual_read_len - pointer to store data length actually read
  4722. * *read_buf - pointer to the read buffer
  4723. ******************************************************************************/
  4724. static int pt_hid_output_retrieve_panel_scan_(
  4725. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4726. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4727. u8 *read_buf)
  4728. {
  4729. int status;
  4730. u8 read_data_id;
  4731. int rc = 0;
  4732. int write_length = 5;
  4733. u8 write_buf[5];
  4734. u8 cmd_offset = 0;
  4735. u8 data_elem_size;
  4736. int size;
  4737. int data_size;
  4738. struct pt_hid_output hid_output = {
  4739. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4740. .write_length = write_length,
  4741. .write_buf = write_buf,
  4742. };
  4743. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4744. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4745. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4746. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4747. write_buf[cmd_offset++] = data_id;
  4748. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4749. if (rc)
  4750. return rc;
  4751. status = cd->response_buf[5];
  4752. if (status)
  4753. return -EINVAL;
  4754. read_data_id = cd->response_buf[6];
  4755. if (read_data_id != data_id)
  4756. return -EPROTO;
  4757. size = get_unaligned_le16(&cd->response_buf[0]);
  4758. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4759. *config = cd->response_buf[9];
  4760. data_elem_size = *config & 0x07;
  4761. data_size = *actual_read_len * data_elem_size;
  4762. if (read_buf)
  4763. memcpy(read_buf, &cd->response_buf[10], data_size);
  4764. if (response)
  4765. memcpy(response, cd->response_buf, size);
  4766. return rc;
  4767. }
  4768. /*******************************************************************************
  4769. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4770. *
  4771. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4772. * access to the DUT.
  4773. *
  4774. * RETURN:
  4775. * 0 = success
  4776. * !0 = failure
  4777. *
  4778. * PARAMETERS:
  4779. * *dev - pointer to device structure
  4780. * protect - flag to call protected or non-protected
  4781. * read_offset - read pointer offset
  4782. * read_count - length of data to read
  4783. * data_id - enumerated test ID to read selftest results from
  4784. * *response - pointer to store the read response status
  4785. * *config - pointer to store config data
  4786. * *actual_read_len - pointer to store data length actually read
  4787. * *read_buf - pointer to the read buffer
  4788. ******************************************************************************/
  4789. static int pt_hid_output_retrieve_panel_scan(
  4790. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4791. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4792. u8 *read_buf)
  4793. {
  4794. int rc;
  4795. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4796. if (rc < 0) {
  4797. pt_debug(cd->dev, DL_ERROR,
  4798. "%s: fail get exclusive ex=%p own=%p\n",
  4799. __func__, cd->exclusive_dev, cd->dev);
  4800. return rc;
  4801. }
  4802. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4803. read_count, data_id, response, config,
  4804. actual_read_len, read_buf);
  4805. if (release_exclusive(cd, cd->dev) < 0)
  4806. pt_debug(cd->dev, DL_ERROR,
  4807. "%s: fail to release exclusive\n", __func__);
  4808. return rc;
  4809. }
  4810. /*******************************************************************************
  4811. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4812. *
  4813. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4814. * calls to the protected or unprotected call to
  4815. * pt_hid_output_retrieve_panel_scan
  4816. *
  4817. * RETURN:
  4818. * 0 = success
  4819. * !0 = failure
  4820. *
  4821. * PARAMETERS:
  4822. * *dev - pointer to device structure
  4823. * protect - flag to call protected or non-protected
  4824. * read_offset - read pointer offset
  4825. * read_count - length of data to read
  4826. * data_id - enumerated test ID to read selftest results from
  4827. * *response - pointer to store the read response status
  4828. * *config - pointer to store config data
  4829. * *actual_read_len - pointer to store data length actually read
  4830. * *read_buf - pointer to the read buffer
  4831. ******************************************************************************/
  4832. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4833. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4834. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4835. {
  4836. struct pt_core_data *cd = dev_get_drvdata(dev);
  4837. if (protect)
  4838. return pt_hid_output_retrieve_panel_scan(cd,
  4839. read_offset, read_count, data_id, response,
  4840. config, actual_read_len, read_buf);
  4841. return pt_hid_output_retrieve_panel_scan_(cd,
  4842. read_offset, read_count, data_id, response,
  4843. config, actual_read_len, read_buf);
  4844. }
  4845. /*******************************************************************************
  4846. * FUNCTION: _pt_request_pip_user_cmd
  4847. *
  4848. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4849. * calls to the protected or unprotected call to
  4850. * pt_hid_output_user_cmd
  4851. *
  4852. * RETURN:
  4853. * 0 = success
  4854. * !0 = failure
  4855. *
  4856. * PARAMETERS:
  4857. * *dev - pointer to device structure
  4858. * protect - flag to call protected or non-protected
  4859. * read_len - length of data to read
  4860. * *read_buf - pointer to store read data
  4861. * write_len - length of data to write
  4862. * *write_buf - pointer to buffer to write
  4863. * *actual_read_len - pointer to store data length actually read
  4864. ******************************************************************************/
  4865. static int _pt_request_pip_user_cmd(struct device *dev,
  4866. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4867. u8 *write_buf, u16 *actual_read_len)
  4868. {
  4869. struct pt_core_data *cd = dev_get_drvdata(dev);
  4870. if (protect)
  4871. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4872. write_len, write_buf, actual_read_len);
  4873. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4874. write_len, write_buf, actual_read_len);
  4875. }
  4876. /*******************************************************************************
  4877. * FUNCTION: pt_hid_output_bl_get_information_
  4878. *
  4879. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4880. * DUT to retrieve bootloader version and chip identification information.
  4881. *
  4882. * RETURN:
  4883. * 0 = success
  4884. * !0 = failure
  4885. *
  4886. * PARAMETERS:
  4887. * *cd - pointer to core data
  4888. * *return_data - pointer to store the return data
  4889. *****************************************************************************/
  4890. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4891. u8 *return_data)
  4892. {
  4893. int rc;
  4894. int data_len;
  4895. struct pt_hid_output hid_output = {
  4896. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4897. };
  4898. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4899. if (rc)
  4900. return rc;
  4901. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4902. if (!data_len)
  4903. return -EPROTO;
  4904. memcpy(return_data, &cd->response_buf[8], data_len);
  4905. return 0;
  4906. }
  4907. /*******************************************************************************
  4908. * FUNCTION: pt_hid_output_bl_get_information
  4909. *
  4910. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4911. * access to the DUT.
  4912. *
  4913. * RETURN:
  4914. * 0 = success
  4915. * !0 = failure
  4916. *
  4917. * PARAMETERS:
  4918. * *cd - pointer to core data
  4919. * *return_data - pointer to store the return data
  4920. ******************************************************************************/
  4921. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4922. u8 *return_data)
  4923. {
  4924. int rc;
  4925. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4926. if (rc < 0) {
  4927. pt_debug(cd->dev, DL_ERROR,
  4928. "%s: fail get exclusive ex=%p own=%p\n",
  4929. __func__, cd->exclusive_dev, cd->dev);
  4930. return rc;
  4931. }
  4932. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4933. if (release_exclusive(cd, cd->dev) < 0)
  4934. pt_debug(cd->dev, DL_ERROR,
  4935. "%s: fail to release exclusive\n", __func__);
  4936. return rc;
  4937. }
  4938. /*******************************************************************************
  4939. * FUNCTION: _pt_request_pip_bl_get_information
  4940. *
  4941. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4942. * calls to the protected or unprotected call to
  4943. * pt_hid_output_bl_get_information
  4944. *
  4945. * RETURN:
  4946. * 0 = success
  4947. * !0 = failure
  4948. *
  4949. * PARAMETERS:
  4950. * *dev - pointer to device structure
  4951. * protect - flag to call protected or non-protected
  4952. * *return_data - pointer to store bl data
  4953. ******************************************************************************/
  4954. static int _pt_request_pip_bl_get_information(struct device *dev,
  4955. int protect, u8 *return_data)
  4956. {
  4957. struct pt_core_data *cd = dev_get_drvdata(dev);
  4958. if (protect)
  4959. return pt_hid_output_bl_get_information(cd, return_data);
  4960. return pt_hid_output_bl_get_information_(cd, return_data);
  4961. }
  4962. /*******************************************************************************
  4963. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4964. *
  4965. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4966. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4967. * and Design Data block in flash and enables the host to execute the Program
  4968. * and Verify Row command to bootload the application image and data.
  4969. *
  4970. * RETURN:
  4971. * 0 = success
  4972. * !0 = failure
  4973. *
  4974. * PARAMETERS:
  4975. * *cd - pointer to core data
  4976. * protect - flag to call protected or non-protected
  4977. * key_size - size of key
  4978. * *key_buf - pointer to key data to allow operation
  4979. * row_size - size of the meta data row
  4980. * *metadata_row_buf - pointer to meta data to write
  4981. ******************************************************************************/
  4982. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4983. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4984. {
  4985. u16 write_length = key_size + row_size;
  4986. u8 *write_buf;
  4987. int rc = 0;
  4988. struct pt_hid_output hid_output = {
  4989. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4990. .write_length = write_length,
  4991. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4992. };
  4993. write_buf = kzalloc(write_length, GFP_KERNEL);
  4994. if (!write_buf)
  4995. return -ENOMEM;
  4996. hid_output.write_buf = write_buf;
  4997. if (key_size)
  4998. memcpy(write_buf, key_buf, key_size);
  4999. if (row_size)
  5000. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5001. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5002. kfree(write_buf);
  5003. return rc;
  5004. }
  5005. /*******************************************************************************
  5006. * FUNCTION: pt_hid_output_bl_initiate_bl
  5007. *
  5008. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5009. * access to the DUT.
  5010. *
  5011. * RETURN:
  5012. * 0 = success
  5013. * !0 = failure
  5014. *
  5015. * PARAMETERS:
  5016. * *cd - pointer to core data
  5017. * key_size - size of key
  5018. * *key_buf - pointer to key data to allow operation
  5019. * row_size - size of the meta data row
  5020. * *metadata_row_buf - pointer to meta data to write
  5021. ******************************************************************************/
  5022. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5023. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5024. {
  5025. int rc;
  5026. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5027. if (rc < 0) {
  5028. pt_debug(cd->dev, DL_ERROR,
  5029. "%s: fail get exclusive ex=%p own=%p\n",
  5030. __func__, cd->exclusive_dev, cd->dev);
  5031. return rc;
  5032. }
  5033. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5034. row_size, metadata_row_buf);
  5035. if (release_exclusive(cd, cd->dev) < 0)
  5036. pt_debug(cd->dev, DL_ERROR,
  5037. "%s: fail to release exclusive\n", __func__);
  5038. return rc;
  5039. }
  5040. /*******************************************************************************
  5041. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5042. *
  5043. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5044. * calls to the protected or unprotected call to
  5045. * pt_hid_output_bl_initiate_bl
  5046. *
  5047. * RETURN:
  5048. * 0 = success
  5049. * !0 = failure
  5050. *
  5051. * PARAMETERS:
  5052. * *dev - pointer to device structure
  5053. * protect - flag to call protected or non-protected
  5054. * key_size - size of key
  5055. * *key_buf - pointer to key data to allow operation
  5056. * row_size - size of the meta data row
  5057. * *metadata_row_buf - pointer to meta data to write
  5058. ******************************************************************************/
  5059. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5060. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5061. u8 *metadata_row_buf)
  5062. {
  5063. struct pt_core_data *cd = dev_get_drvdata(dev);
  5064. if (protect)
  5065. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5066. row_size, metadata_row_buf);
  5067. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5068. row_size, metadata_row_buf);
  5069. }
  5070. /*******************************************************************************
  5071. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5072. *
  5073. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5074. * and program a 128-byte row into the flash, and then verifies written data.
  5075. *
  5076. * RETURN:
  5077. * 0 = success
  5078. * !0 = failure
  5079. *
  5080. * PARAMETERS:
  5081. * *cd - pointer to core data
  5082. * data_len - length of data_buf
  5083. * *data_buf - firmware image to program
  5084. ******************************************************************************/
  5085. static int pt_hid_output_bl_program_and_verify_(
  5086. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5087. {
  5088. struct pt_hid_output hid_output = {
  5089. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5090. .write_length = data_len,
  5091. .write_buf = data_buf,
  5092. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5093. };
  5094. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5095. }
  5096. /*******************************************************************************
  5097. * FUNCTION: pt_hid_output_bl_program_and_verify
  5098. *
  5099. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5100. * access to the DUT.
  5101. *
  5102. * RETURN:
  5103. * 0 = success
  5104. * !0 = failure
  5105. *
  5106. * PARAMETERS:
  5107. * *cd - pointer to core data
  5108. * data_len - length of data_buf
  5109. * *data_buf - firmware image to program
  5110. ******************************************************************************/
  5111. static int pt_hid_output_bl_program_and_verify(
  5112. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5113. {
  5114. int rc;
  5115. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5116. if (rc < 0) {
  5117. pt_debug(cd->dev, DL_ERROR,
  5118. "%s: fail get exclusive ex=%p own=%p\n",
  5119. __func__, cd->exclusive_dev, cd->dev);
  5120. return rc;
  5121. }
  5122. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5123. if (release_exclusive(cd, cd->dev) < 0)
  5124. pt_debug(cd->dev, DL_ERROR,
  5125. "%s: fail to release exclusive\n", __func__);
  5126. return rc;
  5127. }
  5128. /*******************************************************************************
  5129. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5130. *
  5131. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5132. * to request to have the BL program and verify a FW image
  5133. *
  5134. * RETURN:
  5135. * 0 = success
  5136. * !0 = failure
  5137. *
  5138. * PARAMETERS:
  5139. * *dev - pointer to device structure
  5140. * protect - boolean to determine to call the protected function
  5141. * data_len - length of data_buf
  5142. * *data_buf - firmware image to program
  5143. ******************************************************************************/
  5144. static int _pt_request_pip_bl_program_and_verify(
  5145. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5146. {
  5147. struct pt_core_data *cd = dev_get_drvdata(dev);
  5148. if (protect)
  5149. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5150. data_buf);
  5151. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5152. data_buf);
  5153. }
  5154. /*******************************************************************************
  5155. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5156. *
  5157. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5158. * perform a full verification of the application integrity by calculating the
  5159. * CRC of the image in flash and compare it to the expected CRC stored in the
  5160. * Metadata row.
  5161. *
  5162. * RETURN:
  5163. * 0 = success
  5164. * !0 = failure
  5165. *
  5166. * PARAMETERS:
  5167. * *cd - pointer to core data
  5168. * *result - pointer to store result
  5169. ******************************************************************************/
  5170. static int pt_hid_output_bl_verify_app_integrity_(
  5171. struct pt_core_data *cd, u8 *result)
  5172. {
  5173. int rc;
  5174. struct pt_hid_output hid_output = {
  5175. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5176. };
  5177. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5178. if (rc) {
  5179. *result = 0;
  5180. return rc;
  5181. }
  5182. *result = cd->response_buf[8];
  5183. return 0;
  5184. }
  5185. /*******************************************************************************
  5186. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5187. *
  5188. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5189. * exclusive access to the DUT.
  5190. *
  5191. * RETURN:
  5192. * 0 = success
  5193. * !0 = failure
  5194. *
  5195. * PARAMETERS:
  5196. * *cd - pointer to core data
  5197. * *result - pointer to store result
  5198. ******************************************************************************/
  5199. static int pt_hid_output_bl_verify_app_integrity(
  5200. struct pt_core_data *cd, u8 *result)
  5201. {
  5202. int rc;
  5203. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5204. if (rc < 0) {
  5205. pt_debug(cd->dev, DL_ERROR,
  5206. "%s: fail get exclusive ex=%p own=%p\n",
  5207. __func__, cd->exclusive_dev, cd->dev);
  5208. return rc;
  5209. }
  5210. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5211. if (release_exclusive(cd, cd->dev) < 0)
  5212. pt_debug(cd->dev, DL_ERROR,
  5213. "%s: fail to release exclusive\n", __func__);
  5214. return rc;
  5215. }
  5216. /*******************************************************************************
  5217. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5218. *
  5219. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5220. * to request to have the BL verify the application integrity (PIP1.x only)
  5221. *
  5222. * RETURN:
  5223. * 0 = success
  5224. * !0 = failure
  5225. *
  5226. * PARAMETERS:
  5227. * *dev - pointer to device structure
  5228. * protect - boolean to determine to call the protected function
  5229. * *result - pointer to store result
  5230. ******************************************************************************/
  5231. static int _pt_request_pip_bl_verify_app_integrity(
  5232. struct device *dev, int protect, u8 *result)
  5233. {
  5234. struct pt_core_data *cd = dev_get_drvdata(dev);
  5235. if (protect)
  5236. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5237. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5238. }
  5239. /*******************************************************************************
  5240. * FUNCTION: pt_hid_output_bl_launch_app_
  5241. *
  5242. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5243. * application from bootloader (PIP1.x only).
  5244. *
  5245. * RETURN:
  5246. * 0 = success
  5247. * !0 = failure
  5248. *
  5249. * PARAMETERS:
  5250. * *cd - pointer to core data
  5251. ******************************************************************************/
  5252. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5253. {
  5254. struct pt_hid_output hid_output = {
  5255. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5256. .reset_expected = 1,
  5257. };
  5258. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5259. }
  5260. /*******************************************************************************
  5261. * FUNCTION: pt_hid_output_bl_launch_app
  5262. *
  5263. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5264. * to the DUT.
  5265. *
  5266. * RETURN:
  5267. * 0 = success
  5268. * !0 = failure
  5269. *
  5270. * PARAMETERS:
  5271. * *cd - pointer to core data
  5272. ******************************************************************************/
  5273. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5274. {
  5275. int rc;
  5276. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5277. if (rc < 0) {
  5278. pt_debug(cd->dev, DL_ERROR,
  5279. "%s: fail get exclusive ex=%p own=%p\n",
  5280. __func__, cd->exclusive_dev, cd->dev);
  5281. return rc;
  5282. }
  5283. rc = pt_hid_output_bl_launch_app_(cd);
  5284. if (release_exclusive(cd, cd->dev) < 0)
  5285. pt_debug(cd->dev, DL_ERROR,
  5286. "%s: fail to release exclusive\n", __func__);
  5287. return rc;
  5288. }
  5289. /*******************************************************************************
  5290. * FUNCTION: _pt_request_pip_launch_app
  5291. *
  5292. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5293. * to request to have the BL launch the application. (PIP1.x only)
  5294. *
  5295. * RETURN:
  5296. * 0 = success
  5297. * !0 = failure
  5298. *
  5299. * PARAMETERS:
  5300. * *dev - pointer to device structure
  5301. * protect - boolean to determine to call the protected function
  5302. ******************************************************************************/
  5303. static int _pt_request_pip_launch_app(struct device *dev,
  5304. int protect)
  5305. {
  5306. struct pt_core_data *cd = dev_get_drvdata(dev);
  5307. if (protect)
  5308. return pt_hid_output_bl_launch_app(cd);
  5309. return pt_hid_output_bl_launch_app_(cd);
  5310. }
  5311. /*******************************************************************************
  5312. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5313. *
  5314. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5315. * value store in the System Information.
  5316. *
  5317. * RETURN:
  5318. * 0 = success
  5319. * !0 = failure
  5320. *
  5321. * PARAMETERS:
  5322. * *cd - pointer to core data
  5323. * *panel_id - pointer to where the panel ID will be stored
  5324. ******************************************************************************/
  5325. static int pt_hid_output_bl_get_panel_id_(
  5326. struct pt_core_data *cd, u8 *panel_id)
  5327. {
  5328. int rc;
  5329. struct pt_hid_output hid_output = {
  5330. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5331. };
  5332. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5333. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5334. pt_debug(cd->dev, DL_ERROR,
  5335. "%s: Get Panel ID command not supported\n",
  5336. __func__);
  5337. *panel_id = PANEL_ID_NOT_ENABLED;
  5338. return 0;
  5339. } else if (rc < 0) {
  5340. pt_debug(cd->dev, DL_ERROR,
  5341. "%s: Error on Get Panel ID command\n", __func__);
  5342. return rc;
  5343. }
  5344. *panel_id = cd->response_buf[8];
  5345. return 0;
  5346. }
  5347. /*******************************************************************************
  5348. * FUNCTION: pt_hid_output_bl_get_panel_id
  5349. *
  5350. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5351. * to the DUT.
  5352. *
  5353. * RETURN:
  5354. * 0 = success
  5355. * !0 = failure
  5356. *
  5357. * PARAMETERS:
  5358. * *cd - pointer to core data
  5359. * *panel_id - pointer to where the panel ID will be stored
  5360. ******************************************************************************/
  5361. static int pt_hid_output_bl_get_panel_id(
  5362. struct pt_core_data *cd, u8 *panel_id)
  5363. {
  5364. int rc;
  5365. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5366. if (rc < 0) {
  5367. pt_debug(cd->dev, DL_ERROR,
  5368. "%s: fail get exclusive ex=%p own=%p\n",
  5369. __func__, cd->exclusive_dev, cd->dev);
  5370. return rc;
  5371. }
  5372. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5373. if (release_exclusive(cd, cd->dev) < 0)
  5374. pt_debug(cd->dev, DL_ERROR,
  5375. "%s: fail to release exclusive\n", __func__);
  5376. return rc;
  5377. }
  5378. /*******************************************************************************
  5379. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5380. *
  5381. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5382. * to have the BL retrieve the panel ID
  5383. *
  5384. * RETURN:
  5385. * 0 = success
  5386. * !0 = failure
  5387. *
  5388. * PARAMETERS:
  5389. * *dev - pointer to device structure
  5390. * protect - flag to run in protected mode
  5391. * *panel_id - pointer to where the panel ID will be stored
  5392. ******************************************************************************/
  5393. static int _pt_request_pip_bl_get_panel_id(
  5394. struct device *dev, int protect, u8 *panel_id)
  5395. {
  5396. struct pt_core_data *cd = dev_get_drvdata(dev);
  5397. if (protect)
  5398. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5399. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5400. }
  5401. /*******************************************************************************
  5402. * FUNCTION: pt_pip2_get_mode_sysmode_
  5403. *
  5404. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5405. * PIP2 STATUS command.
  5406. *
  5407. * RETURN:
  5408. * 0 = success
  5409. * !0 = failure
  5410. *
  5411. * PARAMETERS:
  5412. * *cd - pointer to core data structure
  5413. * *mode - pointer to store the retrieved mode
  5414. * *sys_mode - pointer to store the FW system mode
  5415. ******************************************************************************/
  5416. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5417. u8 *mode, u8 *sys_mode)
  5418. {
  5419. u16 actual_read_len;
  5420. u8 read_buf[12];
  5421. u8 status, boot;
  5422. int rc = 0;
  5423. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5424. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5425. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5426. __func__, rc);
  5427. if (!rc) {
  5428. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5429. "PIP2 STATUS");
  5430. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5431. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5432. if (sys_mode) {
  5433. if (status == PIP2_RSP_ERR_NONE &&
  5434. boot == PIP2_STATUS_APP_EXEC)
  5435. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5436. else
  5437. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5438. }
  5439. if (mode) {
  5440. if (status == PIP2_RSP_ERR_NONE &&
  5441. boot == PIP2_STATUS_BOOT_EXEC)
  5442. *mode = PT_MODE_BOOTLOADER;
  5443. else if (status == PIP2_RSP_ERR_NONE &&
  5444. boot == PIP2_STATUS_APP_EXEC)
  5445. *mode = PT_MODE_OPERATIONAL;
  5446. else
  5447. *mode = PT_MODE_UNKNOWN;
  5448. }
  5449. } else {
  5450. if (mode)
  5451. *mode = PT_MODE_UNKNOWN;
  5452. if (sys_mode)
  5453. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5454. pt_debug(cd->dev, DL_WARN,
  5455. "%s: Mode and sys_mode could not be determined\n",
  5456. __func__);
  5457. }
  5458. return rc;
  5459. }
  5460. /*******************************************************************************
  5461. * FUNCTION: pt_pip2_get_mode_sysmode
  5462. *
  5463. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5464. * access to the DUT.
  5465. *
  5466. * RETURN:
  5467. * 0 = success
  5468. * !0 = failure
  5469. *
  5470. * PARAMETERS:
  5471. * *cd - pointer to core data structure
  5472. * *mode - pointer to store the retrieved mode
  5473. * *sys_mode - pointer to store the FW system mode
  5474. ******************************************************************************/
  5475. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5476. u8 *mode, u8 *sys_mode)
  5477. {
  5478. int rc;
  5479. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5480. if (rc < 0) {
  5481. pt_debug(cd->dev, DL_ERROR,
  5482. "%s: fail get exclusive ex=%p own=%p\n",
  5483. __func__, cd->exclusive_dev, cd->dev);
  5484. return rc;
  5485. }
  5486. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5487. if (release_exclusive(cd, cd->dev) < 0)
  5488. pt_debug(cd->dev, DL_ERROR,
  5489. "%s: fail to release exclusive\n", __func__);
  5490. return rc;
  5491. }
  5492. /*******************************************************************************
  5493. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5494. *
  5495. * SUMMARY: Function pointer included in core_commands struct for external
  5496. * calls to the protected or unprotected call to
  5497. * pt_pip2_get_mode_sysmode
  5498. *
  5499. * RETURN:
  5500. * 0 = success
  5501. * !0 = failure
  5502. *
  5503. * PARAMETERS:
  5504. * *dev - pointer to device structure
  5505. * *mode - pointer to store the retrieved mode
  5506. * *sys_mode - pointer to store the FW system mode
  5507. ******************************************************************************/
  5508. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5509. int protect, u8 *mode, u8 *sys_mode)
  5510. {
  5511. struct pt_core_data *cd = dev_get_drvdata(dev);
  5512. if (protect)
  5513. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5514. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5515. }
  5516. /*******************************************************************************
  5517. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5518. *
  5519. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5520. * mode only the following PIP commands will be serviced, any other PIP
  5521. * command the FW will respond with an "Invalid PIP Command" response.
  5522. * - Get HID Descriptor (Register 0x0001, no command ID)
  5523. * - Reset (Register 0x0005, RESET HID request)
  5524. * - Ping (Register 0x0004, Command ID 0x00
  5525. * - Get System Information (Register 0x0004, Command ID 0x02)
  5526. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5527. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5528. * This function will loop on the results of the STATUS command until
  5529. * the FW reports it is out of BOOT mode.
  5530. *
  5531. * NOTE:
  5532. * - This function will update cd->fw_system_mode
  5533. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5534. *
  5535. * RETURN:
  5536. * 0 = success
  5537. * !0 = failure
  5538. *
  5539. * PARAMETERS:
  5540. * *cd - pointer to core data
  5541. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5542. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5543. ******************************************************************************/
  5544. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5545. int *actual_wait)
  5546. {
  5547. int loop = 0;
  5548. u8 sys_mode = cd->fw_system_mode;
  5549. u8 pause = 10; /* in ms */
  5550. int rc = 0;
  5551. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5552. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5553. /*
  5554. * For PIP <1.11, no support for polling wait so do a hard
  5555. * coded wait and assume the FW is out of BOOT. Added 1 to
  5556. * timeout to make it clear in kmsg if non polling was done.
  5557. */
  5558. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5559. pt_debug(cd->dev, DL_ERROR,
  5560. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5561. __func__,
  5562. cd->sysinfo.ttdata.pip_ver_major,
  5563. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5564. msleep(*actual_wait);
  5565. sys_mode = FW_SYS_MODE_SCANNING;
  5566. }
  5567. if (sys_mode == FW_SYS_MODE_BOOT) {
  5568. while (!rc && loop <= max_loop &&
  5569. (sys_mode == FW_SYS_MODE_BOOT)) {
  5570. loop++;
  5571. usleep_range(9000, pause * 1000);
  5572. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5573. pt_debug(cd->dev, DL_DEBUG,
  5574. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5575. __func__, loop * pause, sys_mode);
  5576. }
  5577. *actual_wait = (int)(loop * pause);
  5578. pt_debug(cd->dev, DL_WARN,
  5579. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5580. __func__, *actual_wait, sys_mode);
  5581. if (rc)
  5582. sys_mode = FW_SYS_MODE_UNDEFINED;
  5583. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5584. sys_mode == FW_SYS_MODE_UNDEFINED)
  5585. rc = -EBUSY;
  5586. }
  5587. mutex_lock(&cd->system_lock);
  5588. cd->fw_system_mode = sys_mode;
  5589. mutex_unlock(&cd->system_lock);
  5590. return rc;
  5591. }
  5592. /*******************************************************************************
  5593. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5594. *
  5595. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5596. * access to the DUT.
  5597. *
  5598. * RETURN:
  5599. * 0 = success
  5600. * !0 = failure
  5601. *
  5602. * PARAMETERS:
  5603. * *cd - pointer to core data
  5604. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5605. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5606. ******************************************************************************/
  5607. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5608. int *actual_wait)
  5609. {
  5610. int rc;
  5611. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5612. if (rc < 0) {
  5613. pt_debug(cd->dev, DL_ERROR,
  5614. "%s: fail get exclusive ex=%p own=%p\n",
  5615. __func__, cd->exclusive_dev, cd->dev);
  5616. return rc;
  5617. }
  5618. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5619. if (release_exclusive(cd, cd->dev) < 0)
  5620. pt_debug(cd->dev, DL_ERROR,
  5621. "%s: fail to release exclusive\n", __func__);
  5622. return rc;
  5623. }
  5624. /*******************************************************************************
  5625. * FUNCTION: _pt_get_fw_sys_mode
  5626. *
  5627. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5628. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5629. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5630. * information but any PIP command above 0x1F requires scanning to be disabled
  5631. * before it will be operational. If scanning was not disabled before sending
  5632. * these PIP commands the FW will respond with a 6 byte error response. So to
  5633. * safely determine the scanning state, a PIP message that will not affect the
  5634. * operation of the FW was chosen:
  5635. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5636. * received scanning is active.
  5637. *
  5638. * RETURN:
  5639. * 0 = success
  5640. * !0 = failure
  5641. *
  5642. * PARAMETERS:
  5643. * *cd - pointer to core data
  5644. * *sys_mode - pointer to FW System mode
  5645. * *mode - pointer to mode (BL/FW)
  5646. ******************************************************************************/
  5647. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5648. {
  5649. int write_length = 1;
  5650. int report_length;
  5651. int rc = 0;
  5652. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5653. u8 tmp_mode = PT_MODE_UNKNOWN;
  5654. u8 param[1] = { PT_TCH_PARM_EBID };
  5655. struct pt_hid_output hid_output = {
  5656. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5657. .write_length = write_length,
  5658. .write_buf = param,
  5659. .novalidate = true,
  5660. };
  5661. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5662. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5663. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5664. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5665. __func__, tmp_sys_mode, tmp_mode);
  5666. if (!rc) {
  5667. if (tmp_mode != PT_MODE_OPERATIONAL)
  5668. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5669. }
  5670. goto exit;
  5671. }
  5672. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5673. if (cd->mode != PT_MODE_OPERATIONAL) {
  5674. tmp_mode = cd->mode;
  5675. goto exit;
  5676. }
  5677. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5678. if (rc)
  5679. goto exit;
  5680. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5681. if ((report_length == 0x06) &&
  5682. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5683. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5684. tmp_mode = PIP2_STATUS_APP_EXEC;
  5685. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5686. } else if ((report_length == 0x0A) &&
  5687. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5688. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5689. tmp_mode = PIP2_STATUS_APP_EXEC;
  5690. tmp_sys_mode = FW_SYS_MODE_TEST;
  5691. }
  5692. exit:
  5693. if (mode)
  5694. *mode = tmp_mode;
  5695. if (sys_mode)
  5696. *sys_mode = tmp_sys_mode;
  5697. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5698. __func__, tmp_mode, tmp_sys_mode);
  5699. return rc;
  5700. }
  5701. /*******************************************************************************
  5702. * FUNCTION: pt_get_fw_sys_mode
  5703. *
  5704. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5705. * is active or not.
  5706. *
  5707. * RETURN:
  5708. * 0 = success
  5709. * !0 = failure
  5710. *
  5711. * PARAMETERS:
  5712. * *cd - pointer to core data
  5713. * *sys_mode - pointer to fw system mode
  5714. * *mode - pointer to mode
  5715. ******************************************************************************/
  5716. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5717. {
  5718. int rc;
  5719. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5720. if (rc < 0) {
  5721. pt_debug(cd->dev, DL_ERROR,
  5722. "%s: fail get exclusive ex=%p own=%p\n",
  5723. __func__, cd->exclusive_dev, cd->dev);
  5724. return rc;
  5725. }
  5726. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5727. if (release_exclusive(cd, cd->dev) < 0)
  5728. pt_debug(cd->dev, DL_ERROR,
  5729. "%s: fail to release exclusive\n", __func__);
  5730. return rc;
  5731. }
  5732. /*******************************************************************************
  5733. * FUNCTION: _pt_request_get_fw_sys_mode
  5734. *
  5735. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5736. * to request to get scan state
  5737. *
  5738. * RETURN:
  5739. * 0 = success
  5740. * !0 = failure
  5741. *
  5742. * PARAMETERS:
  5743. * *dev - pointer to device structure
  5744. * protect - flag to call protected or non-protected
  5745. * *sys_mode - pointer to FW system mode
  5746. * *mode - pointer to mode
  5747. ******************************************************************************/
  5748. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5749. u8 *sys_mode, u8 *mode)
  5750. {
  5751. struct pt_core_data *cd = dev_get_drvdata(dev);
  5752. if (protect)
  5753. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5754. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5755. }
  5756. /* Default hid descriptor to provide basic register map */
  5757. const struct pt_hid_desc hid_desc_default = {
  5758. 230, /* hid_desc_len */
  5759. HID_APP_REPORT_ID, /* packet_id */
  5760. 0x00, /* reserved_byte */
  5761. 0x0100, /* bcd_version */
  5762. 0x00EC, /* report_desc_len */
  5763. 0x0002, /* report_desc_register */
  5764. 0x0003, /* input_register */
  5765. 0x00FE, /* max_input_len */
  5766. 0x0004, /* output_register */
  5767. 0x00FE, /* max_output_len */
  5768. 0x0005, /* command_register */
  5769. 0x0006, /* data_register */
  5770. 0x04B4, /* vendor_id */
  5771. 0xC101, /* product_id */
  5772. 0x0100, /* version_id */
  5773. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5774. };
  5775. /*******************************************************************************
  5776. * FUNCTION: pt_init_hid_descriptor
  5777. *
  5778. * SUMMARY: Setup default values for HID descriptor structure
  5779. *
  5780. *
  5781. * PARAMETERS:
  5782. * *desc - pointer to the HID descriptor data read back from DUT
  5783. ******************************************************************************/
  5784. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5785. {
  5786. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5787. }
  5788. /*******************************************************************************
  5789. * FUNCTION: pt_get_hid_descriptor_
  5790. *
  5791. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5792. * into the HID descriptor structure
  5793. *
  5794. * RETURN:
  5795. * 0 = success
  5796. * !0 = failure
  5797. *
  5798. * PARAMETERS:
  5799. * *cd - pointer to core data
  5800. * *desc - pointer to the HID descriptor data read back from DUT
  5801. ******************************************************************************/
  5802. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5803. struct pt_hid_desc *desc)
  5804. {
  5805. struct device *dev = cd->dev;
  5806. int rc = 0;
  5807. int t;
  5808. u8 cmd[2];
  5809. /*
  5810. * During startup the HID descriptor is required for all future
  5811. * processing. If IRQ is already asserted due to an early touch report
  5812. * the report must be cleared before sending command.
  5813. */
  5814. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5815. /* Read HID descriptor length and version */
  5816. mutex_lock(&cd->system_lock);
  5817. cd->hid_cmd_state = 1;
  5818. mutex_unlock(&cd->system_lock);
  5819. /* Set HID descriptor register */
  5820. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5821. sizeof(cd->hid_core.hid_desc_register));
  5822. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5823. __func__, sizeof(cmd));
  5824. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5825. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5826. if (rc) {
  5827. pt_debug(dev, DL_ERROR,
  5828. "%s: failed to get HID descriptor, rc=%d\n",
  5829. __func__, rc);
  5830. goto error;
  5831. }
  5832. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5833. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5834. if (IS_TMO(t)) {
  5835. #ifdef TTDL_DIAGNOSTICS
  5836. cd->bus_transmit_error_count++;
  5837. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5838. #endif /* TTDL_DIAGNOSTICS */
  5839. pt_debug(cd->dev, DL_ERROR,
  5840. "%s: HID get descriptor timed out\n", __func__);
  5841. rc = -ETIME;
  5842. goto error;
  5843. } else {
  5844. cd->hw_detected = true;
  5845. }
  5846. /* Load the HID descriptor including all registers */
  5847. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5848. /* Check HID descriptor length and version */
  5849. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5850. le16_to_cpu(desc->hid_desc_len),
  5851. le16_to_cpu(desc->bcd_version));
  5852. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5853. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5854. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5855. __func__);
  5856. return -ENODEV;
  5857. }
  5858. goto exit;
  5859. error:
  5860. mutex_lock(&cd->system_lock);
  5861. cd->hid_cmd_state = 0;
  5862. mutex_unlock(&cd->system_lock);
  5863. exit:
  5864. return rc;
  5865. }
  5866. /*******************************************************************************
  5867. * FUNCTION: pt_get_hid_descriptor
  5868. *
  5869. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5870. *
  5871. * RETURN:
  5872. * 0 = success
  5873. * !0 = failure
  5874. *
  5875. * PARAMETERS:
  5876. * *cd - pointer to core data
  5877. * *desc - pointer to the HID descriptor data read back from DUT
  5878. ******************************************************************************/
  5879. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5880. struct pt_hid_desc *desc)
  5881. {
  5882. int rc;
  5883. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5884. if (rc < 0) {
  5885. pt_debug(cd->dev, DL_ERROR,
  5886. "%s: fail get exclusive ex=%p own=%p\n",
  5887. __func__, cd->exclusive_dev, cd->dev);
  5888. return rc;
  5889. }
  5890. rc = pt_get_hid_descriptor_(cd, desc);
  5891. if (release_exclusive(cd, cd->dev) < 0)
  5892. pt_debug(cd->dev, DL_ERROR,
  5893. "%s: fail to release exclusive\n", __func__);
  5894. return rc;
  5895. }
  5896. /*******************************************************************************
  5897. * FUNCTION: pt_pip2_get_version_
  5898. *
  5899. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5900. * cd-ttdata
  5901. *
  5902. * RETURN:
  5903. * 0 = success
  5904. * !0 = failure
  5905. *
  5906. * PARAMETERS:
  5907. * *cd - pointer to core data
  5908. ******************************************************************************/
  5909. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5910. {
  5911. int rc = 0;
  5912. int status;
  5913. u8 read_buf[64];
  5914. u16 actual_read_len;
  5915. rc = _pt_request_pip2_send_cmd(cd->dev,
  5916. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5917. NULL, 0, read_buf, &actual_read_len);
  5918. if (rc) {
  5919. pt_debug(cd->dev, DL_ERROR,
  5920. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5921. __func__, rc);
  5922. return rc;
  5923. }
  5924. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5925. if (status == 0) {
  5926. /* Parse the PIP2 VERSION response into ttdata */
  5927. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5928. } else {
  5929. pt_debug(cd->dev, DL_ERROR,
  5930. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5931. __func__, status);
  5932. return status;
  5933. }
  5934. return rc;
  5935. }
  5936. /*******************************************************************************
  5937. * FUNCTION: pt_pip2_get_version
  5938. *
  5939. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5940. * access to the DUT.
  5941. *
  5942. * RETURN:
  5943. * 0 = success
  5944. * !0 = failure
  5945. *
  5946. * PARAMETERS:
  5947. * *cd - pointer to core data structure
  5948. ******************************************************************************/
  5949. static int pt_pip2_get_version(struct pt_core_data *cd)
  5950. {
  5951. int rc;
  5952. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5953. if (rc < 0) {
  5954. pt_debug(cd->dev, DL_ERROR,
  5955. "%s: fail get exclusive ex=%p own=%p\n",
  5956. __func__, cd->exclusive_dev, cd->dev);
  5957. return rc;
  5958. }
  5959. rc = pt_pip2_get_version_(cd);
  5960. if (release_exclusive(cd, cd->dev) < 0)
  5961. pt_debug(cd->dev, DL_ERROR,
  5962. "%s: fail to release exclusive\n", __func__);
  5963. return rc;
  5964. }
  5965. /*******************************************************************************
  5966. * FUNCTION: _pt_request_active_pip_protocol
  5967. *
  5968. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5969. * Function will return PIP version of BL or application based on
  5970. * when it's called.
  5971. *
  5972. * RETURN:
  5973. * 0 = success
  5974. * !0 = failure
  5975. *
  5976. * PARAMETERS:
  5977. * *dev - pointer to device structure
  5978. * protect - flag to run in protected mode
  5979. * *pip_version_major - pointer to store PIP major version
  5980. * *pip_version_minor - pointer to store PIP minor version
  5981. ******************************************************************************/
  5982. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5983. u8 *pip_version_major, u8 *pip_version_minor)
  5984. {
  5985. struct pt_core_data *cd = dev_get_drvdata(dev);
  5986. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5987. int rc = 0;
  5988. struct pt_hid_output sys_info = {
  5989. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5990. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5991. };
  5992. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5993. /* Skip PIP2 command if DUT generation is confirmed */
  5994. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5995. goto skip_pip2_command;
  5996. rc = pt_pip2_get_version_(cd);
  5997. if (!rc) {
  5998. *pip_version_major = ttdata->pip_ver_major;
  5999. *pip_version_minor = ttdata->pip_ver_minor;
  6000. pt_debug(dev, DL_INFO,
  6001. "%s: pip_version = %d.%d\n", __func__,
  6002. *pip_version_major, *pip_version_minor);
  6003. } else {
  6004. /*
  6005. * Legacy products do not support the pip2 protocol to get
  6006. * pip version. However, they do support the "get sysinfo"
  6007. * command to get pip version from FW, but the bootloader
  6008. * does not support it. This function will try "get sysinfo"
  6009. * command if the pip2 command failed but this cmd could also
  6010. * fail if DUT is stuck in bootloader mode.
  6011. */
  6012. pt_debug(dev, DL_INFO,
  6013. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6014. __func__, rc);
  6015. skip_pip2_command:
  6016. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6017. if (!rc) {
  6018. *pip_version_minor =
  6019. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6020. *pip_version_major =
  6021. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6022. pt_debug(dev, DL_INFO,
  6023. "%s: pip_version = %d.%d\n", __func__,
  6024. *pip_version_major, *pip_version_minor);
  6025. } else {
  6026. *pip_version_major = 0;
  6027. *pip_version_minor = 0;
  6028. pt_debug(dev, DL_ERROR,
  6029. "%s: pip_version Not Detected\n", __func__);
  6030. }
  6031. }
  6032. return rc;
  6033. }
  6034. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6035. /*******************************************************************************
  6036. * FUNCTION: _pt_detect_dut_generation
  6037. *
  6038. * SUMMARY: Determine the generation of device that we are communicating with:
  6039. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6040. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6041. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6042. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6043. * coming form the BL or the FW:
  6044. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6045. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6046. * This function will return a modified status if it detects the DUT
  6047. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6048. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6049. *
  6050. * RETURN:
  6051. * 0 = success
  6052. * !0 = failure
  6053. *
  6054. * PARAMETERS:
  6055. * *dev - pointer to device structure
  6056. * *status - pointer to status bitmask
  6057. * *dut_gen - pointer to store the dut_generation
  6058. * *mode - pointer to store the PT_MODE
  6059. ******************************************************************************/
  6060. static int _pt_detect_dut_generation(struct device *dev,
  6061. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6062. {
  6063. int rc = 0;
  6064. u8 dut_gen_tmp = DUT_UNKNOWN;
  6065. u8 mode_tmp = PT_MODE_UNKNOWN;
  6066. u8 attempt = 1;
  6067. u32 status_tmp = STARTUP_STATUS_START;
  6068. struct pt_core_data *cd = dev_get_drvdata(dev);
  6069. struct pt_hid_desc hid_desc;
  6070. memset(&hid_desc, 0, sizeof(hid_desc));
  6071. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6072. while (rc && attempt < 3) {
  6073. attempt++;
  6074. usleep_range(2000, 5000);
  6075. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6076. }
  6077. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6078. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6079. mode_tmp = PT_MODE_BOOTLOADER;
  6080. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6081. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6082. rc = pt_pip2_get_version_(cd);
  6083. if (!rc)
  6084. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6085. else
  6086. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6087. mode_tmp = PT_MODE_OPERATIONAL;
  6088. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6089. rc = 0; /* To return success instead of error code */
  6090. } else if (rc) {
  6091. rc = pt_pip2_get_version_(cd);
  6092. if (!rc) {
  6093. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6094. mode_tmp = PT_MODE_BOOTLOADER;
  6095. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6096. }
  6097. }
  6098. mutex_lock(&cd->system_lock);
  6099. if (dut_gen)
  6100. *dut_gen = dut_gen_tmp;
  6101. if (mode)
  6102. *mode = mode_tmp;
  6103. if (status)
  6104. *status = status_tmp;
  6105. mutex_unlock(&cd->system_lock);
  6106. #ifdef TTDL_DIAGNOSTICS
  6107. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6108. __func__, dut_gen_tmp, mode_tmp);
  6109. #endif /* TTDL_DIAGNOSTICS */
  6110. return rc;
  6111. }
  6112. /*******************************************************************************
  6113. * FUNCTION: _pt_request_dut_generation
  6114. *
  6115. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6116. * to get current dut generation.
  6117. *
  6118. * NOTE: This function WILL NOT try to determine dut generation.
  6119. *
  6120. * RETURN:
  6121. * The current dut generation.
  6122. *
  6123. * PARAMETERS:
  6124. * *dev - pointer to device structure
  6125. ******************************************************************************/
  6126. static int _pt_request_dut_generation(struct device *dev)
  6127. {
  6128. struct pt_core_data *cd = dev_get_drvdata(dev);
  6129. return cd->active_dut_generation;
  6130. }
  6131. #define HW_VERSION_LEN_MAX 13
  6132. /*******************************************************************************
  6133. * FUNCTION: _legacy_generate_hw_version
  6134. *
  6135. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6136. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6137. * command) to the hw_version.
  6138. *
  6139. * RETURN:
  6140. * 0 = success
  6141. * !0 = failure
  6142. *
  6143. * PARAMETERS:
  6144. * *cd - pointer to core data
  6145. * *hw_version - pointer to store the hardware version
  6146. ******************************************************************************/
  6147. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6148. char *hw_version)
  6149. {
  6150. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6151. if (cd->sysinfo.ready) {
  6152. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6153. ttdata->jtag_id_h, cd->pid_for_loader);
  6154. return 0;
  6155. } else if (cd->bl_info.ready) {
  6156. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6157. cd->bl_info.chip_id, cd->pid_for_loader);
  6158. return 0;
  6159. } else {
  6160. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6161. pt_debug(cd->dev, DL_ERROR,
  6162. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6163. return -ENODATA;
  6164. }
  6165. }
  6166. /*******************************************************************************
  6167. * FUNCTION: _pip2_generate_hw_version
  6168. *
  6169. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6170. * VERSION command) to the hw_version.
  6171. *
  6172. * RETURN:
  6173. * 0 = success
  6174. * !0 = failure
  6175. *
  6176. * PARAMETERS:
  6177. * *cd - pointer to core data
  6178. * *hw_version - pointer to store the hardware version
  6179. ******************************************************************************/
  6180. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6181. {
  6182. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6183. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6184. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6185. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6186. return 0;
  6187. } else {
  6188. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6189. pt_debug(cd->dev, DL_ERROR,
  6190. "%s: PIP Version are not ready\n", __func__);
  6191. return -ENODATA;
  6192. }
  6193. }
  6194. /*******************************************************************************
  6195. * FUNCTION: pt_generate_hw_version
  6196. *
  6197. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6198. * hw_version from static data.
  6199. *
  6200. * RETURN:
  6201. * 0 = success
  6202. * !0 = failure
  6203. *
  6204. * PARAMETERS:
  6205. * *cd - pointer to core data
  6206. * *hw_version - pointer to store the hardware version
  6207. ******************************************************************************/
  6208. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6209. {
  6210. int rc = 0;
  6211. if (!hw_version)
  6212. return -ENOMEM;
  6213. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6214. rc = _legacy_generate_hw_version(cd, hw_version);
  6215. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6216. rc = _pip2_generate_hw_version(cd, hw_version);
  6217. else {
  6218. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6219. rc = -ENODATA;
  6220. }
  6221. return rc;
  6222. }
  6223. /*******************************************************************************
  6224. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6225. *
  6226. * NOTE: The calling function must ensure to free *hw_version
  6227. *
  6228. * RETURN:
  6229. * 0 = success
  6230. * !0 = Failure
  6231. *
  6232. * PARAMETERS:
  6233. * *dev - pointer to device structure
  6234. * *hw_version - pointer to where the hw_version string will be stored
  6235. ******************************************************************************/
  6236. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6237. {
  6238. int rc = 0;
  6239. u16 actual_read_len;
  6240. u16 pip_ver;
  6241. u8 rd_buf[256];
  6242. u8 status;
  6243. u8 index = PIP2_RESP_STATUS_OFFSET;
  6244. u8 return_data[8];
  6245. u8 panel_id;
  6246. struct pt_core_data *cd = dev_get_drvdata(dev);
  6247. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6248. if (!hw_version)
  6249. return -ENOMEM;
  6250. if (!cd->hw_detected) {
  6251. /* No HW detected */
  6252. rc = -ENODEV;
  6253. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6254. __func__);
  6255. goto exit_error;
  6256. }
  6257. memset(return_data, 0, ARRAY_SIZE(return_data));
  6258. /* For Parade TC or TT parts */
  6259. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6260. rc = _pt_request_pip2_send_cmd(dev,
  6261. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6262. NULL, 0, rd_buf, &actual_read_len);
  6263. if (rc) {
  6264. pt_debug(dev, DL_ERROR,
  6265. "%s: Failed to send PIP2 VERSION cmd\n",
  6266. __func__);
  6267. goto exit_error;
  6268. }
  6269. status = rd_buf[index];
  6270. if (status == 0) {
  6271. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6272. /*
  6273. * BL PIP 2.02 and greater the version fields are
  6274. * swapped
  6275. */
  6276. if (pip_ver >= 0x0202) {
  6277. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6278. "%02X%02X.%02X%02X.FF",
  6279. rd_buf[index + 10], rd_buf[index + 9],
  6280. rd_buf[index + 8], rd_buf[index + 7]);
  6281. } else {
  6282. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6283. "%02X%02X.%02X%02X.FF",
  6284. rd_buf[index + 8], rd_buf[index + 7],
  6285. rd_buf[index + 10], rd_buf[index + 9]);
  6286. }
  6287. return STATUS_SUCCESS;
  6288. } else {
  6289. rc = status;
  6290. pt_debug(dev, DL_WARN,
  6291. "%s: PIP2 VERSION cmd response error\n",
  6292. __func__);
  6293. }
  6294. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6295. /*
  6296. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6297. * hard coded to FFFF
  6298. */
  6299. if (cd->mode == PT_MODE_OPERATIONAL) {
  6300. rc = pt_hid_output_get_sysinfo(cd);
  6301. if (!rc) {
  6302. panel_id =
  6303. cd->sysinfo.sensing_conf_data.panel_id;
  6304. } else {
  6305. panel_id = PANEL_ID_NOT_ENABLED;
  6306. }
  6307. /* In FW - simply retrieve from ttdata struct */
  6308. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6309. "%04X.FFFF.%02X",
  6310. ttdata->jtag_id_h,
  6311. panel_id);
  6312. return STATUS_SUCCESS;
  6313. } else {
  6314. /*
  6315. * Return the stored value if PT_PANEL_ID_BY_BL
  6316. * is not supported while other feature is.
  6317. */
  6318. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6319. rc = pt_hid_output_bl_get_information(
  6320. cd, return_data);
  6321. if (!rc) {
  6322. rc = pt_hid_output_bl_get_panel_id(
  6323. cd, &panel_id);
  6324. }
  6325. } else
  6326. panel_id = cd->pid_for_loader;
  6327. if (!rc) {
  6328. snprintf(hw_version,
  6329. HW_VERSION_LEN_MAX,
  6330. "%02X%02X.FFFF.%02X",
  6331. return_data[3], return_data[2],
  6332. panel_id);
  6333. return STATUS_SUCCESS;
  6334. }
  6335. }
  6336. } else {
  6337. /* Unknown generation */
  6338. rc = -ENODEV;
  6339. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6340. __func__);
  6341. }
  6342. exit_error:
  6343. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6344. return rc;
  6345. }
  6346. /*******************************************************************************
  6347. * FUNCTION: pt_start_wd_timer
  6348. *
  6349. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6350. *
  6351. * RETURN: void
  6352. *
  6353. * PARAMETERS:
  6354. * *cd - pointer to core data
  6355. ******************************************************************************/
  6356. static void pt_start_wd_timer(struct pt_core_data *cd)
  6357. {
  6358. if (cd->watchdog_interval < 100) {
  6359. pt_debug(cd->dev, DL_ERROR,
  6360. "%s: WARNING: Invalid watchdog interval: %d\n",
  6361. __func__, cd->watchdog_interval);
  6362. return;
  6363. }
  6364. if (cd->watchdog_force_stop) {
  6365. pt_debug(cd->dev, DL_INFO,
  6366. "%s: TTDL WD Forced stop\n", __func__);
  6367. return;
  6368. }
  6369. mod_timer(&cd->watchdog_timer, jiffies +
  6370. msecs_to_jiffies(cd->watchdog_interval));
  6371. cd->watchdog_enabled = 1;
  6372. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6373. }
  6374. /*******************************************************************************
  6375. * FUNCTION: pt_stop_wd_timer
  6376. *
  6377. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6378. *
  6379. * RETURN: void
  6380. *
  6381. * PARAMETERS:
  6382. * *cd - pointer to core data
  6383. ******************************************************************************/
  6384. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6385. {
  6386. if (!cd->watchdog_interval)
  6387. return;
  6388. /*
  6389. * Ensure we wait until the watchdog timer
  6390. * running on a different CPU finishes
  6391. */
  6392. del_timer_sync(&cd->watchdog_timer);
  6393. cancel_work_sync(&cd->watchdog_work);
  6394. del_timer_sync(&cd->watchdog_timer);
  6395. cd->watchdog_enabled = 0;
  6396. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6397. }
  6398. /*******************************************************************************
  6399. * FUNCTION: pt_hw_soft_reset
  6400. *
  6401. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6402. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6403. * schedule an enum if it fires when the DUT is being reset.
  6404. * This can cause a double reset.
  6405. *
  6406. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6407. * the WD active could cause this function to fail!
  6408. *
  6409. * RETURN:
  6410. * 0 = success
  6411. * !0 = failure
  6412. *
  6413. * PARAMETERS:
  6414. * *cd - pointer to core data struct
  6415. * protect - flag to call protected or non-protected
  6416. ******************************************************************************/
  6417. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6418. {
  6419. int rc = 0;
  6420. mutex_lock(&cd->system_lock);
  6421. cd->startup_status = STARTUP_STATUS_START;
  6422. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6423. mutex_unlock(&cd->system_lock);
  6424. if (protect)
  6425. rc = pt_hid_cmd_reset(cd);
  6426. else
  6427. rc = pt_hid_cmd_reset_(cd);
  6428. if (rc < 0) {
  6429. pt_debug(cd->dev, DL_ERROR,
  6430. "%s: FAILED to execute SOFT reset\n", __func__);
  6431. return rc;
  6432. }
  6433. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6434. __func__);
  6435. return 0;
  6436. }
  6437. /*******************************************************************************
  6438. * FUNCTION: pt_hw_hard_reset
  6439. *
  6440. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6441. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6442. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6443. * schedule an enum if it fires when the DUT is being reset.
  6444. * This can cause a double reset.
  6445. *
  6446. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6447. * the WD active could cause this function to fail!
  6448. *
  6449. * RETURN:
  6450. * 0 = success
  6451. * !0 = failure
  6452. *
  6453. * PARAMETERS:
  6454. * *cd - pointer to core data struct
  6455. ******************************************************************************/
  6456. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6457. {
  6458. if (cd->cpdata->xres) {
  6459. cd->startup_status = STARTUP_STATUS_START;
  6460. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6461. __func__);
  6462. cd->cpdata->xres(cd->cpdata, cd->dev);
  6463. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6464. __func__);
  6465. return 0;
  6466. }
  6467. pt_debug(cd->dev, DL_ERROR,
  6468. "%s: FAILED to execute HARD reset\n", __func__);
  6469. return -ENODEV;
  6470. }
  6471. /*******************************************************************************
  6472. * FUNCTION: pt_dut_reset
  6473. *
  6474. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6475. * soft reset.
  6476. *
  6477. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6478. * the WD active could cause this function to fail!
  6479. * NOTE: "protect" flag is only used for soft reset.
  6480. *
  6481. * RETURN:
  6482. * 0 = success
  6483. * !0 = failure
  6484. *
  6485. * PARAMETERS:
  6486. * *cd - pointer to core data structure
  6487. * protect - flag to call protected or non-protected
  6488. ******************************************************************************/
  6489. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6490. {
  6491. int rc = 0;
  6492. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6493. mutex_lock(&cd->system_lock);
  6494. cd->hid_reset_cmd_state = 1;
  6495. rc = pt_hw_hard_reset(cd);
  6496. mutex_unlock(&cd->system_lock);
  6497. if (rc == -ENODEV) {
  6498. mutex_lock(&cd->system_lock);
  6499. cd->hid_reset_cmd_state = 0;
  6500. mutex_unlock(&cd->system_lock);
  6501. pt_debug(cd->dev, DL_ERROR,
  6502. "%s: Hard reset failed, try soft reset\n", __func__);
  6503. rc = pt_hw_soft_reset(cd, protect);
  6504. }
  6505. if (rc)
  6506. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6507. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6508. return rc;
  6509. }
  6510. /*******************************************************************************
  6511. * FUNCTION: pt_dut_reset_and_wait
  6512. *
  6513. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6514. * complete
  6515. *
  6516. * RETURN:
  6517. * 0 = success
  6518. * !0 = failure
  6519. *
  6520. * PARAMETERS:
  6521. * *cd - pointer to core data structure
  6522. ******************************************************************************/
  6523. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6524. {
  6525. int rc = 0;
  6526. int t;
  6527. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6528. if (rc < 0)
  6529. goto exit;
  6530. t = wait_event_timeout(cd->wait_q,
  6531. (cd->hid_reset_cmd_state == 0),
  6532. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6533. if (IS_TMO(t)) {
  6534. #ifdef TTDL_DIAGNOSTICS
  6535. cd->bus_transmit_error_count++;
  6536. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6537. #endif /* TTDL_DIAGNOSTICS */
  6538. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6539. __func__);
  6540. rc = -ETIME;
  6541. goto exit;
  6542. }
  6543. exit:
  6544. return rc;
  6545. }
  6546. /*
  6547. * touch default parameters (from report descriptor) to resolve protocol for
  6548. * touch report
  6549. */
  6550. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6551. /* byte offset, size, min, max, bit offset, report */
  6552. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6553. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6554. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6555. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6556. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6557. };
  6558. /*
  6559. * button default parameters (from report descriptor) to resolve protocol for
  6560. * button report
  6561. */
  6562. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6563. /* byte offset, size, min, max, bit offset, report */
  6564. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6565. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6566. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6567. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6568. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6569. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6570. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6571. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6572. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6573. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6574. };
  6575. /*******************************************************************************
  6576. * FUNCTION: pt_init_pip_report_fields
  6577. *
  6578. * SUMMARY: Setup default values for touch/button report parsing.
  6579. *
  6580. * PARAMETERS:
  6581. * *cd - pointer to core data structure
  6582. ******************************************************************************/
  6583. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6584. {
  6585. struct pt_sysinfo *si = &cd->sysinfo;
  6586. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6587. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6588. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6589. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6590. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6591. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6592. cd->features.easywake = 1;
  6593. cd->features.noise_metric = 1;
  6594. cd->features.tracking_heatmap = 1;
  6595. cd->features.sensor_data = 1;
  6596. }
  6597. /*******************************************************************************
  6598. * FUNCTION: pt_get_mode
  6599. *
  6600. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6601. * message
  6602. *
  6603. * RETURN: Enum of the current mode
  6604. *
  6605. * PARAMETERS:
  6606. * *cd - pointer to the Core Data structure
  6607. * protect - run command in protected mode
  6608. * *mode - pointer to store the retrieved mode
  6609. ******************************************************************************/
  6610. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6611. {
  6612. if (desc->packet_id == HID_APP_REPORT_ID)
  6613. return PT_MODE_OPERATIONAL;
  6614. else if (desc->packet_id == HID_BL_REPORT_ID)
  6615. return PT_MODE_BOOTLOADER;
  6616. return PT_MODE_UNKNOWN;
  6617. }
  6618. /*******************************************************************************
  6619. * FUNCTION: _pt_request_get_mode
  6620. *
  6621. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6622. * to determine the current mode of the DUT by use of the Get HID
  6623. * Descriptor command.
  6624. *
  6625. * RETURN:
  6626. * 0 = success
  6627. * !0 = failure
  6628. *
  6629. * PARAMETERS:
  6630. * *dev - pointer to device structure
  6631. * protect - run command in protected mode
  6632. * *mode - pointer to store the retrieved mode
  6633. ******************************************************************************/
  6634. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6635. {
  6636. struct pt_core_data *cd = dev_get_drvdata(dev);
  6637. struct pt_hid_desc hid_desc;
  6638. int rc = 0;
  6639. memset(&hid_desc, 0, sizeof(hid_desc));
  6640. if (protect)
  6641. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6642. else
  6643. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6644. if (rc)
  6645. *mode = PT_MODE_UNKNOWN;
  6646. else
  6647. *mode = pt_get_mode(cd, &hid_desc);
  6648. return rc;
  6649. }
  6650. /*******************************************************************************
  6651. * FUNCTION: pt_queue_enum_
  6652. *
  6653. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6654. * function. It won't try to add/delete sysfs node or modules.
  6655. *
  6656. * RETURN: void
  6657. *
  6658. * PARAMETERS:
  6659. * *cd - pointer to core data
  6660. ******************************************************************************/
  6661. static void pt_queue_enum_(struct pt_core_data *cd)
  6662. {
  6663. if (cd->startup_state == STARTUP_NONE) {
  6664. cd->startup_state = STARTUP_QUEUED;
  6665. #ifdef TTDL_DIAGNOSTICS
  6666. if (!cd->bridge_mode)
  6667. schedule_work(&cd->enum_work);
  6668. else
  6669. cd->startup_state = STARTUP_NONE;
  6670. #else
  6671. schedule_work(&cd->enum_work);
  6672. #endif
  6673. pt_debug(cd->dev, DL_INFO,
  6674. "%s: enum_work queued\n", __func__);
  6675. } else {
  6676. pt_debug(cd->dev, DL_WARN,
  6677. "%s: Enum not queued - startup_state = %d\n",
  6678. __func__, cd->startup_state);
  6679. }
  6680. }
  6681. /*******************************************************************************
  6682. * FUNCTION: pt_queue_enum
  6683. *
  6684. * SUMMARY: Queues a TTDL enum within a mutex lock
  6685. *
  6686. * RETURN: void
  6687. *
  6688. * PARAMETERS:
  6689. * *cd - pointer to core data
  6690. ******************************************************************************/
  6691. static void pt_queue_enum(struct pt_core_data *cd)
  6692. {
  6693. mutex_lock(&cd->system_lock);
  6694. pt_queue_enum_(cd);
  6695. mutex_unlock(&cd->system_lock);
  6696. }
  6697. static void remove_sysfs_and_modules(struct device *dev);
  6698. /*******************************************************************************
  6699. * FUNCTION: pt_queue_restart
  6700. *
  6701. * SUMMARY: Queues a TTDL restart within a mutex lock
  6702. *
  6703. * RETURN: void
  6704. *
  6705. * PARAMETERS:
  6706. * *cd - pointer to core data
  6707. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6708. * False: will not perform remove action
  6709. ******************************************************************************/
  6710. static void pt_queue_restart(struct pt_core_data *cd)
  6711. {
  6712. mutex_lock(&cd->system_lock);
  6713. if (cd->startup_state == STARTUP_NONE) {
  6714. cd->startup_state = STARTUP_QUEUED;
  6715. schedule_work(&cd->ttdl_restart_work);
  6716. pt_debug(cd->dev, DL_INFO,
  6717. "%s: pt_ttdl_restart queued\n", __func__);
  6718. } else {
  6719. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6720. __func__, cd->startup_state);
  6721. }
  6722. mutex_unlock(&cd->system_lock);
  6723. }
  6724. /*******************************************************************************
  6725. * FUNCTION: call_atten_cb
  6726. *
  6727. * SUMMARY: Iterate over attention list call the function that registered.
  6728. *
  6729. * RETURN: void
  6730. *
  6731. * PARAMETERS:
  6732. * *cd - pointer to core data
  6733. * type - type of attention list
  6734. * mode - condition for execution
  6735. ******************************************************************************/
  6736. static void call_atten_cb(struct pt_core_data *cd,
  6737. enum pt_atten_type type, int mode)
  6738. {
  6739. struct atten_node *atten, *atten_n;
  6740. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6741. __func__, type, mode);
  6742. spin_lock(&cd->spinlock);
  6743. list_for_each_entry_safe(atten, atten_n,
  6744. &cd->atten_list[type], node) {
  6745. if (!mode || atten->mode & mode) {
  6746. spin_unlock(&cd->spinlock);
  6747. pt_debug(cd->dev, DL_DEBUG,
  6748. "%s: attention for '%s'",
  6749. __func__, dev_name(atten->dev));
  6750. atten->func(atten->dev);
  6751. spin_lock(&cd->spinlock);
  6752. }
  6753. }
  6754. spin_unlock(&cd->spinlock);
  6755. }
  6756. /*******************************************************************************
  6757. * FUNCTION: start_fw_upgrade
  6758. *
  6759. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6760. * start firmware upgrade.
  6761. *
  6762. * RETURN:
  6763. * 0 = success
  6764. *
  6765. * PARAMETERS:
  6766. * *data - pointer to core data
  6767. ******************************************************************************/
  6768. static int start_fw_upgrade(void *data)
  6769. {
  6770. struct pt_core_data *cd = (struct pt_core_data *)data;
  6771. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6772. return 0;
  6773. }
  6774. /*******************************************************************************
  6775. * FUNCTION: pt_put_device_into_easy_wakeup_
  6776. *
  6777. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6778. * wake up state.
  6779. *
  6780. * RETURN:
  6781. * 0 = success
  6782. * !0 = failure
  6783. *
  6784. * PARAMETERS:
  6785. * *cd - pointer to core data
  6786. ******************************************************************************/
  6787. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6788. {
  6789. int rc = 0;
  6790. u8 status = 0;
  6791. rc = pt_hid_output_enter_easywake_state_(cd,
  6792. cd->easy_wakeup_gesture, &status);
  6793. if (rc || status == 0)
  6794. return -EBUSY;
  6795. return rc;
  6796. }
  6797. /*******************************************************************************
  6798. * FUNCTION: pt_put_device_into_deep_sleep_
  6799. *
  6800. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6801. *
  6802. * RETURN:
  6803. * 0 = success
  6804. * !0 = error
  6805. *
  6806. * PARAMETERS:
  6807. * *cd - pointer to core data
  6808. ******************************************************************************/
  6809. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6810. {
  6811. int rc = 0;
  6812. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6813. if (rc)
  6814. rc = -EBUSY;
  6815. return rc;
  6816. }
  6817. /*******************************************************************************
  6818. * FUNCTION: pt_put_device_into_deep_standby_
  6819. *
  6820. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6821. *
  6822. * RETURN:
  6823. * 0 = success
  6824. * !0 = error
  6825. *
  6826. * PARAMETERS:
  6827. * *cd - pointer to core data
  6828. ******************************************************************************/
  6829. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6830. {
  6831. int rc = 0;
  6832. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6833. if (rc)
  6834. rc = -EBUSY;
  6835. return rc;
  6836. }
  6837. /*******************************************************************************
  6838. * FUNCTION: pt_core_poweroff_device_
  6839. *
  6840. * SUMMARY: Disable IRQ and HW power down the device.
  6841. *
  6842. * RETURN:
  6843. * 0 = success
  6844. * !0 = failure
  6845. *
  6846. * PARAMETERS:
  6847. * *cd - pointer to core data
  6848. ******************************************************************************/
  6849. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6850. {
  6851. int rc;
  6852. if (cd->irq_enabled) {
  6853. cd->irq_enabled = false;
  6854. disable_irq_nosync(cd->irq);
  6855. }
  6856. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6857. if (rc < 0)
  6858. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6859. __func__, rc);
  6860. return rc;
  6861. }
  6862. /*******************************************************************************
  6863. * FUNCTION: pt_core_sleep_
  6864. *
  6865. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6866. * configuration in the core platform data structure.
  6867. *
  6868. * RETURN:
  6869. * 0 = success
  6870. * !0 = failure
  6871. *
  6872. * PARAMETERS:
  6873. * *cd - pointer to core data
  6874. ******************************************************************************/
  6875. static int pt_core_sleep_(struct pt_core_data *cd)
  6876. {
  6877. int rc = 0;
  6878. mutex_lock(&cd->system_lock);
  6879. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6880. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6881. mutex_unlock(&cd->system_lock);
  6882. pt_debug(cd->dev, DL_INFO,
  6883. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6884. return 0;
  6885. } else {
  6886. cd->sleep_state = SS_SLEEPING;
  6887. }
  6888. mutex_unlock(&cd->system_lock);
  6889. /* Ensure watchdog and startup works stopped */
  6890. pt_stop_wd_timer(cd);
  6891. cancel_work_sync(&cd->enum_work);
  6892. pt_stop_wd_timer(cd);
  6893. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6894. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6895. rc = pt_core_poweroff_device_(cd);
  6896. if (rc)
  6897. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6898. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6899. pt_debug(cd->dev, DL_INFO,
  6900. "%s: Entering into deep standby mode:\n", __func__);
  6901. rc = pt_put_device_into_deep_standby_(cd);
  6902. if (rc)
  6903. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6904. } else {
  6905. pt_debug(cd->dev, DL_INFO,
  6906. "%s: Entering into deep sleep mode:\n", __func__);
  6907. rc = pt_put_device_into_deep_sleep_(cd);
  6908. if (rc)
  6909. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6910. }
  6911. mutex_lock(&cd->system_lock);
  6912. cd->sleep_state = SS_SLEEP_ON;
  6913. mutex_unlock(&cd->system_lock);
  6914. return rc;
  6915. }
  6916. /*******************************************************************************
  6917. * FUNCTION: pt_core_easywake_on_
  6918. *
  6919. * SUMMARY: Suspend the device with easy wake on the
  6920. * configuration in the core platform data structure.
  6921. *
  6922. * RETURN:
  6923. * 0 = success
  6924. * !0 = failure
  6925. *
  6926. * PARAMETERS:
  6927. * *cd - pointer to core data
  6928. ******************************************************************************/
  6929. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6930. {
  6931. int rc = 0;
  6932. mutex_lock(&cd->system_lock);
  6933. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6934. mutex_unlock(&cd->system_lock);
  6935. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6936. __func__, cd->sleep_state);
  6937. return 0;
  6938. }
  6939. mutex_unlock(&cd->system_lock);
  6940. /* Ensure watchdog and startup works stopped */
  6941. pt_stop_wd_timer(cd);
  6942. cancel_work_sync(&cd->enum_work);
  6943. pt_stop_wd_timer(cd);
  6944. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6945. rc = pt_put_device_into_easy_wakeup_(cd);
  6946. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6947. if (rc)
  6948. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6949. }
  6950. mutex_lock(&cd->system_lock);
  6951. cd->sleep_state = SS_EASY_WAKING_ON;
  6952. mutex_unlock(&cd->system_lock);
  6953. return rc;
  6954. }
  6955. /*******************************************************************************
  6956. * FUNCTION: pt_core_easywake_on
  6957. *
  6958. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6959. *
  6960. * RETURN:
  6961. * 0 = success
  6962. * !0 = failure
  6963. *
  6964. * PARAMETERS:
  6965. * *cd - pointer to core data
  6966. ******************************************************************************/
  6967. static int pt_core_easywake_on(struct pt_core_data *cd)
  6968. {
  6969. int rc = 0;
  6970. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6971. if (rc < 0) {
  6972. pt_debug(cd->dev, DL_ERROR,
  6973. "%s: fail get exclusive ex=%p own=%p\n",
  6974. __func__, cd->exclusive_dev, cd->dev);
  6975. return rc;
  6976. }
  6977. rc = pt_core_easywake_on_(cd);
  6978. if (release_exclusive(cd, cd->dev) < 0)
  6979. pt_debug(cd->dev, DL_ERROR,
  6980. "%s: fail to release exclusive\n", __func__);
  6981. else
  6982. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6983. __func__);
  6984. return rc;
  6985. }
  6986. /*******************************************************************************
  6987. * FUNCTION: pt_core_sleep
  6988. *
  6989. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6990. *
  6991. * RETURN:
  6992. * 0 = success
  6993. * !0 = failure
  6994. *
  6995. * PARAMETERS:
  6996. * *cd - pointer to core data
  6997. ******************************************************************************/
  6998. static int pt_core_sleep(struct pt_core_data *cd)
  6999. {
  7000. int rc = 0;
  7001. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7002. if (rc < 0) {
  7003. pt_debug(cd->dev, DL_ERROR,
  7004. "%s: fail get exclusive ex=%p own=%p\n",
  7005. __func__, cd->exclusive_dev, cd->dev);
  7006. return rc;
  7007. }
  7008. rc = pt_core_sleep_(cd);
  7009. if (release_exclusive(cd, cd->dev) < 0)
  7010. pt_debug(cd->dev, DL_ERROR,
  7011. "%s: fail to release exclusive\n", __func__);
  7012. else
  7013. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7014. __func__);
  7015. return rc;
  7016. }
  7017. /*******************************************************************************
  7018. * FUNCTION: pt_wakeup_host
  7019. *
  7020. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7021. *
  7022. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7023. *
  7024. * RETURN:
  7025. * 0 = success
  7026. * !0 = failure
  7027. *
  7028. * PARAMETERS:
  7029. * *cd - pointer to core data
  7030. ******************************************************************************/
  7031. static int pt_wakeup_host(struct pt_core_data *cd)
  7032. {
  7033. #ifndef EASYWAKE_TSG6
  7034. /* TSG5 EasyWake */
  7035. int rc = 0;
  7036. int event_id;
  7037. int size = get_unaligned_le16(&cd->input_buf[0]);
  7038. /* Validate report */
  7039. if (size != 4 || cd->input_buf[2] != 4)
  7040. rc = -EINVAL;
  7041. event_id = cd->input_buf[3];
  7042. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7043. __func__, event_id, rc);
  7044. if (rc) {
  7045. pt_core_sleep_(cd);
  7046. goto exit;
  7047. }
  7048. /* attention WAKE */
  7049. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7050. exit:
  7051. return rc;
  7052. #else
  7053. /* TSG6 FW1.3 EasyWake */
  7054. int rc = 0;
  7055. int i = 0;
  7056. int report_length;
  7057. /* Validate report */
  7058. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7059. rc = -EINVAL;
  7060. pt_core_sleep_(cd);
  7061. goto exit;
  7062. }
  7063. /* Get gesture id and gesture data length */
  7064. cd->gesture_id = cd->input_buf[3];
  7065. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7066. cd->gesture_data_length = report_length - 4;
  7067. pt_debug(cd->dev, DL_INFO,
  7068. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7069. __func__, cd->gesture_id, cd->gesture_data_length);
  7070. for (i = 0; i < cd->gesture_data_length; i++)
  7071. cd->gesture_data[i] = cd->input_buf[4 + i];
  7072. /* attention WAKE */
  7073. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7074. exit:
  7075. return rc;
  7076. #endif
  7077. }
  7078. /*******************************************************************************
  7079. * FUNCTION: pt_get_touch_axis
  7080. *
  7081. * SUMMARY: Function to calculate touch axis
  7082. *
  7083. * PARAMETERS:
  7084. * *cd - pointer to core data structure
  7085. * *axis - pointer to axis calculation result
  7086. * size - size in bytes
  7087. * max - max value of result
  7088. * *xy_data - pointer to input data to be parsed
  7089. * bofs - bit offset
  7090. ******************************************************************************/
  7091. static void pt_get_touch_axis(struct pt_core_data *cd,
  7092. int *axis, int size, int max, u8 *data, int bofs)
  7093. {
  7094. int nbyte;
  7095. int next;
  7096. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7097. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7098. next++;
  7099. }
  7100. *axis &= max - 1;
  7101. }
  7102. /*******************************************************************************
  7103. * FUNCTION: move_tracking_heatmap_data
  7104. *
  7105. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7106. * system information structure, xy_mode and xy_data.
  7107. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7108. * the tthe_tuner sysfs node under the label "THM"
  7109. *
  7110. * RETURN:
  7111. * 0 = success
  7112. * !0 = failure
  7113. *
  7114. * PARAMETERS:
  7115. * *cd - pointer to core data
  7116. * *si - pointer to the system information structure
  7117. ******************************************************************************/
  7118. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7119. struct pt_sysinfo *si)
  7120. {
  7121. #ifdef TTHE_TUNER_SUPPORT
  7122. int size = get_unaligned_le16(&cd->input_buf[0]);
  7123. if (size)
  7124. tthe_print(cd, cd->input_buf, size, "THM=");
  7125. #endif
  7126. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7127. return 0;
  7128. }
  7129. /*******************************************************************************
  7130. * FUNCTION: move_sensor_data
  7131. *
  7132. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7133. * information structure, xy_mode and xy_data.
  7134. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7135. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7136. *
  7137. * RETURN:
  7138. * 0 = success
  7139. * !0 = failure
  7140. *
  7141. * PARAMETERS:
  7142. * *cd - pointer to core data
  7143. * *si - pointer to the system information structure
  7144. ******************************************************************************/
  7145. static int move_sensor_data(struct pt_core_data *cd,
  7146. struct pt_sysinfo *si)
  7147. {
  7148. #ifdef TTHE_TUNER_SUPPORT
  7149. int size = get_unaligned_le16(&cd->input_buf[0]);
  7150. if (size)
  7151. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7152. #endif
  7153. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7154. return 0;
  7155. }
  7156. /*******************************************************************************
  7157. * FUNCTION: move_button_data
  7158. *
  7159. * SUMMARY: Move the valid button data from the input buffer into the system
  7160. * information structure, xy_mode and xy_data.
  7161. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7162. * the tthe_tuner sysfs node under the label "OpModeData"
  7163. *
  7164. * RETURN:
  7165. * 0 = success
  7166. * !0 = failure
  7167. *
  7168. * PARAMETERS:
  7169. * *cd - pointer to core data
  7170. * *si - pointer to the system information structure
  7171. ******************************************************************************/
  7172. static int move_button_data(struct pt_core_data *cd,
  7173. struct pt_sysinfo *si)
  7174. {
  7175. #ifdef TTHE_TUNER_SUPPORT
  7176. int size = get_unaligned_le16(&cd->input_buf[0]);
  7177. if (size)
  7178. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7179. #endif
  7180. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7181. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7182. "xy_mode");
  7183. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7184. BTN_REPORT_SIZE);
  7185. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7186. "xy_data");
  7187. return 0;
  7188. }
  7189. /*******************************************************************************
  7190. * FUNCTION: move_touch_data
  7191. *
  7192. * SUMMARY: Move the valid touch data from the input buffer into the system
  7193. * information structure, xy_mode and xy_data.
  7194. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7195. * the tthe_tuner sysfs node under the label "OpModeData"
  7196. *
  7197. * RETURN:
  7198. * 0 = success
  7199. * !0 = failure
  7200. *
  7201. * PARAMETERS:
  7202. * *cd - pointer to core data
  7203. * *si - pointer to the system information structure
  7204. ******************************************************************************/
  7205. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7206. {
  7207. int max_tch = si->sensing_conf_data.max_tch;
  7208. int num_cur_tch;
  7209. int length;
  7210. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7211. #ifdef TTHE_TUNER_SUPPORT
  7212. int size = get_unaligned_le16(&cd->input_buf[0]);
  7213. if (size)
  7214. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7215. #endif
  7216. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7217. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7218. si->desc.tch_header_size, "xy_mode");
  7219. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7220. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7221. if (unlikely(num_cur_tch > max_tch))
  7222. num_cur_tch = max_tch;
  7223. length = num_cur_tch * si->desc.tch_record_size;
  7224. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7225. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7226. return 0;
  7227. }
  7228. /*******************************************************************************
  7229. * FUNCTION: move_hid_pen_data
  7230. *
  7231. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7232. * information structure, xy_mode and xy_data.
  7233. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7234. * the tthe_tuner sysfs node under the label "HID" starting with the
  7235. * report ID.
  7236. *
  7237. * RETURN:
  7238. * 0 = success
  7239. * !0 = failure
  7240. *
  7241. * PARAMETERS:
  7242. * *cd - pointer to core data
  7243. * *si - pointer to the system information structure
  7244. ******************************************************************************/
  7245. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7246. {
  7247. #ifdef TTHE_TUNER_SUPPORT
  7248. int size = get_unaligned_le16(&cd->input_buf[0]);
  7249. if (size) {
  7250. /*
  7251. * HID over USB does not require the two byte length field, so
  7252. * this should print from input_buf[2] but to keep both finger
  7253. * and pen reports the same the length is included
  7254. */
  7255. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7256. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7257. "HID-USB=");
  7258. else
  7259. tthe_print(cd, &(cd->input_buf[0]), size,
  7260. "HID-I2C=");
  7261. }
  7262. #endif
  7263. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7264. return 0;
  7265. }
  7266. /*******************************************************************************
  7267. * FUNCTION: parse_touch_input
  7268. *
  7269. * SUMMARY: Parse the touch report and take action based on the touch
  7270. * report_id.
  7271. *
  7272. * RETURN:
  7273. * 0 = success
  7274. * !0 = failure
  7275. *
  7276. * PARAMETERS:
  7277. * *cd - pointer to core data
  7278. * size - size of touch record
  7279. ******************************************************************************/
  7280. static int parse_touch_input(struct pt_core_data *cd, int size)
  7281. {
  7282. struct pt_sysinfo *si = &cd->sysinfo;
  7283. int report_id = cd->input_buf[2];
  7284. int rc = -EINVAL;
  7285. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7286. __func__);
  7287. if (!si->ready) {
  7288. pt_debug(cd->dev, DL_ERROR,
  7289. "%s: Need system information to parse touches\n",
  7290. __func__);
  7291. return 0;
  7292. }
  7293. if (!si->xy_mode || !si->xy_data)
  7294. return rc;
  7295. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7296. rc = move_touch_data(cd, si);
  7297. else if (report_id == PT_HID_PEN_REPORT_ID)
  7298. rc = move_hid_pen_data(cd, si);
  7299. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7300. rc = move_button_data(cd, si);
  7301. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7302. rc = move_sensor_data(cd, si);
  7303. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7304. rc = move_tracking_heatmap_data(cd, si);
  7305. if (rc)
  7306. return rc;
  7307. /* attention IRQ */
  7308. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7309. return 0;
  7310. }
  7311. /*******************************************************************************
  7312. * FUNCTION: parse_command_input
  7313. *
  7314. * SUMMARY: Move the response data from the input buffer to the response buffer
  7315. *
  7316. * RETURN:
  7317. * 0 = success
  7318. * !0 = failure
  7319. *
  7320. * PARAMETERS:
  7321. * *cd - pointer to core data
  7322. * size - size of response data
  7323. ******************************************************************************/
  7324. static int parse_command_input(struct pt_core_data *cd, int size)
  7325. {
  7326. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7327. __func__);
  7328. memcpy(cd->response_buf, cd->input_buf, size);
  7329. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7330. if (size && cd->show_tt_data) {
  7331. if (cd->pip2_prot_active)
  7332. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7333. else
  7334. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7335. }
  7336. #endif
  7337. mutex_lock(&cd->system_lock);
  7338. cd->hid_cmd_state = 0;
  7339. mutex_unlock(&cd->system_lock);
  7340. wake_up(&cd->wait_q);
  7341. return 0;
  7342. }
  7343. /*******************************************************************************
  7344. * FUNCTION: pt_allow_enumeration
  7345. *
  7346. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7347. * FW sentinel is seen.
  7348. *
  7349. * RETURN:
  7350. * true = allow enumeration or fully re-probe
  7351. * false = skip enumeration and fully re-probe
  7352. *
  7353. * PARAMETERS:
  7354. * *cd - pointer to core data
  7355. ******************************************************************************/
  7356. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7357. {
  7358. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7359. (!cd->hid_reset_cmd_state) &&
  7360. (cd->core_probe_complete) &&
  7361. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7362. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7363. (cd->mode == PT_MODE_OPERATIONAL)) {
  7364. return true;
  7365. }
  7366. if ((!cd->hid_reset_cmd_state) &&
  7367. (cd->core_probe_complete) &&
  7368. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7369. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7370. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7371. return true;
  7372. }
  7373. pt_debug(cd->dev, DL_INFO,
  7374. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7375. __func__,
  7376. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7377. "core_probe_complete(1)", cd->core_probe_complete,
  7378. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7379. "active_dut_gen(0,2)", cd->active_dut_generation);
  7380. return false;
  7381. }
  7382. /*******************************************************************************
  7383. * FUNCTION: pt_is_touch_report
  7384. *
  7385. * SUMMARY: Determine if a report ID should be treated as a touch report
  7386. *
  7387. * RETURN:
  7388. * true = report ID is a touch report
  7389. * false = report ID is not a touch report
  7390. *
  7391. * PARAMETERS:
  7392. * id - Report ID
  7393. ******************************************************************************/
  7394. static bool pt_is_touch_report(int id)
  7395. {
  7396. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7397. id == PT_HID_PEN_REPORT_ID ||
  7398. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7399. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7400. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7401. return true;
  7402. else
  7403. return false;
  7404. }
  7405. /*******************************************************************************
  7406. * FUNCTION: pt_parse_input
  7407. *
  7408. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7409. * on if its a response to a command or asynchronous touch data / reset
  7410. * sentinel. PIP2.x messages have additional error checking that is
  7411. * parsed (SEQ match from cmd to rsp, CRC valid).
  7412. * Look for special packets based on unique lengths:
  7413. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7414. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7415. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7416. * 0xFFXX - Empty buffer (PIP 1.7+)
  7417. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7418. * the initial probe has not yet been done.
  7419. *
  7420. * RETURN:
  7421. * 0 = success
  7422. * !0 = failure
  7423. *
  7424. * PARAMETERS:
  7425. * *cd - pointer to core data
  7426. ******************************************************************************/
  7427. static int pt_parse_input(struct pt_core_data *cd)
  7428. {
  7429. int report_id;
  7430. int cmd_id;
  7431. int is_command = 0;
  7432. int size;
  7433. int print_size;
  7434. bool touch_report = true;
  7435. unsigned short calc_crc;
  7436. unsigned short resp_crc;
  7437. cd->fw_sys_mode_in_standby_state = false;
  7438. size = get_unaligned_le16(&cd->input_buf[0]);
  7439. print_size = size;
  7440. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7441. __func__, print_size);
  7442. if (print_size <= PT_MAX_INPUT)
  7443. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7444. "<<< Read buf");
  7445. if (size == 0 ||
  7446. (size == 11 &&
  7447. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7448. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7449. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7450. PIP2_CMD_COMMAND_ID_MASK) ==
  7451. PIP2_CMD_ID_STATUS)) {
  7452. touch_report = false;
  7453. cd->hw_detected = true;
  7454. cd->bl_pip_ver_ready = false;
  7455. cd->app_pip_ver_ready = false;
  7456. if (size == 0) {
  7457. mutex_lock(&cd->system_lock);
  7458. cd->pip2_prot_active = false;
  7459. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7460. /*
  7461. * For Gen5/6 this sentinel could be from
  7462. * the BL or FW. Attempt to set the correct
  7463. * mode based on the previous PIP command.
  7464. */
  7465. if (cd->hid_cmd_state ==
  7466. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7467. cd->mode = PT_MODE_OPERATIONAL;
  7468. cd->startup_status =
  7469. STARTUP_STATUS_FW_RESET_SENTINEL;
  7470. } else if (cd->hid_cmd_state ==
  7471. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7472. cd->hid_reset_cmd_state) {
  7473. cd->mode = PT_MODE_BOOTLOADER;
  7474. cd->startup_status =
  7475. STARTUP_STATUS_BL_RESET_SENTINEL;
  7476. } else {
  7477. cd->mode = PT_MODE_UNKNOWN;
  7478. cd->startup_status =
  7479. STARTUP_STATUS_START;
  7480. }
  7481. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7482. pt_debug(cd->dev, DL_INFO,
  7483. "%s: ATM Gen5/6 %s sentinel received\n",
  7484. __func__,
  7485. (cd->mode == PT_MODE_OPERATIONAL ?
  7486. "FW" :
  7487. (cd->mode == PT_MODE_BOOTLOADER ?
  7488. "BL" : "Unknown")));
  7489. } else {
  7490. cd->mode = PT_MODE_OPERATIONAL;
  7491. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7492. cd->startup_status =
  7493. STARTUP_STATUS_FW_RESET_SENTINEL;
  7494. pt_debug(cd->dev, DL_INFO,
  7495. "%s: ATM PT/TT FW sentinel received\n",
  7496. __func__);
  7497. }
  7498. mutex_unlock(&cd->system_lock);
  7499. if (pt_allow_enumeration(cd)) {
  7500. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7501. pt_debug(cd->dev, DL_INFO,
  7502. "%s: Queue Restart\n", __func__);
  7503. pt_queue_restart(cd);
  7504. } else {
  7505. pt_debug(cd->dev, DL_INFO,
  7506. "%s: Queue Enum\n", __func__);
  7507. pt_queue_enum(cd);
  7508. }
  7509. } else {
  7510. pt_debug(cd->dev, DL_INFO,
  7511. "%s: Sentinel - No Queued Action\n",
  7512. __func__);
  7513. }
  7514. } else {
  7515. /* Sentinel must be from TT/TC BL */
  7516. mutex_lock(&cd->system_lock);
  7517. cd->pip2_prot_active = true;
  7518. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7519. cd->mode = PT_MODE_BOOTLOADER;
  7520. cd->sysinfo.ready = false;
  7521. mutex_unlock(&cd->system_lock);
  7522. pt_debug(cd->dev, DL_INFO,
  7523. "%s: BL Reset sentinel received\n", __func__);
  7524. if (cd->flashless_dut &&
  7525. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7526. pt_debug(cd->dev, DL_INFO,
  7527. "%s: BL to RAM for flashless DUT\n",
  7528. __func__);
  7529. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7530. }
  7531. }
  7532. mutex_lock(&cd->system_lock);
  7533. memcpy(cd->response_buf, cd->input_buf, 2);
  7534. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7535. mutex_unlock(&cd->system_lock);
  7536. pt_debug(cd->dev, DL_WARN,
  7537. "%s: Device Initiated Reset\n", __func__);
  7538. wake_up(&cd->wait_q);
  7539. return 0;
  7540. }
  7541. cd->hid_reset_cmd_state = 0;
  7542. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7543. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7544. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7545. cd->hid_cmd_state = 0;
  7546. wake_up(&cd->wait_q);
  7547. mutex_unlock(&cd->system_lock);
  7548. return 0;
  7549. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7550. /*
  7551. * This debug message below is used by PBATS to calculate the
  7552. * time from the last lift off IRQ to when FW exits LFT mode.
  7553. */
  7554. touch_report = false;
  7555. pt_debug(cd->dev, DL_WARN,
  7556. "%s: DUT - Empty buffer detected\n", __func__);
  7557. return 0;
  7558. } else if (size > PT_MAX_INPUT || size < 0) {
  7559. pt_debug(cd->dev, DL_ERROR,
  7560. "%s: DUT - Unexpected len field in active bus data!\n",
  7561. __func__);
  7562. return -EINVAL;
  7563. }
  7564. if (cd->pip2_prot_active) {
  7565. pt_debug(cd->dev, DL_DEBUG,
  7566. "%s: Decode PIP2.x Response\n", __func__);
  7567. /* PIP2 does not have a report id, hard code it */
  7568. report_id = 0x00;
  7569. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7570. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7571. resp_crc = cd->input_buf[size - 2] << 8;
  7572. resp_crc |= cd->input_buf[size - 1];
  7573. if ((cd->pip2_cmd_tag_seq !=
  7574. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7575. (resp_crc != calc_crc) &&
  7576. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7577. == PT_PIP_TOUCH_REPORT_ID) ||
  7578. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7579. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7580. pt_debug(cd->dev, DL_WARN,
  7581. "%s: %s %d %s\n",
  7582. __func__,
  7583. "Received PIP1 report id =",
  7584. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7585. "when expecting a PIP2 report - IGNORE report");
  7586. return 0;
  7587. }
  7588. is_command = 1;
  7589. touch_report = false;
  7590. } else {
  7591. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7592. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7593. }
  7594. #ifdef TTDL_DIAGNOSTICS
  7595. pt_debug(cd->dev, DL_DEBUG,
  7596. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7597. __func__, cd->pip2_prot_active, report_id,
  7598. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7599. #endif /* TTDL_DIAGNOSTICS */
  7600. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7601. pt_wakeup_host(cd);
  7602. #ifdef TTHE_TUNER_SUPPORT
  7603. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7604. #endif
  7605. return 0;
  7606. }
  7607. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7608. msecs_to_jiffies(cd->watchdog_interval));
  7609. /*
  7610. * If it is PIP2 response, the report_id has been set to 0,
  7611. * so it will not be parsed as a touch packet.
  7612. */
  7613. if (!pt_is_touch_report(report_id)) {
  7614. is_command = 1;
  7615. touch_report = false;
  7616. }
  7617. if (unlikely(is_command)) {
  7618. parse_command_input(cd, size);
  7619. return 0;
  7620. }
  7621. if (touch_report)
  7622. parse_touch_input(cd, size);
  7623. return 0;
  7624. }
  7625. /*******************************************************************************
  7626. * FUNCTION: pt_read_input
  7627. *
  7628. * SUMMARY: Reads incoming data off of the active bus
  7629. *
  7630. * RETURN:
  7631. * 0 = success
  7632. * !0 = failure
  7633. *
  7634. * PARAMETERS:
  7635. * *cd - pointer to core data
  7636. ******************************************************************************/
  7637. static int pt_read_input(struct pt_core_data *cd)
  7638. {
  7639. struct device *dev = cd->dev;
  7640. int rc = 0;
  7641. int t;
  7642. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7643. /*
  7644. * Workaround for easywake failure
  7645. * Interrupt for easywake, wait for bus controller to wake
  7646. */
  7647. mutex_lock(&cd->system_lock);
  7648. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7649. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7650. mutex_unlock(&cd->system_lock);
  7651. if (!dev->power.is_suspended)
  7652. goto read;
  7653. t = wait_event_timeout(cd->wait_q,
  7654. (cd->wait_until_wake == 1),
  7655. msecs_to_jiffies(2000));
  7656. #ifdef TTDL_DIAGNOSTICS
  7657. if (IS_TMO(t)) {
  7658. cd->bus_transmit_error_count++;
  7659. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7660. pt_debug(dev, DL_ERROR,
  7661. "%s: !!!I2C Transmission Error %d\n",
  7662. __func__,
  7663. cd->bus_transmit_error_count);
  7664. }
  7665. #else
  7666. if (IS_TMO(t))
  7667. pt_queue_enum(cd);
  7668. #endif /* TTDL_DIAGNOSTICS */
  7669. goto read;
  7670. }
  7671. }
  7672. mutex_unlock(&cd->system_lock);
  7673. read:
  7674. /* Try reading up to 'retry' times */
  7675. while (retry-- != 0) {
  7676. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7677. PT_MAX_INPUT);
  7678. if (!rc) {
  7679. pt_debug(dev, DL_DEBUG,
  7680. "%s: Read input successfully\n", __func__);
  7681. goto read_exit;
  7682. }
  7683. usleep_range(1000, 2000);
  7684. }
  7685. pt_debug(dev, DL_ERROR,
  7686. "%s: Error getting report, rc=%d\n", __func__, rc);
  7687. read_exit:
  7688. return rc;
  7689. }
  7690. /*******************************************************************************
  7691. * FUNCTION: pt_irq
  7692. *
  7693. * SUMMARY: Process all detected interrupts
  7694. *
  7695. * RETURN:
  7696. * IRQ_HANDLED - Finished processing the interrupt
  7697. *
  7698. * PARAMETERS:
  7699. * irq - IRQ number
  7700. * *handle - pointer to core data struct
  7701. ******************************************************************************/
  7702. irqreturn_t pt_irq(int irq, void *handle)
  7703. {
  7704. struct pt_core_data *cd = handle;
  7705. int rc = 0;
  7706. if (!pt_check_irq_asserted(cd))
  7707. return IRQ_HANDLED;
  7708. rc = pt_read_input(cd);
  7709. #ifdef TTDL_DIAGNOSTICS
  7710. cd->irq_count++;
  7711. /* Used to calculate T-Refresh */
  7712. if (cd->t_refresh_active) {
  7713. if (cd->t_refresh_count == 0) {
  7714. cd->t_refresh_time = jiffies;
  7715. cd->t_refresh_count++;
  7716. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7717. cd->t_refresh_count++;
  7718. } else {
  7719. cd->t_refresh_active = 0;
  7720. cd->t_refresh_time =
  7721. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7722. }
  7723. }
  7724. #endif /* TTDL_DIAGNOSTICS */
  7725. if (!rc)
  7726. pt_parse_input(cd);
  7727. return IRQ_HANDLED;
  7728. }
  7729. /*******************************************************************************
  7730. * FUNCTION: _pt_subscribe_attention
  7731. *
  7732. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7733. * to subscribe themselves into the TTDL attention list
  7734. *
  7735. * RETURN:
  7736. * 0 = success
  7737. * !0 = failure
  7738. *
  7739. * PARAMETERS:
  7740. * *dev - pointer to device structure
  7741. * type - attention type enum
  7742. * *id - ID of the module calling this function
  7743. * *func - callback function pointer to be called when notified
  7744. * mode - attention mode
  7745. ******************************************************************************/
  7746. int _pt_subscribe_attention(struct device *dev,
  7747. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7748. int mode)
  7749. {
  7750. struct pt_core_data *cd = dev_get_drvdata(dev);
  7751. struct atten_node *atten, *atten_new;
  7752. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7753. if (!atten_new)
  7754. return -ENOMEM;
  7755. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7756. dev_name(cd->dev));
  7757. spin_lock(&cd->spinlock);
  7758. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7759. if (atten->id == id && atten->mode == mode) {
  7760. spin_unlock(&cd->spinlock);
  7761. kfree(atten_new);
  7762. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7763. __func__,
  7764. "already subscribed attention",
  7765. dev, "mode", mode);
  7766. return 0;
  7767. }
  7768. }
  7769. atten_new->id = id;
  7770. atten_new->dev = dev;
  7771. atten_new->mode = mode;
  7772. atten_new->func = func;
  7773. list_add(&atten_new->node, &cd->atten_list[type]);
  7774. spin_unlock(&cd->spinlock);
  7775. return 0;
  7776. }
  7777. /*******************************************************************************
  7778. * FUNCTION: _pt_unsubscribe_attention
  7779. *
  7780. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7781. * to unsubscribe themselves from the TTDL attention list
  7782. *
  7783. * RETURN:
  7784. * 0 = success
  7785. * !0 = failure
  7786. *
  7787. * PARAMETERS:
  7788. * *dev - pointer to device structure
  7789. * type - attention type enum
  7790. * *id - ID of the module calling this function
  7791. * *func - function pointer
  7792. * mode - attention mode
  7793. ******************************************************************************/
  7794. int _pt_unsubscribe_attention(struct device *dev,
  7795. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7796. int mode)
  7797. {
  7798. struct pt_core_data *cd = dev_get_drvdata(dev);
  7799. struct atten_node *atten, *atten_n;
  7800. spin_lock(&cd->spinlock);
  7801. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7802. if (atten->id == id && atten->mode == mode) {
  7803. list_del(&atten->node);
  7804. spin_unlock(&cd->spinlock);
  7805. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7806. __func__,
  7807. "unsub for atten->dev", atten->dev,
  7808. "atten->mode", atten->mode);
  7809. kfree(atten);
  7810. return 0;
  7811. }
  7812. }
  7813. spin_unlock(&cd->spinlock);
  7814. return -ENODEV;
  7815. }
  7816. /*******************************************************************************
  7817. * FUNCTION: _pt_request_exclusive
  7818. *
  7819. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7820. * to request exclusive access
  7821. *
  7822. * RETURN:
  7823. * 0 = success
  7824. * !0 = failure
  7825. *
  7826. * PARAMETERS:
  7827. * *dev - pointer to device structure
  7828. * timeout_ms - timeout to wait for exclusive access
  7829. ******************************************************************************/
  7830. static int _pt_request_exclusive(struct device *dev,
  7831. int timeout_ms)
  7832. {
  7833. struct pt_core_data *cd = dev_get_drvdata(dev);
  7834. return request_exclusive(cd, (void *)dev, timeout_ms);
  7835. }
  7836. /*******************************************************************************
  7837. * FUNCTION: _pt_release_exclusive
  7838. *
  7839. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7840. * to release exclusive access
  7841. *
  7842. * RETURN:
  7843. * 0 = success
  7844. * !0 = failure
  7845. *
  7846. * PARAMETERS:
  7847. * *dev - pointer to device structure
  7848. ******************************************************************************/
  7849. static int _pt_release_exclusive(struct device *dev)
  7850. {
  7851. struct pt_core_data *cd = dev_get_drvdata(dev);
  7852. return release_exclusive(cd, (void *)dev);
  7853. }
  7854. /*******************************************************************************
  7855. * FUNCTION: _pt_request_reset
  7856. *
  7857. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7858. * to request the DUT to be reset. Function returns err if refused or
  7859. * timeout occurs (Note: core uses fixed timeout period).
  7860. *
  7861. * NOTE: Function blocks until ISR occurs.
  7862. *
  7863. * RETURN:
  7864. * 0 = success
  7865. * !0 = failure
  7866. *
  7867. * PARAMETERS:
  7868. * *dev - pointer to device structure
  7869. * protect - flag to call protected or non-protected
  7870. ******************************************************************************/
  7871. static int _pt_request_reset(struct device *dev, int protect)
  7872. {
  7873. struct pt_core_data *cd = dev_get_drvdata(dev);
  7874. int rc;
  7875. rc = pt_dut_reset(cd, protect);
  7876. if (rc < 0) {
  7877. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7878. __func__, rc);
  7879. }
  7880. return rc;
  7881. }
  7882. /*******************************************************************************
  7883. * FUNCTION: _pt_request_enum
  7884. *
  7885. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7886. * to request TTDL to queue an enum. This function will return err
  7887. * if refused; if no error then enum has completed and system is in
  7888. * normal operation mode.
  7889. *
  7890. * RETURN:
  7891. * 0 = success
  7892. * !0 = failure
  7893. *
  7894. * PARAMETERS:
  7895. * *dev - pointer to device structure
  7896. * wait - boolean to determine if to wait for startup event
  7897. ******************************************************************************/
  7898. static int _pt_request_enum(struct device *dev, bool wait)
  7899. {
  7900. struct pt_core_data *cd = dev_get_drvdata(dev);
  7901. pt_queue_enum(cd);
  7902. if (wait)
  7903. wait_event_timeout(cd->wait_q,
  7904. cd->startup_state == STARTUP_NONE,
  7905. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7906. return 0;
  7907. }
  7908. /*******************************************************************************
  7909. * FUNCTION: _pt_request_sysinfo
  7910. *
  7911. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7912. * to request the pointer to the system information structure. This
  7913. * function will return NULL if sysinfo has not been acquired from the
  7914. * DUT yet.
  7915. *
  7916. * RETURN: Pointer to the system information struct
  7917. *
  7918. * PARAMETERS:
  7919. * *dev - pointer to device structure
  7920. ******************************************************************************/
  7921. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7922. {
  7923. struct pt_core_data *cd = dev_get_drvdata(dev);
  7924. int rc = 0;
  7925. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7926. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7927. !cd->sysinfo.ready) {
  7928. rc = pt_hid_output_get_sysinfo_(cd);
  7929. if (rc) {
  7930. pt_debug(cd->dev, DL_ERROR,
  7931. "%s: Error getting sysinfo rc=%d\n",
  7932. __func__, rc);
  7933. }
  7934. }
  7935. if (cd->sysinfo.ready)
  7936. return &cd->sysinfo;
  7937. return NULL;
  7938. }
  7939. /*******************************************************************************
  7940. * FUNCTION: _pt_request_loader_pdata
  7941. *
  7942. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7943. * to request the pointer to the loader platform data
  7944. *
  7945. * RETURN: Pointer to the loader platform data struct
  7946. *
  7947. * PARAMETERS:
  7948. * *dev - pointer to device structure
  7949. ******************************************************************************/
  7950. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7951. struct device *dev)
  7952. {
  7953. struct pt_core_data *cd = dev_get_drvdata(dev);
  7954. return cd->pdata->loader_pdata;
  7955. }
  7956. /*******************************************************************************
  7957. * FUNCTION: _pt_request_start_wd
  7958. *
  7959. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7960. * to request to start the TTDL watchdog
  7961. *
  7962. * RETURN:
  7963. * 0 = success
  7964. * !0 = failure
  7965. *
  7966. * PARAMETERS:
  7967. * *dev - pointer to device structure
  7968. ******************************************************************************/
  7969. static int _pt_request_start_wd(struct device *dev)
  7970. {
  7971. struct pt_core_data *cd = dev_get_drvdata(dev);
  7972. pt_start_wd_timer(cd);
  7973. return 0;
  7974. }
  7975. /*******************************************************************************
  7976. * FUNCTION: _pt_request_stop_wd
  7977. *
  7978. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7979. * to request to stop the TTDL watchdog
  7980. *
  7981. * RETURN:
  7982. * 0 = success
  7983. * !0 = failure
  7984. *
  7985. * PARAMETERS:
  7986. * *dev - pointer to device structure
  7987. ******************************************************************************/
  7988. static int _pt_request_stop_wd(struct device *dev)
  7989. {
  7990. struct pt_core_data *cd = dev_get_drvdata(dev);
  7991. pt_stop_wd_timer(cd);
  7992. return 0;
  7993. }
  7994. /*******************************************************************************
  7995. * FUNCTION: pt_pip2_launch_app
  7996. *
  7997. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7998. * the FW reset sentinel to indicate the function succeeded.
  7999. *
  8000. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8001. * in a timeout delay and with the DUT being reset with an XRES.
  8002. *
  8003. * RETURN:
  8004. * 0 = success
  8005. * !0 = failure
  8006. *
  8007. * PARAMETERS:
  8008. * *cd - pointer to core data structure
  8009. * protect - flag to call protected or non-protected
  8010. ******************************************************************************/
  8011. static int pt_pip2_launch_app(struct device *dev, int protect)
  8012. {
  8013. struct pt_core_data *cd = dev_get_drvdata(dev);
  8014. u16 actual_read_len;
  8015. u16 tmp_startup_status = cd->startup_status;
  8016. u8 read_buf[12];
  8017. u8 status;
  8018. int time = 0;
  8019. int rc = 0;
  8020. mutex_lock(&cd->system_lock);
  8021. cd->startup_status = STARTUP_STATUS_START;
  8022. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8023. mutex_unlock(&cd->system_lock);
  8024. rc = _pt_request_pip2_send_cmd(dev, protect,
  8025. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8026. &actual_read_len);
  8027. if (rc) {
  8028. pt_debug(dev, DL_ERROR,
  8029. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8030. __func__, rc);
  8031. } else {
  8032. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8033. /* Test for no or invalid image in FLASH, no point to reset */
  8034. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8035. rc = status;
  8036. goto exit;
  8037. }
  8038. /* Any other boot failure */
  8039. if (status != 0) {
  8040. pt_debug(dev, DL_ERROR,
  8041. "%s: FW did not EXECUTE, status = %d\n",
  8042. __func__, status);
  8043. rc = status;
  8044. }
  8045. }
  8046. if (rc) {
  8047. pt_debug(dev, DL_ERROR,
  8048. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8049. __func__, rc);
  8050. goto exit;
  8051. }
  8052. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8053. msleep(20);
  8054. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8055. __func__, time, cd->startup_status);
  8056. time += 20;
  8057. }
  8058. if (cd->startup_status == STARTUP_STATUS_START) {
  8059. pt_debug(cd->dev, DL_WARN,
  8060. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8061. rc = -ETIME;
  8062. }
  8063. exit:
  8064. if (cd->startup_status == STARTUP_STATUS_START) {
  8065. /* Reset to original state because we could be stuck in BL */
  8066. mutex_lock(&cd->system_lock);
  8067. cd->startup_status = tmp_startup_status;
  8068. mutex_unlock(&cd->system_lock);
  8069. }
  8070. return rc;
  8071. }
  8072. /*******************************************************************************
  8073. * FUNCTION: _pt_request_pip2_launch_app
  8074. *
  8075. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8076. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8077. * consumed.
  8078. *
  8079. * RETURN:
  8080. * 0 = success
  8081. * !0 = failure
  8082. *
  8083. * PARAMETERS:
  8084. * *cd - pointer to core data structure
  8085. ******************************************************************************/
  8086. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8087. {
  8088. return pt_pip2_launch_app(dev, protect);
  8089. }
  8090. /*******************************************************************************
  8091. * FUNCTION: _pt_request_wait_for_enum_state
  8092. *
  8093. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8094. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8095. *
  8096. * RETURN:
  8097. * 0 = success
  8098. * !0 = failure
  8099. *
  8100. * PARAMETERS:
  8101. * *cd - pointer to core data structure
  8102. * timeout - timeout for how long to wait
  8103. * state - enum state to wait for
  8104. ******************************************************************************/
  8105. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8106. int state)
  8107. {
  8108. int rc = 0;
  8109. int t;
  8110. struct pt_core_data *cd = dev_get_drvdata(dev);
  8111. t = wait_event_timeout(cd->wait_q,
  8112. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8113. msecs_to_jiffies(timeout));
  8114. if (IS_TMO(t)) {
  8115. pt_debug(cd->dev, DL_ERROR,
  8116. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8117. __func__, state, timeout);
  8118. pt_debug(cd->dev, DL_WARN,
  8119. "%s: enum state reached 0x%04X\n",
  8120. __func__, cd->startup_status);
  8121. rc = -ETIME;
  8122. } else if (cd->startup_status & state) {
  8123. pt_debug(cd->dev, DL_INFO,
  8124. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8125. __func__, cd->startup_status,
  8126. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8127. } else {
  8128. if (t == 1) {
  8129. pt_debug(
  8130. cd->dev, DL_ERROR,
  8131. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8132. __func__, state, timeout);
  8133. rc = -ETIME;
  8134. } else {
  8135. pt_debug(
  8136. cd->dev, DL_ERROR,
  8137. "%s: Enum state 0x%04X not reached in %dms\n",
  8138. __func__, state, timeout - jiffies_to_msecs(t));
  8139. rc = -EINVAL;
  8140. }
  8141. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8142. __func__, cd->startup_status);
  8143. }
  8144. return rc;
  8145. }
  8146. /*******************************************************************************
  8147. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8148. *
  8149. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8150. * deep sleep.
  8151. *
  8152. * RETURN:
  8153. * 0 = success
  8154. * !0 = error
  8155. *
  8156. * PARAMETERS:
  8157. * *cd - pointer to core data
  8158. ******************************************************************************/
  8159. static int pt_core_wake_device_from_deep_sleep_(
  8160. struct pt_core_data *cd)
  8161. {
  8162. int rc;
  8163. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8164. if (rc)
  8165. rc = -EAGAIN;
  8166. /* Prevent failure on sequential wake/sleep requests from OS */
  8167. msleep(20);
  8168. return rc;
  8169. }
  8170. /*******************************************************************************
  8171. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8172. *
  8173. * SUMMARY: Wake up device from Easy-Wake state.
  8174. *
  8175. * RETURN:
  8176. * 0 = success
  8177. * !0 = failure
  8178. *
  8179. * PARAMETERS:
  8180. * *cd - pointer to core data
  8181. ******************************************************************************/
  8182. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8183. {
  8184. int rc = 0;
  8185. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8186. return rc;
  8187. }
  8188. /*******************************************************************************
  8189. * FUNCTION: pt_restore_parameters_
  8190. *
  8191. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8192. * back to the DUT
  8193. *
  8194. * RETURN:
  8195. * 0 = success
  8196. * !0 = failure
  8197. *
  8198. * PARAMETERS:
  8199. * *cd - pointer the core data structure
  8200. ******************************************************************************/
  8201. static int pt_restore_parameters_(struct pt_core_data *cd)
  8202. {
  8203. struct param_node *param;
  8204. int rc = 0;
  8205. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8206. goto exit;
  8207. spin_lock(&cd->spinlock);
  8208. list_for_each_entry(param, &cd->param_list, node) {
  8209. spin_unlock(&cd->spinlock);
  8210. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8211. __func__, param->id, param->value);
  8212. rc = pt_pip_set_param_(cd, param->id,
  8213. param->value, param->size);
  8214. if (rc)
  8215. goto exit;
  8216. spin_lock(&cd->spinlock);
  8217. }
  8218. spin_unlock(&cd->spinlock);
  8219. exit:
  8220. return rc;
  8221. }
  8222. /*******************************************************************************
  8223. * FUNCTION: pt_pip2_exit_bl_
  8224. *
  8225. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8226. * a DUT that may not have flash and will need FW to be loaded into RAM
  8227. *
  8228. * RETURN:
  8229. * 0 = success
  8230. * !0 = failure
  8231. *
  8232. * PARAMETERS:
  8233. * *cd - pointer the core data structure
  8234. * *status_str - pointer to optional status string buffer
  8235. * buf_size - size of status_str buffer
  8236. ******************************************************************************/
  8237. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8238. {
  8239. int rc;
  8240. int wait_time = 0;
  8241. u8 mode = PT_MODE_UNKNOWN;
  8242. bool load_status_str = false;
  8243. /*
  8244. * Below function has protective call to ensure no enum is still on
  8245. * going, while this kind of protection should be applied widely in
  8246. * future (TODO).
  8247. */
  8248. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8249. if (status_str && buf_size <= 50)
  8250. load_status_str = true;
  8251. if (mode == PT_MODE_BOOTLOADER) {
  8252. if (cd->flashless_dut == 1) {
  8253. rc = pt_hw_hard_reset(cd);
  8254. } else {
  8255. rc = pt_pip2_launch_app(cd->dev,
  8256. PT_CORE_CMD_UNPROTECTED);
  8257. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8258. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8259. __func__, "Invalid image in FLASH rc", rc);
  8260. } else if (rc) {
  8261. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8262. __func__, "Failed to launch app rc", rc);
  8263. }
  8264. }
  8265. if (!rc) {
  8266. if (cd->flashless_dut == 1) {
  8267. /* Wait for BL to complete before enum */
  8268. rc = _pt_request_wait_for_enum_state(cd->dev,
  8269. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8270. if (rc && load_status_str) {
  8271. strlcpy(status_str, "No FW sentinel after BL",
  8272. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8273. goto exit;
  8274. }
  8275. }
  8276. /*
  8277. * If the host wants to interact with the FW or do a
  8278. * forced calibration, the FW must be out of BOOT mode
  8279. * and the system information must have been retrieved.
  8280. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8281. * If, however, the enumeration does not reach this
  8282. * point, the DUT may still be in APP mode so test
  8283. * for all conditions.
  8284. */
  8285. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8286. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8287. if (!rc || cd->startup_status >=
  8288. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8289. mutex_lock(&cd->system_lock);
  8290. cd->mode = PT_MODE_OPERATIONAL;
  8291. mutex_unlock(&cd->system_lock);
  8292. }
  8293. if (rc) {
  8294. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8295. __func__, "Failed to enum with DUT",
  8296. cd->startup_status);
  8297. if (load_status_str && !(cd->startup_status &
  8298. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8299. strlcpy(status_str, "FW Stuck in Boot mode",
  8300. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8301. goto exit;
  8302. }
  8303. }
  8304. /*
  8305. * The coming FW sentinel could wake up the event
  8306. * queue, which has chance to be taken by next command
  8307. * wrongly. Following delay is a workaround to avoid
  8308. * this issue for most situations.
  8309. */
  8310. msleep(20);
  8311. pt_start_wd_timer(cd);
  8312. }
  8313. if (load_status_str) {
  8314. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8315. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8316. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8317. else if (!rc)
  8318. strlcpy(status_str, "Entered APP from BL mode",
  8319. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8320. else
  8321. strlcpy(status_str, "Failed to enter APP from BL mode",
  8322. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8323. }
  8324. } else if (mode == PT_MODE_OPERATIONAL) {
  8325. mutex_lock(&cd->system_lock);
  8326. cd->mode = mode;
  8327. mutex_unlock(&cd->system_lock);
  8328. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8329. if (load_status_str) {
  8330. if (!rc)
  8331. strlcpy(status_str, "Already in APP mode",
  8332. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8333. else
  8334. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8335. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8336. }
  8337. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8338. mutex_lock(&cd->system_lock);
  8339. cd->mode = mode;
  8340. mutex_unlock(&cd->system_lock);
  8341. if (load_status_str)
  8342. strlcpy(status_str, "Failed to determine active mode",
  8343. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8344. }
  8345. exit:
  8346. if (!rc)
  8347. pt_start_wd_timer(cd);
  8348. return rc;
  8349. }
  8350. /*******************************************************************************
  8351. * FUNCTION: pt_pip2_exit_bl
  8352. *
  8353. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8354. * access.
  8355. *
  8356. * RETURN:
  8357. * 0 = success
  8358. * !0 = failure
  8359. *
  8360. * PARAMETERS:
  8361. * *cd - pointer the core data structure
  8362. * *status_str - pointer to optional status string buffer
  8363. * buf_size - size of status_str buffer
  8364. ******************************************************************************/
  8365. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8366. {
  8367. int rc;
  8368. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8369. if (rc < 0) {
  8370. pt_debug(cd->dev, DL_ERROR,
  8371. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8372. cd->exclusive_dev, cd->dev);
  8373. return rc;
  8374. }
  8375. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8376. if (release_exclusive(cd, cd->dev) < 0)
  8377. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8378. __func__);
  8379. return rc;
  8380. }
  8381. /*******************************************************************************
  8382. * FUNCTION: _fast_startup
  8383. *
  8384. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8385. *
  8386. * RETURN:
  8387. * 0 = success
  8388. * !0 = failure
  8389. *
  8390. * PARAMETERS:
  8391. * *cd - pointer the core data structure
  8392. ******************************************************************************/
  8393. static int _fast_startup(struct pt_core_data *cd)
  8394. {
  8395. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8396. int rc = 0;
  8397. u8 mode = PT_MODE_UNKNOWN;
  8398. struct pt_hid_desc hid_desc;
  8399. int wait_time = 0;
  8400. memset(&hid_desc, 0, sizeof(hid_desc));
  8401. reset:
  8402. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8403. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8404. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8405. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8406. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8407. __func__);
  8408. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8409. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8410. if (rc < 0) {
  8411. pt_debug(cd->dev, DL_ERROR,
  8412. "%s: Error on getting HID descriptor r=%d\n",
  8413. __func__, rc);
  8414. if (retry--)
  8415. goto reset;
  8416. goto exit;
  8417. }
  8418. cd->mode = pt_get_mode(cd, &hid_desc);
  8419. if (cd->mode == PT_MODE_BOOTLOADER) {
  8420. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8421. __func__);
  8422. rc = pt_hid_output_bl_launch_app_(cd);
  8423. if (rc < 0) {
  8424. pt_debug(cd->dev, DL_ERROR,
  8425. "%s: Error on launch app r=%d\n",
  8426. __func__, rc);
  8427. if (retry--)
  8428. goto reset;
  8429. goto exit;
  8430. }
  8431. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8432. if (rc < 0) {
  8433. pt_debug(cd->dev, DL_ERROR,
  8434. "%s: Error on getting HID descriptor r=%d\n",
  8435. __func__, rc);
  8436. if (retry--)
  8437. goto reset;
  8438. goto exit;
  8439. }
  8440. cd->mode = pt_get_mode(cd, &hid_desc);
  8441. if (cd->mode == PT_MODE_BOOTLOADER) {
  8442. if (retry--)
  8443. goto reset;
  8444. goto exit;
  8445. }
  8446. }
  8447. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8448. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8449. } else {
  8450. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8451. __func__);
  8452. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8453. /* Wait for any sentinel before first try */
  8454. rc = _pt_request_wait_for_enum_state(
  8455. cd->dev, 150,
  8456. STARTUP_STATUS_BL_RESET_SENTINEL |
  8457. STARTUP_STATUS_FW_RESET_SENTINEL);
  8458. if (rc)
  8459. pt_debug(cd->dev, DL_ERROR,
  8460. "%s: No Sentinel detected rc = %d\n",
  8461. __func__, rc);
  8462. } else
  8463. pt_flush_bus_if_irq_asserted(cd,
  8464. PT_FLUSH_BUS_BASED_ON_LEN);
  8465. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8466. if (rc) {
  8467. pt_debug(cd->dev, DL_ERROR,
  8468. "%s: Get mode failed, mode unknown\n",
  8469. __func__);
  8470. mode = PT_MODE_UNKNOWN;
  8471. }
  8472. cd->mode = mode;
  8473. if (cd->mode == PT_MODE_BOOTLOADER) {
  8474. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8475. __func__);
  8476. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8477. if (rc) {
  8478. pt_debug(cd->dev, DL_ERROR,
  8479. "%s Failed to exit bootloader\n",
  8480. __func__);
  8481. msleep(50);
  8482. rc = -ENODEV;
  8483. if (retry--)
  8484. goto reset;
  8485. goto exit;
  8486. } else {
  8487. pt_debug(cd->dev, DL_INFO,
  8488. "%s: Exit bootloader successfully\n",
  8489. __func__);
  8490. }
  8491. if (cd->mode != PT_MODE_OPERATIONAL) {
  8492. pt_debug(cd->dev, DL_WARN,
  8493. "%s: restore mode failure mode = %d\n",
  8494. __func__, cd->mode);
  8495. if (retry--)
  8496. goto reset;
  8497. goto exit;
  8498. }
  8499. }
  8500. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8501. }
  8502. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8503. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8504. if (!rc) {
  8505. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8506. pt_debug(cd->dev, DL_WARN,
  8507. "%s: Exit FW BOOT Mode after %dms\n",
  8508. __func__, wait_time);
  8509. } else {
  8510. pt_debug(cd->dev, DL_WARN,
  8511. "%s: FW stuck in BOOT Mode after %dms\n",
  8512. __func__, wait_time);
  8513. goto exit;
  8514. }
  8515. if (!cd->sysinfo.ready) {
  8516. rc = pt_hid_output_get_sysinfo_(cd);
  8517. if (rc) {
  8518. pt_debug(cd->dev, DL_ERROR,
  8519. "%s: Error on getting sysinfo r=%d\n",
  8520. __func__, rc);
  8521. if (retry--)
  8522. goto reset;
  8523. goto exit;
  8524. }
  8525. }
  8526. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8527. rc = pt_restore_parameters_(cd);
  8528. if (rc)
  8529. pt_debug(cd->dev, DL_ERROR,
  8530. "%s: failed to restore parameters rc=%d\n",
  8531. __func__, rc);
  8532. else
  8533. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8534. exit:
  8535. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8536. return rc;
  8537. }
  8538. /*******************************************************************************
  8539. * FUNCTION: pt_core_poweron_device_
  8540. *
  8541. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8542. *
  8543. * RETURN:
  8544. * 0 = success
  8545. * !0 = failure
  8546. *
  8547. * PARAMETERS:
  8548. * *cd - pointer to core data
  8549. ******************************************************************************/
  8550. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8551. {
  8552. struct device *dev = cd->dev;
  8553. int rc = 0;
  8554. /*
  8555. * After power on action, the chip can general FW sentinel. It can
  8556. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8557. * _fast_startup() can perform a quick enumeration too, here doesn't
  8558. * need another enumeration.
  8559. */
  8560. mutex_lock(&cd->system_lock);
  8561. cd->startup_status = STARTUP_STATUS_START;
  8562. cd->hid_reset_cmd_state = 1;
  8563. mutex_unlock(&cd->system_lock);
  8564. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8565. if (rc < 0) {
  8566. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8567. __func__, rc);
  8568. goto exit;
  8569. }
  8570. if (!cd->irq_enabled) {
  8571. cd->irq_enabled = true;
  8572. enable_irq(cd->irq);
  8573. }
  8574. /* TBD: following function doesn't update startup_status */
  8575. rc = _fast_startup(cd);
  8576. exit:
  8577. return rc;
  8578. }
  8579. /*******************************************************************************
  8580. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8581. *
  8582. * SUMMARY: Reset device, and then trigger a full enumeration.
  8583. *
  8584. * RETURN:
  8585. * 0 = success
  8586. * !0 = failure
  8587. *
  8588. * PARAMETERS:
  8589. * *cd - pointer to core data
  8590. ******************************************************************************/
  8591. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8592. {
  8593. int rc;
  8594. rc = pt_dut_reset_and_wait(cd);
  8595. if (rc < 0) {
  8596. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8597. __func__, rc);
  8598. goto exit;
  8599. }
  8600. rc = _fast_startup(cd);
  8601. exit:
  8602. return rc;
  8603. }
  8604. /*******************************************************************************
  8605. * FUNCTION: pt_core_easywake_off_
  8606. *
  8607. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8608. * the configuration in the core platform data structure.
  8609. *
  8610. * RETURN:
  8611. * 0 = success
  8612. * !0 = failure
  8613. *
  8614. * PARAMETERS:
  8615. * *cd - pointer to core data
  8616. ******************************************************************************/
  8617. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8618. {
  8619. int rc = 0;
  8620. mutex_lock(&cd->system_lock);
  8621. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8622. mutex_unlock(&cd->system_lock);
  8623. pt_debug(cd->dev, DL_INFO,
  8624. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8625. return 0;
  8626. }
  8627. mutex_unlock(&cd->system_lock);
  8628. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8629. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8630. rc = pt_core_wake_device_from_easy_wake_(cd);
  8631. if (rc < 0)
  8632. pt_debug(cd->dev, DL_ERROR,
  8633. "%s - %d failed %d\n", __func__, rc);
  8634. }
  8635. mutex_lock(&cd->system_lock);
  8636. cd->sleep_state = SS_EASY_WAKING_OFF;
  8637. mutex_unlock(&cd->system_lock);
  8638. pt_start_wd_timer(cd);
  8639. return rc;
  8640. }
  8641. /*******************************************************************************
  8642. * FUNCTION: pt_core_easywake_off
  8643. *
  8644. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8645. *
  8646. * RETURN:
  8647. * 0 = success
  8648. * !0 = failure
  8649. *
  8650. * PARAMETERS:
  8651. * *cd - pointer to core data
  8652. ******************************************************************************/
  8653. static int pt_core_easywake_off(struct pt_core_data *cd)
  8654. {
  8655. int rc;
  8656. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8657. if (rc < 0) {
  8658. pt_debug(cd->dev, DL_ERROR,
  8659. "%s: fail get exclusive ex=%p own=%p\n",
  8660. __func__, cd->exclusive_dev, cd->dev);
  8661. return rc;
  8662. }
  8663. rc = pt_core_easywake_off_(cd);
  8664. if (release_exclusive(cd, cd->dev) < 0)
  8665. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8666. __func__);
  8667. else
  8668. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8669. __func__);
  8670. return rc;
  8671. }
  8672. /*******************************************************************************
  8673. * FUNCTION: pt_core_wake_
  8674. *
  8675. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8676. * the configuration in the core platform data structure.
  8677. *
  8678. * RETURN:
  8679. * 0 = success
  8680. * !0 = failure
  8681. *
  8682. * PARAMETERS:
  8683. * *cd - pointer to core data
  8684. ******************************************************************************/
  8685. static int pt_core_wake_(struct pt_core_data *cd)
  8686. {
  8687. int rc = 0;
  8688. mutex_lock(&cd->system_lock);
  8689. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8690. mutex_unlock(&cd->system_lock);
  8691. pt_debug(cd->dev, DL_INFO,
  8692. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8693. return 0;
  8694. } else {
  8695. cd->sleep_state = SS_WAKING;
  8696. }
  8697. mutex_unlock(&cd->system_lock);
  8698. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8699. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8700. pt_debug(cd->dev, DL_INFO,
  8701. "%s: Entering into poweron mode:\n", __func__);
  8702. rc = pt_core_poweron_device_(cd);
  8703. if (rc < 0)
  8704. pr_err("%s: Poweron error detected: rc=%d\n",
  8705. __func__, rc);
  8706. }
  8707. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8708. rc = pt_core_wake_device_from_deep_standby_(cd);
  8709. else /* Default action to exit DeepSleep */
  8710. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8711. }
  8712. mutex_lock(&cd->system_lock);
  8713. cd->sleep_state = SS_SLEEP_OFF;
  8714. mutex_unlock(&cd->system_lock);
  8715. pt_start_wd_timer(cd);
  8716. return rc;
  8717. }
  8718. /*******************************************************************************
  8719. * FUNCTION: pt_core_wake_
  8720. *
  8721. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8722. *
  8723. * RETURN:
  8724. * 0 = success
  8725. * !0 = failure
  8726. *
  8727. * PARAMETERS:
  8728. * *cd - pointer to core data
  8729. ******************************************************************************/
  8730. static int pt_core_wake(struct pt_core_data *cd)
  8731. {
  8732. int rc = 0;
  8733. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8734. if (rc < 0) {
  8735. pt_debug(cd->dev, DL_ERROR,
  8736. "%s: fail get exclusive ex=%p own=%p\n",
  8737. __func__, cd->exclusive_dev, cd->dev);
  8738. return rc;
  8739. }
  8740. rc = pt_core_wake_(cd);
  8741. if (release_exclusive(cd, cd->dev) < 0)
  8742. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8743. __func__);
  8744. else
  8745. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8746. __func__);
  8747. return rc;
  8748. }
  8749. /*******************************************************************************
  8750. * FUNCTION: pt_get_ic_crc_
  8751. *
  8752. * SUMMARY: This function retrieves the config block CRC
  8753. *
  8754. * NOTE: The post condition of calling this function will be that the DUT will
  8755. * be in SCANNINING mode if no failures occur
  8756. *
  8757. * RETURN:
  8758. * 0 = success
  8759. * !0 = failure
  8760. *
  8761. * PARAMETERS:
  8762. * *cd - pointer the core data structure
  8763. * ebid - enumerated block ID
  8764. ******************************************************************************/
  8765. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8766. {
  8767. struct pt_sysinfo *si = &cd->sysinfo;
  8768. int rc = 0;
  8769. u8 status;
  8770. u16 calculated_crc = 0;
  8771. u16 stored_crc = 0;
  8772. rc = pt_pip_suspend_scanning_(cd);
  8773. if (rc)
  8774. goto error;
  8775. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8776. &calculated_crc, &stored_crc);
  8777. if (rc)
  8778. goto exit;
  8779. if (status) {
  8780. rc = -EINVAL;
  8781. goto exit;
  8782. }
  8783. si->ttconfig.crc = stored_crc;
  8784. exit:
  8785. pt_pip_resume_scanning_(cd);
  8786. error:
  8787. pt_debug(cd->dev, DL_INFO,
  8788. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8789. __func__, ebid, calculated_crc, stored_crc, rc);
  8790. return rc;
  8791. }
  8792. /*******************************************************************************
  8793. * FUNCTION: pt_pip2_read_gpio
  8794. *
  8795. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8796. * bits into the passed in variable
  8797. *
  8798. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8799. *
  8800. * RETURN:
  8801. * 0 = success
  8802. * !0 = failure
  8803. *
  8804. * PARAMETERS:
  8805. * *dev - pointer to device structure
  8806. * *status - pointer to where the command response status is stored
  8807. * *gpio - pointer to device attributes structure
  8808. ******************************************************************************/
  8809. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8810. {
  8811. int rc = 0;
  8812. u16 actual_read_len;
  8813. u8 read_buf[12];
  8814. u8 tmp_status = 0;
  8815. u8 index = PIP2_RESP_STATUS_OFFSET;
  8816. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8817. rc = _pt_request_pip2_send_cmd(dev,
  8818. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8819. NULL, 0, read_buf, &actual_read_len);
  8820. if (rc) {
  8821. pt_debug(dev, DL_ERROR,
  8822. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8823. rc = -ECOMM;
  8824. } else {
  8825. tmp_status = read_buf[index];
  8826. }
  8827. if (status)
  8828. *status = tmp_status;
  8829. if (!rc && gpio && (tmp_status == 0)) {
  8830. *gpio = ((read_buf[index + 4] << 24) |
  8831. (read_buf[index + 3] << 16) |
  8832. (read_buf[index + 2] << 8) |
  8833. (read_buf[index + 1]));
  8834. }
  8835. return rc;
  8836. }
  8837. /*******************************************************************************
  8838. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8839. *
  8840. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8841. *
  8842. * RETURN:
  8843. * 0 = success
  8844. * !0 = failure
  8845. *
  8846. * PARAMETERS:
  8847. * *cd - pointer the core data structure
  8848. * *pid - pointer to store panel ID
  8849. ******************************************************************************/
  8850. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8851. {
  8852. u32 gpio_value = 0;
  8853. u8 status = 0;
  8854. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8855. int rc = 0;
  8856. if (!pid)
  8857. return -ENOMEM;
  8858. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8859. if (!rc) {
  8860. if (status == 0) {
  8861. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8862. PT_PANEL_ID_SHIFT;
  8863. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8864. __func__,
  8865. "BL mode PID", panel_id, "gpio", gpio_value);
  8866. *pid = panel_id;
  8867. } else {
  8868. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8869. __func__,
  8870. "BL read gpio failed status", status);
  8871. }
  8872. } else {
  8873. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8874. __func__,
  8875. "BL read gpio failed status", status);
  8876. }
  8877. return rc;
  8878. }
  8879. /*******************************************************************************
  8880. * FUNCTION: pt_enum_with_dut_
  8881. *
  8882. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8883. * The core data (cd) startup_status will store, as a bitmask, each
  8884. * state of the enumeration process. The startup will be attempted
  8885. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8886. *
  8887. * RETURN:
  8888. * 0 = success
  8889. * !0 = failure
  8890. *
  8891. * PARAMETERS:
  8892. * *cd - pointer the core data structure
  8893. * reset - Flag to reset the DUT before attempting to enumerate
  8894. * *status - poionter to store the enum status bitmask flags
  8895. ******************************************************************************/
  8896. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8897. u32 *enum_status)
  8898. {
  8899. int try = 1;
  8900. int rc = 0;
  8901. int wait_time = 0;
  8902. bool detected = false;
  8903. u8 return_data[8];
  8904. u8 mode = PT_MODE_UNKNOWN;
  8905. u8 pid = PANEL_ID_NOT_ENABLED;
  8906. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8907. struct pt_hid_desc hid_desc;
  8908. memset(&hid_desc, 0, sizeof(hid_desc));
  8909. #ifdef TTHE_TUNER_SUPPORT
  8910. tthe_print(cd, NULL, 0, "enter startup");
  8911. #endif
  8912. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8913. __func__, cd->startup_status, reset);
  8914. pt_stop_wd_timer(cd);
  8915. reset:
  8916. if (try > 1)
  8917. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8918. __func__, try);
  8919. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8920. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8921. pt_debug(cd->dev, DL_INFO,
  8922. "%s: PIP1 Enumeration start\n", __func__);
  8923. /* Only reset the DUT after the first try */
  8924. if (reset || (try > 1)) {
  8925. /*
  8926. * Reset hardware only for Legacy parts. Skip for TT/TC
  8927. * parts because if the FW image was loaded directly
  8928. * to SRAM issueing a reset ill wipe out what was just
  8929. * loaded.
  8930. */
  8931. rc = pt_dut_reset_and_wait(cd);
  8932. if (rc < 0) {
  8933. pt_debug(cd->dev, DL_ERROR,
  8934. "%s: Error on h/w reset r=%d\n",
  8935. __func__, rc);
  8936. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8937. goto reset;
  8938. goto exit;
  8939. }
  8940. /* sleep to allow FW to be launched if available */
  8941. msleep(120);
  8942. }
  8943. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8944. if (rc < 0) {
  8945. pt_debug(cd->dev, DL_ERROR,
  8946. "%s: Error getting HID Descriptor r=%d\n",
  8947. __func__, rc);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. rc = -EIO;
  8951. goto exit;
  8952. }
  8953. detected = true;
  8954. cd->mode = pt_get_mode(cd, &hid_desc);
  8955. /*
  8956. * Most systems do not use an XY pin as the panel_id and so
  8957. * the BL is used to retrieve the panel_id, however for
  8958. * systems that do use an XY pin, the panel_id MUST be
  8959. * retrieved from the system information when running FW
  8960. * (done below) and so this section of code is skipped.
  8961. * Entering the BL here is only needed on XY_PIN systems.
  8962. */
  8963. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8964. if (cd->mode == PT_MODE_OPERATIONAL) {
  8965. rc = pt_pip_start_bootloader_(cd);
  8966. if (rc < 0) {
  8967. pt_debug(cd->dev, DL_ERROR,
  8968. "%s: Error on start bootloader r=%d\n",
  8969. __func__, rc);
  8970. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8971. goto reset;
  8972. goto exit;
  8973. }
  8974. cd->mode = PT_MODE_BOOTLOADER;
  8975. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8976. __func__);
  8977. }
  8978. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8979. if (!rc) {
  8980. cd->bl_info.ready = true;
  8981. cd->bl_info.chip_id =
  8982. get_unaligned_le16(&return_data[2]);
  8983. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8984. __func__, cd->bl_info.chip_id);
  8985. } else {
  8986. pt_debug(cd->dev, DL_ERROR,
  8987. "%s: failed to get chip ID, r=%d\n",
  8988. __func__, rc);
  8989. }
  8990. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8991. mutex_lock(&cd->system_lock);
  8992. if (!rc) {
  8993. cd->pid_for_loader = pid;
  8994. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8995. __func__, cd->pid_for_loader);
  8996. } else {
  8997. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8998. pt_debug(cd->dev, DL_WARN,
  8999. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9000. __func__, cd->pid_for_loader);
  9001. }
  9002. mutex_unlock(&cd->system_lock);
  9003. }
  9004. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9005. if (cd->mode == PT_MODE_BOOTLOADER) {
  9006. rc = pt_hid_output_bl_launch_app_(cd);
  9007. if (rc < 0) {
  9008. pt_debug(cd->dev, DL_ERROR,
  9009. "%s: Error on launch app r=%d\n",
  9010. __func__, rc);
  9011. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9012. goto reset;
  9013. rc = -ENODEV;
  9014. goto exit;
  9015. }
  9016. /* sleep to allow FW to be launched if available */
  9017. msleep(120);
  9018. /* Ensure the DUT is now in Application mode */
  9019. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9020. if (rc < 0) {
  9021. pt_debug(cd->dev, DL_ERROR,
  9022. "%s: Error getting HID Desc r=%d\n",
  9023. __func__, rc);
  9024. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9025. goto reset;
  9026. rc = -EIO;
  9027. goto exit;
  9028. }
  9029. cd->mode = pt_get_mode(cd, &hid_desc);
  9030. if (cd->mode == PT_MODE_BOOTLOADER) {
  9031. pt_debug(cd->dev, DL_WARN,
  9032. "%s: Error confiming exit BL\n",
  9033. __func__);
  9034. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9035. goto reset;
  9036. rc = -ENODEV;
  9037. goto exit;
  9038. }
  9039. }
  9040. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9041. cd->mode = PT_MODE_OPERATIONAL;
  9042. *enum_status |= STARTUP_STATUS_GET_DESC;
  9043. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9044. } else {
  9045. /* Generation is PIP2 Capable */
  9046. pt_debug(cd->dev, DL_INFO,
  9047. "%s: PIP2 Enumeration start\n", __func__);
  9048. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9049. if (rc) {
  9050. pt_debug(cd->dev, DL_ERROR,
  9051. "%s: Get mode failed, mode unknown\n",
  9052. __func__);
  9053. mode = PT_MODE_UNKNOWN;
  9054. } else
  9055. detected = true;
  9056. cd->mode = mode;
  9057. switch (cd->mode) {
  9058. case PT_MODE_OPERATIONAL:
  9059. pt_debug(cd->dev, DL_INFO,
  9060. "%s: Operational mode\n", __func__);
  9061. if (cd->app_pip_ver_ready == false) {
  9062. rc = pt_pip2_get_version_(cd);
  9063. if (!rc)
  9064. cd->app_pip_ver_ready = true;
  9065. else {
  9066. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9067. goto reset;
  9068. goto exit;
  9069. }
  9070. }
  9071. break;
  9072. case PT_MODE_BOOTLOADER:
  9073. pt_debug(cd->dev, DL_INFO,
  9074. "%s: Bootloader mode\n", __func__);
  9075. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9076. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9077. mutex_lock(&cd->system_lock);
  9078. if (!rc) {
  9079. cd->pid_for_loader = pid;
  9080. pt_debug(cd->dev, DL_INFO,
  9081. "%s: Panel ID: 0x%02X\n",
  9082. __func__, cd->pid_for_loader);
  9083. } else {
  9084. cd->pid_for_loader =
  9085. PANEL_ID_NOT_ENABLED;
  9086. pt_debug(cd->dev, DL_WARN,
  9087. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9088. __func__, cd->pid_for_loader);
  9089. }
  9090. mutex_unlock(&cd->system_lock);
  9091. }
  9092. if (cd->bl_pip_ver_ready == false) {
  9093. rc = pt_pip2_get_version_(cd);
  9094. if (!rc)
  9095. cd->bl_pip_ver_ready = true;
  9096. else {
  9097. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9098. goto reset;
  9099. goto exit;
  9100. }
  9101. }
  9102. /*
  9103. * Launch app command will fail in flashless mode.
  9104. * Skip launch app command here to save time for
  9105. * enumeration flow.
  9106. */
  9107. if (cd->flashless_dut)
  9108. goto exit;
  9109. /*
  9110. * pt_pip2_launch_app() is needed here instead of
  9111. * pt_pip2_exit_bl() because exit_bl will cause several
  9112. * hw_resets to occur and the auto BL on a flashless
  9113. * DUT will fail.
  9114. */
  9115. rc = pt_pip2_launch_app(cd->dev,
  9116. PT_CORE_CMD_UNPROTECTED);
  9117. if (rc) {
  9118. pt_debug(cd->dev, DL_ERROR,
  9119. "%s: Error on launch app r=%d\n",
  9120. __func__, rc);
  9121. msleep(50);
  9122. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9123. goto reset;
  9124. goto exit;
  9125. }
  9126. /*
  9127. * IRQ thread can be delayed if the serial log print is
  9128. * enabled. It causes next command to get wrong response
  9129. * Here the delay is to ensure pt_parse_input() to be
  9130. * finished.
  9131. */
  9132. msleep(60);
  9133. /* Check and update the mode */
  9134. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9135. if (rc) {
  9136. pt_debug(cd->dev, DL_ERROR,
  9137. "%s: Get mode failed, mode unknown\n",
  9138. __func__);
  9139. mode = PT_MODE_UNKNOWN;
  9140. }
  9141. cd->mode = mode;
  9142. if (cd->mode == PT_MODE_OPERATIONAL) {
  9143. pt_debug(cd->dev, DL_INFO,
  9144. "%s: Launched to Operational mode\n",
  9145. __func__);
  9146. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9147. pt_debug(cd->dev, DL_ERROR,
  9148. "%s: Launch failed, Bootloader mode\n",
  9149. __func__);
  9150. goto exit;
  9151. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9152. pt_debug(cd->dev, DL_ERROR,
  9153. "%s: Launch failed, Unknown mode\n",
  9154. __func__);
  9155. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9156. goto reset;
  9157. goto exit;
  9158. }
  9159. if (cd->app_pip_ver_ready == false) {
  9160. rc = pt_pip2_get_version_(cd);
  9161. if (!rc)
  9162. cd->app_pip_ver_ready = true;
  9163. else {
  9164. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9165. goto reset;
  9166. goto exit;
  9167. }
  9168. }
  9169. break;
  9170. default:
  9171. pt_debug(cd->dev, DL_ERROR,
  9172. "%s: Unknown mode\n", __func__);
  9173. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9174. goto reset;
  9175. goto exit;
  9176. }
  9177. *enum_status |= STARTUP_STATUS_GET_DESC;
  9178. }
  9179. pt_init_pip_report_fields(cd);
  9180. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9181. if (!cd->features.easywake)
  9182. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9183. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9184. rc = pt_hid_output_get_sysinfo_(cd);
  9185. if (rc) {
  9186. pt_debug(cd->dev, DL_ERROR,
  9187. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9188. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9189. goto reset;
  9190. goto exit;
  9191. }
  9192. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9193. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9194. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9195. if (!rc) {
  9196. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9197. pt_debug(cd->dev, DL_WARN,
  9198. "%s: Exit FW BOOT Mode after %dms\n",
  9199. __func__, wait_time);
  9200. } else {
  9201. pt_debug(cd->dev, DL_WARN,
  9202. "%s: FW stuck in BOOT Mode after %dms\n",
  9203. __func__, wait_time);
  9204. goto exit;
  9205. }
  9206. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9207. __func__,
  9208. cd->sysinfo.ttdata.pip_ver_major,
  9209. cd->sysinfo.ttdata.pip_ver_minor);
  9210. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9211. if (rc) {
  9212. pt_debug(cd->dev, DL_ERROR,
  9213. "%s: DUT Config block CRC failure rc=%d\n",
  9214. __func__, rc);
  9215. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9216. goto reset;
  9217. } else {
  9218. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9219. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9220. pt_debug(cd->dev, DL_ERROR,
  9221. "%s: scan state: %d, retry: %d\n",
  9222. __func__, sys_mode, try);
  9223. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9224. goto reset;
  9225. } else
  9226. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9227. }
  9228. rc = pt_restore_parameters_(cd);
  9229. if (rc) {
  9230. pt_debug(cd->dev, DL_ERROR,
  9231. "%s: Failed to restore parameters rc=%d\n",
  9232. __func__, rc);
  9233. } else
  9234. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9235. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9236. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9237. cd->startup_retry_count = 0;
  9238. exit:
  9239. /* Generate the HW Version of the connected DUT and store in cd */
  9240. pt_generate_hw_version(cd, cd->hw_version);
  9241. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9242. cd->hw_version);
  9243. pt_start_wd_timer(cd);
  9244. if (!detected)
  9245. rc = -ENODEV;
  9246. #ifdef TTHE_TUNER_SUPPORT
  9247. tthe_print(cd, NULL, 0, "exit startup");
  9248. #endif
  9249. pt_debug(cd->dev, DL_WARN,
  9250. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9251. __func__, rc, try);
  9252. return rc;
  9253. }
  9254. /*******************************************************************************
  9255. * FUNCTION: pt_enum_with_dut
  9256. *
  9257. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9258. * requesting exclusive access around the call.
  9259. *
  9260. * RETURN:
  9261. * 0 = success
  9262. * !0 = failure
  9263. *
  9264. * PARAMETERS:
  9265. * *cd - pointer the core data structure
  9266. * reset - Flag to reset the DUT before attempting to enumerate
  9267. * *status - pointer to store the enum status bitmask flags
  9268. ******************************************************************************/
  9269. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9270. {
  9271. int rc = 0;
  9272. mutex_lock(&cd->system_lock);
  9273. cd->startup_state = STARTUP_RUNNING;
  9274. mutex_unlock(&cd->system_lock);
  9275. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9276. if (rc) {
  9277. pt_debug(cd->dev, DL_ERROR,
  9278. "%s: fail get exclusive ex=%p own=%p\n",
  9279. __func__, cd->exclusive_dev, cd->dev);
  9280. goto exit;
  9281. }
  9282. rc = pt_enum_with_dut_(cd, reset, status);
  9283. if (release_exclusive(cd, cd->dev) < 0)
  9284. /* Don't return fail code, mode is already changed. */
  9285. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9286. __func__);
  9287. else
  9288. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9289. __func__);
  9290. exit:
  9291. mutex_lock(&cd->system_lock);
  9292. /* Clear startup state for any tasks waiting for startup completion */
  9293. cd->startup_state = STARTUP_NONE;
  9294. mutex_unlock(&cd->system_lock);
  9295. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9296. *status |= STARTUP_STATUS_COMPLETE;
  9297. /* Wake the waiters for end of startup */
  9298. wake_up(&cd->wait_q);
  9299. return rc;
  9300. }
  9301. static int add_sysfs_interfaces(struct device *dev);
  9302. static void remove_sysfs_interfaces(struct device *dev);
  9303. static void remove_sysfs_and_modules(struct device *dev);
  9304. static void pt_release_modules(struct pt_core_data *cd);
  9305. static void pt_probe_modules(struct pt_core_data *cd);
  9306. /*******************************************************************************
  9307. * FUNCTION: _pt_ttdl_restart
  9308. *
  9309. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9310. *
  9311. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9312. * existing sysfs nodes will produce an error.
  9313. *
  9314. * RETURN:
  9315. * 0 = success
  9316. * !0 = failure
  9317. *
  9318. * PARAMETERS:
  9319. * *dev - pointer to core device
  9320. ******************************************************************************/
  9321. static int _pt_ttdl_restart(struct device *dev)
  9322. {
  9323. int rc = 0;
  9324. struct pt_core_data *cd = dev_get_drvdata(dev);
  9325. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9326. struct i2c_client *client =
  9327. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9328. #endif
  9329. /*
  9330. * Make sure the device is awake, pt_mt_release function will
  9331. * cause pm sleep function and lead to deadlock.
  9332. */
  9333. pm_runtime_get_sync(dev);
  9334. /* Use ttdl_restart_lock to avoid reentry */
  9335. mutex_lock(&cd->ttdl_restart_lock);
  9336. remove_sysfs_and_modules(cd->dev);
  9337. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9338. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9339. pt_debug(dev, DL_ERROR,
  9340. "%s I2C functionality not Supported\n", __func__);
  9341. rc = -EIO;
  9342. goto ttdl_no_error;
  9343. }
  9344. #endif
  9345. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9346. rc = _pt_detect_dut_generation(cd->dev,
  9347. &cd->startup_status, &cd->active_dut_generation,
  9348. &cd->mode);
  9349. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9350. pt_debug(dev, DL_ERROR,
  9351. "%s: Error, Unknown DUT Generation rc=%d\n",
  9352. __func__, rc);
  9353. }
  9354. }
  9355. rc = add_sysfs_interfaces(cd->dev);
  9356. if (rc < 0)
  9357. pt_debug(cd->dev, DL_ERROR,
  9358. "%s: Error, failed sysfs nodes rc=%d\n",
  9359. __func__, rc);
  9360. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9361. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9362. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9363. if (rc < 0)
  9364. pt_debug(dev, DL_ERROR,
  9365. "%s: Error, Failed to Enumerate\n", __func__);
  9366. }
  9367. rc = pt_mt_probe(dev);
  9368. if (rc < 0) {
  9369. pt_debug(dev, DL_ERROR,
  9370. "%s: Error, fail mt probe\n", __func__);
  9371. }
  9372. rc = pt_btn_probe(dev);
  9373. if (rc < 0) {
  9374. pt_debug(dev, DL_ERROR,
  9375. "%s: Error, fail btn probe\n", __func__);
  9376. }
  9377. pt_probe_modules(cd);
  9378. pt_debug(cd->dev, DL_WARN,
  9379. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9380. rc = 0;
  9381. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9382. ttdl_no_error:
  9383. #endif
  9384. mutex_unlock(&cd->ttdl_restart_lock);
  9385. mutex_lock(&cd->system_lock);
  9386. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9387. cd->startup_state = STARTUP_NONE;
  9388. mutex_unlock(&cd->system_lock);
  9389. pm_runtime_put(dev);
  9390. return rc;
  9391. }
  9392. /*******************************************************************************
  9393. * FUNCTION: pt_restart_work_function
  9394. *
  9395. * SUMMARY: This is the wrapper function placed in a work queue to call
  9396. * _pt_ttdl_restart()
  9397. *
  9398. * RETURN: none
  9399. *
  9400. * PARAMETERS:
  9401. * *work - pointer to the work_struct
  9402. ******************************************************************************/
  9403. static void pt_restart_work_function(struct work_struct *work)
  9404. {
  9405. struct pt_core_data *cd = container_of(work,
  9406. struct pt_core_data, ttdl_restart_work);
  9407. int rc = 0;
  9408. rc = _pt_ttdl_restart(cd->dev);
  9409. if (rc < 0)
  9410. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9411. __func__, rc);
  9412. }
  9413. /*******************************************************************************
  9414. * FUNCTION: pt_enum_work_function
  9415. *
  9416. * SUMMARY: This is the wrapper function placed in a work queue to call
  9417. * pt_enum_with_dut()
  9418. *
  9419. * RETURN: none
  9420. *
  9421. * PARAMETERS:
  9422. * *work - pointer to the work_struct
  9423. ******************************************************************************/
  9424. static void pt_enum_work_function(struct work_struct *work)
  9425. {
  9426. struct pt_core_data *cd = container_of(work,
  9427. struct pt_core_data, enum_work);
  9428. int rc;
  9429. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9430. if (rc < 0)
  9431. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9432. __func__, rc);
  9433. }
  9434. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9435. {
  9436. int rc;
  9437. if (!get) {
  9438. rc = 0;
  9439. goto regulator_put;
  9440. }
  9441. cd->vdd = regulator_get(cd->dev, "vdd");
  9442. if (IS_ERR(cd->vdd)) {
  9443. rc = PTR_ERR(cd->vdd);
  9444. dev_err(cd->dev,
  9445. "Regulator get failed vdd rc=%d\n", rc);
  9446. goto regulator_put;
  9447. }
  9448. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9449. if (IS_ERR(cd->vcc_i2c)) {
  9450. rc = PTR_ERR(cd->vcc_i2c);
  9451. dev_err(cd->dev,
  9452. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9453. goto regulator_put;
  9454. }
  9455. return 0;
  9456. regulator_put:
  9457. if (cd->vdd) {
  9458. regulator_put(cd->vdd);
  9459. cd->vdd = NULL;
  9460. }
  9461. if (cd->vcc_i2c) {
  9462. regulator_put(cd->vcc_i2c);
  9463. cd->vcc_i2c = NULL;
  9464. }
  9465. return rc;
  9466. }
  9467. #ifdef ENABLE_I2C_REG_ONLY
  9468. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9469. {
  9470. int rc = 0;
  9471. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9472. if (!en) {
  9473. rc = 0;
  9474. goto disable_vcc_i2c_reg_only;
  9475. }
  9476. if (cd->vcc_i2c) {
  9477. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9478. if (rc < 0)
  9479. pt_debug(cd->dev, DL_INFO,
  9480. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9481. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9482. __func__, rc);
  9483. }
  9484. return 0;
  9485. disable_vcc_i2c_reg_only:
  9486. if (cd->vcc_i2c) {
  9487. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9488. if (rc < 0)
  9489. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9490. __func__, rc);
  9491. }
  9492. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9493. return rc;
  9494. }
  9495. #endif
  9496. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9497. {
  9498. int rc;
  9499. if (!en) {
  9500. rc = 0;
  9501. goto disable_vcc_i2c_reg;
  9502. }
  9503. if (cd->vdd) {
  9504. if (regulator_count_voltages(cd->vdd) > 0) {
  9505. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9506. FT_VTG_MAX_UV);
  9507. if (rc) {
  9508. dev_err(cd->dev,
  9509. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9510. goto exit;
  9511. }
  9512. }
  9513. rc = regulator_enable(cd->vdd);
  9514. if (rc) {
  9515. dev_err(cd->dev,
  9516. "Regulator vdd enable failed rc=%d\n", rc);
  9517. goto exit;
  9518. }
  9519. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9520. }
  9521. if (cd->vcc_i2c) {
  9522. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9523. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9524. FT_I2C_VTG_MAX_UV);
  9525. if (rc) {
  9526. dev_err(cd->dev,
  9527. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9528. goto disable_vdd_reg;
  9529. }
  9530. }
  9531. rc = regulator_enable(cd->vcc_i2c);
  9532. if (rc) {
  9533. dev_err(cd->dev,
  9534. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9535. goto disable_vdd_reg;
  9536. }
  9537. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9538. }
  9539. return 0;
  9540. disable_vcc_i2c_reg:
  9541. if (cd->vcc_i2c) {
  9542. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9543. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9544. FT_I2C_VTG_MAX_UV);
  9545. regulator_disable(cd->vcc_i2c);
  9546. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9547. }
  9548. disable_vdd_reg:
  9549. if (cd->vdd) {
  9550. if (regulator_count_voltages(cd->vdd) > 0)
  9551. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9552. FT_VTG_MAX_UV);
  9553. regulator_disable(cd->vdd);
  9554. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9555. }
  9556. exit:
  9557. return rc;
  9558. }
  9559. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9560. #define KERNEL_VER_GT_3_19
  9561. #endif
  9562. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9563. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9564. #if defined(CONFIG_PM_SLEEP)
  9565. /*******************************************************************************
  9566. * FUNCTION: pt_core_rt_suspend
  9567. *
  9568. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9569. *
  9570. * RETURN:
  9571. * 0 = success
  9572. * !0 = failure
  9573. *
  9574. * PARAMETERS:
  9575. * *dev - pointer to core device
  9576. ******************************************************************************/
  9577. static int pt_core_rt_suspend(struct device *dev)
  9578. {
  9579. struct pt_core_data *cd = dev_get_drvdata(dev);
  9580. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9581. __func__, cd->core_probe_complete);
  9582. return 0;
  9583. }
  9584. /*******************************************************************************
  9585. * FUNCTION: pt_core_rt_resume
  9586. *
  9587. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9588. *
  9589. * RETURN:
  9590. * 0 = success
  9591. * !0 = failure
  9592. *
  9593. * PARAMETERS:
  9594. * *dev - pointer to core device
  9595. ******************************************************************************/
  9596. static int pt_core_rt_resume(struct device *dev)
  9597. {
  9598. struct pt_core_data *cd = dev_get_drvdata(dev);
  9599. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9600. __func__, cd->core_probe_complete);
  9601. return 0;
  9602. }
  9603. #endif /* CONFIG_PM_SLEEP */
  9604. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9605. #if defined(CONFIG_PM_SLEEP)
  9606. /*******************************************************************************
  9607. * FUNCTION: pt_core_suspend_
  9608. *
  9609. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9610. * pt_core_sleep. This function may disable IRQ during sleep state.
  9611. *
  9612. * RETURN:
  9613. * 0 = success
  9614. * !0 = failure
  9615. *
  9616. * PARAMETERS:
  9617. * *dev - pointer to core device
  9618. ******************************************************************************/
  9619. static int pt_core_suspend_(struct device *dev)
  9620. {
  9621. int rc;
  9622. struct pt_core_data *cd = dev_get_drvdata(dev);
  9623. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9624. rc = pt_core_sleep(cd);
  9625. if (rc) {
  9626. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9627. __func__, rc);
  9628. return -EAGAIN;
  9629. }
  9630. rc = pt_enable_regulator(cd, false);
  9631. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9632. return 0;
  9633. /* Required to prevent interrupts before bus awake */
  9634. disable_irq(cd->irq);
  9635. cd->irq_disabled = 1;
  9636. if (device_may_wakeup(dev)) {
  9637. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9638. __func__);
  9639. if (!enable_irq_wake(cd->irq))
  9640. cd->irq_wake = 1;
  9641. } else {
  9642. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9643. __func__);
  9644. }
  9645. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9646. return rc;
  9647. }
  9648. /*******************************************************************************
  9649. * FUNCTION: pt_core_suspend
  9650. *
  9651. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9652. * woke up or put to sleep based on Kernel power state even when the display
  9653. * is off based on the check of TTDL core platform flag.
  9654. *
  9655. * RETURN:
  9656. * 0 = success
  9657. * !0 = failure
  9658. *
  9659. * PARAMETERS:
  9660. * *dev - pointer to core device
  9661. ******************************************************************************/
  9662. static int pt_core_suspend(struct device *dev)
  9663. {
  9664. struct pt_core_data *cd = dev_get_drvdata(dev);
  9665. int rc = 0;
  9666. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9667. return 0;
  9668. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9669. cancel_work_sync(&cd->resume_work);
  9670. cancel_work_sync(&cd->suspend_work);
  9671. mutex_lock(&cd->system_lock);
  9672. cd->wait_until_wake = 0;
  9673. mutex_unlock(&cd->system_lock);
  9674. if (pm_suspend_via_firmware()) {
  9675. rc = pt_core_suspend_(cd->dev);
  9676. cd->quick_boot = true;
  9677. } else {
  9678. rc = pt_enable_i2c_regulator(cd, false);
  9679. if (rc < 0)
  9680. pt_debug(cd->dev, DL_ERROR,
  9681. "%s: Error on disabling i2c regulator\n", __func__);
  9682. }
  9683. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9684. return rc;
  9685. }
  9686. /*******************************************************************************
  9687. * FUNCTION: pt_core_resume_
  9688. *
  9689. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9690. * pt_core_wake. This function may enable IRQ before wake up.
  9691. *
  9692. * RETURN:
  9693. * 0 = success
  9694. * !0 = failure
  9695. *
  9696. * PARAMETERS:
  9697. * *dev - pointer to core device
  9698. ******************************************************************************/
  9699. static int pt_core_resume_(struct device *dev)
  9700. {
  9701. int rc = 0;
  9702. struct pt_core_data *cd = dev_get_drvdata(dev);
  9703. dev_info(dev, "%s: Entering into resume mode:\n",
  9704. __func__);
  9705. rc = pt_enable_regulator(cd, true);
  9706. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9707. __func__, rc);
  9708. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9709. goto exit;
  9710. /*
  9711. * Bus pm does not call suspend if device runtime suspended
  9712. * This flag is covers that case
  9713. */
  9714. if (cd->irq_disabled) {
  9715. enable_irq(cd->irq);
  9716. cd->irq_disabled = 0;
  9717. }
  9718. if (device_may_wakeup(dev)) {
  9719. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9720. __func__);
  9721. if (cd->irq_wake) {
  9722. disable_irq_wake(cd->irq);
  9723. cd->irq_wake = 0;
  9724. }
  9725. } else {
  9726. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9727. __func__);
  9728. }
  9729. exit:
  9730. rc = pt_core_wake(cd);
  9731. if (rc) {
  9732. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9733. __func__, rc);
  9734. pt_enable_regulator(cd, false);
  9735. return -EAGAIN;
  9736. }
  9737. return rc;
  9738. }
  9739. /*******************************************************************************
  9740. * FUNCTION: pt_core_restore
  9741. *
  9742. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9743. * pt_core_wake. This function may enable IRQ before wake up.
  9744. *
  9745. * RETURN:
  9746. * 0 = success
  9747. * !0 = failure
  9748. *
  9749. * PARAMETERS:
  9750. * *dev - pointer to core device
  9751. ******************************************************************************/
  9752. static int pt_core_restore(struct device *dev)
  9753. {
  9754. int rc = 0;
  9755. struct pt_core_data *cd = dev_get_drvdata(dev);
  9756. dev_info(dev, "%s: Entering into resume mode:\n",
  9757. __func__);
  9758. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9759. return rc;
  9760. }
  9761. /*******************************************************************************
  9762. * FUNCTION: suspend_offload_work
  9763. *
  9764. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9765. * along with kernel power state even the display is off based on the check of
  9766. * TTDL core platform flag.
  9767. *
  9768. * RETURN:
  9769. * 0 = success
  9770. * !0 = failure
  9771. *
  9772. * PARAMETERS:
  9773. * *dev - pointer to core device
  9774. ******************************************************************************/
  9775. static void pt_suspend_offload_work(struct work_struct *work)
  9776. {
  9777. int rc = 0;
  9778. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9779. suspend_offload_work);
  9780. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9781. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9782. return;
  9783. rc = pt_core_suspend_(cd->dev);
  9784. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9785. }
  9786. /*******************************************************************************
  9787. * FUNCTION: resume_offload_work
  9788. *
  9789. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9790. * along with kernel power state even the display is off based on the check of
  9791. * TTDL core platform flag.
  9792. *
  9793. * RETURN:
  9794. * 0 = success
  9795. * !0 = failure
  9796. *
  9797. * PARAMETERS:
  9798. * *dev - pointer to core device
  9799. ******************************************************************************/
  9800. static void pt_resume_offload_work(struct work_struct *work)
  9801. {
  9802. int rc = 0;
  9803. int retry_count = 10;
  9804. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9805. resume_offload_work);
  9806. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9807. do {
  9808. retry_count--;
  9809. rc = pt_core_resume_(pt_data->dev);
  9810. if (rc < 0)
  9811. pt_debug(pt_data->dev, DL_ERROR,
  9812. "%s: Error on wake\n", __func__);
  9813. } while (retry_count && rc < 0);
  9814. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9815. rc = pt_core_easywake_on(pt_data);
  9816. if (rc < 0) {
  9817. pt_debug(pt_data->dev, DL_ERROR,
  9818. "%s: Error on enable touch to wake key\n",
  9819. __func__);
  9820. return;
  9821. }
  9822. pt_data->fb_state = FB_OFF;
  9823. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9824. #endif
  9825. pt_data->quick_boot = false;
  9826. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9827. }
  9828. /*******************************************************************************
  9829. * FUNCTION: pt_core_resume
  9830. *
  9831. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9832. * along with kernel power state even the display is off based on the check of
  9833. * TTDL core platform flag.
  9834. *
  9835. * RETURN:
  9836. * 0 = success
  9837. * !0 = failure
  9838. *
  9839. * PARAMETERS:
  9840. * *dev - pointer to core device
  9841. ******************************************************************************/
  9842. static int pt_core_resume(struct device *dev)
  9843. {
  9844. struct pt_core_data *cd = dev_get_drvdata(dev);
  9845. int rc = 0;
  9846. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9847. return 0;
  9848. if (pm_suspend_via_firmware()) {
  9849. rc = pt_core_restore(cd->dev);
  9850. } else {
  9851. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9852. rc = pt_enable_i2c_regulator(cd, true);
  9853. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9854. }
  9855. mutex_lock(&cd->system_lock);
  9856. cd->wait_until_wake = 1;
  9857. mutex_unlock(&cd->system_lock);
  9858. wake_up(&cd->wait_q);
  9859. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9860. return rc;
  9861. }
  9862. #endif
  9863. #ifdef NEED_SUSPEND_NOTIFIER
  9864. /*******************************************************************************
  9865. * FUNCTION: pt_pm_notifier
  9866. *
  9867. * SUMMARY: This function is registered to notifier chain and will perform
  9868. * suspend operation if match event PM_SUSPEND_PREPARE.
  9869. *
  9870. * RETURN:
  9871. * 0 = success
  9872. * !0 = failure
  9873. *
  9874. * PARAMETERS:
  9875. * *nb - pointer to notifier_block structure
  9876. * action - notifier event type
  9877. * *data - void pointer
  9878. ******************************************************************************/
  9879. static int pt_pm_notifier(struct notifier_block *nb,
  9880. unsigned long action, void *data)
  9881. {
  9882. struct pt_core_data *cd = container_of(nb,
  9883. struct pt_core_data, pm_notifier);
  9884. if (action == PM_SUSPEND_PREPARE) {
  9885. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9886. __func__);
  9887. /*
  9888. * If PM runtime is not suspended, either call runtime
  9889. * PM suspend callback or wait until it finishes
  9890. */
  9891. if (!pm_runtime_suspended(cd->dev))
  9892. pm_runtime_suspend(cd->dev);
  9893. (void) pt_core_suspend(cd->dev);
  9894. }
  9895. return NOTIFY_DONE;
  9896. }
  9897. #endif
  9898. const struct dev_pm_ops pt_pm_ops = {
  9899. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9900. .freeze = pt_core_suspend,
  9901. .restore = pt_core_restore,
  9902. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9903. NULL)
  9904. };
  9905. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9906. /*******************************************************************************
  9907. * FUNCTION: _pt_request_pip2_enter_bl
  9908. *
  9909. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9910. * XRES pin or a soft reset.
  9911. *
  9912. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9913. * the WD active could cause this function to fail!
  9914. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9915. * will not try to determine the current mode but will proceed with
  9916. * resetting the DUT and entering the BL.
  9917. *
  9918. * NOTE: The definition of result code:
  9919. * PT_ENTER_BL_PASS (0)
  9920. * PT_ENTER_BL_ERROR (1)
  9921. * PT_ENTER_BL_RESET_FAIL (2)
  9922. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9923. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9924. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9925. *
  9926. * RETURNS:
  9927. * 0 = success
  9928. * !0 = failure
  9929. *
  9930. *
  9931. * PARAMETERS:
  9932. * *dev - pointer to device structure
  9933. * *start_mode - pointer to the mode the DUT was in when this function
  9934. * starts
  9935. * *result - pointer to store the result when to enter BL
  9936. ******************************************************************************/
  9937. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9938. {
  9939. int rc = 0;
  9940. int t;
  9941. int tmp_result = PT_ENTER_BL_ERROR;
  9942. int flash_info_retry = 2;
  9943. u8 mode = PT_MODE_UNKNOWN;
  9944. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9945. u8 read_buf[32];
  9946. u16 actual_read_len;
  9947. struct pt_core_data *cd = dev_get_drvdata(dev);
  9948. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9949. u8 time = 0;
  9950. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9951. if (cd->watchdog_enabled) {
  9952. pt_debug(dev, DL_WARN,
  9953. "%s: Watchdog must be stopped before entering BL\n",
  9954. __func__);
  9955. goto exit;
  9956. }
  9957. cancel_work_sync(&cd->enum_work);
  9958. cancel_work_sync(&cd->watchdog_work);
  9959. /* if undefined assume operational/test to bypass all checks */
  9960. if (*start_mode == PT_MODE_IGNORE) {
  9961. mode = PT_MODE_OPERATIONAL;
  9962. sys_mode = FW_SYS_MODE_TEST;
  9963. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9964. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9965. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9966. if (rc) {
  9967. pt_debug(dev, DL_ERROR,
  9968. "%s: Get mode failed, mode unknown\n",
  9969. __func__);
  9970. }
  9971. *start_mode = mode;
  9972. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9973. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9974. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9975. sys_mode = FW_SYS_MODE_SCANNING;
  9976. }
  9977. _retry:
  9978. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9979. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9980. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9981. switch (mode) {
  9982. case PT_MODE_UNKNOWN:
  9983. /*
  9984. * When the mode could not be determined the DUT could be
  9985. * in App mode running corrupted FW or FW that is not
  9986. * responding to the mode request, assume no communication
  9987. * and do a hard reset
  9988. */
  9989. mutex_lock(&cd->system_lock);
  9990. cd->startup_status = STARTUP_STATUS_START;
  9991. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9992. mutex_unlock(&cd->system_lock);
  9993. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9994. if (rc) {
  9995. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9996. goto exit;
  9997. }
  9998. break;
  9999. case PT_MODE_OPERATIONAL:
  10000. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10001. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10002. __func__);
  10003. rc = pt_pip_suspend_scanning_(cd);
  10004. if (rc) {
  10005. /*
  10006. * Print to log but don't exit, the FW could be
  10007. * running but be hung or fail to respond to
  10008. * this request
  10009. */
  10010. pt_debug(dev, DL_ERROR,
  10011. "%s Suspend Scan Failed\n", __func__);
  10012. }
  10013. /* sleep to allow the suspend scan to be processed */
  10014. usleep_range(1000, 2000);
  10015. }
  10016. mutex_lock(&cd->system_lock);
  10017. cd->startup_status = STARTUP_STATUS_START;
  10018. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10019. mutex_unlock(&cd->system_lock);
  10020. /* Reset device to enter the BL */
  10021. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10022. if (rc) {
  10023. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10024. goto exit;
  10025. }
  10026. break;
  10027. case PT_MODE_BOOTLOADER:
  10028. /* Do nothing as we are already in the BL */
  10029. tmp_result = PT_ENTER_BL_PASS;
  10030. goto exit;
  10031. default:
  10032. /* Should NEVER get here */
  10033. tmp_result = PT_ENTER_BL_ERROR;
  10034. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10035. goto exit;
  10036. }
  10037. if (!cd->flashless_dut &&
  10038. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10039. /*
  10040. * Sending the special "Host Mode" command will instruct the
  10041. * BL to not execute the FW it has loaded into RAM.
  10042. * The command must be sent within a 40ms window from releasing
  10043. * the XRES pin. If the messages is sent too early it will NAK,
  10044. * so keep sending it every 2ms until it is accepted by the BL.
  10045. * A no-flash DUT does not require this command as there is no
  10046. * FW for the BL to load and execute.
  10047. */
  10048. usleep_range(4000, 6000);
  10049. pt_debug(cd->dev, DL_INFO,
  10050. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10051. __func__, (int)sizeof(host_mode_cmd));
  10052. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10053. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10054. rc = 1;
  10055. while (rc && time < 34) {
  10056. rc = pt_adap_write_read_specific(cd, 4,
  10057. host_mode_cmd, NULL);
  10058. usleep_range(1800, 2000);
  10059. time += 2;
  10060. }
  10061. /* Sleep to allow the BL to come up */
  10062. usleep_range(1000, 2000);
  10063. }
  10064. /*
  10065. * To avoid the case that next PIP command can be confused by BL/FW
  10066. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10067. * followed by "wakeup event" function can lower the failure rate.
  10068. */
  10069. t = wait_event_timeout(cd->wait_q,
  10070. ((cd->startup_status != STARTUP_STATUS_START)
  10071. && (cd->hid_reset_cmd_state == 0)),
  10072. msecs_to_jiffies(300));
  10073. if (IS_TMO(t)) {
  10074. pt_debug(cd->dev, DL_ERROR,
  10075. "%s: TMO waiting for BL sentinel\n", __func__);
  10076. }
  10077. /* Check if device is now in BL mode */
  10078. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10079. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10080. cd->startup_status);
  10081. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10082. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10083. mutex_lock(&cd->system_lock);
  10084. cd->pip2_prot_active = true;
  10085. cd->mode = PT_MODE_BOOTLOADER;
  10086. mutex_unlock(&cd->system_lock);
  10087. tmp_result = PT_ENTER_BL_PASS;
  10088. } else {
  10089. /*
  10090. * If the device doesn't enter BL mode as expected and rc is
  10091. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10092. * the function should return an error code to indicate this
  10093. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10094. */
  10095. if (!rc)
  10096. rc = -EINVAL;
  10097. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10098. mutex_lock(&cd->system_lock);
  10099. cd->mode = mode;
  10100. mutex_unlock(&cd->system_lock);
  10101. pt_debug(dev, DL_ERROR,
  10102. "%s ERROR: Not in BL as expected", __func__);
  10103. }
  10104. exit:
  10105. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10106. /* Check to get (buffered) flash information */
  10107. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10108. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10109. read_buf, &actual_read_len);
  10110. if (!rc) {
  10111. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10112. pt_debug(
  10113. dev, DL_WARN,
  10114. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10115. __func__,
  10116. read_buf[PIP2_RESP_BODY_OFFSET]);
  10117. /*
  10118. * If the BL was unable to cache the correct
  10119. * values when entering the first time due to
  10120. * the Flash part not having been powered up
  10121. * long enough, re-enter the BL to trigger the
  10122. * BL to re-attempt to cache the values.
  10123. */
  10124. if (flash_info_retry-- > 0) {
  10125. mode = PT_MODE_UNKNOWN;
  10126. pt_debug(dev, DL_WARN,
  10127. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10128. __func__, flash_info_retry);
  10129. goto _retry;
  10130. } else {
  10131. pt_debug(dev, DL_WARN,
  10132. "%s Manufacturer ID Unknown\n",
  10133. __func__);
  10134. tmp_result = PT_ENTER_BL_PASS;
  10135. }
  10136. }
  10137. } else {
  10138. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10139. pt_debug(
  10140. dev, DL_ERROR,
  10141. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10142. __func__);
  10143. }
  10144. }
  10145. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10146. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10147. "SUPPRESS");
  10148. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10149. if (result)
  10150. *result = tmp_result;
  10151. return rc;
  10152. }
  10153. /*******************************************************************************
  10154. * FUNCTION: _pt_pip2_file_open
  10155. *
  10156. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10157. *
  10158. * NOTE: The DUT must be in BL mode for this command to work
  10159. *
  10160. * RETURNS:
  10161. * <0 = Error
  10162. * >0 = file handle opened
  10163. *
  10164. * PARAMETERS:
  10165. * *dev - pointer to device structure
  10166. * file_no - PIP2 file number to open
  10167. ******************************************************************************/
  10168. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10169. {
  10170. int ret = 0;
  10171. u16 status;
  10172. u16 actual_read_len;
  10173. u8 file_handle;
  10174. u8 data[2];
  10175. u8 read_buf[10];
  10176. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10177. data[0] = file_no;
  10178. ret = _pt_request_pip2_send_cmd(dev,
  10179. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10180. data, 1, read_buf, &actual_read_len);
  10181. if (ret) {
  10182. pt_debug(dev, DL_ERROR,
  10183. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10184. __func__, file_no);
  10185. return -PIP2_RSP_ERR_NOT_OPEN;
  10186. }
  10187. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10188. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10189. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10190. (file_handle != file_no)) {
  10191. pt_debug(dev, DL_ERROR,
  10192. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10193. __func__, status, file_handle);
  10194. return -status;
  10195. }
  10196. return file_handle;
  10197. }
  10198. /*******************************************************************************
  10199. * FUNCTION: _pt_pip2_file_close
  10200. *
  10201. * SUMMARY: Using the BL PIP2 commands close a file
  10202. *
  10203. * NOTE: The DUT must be in BL mode for this command to work
  10204. *
  10205. * RETURNS:
  10206. * <0 = Error
  10207. * >0 = file handle closed
  10208. *
  10209. * PARAMETERS:
  10210. * *dev - pointer to device structure
  10211. * file_handle - handle to the file to be closed
  10212. ******************************************************************************/
  10213. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10214. {
  10215. int ret = 0;
  10216. u16 status;
  10217. u16 actual_read_len;
  10218. u8 data[2];
  10219. u8 read_buf[32];
  10220. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10221. data[0] = file_handle;
  10222. ret = _pt_request_pip2_send_cmd(dev,
  10223. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10224. data, 1, read_buf, &actual_read_len);
  10225. if (ret) {
  10226. pt_debug(dev, DL_ERROR,
  10227. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10228. __func__, file_handle);
  10229. return -ETIME;
  10230. }
  10231. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10232. if (status != 0x00) {
  10233. pt_debug(dev, DL_ERROR,
  10234. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10235. __func__, status, file_handle);
  10236. return -status;
  10237. }
  10238. return file_handle;
  10239. }
  10240. /*******************************************************************************
  10241. * FUNCTION: _pt_pip2_file_erase
  10242. *
  10243. * SUMMARY: Using the BL PIP2 commands erase a file
  10244. *
  10245. * NOTE: The DUT must be in BL mode for this command to work
  10246. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10247. * one retry is attempted for each sector in a file.
  10248. *
  10249. * RETURNS:
  10250. * <0 = Error
  10251. * >0 = file handle closed
  10252. *
  10253. * PARAMETERS:
  10254. * *dev - pointer to device structure
  10255. * file_handle - handle to the file to be erased
  10256. * *status - PIP2 erase status code
  10257. ******************************************************************************/
  10258. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10259. {
  10260. int ret = 0;
  10261. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10262. int retry = 1;
  10263. u16 actual_read_len;
  10264. u8 data[2];
  10265. u8 read_buf[10];
  10266. struct pt_core_data *cd = dev_get_drvdata(dev);
  10267. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10268. data[0] = file_handle;
  10269. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10270. *status = PIP2_RSP_ERR_TIMEOUT;
  10271. /* Increase waiting time for large file erase */
  10272. mutex_lock(&cd->system_lock);
  10273. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10274. mutex_unlock(&cd->system_lock);
  10275. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10276. ret = _pt_request_pip2_send_cmd(dev,
  10277. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10278. data, 2, read_buf, &actual_read_len);
  10279. if (ret)
  10280. break;
  10281. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10282. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10283. #ifdef TTDL_DIAGNOSTICS
  10284. cd->file_erase_timeout_count++;
  10285. #endif
  10286. pt_debug(dev, DL_WARN,
  10287. "%s: ERASE timeout %d for file = %d\n",
  10288. __func__, retry, file_handle);
  10289. }
  10290. retry++;
  10291. }
  10292. mutex_lock(&cd->system_lock);
  10293. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10294. mutex_unlock(&cd->system_lock);
  10295. if (ret) {
  10296. pt_debug(dev, DL_ERROR,
  10297. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10298. __func__, ret, file_handle);
  10299. return -EIO;
  10300. }
  10301. if (*status != 0x00) {
  10302. pt_debug(dev, DL_ERROR,
  10303. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10304. __func__, *status, file_handle);
  10305. return -EIO;
  10306. }
  10307. return file_handle;
  10308. }
  10309. /*******************************************************************************
  10310. * FUNCTION: _pt_pip2_file_read
  10311. *
  10312. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10313. *
  10314. * NOTE: The DUT must be in BL mode for this command to work
  10315. *
  10316. * RETURNS:
  10317. * <0 = Error
  10318. * >0 = number of bytes read
  10319. *
  10320. * PARAMETERS:
  10321. * *dev - pointer to device structure
  10322. * file_handle - File handle to read from
  10323. * num_bytes - number of bytes to read
  10324. ******************************************************************************/
  10325. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10326. u8 *read_buf)
  10327. {
  10328. int ret = 0;
  10329. u16 status;
  10330. u16 actual_read_len;
  10331. u8 data[3];
  10332. data[0] = file_handle;
  10333. data[1] = (num_bytes & 0x00FF);
  10334. data[2] = (num_bytes & 0xFF00) >> 8;
  10335. ret = _pt_request_pip2_send_cmd(dev,
  10336. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10337. data, 3, read_buf, &actual_read_len);
  10338. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10339. if (ret || ((status != 0x00) && (status != 0x03))) {
  10340. pt_debug(dev, DL_ERROR,
  10341. "%s File open failure with error code = %d\n",
  10342. __func__, status);
  10343. return -EPERM;
  10344. }
  10345. ret = num_bytes;
  10346. return ret;
  10347. }
  10348. /*******************************************************************************
  10349. * FUNCTION: _pt_read_us_file
  10350. *
  10351. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10352. * then read the entire file.
  10353. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10354. *
  10355. * RETURN:
  10356. * 0 = success
  10357. * !0 = failure
  10358. *
  10359. * PARAMETERS:
  10360. * *file_path - pointer to the file path
  10361. * *buf - pointer to the buffer to store the file contents
  10362. * *size - pointer to the size of the file
  10363. ******************************************************************************/
  10364. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10365. {
  10366. struct file *filp = NULL;
  10367. struct inode *inode = NULL;
  10368. unsigned int file_len = 0;
  10369. unsigned int read_len = 0;
  10370. mm_segment_t oldfs;
  10371. int rc = 0;
  10372. if (file_path == NULL || buf == NULL) {
  10373. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10374. return -EINVAL;
  10375. }
  10376. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10377. oldfs = force_uaccess_begin();
  10378. filp = filp_open_block(file_path, O_RDONLY, 0400);
  10379. if (IS_ERR(filp)) {
  10380. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10381. file_path);
  10382. rc = -ENOENT;
  10383. goto err;
  10384. }
  10385. if (filp->f_op == NULL) {
  10386. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10387. __func__);
  10388. rc = -EINVAL;
  10389. goto exit;
  10390. }
  10391. inode = filp->f_path.dentry->d_inode;
  10392. if (inode == NULL) {
  10393. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10394. __func__);
  10395. rc = -EINVAL;
  10396. goto exit;
  10397. }
  10398. file_len = i_size_read(inode->i_mapping->host);
  10399. if (file_len == 0) {
  10400. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10401. __func__, file_len);
  10402. rc = -EINVAL;
  10403. goto exit;
  10404. }
  10405. if (*size == 0)
  10406. read_len = file_len;
  10407. else
  10408. read_len = *size;
  10409. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10410. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10411. __func__, read_len);
  10412. rc = -EINVAL;
  10413. goto exit;
  10414. }
  10415. filp->private_data = inode->i_private;
  10416. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  10417. if (filp->f_op->read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10418. #else
  10419. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10420. #endif
  10421. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10422. rc = -EINVAL;
  10423. goto exit;
  10424. }
  10425. *size = read_len;
  10426. exit:
  10427. if (filp_close(filp, NULL) != 0)
  10428. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10429. err:
  10430. force_uaccess_end(oldfs);
  10431. return rc;
  10432. }
  10433. /*******************************************************************************
  10434. * FUNCTION: _pt_request_pip2_bin_hdr
  10435. *
  10436. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10437. * in the case of a flashless DUT, and parse the contents
  10438. *
  10439. * RETURNS:
  10440. * 0 = Success
  10441. * !0 = Error condition
  10442. *
  10443. * PARAMETERS:
  10444. * *dev - pointer to device structure
  10445. ******************************************************************************/
  10446. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10447. {
  10448. struct pt_core_data *cd = dev_get_drvdata(dev);
  10449. u8 file_handle;
  10450. u8 read_buf[255];
  10451. u8 hdr_len = 0;
  10452. u8 i;
  10453. int bytes_read;
  10454. int read_size;
  10455. int ret = 0;
  10456. int rc = 0;
  10457. bool load_hdr_struct = false;
  10458. if (cd->flashless_dut) {
  10459. read_size = sizeof(read_buf);
  10460. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10461. read_buf, &read_size);
  10462. if (rc) {
  10463. ret = rc;
  10464. pt_debug(dev, DL_ERROR,
  10465. "%s Failed to read fw image from US, rc=%d\n",
  10466. __func__, rc);
  10467. goto exit;
  10468. }
  10469. load_hdr_struct = true;
  10470. hdr_len = read_buf[0];
  10471. i = 0;
  10472. } else {
  10473. if (cd->mode != PT_MODE_BOOTLOADER) {
  10474. ret = -EPERM;
  10475. goto exit;
  10476. }
  10477. /* Open the bin file in Flash */
  10478. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10479. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10480. if (file_handle != PIP2_FW_FILE) {
  10481. ret = -ENOENT;
  10482. pt_debug(dev, DL_ERROR,
  10483. "%s Failed to open bin file\n", __func__);
  10484. goto exit;
  10485. }
  10486. /* Read the header length from the file */
  10487. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10488. read_size = 1;
  10489. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10490. read_buf);
  10491. if (bytes_read != read_size) {
  10492. ret = -EX_ERR_FREAD;
  10493. pt_debug(dev, DL_ERROR,
  10494. "%s Failed to bin file header len\n", __func__);
  10495. goto exit_close_file;
  10496. }
  10497. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10498. if (hdr_len == 0xFF) {
  10499. ret = -EX_ERR_FLEN;
  10500. pt_debug(dev, DL_ERROR,
  10501. "%s Bin header len is invalid\n", __func__);
  10502. goto exit_close_file;
  10503. }
  10504. /* Read the rest of the header from the bin file */
  10505. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10506. memset(read_buf, 0, sizeof(read_buf));
  10507. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10508. read_buf);
  10509. if (bytes_read != hdr_len) {
  10510. ret = -EX_ERR_FREAD;
  10511. pt_debug(dev, DL_ERROR,
  10512. "%s Failed to read bin file\n", __func__);
  10513. goto exit_close_file;
  10514. }
  10515. load_hdr_struct = true;
  10516. exit_close_file:
  10517. /* Close the file */
  10518. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10519. ret = -EX_ERR_FCLOSE;
  10520. pt_debug(dev, DL_ERROR,
  10521. "%s Failed to close bin file\n", __func__);
  10522. }
  10523. /*
  10524. * The length was already read so subtract 1 to make the rest of
  10525. * the offsets match the spec
  10526. */
  10527. i = PIP2_RESP_BODY_OFFSET - 1;
  10528. }
  10529. if (load_hdr_struct) {
  10530. hdr->length = hdr_len;
  10531. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10532. hdr->fw_major = (read_buf[i+3]);
  10533. hdr->fw_minor = (read_buf[i+4]);
  10534. hdr->fw_crc = (read_buf[i+5] << 24) |
  10535. (read_buf[i+6] << 16) |
  10536. (read_buf[i+7] << 8) |
  10537. (read_buf[i+8]);
  10538. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10539. (read_buf[i+10] << 16) |
  10540. (read_buf[i+11] << 8) |
  10541. (read_buf[i+12]);
  10542. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10543. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10544. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10545. if (hdr_len >= 22) {
  10546. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10547. (read_buf[i+20] << 16) |
  10548. (read_buf[i+21] << 8) |
  10549. (read_buf[i+22]);
  10550. } else {
  10551. hdr->hex_file_size = 0;
  10552. }
  10553. }
  10554. exit:
  10555. return ret;
  10556. }
  10557. /*******************************************************************************
  10558. * FUNCTION: _pt_pip2_file_get_stats
  10559. *
  10560. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10561. * opened file
  10562. *
  10563. * NOTE: The DUT must be in BL mode for this command to work
  10564. *
  10565. * RETURNS:
  10566. * !0 = Error
  10567. * 0 = Success
  10568. *
  10569. * PARAMETERS:
  10570. * *dev - pointer to device structure
  10571. * file_handle - File handle to read from
  10572. * *address - pointer to store address of file
  10573. * *file_size _ pointer to store size of file
  10574. ******************************************************************************/
  10575. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10576. u32 *file_size)
  10577. {
  10578. int ret = 1;
  10579. u16 status;
  10580. u16 actual_read_len;
  10581. u8 data[2];
  10582. u8 read_buf[16];
  10583. data[0] = file_handle;
  10584. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10585. ret = _pt_request_pip2_send_cmd(dev,
  10586. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10587. data, 2, read_buf, &actual_read_len);
  10588. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10589. if (ret || (status != 0x00)) {
  10590. pt_debug(dev, DL_ERROR,
  10591. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10592. __func__, status, file_handle, ret);
  10593. ret = -EIO;
  10594. goto exit;
  10595. }
  10596. pt_debug(dev, DL_DEBUG,
  10597. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10598. if (address) {
  10599. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10600. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10601. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10602. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10603. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10604. __func__, *address);
  10605. }
  10606. if (file_size) {
  10607. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10608. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10609. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10610. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10611. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10612. __func__, *file_size);
  10613. }
  10614. exit:
  10615. return ret;
  10616. }
  10617. /*******************************************************************************
  10618. * FUNCTION: _pt_pip2_file_seek_offset
  10619. *
  10620. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10621. * opened file
  10622. *
  10623. * NOTE: The DUT must be in BL mode for this command to work
  10624. * NOTE: File open/erase command can reset the offset
  10625. *
  10626. * RETURNS:
  10627. * !0 = Error
  10628. * 0 = Success
  10629. *
  10630. * PARAMETERS:
  10631. * *dev - pointer to device structure
  10632. * file_handle - File handle to read from
  10633. * read_offset - read offset of file
  10634. * write_offset - write offset of file
  10635. ******************************************************************************/
  10636. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10637. u32 read_offset, u32 write_offset)
  10638. {
  10639. int ret = 1;
  10640. u16 status;
  10641. u16 actual_read_len;
  10642. u8 data[10];
  10643. u8 read_buf[16];
  10644. data[0] = file_handle;
  10645. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10646. data[2] = 0xff & read_offset;
  10647. data[3] = 0xff & (read_offset >> 8);
  10648. data[4] = 0xff & (read_offset >> 16);
  10649. data[5] = 0xff & (read_offset >> 24);
  10650. data[6] = 0xff & write_offset;
  10651. data[7] = 0xff & (write_offset >> 8);
  10652. data[8] = 0xff & (write_offset >> 16);
  10653. data[9] = 0xff & (write_offset >> 24);
  10654. ret = _pt_request_pip2_send_cmd(dev,
  10655. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10656. data, 10, read_buf, &actual_read_len);
  10657. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10658. if (ret || (status != 0x00)) {
  10659. pt_debug(dev, DL_ERROR,
  10660. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10661. __func__, status, ret, file_handle);
  10662. ret = -EIO;
  10663. }
  10664. return ret;
  10665. }
  10666. /*******************************************************************************
  10667. * FUNCTION: _pt_pip2_file_crc
  10668. *
  10669. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10670. * the file.
  10671. *
  10672. * NOTE: The DUT must be in BL mode for this command to work
  10673. * NOTE: This command only can be used for BL version 1.8 or greater.
  10674. * BL version 1.8 added this change according to PGV-173.
  10675. *
  10676. * RETURNS:
  10677. * !0 = Error
  10678. * 0 = Success
  10679. *
  10680. * PARAMETERS:
  10681. * *dev - pointer to device structure
  10682. * file_handle - File handle to read from
  10683. * offset - start offset for CRC calculation
  10684. * length - number of bytes to calculate CRC over
  10685. * read_buf - pointer to the read buffer
  10686. ******************************************************************************/
  10687. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10688. u32 offset, u32 length, u8 *read_buf)
  10689. {
  10690. int rc = 1;
  10691. u16 actual_read_len;
  10692. u8 data[10];
  10693. data[0] = file_handle;
  10694. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10695. data[2] = 0xff & offset;
  10696. data[3] = 0xff & (offset >> 8);
  10697. data[4] = 0xff & (offset >> 16);
  10698. data[5] = 0xff & (offset >> 24);
  10699. data[6] = 0xff & length;
  10700. data[7] = 0xff & (length >> 8);
  10701. data[8] = 0xff & (length >> 16);
  10702. data[9] = 0xff & (length >> 24);
  10703. rc = _pt_request_pip2_send_cmd(dev,
  10704. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10705. data, 10, read_buf, &actual_read_len);
  10706. if (rc)
  10707. pt_debug(dev, DL_ERROR,
  10708. "%s Return FILE_CRC failure, rc = %d\n",
  10709. __func__, rc);
  10710. return rc;
  10711. }
  10712. /*******************************************************************************
  10713. * FUNCTION: pt_pip2_ping_test
  10714. *
  10715. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10716. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10717. * response payload matches what was sent.
  10718. * The max payload size is 247:
  10719. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10720. *
  10721. * RETURN:
  10722. * 0 = success
  10723. * !0 = failure
  10724. *
  10725. * PARAMETERS:
  10726. * *dev - pointer to device structure
  10727. * *attr - pointer to device attributes
  10728. * *buf - pointer to output buffer
  10729. ******************************************************************************/
  10730. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10731. {
  10732. u16 actual_read_len;
  10733. u8 *read_buf = NULL;
  10734. u8 *data = NULL;
  10735. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10736. int i = 1;
  10737. int j = 0;
  10738. int rc = 0;
  10739. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10740. if (!read_buf)
  10741. goto ping_test_exit;
  10742. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10743. if (!data)
  10744. goto ping_test_exit;
  10745. /* Load data payload with an array of walking 1's */
  10746. for (i = 0; i < 255; i++)
  10747. data[i] = 0x01 << (i % 8);
  10748. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10749. for (i = 0; i <= max_bytes; i++) {
  10750. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10751. PIP2_CMD_ID_PING, data, i, read_buf,
  10752. &actual_read_len);
  10753. if (rc) {
  10754. pt_debug(dev, DL_ERROR,
  10755. "%s: PING failed with %d byte payload\n",
  10756. __func__, i);
  10757. break;
  10758. }
  10759. /* Verify data returned matches data sent */
  10760. for (j = 0; j < i; j++) {
  10761. if (read_buf[data_offset + j] != data[j]) {
  10762. pt_debug(dev, DL_DEBUG,
  10763. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10764. __func__, i, j, data[j], j,
  10765. read_buf[data_offset + j]);
  10766. goto ping_test_exit;
  10767. }
  10768. }
  10769. }
  10770. ping_test_exit:
  10771. *last_packet_size = i - 1;
  10772. kfree(read_buf);
  10773. kfree(data);
  10774. return rc;
  10775. }
  10776. /*******************************************************************************
  10777. * FUNCTION: _pt_ic_parse_input_hex
  10778. *
  10779. * SUMMARY: Parse a char data array as space delimited hex values into
  10780. * an int array.
  10781. *
  10782. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10783. * buffer to store data is "u32". This function is still needed by the
  10784. * "command" sysfs node because the buffer type to store data is "u8".
  10785. *
  10786. * RETURN: Length of parsed data
  10787. *
  10788. * PARAMETERS:
  10789. * *dev - pointer to device structure
  10790. * *buf - pointer to buffer that holds the input array to parse
  10791. * buf_size - size of buf
  10792. * *ic_buf - pointer to array to store parsed data
  10793. * ic_buf_size - max size of ic_buf
  10794. ******************************************************************************/
  10795. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10796. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10797. {
  10798. const char *pbuf = buf;
  10799. unsigned long value;
  10800. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10801. u32 i = 0;
  10802. u32 j;
  10803. int last = 0;
  10804. int ret;
  10805. pt_debug(dev, DL_DEBUG,
  10806. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10807. __func__, pbuf, buf, buf_size, "scan buf size",
  10808. PT_MAX_PIP2_MSG_SIZE, buf);
  10809. while (pbuf <= (buf + buf_size)) {
  10810. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10811. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10812. __func__, "Max cmd size exceeded", i,
  10813. PT_MAX_PIP2_MSG_SIZE);
  10814. return -EINVAL;
  10815. }
  10816. if (i >= ic_buf_size) {
  10817. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10818. __func__, "Buffer size exceeded", i,
  10819. ic_buf_size);
  10820. return -EINVAL;
  10821. }
  10822. while (((*pbuf == ' ') || (*pbuf == ','))
  10823. && (pbuf < (buf + buf_size))) {
  10824. last = *pbuf;
  10825. pbuf++;
  10826. }
  10827. if (pbuf >= (buf + buf_size))
  10828. break;
  10829. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10830. if ((last == ',') && (*pbuf == ',')) {
  10831. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10832. __func__, "Invalid data format.");
  10833. return -EINVAL;
  10834. }
  10835. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10836. && (pbuf < (buf + buf_size))
  10837. && (*pbuf != ' ')
  10838. && (*pbuf != ','); j++) {
  10839. last = *pbuf;
  10840. scan_buf[j] = *pbuf++;
  10841. }
  10842. ret = kstrtoul(scan_buf, 16, &value);
  10843. if (ret < 0) {
  10844. pt_debug(dev, DL_ERROR,
  10845. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10846. "Invalid data format. ", scan_buf,
  10847. "Use \"0xHH,...,0xHH\"", " instead.",
  10848. i, ret);
  10849. return ret;
  10850. }
  10851. ic_buf[i] = value;
  10852. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10853. __func__, i, value);
  10854. i++;
  10855. }
  10856. return i;
  10857. }
  10858. /*******************************************************************************
  10859. * FUNCTION: _pt_ic_parse_input
  10860. *
  10861. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10862. * hex values or dec values into an int array with the following rules:
  10863. * 1) Hex values must have a "0x" prefix for each element or the first element
  10864. * only
  10865. * 2) Dec values do not have any prefix
  10866. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10867. * string
  10868. *
  10869. * RETURN: Number of values parsed
  10870. *
  10871. * PARAMETERS:
  10872. * *dev - pointer to device structure
  10873. * *buf - pointer to buffer that holds the input array to parse
  10874. * buf_size - size of buf
  10875. * *out_buf - pointer to array to store parsed data
  10876. * out_buf_size - max size of buffer to be stored
  10877. ******************************************************************************/
  10878. static int _pt_ic_parse_input(struct device *dev,
  10879. const char *buf, size_t buf_size,
  10880. u32 *out_buf, size_t out_buf_size)
  10881. {
  10882. const char *pbuf = buf;
  10883. unsigned long value;
  10884. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10885. u32 i = 0;
  10886. u32 j;
  10887. int last = 0;
  10888. int ret = 0;
  10889. u8 str_base = 0;
  10890. pt_debug(dev, DL_DEBUG,
  10891. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10892. __func__, buf_size, out_buf_size, "scan buf size",
  10893. PT_MAX_PIP2_MSG_SIZE, buf);
  10894. while (pbuf <= (buf + buf_size)) {
  10895. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10896. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10897. __func__, "Max cmd size exceeded", i,
  10898. PT_MAX_PIP2_MSG_SIZE);
  10899. ret = -EINVAL;
  10900. goto error;
  10901. }
  10902. if (i >= out_buf_size) {
  10903. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10904. __func__, "Buffer size exceeded", i,
  10905. out_buf_size);
  10906. ret = -EINVAL;
  10907. goto error;
  10908. }
  10909. while (((*pbuf == ' ') || (*pbuf == ','))
  10910. && (pbuf < (buf + buf_size))) {
  10911. last = *pbuf;
  10912. pbuf++;
  10913. }
  10914. if (pbuf >= (buf + buf_size))
  10915. break;
  10916. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10917. if ((last == ',') && (*pbuf == ',')) {
  10918. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10919. __func__, "Invalid data format.");
  10920. ret = -EINVAL;
  10921. goto error;
  10922. }
  10923. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10924. && (pbuf < (buf + buf_size))
  10925. && (*pbuf != ' ')
  10926. && (*pbuf != ','); j++) {
  10927. last = *pbuf;
  10928. scan_buf[j] = *pbuf++;
  10929. }
  10930. if (i == 0) {
  10931. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10932. (strncmp(scan_buf, "0X", 2) == 0))
  10933. str_base = 16;
  10934. else
  10935. str_base = 10;
  10936. } else {
  10937. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10938. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10939. (str_base == 10)) {
  10940. pt_debug(dev, DL_ERROR,
  10941. "%s: Decimal and Heximal data mixed\n",
  10942. __func__);
  10943. ret = -EINVAL;
  10944. goto error;
  10945. }
  10946. }
  10947. ret = kstrtoul(scan_buf, str_base, &value);
  10948. if (ret < 0) {
  10949. pt_debug(dev, DL_ERROR,
  10950. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10951. "Invalid data format. ", scan_buf,
  10952. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10953. " instead.", i, ret);
  10954. goto error;
  10955. }
  10956. out_buf[i] = value;
  10957. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10958. __func__, i, value, value);
  10959. i++;
  10960. }
  10961. ret = i;
  10962. error:
  10963. return ret;
  10964. }
  10965. #ifdef TTHE_TUNER_SUPPORT
  10966. /*******************************************************************************
  10967. * FUNCTION: tthe_debugfs_open
  10968. *
  10969. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10970. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10971. * printing a large number of fingers and sensor data without overflowing
  10972. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10973. * even multiple of PAGE_SIZE
  10974. *
  10975. * RETURN:
  10976. * 0 = success
  10977. * !0 = failure
  10978. *
  10979. * PARAMETERS:
  10980. * *inode - file inode number
  10981. * *filp - file pointer to debugfs file
  10982. ******************************************************************************/
  10983. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10984. {
  10985. struct pt_core_data *cd = inode->i_private;
  10986. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10987. filp->private_data = inode->i_private;
  10988. if (cd->tthe_buf)
  10989. return -EBUSY;
  10990. while (buf_size < 4096)
  10991. buf_size = buf_size << 1;
  10992. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10993. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10994. cd->tthe_buf_size = buf_size;
  10995. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10996. if (!cd->tthe_buf)
  10997. return -ENOMEM;
  10998. return 0;
  10999. }
  11000. /*******************************************************************************
  11001. * FUNCTION: tthe_debugfs_close
  11002. *
  11003. * SUMMARY: Close method for tthe_tuner debugfs node.
  11004. *
  11005. * RETURN:
  11006. * 0 = success
  11007. * !0 = failure
  11008. *
  11009. * PARAMETERS:
  11010. * *inode - file inode number
  11011. * *filp - file pointer to debugfs file
  11012. ******************************************************************************/
  11013. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11014. {
  11015. struct pt_core_data *cd = filp->private_data;
  11016. filp->private_data = NULL;
  11017. kfree(cd->tthe_buf);
  11018. cd->tthe_buf = NULL;
  11019. return 0;
  11020. }
  11021. /*******************************************************************************
  11022. * FUNCTION: tthe_debugfs_read
  11023. *
  11024. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11025. * tthe_buf to user buffer.
  11026. *
  11027. * RETURN: Size of debugfs data print
  11028. *
  11029. * PARAMETERS:
  11030. * *filp - file pointer to debugfs file
  11031. * *buf - the user space buffer to read to
  11032. * count - the maximum number of bytes to read
  11033. * *ppos - the current position in the buffer
  11034. ******************************************************************************/
  11035. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11036. size_t count, loff_t *ppos)
  11037. {
  11038. struct pt_core_data *cd = filp->private_data;
  11039. int size;
  11040. int ret;
  11041. static int partial_read;
  11042. wait_event_interruptible(cd->wait_q,
  11043. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11044. mutex_lock(&cd->tthe_lock);
  11045. if (cd->tthe_exit) {
  11046. mutex_unlock(&cd->tthe_lock);
  11047. return 0;
  11048. }
  11049. if (count > cd->tthe_buf_len)
  11050. size = cd->tthe_buf_len;
  11051. else
  11052. size = count;
  11053. if (!size) {
  11054. mutex_unlock(&cd->tthe_lock);
  11055. return 0;
  11056. }
  11057. if (partial_read) {
  11058. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11059. partial_read = 0;
  11060. } else {
  11061. ret = copy_to_user(buf, cd->tthe_buf, size);
  11062. }
  11063. if (size == count)
  11064. partial_read = count;
  11065. if (ret == size)
  11066. return -EFAULT;
  11067. size -= ret;
  11068. cd->tthe_buf_len -= size;
  11069. mutex_unlock(&cd->tthe_lock);
  11070. *ppos += size;
  11071. return size;
  11072. }
  11073. static const struct file_operations tthe_debugfs_fops = {
  11074. .open = tthe_debugfs_open,
  11075. .release = tthe_debugfs_close,
  11076. .read = tthe_debugfs_read,
  11077. };
  11078. #endif
  11079. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11080. .start_bl = _pt_request_pip_start_bl,
  11081. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11082. .resume_scanning = _pt_request_pip_resume_scanning,
  11083. .get_param = _pt_request_pip_get_param,
  11084. .set_param = _pt_request_pip_set_param,
  11085. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11086. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11087. .get_data_structure = _pt_request_pip_get_data_structure,
  11088. .run_selftest = _pt_request_pip_run_selftest,
  11089. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11090. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11091. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11092. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11093. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11094. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11095. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11096. .read_data_block = _pt_request_pip_read_data_block,
  11097. .write_data_block = _pt_request_pip_write_data_block,
  11098. .user_cmd = _pt_request_pip_user_cmd,
  11099. .get_bl_info = _pt_request_pip_bl_get_information,
  11100. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11101. .launch_app = _pt_request_pip_launch_app,
  11102. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11103. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11104. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11105. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11106. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11107. .pip2_file_open = _pt_pip2_file_open,
  11108. .pip2_file_close = _pt_pip2_file_close,
  11109. .pip2_file_erase = _pt_pip2_file_erase,
  11110. .read_us_file = _pt_read_us_file,
  11111. .manage_cal_data = _pt_manage_local_cal_data,
  11112. .calc_crc = crc_ccitt_calculate,
  11113. #ifdef TTDL_DIAGNOSTICS
  11114. .pip2_file_read = _pt_pip2_file_read,
  11115. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11116. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11117. .pip2_file_crc = _pt_pip2_file_crc,
  11118. #endif
  11119. };
  11120. static struct pt_core_commands _pt_core_commands = {
  11121. .subscribe_attention = _pt_subscribe_attention,
  11122. .unsubscribe_attention = _pt_unsubscribe_attention,
  11123. .request_exclusive = _pt_request_exclusive,
  11124. .release_exclusive = _pt_release_exclusive,
  11125. .request_reset = _pt_request_reset,
  11126. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11127. .request_enum = _pt_request_enum,
  11128. .request_sysinfo = _pt_request_sysinfo,
  11129. .request_loader_pdata = _pt_request_loader_pdata,
  11130. .request_stop_wd = _pt_request_stop_wd,
  11131. .request_start_wd = _pt_request_start_wd,
  11132. .request_get_mode = _pt_request_get_mode,
  11133. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11134. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11135. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11136. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11137. .request_dut_generation = _pt_request_dut_generation,
  11138. .request_hw_version = _pt_request_hw_version,
  11139. .parse_sysfs_input = _pt_ic_parse_input,
  11140. #ifdef TTHE_TUNER_SUPPORT
  11141. .request_tthe_print = _pt_request_tthe_print,
  11142. #endif
  11143. #ifdef TTDL_DIAGNOSTICS
  11144. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11145. #endif
  11146. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11147. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11148. };
  11149. struct pt_core_commands *pt_get_commands(void)
  11150. {
  11151. return &_pt_core_commands;
  11152. }
  11153. EXPORT_SYMBOL_GPL(pt_get_commands);
  11154. static DEFINE_MUTEX(core_list_lock);
  11155. static LIST_HEAD(core_list);
  11156. static DEFINE_MUTEX(module_list_lock);
  11157. static LIST_HEAD(module_list);
  11158. static int core_number;
  11159. /*******************************************************************************
  11160. * FUNCTION: pt_probe_module
  11161. *
  11162. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11163. *
  11164. * RETURN:
  11165. * 0 = success
  11166. * !0 = failure
  11167. *
  11168. * PARAMETERS:
  11169. * *cd - pointer to core data
  11170. * *module - pointer to module structure
  11171. ******************************************************************************/
  11172. static int pt_probe_module(struct pt_core_data *cd,
  11173. struct pt_module *module)
  11174. {
  11175. struct module_node *module_node;
  11176. int rc = 0;
  11177. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11178. if (!module_node)
  11179. return -ENOMEM;
  11180. module_node->module = module;
  11181. mutex_lock(&cd->module_list_lock);
  11182. list_add(&module_node->node, &cd->module_list);
  11183. mutex_unlock(&cd->module_list_lock);
  11184. rc = module->probe(cd->dev, &module_node->data);
  11185. if (rc) {
  11186. /*
  11187. * Remove from the list when probe fails
  11188. * in order not to call release
  11189. */
  11190. mutex_lock(&cd->module_list_lock);
  11191. list_del(&module_node->node);
  11192. mutex_unlock(&cd->module_list_lock);
  11193. kfree(module_node);
  11194. goto exit;
  11195. }
  11196. exit:
  11197. return rc;
  11198. }
  11199. /*******************************************************************************
  11200. * FUNCTION: pt_release_module
  11201. *
  11202. * SUMMARY: Call the release pointer and remove the module pointer from
  11203. * module_list.
  11204. *
  11205. * PARAMETERS:
  11206. * *cd - pointer to core data
  11207. * *module - pointer to module structure
  11208. ******************************************************************************/
  11209. static void pt_release_module(struct pt_core_data *cd,
  11210. struct pt_module *module)
  11211. {
  11212. struct module_node *m, *m_n;
  11213. mutex_lock(&cd->module_list_lock);
  11214. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11215. if (m->module == module) {
  11216. module->release(cd->dev, m->data);
  11217. list_del(&m->node);
  11218. kfree(m);
  11219. break;
  11220. }
  11221. mutex_unlock(&cd->module_list_lock);
  11222. }
  11223. /*******************************************************************************
  11224. * FUNCTION: pt_probe_modules
  11225. *
  11226. * SUMMARY: Iterate module_list and probe each module.
  11227. *
  11228. * PARAMETERS:
  11229. * *cd - pointer to core data
  11230. ******************************************************************************/
  11231. static void pt_probe_modules(struct pt_core_data *cd)
  11232. {
  11233. struct pt_module *m;
  11234. int rc = 0;
  11235. mutex_lock(&module_list_lock);
  11236. list_for_each_entry(m, &module_list, node) {
  11237. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11238. __func__, m->name);
  11239. rc = pt_probe_module(cd, m);
  11240. if (rc)
  11241. pt_debug(cd->dev, DL_ERROR,
  11242. "%s: Probe fails for module %s\n",
  11243. __func__, m->name);
  11244. }
  11245. mutex_unlock(&module_list_lock);
  11246. }
  11247. /*******************************************************************************
  11248. * FUNCTION: pt_release_modules
  11249. *
  11250. * SUMMARY: Iterate module_list and remove each module.
  11251. *
  11252. * PARAMETERS:
  11253. * *cd - pointer to core data
  11254. ******************************************************************************/
  11255. static void pt_release_modules(struct pt_core_data *cd)
  11256. {
  11257. struct pt_module *m;
  11258. mutex_lock(&module_list_lock);
  11259. list_for_each_entry(m, &module_list, node)
  11260. pt_release_module(cd, m);
  11261. mutex_unlock(&module_list_lock);
  11262. }
  11263. /*******************************************************************************
  11264. * FUNCTION: pt_get_core_data
  11265. *
  11266. * SUMMARY: Iterate core_list and get core data.
  11267. *
  11268. * RETURN:
  11269. * pointer to core data or null pointer if fail
  11270. *
  11271. * PARAMETERS:
  11272. * *id - pointer to core id
  11273. ******************************************************************************/
  11274. struct pt_core_data *pt_get_core_data(char *id)
  11275. {
  11276. struct pt_core_data *d;
  11277. list_for_each_entry(d, &core_list, node)
  11278. if (!strncmp(d->core_id, id, 20))
  11279. return d;
  11280. return NULL;
  11281. }
  11282. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11283. /*******************************************************************************
  11284. * FUNCTION: pt_add_core
  11285. *
  11286. * SUMMARY: Add core data to the core_list.
  11287. *
  11288. * PARAMETERS:
  11289. * *dev - pointer to device structure
  11290. ******************************************************************************/
  11291. static void pt_add_core(struct device *dev)
  11292. {
  11293. struct pt_core_data *d;
  11294. struct pt_core_data *cd = dev_get_drvdata(dev);
  11295. mutex_lock(&core_list_lock);
  11296. list_for_each_entry(d, &core_list, node)
  11297. if (d->dev == dev)
  11298. goto unlock;
  11299. list_add(&cd->node, &core_list);
  11300. unlock:
  11301. mutex_unlock(&core_list_lock);
  11302. }
  11303. /*******************************************************************************
  11304. * FUNCTION: pt_del_core
  11305. *
  11306. * SUMMARY: Remove core data from the core_list.
  11307. *
  11308. * PARAMETERS:
  11309. * *dev - pointer to device structure
  11310. ******************************************************************************/
  11311. static void pt_del_core(struct device *dev)
  11312. {
  11313. struct pt_core_data *d, *d_n;
  11314. mutex_lock(&core_list_lock);
  11315. list_for_each_entry_safe(d, d_n, &core_list, node)
  11316. if (d->dev == dev) {
  11317. list_del(&d->node);
  11318. goto unlock;
  11319. }
  11320. unlock:
  11321. mutex_unlock(&core_list_lock);
  11322. }
  11323. /*******************************************************************************
  11324. * FUNCTION: pt_register_module
  11325. *
  11326. * SUMMARY: Register the module to module_list and probe the module for each
  11327. * core.
  11328. *
  11329. * RETURN:
  11330. * 0 = success
  11331. * !0 = failure
  11332. *
  11333. * PARAMETERS:
  11334. * *module - pointer to module structure
  11335. ******************************************************************************/
  11336. int pt_register_module(struct pt_module *module)
  11337. {
  11338. struct pt_module *m;
  11339. struct pt_core_data *cd;
  11340. int rc = 0;
  11341. if (!module || !module->probe || !module->release)
  11342. return -EINVAL;
  11343. mutex_lock(&module_list_lock);
  11344. list_for_each_entry(m, &module_list, node)
  11345. if (m == module) {
  11346. rc = -EEXIST;
  11347. goto unlock;
  11348. }
  11349. list_add(&module->node, &module_list);
  11350. /* Probe the module for each core */
  11351. mutex_lock(&core_list_lock);
  11352. list_for_each_entry(cd, &core_list, node)
  11353. pt_probe_module(cd, module);
  11354. mutex_unlock(&core_list_lock);
  11355. unlock:
  11356. mutex_unlock(&module_list_lock);
  11357. return rc;
  11358. }
  11359. EXPORT_SYMBOL_GPL(pt_register_module);
  11360. /*******************************************************************************
  11361. * FUNCTION: pt_unregister_module
  11362. *
  11363. * SUMMARY: Release the module for each core and remove the module from
  11364. * module_list.
  11365. *
  11366. * RETURN:
  11367. * 0 = success
  11368. * !0 = failure
  11369. *
  11370. * PARAMETERS:
  11371. * *module - pointer to module structure
  11372. ******************************************************************************/
  11373. void pt_unregister_module(struct pt_module *module)
  11374. {
  11375. struct pt_module *m, *m_n;
  11376. struct pt_core_data *cd;
  11377. if (!module)
  11378. return;
  11379. mutex_lock(&module_list_lock);
  11380. /* Release the module for each core */
  11381. mutex_lock(&core_list_lock);
  11382. list_for_each_entry(cd, &core_list, node)
  11383. pt_release_module(cd, module);
  11384. mutex_unlock(&core_list_lock);
  11385. list_for_each_entry_safe(m, m_n, &module_list, node)
  11386. if (m == module) {
  11387. list_del(&m->node);
  11388. break;
  11389. }
  11390. mutex_unlock(&module_list_lock);
  11391. }
  11392. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11393. /*******************************************************************************
  11394. * FUNCTION: pt_get_module_data
  11395. *
  11396. * SUMMARY: Get module data from module_node by module_list.
  11397. *
  11398. * RETURN:
  11399. * pointer to module data
  11400. *
  11401. * PARAMETERS:
  11402. * *dev - pointer to device structure
  11403. * *module - pointer to module structure
  11404. ******************************************************************************/
  11405. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11406. {
  11407. struct pt_core_data *cd = dev_get_drvdata(dev);
  11408. struct module_node *m;
  11409. void *data = NULL;
  11410. mutex_lock(&cd->module_list_lock);
  11411. list_for_each_entry(m, &cd->module_list, node)
  11412. if (m->module == module) {
  11413. data = m->data;
  11414. break;
  11415. }
  11416. mutex_unlock(&cd->module_list_lock);
  11417. return data;
  11418. }
  11419. EXPORT_SYMBOL(pt_get_module_data);
  11420. #ifdef CONFIG_HAS_EARLYSUSPEND
  11421. /*******************************************************************************
  11422. * FUNCTION: pt_early_suspend
  11423. *
  11424. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11425. * attention list.
  11426. *
  11427. * PARAMETERS:
  11428. * *h - pointer to early_suspend structure
  11429. ******************************************************************************/
  11430. static void pt_early_suspend(struct early_suspend *h)
  11431. {
  11432. struct pt_core_data *cd =
  11433. container_of(h, struct pt_core_data, es);
  11434. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11435. }
  11436. /*******************************************************************************
  11437. * FUNCTION: pt_late_resume
  11438. *
  11439. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11440. * attention list.
  11441. *
  11442. * PARAMETERS:
  11443. * *h - pointer to early_suspend structure
  11444. ******************************************************************************/
  11445. static void pt_late_resume(struct early_suspend *h)
  11446. {
  11447. struct pt_core_data *cd =
  11448. container_of(h, struct pt_core_data, es);
  11449. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11450. }
  11451. /*******************************************************************************
  11452. * FUNCTION: pt_setup_early_suspend
  11453. *
  11454. * SUMMARY: Register early/suspend function to the system.
  11455. *
  11456. * PARAMETERS:
  11457. * *cd - pointer to core data
  11458. ******************************************************************************/
  11459. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11460. {
  11461. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11462. cd->es.suspend = pt_early_suspend;
  11463. cd->es.resume = pt_late_resume;
  11464. register_early_suspend(&cd->es);
  11465. }
  11466. #elif defined(CONFIG_DRM)
  11467. static void pt_resume_work(struct work_struct *work)
  11468. {
  11469. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11470. resume_work);
  11471. int rc = 0;
  11472. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11473. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11474. return;
  11475. rc = pt_core_easywake_off(pt_data);
  11476. if (rc < 0) {
  11477. pt_debug(pt_data->dev, DL_ERROR,
  11478. "%s: Error on wake\n", __func__);
  11479. }
  11480. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11481. return;
  11482. }
  11483. static void pt_suspend_work(struct work_struct *work)
  11484. {
  11485. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11486. suspend_work);
  11487. int rc = 0;
  11488. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11489. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11490. return;
  11491. rc = pt_core_easywake_on(pt_data);
  11492. if (rc < 0) {
  11493. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11494. return;
  11495. }
  11496. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11497. return;
  11498. }
  11499. /*******************************************************************************
  11500. * FUNCTION: drm_notifier_callback
  11501. *
  11502. * SUMMARY: Call back function for DRM notifier to allow to call
  11503. * resume/suspend attention list.
  11504. *
  11505. * RETURN:
  11506. * 0 = success
  11507. *
  11508. * PARAMETERS:
  11509. * *self - pointer to notifier_block structure
  11510. * event - event type of fb notifier
  11511. * *data - pointer to fb_event structure
  11512. ******************************************************************************/
  11513. static void drm_notifier_callback(enum panel_event_notifier_tag tag,
  11514. struct panel_event_notification *notification, void *client_data)
  11515. {
  11516. struct pt_core_data *cd = client_data;
  11517. if(!notification)
  11518. {
  11519. pt_debug(cd->dev,DL_INFO, "%s: Invalid notification\n", __func__);
  11520. return;
  11521. }
  11522. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11523. if (!(notification->notif_type == DRM_PANEL_EVENT_BLANK ||
  11524. notification->notif_type == DRM_PANEL_EVENT_BLANK)) {
  11525. pt_debug(cd->dev, DL_INFO, "%s: Event(%d) do not need process\n",
  11526. __func__, notification->notif_type);
  11527. goto exit;
  11528. }
  11529. if (cd->quick_boot || cd->drv_debug_suspend)
  11530. goto exit;
  11531. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%d,fb_state %d",
  11532. __func__, notification->notif_type, cd->fb_state);
  11533. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11534. __func__, (notification->notif_type == DRM_PANEL_EVENT_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11535. if (notification->notif_type == DRM_PANEL_EVENT_UNBLANK) {
  11536. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11537. if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11538. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %d, not care\n",
  11539. __func__, notification->notif_type);
  11540. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11541. if (cd->fb_state != FB_ON) {
  11542. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11543. __func__);
  11544. cancel_work_sync(&cd->resume_offload_work);
  11545. cancel_work_sync(&cd->suspend_offload_work);
  11546. cancel_work_sync(&cd->resume_work);
  11547. cancel_work_sync(&cd->suspend_work);
  11548. queue_work(cd->pt_workqueue, &cd->resume_work);
  11549. #if defined(CONFIG_PM_SLEEP)
  11550. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11551. __func__);
  11552. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11553. pt_core_resume_(cd->dev);
  11554. #endif
  11555. cd->fb_state = FB_ON;
  11556. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11557. }
  11558. }
  11559. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK_LP) {
  11560. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11561. if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11562. if (cd->fb_state != FB_OFF) {
  11563. #if defined(CONFIG_PM_SLEEP)
  11564. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11565. __func__);
  11566. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11567. pt_core_suspend_(cd->dev);
  11568. #endif
  11569. cancel_work_sync(&cd->resume_offload_work);
  11570. cancel_work_sync(&cd->suspend_offload_work);
  11571. cancel_work_sync(&cd->resume_work);
  11572. cancel_work_sync(&cd->suspend_work);
  11573. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11574. cd->fb_state = FB_OFF;
  11575. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11576. }
  11577. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11578. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11579. __func__, notification->notif_type);
  11580. }
  11581. } else {
  11582. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11583. __func__, notification->notif_type);
  11584. }
  11585. exit:
  11586. return;
  11587. }
  11588. /*******************************************************************************
  11589. * FUNCTION: pt_setup_drm_notifier
  11590. *
  11591. * SUMMARY: Set up call back function into drm notifier.
  11592. *
  11593. * PARAMETERS:
  11594. * *cd - pointer to core data
  11595. ******************************************************************************/
  11596. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11597. {
  11598. void *cookie = NULL;
  11599. if (!active_panel)
  11600. pt_debug(cd->dev, DL_ERROR,
  11601. "%s: Active panel not registered!\n", __func__);
  11602. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11603. if (!cd->pt_workqueue) {
  11604. pt_debug(cd->dev, DL_ERROR,
  11605. "%s: worker thread creation failed !\n", __func__);
  11606. }
  11607. if (cd->pt_workqueue) {
  11608. INIT_WORK(&cd->resume_work, pt_resume_work);
  11609. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11610. }
  11611. cookie = panel_event_notifier_register(PANEL_EVENT_NOTIFICATION_PRIMARY,
  11612. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH,
  11613. active_panel,&drm_notifier_callback, cd);
  11614. }
  11615. #elif defined(CONFIG_FB)
  11616. /*******************************************************************************
  11617. * FUNCTION: fb_notifier_callback
  11618. *
  11619. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11620. * resume/suspend attention list.
  11621. *
  11622. * RETURN:
  11623. * 0 = success
  11624. *
  11625. * PARAMETERS:
  11626. * *self - pointer to notifier_block structure
  11627. * event - event type of fb notifier
  11628. * *data - pointer to fb_event structure
  11629. ******************************************************************************/
  11630. static int fb_notifier_callback(struct notifier_block *self,
  11631. unsigned long event, void *data)
  11632. {
  11633. struct pt_core_data *cd =
  11634. container_of(self, struct pt_core_data, fb_notifier);
  11635. struct fb_event *evdata = data;
  11636. int *blank;
  11637. if (event != FB_EVENT_BLANK || !evdata)
  11638. goto exit;
  11639. blank = evdata->data;
  11640. if (*blank == FB_BLANK_UNBLANK) {
  11641. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11642. if (cd->fb_state != FB_ON) {
  11643. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11644. #if defined(CONFIG_PM_SLEEP)
  11645. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11646. pt_core_resume_(cd->dev);
  11647. #endif
  11648. cd->fb_state = FB_ON;
  11649. }
  11650. } else if (*blank == FB_BLANK_POWERDOWN) {
  11651. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11652. if (cd->fb_state != FB_OFF) {
  11653. #if defined(CONFIG_PM_SLEEP)
  11654. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11655. pt_core_suspend_(cd->dev);
  11656. #endif
  11657. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11658. cd->fb_state = FB_OFF;
  11659. }
  11660. }
  11661. exit:
  11662. return 0;
  11663. }
  11664. /*******************************************************************************
  11665. * FUNCTION: pt_setup_fb_notifier
  11666. *
  11667. * SUMMARY: Set up call back function into fb notifier.
  11668. *
  11669. * PARAMETERS:
  11670. * *cd - pointer to core data
  11671. ******************************************************************************/
  11672. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11673. {
  11674. int rc = 0;
  11675. cd->fb_state = FB_ON;
  11676. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11677. rc = fb_register_client(&cd->fb_notifier);
  11678. if (rc)
  11679. pt_debug(cd->dev, DL_ERROR,
  11680. "Unable to register fb_notifier: %d\n", rc);
  11681. }
  11682. #endif
  11683. /*******************************************************************************
  11684. * FUNCTION: pt_watchdog_work
  11685. *
  11686. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11687. * sleeping then this function simply returns. If the DUT is awake the
  11688. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11689. * somehow a response is waiting on the DUT that has not been read. If
  11690. * this occurs the message is simply consumed. If or once the IRQ is
  11691. * cleared, a PIP PING message is sent to the DUT and if the response
  11692. * is received the watchdog succeeds and exits, if no response is seen
  11693. * a startup is queued unless the maximum number of startups have already
  11694. * been attempted, in that case a BL is attempted.
  11695. *
  11696. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11697. * work thread
  11698. *
  11699. * RETURN: void
  11700. *
  11701. * PARAMETERS:
  11702. * *work - pointer to a work structure for the watchdog work queue
  11703. ******************************************************************************/
  11704. static void pt_watchdog_work(struct work_struct *work)
  11705. {
  11706. int rc = 0;
  11707. struct pt_core_data *cd = container_of(work,
  11708. struct pt_core_data, watchdog_work);
  11709. /*
  11710. * if found the current sleep_state is SS_SLEEPING
  11711. * then no need to request_exclusive, directly return
  11712. */
  11713. if (cd->sleep_state == SS_SLEEPING)
  11714. return;
  11715. #ifdef TTDL_DIAGNOSTICS
  11716. cd->watchdog_count++;
  11717. #endif /* TTDL_DIAGNOSTICS */
  11718. /*
  11719. * The first WD interval was extended to allow DDI to come up.
  11720. * If the WD interval is not the default then adjust timer to the
  11721. * current setting. The user can override value with drv_debug sysfs.
  11722. */
  11723. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11724. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11725. msecs_to_jiffies(cd->watchdog_interval));
  11726. }
  11727. if (pt_check_irq_asserted(cd)) {
  11728. #ifdef TTDL_DIAGNOSTICS
  11729. cd->watchdog_irq_stuck_count++;
  11730. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11731. #endif /* TTDL_DIAGNOSTICS */
  11732. pt_debug(cd->dev, DL_ERROR,
  11733. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11734. __func__);
  11735. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11736. }
  11737. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11738. if (rc < 0) {
  11739. pt_debug(cd->dev, DL_ERROR,
  11740. "%s: fail get exclusive ex=%p own=%p\n",
  11741. __func__, cd->exclusive_dev, cd->dev);
  11742. goto queue_startup;
  11743. }
  11744. rc = pt_pip_null_(cd);
  11745. if (release_exclusive(cd, cd->dev) < 0)
  11746. pt_debug(cd->dev, DL_ERROR,
  11747. "%s: fail to release exclusive\n", __func__);
  11748. queue_startup:
  11749. if (rc) {
  11750. #ifdef TTDL_DIAGNOSTICS
  11751. cd->watchdog_failed_access_count++;
  11752. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11753. #endif /* TTDL_DIAGNOSTICS */
  11754. pt_debug(cd->dev, DL_ERROR,
  11755. "%s: failed to access device in WD, retry count=%d\n",
  11756. __func__, cd->startup_retry_count);
  11757. /* Already tried FW upgrade because of watchdog but failed */
  11758. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11759. return;
  11760. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11761. /*
  11762. * Any wrapper function that trys to disable the
  11763. * WD killing this worker cannot be called here.
  11764. */
  11765. rc = request_exclusive(cd, cd->dev,
  11766. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11767. if (rc < 0) {
  11768. pt_debug(cd->dev, DL_ERROR,
  11769. "%s: fail get exclusive ex=%p own=%p\n",
  11770. __func__, cd->exclusive_dev, cd->dev);
  11771. goto exit;
  11772. }
  11773. cd->hw_detected = false;
  11774. cd->startup_status = STARTUP_STATUS_START;
  11775. pt_debug(cd->dev, DL_DEBUG,
  11776. "%s: Startup Status Reset\n", __func__);
  11777. rc = pt_dut_reset_and_wait(cd);
  11778. if (release_exclusive(cd, cd->dev) < 0)
  11779. pt_debug(cd->dev, DL_ERROR,
  11780. "%s: fail to release exclusive\n",
  11781. __func__);
  11782. if (!rc) {
  11783. cd->hw_detected = true;
  11784. if (!cd->flashless_dut)
  11785. pt_queue_enum(cd);
  11786. }
  11787. #ifdef TTDL_DIAGNOSTICS
  11788. cd->wd_xres_count++;
  11789. pt_debug(cd->dev, DL_ERROR,
  11790. "%s: Comm Failed - DUT reset [#%d]\n",
  11791. __func__, cd->wd_xres_count);
  11792. #endif /* TTDL_DIAGNOSTICS */
  11793. } else {
  11794. /*
  11795. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11796. * reset the part to regain communications, try to BL
  11797. */
  11798. pt_debug(cd->dev, DL_ERROR,
  11799. "%s: WD DUT access failure, Start FW Upgrade\n",
  11800. __func__);
  11801. #ifdef TTDL_DIAGNOSTICS
  11802. /*
  11803. * When diagnostics is enabled allow TTDL to keep
  11804. * trying to find the DUT. This allows the DUT to be
  11805. * hot swap-able while the host stays running. In
  11806. * production this may not be wanted as a customer
  11807. * may have several touch drivers and any driver
  11808. * that doesn't match the current DUT should give
  11809. * up trying and give up using the bus.
  11810. */
  11811. pt_debug(cd->dev, DL_INFO,
  11812. "%s: Resetting startup_retry_count\n",
  11813. __func__);
  11814. cd->startup_retry_count = 0;
  11815. #endif /* TTDL_DIAGNOSTICS */
  11816. /*
  11817. * Since fw may be broken,reset sysinfo ready flag
  11818. * to let upgrade function work.
  11819. */
  11820. mutex_lock(&cd->system_lock);
  11821. cd->sysinfo.ready = false;
  11822. mutex_unlock(&cd->system_lock);
  11823. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11824. pt_debug(cd->dev, DL_ERROR,
  11825. "%s: Queue Restart\n", __func__);
  11826. pt_queue_restart(cd);
  11827. } else
  11828. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11829. }
  11830. } else {
  11831. cd->hw_detected = true;
  11832. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11833. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11834. pt_debug(cd->dev, DL_ERROR,
  11835. "%s: HW detected but not enumerated\n",
  11836. __func__);
  11837. pt_queue_enum(cd);
  11838. }
  11839. }
  11840. exit:
  11841. pt_start_wd_timer(cd);
  11842. }
  11843. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11844. /*******************************************************************************
  11845. * FUNCTION: pt_watchdog_timer
  11846. *
  11847. * SUMMARY: The function that is called when the WD timer expires. If the
  11848. * watchdog work is not already busy schedule the watchdog work queue.
  11849. *
  11850. * RETURN: void
  11851. *
  11852. * PARAMETERS:
  11853. * handle - Handle to the watchdog timer
  11854. ******************************************************************************/
  11855. static void pt_watchdog_timer(unsigned long handle)
  11856. {
  11857. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11858. if (!cd)
  11859. return;
  11860. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11861. __func__);
  11862. if (!work_pending(&cd->watchdog_work))
  11863. schedule_work(&cd->watchdog_work);
  11864. }
  11865. #else
  11866. /*******************************************************************************
  11867. * FUNCTION: pt_watchdog_timer
  11868. *
  11869. * SUMMARY: The function that is called when the WD timer expires. If the
  11870. * watchdog work is not already busy schedule the watchdog work queue.
  11871. *
  11872. * RETURN: void
  11873. *
  11874. * PARAMETERS:
  11875. * *t - Pointer to timer list
  11876. ******************************************************************************/
  11877. static void pt_watchdog_timer(struct timer_list *t)
  11878. {
  11879. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11880. if (!cd)
  11881. return;
  11882. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11883. __func__);
  11884. if (!work_pending(&cd->watchdog_work))
  11885. schedule_work(&cd->watchdog_work);
  11886. }
  11887. #endif
  11888. /*******************************************************************************
  11889. * Core sysfs show and store functions
  11890. ******************************************************************************/
  11891. /*******************************************************************************
  11892. * FUNCTION: pt_hw_version_show
  11893. *
  11894. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11895. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11896. *
  11897. * RETURN: size of data written to sysfs node
  11898. *
  11899. * PARAMETERS:
  11900. * *dev - pointer to device structure
  11901. * *attr - pointer to device attributes structure
  11902. * *buf - pointer to print output buffer
  11903. ******************************************************************************/
  11904. static ssize_t pt_hw_version_show(struct device *dev,
  11905. struct device_attribute *attr, char *buf)
  11906. {
  11907. struct pt_core_data *cd = dev_get_drvdata(dev);
  11908. _pt_request_hw_version(dev, cd->hw_version);
  11909. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11910. }
  11911. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11912. /*******************************************************************************
  11913. * FUNCTION: pt_drv_version_show
  11914. *
  11915. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11916. * TTDL version information
  11917. *
  11918. * RETURN: Char buffer with printed TTDL version information
  11919. *
  11920. * PARAMETERS:
  11921. * *dev - pointer to device structure
  11922. * *attr - pointer to device attributes
  11923. * *buf - pointer to output buffer
  11924. ******************************************************************************/
  11925. static ssize_t pt_drv_version_show(struct device *dev,
  11926. struct device_attribute *attr, char *buf)
  11927. {
  11928. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11929. "Driver: %s\nVersion: %s\nDate: %s\n",
  11930. pt_driver_core_name, pt_driver_core_version,
  11931. pt_driver_core_date);
  11932. }
  11933. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11934. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11935. /*******************************************************************************
  11936. * FUNCTION: pt_fw_version_show
  11937. *
  11938. * SUMMARY: Show method for the fw_version sysfs node that will
  11939. * show the firmware, bootloader and PIP version information
  11940. *
  11941. * RETURN: Size of printed buffer
  11942. *
  11943. * PARAMETERS:
  11944. * *dev - pointer to device structure
  11945. * *attr - pointer to device attributes
  11946. * *buf - pointer to output buffer
  11947. ******************************************************************************/
  11948. static ssize_t pt_fw_version_show(struct device *dev,
  11949. struct device_attribute *attr, char *buf)
  11950. {
  11951. struct pt_core_data *cd = dev_get_drvdata(dev);
  11952. struct pt_ttdata *ttdata;
  11953. int rc = 0;
  11954. if (cd->mode == PT_MODE_OPERATIONAL)
  11955. rc = pt_hid_output_get_sysinfo_(cd);
  11956. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11957. __func__, cd->mode, cd->sysinfo.ready);
  11958. if (cd->sysinfo.ready)
  11959. ttdata = &cd->sysinfo.ttdata;
  11960. else
  11961. rc = -ENODATA;
  11962. if (!rc) {
  11963. return scnprintf(buf, strlen(buf),
  11964. "Status: %d\n"
  11965. "FW : %d.%d.%d\n"
  11966. "Config: %d\n"
  11967. "BL : %d.%d\n"
  11968. "PIP : %d.%d\n",
  11969. rc,
  11970. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11971. ttdata->revctrl,
  11972. ttdata->fw_ver_conf,
  11973. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11974. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11975. } else {
  11976. return scnprintf(buf, strlen(buf),
  11977. "Status: %d\n"
  11978. "FW : n/a\n"
  11979. "Config: n/a\n"
  11980. "BL : n/a\n"
  11981. "PIP : n/a\n",
  11982. rc);
  11983. }
  11984. }
  11985. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11986. /*******************************************************************************
  11987. * FUNCTION: pt_sysinfo_show
  11988. *
  11989. * SUMMARY: Show method for the sysinfo sysfs node that will
  11990. * show all the information from get system information command.
  11991. *
  11992. * RETURN: Size of printed buffer
  11993. *
  11994. * PARAMETERS:
  11995. * *dev - pointer to device structure
  11996. * *attr - pointer to device attributes
  11997. * *buf - pointer to output buffer
  11998. ******************************************************************************/
  11999. static ssize_t pt_sysinfo_show(struct device *dev,
  12000. struct device_attribute *attr, char *buf)
  12001. {
  12002. struct pt_core_data *cd = dev_get_drvdata(dev);
  12003. struct pt_sysinfo *si;
  12004. struct pt_ttdata *ttdata = NULL;
  12005. struct pt_sensing_conf_data *scd = NULL;
  12006. int rc = 0;
  12007. if (cd->mode == PT_MODE_OPERATIONAL) {
  12008. rc = pt_hid_output_get_sysinfo_(cd);
  12009. if (cd->sysinfo.ready) {
  12010. si = &cd->sysinfo;
  12011. ttdata = &si->ttdata;
  12012. scd = &si->sensing_conf_data;
  12013. } else
  12014. rc = -ENODATA;
  12015. } else
  12016. rc = -EPERM;
  12017. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12018. __func__, cd->mode, cd->sysinfo.ready);
  12019. if (!rc && ttdata && scd) {
  12020. return scnprintf(buf, strlen(buf),
  12021. "Status: %d\n"
  12022. "pip_ver_major: 0x%02X\n"
  12023. "pip_ver_minor: 0x%02X\n"
  12024. "fw_pid : 0x%04X\n"
  12025. "fw_ver_major : 0x%02X\n"
  12026. "fw_ver_minor : 0x%02X\n"
  12027. "revctrl : 0x%08X\n"
  12028. "fw_ver_conf : 0x%04X\n"
  12029. "bl_ver_major : 0x%02X\n"
  12030. "bl_ver_minor : 0x%02X\n"
  12031. "jtag_id_h : 0x%04X\n"
  12032. "jtag_id_l : 0x%04X\n"
  12033. "mfg_id[0] : 0x%02X\n"
  12034. "mfg_id[1] : 0x%02X\n"
  12035. "mfg_id[2] : 0x%02X\n"
  12036. "mfg_id[3] : 0x%02X\n"
  12037. "mfg_id[4] : 0x%02X\n"
  12038. "mfg_id[5] : 0x%02X\n"
  12039. "mfg_id[6] : 0x%02X\n"
  12040. "mfg_id[7] : 0x%02X\n"
  12041. "post_code : 0x%04X\n"
  12042. "electrodes_x : 0x%02X\n"
  12043. "electrodes_y : 0x%02X\n"
  12044. "len_x : 0x%04X\n"
  12045. "len_y : 0x%04X\n"
  12046. "res_x : 0x%04X\n"
  12047. "res_y : 0x%04X\n"
  12048. "max_z : 0x%04X\n"
  12049. "origin_x : 0x%02X\n"
  12050. "origin_y : 0x%02X\n"
  12051. "panel_id : 0x%02X\n"
  12052. "btn : 0x%02X\n"
  12053. "scan_mode : 0x%02X\n"
  12054. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12055. rc,
  12056. ttdata->pip_ver_major,
  12057. ttdata->pip_ver_minor,
  12058. ttdata->fw_pid,
  12059. ttdata->fw_ver_major,
  12060. ttdata->fw_ver_minor,
  12061. ttdata->revctrl,
  12062. ttdata->fw_ver_conf,
  12063. ttdata->bl_ver_major,
  12064. ttdata->bl_ver_minor,
  12065. ttdata->jtag_id_h,
  12066. ttdata->jtag_id_l,
  12067. ttdata->mfg_id[0],
  12068. ttdata->mfg_id[1],
  12069. ttdata->mfg_id[2],
  12070. ttdata->mfg_id[3],
  12071. ttdata->mfg_id[4],
  12072. ttdata->mfg_id[5],
  12073. ttdata->mfg_id[6],
  12074. ttdata->mfg_id[7],
  12075. ttdata->post_code,
  12076. scd->electrodes_x,
  12077. scd->electrodes_y,
  12078. scd->len_x,
  12079. scd->len_y,
  12080. scd->res_x,
  12081. scd->res_y,
  12082. scd->max_z,
  12083. scd->origin_x,
  12084. scd->origin_y,
  12085. scd->panel_id,
  12086. scd->btn,
  12087. scd->scan_mode,
  12088. scd->max_tch);
  12089. } else {
  12090. return scnprintf(buf, strlen(buf),
  12091. "Status: %d\n",
  12092. rc);
  12093. }
  12094. }
  12095. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12096. /*******************************************************************************
  12097. * FUNCTION: pt_hw_reset_show
  12098. *
  12099. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12100. * by toggling the XRES line and then calls the startup function to
  12101. * allow TTDL to re-enumerate the DUT.
  12102. * The printed value reflects the status of the full reset/enum.
  12103. *
  12104. * PARAMETERS:
  12105. * *dev - pointer to Device structure
  12106. * *attr - pointer to the device attribute structure
  12107. * *buf - pointer to buffer to print
  12108. ******************************************************************************/
  12109. static ssize_t pt_hw_reset_show(struct device *dev,
  12110. struct device_attribute *attr, char *buf)
  12111. {
  12112. struct pt_core_data *cd = dev_get_drvdata(dev);
  12113. int rc = 0;
  12114. int time = 0;
  12115. u8 reset_status = 0;
  12116. int t;
  12117. struct pt_hid_desc hid_desc;
  12118. memset(&hid_desc, 0, sizeof(hid_desc));
  12119. /* Only allow DUT reset if no active BL in progress */
  12120. mutex_lock(&cd->firmware_class_lock);
  12121. mutex_lock(&cd->system_lock);
  12122. cd->startup_state = STARTUP_NONE;
  12123. mutex_unlock(&(cd->system_lock));
  12124. pt_stop_wd_timer(cd);
  12125. /* ensure no left over exclusive access is still locked */
  12126. release_exclusive(cd, cd->dev);
  12127. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12128. if (rc) {
  12129. mutex_unlock(&cd->firmware_class_lock);
  12130. pt_debug(cd->dev, DL_ERROR,
  12131. "%s: HW reset failed rc = %d\n", __func__, rc);
  12132. goto exit_hw_reset;
  12133. }
  12134. reset_status |= 0x01 << 0;
  12135. if (cd->flashless_dut) {
  12136. mutex_unlock(&cd->firmware_class_lock);
  12137. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12138. msecs_to_jiffies(200));
  12139. if (IS_TMO(t)) {
  12140. pt_debug(dev, DL_ERROR,
  12141. "%s: Timeout waiting for FW update",
  12142. __func__);
  12143. rc = -ETIME;
  12144. goto exit_hw_reset;
  12145. } else {
  12146. pt_debug(dev, DL_INFO,
  12147. "%s: ----- Wait FW Loading ----",
  12148. __func__);
  12149. rc = _pt_request_wait_for_enum_state(
  12150. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12151. if (rc) {
  12152. pt_debug(cd->dev, DL_ERROR,
  12153. "%s: No FW Sentinel detected rc = %d\n",
  12154. __func__, rc);
  12155. goto exit_hw_reset;
  12156. }
  12157. reset_status |= 0x01 << 1;
  12158. }
  12159. } else {
  12160. /* Wait for any sentinel */
  12161. rc = _pt_request_wait_for_enum_state(dev, 150,
  12162. STARTUP_STATUS_BL_RESET_SENTINEL |
  12163. STARTUP_STATUS_FW_RESET_SENTINEL);
  12164. if (rc) {
  12165. mutex_unlock(&cd->firmware_class_lock);
  12166. pt_debug(cd->dev, DL_ERROR,
  12167. "%s: No Sentinel detected rc = %d\n",
  12168. __func__, rc);
  12169. goto exit_hw_reset;
  12170. }
  12171. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12172. msleep(30);
  12173. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12174. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12175. if (rc < 0) {
  12176. pt_debug(cd->dev, DL_ERROR,
  12177. "%s: Error on getting HID descriptor r=%d\n",
  12178. __func__, rc);
  12179. goto exit_hw_reset;
  12180. }
  12181. cd->mode = pt_get_mode(cd, &hid_desc);
  12182. if (cd->mode == PT_MODE_BOOTLOADER)
  12183. rc = pt_hid_output_bl_launch_app_(cd);
  12184. } else {
  12185. if (cd->mode == PT_MODE_BOOTLOADER)
  12186. rc = pt_pip2_launch_app(dev,
  12187. PT_CORE_CMD_UNPROTECTED);
  12188. }
  12189. if (rc) {
  12190. mutex_unlock(&cd->firmware_class_lock);
  12191. pt_debug(cd->dev, DL_ERROR,
  12192. "%s: PIP launch app failed rc = %d\n",
  12193. __func__, rc);
  12194. goto exit_hw_reset;
  12195. }
  12196. mutex_unlock(&cd->firmware_class_lock);
  12197. reset_status |= 0x01 << 1;
  12198. msleep(20);
  12199. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12200. (cd->mode != PT_MODE_OPERATIONAL))
  12201. pt_queue_restart(cd);
  12202. else
  12203. pt_queue_enum(cd);
  12204. }
  12205. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12206. msleep(50);
  12207. pt_debug(cd->dev, DL_INFO,
  12208. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12209. __func__, time, STARTUP_STATUS_COMPLETE,
  12210. cd->startup_status);
  12211. time += 50;
  12212. }
  12213. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12214. rc = -ETIME;
  12215. goto exit_hw_reset;
  12216. }
  12217. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12218. __func__, cd->startup_status);
  12219. reset_status |= 0x01 << 2;
  12220. pt_start_wd_timer(cd);
  12221. exit_hw_reset:
  12222. return scnprintf(buf, strlen(buf),
  12223. "Status: %d\n"
  12224. "Reset Status: 0x%02X\n", rc, reset_status);
  12225. }
  12226. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12227. /*******************************************************************************
  12228. * FUNCTION: pt_pip2_cmd_rsp_store
  12229. *
  12230. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12231. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12232. * Command byte order:
  12233. * Byte [0] - PIP2 command ID
  12234. * Byte [1-n] - PIP2 command payload
  12235. *
  12236. * RETURN: Size of passed in buffer
  12237. *
  12238. * PARAMETERS:
  12239. * *dev - pointer to device structure
  12240. * *attr - pointer to device attributes
  12241. * *buf - pointer to buffer that hold the command parameters
  12242. * size - size of buf
  12243. ******************************************************************************/
  12244. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12245. struct device_attribute *attr, const char *buf, size_t size)
  12246. {
  12247. struct pt_core_data *cd = dev_get_drvdata(dev);
  12248. u16 actual_read_len;
  12249. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12250. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12251. u8 pip2_cmd_id = 0x00;
  12252. u8 *pip2_cmd_data = NULL;
  12253. int data_len = 0;
  12254. int length;
  12255. int rc = 0;
  12256. /* clear shared data */
  12257. mutex_lock(&cd->sysfs_lock);
  12258. cd->raw_cmd_status = 0;
  12259. cd->cmd_rsp_buf_len = 0;
  12260. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12261. mutex_unlock(&cd->sysfs_lock);
  12262. length = _pt_ic_parse_input_hex(dev, buf, size,
  12263. input_data, PT_MAX_PIP2_MSG_SIZE);
  12264. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12265. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12266. __func__);
  12267. rc = -EINVAL;
  12268. goto exit;
  12269. }
  12270. /* Send PIP2 command if enough data was provided */
  12271. if (length >= 1) {
  12272. pip2_cmd_id = input_data[0];
  12273. pip2_cmd_data = &input_data[1];
  12274. data_len = length - 1;
  12275. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12276. pip2_cmd_id, pip2_cmd_data, data_len,
  12277. read_buf, &actual_read_len);
  12278. cd->raw_cmd_status = rc;
  12279. if (rc) {
  12280. pt_debug(dev, DL_ERROR,
  12281. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12282. __func__, pip2_cmd_id, rc);
  12283. goto exit;
  12284. } else {
  12285. cd->cmd_rsp_buf_len = actual_read_len;
  12286. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12287. pt_debug(dev, DL_ERROR,
  12288. "%s: PIP2 actual_read_len = %d\n",
  12289. __func__, actual_read_len);
  12290. }
  12291. } else {
  12292. rc = -EINVAL;
  12293. pt_debug(dev, DL_ERROR,
  12294. "%s: Insufficient data provided for PIP2 cmd\n",
  12295. __func__);
  12296. }
  12297. exit:
  12298. if (rc)
  12299. return rc;
  12300. return size;
  12301. }
  12302. /*******************************************************************************
  12303. * FUNCTION: pt_pip2_cmd_rsp_show
  12304. *
  12305. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12306. * response generated after using the store method of the pip2_cmd_rsp
  12307. * sysfs node, are available to be read here.
  12308. *
  12309. * PARAMETERS:
  12310. * *dev - pointer to Device structure
  12311. * *attr - pointer to the device attribute structure
  12312. * *buf - pointer to buffer to print
  12313. ******************************************************************************/
  12314. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12315. struct device_attribute *attr, char *buf)
  12316. {
  12317. struct pt_core_data *cd = dev_get_drvdata(dev);
  12318. int i;
  12319. ssize_t data_len;
  12320. int index;
  12321. mutex_lock(&cd->sysfs_lock);
  12322. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12323. "Status: %d\n", cd->raw_cmd_status);
  12324. if (cd->raw_cmd_status)
  12325. goto error;
  12326. /* Remove the CRC from the length of the response */
  12327. data_len = cd->cmd_rsp_buf_len - 2;
  12328. /* Start printing from the data payload */
  12329. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12330. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12331. "%02X ", cd->cmd_rsp_buf[i]);
  12332. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12333. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12334. "\n(%zd bytes)\n",
  12335. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12336. } else {
  12337. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12338. "\n(%zd bytes)\n", 0);
  12339. }
  12340. error:
  12341. mutex_unlock(&cd->sysfs_lock);
  12342. return index;
  12343. }
  12344. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12345. pt_pip2_cmd_rsp_store);
  12346. /*******************************************************************************
  12347. * FUNCTION: pt_command_store
  12348. *
  12349. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12350. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12351. * TTDL will not parse the command.
  12352. *
  12353. * RETURN: Size of passed in buffer
  12354. *
  12355. * PARAMETERS:
  12356. * *dev - pointer to device structure
  12357. * *attr - pointer to device attributes
  12358. * *buf - pointer to buffer that hold the command parameters
  12359. * size - size of buf
  12360. ******************************************************************************/
  12361. static ssize_t pt_command_store(struct device *dev,
  12362. struct device_attribute *attr, const char *buf, size_t size)
  12363. {
  12364. struct pt_core_data *cd = dev_get_drvdata(dev);
  12365. unsigned short crc;
  12366. u16 actual_read_len;
  12367. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12368. int length;
  12369. int len_field;
  12370. int rc = 0;
  12371. mutex_lock(&cd->sysfs_lock);
  12372. cd->cmd_rsp_buf_len = 0;
  12373. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12374. mutex_unlock(&cd->sysfs_lock);
  12375. length = _pt_ic_parse_input_hex(dev, buf, size,
  12376. input_data, PT_MAX_PIP2_MSG_SIZE);
  12377. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12378. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12379. __func__);
  12380. rc = -EINVAL;
  12381. goto pt_command_store_exit;
  12382. }
  12383. /* PIP2 messages begin with 01 01 */
  12384. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12385. cd->pip2_prot_active = 1;
  12386. /* Override next seq tag with what was sent */
  12387. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12388. /* For PIP2 cmd if length does not include crc, add it */
  12389. len_field = (input_data[3] << 8) | input_data[2];
  12390. if (len_field == length && length <= 254) {
  12391. crc = crc_ccitt_calculate(&input_data[2],
  12392. length - 2);
  12393. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12394. __func__, length, crc);
  12395. input_data[length] = (crc & 0xFF00) >> 8;
  12396. input_data[length + 1] = crc & 0x00FF;
  12397. length = length + 2;
  12398. }
  12399. }
  12400. /* write PIP command to log */
  12401. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12402. pm_runtime_get_sync(dev);
  12403. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12404. length, input_data, &actual_read_len);
  12405. pm_runtime_put(dev);
  12406. mutex_lock(&cd->sysfs_lock);
  12407. cd->raw_cmd_status = rc;
  12408. if (rc) {
  12409. cd->cmd_rsp_buf_len = 0;
  12410. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12411. __func__, buf);
  12412. } else {
  12413. cd->cmd_rsp_buf_len = actual_read_len;
  12414. }
  12415. cd->pip2_prot_active = 0;
  12416. mutex_unlock(&cd->sysfs_lock);
  12417. pt_command_store_exit:
  12418. if (rc)
  12419. return rc;
  12420. return size;
  12421. }
  12422. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12423. /*******************************************************************************
  12424. * FUNCTION: pt_response_show
  12425. *
  12426. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12427. * response generated after using the pt_command_store sysfs node, are
  12428. * available to be read here.
  12429. *
  12430. * PARAMETERS:
  12431. * *dev - pointer to Device structure
  12432. * *attr - pointer to the device attribute structure
  12433. * *buf - pointer to buffer to print
  12434. ******************************************************************************/
  12435. static ssize_t pt_response_show(struct device *dev,
  12436. struct device_attribute *attr, char *buf)
  12437. {
  12438. struct pt_core_data *cd = dev_get_drvdata(dev);
  12439. int i;
  12440. ssize_t num_read;
  12441. int index;
  12442. mutex_lock(&cd->sysfs_lock);
  12443. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12444. "Status: %d\n", cd->raw_cmd_status);
  12445. if (cd->raw_cmd_status)
  12446. goto error;
  12447. num_read = cd->cmd_rsp_buf_len;
  12448. for (i = 0; i < num_read; i++)
  12449. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12450. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12451. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12452. "(%zd bytes)\n", num_read);
  12453. error:
  12454. mutex_unlock(&cd->sysfs_lock);
  12455. return index;
  12456. }
  12457. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12458. /*******************************************************************************
  12459. * FUNCTION: pt_dut_debug_show
  12460. *
  12461. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12462. * are available for the store method.
  12463. *
  12464. * RETURN:
  12465. * 0 = success
  12466. * !0 = failure
  12467. *
  12468. * PARAMETERS:
  12469. * *dev - pointer to device structure
  12470. * *attr - pointer to device attributes
  12471. * *buf - pointer to output buffer
  12472. ******************************************************************************/
  12473. static ssize_t pt_dut_debug_show(struct device *dev,
  12474. struct device_attribute *attr, char *buf)
  12475. {
  12476. ssize_t ret;
  12477. ret = scnprintf(buf, strlen(buf),
  12478. "Status: 0\n"
  12479. "dut_debug sends the following commands to the DUT:\n"
  12480. "%d %s \t- %s\n"
  12481. "%d %s \t- %s\n"
  12482. "%d %s \t- %s\n"
  12483. "%d %s \t- %s\n"
  12484. "%d %s \t- %s\n"
  12485. "%d %s \t- %s\n"
  12486. "%d %s \t- %s\n"
  12487. "%d %s \t- %s\n"
  12488. "%d %s \t- %s\n"
  12489. "%d %s \t- %s\n"
  12490. "%d %s \t- %s\n"
  12491. "%d %s \t- %s\n"
  12492. "%d %s \t- %s\n"
  12493. "%d %s \t- %s\n"
  12494. "%d %s \t- %s\n"
  12495. "%d %s \t- %s\n"
  12496. "%d %s \t- %s\n"
  12497. ,
  12498. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12499. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12500. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12501. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12502. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12503. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12504. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12505. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12506. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12507. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12508. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12509. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12510. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12511. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12512. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12513. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12514. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12515. );
  12516. return ret;
  12517. }
  12518. /*******************************************************************************
  12519. * FUNCTION: pt_drv_debug_show
  12520. *
  12521. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12522. * are available for the store method.
  12523. *
  12524. * RETURN:
  12525. * 0 = success
  12526. * !0 = failure
  12527. *
  12528. * PARAMETERS:
  12529. * *dev - pointer to device structure
  12530. * *attr - pointer to device attributes
  12531. * *buf - pointer to output buffer
  12532. ******************************************************************************/
  12533. static ssize_t pt_drv_debug_show(struct device *dev,
  12534. struct device_attribute *attr, char *buf)
  12535. {
  12536. ssize_t ret;
  12537. ret = scnprintf(buf, strlen(buf),
  12538. "Status: 0\n"
  12539. "drv_debug supports the following values:\n"
  12540. "%d %s \t- %s\n"
  12541. "%d %s \t- %s\n"
  12542. "%d %s \t- %s\n"
  12543. "%d %s \t- %s\n"
  12544. "%d %s \t- %s\n"
  12545. "%d %s \t- %s\n"
  12546. "%d %s \t- %s\n"
  12547. "%d %s \t- %s\n"
  12548. "%d %s \t- %s\n"
  12549. "%d %s - %s\n"
  12550. "%d %s \t- %s\n"
  12551. "%d %s \t- %s\n"
  12552. #ifdef TTDL_DIAGNOSTICS
  12553. "%d %s \t- %s\n"
  12554. "%d %s \t- %s\n"
  12555. "%d %s \t- %s\n"
  12556. #endif /* TTDL_DIAGNOSTICS */
  12557. "%d %s \t- %s\n"
  12558. #ifdef TTDL_DIAGNOSTICS
  12559. "%d %s \t- %s\n"
  12560. "%d %s \t- %s\n"
  12561. "%d %s \t- %s\n"
  12562. "%d %s \t- %s\n"
  12563. "%d %s \t- %s\n"
  12564. "%d %s \t- %s\n"
  12565. "%d %s \t- %s\n"
  12566. "%d %s \t- %s\n"
  12567. "%d %s \t- %s\n"
  12568. #endif /* TTDL_DIAGNOSTICS */
  12569. ,
  12570. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12571. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12572. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12573. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12574. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12575. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12576. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12577. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12578. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12579. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12580. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12581. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12582. #ifdef TTDL_DIAGNOSTICS
  12583. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12584. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12585. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12586. #endif /* TTDL_DIAGNOSTICS */
  12587. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12588. #ifdef TTDL_DIAGNOSTICS
  12589. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12590. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12591. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12592. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12593. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12594. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12595. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12596. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12597. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12598. "TTHE_TUNER HID USB Format"
  12599. #endif /* TTDL_DIAGNOSTICS */
  12600. );
  12601. return ret;
  12602. }
  12603. /*******************************************************************************
  12604. * FUNCTION: pt_drv_debug_store
  12605. *
  12606. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12607. * dut_debug. Drv_debug will contain all functionality that can be run
  12608. * without a DUT preset and is available anytime TTDL is running.
  12609. * Dut_debug requires a DUT to be available and will only be created after
  12610. * a DUT has been detected.
  12611. * This function will eventually be split into two but until the overlap
  12612. * has been depricated this function contains all commands that can be
  12613. * used for TTDL/DUT debugging status and control.
  12614. * All commands require at least one value to be passed in *buf with some
  12615. * requiring two.
  12616. *
  12617. * RETURN: Size of passed in buffer
  12618. *
  12619. * PARAMETERS:
  12620. * *dev - pointer to device structure
  12621. * *attr - pointer to device attributes
  12622. * *buf - pointer to buffer that hold the command parameters
  12623. * size - size of buf
  12624. ******************************************************************************/
  12625. static ssize_t pt_drv_debug_store(struct device *dev,
  12626. struct device_attribute *attr, const char *buf, size_t size)
  12627. {
  12628. struct pt_core_data *cd = dev_get_drvdata(dev);
  12629. unsigned long value;
  12630. int rc = 0;
  12631. u8 return_data[8];
  12632. static u8 wd_disabled;
  12633. u32 input_data[3];
  12634. int length;
  12635. #ifdef TTDL_DIAGNOSTICS
  12636. struct i2c_client *client = to_i2c_client(dev);
  12637. unsigned short crc = 0;
  12638. u16 cal_size;
  12639. #endif
  12640. input_data[0] = 0;
  12641. input_data[1] = 0;
  12642. /* Maximmum input is two elements */
  12643. length = _pt_ic_parse_input(dev, buf, size,
  12644. input_data, ARRAY_SIZE(input_data));
  12645. if (length < 1 || length > 2) {
  12646. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12647. __func__);
  12648. rc = -EINVAL;
  12649. goto pt_drv_debug_store_exit;
  12650. }
  12651. value = input_data[0];
  12652. if (length == 1) {
  12653. pt_debug(dev, DL_DEBUG,
  12654. "%s: Debug Cmd Received (id=%d)\n",
  12655. __func__, input_data[0]);
  12656. } else if (length == 2) {
  12657. pt_debug(dev, DL_DEBUG,
  12658. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12659. __func__, input_data[0], input_data[1]);
  12660. } else {
  12661. pt_debug(dev, DL_DEBUG,
  12662. "%s: Invalid arguments received\n", __func__);
  12663. rc = -EINVAL;
  12664. goto pt_drv_debug_store_exit;
  12665. }
  12666. /* Start watchdog timer command */
  12667. if (value == PT_DRV_DBG_START_WD) {
  12668. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12669. wd_disabled = 0;
  12670. cd->watchdog_force_stop = false;
  12671. pt_start_wd_timer(cd);
  12672. goto pt_drv_debug_store_exit;
  12673. }
  12674. /* Stop watchdog timer temporarily */
  12675. pt_stop_wd_timer(cd);
  12676. if (value == PT_DRV_DBG_STOP_WD) {
  12677. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12678. wd_disabled = 1;
  12679. cd->watchdog_force_stop = true;
  12680. goto pt_drv_debug_store_exit;
  12681. }
  12682. switch (value) {
  12683. case PT_DRV_DBG_SUSPEND: /* 4 */
  12684. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12685. wd_disabled = 1;
  12686. rc = pt_core_suspend_(cd->dev);
  12687. if (rc)
  12688. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12689. __func__, rc);
  12690. else {
  12691. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12692. __func__);
  12693. cd->drv_debug_suspend = true;
  12694. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12695. }
  12696. break;
  12697. case PT_DRV_DBG_RESUME: /* 5 */
  12698. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12699. rc = pt_core_resume_(cd->dev);
  12700. if (rc)
  12701. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12702. __func__, rc);
  12703. else {
  12704. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12705. __func__);
  12706. cd->drv_debug_suspend = false;
  12707. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12708. }
  12709. break;
  12710. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12711. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12712. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12713. break;
  12714. case PT_DUT_DBG_HID_RESET: /* 50 */
  12715. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12716. pt_hid_cmd_reset(cd);
  12717. break;
  12718. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12719. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12720. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12721. wd_disabled = 0;
  12722. break;
  12723. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12724. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12725. __func__);
  12726. wd_disabled = 1;
  12727. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12728. break;
  12729. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12730. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12731. __func__);
  12732. wd_disabled = 1;
  12733. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12734. break;
  12735. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12736. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12737. pt_hid_output_bl_get_information(cd, return_data);
  12738. break;
  12739. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12740. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12741. __func__);
  12742. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12743. break;
  12744. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12745. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12746. pt_hid_output_bl_launch_app(cd);
  12747. break;
  12748. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12749. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12750. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12751. break;
  12752. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12753. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12754. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12755. break;
  12756. case PT_DUT_DBG_RESET: /* 98 */
  12757. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12758. rc = pt_hw_hard_reset(cd);
  12759. break;
  12760. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12761. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12762. pt_pip_null(cd);
  12763. break;
  12764. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12765. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12766. rc = pt_pip_start_bootloader(cd);
  12767. if (!rc) {
  12768. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12769. cd->mode = PT_MODE_BOOTLOADER;
  12770. }
  12771. break;
  12772. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12773. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12774. pt_hid_output_get_sysinfo(cd);
  12775. break;
  12776. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12777. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12778. pt_pip_suspend_scanning(cd);
  12779. break;
  12780. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12781. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12782. pt_pip_resume_scanning(cd);
  12783. break;
  12784. #ifdef TTHE_TUNER_SUPPORT
  12785. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12786. cd->tthe_exit = 1;
  12787. wake_up(&cd->wait_q);
  12788. kfree(cd->tthe_buf);
  12789. cd->tthe_buf = NULL;
  12790. cd->tthe_exit = 0;
  12791. break;
  12792. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12793. if (cd->tthe_buf)
  12794. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12795. else
  12796. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12797. __func__);
  12798. break;
  12799. #endif
  12800. #ifdef TTDL_DIAGNOSTICS
  12801. case PT_DUT_DBG_HID_DESC: /* 109 */
  12802. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12803. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12804. break;
  12805. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12806. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12807. __func__);
  12808. pt_erase_parameter_list(cd);
  12809. break;
  12810. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12811. rc = pt_read_input(cd);
  12812. if (!rc)
  12813. pt_parse_input(cd);
  12814. break;
  12815. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12816. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12817. &cal_size, &crc);
  12818. if (rc)
  12819. pt_debug(dev, DL_ERROR,
  12820. "%s: CAL Data clear failed rc=%d\n",
  12821. __func__, rc);
  12822. else
  12823. pt_debug(dev, DL_INFO,
  12824. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12825. __func__, crc, size);
  12826. break;
  12827. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12828. mutex_lock(&cd->system_lock);
  12829. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12830. cd->debug_level = input_data[1];
  12831. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12832. __func__, cd->debug_level);
  12833. } else {
  12834. rc = -EINVAL;
  12835. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12836. __func__, input_data[1]);
  12837. }
  12838. mutex_unlock(&(cd->system_lock));
  12839. break;
  12840. #endif
  12841. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12842. mutex_lock(&cd->system_lock);
  12843. if (input_data[1] > 100) {
  12844. cd->watchdog_interval = input_data[1];
  12845. pt_debug(dev, DL_INFO,
  12846. "%s: Set watchdog_ interval to: %d\n",
  12847. __func__, cd->watchdog_interval);
  12848. pt_start_wd_timer(cd);
  12849. } else {
  12850. rc = -EINVAL;
  12851. pt_debug(dev, DL_ERROR,
  12852. "%s: Invalid watchdog interval: %d\n",
  12853. __func__, input_data[1]);
  12854. }
  12855. mutex_unlock(&(cd->system_lock));
  12856. break;
  12857. #ifdef TTDL_DIAGNOSTICS
  12858. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12859. mutex_lock(&cd->system_lock);
  12860. if (input_data[1] == 0) {
  12861. cd->show_timestamp = 0;
  12862. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12863. __func__);
  12864. } else if (input_data[1] == 1) {
  12865. cd->show_timestamp = 1;
  12866. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12867. __func__);
  12868. } else {
  12869. rc = -EINVAL;
  12870. pt_debug(dev, DL_ERROR,
  12871. "%s: Invalid parameter: %d\n",
  12872. __func__, input_data[1]);
  12873. }
  12874. mutex_unlock(&(cd->system_lock));
  12875. break;
  12876. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12877. if (input_data[1] == 0) {
  12878. cd->cpdata->setup_power(cd->cpdata,
  12879. PT_MT_POWER_OFF, cd->dev);
  12880. pt_debug(dev, DL_INFO,
  12881. "%s: Initiate Power Off\n", __func__);
  12882. } else if (input_data[1] == 1) {
  12883. cd->cpdata->setup_power(cd->cpdata,
  12884. PT_MT_POWER_ON, cd->dev);
  12885. pt_debug(dev, DL_INFO,
  12886. "%s: Initiate Power On\n", __func__);
  12887. } else {
  12888. rc = -EINVAL;
  12889. pt_debug(dev, DL_ERROR,
  12890. "%s: Invalid parameter: %d\n",
  12891. __func__, input_data[1]);
  12892. }
  12893. break;
  12894. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12895. if (input_data[1] == 0) {
  12896. pm_runtime_put(dev);
  12897. pt_debug(dev, DL_ERROR,
  12898. "%s: Force call pm_runtime_put()\n", __func__);
  12899. } else if (input_data[1] == 1) {
  12900. pm_runtime_get_sync(dev);
  12901. pt_debug(dev, DL_ERROR,
  12902. "%s: Force call pm_runtime_get_sync()\n",
  12903. __func__);
  12904. } else {
  12905. rc = -EINVAL;
  12906. pt_debug(dev, DL_ERROR,
  12907. "%s: WARNING: Invalid parameter: %d\n",
  12908. __func__, input_data[1]);
  12909. }
  12910. break;
  12911. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12912. mutex_lock(&cd->system_lock);
  12913. if (input_data[1] == 0) {
  12914. cd->show_tt_data = false;
  12915. pt_debug(dev, DL_INFO,
  12916. "%s: Disable TT_DATA\n", __func__);
  12917. } else if (input_data[1] == 1) {
  12918. cd->show_tt_data = true;
  12919. pt_debug(dev, DL_INFO,
  12920. "%s: Enable TT_DATA\n", __func__);
  12921. } else {
  12922. rc = -EINVAL;
  12923. pt_debug(dev, DL_ERROR,
  12924. "%s: Invalid parameter: %d\n",
  12925. __func__, input_data[1]);
  12926. }
  12927. mutex_unlock(&(cd->system_lock));
  12928. break;
  12929. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12930. if (input_data[1] == cd->active_dut_generation) {
  12931. mutex_lock(&cd->system_lock);
  12932. cd->set_dut_generation = true;
  12933. mutex_unlock(&(cd->system_lock));
  12934. } else {
  12935. mutex_lock(&cd->system_lock);
  12936. if (input_data[1] == 0) {
  12937. cd->active_dut_generation = DUT_UNKNOWN;
  12938. cd->set_dut_generation = false;
  12939. } else if (input_data[1] == 1) {
  12940. cd->active_dut_generation = DUT_PIP1_ONLY;
  12941. cd->set_dut_generation = true;
  12942. } else if (input_data[1] == 2) {
  12943. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12944. cd->set_dut_generation = true;
  12945. } else {
  12946. rc = -EINVAL;
  12947. pt_debug(dev, DL_ERROR,
  12948. "%s: Invalid parameter: %d\n",
  12949. __func__, input_data[1]);
  12950. break;
  12951. }
  12952. cd->startup_status = STARTUP_STATUS_START;
  12953. pt_debug(cd->dev, DL_DEBUG,
  12954. "%s: Startup Status Reset\n", __func__);
  12955. mutex_unlock(&(cd->system_lock));
  12956. pt_debug(dev, DL_INFO,
  12957. "%s: Active DUT Generation Set to: %d\n",
  12958. __func__, cd->active_dut_generation);
  12959. /* Changing DUT generations full restart needed */
  12960. pt_queue_restart(cd);
  12961. }
  12962. break;
  12963. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12964. mutex_lock(&cd->system_lock);
  12965. if (input_data[1] == 0) {
  12966. cd->bridge_mode = false;
  12967. pt_debug(dev, DL_INFO,
  12968. "%s: Disable Bridge Mode\n", __func__);
  12969. } else if (input_data[1] == 1) {
  12970. cd->bridge_mode = true;
  12971. pt_debug(dev, DL_INFO,
  12972. "%s: Enable Bridge Mode\n", __func__);
  12973. } else {
  12974. rc = -EINVAL;
  12975. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12976. __func__, input_data[1]);
  12977. }
  12978. mutex_unlock(&(cd->system_lock));
  12979. break;
  12980. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12981. mutex_lock(&cd->system_lock);
  12982. /* Only a 7bit address is valid */
  12983. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12984. client->addr = input_data[1];
  12985. pt_debug(dev, DL_INFO,
  12986. "%s: Set I2C Address: 0x%2X\n",
  12987. __func__, client->addr);
  12988. } else {
  12989. rc = -EINVAL;
  12990. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12991. __func__, input_data[1]);
  12992. client->addr = 0x24;
  12993. }
  12994. mutex_unlock(&(cd->system_lock));
  12995. break;
  12996. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12997. mutex_lock(&cd->system_lock);
  12998. if (input_data[1] == 0) {
  12999. cd->flashless_dut = 0;
  13000. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13001. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13002. __func__);
  13003. } else if (input_data[1] == 1) {
  13004. cd->flashless_dut = 1;
  13005. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13006. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13007. __func__);
  13008. }
  13009. mutex_unlock(&(cd->system_lock));
  13010. break;
  13011. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13012. mutex_lock(&cd->system_lock);
  13013. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13014. cd->force_pip2_seq = input_data[1];
  13015. cd->pip2_cmd_tag_seq = input_data[1];
  13016. pt_debug(dev, DL_INFO,
  13017. "%s: Force PIP2 Seq to: 0x%02X\n",
  13018. __func__, input_data[1]);
  13019. } else {
  13020. cd->force_pip2_seq = 0;
  13021. pt_debug(dev, DL_INFO,
  13022. "%s: Clear Forced PIP2 Seq\n", __func__);
  13023. }
  13024. mutex_unlock(&(cd->system_lock));
  13025. break;
  13026. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13027. mutex_lock(&cd->system_lock);
  13028. if (input_data[1] == 0) {
  13029. cd->bl_with_no_int = 0;
  13030. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13031. } else if (input_data[1] == 1) {
  13032. cd->bl_with_no_int = 1;
  13033. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13034. __func__);
  13035. } else {
  13036. rc = -EINVAL;
  13037. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13038. __func__, input_data[1]);
  13039. }
  13040. mutex_unlock(&(cd->system_lock));
  13041. break;
  13042. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13043. mutex_lock(&cd->system_lock);
  13044. if (input_data[1] == 0) {
  13045. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13046. pt_debug(dev, DL_INFO,
  13047. "%s: Disable Calibration cache in host\n",
  13048. __func__);
  13049. } else if (input_data[1] == 1) {
  13050. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13051. pt_debug(dev, DL_INFO,
  13052. "%s: Enable Calibration cache in host\n",
  13053. __func__);
  13054. } else {
  13055. rc = -EINVAL;
  13056. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13057. __func__, input_data[1]);
  13058. }
  13059. mutex_unlock(&(cd->system_lock));
  13060. break;
  13061. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13062. mutex_lock(&cd->system_lock);
  13063. if (input_data[1] == 0) {
  13064. cd->multi_chip = PT_FEATURE_DISABLE;
  13065. cd->ttdl_bist_select = 0x07;
  13066. pt_debug(dev, DL_INFO,
  13067. "%s: Disable Multi-chip support\n", __func__);
  13068. } else if (input_data[1] == 1) {
  13069. cd->multi_chip = PT_FEATURE_ENABLE;
  13070. cd->ttdl_bist_select = 0x3F;
  13071. pt_debug(dev, DL_INFO,
  13072. "%s: Enable Multi-chip support\n", __func__);
  13073. } else {
  13074. rc = -EINVAL;
  13075. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13076. __func__, input_data[1]);
  13077. }
  13078. mutex_unlock(&(cd->system_lock));
  13079. break;
  13080. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13081. mutex_lock(&cd->system_lock);
  13082. if (input_data[1] <= 0x07) {
  13083. cd->panel_id_support = input_data[1];
  13084. pt_debug(dev, DL_INFO,
  13085. "%s: Set panel_id_support to %d\n",
  13086. __func__, cd->panel_id_support);
  13087. } else {
  13088. rc = -EINVAL;
  13089. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13090. __func__, input_data[1]);
  13091. }
  13092. mutex_unlock(&(cd->system_lock));
  13093. break;
  13094. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13095. mutex_lock(&cd->system_lock);
  13096. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13097. /*
  13098. * The timeout is changed for some cases so the
  13099. * pip_cmd_timeout_default is used to retore back to
  13100. * what the user requested as the new timeout.
  13101. */
  13102. cd->pip_cmd_timeout_default = input_data[1];
  13103. cd->pip_cmd_timeout = input_data[1];
  13104. pt_debug(dev, DL_INFO,
  13105. "%s: PIP Timeout = %d\n", __func__,
  13106. cd->pip_cmd_timeout_default);
  13107. } else {
  13108. rc = -EINVAL;
  13109. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13110. __func__, input_data[1]);
  13111. }
  13112. mutex_unlock(&(cd->system_lock));
  13113. break;
  13114. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13115. mutex_lock(&cd->system_lock);
  13116. if (input_data[1] == 0) {
  13117. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13118. pt_debug(dev, DL_INFO,
  13119. "%s: Disable tthe_tuner HID-USB format\n",
  13120. __func__);
  13121. } else if (input_data[1] == 1) {
  13122. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13123. pt_debug(dev, DL_INFO,
  13124. "%s: Enable tthe_tuner HID-USB format\n",
  13125. __func__);
  13126. } else {
  13127. rc = -EINVAL;
  13128. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13129. __func__, input_data[1]);
  13130. }
  13131. mutex_unlock(&(cd->system_lock));
  13132. break;
  13133. #endif /* TTDL_DIAGNOSTICS */
  13134. default:
  13135. rc = -EINVAL;
  13136. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13137. }
  13138. /* Enable watchdog timer */
  13139. if (!wd_disabled)
  13140. pt_start_wd_timer(cd);
  13141. pt_drv_debug_store_exit:
  13142. if (rc)
  13143. return rc;
  13144. return size;
  13145. }
  13146. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13147. pt_drv_debug_store);
  13148. /*******************************************************************************
  13149. * FUNCTION: pt_sleep_status_show
  13150. *
  13151. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13152. * sleep status as either ON or OFF
  13153. *
  13154. * RETURN:
  13155. * 0 = success
  13156. * !0 = failure
  13157. *
  13158. * PARAMETERS:
  13159. * *dev - pointer to device structure
  13160. * *attr - pointer to device attributes
  13161. * *buf - pointer to output buffer
  13162. ******************************************************************************/
  13163. static ssize_t pt_sleep_status_show(struct device *dev,
  13164. struct device_attribute *attr, char *buf)
  13165. {
  13166. struct pt_core_data *cd = dev_get_drvdata(dev);
  13167. ssize_t ret;
  13168. mutex_lock(&cd->system_lock);
  13169. if (cd->sleep_state == SS_SLEEP_ON)
  13170. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13171. else
  13172. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13173. mutex_unlock(&cd->system_lock);
  13174. return ret;
  13175. }
  13176. /*******************************************************************************
  13177. * FUNCTION: pt_panel_id_show
  13178. *
  13179. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13180. * detected panel ID from the DUT
  13181. *
  13182. * RETURN:
  13183. * 0 = success
  13184. * !0 = failure
  13185. *
  13186. * PARAMETERS:
  13187. * *dev - pointer to device structure
  13188. * *attr - pointer to device attributes
  13189. * *buf - pointer to output buffer
  13190. ******************************************************************************/
  13191. static ssize_t pt_panel_id_show(struct device *dev,
  13192. struct device_attribute *attr, char *buf)
  13193. {
  13194. struct pt_core_data *cd = dev_get_drvdata(dev);
  13195. ssize_t ret;
  13196. u8 pid = PANEL_ID_NOT_ENABLED;
  13197. int rc = 0;
  13198. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13199. /*
  13200. * The DUT should report the same panel ID from both the BL and
  13201. * the FW unless the panel_id feature is set to only
  13202. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13203. * to retrieve the panel_id.
  13204. */
  13205. if (cd->mode == PT_MODE_BOOTLOADER) {
  13206. /*
  13207. * Return the stored value if PT_PANEL_ID_BY_BL
  13208. * is not supported while other feature exits.
  13209. */
  13210. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13211. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13212. if (rc) {
  13213. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13214. "Failed to retrieve Panel ID. ",
  13215. "Using cached value\n",
  13216. __func__);
  13217. }
  13218. }
  13219. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13220. if (cd->panel_id_support &
  13221. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13222. /* For all systems sysinfo has the panel_id */
  13223. rc = pt_hid_output_get_sysinfo(cd);
  13224. if (!rc)
  13225. pid =
  13226. cd->sysinfo.sensing_conf_data.panel_id;
  13227. pt_debug(dev, DL_ERROR,
  13228. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13229. __func__, rc, pid);
  13230. }
  13231. } else {
  13232. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13233. __func__);
  13234. rc = -EPERM;
  13235. }
  13236. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13237. if (cd->mode == PT_MODE_BOOTLOADER) {
  13238. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13239. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13240. if (rc) {
  13241. pt_debug(dev, DL_ERROR,
  13242. "%s: BL get panel ID failed rc=%d\n",
  13243. __func__, rc);
  13244. }
  13245. }
  13246. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13247. if (cd->panel_id_support &
  13248. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13249. rc = pt_hid_output_get_sysinfo(cd);
  13250. if (!rc)
  13251. pid =
  13252. cd->sysinfo.sensing_conf_data.panel_id;
  13253. pt_debug(dev, DL_ERROR,
  13254. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13255. __func__, rc, pid);
  13256. }
  13257. } else {
  13258. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13259. __func__);
  13260. rc = -EPERM;
  13261. }
  13262. } else {
  13263. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13264. __func__);
  13265. rc = -EPERM;
  13266. }
  13267. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13268. rc, pid);
  13269. return ret;
  13270. }
  13271. /*******************************************************************************
  13272. * FUNCTION: pt_get_param_store
  13273. *
  13274. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13275. * ID to retrieve with the show method.
  13276. *
  13277. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13278. *
  13279. * RETURN: Size of passed in buffer if successful
  13280. *
  13281. * PARAMETERS:
  13282. * *dev - pointer to device structure
  13283. * *attr - pointer to device attributes
  13284. * *buf - pointer to output buffer
  13285. * size - size of buf
  13286. ******************************************************************************/
  13287. static ssize_t pt_get_param_store(struct device *dev,
  13288. struct device_attribute *attr, const char *buf, size_t size)
  13289. {
  13290. struct pt_core_data *cd = dev_get_drvdata(dev);
  13291. u32 input_data[2];
  13292. int length;
  13293. int rc = 0;
  13294. /* Maximum input of one value */
  13295. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13296. ARRAY_SIZE(input_data));
  13297. if (length != 1) {
  13298. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13299. __func__);
  13300. rc = -EINVAL;
  13301. goto exit;
  13302. }
  13303. mutex_lock(&cd->system_lock);
  13304. cd->get_param_id = input_data[0];
  13305. mutex_unlock(&(cd->system_lock));
  13306. exit:
  13307. if (rc)
  13308. return rc;
  13309. return size;
  13310. }
  13311. /*******************************************************************************
  13312. * FUNCTION: pt_get_param_show
  13313. *
  13314. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13315. * parameter data from the DUT based on the ID stored in the core
  13316. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13317. * communicate or some other error occures, an error status is returned
  13318. * with no value following.
  13319. * Output is in the form:
  13320. * Status: x
  13321. * 0xyyyyyyyy
  13322. * The 32bit data will only follow the status code if the status == 0
  13323. *
  13324. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13325. *
  13326. * RETURN:
  13327. * 0 = success
  13328. * !0 = failure
  13329. *
  13330. * PARAMETERS:
  13331. * *dev - pointer to device structure
  13332. * *attr - pointer to device attributes
  13333. * *buf - pointer to output buffer
  13334. ******************************************************************************/
  13335. static ssize_t pt_get_param_show(struct device *dev,
  13336. struct device_attribute *attr, char *buf)
  13337. {
  13338. struct pt_core_data *cd = dev_get_drvdata(dev);
  13339. ssize_t ret = 0;
  13340. int status;
  13341. u32 value = 0;
  13342. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13343. if (status) {
  13344. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13345. __func__, "pt_get_param", status);
  13346. ret = scnprintf(buf, strlen(buf),
  13347. "%s %d\n",
  13348. "Status:", status);
  13349. } else {
  13350. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13351. __func__, cd->get_param_id, value);
  13352. ret = scnprintf(buf, strlen(buf),
  13353. "Status: %d\n"
  13354. "0x%04X\n",
  13355. status, value);
  13356. }
  13357. return ret;
  13358. }
  13359. /*******************************************************************************
  13360. * FUNCTION: pt_ttdl_restart_show
  13361. *
  13362. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13363. * probed modules, calls startup() and then re-probes modules.
  13364. *
  13365. * RETURN: size of data written to sysfs node
  13366. *
  13367. * PARAMETERS:
  13368. * *dev - pointer to device structure
  13369. * *attr - pointer to device attributes structure
  13370. * *buf - pointer to print output buffer
  13371. ******************************************************************************/
  13372. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13373. struct device_attribute *attr, char *buf)
  13374. {
  13375. struct pt_core_data *cd = dev_get_drvdata(dev);
  13376. int t;
  13377. int rc = 0;
  13378. mutex_lock(&cd->system_lock);
  13379. cd->startup_state = STARTUP_NONE;
  13380. mutex_unlock(&(cd->system_lock));
  13381. /* ensure no left over exclusive access is still locked */
  13382. release_exclusive(cd, cd->dev);
  13383. pt_queue_restart(cd);
  13384. t = wait_event_timeout(cd->wait_q,
  13385. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13386. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13387. if (IS_TMO(t)) {
  13388. pt_debug(cd->dev, DL_ERROR,
  13389. "%s: TMO waiting for FW sentinel\n", __func__);
  13390. rc = -ETIME;
  13391. }
  13392. return scnprintf(buf, strlen(buf),
  13393. "Status: %d\n"
  13394. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13395. }
  13396. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13397. /*******************************************************************************
  13398. * FUNCTION: pt_pip2_gpio_read_show
  13399. *
  13400. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13401. * contents of the response to the passed in output buffer.
  13402. *
  13403. * RETURN: size of data written to sysfs node
  13404. *
  13405. * PARAMETERS:
  13406. * *dev - pointer to device structure
  13407. * *attr - pointer to device attributes structure
  13408. * *buf - pointer to print output buffer
  13409. ******************************************************************************/
  13410. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13411. struct device_attribute *attr, char *buf)
  13412. {
  13413. struct pt_core_data *cd = dev_get_drvdata(dev);
  13414. u8 status = 0;
  13415. u32 gpio_value = 0;
  13416. int rc = 0;
  13417. /* This functionality is only available in the BL */
  13418. if (cd->mode == PT_MODE_BOOTLOADER)
  13419. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13420. else
  13421. rc = -EPERM;
  13422. if (!rc) {
  13423. if (status == 0)
  13424. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13425. "Status: %d\n"
  13426. "DUT GPIO Reg: 0x%08X\n",
  13427. rc, gpio_value);
  13428. else
  13429. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13430. "Status: %d\n"
  13431. "DUT GPIO Reg: n/a\n",
  13432. status);
  13433. } else
  13434. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13435. "Status: %d\n"
  13436. "DUT GPIO Reg: n/a\n",
  13437. rc);
  13438. }
  13439. /*******************************************************************************
  13440. * FUNCTION: pt_pip2_version_show
  13441. *
  13442. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13443. * contents of the response to the passed in output buffer.
  13444. *
  13445. * RETURN: size of data written to sysfs node
  13446. *
  13447. * PARAMETERS:
  13448. * *dev - pointer to device structure
  13449. * *attr - pointer to device attributes structure
  13450. * *buf - pointer to print output buffer
  13451. ******************************************************************************/
  13452. static ssize_t pt_pip2_version_show(struct device *dev,
  13453. struct device_attribute *attr, char *buf)
  13454. {
  13455. int rc = 0;
  13456. struct pt_core_data *cd = dev_get_drvdata(dev);
  13457. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13458. rc = pt_pip2_get_version(cd);
  13459. if (!rc) {
  13460. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13461. "PIP VERSION : %02X.%02X\n"
  13462. "BL VERSION : %02X.%02X\n"
  13463. "FW VERSION : %02X.%02X\n"
  13464. "SILICON ID : %04X.%04X\n"
  13465. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13466. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13467. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13468. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13469. ttdata->chip_id, ttdata->chip_rev,
  13470. ttdata->uid[0], ttdata->uid[1],
  13471. ttdata->uid[2], ttdata->uid[3],
  13472. ttdata->uid[4], ttdata->uid[5],
  13473. ttdata->uid[6], ttdata->uid[7],
  13474. ttdata->uid[8], ttdata->uid[9],
  13475. ttdata->uid[10], ttdata->uid[11]);
  13476. } else {
  13477. pt_debug(dev, DL_ERROR,
  13478. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13479. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13480. "PIP VERSION : -\n"
  13481. "BL VERSION : -\n"
  13482. "FW VERSION : -\n"
  13483. "SILICON ID : -\n"
  13484. "UID : -\n");
  13485. }
  13486. }
  13487. #ifdef TTDL_DIAGNOSTICS
  13488. /*******************************************************************************
  13489. * FUNCTION: pt_ttdl_status_show
  13490. *
  13491. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13492. * variable states and GPIO levels. Additional information printed when
  13493. * TTDL_DIAGNOSTICS is enabled.
  13494. *
  13495. * NOTE: All counters will be reset to 0 when this function is called.
  13496. *
  13497. * RETURN:
  13498. * 0 = success
  13499. * !0 = failure
  13500. *
  13501. * PARAMETERS:
  13502. * *dev - pointer to device structure
  13503. * *attr - pointer to device attributes
  13504. * *buf - pointer to output buffer
  13505. ******************************************************************************/
  13506. static ssize_t pt_ttdl_status_show(struct device *dev,
  13507. struct device_attribute *attr, char *buf)
  13508. {
  13509. struct pt_core_data *cd = dev_get_drvdata(dev);
  13510. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13511. struct i2c_client *client = to_i2c_client(dev);
  13512. ssize_t ret;
  13513. u16 cal_size = 0;
  13514. unsigned short crc = 0;
  13515. if (cd->cal_cache_in_host)
  13516. _pt_manage_local_cal_data(dev,
  13517. PT_CAL_DATA_INFO, &cal_size, &crc);
  13518. ret = scnprintf(buf, strlen(buf),
  13519. "%s: 0x%04X\n"
  13520. "%s: %d\n"
  13521. "%s: %s\n"
  13522. "%s: %s %s\n"
  13523. "%s: %s\n"
  13524. "%s: 0x%02X\n"
  13525. "%s: %s\n"
  13526. "%s: %s\n"
  13527. "%s: %s\n"
  13528. "%s: %s\n"
  13529. "%s: %d\n"
  13530. "%s: %d\n"
  13531. "%s: %s\n"
  13532. "%s: %s\n"
  13533. "%s: %d\n"
  13534. #ifdef TTDL_DIAGNOSTICS
  13535. "%s: %d\n"
  13536. "%s: %d\n"
  13537. "%s: %d\n"
  13538. "%s: %d\n"
  13539. "%s: %d\n"
  13540. "%s: %d\n"
  13541. "%s: %d\n"
  13542. "%s: %d\n"
  13543. "%s: %d\n"
  13544. "%s: %d\n"
  13545. "%s: %s\n"
  13546. "%s: %s\n"
  13547. "%s: %s\n"
  13548. "%s: %d\n"
  13549. "%s: 0x%04X\n"
  13550. "%s: %s\n"
  13551. #endif /* TTDL_DIAGNOSTICS */
  13552. ,
  13553. "Startup Status ", cd->startup_status,
  13554. "TTDL Debug Level ", cd->debug_level,
  13555. "Mode ",
  13556. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13557. "Operational" : "BL") : "Unknown",
  13558. "DUT Generation ",
  13559. cd->active_dut_generation ?
  13560. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13561. "PT TC/TT" : "Gen5/6") : "Unknown",
  13562. cd->active_dut_generation ?
  13563. (cd->set_dut_generation == true ?
  13564. "(Set)" : "(Detected)") : "",
  13565. "HW Detected ",
  13566. cd->hw_detected ? "True" : "False",
  13567. "I2C Address ",
  13568. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13569. "Active Bus Module ",
  13570. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13571. "Flashless Mode ",
  13572. cd->flashless_dut == 1 ? "Yes" : "No",
  13573. "GPIO state - IRQ ",
  13574. cd->cpdata->irq_stat ?
  13575. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13576. "High" : "Low") : "not defined",
  13577. "GPIO state - TP_XRES ",
  13578. pdata->core_pdata->rst_gpio ?
  13579. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13580. "High" : "Low") : "not defined",
  13581. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13582. "Startup Retry Count ", cd->startup_retry_count,
  13583. "WD - Manual Force Stop ",
  13584. cd->watchdog_force_stop ? "True" : "False",
  13585. "WD - Enabled ",
  13586. cd->watchdog_enabled ? "True" : "False",
  13587. "WD - Interval (ms) ", cd->watchdog_interval
  13588. #ifdef TTDL_DIAGNOSTICS
  13589. , "WD - Triggered Count ", cd->watchdog_count,
  13590. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13591. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13592. "WD - XRES Count ", cd->wd_xres_count,
  13593. "IRQ Triggered Count ", cd->irq_count,
  13594. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13595. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13596. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13597. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13598. "Error GPIO trigger type ", cd->err_gpio_type,
  13599. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13600. "Suppress No-Flash Auto BL ",
  13601. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13602. "Yes" : "No",
  13603. "Calibration Cache on host ",
  13604. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13605. "Yes" : "No",
  13606. "Calibration Cache size ", cal_size,
  13607. "Calibration Cache chip ID ", crc,
  13608. "Multi-Chip Support ",
  13609. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13610. #endif /* TTDL_DIAGNOSTICS */
  13611. );
  13612. #ifdef TTDL_DIAGNOSTICS
  13613. /* Reset all diagnostic counters */
  13614. cd->watchdog_count = 0;
  13615. cd->watchdog_irq_stuck_count = 0;
  13616. cd->watchdog_failed_access_count = 0;
  13617. cd->wd_xres_count = 0;
  13618. cd->irq_count = 0;
  13619. cd->bl_retry_packet_count = 0;
  13620. cd->pip2_crc_error_count = 0;
  13621. cd->bus_transmit_error_count = 0;
  13622. #endif
  13623. return ret;
  13624. }
  13625. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13626. /*******************************************************************************
  13627. * FUNCTION: pt_pip2_enter_bl_show
  13628. *
  13629. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13630. * the DUT into the BL and show the success or failure of entering the BL
  13631. *
  13632. * RETURN:
  13633. * 0 = success
  13634. * !0 = failure
  13635. *
  13636. * PARAMETERS:
  13637. * *dev - pointer to device structure
  13638. * *attr - pointer to device attributes
  13639. * *buf - pointer to output buffer
  13640. ******************************************************************************/
  13641. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13642. struct device_attribute *attr, char *buf)
  13643. {
  13644. ssize_t ret = 0;
  13645. int rc = 0;
  13646. int result = 0;
  13647. u8 mode = PT_MODE_UNKNOWN;
  13648. struct pt_core_data *cd = dev_get_drvdata(dev);
  13649. /* Turn off the TTDL WD before enter bootloader */
  13650. pt_stop_wd_timer(cd);
  13651. /* Ensure NO enumeration work is queued or will be queued */
  13652. cancel_work_sync(&cd->enum_work);
  13653. mutex_lock(&cd->system_lock);
  13654. cd->bridge_mode = true;
  13655. mutex_unlock(&cd->system_lock);
  13656. /* set mode to operational to avoid any extra PIP traffic */
  13657. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13658. switch (result) {
  13659. case PT_ENTER_BL_PASS:
  13660. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13661. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13662. break;
  13663. case PT_ENTER_BL_ERROR:
  13664. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13665. rc,
  13666. " Unknown Error");
  13667. break;
  13668. case PT_ENTER_BL_RESET_FAIL:
  13669. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13670. rc,
  13671. " Soft Reset Failed");
  13672. break;
  13673. case PT_ENTER_BL_HID_START_BL_FAIL:
  13674. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13675. rc,
  13676. " PIP Start BL Cmd Failed");
  13677. break;
  13678. case PT_ENTER_BL_CONFIRM_FAIL:
  13679. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13680. rc,
  13681. " Error confirming DUT entered BL");
  13682. break;
  13683. default:
  13684. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13685. rc, " Unknown Error");
  13686. break;
  13687. };
  13688. /* Allow enumeration work to be queued again */
  13689. cd->bridge_mode = false;
  13690. return ret;
  13691. }
  13692. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13693. /*******************************************************************************
  13694. * FUNCTION: pt_pip2_exit_bl_show
  13695. *
  13696. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13697. * launch the APP and put the DUT Application mode
  13698. *
  13699. * RETURN:
  13700. * 0 = success
  13701. * !0 = failure
  13702. *
  13703. * PARAMETERS:
  13704. * *dev - pointer to device structure
  13705. * *attr - pointer to device attributes
  13706. * *buf - pointer to output buffer
  13707. ******************************************************************************/
  13708. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13709. struct device_attribute *attr, char *buf)
  13710. {
  13711. struct pt_core_data *cd = dev_get_drvdata(dev);
  13712. ssize_t ret = 0;
  13713. int rc = 0;
  13714. u8 status_str[PT_STATUS_STR_LEN];
  13715. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13716. /*
  13717. * Perform enum if startup_status doesn't reach to
  13718. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13719. */
  13720. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13721. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13722. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13723. strlcpy(status_str,
  13724. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13725. }
  13726. }
  13727. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13728. rc, status_str);
  13729. return ret;
  13730. }
  13731. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13732. #endif
  13733. #ifdef EASYWAKE_TSG6
  13734. /*******************************************************************************
  13735. * FUNCTION: pt_easy_wakeup_gesture_show
  13736. *
  13737. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13738. * current easy wakeup gesture
  13739. *
  13740. * RETURN:
  13741. * 0 = success
  13742. * !0 = failure
  13743. *
  13744. * PARAMETERS:
  13745. * *dev - pointer to device structure
  13746. * *attr - pointer to device attributes
  13747. * *buf - pointer to output buffer
  13748. ******************************************************************************/
  13749. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13750. struct device_attribute *attr, char *buf)
  13751. {
  13752. struct pt_core_data *cd = dev_get_drvdata(dev);
  13753. ssize_t ret;
  13754. mutex_lock(&cd->system_lock);
  13755. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13756. cd->easy_wakeup_gesture);
  13757. mutex_unlock(&cd->system_lock);
  13758. return ret;
  13759. }
  13760. /*******************************************************************************
  13761. * FUNCTION: pt_easy_wakeup_gesture_store
  13762. *
  13763. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13764. * allows the wake gesture to be set to a custom value.
  13765. *
  13766. * RETURN: Size of passed in buffer is success
  13767. *
  13768. * PARAMETERS:
  13769. * *dev - pointer to device structure
  13770. * *attr - pointer to device attributes
  13771. * *buf - pointer to buffer that hold the command parameters
  13772. * size - size of buf
  13773. ******************************************************************************/
  13774. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13775. struct device_attribute *attr, const char *buf, size_t size)
  13776. {
  13777. struct pt_core_data *cd = dev_get_drvdata(dev);
  13778. u32 input_data[2];
  13779. int length;
  13780. int rc = 0;
  13781. /* Maximum input of one value */
  13782. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13783. ARRAY_SIZE(input_data));
  13784. if (length != 1) {
  13785. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13786. __func__);
  13787. rc = -EINVAL;
  13788. goto exit;
  13789. }
  13790. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13791. __func__, cd->features.easywake);
  13792. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13793. rc = -EINVAL;
  13794. goto exit;
  13795. }
  13796. pm_runtime_get_sync(dev);
  13797. mutex_lock(&cd->system_lock);
  13798. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13799. cd->easy_wakeup_gesture = (u8)input_data[0];
  13800. pt_debug(dev, DL_INFO,
  13801. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13802. __func__, cd->easy_wakeup_gesture);
  13803. } else
  13804. rc = -ENODEV;
  13805. mutex_unlock(&cd->system_lock);
  13806. pm_runtime_put(dev);
  13807. exit:
  13808. if (rc)
  13809. return rc;
  13810. return size;
  13811. }
  13812. /*******************************************************************************
  13813. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13814. *
  13815. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13816. * show the TSG6 easywake gesture ID
  13817. *
  13818. * RETURN:
  13819. * 0 = success
  13820. * !0 = failure
  13821. *
  13822. * PARAMETERS:
  13823. * *dev - pointer to device structure
  13824. * *attr - pointer to device attributes
  13825. * *buf - pointer to output buffer
  13826. ******************************************************************************/
  13827. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13828. struct device_attribute *attr, char *buf)
  13829. {
  13830. struct pt_core_data *cd = dev_get_drvdata(dev);
  13831. ssize_t ret;
  13832. mutex_lock(&cd->system_lock);
  13833. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13834. cd->gesture_id);
  13835. mutex_unlock(&cd->system_lock);
  13836. return ret;
  13837. }
  13838. /*******************************************************************************
  13839. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13840. *
  13841. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13842. * show the TSG6 easywake gesture data in the following format:
  13843. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13844. *
  13845. * RETURN:
  13846. * 0 = success
  13847. * !0 = failure
  13848. *
  13849. * PARAMETERS:
  13850. * *dev - pointer to device structure
  13851. * *attr - pointer to device attributes
  13852. * *buf - pointer to output buffer
  13853. ******************************************************************************/
  13854. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13855. struct device_attribute *attr, char *buf)
  13856. {
  13857. struct pt_core_data *cd = dev_get_drvdata(dev);
  13858. ssize_t ret = 0;
  13859. int i;
  13860. mutex_lock(&cd->system_lock);
  13861. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13862. for (i = 0; i < cd->gesture_data_length; i++)
  13863. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13864. "0x%02X\n", cd->gesture_data[i]);
  13865. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13866. "(%d bytes)\n", cd->gesture_data_length);
  13867. mutex_unlock(&cd->system_lock);
  13868. return ret;
  13869. }
  13870. #endif /* EASYWAKE_TSG6 */
  13871. #ifdef TTDL_DIAGNOSTICS
  13872. /*******************************************************************************
  13873. * FUNCTION: pt_err_gpio_show
  13874. *
  13875. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13876. * setting up the gpio was successful
  13877. *
  13878. * RETURN: Char buffer with printed GPIO creation state
  13879. *
  13880. * PARAMETERS:
  13881. * *dev - pointer to device structure
  13882. * *attr - pointer to device attributes
  13883. * *buf - pointer to output buffer
  13884. ******************************************************************************/
  13885. static ssize_t pt_err_gpio_show(struct device *dev,
  13886. struct device_attribute *attr, char *buf)
  13887. {
  13888. struct pt_core_data *cd = dev_get_drvdata(dev);
  13889. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13890. "Err GPIO (%d) : %s\n"
  13891. "Err GPIO trig type: %d\n",
  13892. cd->err_gpio,
  13893. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13894. "HIGH" : "low") : "not defined"),
  13895. cd->err_gpio_type);
  13896. }
  13897. /*******************************************************************************
  13898. * FUNCTION: pt_err_gpio_store
  13899. *
  13900. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13901. * available host GPIO to be used to trigger when TTDL detects a PIP
  13902. * command/response timeout.
  13903. *
  13904. * RETURN:
  13905. * 0 = success
  13906. * !0 = failure
  13907. *
  13908. * PARAMETERS:
  13909. * *dev - pointer to device structure
  13910. * *attr - pointer to device attributes
  13911. * *buf - pointer to buffer that hold the command parameters
  13912. * size - size of buf
  13913. ******************************************************************************/
  13914. static ssize_t pt_err_gpio_store(struct device *dev,
  13915. struct device_attribute *attr, const char *buf, size_t size)
  13916. {
  13917. struct pt_core_data *cd = dev_get_drvdata(dev);
  13918. unsigned long gpio;
  13919. int rc = 0;
  13920. u32 input_data[3];
  13921. int length;
  13922. u8 err_type;
  13923. input_data[0] = 0;
  13924. input_data[1] = 0;
  13925. /* Maximmum input is two elements */
  13926. length = _pt_ic_parse_input(dev, buf, size,
  13927. input_data, ARRAY_SIZE(input_data));
  13928. if (length < 1 || length > 2) {
  13929. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13930. __func__);
  13931. rc = -EINVAL;
  13932. goto exit;
  13933. }
  13934. gpio = input_data[0];
  13935. err_type = (u8)input_data[1];
  13936. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13937. rc = -EINVAL;
  13938. goto exit;
  13939. }
  13940. mutex_lock(&cd->system_lock);
  13941. gpio_free(gpio);
  13942. rc = gpio_request(gpio, NULL);
  13943. if (rc) {
  13944. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13945. rc = -ENODEV;
  13946. } else {
  13947. cd->err_gpio = gpio;
  13948. cd->err_gpio_type = err_type;
  13949. gpio_direction_output(gpio, 0);
  13950. }
  13951. mutex_unlock(&cd->system_lock);
  13952. exit:
  13953. if (rc)
  13954. return rc;
  13955. return size;
  13956. }
  13957. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13958. pt_err_gpio_store);
  13959. /*******************************************************************************
  13960. * FUNCTION: pt_drv_irq_show
  13961. *
  13962. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13963. * TTDL interrupt is enabled/disabled
  13964. *
  13965. * RETURN:
  13966. * 0 = success
  13967. * !0 = failure
  13968. *
  13969. * PARAMETERS:
  13970. * *dev - pointer to device structure
  13971. * *attr - pointer to device attributes
  13972. * *buf - pointer to output buffer
  13973. ******************************************************************************/
  13974. static ssize_t pt_drv_irq_show(struct device *dev,
  13975. struct device_attribute *attr, char *buf)
  13976. {
  13977. struct pt_core_data *cd = dev_get_drvdata(dev);
  13978. ssize_t ret = 0;
  13979. mutex_lock(&cd->system_lock);
  13980. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13981. if (cd->irq_enabled)
  13982. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13983. "Driver interrupt: ENABLED\n");
  13984. else
  13985. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13986. "Driver interrupt: DISABLED\n");
  13987. mutex_unlock(&cd->system_lock);
  13988. return ret;
  13989. }
  13990. /*******************************************************************************
  13991. * FUNCTION: pt_drv_irq_store
  13992. *
  13993. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13994. * IRQ to be enabled/disabled.
  13995. *
  13996. * RETURN: Size of passed in buffer
  13997. *
  13998. * PARAMETERS:
  13999. * *dev - pointer to device structure
  14000. * *attr - pointer to device attributes
  14001. * *buf - pointer to buffer that hold the command parameters
  14002. * size - size of buf
  14003. ******************************************************************************/
  14004. static ssize_t pt_drv_irq_store(struct device *dev,
  14005. struct device_attribute *attr, const char *buf, size_t size)
  14006. {
  14007. struct pt_core_data *cd = dev_get_drvdata(dev);
  14008. u32 input_data[2];
  14009. int length;
  14010. int rc = 0;
  14011. /* Maximum input of one value */
  14012. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14013. ARRAY_SIZE(input_data));
  14014. if (length != 1) {
  14015. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14016. __func__);
  14017. rc = -EINVAL;
  14018. goto exit;
  14019. }
  14020. mutex_lock(&cd->system_lock);
  14021. switch (input_data[0]) {
  14022. case 0:
  14023. if (cd->irq_enabled) {
  14024. cd->irq_enabled = false;
  14025. /* Disable IRQ has no return value to check */
  14026. disable_irq_nosync(cd->irq);
  14027. pt_debug(dev, DL_INFO,
  14028. "%s: Driver IRQ now disabled\n",
  14029. __func__);
  14030. } else
  14031. pt_debug(dev, DL_INFO,
  14032. "%s: Driver IRQ already disabled\n",
  14033. __func__);
  14034. break;
  14035. case 1:
  14036. if (cd->irq_enabled == false) {
  14037. cd->irq_enabled = true;
  14038. enable_irq(cd->irq);
  14039. pt_debug(dev, DL_INFO,
  14040. "%s: Driver IRQ now enabled\n",
  14041. __func__);
  14042. } else
  14043. pt_debug(dev, DL_INFO,
  14044. "%s: Driver IRQ already enabled\n",
  14045. __func__);
  14046. break;
  14047. default:
  14048. rc = -EINVAL;
  14049. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14050. }
  14051. mutex_unlock(&(cd->system_lock));
  14052. exit:
  14053. if (rc)
  14054. return rc;
  14055. return size;
  14056. }
  14057. /*******************************************************************************
  14058. * FUNCTION: pt_pip2_bin_hdr_show
  14059. *
  14060. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14061. * the bin file header from flash and show each field
  14062. *
  14063. * RETURN:
  14064. * 0 = success
  14065. * !0 = failure
  14066. *
  14067. * PARAMETERS:
  14068. * *dev - pointer to device structure
  14069. * *attr - pointer to device attributes
  14070. * *buf - pointer to output buffer
  14071. ******************************************************************************/
  14072. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14073. struct device_attribute *attr, char *buf)
  14074. {
  14075. ssize_t ret = 0;
  14076. struct pt_bin_file_hdr hdr = {0};
  14077. int rc;
  14078. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14079. ret = scnprintf(buf, strlen(buf),
  14080. "%s: %d\n"
  14081. "%s: %d\n"
  14082. "%s: 0x%04X\n"
  14083. "%s: %d\n"
  14084. "%s: %d\n"
  14085. "%s: %d\n"
  14086. "%s: 0x%08X\n"
  14087. "%s: 0x%04X\n"
  14088. "%s: 0x%04X\n"
  14089. "%s: %d\n"
  14090. "%s: %d\n",
  14091. "Status", rc,
  14092. "Header Length ", hdr.length,
  14093. "TTPID ", hdr.ttpid,
  14094. "FW Major Ver ", hdr.fw_major,
  14095. "FW Minor Ver ", hdr.fw_minor,
  14096. "FW Rev Control ", hdr.fw_rev_ctrl,
  14097. "FW CRC ", hdr.fw_crc,
  14098. "Silicon Rev ", hdr.si_rev,
  14099. "Silicon ID ", hdr.si_id,
  14100. "Config Ver ", hdr.config_ver,
  14101. "HEX File Size ", hdr.hex_file_size
  14102. );
  14103. return ret;
  14104. }
  14105. /*******************************************************************************
  14106. * FUNCTION: pt_platform_data_show
  14107. *
  14108. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14109. * active platform data including: GPIOs, Vendor and Product IDs,
  14110. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14111. * HID registers, and Easy wake gesture
  14112. *
  14113. * RETURN: Size of printed data
  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_platform_data_show(struct device *dev,
  14121. struct device_attribute *attr, char *buf)
  14122. {
  14123. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14124. struct pt_core_data *cd = dev_get_drvdata(dev);
  14125. ssize_t ret;
  14126. ret = scnprintf(buf, strlen(buf),
  14127. "%s: %d\n"
  14128. "%s: %d\n"
  14129. "%s: %d\n"
  14130. "%s: %d\n"
  14131. "%s: %d\n"
  14132. "%s: %d\n"
  14133. "%s: %d\n"
  14134. "%s: %d\n"
  14135. "%s: %d\n"
  14136. "%s: %d\n"
  14137. "%s: %d\n"
  14138. "%s: %d\n"
  14139. "%s: %d\n"
  14140. "%s: %d\n"
  14141. "%s: %d\n"
  14142. "%s: %d\n"
  14143. "%s: %d\n"
  14144. "%s: %d\n"
  14145. "%s: %d\n"
  14146. "%s: %d\n"
  14147. "%s: %d\n"
  14148. "%s: %d\n"
  14149. "%s: %d\n"
  14150. "%s: %d\n"
  14151. "%s: %d\n"
  14152. "%s: %d\n"
  14153. "%s: %d\n"
  14154. "%s: %s\n"
  14155. "%s: %s\n"
  14156. "%s: %d\n",
  14157. "Status", 0,
  14158. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14159. "Interrupt GPIO Value ",
  14160. pdata->core_pdata->irq_gpio ?
  14161. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14162. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14163. "Reset GPIO Value ",
  14164. pdata->core_pdata->rst_gpio ?
  14165. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14166. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14167. "DDI Reset GPIO Value ",
  14168. pdata->core_pdata->ddi_rst_gpio ?
  14169. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14170. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14171. "VDDI GPIO Value ",
  14172. pdata->core_pdata->vddi_gpio ?
  14173. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14174. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14175. "VCC GPIO Value ",
  14176. pdata->core_pdata->vcc_gpio ?
  14177. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14178. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14179. "AVDD GPIO Value ",
  14180. pdata->core_pdata->avdd_gpio ?
  14181. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14182. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14183. "AVEE GPIO Value ",
  14184. pdata->core_pdata->avee_gpio ?
  14185. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14186. "Vendor ID ", pdata->core_pdata->vendor_id,
  14187. "Product ID ", pdata->core_pdata->product_id,
  14188. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14189. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14190. "Core data flags ", pdata->core_pdata->flags,
  14191. "MT data flags ", pdata->mt_pdata->flags,
  14192. "Loader data flags ", pdata->loader_pdata->flags,
  14193. "Level trigger delay (us) ",
  14194. pdata->core_pdata->level_irq_udelay,
  14195. "HID Descriptor Register ",
  14196. pdata->core_pdata->hid_desc_register,
  14197. "HID Output Register ",
  14198. cd->hid_desc.output_register,
  14199. "HID Command Register ",
  14200. cd->hid_desc.command_register,
  14201. "Easy wakeup gesture ",
  14202. pdata->core_pdata->easy_wakeup_gesture,
  14203. "Config DUT generation ",
  14204. pdata->core_pdata->config_dut_generation ?
  14205. (pdata->core_pdata->config_dut_generation ==
  14206. CONFIG_DUT_PIP2_CAPABLE ?
  14207. "PT TC/TT" : "Gen5/6") : "Auto",
  14208. "Watchdog Force Stop ",
  14209. pdata->core_pdata->watchdog_force_stop ?
  14210. "True" : "False",
  14211. "Panel ID Support ",
  14212. pdata->core_pdata->panel_id_support);
  14213. return ret;
  14214. }
  14215. #define PT_ERR_STR_SIZE 64
  14216. /*******************************************************************************
  14217. * FUNCTION: pt_bist_bus_test
  14218. *
  14219. * SUMMARY: Tests the connectivity of the active bus pins:
  14220. * I2C - SDA and SCL
  14221. * SPI - MOSI, MISO, CLK
  14222. *
  14223. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14224. * bus. If any data is read we know the I2C/SPI pins are connected
  14225. *
  14226. * RETURN:
  14227. * 0 = Success
  14228. * !0 = Failure
  14229. *
  14230. * PARAMETERS:
  14231. * *dev - pointer to device structure
  14232. * *net_toggled - pointer to where to store if bus toggled
  14233. * *err_str - pointer to error string buffer
  14234. ******************************************************************************/
  14235. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14236. {
  14237. struct pt_core_data *cd = dev_get_drvdata(dev);
  14238. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14239. u8 *read_buf = NULL;
  14240. int bytes_read = 0;
  14241. int rc = 0;
  14242. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14243. if (read_buf == NULL) {
  14244. rc = -ENOMEM;
  14245. goto exit;
  14246. }
  14247. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14248. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14249. disable_irq(cd->irq);
  14250. mutex_lock(&cd->system_lock);
  14251. cd->irq_disabled = true;
  14252. mutex_unlock(&cd->system_lock);
  14253. /*
  14254. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14255. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14256. */
  14257. usleep_range(5000, 6000);
  14258. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14259. __func__, (int)sizeof(ver_cmd));
  14260. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14261. ">>> User CMD");
  14262. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14263. if (rc) {
  14264. pt_debug(dev, DL_ERROR,
  14265. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14266. *net_toggled = 0;
  14267. strlcpy(err_str,
  14268. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14269. goto exit_enable_irq;
  14270. }
  14271. usleep_range(4000, 5000);
  14272. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14273. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14274. __func__, bytes_read);
  14275. if (bytes_read == 0) {
  14276. *net_toggled = 0;
  14277. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14278. __func__);
  14279. strlcpy(err_str,
  14280. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14281. rc = -EIO;
  14282. goto exit_enable_irq;
  14283. } else {
  14284. if (cd->bus_ops->bustype == BUS_I2C)
  14285. *net_toggled = 1;
  14286. else {
  14287. if ((bytes_read > 3) &&
  14288. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14289. PIP2_CMD_ID_VERSION)
  14290. *net_toggled = 1;
  14291. else {
  14292. *net_toggled = 0;
  14293. pt_debug(dev, DL_INFO,
  14294. "%s: BUS Read Failed, %d bytes read\n",
  14295. __func__, bytes_read);
  14296. strlcpy(err_str,
  14297. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14298. }
  14299. }
  14300. }
  14301. exit_enable_irq:
  14302. enable_irq(cd->irq);
  14303. usleep_range(5000, 6000);
  14304. mutex_lock(&cd->system_lock);
  14305. cd->irq_disabled = false;
  14306. mutex_unlock(&cd->system_lock);
  14307. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14308. exit:
  14309. kfree(read_buf);
  14310. return rc;
  14311. }
  14312. /*******************************************************************************
  14313. * FUNCTION: pt_bist_irq_test
  14314. *
  14315. * SUMMARY: Tests the connectivity of the IRQ net
  14316. *
  14317. * This test will ensure there is a good connection between the host
  14318. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14319. * asserted and if so keep reading messages off of the bus until
  14320. * it de-asserts. Possible outcomes:
  14321. * - IRQ was already de-asserted: Send a PIP command and if an
  14322. * interrupt is generated the test passes.
  14323. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14324. * test passes.
  14325. * - IRQ stays asserted: After reading the bus multiple times
  14326. * the IRQ stays asserted. Likely open or shorted to GND
  14327. *
  14328. * RETURN:
  14329. * 0 = Success
  14330. * !0 = Failure
  14331. *
  14332. * PARAMETERS:
  14333. * *dev - pointer to device structure
  14334. * *bus_toggled - pointer to where to store if bus toggled
  14335. * *irq_toggled - pointer to where to store if IRQ toggled
  14336. * *xres_toggled - pointer to where to store if XRES toggled
  14337. * *err_str - pointer to error string buffer
  14338. ******************************************************************************/
  14339. static int pt_bist_irq_test(struct device *dev,
  14340. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14341. {
  14342. struct pt_core_data *cd = dev_get_drvdata(dev);
  14343. u8 *read_buf = NULL;
  14344. u8 mode = PT_MODE_UNKNOWN;
  14345. u16 actual_read_len;
  14346. int bytes_read = 0;
  14347. int count = 0;
  14348. int rc = 0;
  14349. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14350. if (read_buf == NULL) {
  14351. rc = -ENOMEM;
  14352. goto exit;
  14353. }
  14354. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14355. cd->irq_count = 0;
  14356. /*
  14357. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14358. * the bus based on the 2 byte initial length read. Try up to 5x.
  14359. */
  14360. while (pt_check_irq_asserted(cd) && count < 5) {
  14361. count++;
  14362. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14363. }
  14364. if (count > 1 && count < 5 && bytes_read > 0) {
  14365. /*
  14366. * IRQ was stuck but data was successfully read from the
  14367. * bus releasing the IRQ line.
  14368. */
  14369. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14370. __func__, count, bytes_read);
  14371. *bus_toggled = 1;
  14372. *irq_toggled = 1;
  14373. goto exit;
  14374. }
  14375. if (count == 5 && bytes_read == 0) {
  14376. /*
  14377. * Looped 5x and read nothing off the bus yet the IRQ is still
  14378. * asserted. Possible conditions:
  14379. * - IRQ open circuit
  14380. * - IRQ shorted to GND
  14381. * - I2C/SPI bus is disconnected
  14382. * - FW holding the pin low
  14383. * Try entering the BL to see if communication works there.
  14384. */
  14385. mode = PT_MODE_IGNORE;
  14386. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14387. if (rc) {
  14388. pt_debug(dev, DL_ERROR,
  14389. "%s Failed to enter BL\n", __func__);
  14390. strlcpy(err_str,
  14391. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14392. *irq_toggled = 0;
  14393. goto exit;
  14394. }
  14395. /*
  14396. * If original mode was operational and we successfully
  14397. * entered the BL, then the XRES net must have toggled
  14398. */
  14399. if (mode == PT_MODE_OPERATIONAL)
  14400. *xres_toggled = 1;
  14401. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14402. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14403. &actual_read_len);
  14404. if (rc) {
  14405. /*
  14406. * Could not communicate to DUT in BL mode. Save the
  14407. * error string, slim chance but the XRES test below may
  14408. * show the IRQ is actually working.
  14409. */
  14410. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14411. pt_debug(dev, DL_ERROR,
  14412. "%s: %s, count=%d bytes_read=%d\n",
  14413. __func__, err_str, count, bytes_read);
  14414. *irq_toggled = 0;
  14415. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14416. goto exit;
  14417. } else {
  14418. *bus_toggled = 1;
  14419. *irq_toggled = 1;
  14420. goto exit;
  14421. }
  14422. }
  14423. if (pt_check_irq_asserted(cd)) {
  14424. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14425. rc = -EIO;
  14426. *irq_toggled = 0;
  14427. goto exit;
  14428. }
  14429. /* Try sending a PIP command to see if we get a response */
  14430. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14431. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14432. if (rc) {
  14433. /*
  14434. * Potential IRQ issue, no communication in App mode, attempt
  14435. * the same command in the BL
  14436. */
  14437. mode = PT_MODE_IGNORE;
  14438. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14439. if (rc) {
  14440. pt_debug(dev, DL_ERROR,
  14441. "%s Failed to enter BL\n", __func__);
  14442. *irq_toggled = 0;
  14443. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14444. goto exit;
  14445. }
  14446. /*
  14447. * If original mode was operational and we successfully
  14448. * entered the BL, this will be useful info for the tp_xres
  14449. * test below.
  14450. */
  14451. if (mode == PT_MODE_OPERATIONAL)
  14452. *xres_toggled = 1;
  14453. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14454. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14455. &actual_read_len);
  14456. if (rc) {
  14457. /*
  14458. * Could not communicate in FW mode or BL mode. Save the
  14459. * error string, slim chance but the XRES test below may
  14460. * show the IRQ is actually working.
  14461. */
  14462. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14463. pt_debug(dev, DL_ERROR,
  14464. "%s: request_active_pip_prot failed\n",
  14465. __func__);
  14466. *irq_toggled = 0;
  14467. goto exit;
  14468. }
  14469. }
  14470. if (cd->irq_count > 0) {
  14471. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14472. cd->irq_count);
  14473. *bus_toggled = 1;
  14474. *irq_toggled = 1;
  14475. goto exit;
  14476. }
  14477. exit:
  14478. kfree(read_buf);
  14479. return rc;
  14480. }
  14481. /*******************************************************************************
  14482. * FUNCTION: pt_bist_xres_test
  14483. *
  14484. * SUMMARY: Tests the connectivity of the TP_XRES net
  14485. *
  14486. * This test will ensure there is a good connection between the host
  14487. * and the DUT on the tp_xres pin. The pin will be toggled to
  14488. * generate a TP reset which will cause the DUT to output a reset
  14489. * sentinel. If the reset sentinel is seen the test passes. If it is
  14490. * not seen the test will attempt to send a soft reset to simply gain
  14491. * some additional information on the failure:
  14492. * - soft reset fails to send: XRES and IRQ likely open
  14493. * - soft reset passes: XRES likely open or stuck de-asserted
  14494. * - soft reset fails: XRES likely stuck asserted
  14495. *
  14496. * RETURN:
  14497. * 0 = Success
  14498. * !0 = Failure
  14499. *
  14500. * PARAMETERS:
  14501. * *dev - pointer to device structure
  14502. * *bus_toggled - pointer to where to store if bus toggled
  14503. * *irq_toggled - pointer to where to store if IRQ toggled
  14504. * *xres_toggled - pointer to where to store if XRES toggled
  14505. * *err_str - pointer to error string buffer
  14506. ******************************************************************************/
  14507. static int pt_bist_xres_test(struct device *dev,
  14508. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14509. {
  14510. struct pt_core_data *cd = dev_get_drvdata(dev);
  14511. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14512. u8 *read_buf = NULL;
  14513. u8 mode = PT_MODE_UNKNOWN;
  14514. int rc = 0;
  14515. int t = 0;
  14516. int timeout = 300;
  14517. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14518. if (read_buf == NULL) {
  14519. rc = -ENOMEM;
  14520. goto exit;
  14521. }
  14522. /* Clear the startup bit mask, reset and enum will re-populate it */
  14523. cd->startup_status = STARTUP_STATUS_START;
  14524. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14525. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14526. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14527. rc = -ENODEV;
  14528. goto exit;
  14529. }
  14530. /* Ensure we have nothing pending on active bus */
  14531. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14532. /* Perform a hard XRES toggle and wait for reset sentinel */
  14533. mutex_lock(&cd->system_lock);
  14534. cd->hid_reset_cmd_state = 1;
  14535. mutex_unlock(&cd->system_lock);
  14536. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14537. rc = pt_hw_hard_reset(cd);
  14538. /* Set timeout to 1s for the flashless case where a BL could be done */
  14539. if (cd->flashless_dut)
  14540. timeout = 1000;
  14541. /*
  14542. * To avoid the case that next PIP command can be confused by BL/FW
  14543. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14544. * followed by "wakeup event" function can lower the failure rate.
  14545. */
  14546. t = wait_event_timeout(cd->wait_q,
  14547. ((cd->startup_status != STARTUP_STATUS_START)
  14548. && (cd->hid_reset_cmd_state == 0)),
  14549. msecs_to_jiffies(timeout));
  14550. if (IS_TMO(t)) {
  14551. pt_debug(cd->dev, DL_ERROR,
  14552. "%s: TMO waiting for sentinel\n", __func__);
  14553. *xres_toggled = 0;
  14554. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14555. /*
  14556. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14557. * To enter the BL we need to generate an XRES so first try to
  14558. * launch the applicaiton
  14559. */
  14560. if (cd->mode == PT_MODE_BOOTLOADER)
  14561. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14562. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14563. __func__);
  14564. mode = PT_MODE_IGNORE;
  14565. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14566. if (rc) {
  14567. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14568. __func__);
  14569. *xres_toggled = 0;
  14570. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14571. goto exit;
  14572. } else {
  14573. /* Wait for the BL sentinel */
  14574. t = wait_event_timeout(cd->wait_q,
  14575. (cd->startup_status != STARTUP_STATUS_START),
  14576. msecs_to_jiffies(500));
  14577. if (IS_TMO(t)) {
  14578. pt_debug(cd->dev, DL_ERROR,
  14579. "%s: TMO waiting for BL sentinel\n",
  14580. __func__);
  14581. *xres_toggled = 0;
  14582. strlcpy(err_str,
  14583. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14584. rc = -ETIME;
  14585. goto exit;
  14586. }
  14587. }
  14588. }
  14589. mutex_lock(&cd->system_lock);
  14590. cd->hid_reset_cmd_state = 0;
  14591. mutex_unlock(&cd->system_lock);
  14592. /* Look for BL or FW reset sentinels */
  14593. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14594. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14595. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14596. /* If a sentinel was seen, all nets are working */
  14597. *xres_toggled = 1;
  14598. *irq_toggled = 1;
  14599. /*
  14600. * For SPI test, bus read result can be confused as FW sentinel
  14601. * if MISO(slave) is connected to MISO(host).
  14602. */
  14603. if (cd->bus_ops->bustype == BUS_I2C)
  14604. *bus_toggled = 1;
  14605. } else {
  14606. /*
  14607. * Hard reset failed, however some additional information
  14608. * could be determined. Try a soft reset to see if DUT resets
  14609. * with the possible outcomes:
  14610. * - if it resets the line is not stuck asserted
  14611. * - if it does not reset the line could be stuck asserted
  14612. */
  14613. *xres_toggled = 0;
  14614. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14615. msleep(30);
  14616. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14617. __func__, rc);
  14618. if (rc) {
  14619. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14620. pt_debug(dev, DL_ERROR,
  14621. "%s: Hard reset failed, soft reset failed %s\n",
  14622. __func__, err_str);
  14623. goto exit;
  14624. }
  14625. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14626. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14627. strlcpy(err_str,
  14628. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14629. pt_debug(dev, DL_ERROR,
  14630. "%s: Hard reset failed, soft reset passed-%s\n",
  14631. __func__, err_str);
  14632. } else if (cd->startup_status == 0) {
  14633. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14634. pt_debug(dev, DL_ERROR,
  14635. "%s: Hard reset failed, soft reset failed-%s\n",
  14636. __func__, err_str);
  14637. } else {
  14638. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14639. pt_debug(dev, DL_ERROR,
  14640. "%s: Hard and Soft reset failed - %s\n",
  14641. __func__, err_str);
  14642. }
  14643. }
  14644. exit:
  14645. kfree(read_buf);
  14646. return rc;
  14647. }
  14648. /*******************************************************************************
  14649. * FUNCTION: pt_bist_slave_irq_test
  14650. *
  14651. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14652. *
  14653. * This test will ensure there is a good IRQ connection between the master
  14654. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14655. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14656. * saw each slave trigger the IRQ with it's reset sentinel.
  14657. *
  14658. * RETURN:
  14659. * 0 = Success
  14660. * !0 = Failure
  14661. *
  14662. * PARAMETERS:
  14663. * *dev - pointer to device structure
  14664. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14665. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14666. * *err_str - pointer to error string buffer
  14667. * *slave_detect - pointer to slave detect buffer
  14668. * *boot_err - pointer to boot_err buffer
  14669. ******************************************************************************/
  14670. static int pt_bist_slave_irq_test(struct device *dev,
  14671. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14672. u8 *slave_detect, u8 *boot_err)
  14673. {
  14674. struct pt_core_data *cd = dev_get_drvdata(dev);
  14675. u8 mode = PT_MODE_UNKNOWN;
  14676. u8 status;
  14677. u8 boot;
  14678. u8 read_buf[12];
  14679. u8 detected = 0;
  14680. u8 last_err = -1;
  14681. u16 actual_read_len;
  14682. int result = 0;
  14683. int rc = 0;
  14684. /*
  14685. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14686. * detect bits. If the DUT was in FW, entering the BL will cause an
  14687. * XRES signal which will inadvertently test the XRES net as well
  14688. */
  14689. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14690. if (rc) {
  14691. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14692. __func__, rc);
  14693. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14694. goto exit;
  14695. }
  14696. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14697. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14698. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14699. &actual_read_len);
  14700. if (!rc) {
  14701. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14702. "PIP2 STATUS");
  14703. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14704. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14705. /* Slave detect is only valid if status ok and in boot exec */
  14706. if (status == PIP2_RSP_ERR_NONE &&
  14707. boot == PIP2_STATUS_BOOT_EXEC) {
  14708. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14709. SLAVE_DETECT_MASK;
  14710. } else {
  14711. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14712. rc = -EPERM;
  14713. }
  14714. } else {
  14715. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14716. __func__);
  14717. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14718. goto exit;
  14719. }
  14720. /*
  14721. * Retrieve boot error regardless of the state of the slave detect
  14722. * bit because the IRQ could have been stuck high or low.
  14723. */
  14724. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14725. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14726. read_buf, &actual_read_len);
  14727. if (!rc) {
  14728. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14729. "PIP2 GET_LAST_ERRNO");
  14730. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14731. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14732. if (last_err) {
  14733. pt_debug(cd->dev, DL_ERROR,
  14734. "%s: Master Boot Last Err = 0x%02X\n",
  14735. __func__, last_err);
  14736. }
  14737. } else {
  14738. pt_debug(cd->dev, DL_ERROR,
  14739. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14740. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14741. }
  14742. exit:
  14743. pt_debug(cd->dev, DL_INFO,
  14744. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14745. __func__, rc, detected, last_err);
  14746. if (err_str && last_err) {
  14747. if (detected)
  14748. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14749. "- Likely stuck low. Boot Error:",
  14750. last_err);
  14751. else
  14752. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14753. "- Likely stuck high. Boot Error:",
  14754. last_err);
  14755. }
  14756. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14757. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14758. last_err = PIP2_RSP_ERR_NONE;
  14759. if (slave_irq_toggled)
  14760. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14761. if (slave_bus_toggled) {
  14762. /* Leave as UNTEST if slave not detected */
  14763. if (detected)
  14764. *slave_bus_toggled = !last_err ? true : false;
  14765. }
  14766. if (slave_detect)
  14767. *slave_detect = detected;
  14768. if (boot_err)
  14769. *boot_err = last_err;
  14770. if (slave_irq_toggled && slave_bus_toggled)
  14771. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14772. __func__,
  14773. "Detected", detected,
  14774. "slave_irq_toggled", *slave_irq_toggled,
  14775. "slave_bus_toggled", *slave_bus_toggled);
  14776. return rc;
  14777. }
  14778. /*******************************************************************************
  14779. * FUNCTION: pt_bist_slave_xres_test
  14780. *
  14781. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14782. *
  14783. * This test will ensure there is a good TP_XRES connection between the
  14784. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14785. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14786. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14787. * it's reset sentinel.
  14788. *
  14789. * RETURN:
  14790. * 0 = Success
  14791. * !0 = Failure
  14792. *
  14793. * PARAMETERS:
  14794. * *dev - pointer to device structure
  14795. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14796. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14797. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14798. * *err_str - pointer to error string buffer
  14799. ******************************************************************************/
  14800. static int pt_bist_slave_xres_test(struct device *dev,
  14801. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14802. u8 *err_str)
  14803. {
  14804. struct pt_core_data *cd = dev_get_drvdata(dev);
  14805. u8 slave_detect = 0;
  14806. u8 boot_err = 0;
  14807. int rc = 0;
  14808. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14809. mutex_lock(&cd->system_lock);
  14810. cd->hid_reset_cmd_state = 1;
  14811. mutex_unlock(&cd->system_lock);
  14812. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14813. pt_hw_hard_reset(cd);
  14814. msleep(100);
  14815. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14816. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14817. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14818. if (!rc && *slave_irq_toggled == false) {
  14819. /*
  14820. * If the slave IRQ did not toggle, either the slave_detect
  14821. * bit was not set or we had a boot error. If the slave
  14822. * detect was not set the slave did not reset causing a boot
  14823. * error.
  14824. */
  14825. if (!slave_detect)
  14826. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14827. else
  14828. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14829. "- likely open or an IRQ issue. Boot Error:",
  14830. boot_err);
  14831. }
  14832. if (slave_xres_toggled) {
  14833. if (!rc)
  14834. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14835. else
  14836. *slave_xres_toggled = false;
  14837. }
  14838. return rc;
  14839. }
  14840. /*******************************************************************************
  14841. * FUNCTION: pt_bist_slave_bus_test
  14842. *
  14843. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14844. *
  14845. * This test will ensure a good SPI bus connection between the
  14846. * master DUT and the slave DUT(s). This bus connection is ensured by
  14847. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14848. * then there is a master/slave communication issue. Opening file 0 on
  14849. * the master will open it on the slave as well if the slave was detected.
  14850. *
  14851. * RETURN:
  14852. * 0 = Success
  14853. * !0 = Failure
  14854. *
  14855. * PARAMETERS:
  14856. * *dev - pointer to device structure
  14857. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14858. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14859. * *err_str - pointer to error string buffer
  14860. ******************************************************************************/
  14861. static int pt_bist_slave_bus_test(struct device *dev,
  14862. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14863. {
  14864. struct pt_core_data *cd = dev_get_drvdata(dev);
  14865. u8 mode = PT_MODE_UNKNOWN;
  14866. u8 bus_toggled = false;
  14867. u8 file_handle;
  14868. int result = 0;
  14869. int rc = 0;
  14870. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14871. if (rc) {
  14872. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14873. __func__, rc);
  14874. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14875. goto exit;
  14876. }
  14877. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14878. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14879. if (file_handle != PIP2_RAM_FILE) {
  14880. rc = -ENOENT;
  14881. bus_toggled = false;
  14882. pt_debug(dev, DL_ERROR,
  14883. "%s Failed to open bin file\n", __func__);
  14884. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14885. goto exit;
  14886. } else {
  14887. bus_toggled = true;
  14888. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14889. pt_debug(dev, DL_ERROR,
  14890. "%s: File Close failed, file_handle=%d\n",
  14891. __func__, file_handle);
  14892. }
  14893. }
  14894. exit:
  14895. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14896. if (slave_irq_toggled)
  14897. *slave_irq_toggled = bus_toggled;
  14898. if (slave_bus_toggled)
  14899. *slave_bus_toggled = bus_toggled;
  14900. return rc;
  14901. }
  14902. /*******************************************************************************
  14903. * FUNCTION: pt_ttdl_bist_show
  14904. *
  14905. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14906. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14907. *
  14908. * NOTE: This function will reset the DUT and the startup_status bit
  14909. * mask. A pt_enum will be queued after completion.
  14910. *
  14911. * NOTE: The order of the net tests is done to optimize the time it takes
  14912. * to run. The first test is capable of verifying all nets, each subsequent
  14913. * test is only run if the previous was not able to see all nets toggle.
  14914. *
  14915. * RETURN:
  14916. * 0 = success
  14917. * !0 = failure
  14918. *
  14919. * PARAMETERS:
  14920. * *dev - pointer to device structure
  14921. * *attr - pointer to device attributes
  14922. * *buf - pointer to output buffer
  14923. ******************************************************************************/
  14924. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14925. struct device_attribute *attr, char *buf)
  14926. {
  14927. struct pt_core_data *cd = dev_get_drvdata(dev);
  14928. ssize_t ret;
  14929. char *bus_err_str = NULL;
  14930. char *irq_err_str = NULL;
  14931. char *xres_err_str = NULL;
  14932. char *slave_bus_err_str = NULL;
  14933. char *slave_irq_err_str = NULL;
  14934. char *slave_xres_err_str = NULL;
  14935. u8 tests;
  14936. int rc = 0;
  14937. int num_tests = 0;
  14938. int status = 1; /* 0 = Pass, !0 = fail */
  14939. u8 bus_toggled = 0x0F; /* default to untested */
  14940. u8 i2c_toggled = 0x0F; /* default to untested */
  14941. u8 spi_toggled = 0x0F; /* default to untested */
  14942. u8 irq_toggled = 0x0F; /* default to untested */
  14943. u8 xres_toggled = 0x0F; /* default to untested */
  14944. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14945. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14946. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14947. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14948. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14949. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14950. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14951. goto print_results;
  14952. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14953. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14954. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14955. if (cd->multi_chip) {
  14956. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14957. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14958. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14959. if (!slave_bus_err_str ||
  14960. !slave_irq_err_str ||
  14961. !slave_xres_err_str)
  14962. goto print_results;
  14963. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14964. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14965. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14966. }
  14967. /* Turn off the TTDL WD during the test */
  14968. pt_stop_wd_timer(cd);
  14969. /* Shorten default PIP cmd timeout while running BIST */
  14970. cd->pip_cmd_timeout = 200;
  14971. /* Count the number of tests to run */
  14972. tests = cd->ttdl_bist_select;
  14973. while (tests) {
  14974. num_tests += tests & 1;
  14975. tests >>= 1;
  14976. }
  14977. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14978. __func__, cd->ttdl_bist_select, num_tests);
  14979. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14980. if (cd->flashless_dut) {
  14981. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14982. __func__);
  14983. mutex_lock(&cd->system_lock);
  14984. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14985. mutex_unlock(&cd->system_lock);
  14986. }
  14987. /* --------------- TP_XRES BIST TEST --------------- */
  14988. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14989. pt_debug(dev, DL_INFO,
  14990. "%s: ----- Start TP_XRES BIST -----", __func__);
  14991. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14992. &xres_toggled, xres_err_str);
  14993. /* Done if the rest of all nets toggled */
  14994. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14995. goto host_nets_complete;
  14996. }
  14997. /* Flush bus in case a PIP response is waiting from previous test */
  14998. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14999. /* --------------- IRQ BIST TEST --------------- */
  15000. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15001. pt_debug(dev, DL_INFO,
  15002. "%s: ----- Start IRQ BIST -----", __func__);
  15003. bus_toggled = 0xFF;
  15004. irq_toggled = 0xFF;
  15005. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15006. &xres_toggled, irq_err_str);
  15007. /* If this net failed clear results from previous net */
  15008. if (irq_toggled != 1) {
  15009. xres_toggled = 0x0F;
  15010. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15011. }
  15012. if (bus_toggled == 1 && irq_toggled == 1)
  15013. goto host_nets_complete;
  15014. }
  15015. /* Flush bus in case a PIP response is waiting from previous test */
  15016. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15017. /* --------------- BUS BIST TEST --------------- */
  15018. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15019. pt_debug(dev, DL_INFO,
  15020. "%s: ----- Start BUS BIST -----", __func__);
  15021. bus_toggled = 0xFF;
  15022. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15023. /* If this net failed clear results from previous net */
  15024. if (bus_toggled == 0) {
  15025. irq_toggled = 0x0F;
  15026. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15027. }
  15028. }
  15029. host_nets_complete:
  15030. /* --------------- SLAVE XRES BIST TEST --------------- */
  15031. if (cd->multi_chip &&
  15032. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15033. pt_debug(dev, DL_INFO,
  15034. "%s: ----- Start Slave XRES BIST -----", __func__);
  15035. slave_xres_toggled = 0xFF;
  15036. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15037. &slave_bus_toggled, &slave_xres_toggled,
  15038. slave_xres_err_str);
  15039. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15040. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15041. goto print_results;
  15042. }
  15043. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15044. if (cd->multi_chip &&
  15045. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15046. pt_debug(dev, DL_INFO,
  15047. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15048. slave_irq_toggled = 0xFF;
  15049. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15050. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15051. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15052. __func__, slave_irq_toggled);
  15053. if (slave_irq_toggled == 1) {
  15054. slave_bus_toggled = 1;
  15055. goto print_results;
  15056. }
  15057. }
  15058. /* --------------- SLAVE BUS BIST TEST --------------- */
  15059. if (cd->multi_chip &&
  15060. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15061. pt_debug(dev, DL_INFO,
  15062. "%s: ----- Start Slave BUS BIST -----", __func__);
  15063. slave_bus_toggled = 0xFF;
  15064. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15065. &slave_bus_toggled, slave_bus_err_str);
  15066. }
  15067. print_results:
  15068. /* Restore PIP command timeout */
  15069. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15070. /*
  15071. * We're done! - Perform a hard XRES toggle, allowing BL
  15072. * to load FW if there is any in Flash
  15073. */
  15074. mutex_lock(&cd->system_lock);
  15075. cd->hid_reset_cmd_state = 0;
  15076. mutex_unlock(&cd->system_lock);
  15077. pt_debug(dev, DL_INFO,
  15078. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15079. if (cd->flashless_dut) {
  15080. /*
  15081. * For Flashless solution, FW update is triggered after BL is
  15082. * seen that several miliseconds delay is needed.
  15083. */
  15084. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15085. __func__);
  15086. mutex_lock(&cd->system_lock);
  15087. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15088. mutex_unlock(&cd->system_lock);
  15089. /* Reset DUT and wait 100ms to see if loader started */
  15090. pt_hw_hard_reset(cd);
  15091. msleep(100);
  15092. if (cd->fw_updating) {
  15093. pt_debug(dev, DL_INFO,
  15094. "%s: ----- BIST Wait FW Loading ----",
  15095. __func__);
  15096. rc = _pt_request_wait_for_enum_state(
  15097. dev, 4000, STARTUP_STATUS_COMPLETE);
  15098. }
  15099. } else {
  15100. if (cd->mode == PT_MODE_BOOTLOADER) {
  15101. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15102. if (rc) {
  15103. pt_debug(dev, DL_ERROR,
  15104. "%s Failed to launch app\n", __func__);
  15105. rc = pt_hw_hard_reset(cd);
  15106. }
  15107. }
  15108. /*
  15109. * If FW exists the BL may have just started or will start soon,
  15110. * so the FW sentinel may be on it's way but with no FW it will
  15111. * not arrive, wait for it before deciding if we need to queue
  15112. * an enum.
  15113. */
  15114. rc = _pt_request_wait_for_enum_state(
  15115. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15116. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15117. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15118. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15119. __func__);
  15120. pt_queue_enum(cd);
  15121. rc = _pt_request_wait_for_enum_state(
  15122. dev, 2000, STARTUP_STATUS_COMPLETE);
  15123. }
  15124. }
  15125. msleep(20);
  15126. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15127. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15128. pt_start_wd_timer(cd);
  15129. /* Canned print if any memory allocation issues */
  15130. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15131. ret = scnprintf(buf, strlen(buf),
  15132. "Status: %d\n"
  15133. "I2C (SDA,SCL): [UNTEST]\n"
  15134. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15135. "IRQ connection: [UNTEST]\n"
  15136. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15137. if (cd->multi_chip) {
  15138. ret += scnprintf(buf + ret, strlen(buf),
  15139. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15140. "I/P IRQ connection: [UNTEST]\n"
  15141. "I/P TP_XRES connection: [UNTEST]\n");
  15142. }
  15143. } else {
  15144. status = 0;
  15145. if (bus_toggled == 1)
  15146. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15147. if (irq_toggled == 1)
  15148. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15149. if (xres_toggled == 1)
  15150. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15151. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15152. status += bus_toggled;
  15153. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15154. status += irq_toggled;
  15155. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15156. status += xres_toggled;
  15157. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15158. __func__, status, bus_toggled, irq_toggled,
  15159. xres_toggled);
  15160. if (cd->multi_chip) {
  15161. if (slave_irq_toggled == 1)
  15162. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15163. if (slave_xres_toggled == 1)
  15164. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15165. if (slave_bus_toggled == 1)
  15166. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15167. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15168. status += slave_bus_toggled;
  15169. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15170. status += slave_irq_toggled;
  15171. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15172. status += slave_xres_toggled;
  15173. pt_debug(dev, DL_ERROR,
  15174. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15175. __func__, status, bus_toggled, irq_toggled,
  15176. xres_toggled, slave_bus_toggled,
  15177. slave_irq_toggled, slave_xres_toggled);
  15178. }
  15179. if (cd->bus_ops->bustype == BUS_I2C)
  15180. i2c_toggled = bus_toggled;
  15181. else
  15182. spi_toggled = bus_toggled;
  15183. ret = scnprintf(buf, strlen(buf),
  15184. "Status: %d\n"
  15185. "I2C (SDA,SCL): %s %s\n"
  15186. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15187. "IRQ connection: %s %s\n"
  15188. "TP_XRES connection: %s %s\n",
  15189. status == num_tests ? 0 : 1,
  15190. i2c_toggled == 0x0F ? "[UNTEST]" :
  15191. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15192. i2c_toggled == 0x0F ? "" : bus_err_str,
  15193. spi_toggled == 0x0F ? "[UNTEST]" :
  15194. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15195. spi_toggled == 0x0F ? "" : bus_err_str,
  15196. irq_toggled == 0x0F ? "[UNTEST]" :
  15197. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15198. irq_err_str,
  15199. xres_toggled == 0x0F ? "[UNTEST]" :
  15200. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15201. xres_err_str);
  15202. if (cd->multi_chip) {
  15203. ret += scnprintf(buf + ret, strlen(buf),
  15204. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15205. "I/P IRQ connection: %s %s\n"
  15206. "I/P TP_XRES connection: %s %s\n",
  15207. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15208. slave_bus_toggled == 1 ? "[ OK ]" :
  15209. "[FAILED]", slave_bus_err_str,
  15210. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15211. slave_irq_toggled == 1 ? "[ OK ]" :
  15212. "[FAILED]", slave_irq_err_str,
  15213. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15214. slave_xres_toggled == 1 ? "[ OK ]" :
  15215. "[FAILED]", slave_xres_err_str);
  15216. }
  15217. }
  15218. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15219. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15220. __func__, cd->startup_status);
  15221. kfree(bus_err_str);
  15222. kfree(irq_err_str);
  15223. kfree(xres_err_str);
  15224. return ret;
  15225. }
  15226. /*******************************************************************************
  15227. * FUNCTION: pt_ttdl_bist_store
  15228. *
  15229. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15230. *
  15231. * RETURN: Size of passed in buffer if successful
  15232. *
  15233. * PARAMETERS:
  15234. * *dev - pointer to device structure
  15235. * *attr - pointer to device attributes
  15236. * *buf - pointer to command buffer
  15237. * size - size of buf
  15238. ******************************************************************************/
  15239. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15240. struct device_attribute *attr, const char *buf, size_t size)
  15241. {
  15242. struct pt_core_data *cd = dev_get_drvdata(dev);
  15243. u32 input_data[2] = {0};
  15244. int length;
  15245. int rc = 0;
  15246. /* Maximum input of one value */
  15247. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15248. ARRAY_SIZE(input_data));
  15249. if (length != 1) {
  15250. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15251. __func__);
  15252. rc = -EINVAL;
  15253. goto exit;
  15254. } else {
  15255. mutex_lock(&cd->system_lock);
  15256. cd->ttdl_bist_select = input_data[0];
  15257. mutex_unlock(&cd->system_lock);
  15258. }
  15259. exit:
  15260. if (rc)
  15261. return rc;
  15262. return size;
  15263. }
  15264. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15265. pt_ttdl_bist_store);
  15266. /*******************************************************************************
  15267. * FUNCTION: pt_flush_bus_store
  15268. *
  15269. * SUMMARY: Store method for the flush_bus sysfs node.
  15270. *
  15271. * RETURN: Size of passed in buffer if successful
  15272. *
  15273. * PARAMETERS:
  15274. * *dev - pointer to device structure
  15275. * *attr - pointer to device attributes
  15276. * *buf - pointer to command buffer
  15277. * size - size of buf
  15278. ******************************************************************************/
  15279. static ssize_t pt_flush_bus_store(struct device *dev,
  15280. struct device_attribute *attr, const char *buf, size_t size)
  15281. {
  15282. struct pt_core_data *cd = dev_get_drvdata(dev);
  15283. u32 input_data[2] = {0};
  15284. int length;
  15285. int rc = 0;
  15286. /* Maximum input of one value */
  15287. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15288. ARRAY_SIZE(input_data));
  15289. if (length != 1) {
  15290. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15291. __func__);
  15292. rc = -EINVAL;
  15293. goto exit;
  15294. }
  15295. mutex_lock(&cd->system_lock);
  15296. if (input_data[0] == 0)
  15297. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15298. else if (input_data[0] == 1)
  15299. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15300. else
  15301. rc = -EINVAL;
  15302. mutex_unlock(&cd->system_lock);
  15303. exit:
  15304. if (rc)
  15305. return rc;
  15306. return size;
  15307. }
  15308. /*******************************************************************************
  15309. * FUNCTION: pt_flush_bus_show
  15310. *
  15311. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15312. * based on either the size of the first two bytes or a blind 256 bytes.
  15313. *
  15314. * RETURN:
  15315. * 0 = success
  15316. * !0 = failure
  15317. *
  15318. * PARAMETERS:
  15319. * *dev - pointer to device structure
  15320. * *attr - pointer to device attributes
  15321. * *buf - pointer to output buffer
  15322. ******************************************************************************/
  15323. static ssize_t pt_flush_bus_show(struct device *dev,
  15324. struct device_attribute *attr, char *buf)
  15325. {
  15326. ssize_t ret = 0;
  15327. ssize_t bytes = 0;
  15328. struct pt_core_data *cd = dev_get_drvdata(dev);
  15329. mutex_lock(&cd->system_lock);
  15330. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15331. ret = scnprintf(buf, strlen(buf),
  15332. "Status: 0\n"
  15333. "%s: %zd\n",
  15334. "Bytes flushed", bytes);
  15335. mutex_unlock(&cd->system_lock);
  15336. return ret;
  15337. }
  15338. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15339. pt_flush_bus_store);
  15340. /*******************************************************************************
  15341. * FUNCTION: pt_pip2_ping_test_store
  15342. *
  15343. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15344. *
  15345. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15346. * someone may want to test sending invalid packet lengths so any values
  15347. * up to 255 are allowed.
  15348. *
  15349. * RETURN: Size of passed in buffer if successful
  15350. *
  15351. * PARAMETERS:
  15352. * *dev - pointer to device structure
  15353. * *attr - pointer to device attributes
  15354. * *buf - pointer to command buffer
  15355. * size - size of buf
  15356. ******************************************************************************/
  15357. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15358. struct device_attribute *attr, const char *buf, size_t size)
  15359. {
  15360. struct pt_core_data *cd = dev_get_drvdata(dev);
  15361. u32 input_data[2];
  15362. int length;
  15363. int rc = 0;
  15364. /* Maximum input of one value */
  15365. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15366. ARRAY_SIZE(input_data));
  15367. if (length != 1) {
  15368. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15369. __func__);
  15370. rc = -EINVAL;
  15371. goto exit;
  15372. }
  15373. mutex_lock(&cd->system_lock);
  15374. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15375. cd->ping_test_size = input_data[0];
  15376. else
  15377. rc = -EINVAL;
  15378. mutex_unlock(&cd->system_lock);
  15379. exit:
  15380. if (rc)
  15381. return rc;
  15382. return size;
  15383. }
  15384. /*******************************************************************************
  15385. * FUNCTION: pt_ping_test_show
  15386. *
  15387. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15388. * PING command and ramps up the optional payload from 0 to
  15389. * ping_test_size.
  15390. * The max payload size is 247:
  15391. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15392. *
  15393. * RETURN:
  15394. * 0 = success
  15395. * !0 = failure
  15396. *
  15397. * PARAMETERS:
  15398. * *dev - pointer to device structure
  15399. * *attr - pointer to device attributes
  15400. * *buf - pointer to output buffer
  15401. ******************************************************************************/
  15402. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15403. struct device_attribute *attr, char *buf)
  15404. {
  15405. struct pt_core_data *cd = dev_get_drvdata(dev);
  15406. ssize_t ret;
  15407. int last_packet_size;
  15408. int rc = 0;
  15409. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15410. if (rc) {
  15411. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15412. return ret;
  15413. }
  15414. ret = scnprintf(buf, strlen(buf),
  15415. "Status: %d\n"
  15416. "PING payload test passed with packet sizes 0 - %d\n",
  15417. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15418. last_packet_size);
  15419. return ret;
  15420. }
  15421. /*******************************************************************************
  15422. * FUNCTION: pt_t_refresh_store
  15423. *
  15424. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15425. * in integer as the number of interrupts to count. A timer is started to
  15426. * calculate the total time it takes to see that number of interrupts.
  15427. *
  15428. * RETURN: Size of passed in buffer if successful
  15429. *
  15430. * PARAMETERS:
  15431. * *dev - pointer to device structure
  15432. * *attr - pointer to device attributes
  15433. * *buf - pointer to buffer that hold the command parameters
  15434. * size - size of buf
  15435. ******************************************************************************/
  15436. static ssize_t pt_t_refresh_store(struct device *dev,
  15437. struct device_attribute *attr, const char *buf, size_t size)
  15438. {
  15439. struct pt_core_data *cd = dev_get_drvdata(dev);
  15440. u32 input_data[2];
  15441. int length;
  15442. int rc = 0;
  15443. /* Maximum input of one value */
  15444. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15445. ARRAY_SIZE(input_data));
  15446. if (length != 1) {
  15447. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15448. __func__);
  15449. rc = -EINVAL;
  15450. goto exit;
  15451. }
  15452. mutex_lock(&cd->system_lock);
  15453. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15454. input_data[0]);
  15455. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15456. cd->t_refresh_total = input_data[0];
  15457. cd->t_refresh_count = 0;
  15458. cd->t_refresh_active = 1;
  15459. } else {
  15460. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15461. rc = -EINVAL;
  15462. }
  15463. mutex_unlock(&cd->system_lock);
  15464. exit:
  15465. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15466. if (rc)
  15467. return rc;
  15468. return size;
  15469. }
  15470. /*******************************************************************************
  15471. * FUNCTION: pt_t_refresh_show
  15472. *
  15473. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15474. * of the T-Refresh timer counting the time it takes to see a user defined
  15475. * number of interrupts.
  15476. *
  15477. * RETURN:
  15478. * 0 = success
  15479. * !0 = failure
  15480. *
  15481. * PARAMETERS:
  15482. * *dev - pointer to device structure
  15483. * *attr - pointer to device attributes
  15484. * *buf - pointer to output buffer
  15485. ******************************************************************************/
  15486. static ssize_t pt_t_refresh_show(struct device *dev,
  15487. struct device_attribute *attr, char *buf)
  15488. {
  15489. ssize_t ret = 0;
  15490. struct pt_core_data *cd = dev_get_drvdata(dev);
  15491. u32 whole;
  15492. u16 fraction;
  15493. mutex_lock(&cd->system_lock);
  15494. /* Check if we have counted the number requested */
  15495. if (cd->t_refresh_count != cd->t_refresh_total) {
  15496. ret = scnprintf(buf, strlen(buf),
  15497. "Status: 0\n"
  15498. "%s: %d\n",
  15499. "Still counting... current IRQ count",
  15500. cd->t_refresh_count);
  15501. } else {
  15502. /* Ensure T-Refresh is de-activated */
  15503. cd->t_refresh_active = 0;
  15504. whole = cd->t_refresh_time / cd->t_refresh_count;
  15505. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15506. fraction = fraction * 1000 / cd->t_refresh_count;
  15507. ret = scnprintf(buf, strlen(buf),
  15508. "Status: 0\n"
  15509. "%s: %d\n"
  15510. "%s: %d\n"
  15511. "%s: %d\n"
  15512. "%s: %d.%02d\n",
  15513. "Requested IRQ Count ", cd->t_refresh_total,
  15514. "IRQ Counted ", cd->t_refresh_count,
  15515. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15516. "Average T-Refresh (ms) ", whole, fraction);
  15517. }
  15518. mutex_unlock(&cd->system_lock);
  15519. return ret;
  15520. }
  15521. /*******************************************************************************
  15522. * FUNCTION: pt_dut_status_show
  15523. *
  15524. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15525. * more items such as operation mode,easywake state are added in the future.
  15526. *
  15527. * RETURN: Char buffer with printed scan status information
  15528. *
  15529. * PARAMETERS:
  15530. * *dev - pointer to device structure
  15531. * *attr - pointer to device attributes
  15532. * *buf - pointer to output buffer
  15533. ******************************************************************************/
  15534. static ssize_t pt_dut_status_show(struct device *dev,
  15535. struct device_attribute *attr, char *buf)
  15536. {
  15537. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15538. u8 mode = PT_MODE_UNKNOWN;
  15539. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15540. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15541. struct pt_core_data *cd = dev_get_drvdata(dev);
  15542. ssize_t ret;
  15543. u16 calculated_crc = 0;
  15544. u16 stored_crc = 0;
  15545. u8 status;
  15546. int rc = 0;
  15547. /* In STANDBY the DUT will not repond to any PIP cmd */
  15548. if (cd->fw_sys_mode_in_standby_state) {
  15549. mode = PT_MODE_OPERATIONAL;
  15550. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15551. goto print_limited_results;
  15552. }
  15553. /* Retrieve mode and FW system mode which can only be 0-4 */
  15554. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15555. if (rc || mode == PT_MODE_UNKNOWN) {
  15556. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15557. "%s: %d\n"
  15558. "%s: n/a\n"
  15559. "%s: n/a\n"
  15560. "%s: n/a\n"
  15561. "%s: n/a\n",
  15562. "Status", rc,
  15563. "Active Exec ",
  15564. "FW System Mode ",
  15565. "Stored CRC ",
  15566. "Calculated CRC ");
  15567. return ret;
  15568. } else {
  15569. if (mode == PT_MODE_OPERATIONAL) {
  15570. if (sys_mode > FW_SYS_MODE_MAX)
  15571. sys_mode = FW_SYS_MODE_UNDEFINED;
  15572. if (sys_mode != FW_SYS_MODE_TEST)
  15573. goto print_limited_results;
  15574. rc = pt_pip_verify_config_block_crc_(cd,
  15575. PT_TCH_PARM_EBID, &status,
  15576. &calculated_crc, &stored_crc);
  15577. if (rc)
  15578. goto print_limited_results;
  15579. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15580. "%s: %d\n"
  15581. "%s: %s\n"
  15582. "%s: %s\n"
  15583. "%s: 0x%04X\n"
  15584. "%s: 0x%04X\n",
  15585. "Status", rc,
  15586. "Active Exec ", "FW",
  15587. "FW System Mode ", outputstring[sys_mode],
  15588. "Stored CRC ", stored_crc,
  15589. "Calculated CRC ", calculated_crc);
  15590. return ret;
  15591. } else {
  15592. /* When in BL or unknon mode Active Exec is "n/a" */
  15593. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15594. }
  15595. }
  15596. print_limited_results:
  15597. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15598. "%s: %d\n"
  15599. "%s: %s\n"
  15600. "%s: %s\n"
  15601. "%s: n/a\n"
  15602. "%s: n/a\n",
  15603. "Status", rc,
  15604. "Active Exec ",
  15605. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15606. "FW System Mode ", outputstring[sys_mode],
  15607. "Stored CRC ",
  15608. "Calculated CRC ");
  15609. return ret;
  15610. }
  15611. #endif /* TTDL_DIAGNOSTICS */
  15612. /*******************************************************************************
  15613. * Structures of sysfs attributes for all DUT dependent sysfs node
  15614. ******************************************************************************/
  15615. static struct attribute *early_attrs[] = {
  15616. &dev_attr_hw_version.attr,
  15617. &dev_attr_drv_version.attr,
  15618. &dev_attr_drv_ver.attr,
  15619. &dev_attr_fw_version.attr,
  15620. &dev_attr_sysinfo.attr,
  15621. &dev_attr_pip2_cmd_rsp.attr,
  15622. &dev_attr_command.attr,
  15623. &dev_attr_drv_debug.attr,
  15624. &dev_attr_hw_reset.attr,
  15625. &dev_attr_response.attr,
  15626. &dev_attr_ttdl_restart.attr,
  15627. #ifdef TTDL_DIAGNOSTICS
  15628. &dev_attr_ttdl_status.attr,
  15629. &dev_attr_pip2_enter_bl.attr,
  15630. &dev_attr_pip2_exit_bl.attr,
  15631. &dev_attr_err_gpio.attr,
  15632. &dev_attr_flush_bus.attr,
  15633. &dev_attr_ttdl_bist.attr,
  15634. #endif /* TTDL_DIAGNOSTICS */
  15635. NULL,
  15636. };
  15637. static struct attribute_group early_attr_group = {
  15638. .attrs = early_attrs,
  15639. };
  15640. static struct device_attribute pip2_attributes[] = {
  15641. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15642. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15643. #ifdef TTDL_DIAGNOSTICS
  15644. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15645. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15646. pt_pip2_ping_test_store),
  15647. #endif
  15648. };
  15649. static struct device_attribute attributes[] = {
  15650. __ATTR(dut_debug, 0644,
  15651. pt_dut_debug_show, pt_drv_debug_store),
  15652. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15653. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15654. __ATTR(get_param, 0644,
  15655. pt_get_param_show, pt_get_param_store),
  15656. #ifdef EASYWAKE_TSG6
  15657. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15658. pt_easy_wakeup_gesture_store),
  15659. __ATTR(easy_wakeup_gesture_id, 0444,
  15660. pt_easy_wakeup_gesture_id_show, NULL),
  15661. __ATTR(easy_wakeup_gesture_data, 0444,
  15662. pt_easy_wakeup_gesture_data_show, NULL),
  15663. #endif
  15664. #ifdef TTDL_DIAGNOSTICS
  15665. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15666. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15667. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15668. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15669. #endif /* TTDL_DIAGNOSTICS */
  15670. };
  15671. /*******************************************************************************
  15672. * FUNCTION: add_sysfs_interfaces
  15673. *
  15674. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15675. *
  15676. * RETURN:
  15677. * 0 = success
  15678. * !0 = failure
  15679. *
  15680. * PARAMETERS:
  15681. * *dev - pointer to device structure
  15682. ******************************************************************************/
  15683. static int add_sysfs_interfaces(struct device *dev)
  15684. {
  15685. int i;
  15686. int j = 0;
  15687. struct pt_core_data *cd = dev_get_drvdata(dev);
  15688. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15689. if (device_create_file(dev, attributes + i))
  15690. goto undo;
  15691. }
  15692. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15693. __func__, cd->active_dut_generation);
  15694. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15695. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15696. if (device_create_file(dev, pip2_attributes + j))
  15697. goto undo;
  15698. }
  15699. }
  15700. return 0;
  15701. undo:
  15702. for (i--; i >= 0; i--)
  15703. device_remove_file(dev, attributes + i);
  15704. for (j--; j >= 0; j--)
  15705. device_remove_file(dev, pip2_attributes + j);
  15706. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15707. __func__);
  15708. return -ENODEV;
  15709. }
  15710. /*******************************************************************************
  15711. * FUNCTION: remove_sysfs_interfaces
  15712. *
  15713. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15714. *
  15715. * RETURN: void
  15716. *
  15717. * PARAMETERS:
  15718. * *dev - pointer to device structure
  15719. ******************************************************************************/
  15720. static void remove_sysfs_interfaces(struct device *dev)
  15721. {
  15722. int i;
  15723. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15724. device_remove_file(dev, attributes + i);
  15725. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15726. device_remove_file(dev, pip2_attributes + i);
  15727. }
  15728. /*******************************************************************************
  15729. * FUNCTION: remove_sysfs_and_modules
  15730. *
  15731. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15732. *
  15733. * RETURN: void
  15734. *
  15735. * PARAMETERS:
  15736. * *dev - pointer to device structure
  15737. ******************************************************************************/
  15738. static void remove_sysfs_and_modules(struct device *dev)
  15739. {
  15740. struct pt_core_data *cd = dev_get_drvdata(dev);
  15741. /* Queued work should be removed before to release loader module */
  15742. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15743. pt_release_modules(cd);
  15744. pt_btn_release(dev);
  15745. pt_mt_release(dev);
  15746. remove_sysfs_interfaces(dev);
  15747. }
  15748. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15749. {
  15750. int retval;
  15751. /* Get pinctrl if target uses pinctrl */
  15752. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15753. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15754. retval = PTR_ERR(cd->ts_pinctrl);
  15755. dev_dbg(cd->dev,
  15756. "Target does not use pinctrl %d\n", retval);
  15757. goto err_pinctrl_get;
  15758. }
  15759. cd->pinctrl_state_active
  15760. = pinctrl_lookup_state(cd->ts_pinctrl,
  15761. PINCTRL_STATE_ACTIVE);
  15762. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15763. retval = PTR_ERR(cd->pinctrl_state_active);
  15764. dev_err(cd->dev,
  15765. "Can not lookup %s pinstate %d\n",
  15766. PINCTRL_STATE_ACTIVE, retval);
  15767. goto err_pinctrl_lookup;
  15768. }
  15769. cd->pinctrl_state_suspend
  15770. = pinctrl_lookup_state(cd->ts_pinctrl,
  15771. PINCTRL_STATE_SUSPEND);
  15772. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15773. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15774. dev_err(cd->dev,
  15775. "Can not lookup %s pinstate %d\n",
  15776. PINCTRL_STATE_SUSPEND, retval);
  15777. goto err_pinctrl_lookup;
  15778. }
  15779. cd->pinctrl_state_release
  15780. = pinctrl_lookup_state(cd->ts_pinctrl,
  15781. PINCTRL_STATE_RELEASE);
  15782. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15783. retval = PTR_ERR(cd->pinctrl_state_release);
  15784. dev_dbg(cd->dev,
  15785. "Can not lookup %s pinstate %d\n",
  15786. PINCTRL_STATE_RELEASE, retval);
  15787. }
  15788. return 0;
  15789. err_pinctrl_lookup:
  15790. devm_pinctrl_put(cd->ts_pinctrl);
  15791. err_pinctrl_get:
  15792. cd->ts_pinctrl = NULL;
  15793. return retval;
  15794. }
  15795. /*******************************************************************************
  15796. *******************************************************************************
  15797. * FUNCTION: pt_probe
  15798. *
  15799. * SUMMARY: Probe of the core module.
  15800. *
  15801. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15802. * probe functionality is split into two functions; pt_probe() and
  15803. * pt_probe_complete(). the initial setup is done in this function which
  15804. * then creates a WORK task which runs after the probe timer expires. This
  15805. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15806. *
  15807. * RETURN:
  15808. * 0 = success
  15809. * !0 = failure
  15810. *
  15811. * PARAMETERS:
  15812. * *ops - pointer to the bus
  15813. * *dev - pointer to the device structure
  15814. * irq - IRQ
  15815. * xfer_buf_size - size of the buffer
  15816. ******************************************************************************/
  15817. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15818. u16 irq, size_t xfer_buf_size)
  15819. {
  15820. struct pt_core_data *cd;
  15821. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15822. enum pt_atten_type type;
  15823. struct i2c_client *client = to_i2c_client(dev);
  15824. int rc = 0;
  15825. u8 pip_ver_major;
  15826. u8 pip_ver_minor;
  15827. u32 status = STARTUP_STATUS_START;
  15828. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15829. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15830. __func__);
  15831. rc = -ENODEV;
  15832. goto error_no_pdata;
  15833. }
  15834. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15835. if (!pdata->core_pdata->power) {
  15836. pt_debug(dev, DL_ERROR,
  15837. "%s: Missing platform data function\n",
  15838. __func__);
  15839. rc = -ENODEV;
  15840. goto error_no_pdata;
  15841. }
  15842. }
  15843. /* get context and debug print buffers */
  15844. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15845. if (!cd) {
  15846. rc = -ENOMEM;
  15847. goto error_no_pdata;
  15848. }
  15849. /* Initialize device info */
  15850. cd->dev = dev;
  15851. cd->pdata = pdata;
  15852. cd->cpdata = pdata->core_pdata;
  15853. cd->bus_ops = ops;
  15854. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15855. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15856. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15857. cd->hw_detected = false;
  15858. cd->pip2_prot_active = false;
  15859. cd->pip2_send_user_cmd = false;
  15860. cd->bl_pip_ver_ready = false;
  15861. cd->app_pip_ver_ready = false;
  15862. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15863. cd->get_param_id = 0;
  15864. cd->watchdog_enabled = 0;
  15865. cd->startup_retry_count = 0;
  15866. cd->core_probe_complete = 0;
  15867. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15868. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15869. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15870. cd->flashless_dut = 0;
  15871. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15872. cd->bl_with_no_int = 0;
  15873. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15874. cd->multi_chip = PT_FEATURE_DISABLE;
  15875. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15876. cd->sleep_state = SS_SLEEP_NONE;
  15877. cd->quick_boot = false;
  15878. cd->drv_debug_suspend = false;
  15879. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15880. cd->set_dut_generation = true;
  15881. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15882. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15883. cd->set_dut_generation = true;
  15884. cd->active_dut_generation = DUT_PIP1_ONLY;
  15885. } else {
  15886. cd->set_dut_generation = false;
  15887. cd->active_dut_generation = DUT_UNKNOWN;
  15888. }
  15889. /* Initialize with platform data */
  15890. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15891. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15892. cd->hid_cmd_state = 1;
  15893. cd->fw_updating = false;
  15894. cd->multi_chip = 0;
  15895. #ifdef TTDL_DIAGNOSTICS
  15896. cd->t_refresh_active = 0;
  15897. cd->t_refresh_count = 0;
  15898. cd->pip2_crc_error_count = 0;
  15899. cd->wd_xres_count = 0;
  15900. cd->bl_retry_packet_count = 0;
  15901. cd->file_erase_timeout_count = 0;
  15902. cd->show_tt_data = false;
  15903. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15904. cd->err_gpio = 0;
  15905. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15906. cd->ttdl_bist_select = 0x07;
  15907. cd->force_pip2_seq = 0;
  15908. #endif /* TTDL_DIAGNOSTICS */
  15909. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15910. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15911. sizeof(PT_PIP2_BIN_FILE_PATH));
  15912. pt_init_hid_descriptor(&cd->hid_desc);
  15913. /* Read and store the descriptor lengths */
  15914. cd->hid_core.hid_report_desc_len =
  15915. le16_to_cpu(cd->hid_desc.report_desc_len);
  15916. cd->hid_core.hid_max_input_len =
  15917. le16_to_cpu(cd->hid_desc.max_input_len);
  15918. cd->hid_core.hid_max_output_len =
  15919. le16_to_cpu(cd->hid_desc.max_output_len);
  15920. /* Initialize mutexes and spinlocks */
  15921. mutex_init(&cd->module_list_lock);
  15922. mutex_init(&cd->system_lock);
  15923. mutex_init(&cd->sysfs_lock);
  15924. mutex_init(&cd->ttdl_restart_lock);
  15925. mutex_init(&cd->firmware_class_lock);
  15926. spin_lock_init(&cd->spinlock);
  15927. /* Initialize module list */
  15928. INIT_LIST_HEAD(&cd->module_list);
  15929. /* Initialize attention lists */
  15930. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15931. INIT_LIST_HEAD(&cd->atten_list[type]);
  15932. /* Initialize parameter list */
  15933. INIT_LIST_HEAD(&cd->param_list);
  15934. /* Initialize wait queue */
  15935. init_waitqueue_head(&cd->wait_q);
  15936. rc = pt_ts_pinctrl_init(cd);
  15937. if (!rc && cd->ts_pinctrl) {
  15938. /*
  15939. * Pinctrl handle is optional. If pinctrl handle is found
  15940. * let pins to be configured in active state. If not
  15941. * found continue further without error.
  15942. */
  15943. rc = pinctrl_select_state(cd->ts_pinctrl,
  15944. cd->pinctrl_state_active);
  15945. if (rc < 0)
  15946. dev_err(&client->dev, "failed to select pin to active state\n");
  15947. }
  15948. rc = pt_get_regulator(cd, true);
  15949. if (rc) {
  15950. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15951. goto error_alloc_data;
  15952. }
  15953. rc = pt_enable_regulator(cd, true);
  15954. if (rc) {
  15955. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15956. goto error_get_regulator;
  15957. }
  15958. /* Initialize works */
  15959. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15960. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15961. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15962. /* Initialize HID specific data */
  15963. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15964. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15965. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15966. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15967. cd->hid_core.hid_desc_register =
  15968. cpu_to_le16(cd->cpdata->hid_desc_register);
  15969. /* Set platform easywake value */
  15970. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15971. #ifdef CONFIG_DRM
  15972. /* Setup active dsi panel */
  15973. active_panel = cd->cpdata->active_panel;
  15974. #endif
  15975. /* Set platform panel_id value */
  15976. cd->panel_id_support = cd->cpdata->panel_id_support;
  15977. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15978. /* Set Panel ID to default to 0 */
  15979. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15980. else
  15981. /* Set Panel ID to Not Enabled */
  15982. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15983. /* Initialize hw_version default to FFFF.FFFF.FF */
  15984. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15985. dev_set_drvdata(dev, cd);
  15986. /* PtSBC builds will call this function in pt_probe_complete() */
  15987. pt_add_core(dev);
  15988. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15989. if (rc) {
  15990. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15991. __func__);
  15992. goto error_enable_regulator;
  15993. }
  15994. /*
  15995. * Save the pointer to a global value, which will be used
  15996. * in ttdl_restart function
  15997. */
  15998. cd->bus_ops = ops;
  15999. /*
  16000. * When the IRQ GPIO is not direclty accessible and no function is
  16001. * defined to get the IRQ status, the IRQ passed in must be assigned
  16002. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16003. */
  16004. if (!cd->cpdata->irq_stat) {
  16005. cd->irq = irq;
  16006. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16007. __func__, cd->irq);
  16008. }
  16009. /* Call platform init function before setting up the GPIO's */
  16010. if (cd->cpdata->init) {
  16011. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16012. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16013. } else {
  16014. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16015. __func__);
  16016. rc = 0;
  16017. }
  16018. if (rc < 0) {
  16019. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16020. __func__, rc);
  16021. }
  16022. /* Power on any needed regulator(s) */
  16023. if (cd->cpdata->setup_power) {
  16024. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16025. rc = cd->cpdata->setup_power(cd->cpdata,
  16026. PT_MT_POWER_ON, cd->dev);
  16027. } else {
  16028. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16029. __func__);
  16030. rc = 0;
  16031. }
  16032. if (rc < 0)
  16033. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16034. __func__, rc);
  16035. #ifdef TTDL_DIAGNOSTICS
  16036. cd->watchdog_irq_stuck_count = 0;
  16037. cd->bus_transmit_error_count = 0;
  16038. #endif /* TTDL_DIAGNOSTICS */
  16039. if (cd->cpdata->detect) {
  16040. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16041. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16042. pt_platform_detect_read);
  16043. if (!rc) {
  16044. cd->hw_detected = true;
  16045. pt_debug(cd->dev, DL_INFO,
  16046. "%s: HW detected\n", __func__);
  16047. } else {
  16048. cd->hw_detected = false;
  16049. pt_debug(cd->dev, DL_INFO,
  16050. "%s: No HW detected\n", __func__);
  16051. rc = -ENODEV;
  16052. goto error_detect;
  16053. }
  16054. } else {
  16055. pt_debug(dev, DL_ERROR,
  16056. "%s: PARADE No HW detect function pointer\n",
  16057. __func__);
  16058. /*
  16059. * "hw_reset" is not needed in the "if" statement,
  16060. * because "hw_reset" is already included in "hw_detect"
  16061. * function.
  16062. */
  16063. rc = pt_hw_hard_reset(cd);
  16064. if (rc)
  16065. pt_debug(cd->dev, DL_ERROR,
  16066. "%s: FAILED to execute HARD reset\n",
  16067. __func__);
  16068. }
  16069. if (cd->cpdata->setup_irq) {
  16070. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16071. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16072. if (rc) {
  16073. pt_debug(dev, DL_ERROR,
  16074. "%s: Error, couldn't setup IRQ\n", __func__);
  16075. goto error_setup_irq;
  16076. }
  16077. } else {
  16078. pt_debug(dev, DL_ERROR,
  16079. "%s: IRQ function pointer not setup\n",
  16080. __func__);
  16081. goto error_setup_irq;
  16082. }
  16083. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16084. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16085. (unsigned long)cd);
  16086. #else
  16087. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16088. #endif
  16089. pt_stop_wd_timer(cd);
  16090. #ifdef TTHE_TUNER_SUPPORT
  16091. mutex_init(&cd->tthe_lock);
  16092. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16093. 0644, NULL, cd, &tthe_debugfs_fops);
  16094. #endif
  16095. rc = device_init_wakeup(dev, 1);
  16096. if (rc < 0)
  16097. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16098. __func__, rc);
  16099. if (!enable_irq_wake(cd->irq)) {
  16100. cd->irq_wake = 1;
  16101. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16102. }
  16103. pm_runtime_get_noresume(dev);
  16104. pm_runtime_set_active(dev);
  16105. pm_runtime_enable(dev);
  16106. /* If IRQ asserted, read out all from buffer to release INT pin */
  16107. if (cd->cpdata->irq_stat) {
  16108. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16109. } else {
  16110. /* Force a read in case the reset sentinel already arrived */
  16111. rc = pt_read_input(cd);
  16112. if (!rc)
  16113. pt_parse_input(cd);
  16114. }
  16115. /* Without sleep DUT is not ready and will NAK the first write */
  16116. msleep(150);
  16117. /* Attempt to set the DUT generation if not yet set */
  16118. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16119. if (cd->bl_pip_ver_ready ||
  16120. (cd->app_pip_ver_ready &&
  16121. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16122. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16123. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16124. __func__, cd->active_dut_generation);
  16125. } else {
  16126. rc = _pt_detect_dut_generation(cd->dev,
  16127. &status, &cd->active_dut_generation,
  16128. &cd->mode);
  16129. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16130. || rc) {
  16131. pt_debug(cd->dev, DL_ERROR,
  16132. " === DUT Gen unknown, Skip Enum ===\n");
  16133. goto skip_enum;
  16134. }
  16135. }
  16136. }
  16137. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16138. &pip_ver_major, &pip_ver_minor);
  16139. if (pip_ver_major == 2) {
  16140. cd->bl_pip_ver_ready = true;
  16141. pt_debug(dev, DL_ERROR,
  16142. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16143. pip_ver_minor);
  16144. cd->hw_detected = true;
  16145. } else if (pip_ver_major == 1) {
  16146. cd->app_pip_ver_ready = true;
  16147. pt_debug(dev, DL_ERROR,
  16148. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16149. cd->hw_detected = true;
  16150. } else {
  16151. cd->sysinfo.ttdata.pip_ver_major = 0;
  16152. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16153. cd->app_pip_ver_ready = false;
  16154. cd->hw_detected = false;
  16155. pt_debug(dev, DL_ERROR,
  16156. " === PIP Version Not Detected, Skip Enum ===\n");
  16157. /* For legacy DUTS proceed, enum will attempt to launch app */
  16158. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16159. goto skip_enum;
  16160. }
  16161. rc = pt_enum_with_dut(cd, false, &status);
  16162. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16163. __func__, cd->startup_status, status);
  16164. if (rc == -ENODEV) {
  16165. pt_debug(cd->dev, DL_ERROR,
  16166. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16167. /* For PtSBC don't error out, allow TTDL to stay up */
  16168. rc = -EPROBE_DEFER;
  16169. goto error_after_startup;
  16170. }
  16171. /* Suspend scanning until probe is complete to avoid asyc touches */
  16172. pt_pip_suspend_scanning_(cd);
  16173. if (cd->hw_detected) {
  16174. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16175. __func__);
  16176. rc = add_sysfs_interfaces(dev);
  16177. if (rc < 0) {
  16178. pt_debug(dev, DL_ERROR,
  16179. "%s: Error, fail sysfs init\n", __func__);
  16180. goto error_after_startup;
  16181. }
  16182. } else {
  16183. pt_debug(dev, DL_ERROR,
  16184. "%s: No HW detected, sysfs interfaces not added\n",
  16185. __func__);
  16186. }
  16187. skip_enum:
  16188. pm_runtime_put_sync(dev);
  16189. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16190. rc = pt_mt_probe(dev);
  16191. if (rc < 0) {
  16192. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16193. __func__);
  16194. goto error_after_sysfs_create;
  16195. }
  16196. rc = pt_btn_probe(dev);
  16197. if (rc < 0) {
  16198. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16199. __func__);
  16200. goto error_after_startup_mt;
  16201. }
  16202. pt_probe_modules(cd);
  16203. #ifdef CONFIG_HAS_EARLYSUSPEND
  16204. pt_setup_early_suspend(cd);
  16205. #elif defined(CONFIG_DRM)
  16206. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16207. pt_setup_drm_notifier(cd);
  16208. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16209. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16210. #elif defined(CONFIG_FB)
  16211. pt_setup_fb_notifier(cd);
  16212. #endif
  16213. #ifdef NEED_SUSPEND_NOTIFIER
  16214. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16215. register_pm_notifier(&cd->pm_notifier);
  16216. #endif
  16217. pt_pip_resume_scanning_(cd);
  16218. mutex_lock(&cd->system_lock);
  16219. cd->startup_status |= status;
  16220. cd->core_probe_complete = 1;
  16221. mutex_unlock(&cd->system_lock);
  16222. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16223. __func__);
  16224. return 0;
  16225. error_after_startup_mt:
  16226. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16227. pt_mt_release(dev);
  16228. error_after_sysfs_create:
  16229. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16230. pm_runtime_disable(dev);
  16231. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16232. device_wakeup_disable(dev);
  16233. #endif
  16234. device_init_wakeup(dev, 0);
  16235. cancel_work_sync(&cd->enum_work);
  16236. pt_stop_wd_timer(cd);
  16237. pt_free_si_ptrs(cd);
  16238. remove_sysfs_interfaces(dev);
  16239. error_after_startup:
  16240. pr_err("%s PARADE error_after_startup\n", __func__);
  16241. del_timer(&cd->watchdog_timer);
  16242. if (cd->cpdata->setup_irq)
  16243. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16244. error_setup_irq:
  16245. error_detect:
  16246. if (cd->cpdata->init)
  16247. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16248. if (cd->cpdata->setup_power)
  16249. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16250. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16251. error_enable_regulator:
  16252. pt_del_core(dev);
  16253. dev_set_drvdata(dev, NULL);
  16254. pt_enable_regulator(cd, false);
  16255. error_get_regulator:
  16256. pt_get_regulator(cd, false);
  16257. error_alloc_data:
  16258. kfree(cd);
  16259. error_no_pdata:
  16260. pr_err("%s failed.\n", __func__);
  16261. return rc;
  16262. }
  16263. EXPORT_SYMBOL_GPL(pt_probe);
  16264. /*******************************************************************************
  16265. * FUNCTION: pt_release
  16266. *
  16267. * SUMMARY: This function does the following cleanup:
  16268. * - Releases all probed modules
  16269. * - Stops the watchdog
  16270. * - Cancels all pending work tasks
  16271. * - Removes all created sysfs nodes
  16272. * - Removes all created debugfs nodes
  16273. * - Frees the IRQ
  16274. * - Deletes the core
  16275. * - Frees all pointers and HID reports
  16276. *
  16277. * RETURN:
  16278. * 0 = success
  16279. * !0 = failure
  16280. *
  16281. * PARAMETERS:
  16282. * *cd - pointer to the core data structure
  16283. ******************************************************************************/
  16284. int pt_release(struct pt_core_data *cd)
  16285. {
  16286. struct device *dev = cd->dev;
  16287. /*
  16288. * Suspend the device before freeing the startup_work and stopping
  16289. * the watchdog since sleep function restarts watchdog on failure
  16290. */
  16291. pm_runtime_suspend(dev);
  16292. pm_runtime_disable(dev);
  16293. /*
  16294. * Any 'work' that can trigger a new thread should be canceled first.
  16295. * The watchdog is also stopped again because another thread could have
  16296. * restarted it. The irq_work is cancelled last because it is used for
  16297. * all I2C/SPI communication.
  16298. */
  16299. pt_stop_wd_timer(cd);
  16300. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16301. cancel_work_sync(&cd->ttdl_restart_work);
  16302. cancel_work_sync(&cd->enum_work);
  16303. cancel_work_sync(&cd->resume_offload_work);
  16304. cancel_work_sync(&cd->suspend_offload_work);
  16305. cancel_work_sync(&cd->resume_work);
  16306. cancel_work_sync(&cd->suspend_work);
  16307. destroy_workqueue(cd->pt_workqueue);
  16308. pt_stop_wd_timer(cd);
  16309. pt_release_modules(cd);
  16310. pt_proximity_release(dev);
  16311. pt_btn_release(dev);
  16312. pt_mt_release(dev);
  16313. #ifdef CONFIG_HAS_EARLYSUSPEND
  16314. unregister_early_suspend(&cd->es);
  16315. #elif defined(CONFIG_DRM)
  16316. if (active_panel)
  16317. panel_event_notifier_unregister(&cd->fb_notifier);
  16318. #elif defined(CONFIG_FB)
  16319. fb_unregister_client(&cd->fb_notifier);
  16320. #endif
  16321. #ifdef NEED_SUSPEND_NOTIFIER
  16322. unregister_pm_notifier(&cd->pm_notifier);
  16323. #endif
  16324. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16325. device_wakeup_disable(dev);
  16326. #endif
  16327. device_init_wakeup(dev, 0);
  16328. #ifdef TTHE_TUNER_SUPPORT
  16329. mutex_lock(&cd->tthe_lock);
  16330. cd->tthe_exit = 1;
  16331. wake_up(&cd->wait_q);
  16332. mutex_unlock(&cd->tthe_lock);
  16333. debugfs_remove(cd->tthe_debugfs);
  16334. #endif
  16335. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16336. remove_sysfs_interfaces(dev);
  16337. disable_irq_nosync(cd->irq);
  16338. if (cd->cpdata->setup_irq)
  16339. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16340. if (cd->cpdata->init)
  16341. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16342. if (cd->cpdata->setup_power)
  16343. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16344. dev_set_drvdata(dev, NULL);
  16345. pt_del_core(dev);
  16346. if (cd->vcc_i2c)
  16347. regulator_set_load(cd->vcc_i2c, 0);
  16348. if (cd->vdd)
  16349. regulator_set_load(cd->vdd, 0);
  16350. pt_enable_regulator(cd, false);
  16351. pt_get_regulator(cd, false);
  16352. pt_free_si_ptrs(cd);
  16353. kfree(cd);
  16354. return 0;
  16355. }
  16356. EXPORT_SYMBOL_GPL(pt_release);
  16357. MODULE_LICENSE("GPL");
  16358. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16359. MODULE_AUTHOR("Parade Technologies <[email protected]>");