pt_core.c 514 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include "pt_regs.h"
  33. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  34. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  35. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  36. #define FT_VTG_MIN_UV 2800000
  37. #define FT_VTG_MAX_UV 2800000
  38. #define FT_I2C_VTG_MIN_UV 1800000
  39. #define FT_I2C_VTG_MAX_UV 1800000
  40. #define PWR_SUSPEND_LOAD_UA 165
  41. #define I2C_SUSPEND_LOAD_UA 100
  42. #define PWR_ACTIVE_LOAD_MA 12000
  43. #define I2C_ACTIVE_LOAD_MA 30000
  44. #define PT_CORE_STARTUP_RETRY_COUNT 3
  45. #define PT_STATUS_STR_LEN (50)
  46. #if defined(CONFIG_DRM)
  47. static struct drm_panel *active_panel;
  48. #endif
  49. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  50. static int pt_lpm_regulator(struct regulator *reg, int load_uA);
  51. static const char *pt_driver_core_name = PT_CORE_NAME;
  52. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  53. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  54. struct pt_hid_field {
  55. int report_count;
  56. int report_size;
  57. int size; /* report_count * report_size */
  58. int offset;
  59. int data_type;
  60. int logical_min;
  61. int logical_max;
  62. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  63. u32 usage_page;
  64. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  65. struct pt_hid_report *report;
  66. bool record_field;
  67. };
  68. struct pt_hid_report {
  69. u8 id;
  70. u8 type;
  71. int size;
  72. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  73. int num_fields;
  74. int record_field_index;
  75. int header_size;
  76. int record_size;
  77. u32 usage_page;
  78. };
  79. struct atten_node {
  80. struct list_head node;
  81. char *id;
  82. struct device *dev;
  83. int (*func)(struct device *dev);
  84. int mode;
  85. };
  86. struct param_node {
  87. struct list_head node;
  88. u8 id;
  89. u32 value;
  90. u8 size;
  91. };
  92. struct module_node {
  93. struct list_head node;
  94. struct pt_module *module;
  95. void *data;
  96. };
  97. struct pt_hid_cmd {
  98. u8 opcode;
  99. u8 report_type;
  100. union {
  101. u8 report_id;
  102. u8 power_state;
  103. };
  104. u8 has_data_register;
  105. size_t write_length;
  106. u8 *write_buf;
  107. u8 *read_buf;
  108. u8 wait_interrupt;
  109. u8 reset_cmd;
  110. u16 timeout_ms;
  111. };
  112. struct pt_hid_output {
  113. u8 cmd_type;
  114. u16 length;
  115. u8 command_code;
  116. size_t write_length;
  117. u8 *write_buf;
  118. u8 novalidate;
  119. u8 reset_expected;
  120. u16 timeout_ms;
  121. };
  122. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  123. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  124. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  125. #define CREATE_PIP1_FW_CMD(command) \
  126. .cmd_type = PIP1_CMD_TYPE_FW, \
  127. .command_code = command
  128. #define CREATE_PIP1_BL_CMD(command) \
  129. .cmd_type = PIP1_CMD_TYPE_BL, \
  130. .command_code = command
  131. #define PT_MAX_PR_BUF_SIZE 2048
  132. /*******************************************************************************
  133. * FUNCTION: pt_pr_buf
  134. *
  135. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  136. *
  137. * RETURN: Void
  138. *
  139. * PARAMETERS:
  140. * *dev - pointer to Device structure
  141. * debug_level - requested debug level to print at
  142. * *buf - pointer to buffer to print
  143. * buf_len - size of buf
  144. * *data_name - Descriptive name of data prefixed to data
  145. ******************************************************************************/
  146. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  147. u16 buf_len, const char *data_name)
  148. {
  149. struct pt_core_data *cd = dev_get_drvdata(dev);
  150. int i;
  151. ssize_t pr_buf_index = 0;
  152. int max_size;
  153. /* only proceed if valid debug level and there is data to print */
  154. if (debug_level <= cd->debug_level && buf_len > 0) {
  155. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  156. if (!pr_buf)
  157. return;
  158. /*
  159. * With a space each printed char takes 3 bytes, subtract
  160. * the length of the data_name prefix as well as 11 bytes
  161. * for the " [0..xxx]: " printed before the data.
  162. */
  163. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  164. /* Ensure pr_buf_index stays within the 1018 size */
  165. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  166. data_name);
  167. for (i = 0; i < buf_len && i < max_size; i++)
  168. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  169. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  170. pt_debug(dev, debug_level, "%s\n", pr_buf);
  171. kfree(pr_buf);
  172. }
  173. }
  174. EXPORT_SYMBOL_GPL(pt_pr_buf);
  175. #ifdef TTHE_TUNER_SUPPORT
  176. /*******************************************************************************
  177. * FUNCTION: tthe_print
  178. *
  179. * SUMMARY: Format data name and time stamp as the header and format the
  180. * content of input buffer with hex base to "tthe_buf". And then wake up event
  181. * semaphore for tthe debugfs node.
  182. *
  183. * RETURN:
  184. * 0 = success
  185. * !0 = failure
  186. *
  187. * PARAMETERS:
  188. * *cd - pointer to core data
  189. * *buf - pointer to input buffer
  190. * buf_len - size of input buffer
  191. * *data_name - pointer to data name
  192. ******************************************************************************/
  193. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  194. const u8 *data_name)
  195. {
  196. int name_len = strlen(data_name);
  197. int i, n;
  198. u8 *p;
  199. int remain;
  200. u8 data_name_with_time_stamp[100];
  201. /* Prepend timestamp, if requested, to data_name */
  202. if (cd->show_timestamp) {
  203. scnprintf(data_name_with_time_stamp,
  204. sizeof(data_name_with_time_stamp),
  205. "[%u] %s", pt_get_time_stamp(), data_name);
  206. data_name = data_name_with_time_stamp;
  207. name_len = strlen(data_name);
  208. }
  209. mutex_lock(&cd->tthe_lock);
  210. if (!cd->tthe_buf)
  211. goto exit;
  212. /* Add 1 due to the '\n' that is appended at the end */
  213. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  214. goto exit;
  215. if (name_len + buf_len == 0)
  216. goto exit;
  217. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  218. if (remain < name_len)
  219. name_len = remain;
  220. p = cd->tthe_buf + cd->tthe_buf_len;
  221. memcpy(p, data_name, name_len);
  222. cd->tthe_buf_len += name_len;
  223. p += name_len;
  224. remain -= name_len;
  225. *p = 0;
  226. for (i = 0; i < buf_len; i++) {
  227. n = scnprintf(p, remain, "%02X ", buf[i]);
  228. if (n <= 0)
  229. break;
  230. p += n;
  231. remain -= n;
  232. cd->tthe_buf_len += n;
  233. }
  234. n = scnprintf(p, remain, "\n");
  235. cd->tthe_buf_len += n;
  236. exit:
  237. wake_up(&cd->wait_q);
  238. mutex_unlock(&cd->tthe_lock);
  239. return 0;
  240. }
  241. /*******************************************************************************
  242. * FUNCTION: _pt_request_tthe_print
  243. *
  244. * SUMMARY: Function pointer included in core_cmds to allow other modules
  245. * to request to print data to the "tthe_buffer".
  246. *
  247. * RETURN:
  248. * 0 = success
  249. * !0 = failure
  250. *
  251. * PARAMETERS:
  252. * *dev - pointer to device structure
  253. ******************************************************************************/
  254. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  255. int buf_len, const u8 *data_name)
  256. {
  257. struct pt_core_data *cd = dev_get_drvdata(dev);
  258. return tthe_print(cd, buf, buf_len, data_name);
  259. }
  260. #endif
  261. /*******************************************************************************
  262. * FUNCTION: pt_platform_detect_read
  263. *
  264. * SUMMARY: To be passed to platform dectect function to perform a read
  265. * operation.
  266. *
  267. * RETURN:
  268. * 0 = success
  269. * !0 = failure
  270. *
  271. * PARAMETERS:
  272. * *dev - pointer to Device structure
  273. * *buf - pointer to buffer where the data read will be stored
  274. * size - size to be read
  275. ******************************************************************************/
  276. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  277. {
  278. struct pt_core_data *cd = dev_get_drvdata(dev);
  279. return pt_adap_read_default(cd, buf, size);
  280. }
  281. /*******************************************************************************
  282. * FUNCTION: pt_add_parameter
  283. *
  284. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  285. * On every reset of the DUT this linked list is traversed and all
  286. * parameters in it are restored to the DUT.
  287. *
  288. * RETURN:
  289. * 0 = success
  290. * !0 = failure
  291. *
  292. * PARAMETERS:
  293. * *cd - pointer to core data
  294. * param_id - parameter ID to add
  295. * param_value - Value corresponding to the ID
  296. * param_size - Size of param_value
  297. ******************************************************************************/
  298. static int pt_add_parameter(struct pt_core_data *cd,
  299. u8 param_id, u32 param_value, u8 param_size)
  300. {
  301. struct param_node *param, *param_new;
  302. /* Check if parameter already exists in the list */
  303. spin_lock(&cd->spinlock);
  304. list_for_each_entry(param, &cd->param_list, node) {
  305. if (param->id == param_id) {
  306. /* Update parameter */
  307. param->value = param_value;
  308. pt_debug(cd->dev, DL_INFO,
  309. "%s: Update parameter id:%d value:%d size:%d\n",
  310. __func__, param_id, param_value, param_size);
  311. goto exit_unlock;
  312. }
  313. }
  314. spin_unlock(&cd->spinlock);
  315. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  316. if (!param_new)
  317. return -ENOMEM;
  318. param_new->id = param_id;
  319. param_new->value = param_value;
  320. param_new->size = param_size;
  321. pt_debug(cd->dev, DL_INFO,
  322. "%s: Add parameter id:%d value:%d size:%d\n",
  323. __func__, param_id, param_value, param_size);
  324. spin_lock(&cd->spinlock);
  325. list_add(&param_new->node, &cd->param_list);
  326. exit_unlock:
  327. spin_unlock(&cd->spinlock);
  328. return 0;
  329. }
  330. #ifdef TTDL_DIAGNOSTICS
  331. /*******************************************************************************
  332. * FUNCTION: pt_erase_parameter_list
  333. *
  334. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  335. * pairs. In some test cases this functionality is needed to ensure DUT
  336. * returns to a virgin state after a reset and no parameters are restored.
  337. *
  338. * RETURN:
  339. * 0 = success
  340. * !0 = failure
  341. *
  342. * PARAMETERS:
  343. * *cd - pointer to core data
  344. ******************************************************************************/
  345. static int pt_erase_parameter_list(struct pt_core_data *cd)
  346. {
  347. struct param_node *pos, *temp;
  348. spin_lock(&cd->spinlock);
  349. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  350. pt_debug(cd->dev, DL_INFO,
  351. "%s: Parameter Restore List - remove 0x%02x\n",
  352. __func__, pos->id);
  353. list_del(&pos->node);
  354. kfree(pos);
  355. }
  356. spin_unlock(&cd->spinlock);
  357. return 0;
  358. }
  359. /*******************************************************************************
  360. * FUNCTION: pt_count_parameter_list
  361. *
  362. * SUMMARY: Count the items in the RAM parameter restor list
  363. *
  364. * RETURN:
  365. * 0 = success
  366. * !0 = failure
  367. *
  368. * PARAMETERS:
  369. * *cd - pointer to core data
  370. ******************************************************************************/
  371. static int pt_count_parameter_list(struct pt_core_data *cd)
  372. {
  373. struct param_node *pos, *temp;
  374. int entries = 0;
  375. spin_lock(&cd->spinlock);
  376. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  377. entries++;
  378. spin_unlock(&cd->spinlock);
  379. return entries;
  380. }
  381. #endif /* TTDL_DIAGNOSTICS */
  382. /*******************************************************************************
  383. * FUNCTION: request_exclusive
  384. *
  385. * SUMMARY: Request exclusive access to the DUT
  386. *
  387. * RETURN:
  388. * 0 = success
  389. * !0 = failure
  390. *
  391. * PARAMETERS:
  392. * *cd - pointer to core data
  393. * *ownptr - pointer to device
  394. * timeout_ms - Timeout value
  395. ******************************************************************************/
  396. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  397. int timeout_ms)
  398. {
  399. int t = msecs_to_jiffies(timeout_ms);
  400. bool with_timeout = (timeout_ms != 0);
  401. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  402. __func__, timeout_ms);
  403. mutex_lock(&cd->system_lock);
  404. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  405. cd->exclusive_dev = ownptr;
  406. goto exit;
  407. }
  408. cd->exclusive_waits++;
  409. wait:
  410. mutex_unlock(&cd->system_lock);
  411. if (with_timeout) {
  412. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  413. if (IS_TMO(t)) {
  414. pt_debug(cd->dev, DL_ERROR,
  415. "%s: tmo waiting exclusive access\n", __func__);
  416. return -ETIME;
  417. }
  418. } else {
  419. wait_event(cd->wait_q, !cd->exclusive_dev);
  420. }
  421. mutex_lock(&cd->system_lock);
  422. if (cd->exclusive_dev)
  423. goto wait;
  424. cd->exclusive_dev = ownptr;
  425. cd->exclusive_waits--;
  426. exit:
  427. mutex_unlock(&cd->system_lock);
  428. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  429. __func__, ownptr);
  430. return 0;
  431. }
  432. /*******************************************************************************
  433. * FUNCTION: release_exclusive_
  434. *
  435. * SUMMARY: Release exclusive access to the DUT
  436. *
  437. * RETURN:
  438. * 0 = success
  439. *
  440. * PARAMETERS:
  441. * *cd - pointer to core data
  442. * *ownptr - pointer to device
  443. ******************************************************************************/
  444. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  445. {
  446. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  447. __func__);
  448. if (cd->exclusive_dev != ownptr)
  449. return -EINVAL;
  450. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  451. __func__, cd->exclusive_dev);
  452. cd->exclusive_dev = NULL;
  453. wake_up(&cd->wait_q);
  454. return 0;
  455. }
  456. /*******************************************************************************
  457. * FUNCTION: release_exclusive
  458. *
  459. * SUMMARY: Protected wrapper to release_exclusive_()
  460. *
  461. * RETURN:
  462. * 0 = success
  463. * !0 = failure
  464. *
  465. * PARAMETERS:
  466. * *cd - pointer to core data
  467. * *ownptr - pointer to device
  468. ******************************************************************************/
  469. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  470. {
  471. int rc;
  472. mutex_lock(&cd->system_lock);
  473. rc = release_exclusive_(cd, ownptr);
  474. mutex_unlock(&cd->system_lock);
  475. return rc;
  476. }
  477. /*******************************************************************************
  478. * FUNCTION: pt_hid_exec_cmd_
  479. *
  480. * SUMMARY: Send the HID command to the DUT
  481. *
  482. * RETURN:
  483. * 0 = success
  484. * !0 = failure
  485. *
  486. * PARAMETERS:
  487. * *cd - pointer to core data
  488. * *hid_cmd - pointer to the HID command to send
  489. ******************************************************************************/
  490. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  491. struct pt_hid_cmd *hid_cmd)
  492. {
  493. int rc = 0;
  494. u8 *cmd;
  495. u16 cmd_length;
  496. u8 cmd_offset = 0;
  497. cmd_length = 2 /* command register */
  498. + 2 /* command */
  499. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  500. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  501. + hid_cmd->write_length; /* Data length */
  502. cmd = kzalloc(cmd_length, GFP_KERNEL);
  503. if (!cmd)
  504. return -ENOMEM;
  505. /* Set Command register */
  506. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  507. sizeof(cd->hid_desc.command_register));
  508. cmd_offset += sizeof(cd->hid_desc.command_register);
  509. /* Set Command */
  510. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  511. if (hid_cmd->report_id >= 0XF)
  512. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  513. else
  514. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  515. cmd_offset++;
  516. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  517. cmd_offset++;
  518. if (hid_cmd->report_id >= 0XF) {
  519. cmd[cmd_offset] = hid_cmd->report_id;
  520. cmd_offset++;
  521. }
  522. /* Set Data register */
  523. if (hid_cmd->has_data_register) {
  524. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  525. sizeof(cd->hid_desc.data_register));
  526. cmd_offset += sizeof(cd->hid_desc.data_register);
  527. }
  528. /* Set Data */
  529. if (hid_cmd->write_length && hid_cmd->write_buf) {
  530. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  531. hid_cmd->write_length);
  532. cmd_offset += hid_cmd->write_length;
  533. }
  534. pt_debug(cd->dev, DL_INFO,
  535. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  536. __func__, cmd_length, hid_cmd->report_id);
  537. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  538. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  539. hid_cmd->read_buf);
  540. if (rc)
  541. pt_debug(cd->dev, DL_ERROR,
  542. "%s: Fail pt_adap_transfer\n", __func__);
  543. kfree(cmd);
  544. return rc;
  545. }
  546. #ifdef TTDL_DIAGNOSTICS
  547. /*******************************************************************************
  548. * FUNCTION: pt_toggle_err_gpio
  549. *
  550. * SUMMARY: Toggles the pre-defined error GPIO
  551. *
  552. * RETURN: n/a
  553. *
  554. * PARAMETERS:
  555. * *cd - pointer to core data
  556. * type - type of err that occurred
  557. ******************************************************************************/
  558. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  559. {
  560. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  561. __func__, type);
  562. if (cd->err_gpio && type == cd->err_gpio_type) {
  563. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  564. gpio_direction_output(cd->err_gpio,
  565. !gpio_get_value(cd->err_gpio));
  566. }
  567. }
  568. /*******************************************************************************
  569. * FUNCTION: _pt_request_toggle_err_gpio
  570. *
  571. * SUMMARY: Function pointer included in core_cmds to allow other modules
  572. * to request to toggle the err_gpio
  573. *
  574. * RETURN: n/a
  575. *
  576. * PARAMETERS:
  577. * *cd - pointer to core data
  578. * type - type of err that occurred
  579. ******************************************************************************/
  580. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  581. {
  582. struct pt_core_data *cd = dev_get_drvdata(dev);
  583. pt_toggle_err_gpio(cd, type);
  584. }
  585. #endif /* TTDL_DIAGNOSTICS */
  586. /*******************************************************************************
  587. * FUNCTION: pt_hid_exec_cmd_and_wait_
  588. *
  589. * SUMMARY: Send the HID command to the DUT and wait for the response
  590. *
  591. * RETURN:
  592. * 0 = success
  593. * !0 = failure
  594. *
  595. * PARAMETERS:
  596. * *cd - pointer to core data
  597. * *hid_cmd - pointer to the HID command to send
  598. ******************************************************************************/
  599. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  600. struct pt_hid_cmd *hid_cmd)
  601. {
  602. int rc = 0;
  603. int t;
  604. u16 timeout_ms;
  605. int *cmd_state;
  606. if (hid_cmd->reset_cmd)
  607. cmd_state = &cd->hid_reset_cmd_state;
  608. else
  609. cmd_state = &cd->hid_cmd_state;
  610. if (hid_cmd->wait_interrupt) {
  611. mutex_lock(&cd->system_lock);
  612. *cmd_state = 1;
  613. mutex_unlock(&cd->system_lock);
  614. }
  615. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  616. if (rc) {
  617. if (hid_cmd->wait_interrupt)
  618. goto error;
  619. goto exit;
  620. }
  621. if (!hid_cmd->wait_interrupt)
  622. goto exit;
  623. if (hid_cmd->timeout_ms)
  624. timeout_ms = hid_cmd->timeout_ms;
  625. else
  626. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  627. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  628. msecs_to_jiffies(timeout_ms));
  629. if (IS_TMO(t)) {
  630. #ifdef TTDL_DIAGNOSTICS
  631. cd->bus_transmit_error_count++;
  632. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  633. #endif /* TTDL_DIAGNOSTICS */
  634. pt_debug(cd->dev, DL_ERROR,
  635. "%s: HID output cmd execution timed out\n",
  636. __func__);
  637. rc = -ETIME;
  638. goto error;
  639. }
  640. goto exit;
  641. error:
  642. mutex_lock(&cd->system_lock);
  643. *cmd_state = 0;
  644. mutex_unlock(&cd->system_lock);
  645. exit:
  646. return rc;
  647. }
  648. /*******************************************************************************
  649. * FUNCTION: pt_hid_cmd_reset_
  650. *
  651. * SUMMARY: Send the HID RESET command to the DUT
  652. *
  653. * RETURN:
  654. * 0 = success
  655. * !0 = failure
  656. *
  657. * PARAMETERS:
  658. * *cd - pointer to core data
  659. ******************************************************************************/
  660. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  661. {
  662. struct pt_hid_cmd hid_cmd = {
  663. .opcode = HID_CMD_RESET,
  664. .wait_interrupt = 1,
  665. .reset_cmd = 1,
  666. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  667. };
  668. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  669. }
  670. /*******************************************************************************
  671. * FUNCTION: pt_hid_cmd_reset
  672. *
  673. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  674. * access.
  675. *
  676. * RETURN:
  677. * 0 = success
  678. * !0 = failure
  679. *
  680. * PARAMETERS:
  681. * *cd - pointer to core data
  682. ******************************************************************************/
  683. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  684. {
  685. int rc = 0;
  686. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  687. if (rc < 0) {
  688. pt_debug(cd->dev, DL_ERROR,
  689. "%s: fail get exclusive ex=%p own=%p\n",
  690. __func__, cd->exclusive_dev, cd->dev);
  691. return rc;
  692. }
  693. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  694. rc = pt_hid_cmd_reset_(cd);
  695. if (release_exclusive(cd, cd->dev) < 0)
  696. pt_debug(cd->dev, DL_ERROR,
  697. "%s: fail to release exclusive\n", __func__);
  698. return rc;
  699. }
  700. /*******************************************************************************
  701. * FUNCTION: pt_hid_cmd_set_power_
  702. *
  703. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  704. *
  705. * RETURN:
  706. * 0 = success
  707. * !0 = failure
  708. *
  709. * PARAMETERS:
  710. * *cd - pointer to core data
  711. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  712. ******************************************************************************/
  713. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  714. u8 power_state)
  715. {
  716. int rc = 0;
  717. struct pt_hid_cmd hid_cmd = {
  718. .opcode = HID_CMD_SET_POWER,
  719. .wait_interrupt = 1,
  720. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  721. };
  722. hid_cmd.power_state = power_state;
  723. /* The chip won't give response if goes to Deep Standby */
  724. if (power_state == HID_POWER_STANDBY) {
  725. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  726. if (rc)
  727. pt_debug(cd->dev, DL_ERROR,
  728. "%s: Failed to set power to state:%d\n",
  729. __func__, power_state);
  730. else
  731. cd->fw_sys_mode_in_standby_state = true;
  732. return rc;
  733. }
  734. cd->fw_sys_mode_in_standby_state = false;
  735. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  736. if (rc) {
  737. pt_debug(cd->dev, DL_ERROR,
  738. "%s: Failed to set power to state:%d\n",
  739. __func__, power_state);
  740. return rc;
  741. }
  742. /* validate */
  743. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  744. || ((cd->response_buf[3] & 0x3) != power_state)
  745. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  746. rc = -EINVAL;
  747. return rc;
  748. }
  749. /*******************************************************************************
  750. * FUNCTION: pt_hid_cmd_set_power
  751. *
  752. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  753. * exclusive access.
  754. *
  755. * RETURN:
  756. * 0 = success
  757. * !0 = failure
  758. *
  759. * PARAMETERS:
  760. * *cd - pointer to core data
  761. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  762. ******************************************************************************/
  763. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  764. u8 power_state)
  765. {
  766. int rc;
  767. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  768. if (rc < 0) {
  769. pt_debug(cd->dev, DL_ERROR,
  770. "%s: fail get exclusive ex=%p own=%p\n",
  771. __func__, cd->exclusive_dev, cd->dev);
  772. return rc;
  773. }
  774. rc = pt_hid_cmd_set_power_(cd, power_state);
  775. if (release_exclusive(cd, cd->dev) < 0)
  776. pt_debug(cd->dev, DL_ERROR,
  777. "%s: fail to release exclusive\n", __func__);
  778. return rc;
  779. }
  780. static const u16 crc_table[16] = {
  781. 0x0000, 0x1021, 0x2042, 0x3063,
  782. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  783. 0x8108, 0x9129, 0xa14a, 0xb16b,
  784. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  785. };
  786. /*******************************************************************************
  787. * FUNCTION: _pt_compute_crc
  788. *
  789. * SUMMARY: Calculate CRC by CRC table.
  790. *
  791. * RETURN:
  792. * CRC calculation result
  793. *
  794. * PARAMETERS:
  795. * *buf - pointer to the data array to be calculated
  796. * size - size of data array
  797. ******************************************************************************/
  798. static u16 _pt_compute_crc(u8 *buf, u32 size)
  799. {
  800. u16 remainder = 0xFFFF;
  801. u16 xor_mask = 0x0000;
  802. u32 index;
  803. u32 byte_value;
  804. u32 table_index;
  805. u32 crc_bit_width = sizeof(u16) * 8;
  806. /* Divide the message by polynomial, via the table. */
  807. for (index = 0; index < size; index++) {
  808. byte_value = buf[index];
  809. table_index = ((byte_value >> 4) & 0x0F)
  810. ^ (remainder >> (crc_bit_width - 4));
  811. remainder = crc_table[table_index] ^ (remainder << 4);
  812. table_index = (byte_value & 0x0F)
  813. ^ (remainder >> (crc_bit_width - 4));
  814. remainder = crc_table[table_index] ^ (remainder << 4);
  815. }
  816. /* Perform the final remainder CRC. */
  817. return remainder ^ xor_mask;
  818. }
  819. u16 ccitt_Table[] = {
  820. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  821. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  822. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  823. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  824. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  825. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  826. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  827. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  828. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  829. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  830. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  831. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  832. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  833. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  834. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  835. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  836. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  837. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  838. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  839. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  840. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  841. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  842. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  843. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  844. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  845. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  846. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  847. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  848. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  849. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  850. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  851. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  852. };
  853. /*******************************************************************************
  854. * FUNCTION: crc_ccitt_calculate
  855. *
  856. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  857. *
  858. * RETURN:
  859. * CRC calculation result
  860. *
  861. * PARAMETERS:
  862. * *q - pointer to the data array to be calculated
  863. * len - size of data array
  864. ******************************************************************************/
  865. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  866. {
  867. unsigned short crc = 0xffff;
  868. while (len-- > 0)
  869. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  870. return crc;
  871. }
  872. /*******************************************************************************
  873. * FUNCTION: pt_pip2_cmd_calculate_crc
  874. *
  875. * SUMMARY: Calculate the CRC of a command packet
  876. *
  877. * RETURN: void
  878. *
  879. * PARAMETERS:
  880. * *cmd - pointer to command data
  881. * extra_bytes - Extra bytes included in command length
  882. ******************************************************************************/
  883. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  884. u8 extra_bytes)
  885. {
  886. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  887. unsigned short crc;
  888. buf[0] = cmd->len & 0xff;
  889. buf[1] = (cmd->len & 0xff00) >> 8;
  890. buf[2] = cmd->seq;
  891. buf[3] = cmd->id;
  892. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  893. /* Calculate the CRC for the first 4 bytes above and the data payload */
  894. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  895. cmd->crc[0] = (crc & 0xff00) >> 8;
  896. cmd->crc[1] = (crc & 0xff);
  897. }
  898. /*******************************************************************************
  899. * FUNCTION: pt_pip2_get_next_cmd_seq
  900. *
  901. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  902. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  903. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  904. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  905. * increment, just use the forced value.
  906. *
  907. * RETURN: Next command sequence number [0x08-0x0F]
  908. *
  909. * PARAMETERS:
  910. * *cd - pointer to core data
  911. ******************************************************************************/
  912. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  913. {
  914. #ifdef TTDL_DIAGNOSTICS
  915. if (cd->force_pip2_seq <= 0x07) {
  916. cd->pip2_cmd_tag_seq++;
  917. if (cd->pip2_cmd_tag_seq > 0x0F)
  918. cd->pip2_cmd_tag_seq = 0x08;
  919. } else {
  920. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  921. }
  922. #else
  923. cd->pip2_cmd_tag_seq++;
  924. if (cd->pip2_cmd_tag_seq > 0x0F)
  925. cd->pip2_cmd_tag_seq = 0x08;
  926. #endif
  927. return cd->pip2_cmd_tag_seq;
  928. }
  929. /*
  930. * Following macros are to define the response time (the interval between PIP2
  931. * command finishes sending and INT pin falls). The unit is in microsecond.
  932. * It has different time settings between the solution GPIO polling and Bus
  933. * polling due to the considration for system load.
  934. */
  935. #ifdef PT_POLL_RESP_BY_BUS
  936. #define POLL_RETRY_DEFAULT_INTERVAL 50
  937. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  938. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  939. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  940. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  941. #else
  942. #define POLL_RETRY_DEFAULT_INTERVAL 20
  943. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  944. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  945. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  946. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  947. #endif
  948. /*
  949. * id: the command id defined in PIP2
  950. * response_len: the (maximum) length of response.
  951. * response_time_min: minimum response time in microsecond
  952. * response_time_max: maximum response time in microsecond
  953. */
  954. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  955. {.id = PIP2_CMD_ID_PING,
  956. .response_len = 255,
  957. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  958. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  959. {.id = PIP2_CMD_ID_STATUS,
  960. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_CTRL,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  967. {.id = PIP2_CMD_ID_CONFIG,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  971. {.id = PIP2_CMD_ID_CLEAR,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_RESET,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_VERSION,
  980. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  981. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  982. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  983. {.id = PIP2_CMD_ID_FILE_OPEN,
  984. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_CLOSE,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  989. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_READ,
  992. .response_len = 255,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FILE_WRITE,
  996. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  997. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1001. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_FLASH_INFO,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_EXECUTE,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_ID_READ_GPIO,
  1020. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1021. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1022. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1023. {.id = PIP2_CMD_EXECUTE_SCAN,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_SET_PARAMETER,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_GET_PARAMETER,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_SET_DDI_REG,
  1036. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1039. {.id = PIP2_CMD_GET_DDI_REG,
  1040. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1041. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1042. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1043. {.id = PIP2_CMD_ID_END,
  1044. .response_len = 255,
  1045. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1046. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1047. };
  1048. /*******************************************************************************
  1049. * FUNCTION: pt_pip2_get_cmd_response_len
  1050. *
  1051. * SUMMARY: Gets the expected response length based on the command ID
  1052. *
  1053. * RETURN: Expected response length
  1054. *
  1055. * PARAMETERS:
  1056. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1057. ******************************************************************************/
  1058. static int pt_pip2_get_cmd_response_len(u8 id)
  1059. {
  1060. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1061. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1062. p++;
  1063. if (p->id != PIP2_CMD_ID_END)
  1064. return p->response_len;
  1065. else
  1066. return -EPERM;
  1067. }
  1068. /*******************************************************************************
  1069. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1070. *
  1071. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1072. * finishes sending and INT pin falls) based on the command ID
  1073. *
  1074. * RETURN: Estimated minimum response time in microsecond
  1075. *
  1076. * PARAMETERS:
  1077. * id - Command ID
  1078. ******************************************************************************/
  1079. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1080. {
  1081. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1082. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1083. p++;
  1084. if (p->id != PIP2_CMD_ID_END)
  1085. return p->response_time_min;
  1086. else
  1087. return PIP2_RESP_DEFAULT_TIME_MIN;
  1088. }
  1089. /*******************************************************************************
  1090. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1091. *
  1092. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1093. * finishes sending and INT pin falls) based on the command ID
  1094. *
  1095. * RETURN: Estimated maximum response time in microsecond
  1096. *
  1097. * PARAMETERS:
  1098. * id - Command ID
  1099. ******************************************************************************/
  1100. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1101. {
  1102. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1103. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1104. p++;
  1105. if (p->id != PIP2_CMD_ID_END)
  1106. return p->response_time_max;
  1107. else
  1108. return PIP2_RESP_DEFAULT_TIME_MAX;
  1109. }
  1110. /*******************************************************************************
  1111. * FUNCTION: pt_pip2_validate_response
  1112. *
  1113. * SUMMARY: Validate the response of PIP2 command.
  1114. *
  1115. * RETURN:
  1116. * 0 = success
  1117. * !0 = failure
  1118. *
  1119. * PARAMETERS:
  1120. * *cd - pointer to core data
  1121. * *pip2_cmd - pointer to PIP2 command to send
  1122. * *read_buf - pointer to response buffer
  1123. * actual_read_len - actual read length of the response
  1124. ******************************************************************************/
  1125. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1126. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1127. u16 actual_read_len)
  1128. {
  1129. int rc = 0;
  1130. u8 response_seq = 0;
  1131. u8 reserved_bits = 0;
  1132. u8 cmd_id = 0;
  1133. u8 response_bit = 0;
  1134. unsigned short calc_crc = 0;
  1135. unsigned short resp_crc = 0;
  1136. /* Verify the length of response buffer */
  1137. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1138. pt_debug(cd->dev, DL_ERROR,
  1139. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1140. __func__, pip2_cmd->id, actual_read_len);
  1141. rc = -EINVAL;
  1142. goto exit;
  1143. }
  1144. /* Verify the CRC */
  1145. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1146. resp_crc = read_buf[actual_read_len - 2] << 8;
  1147. resp_crc |= read_buf[actual_read_len - 1];
  1148. if (resp_crc != calc_crc) {
  1149. pt_debug(cd->dev, DL_ERROR,
  1150. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1151. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1152. #ifdef TTDL_DIAGNOSTICS
  1153. cd->pip2_crc_error_count++;
  1154. #endif /* TTDL_DIAGNOSTICS */
  1155. rc = -EINVAL;
  1156. goto exit;
  1157. }
  1158. /* Verify the response bit is set */
  1159. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1160. if (!response_bit) {
  1161. pt_debug(cd->dev, DL_ERROR,
  1162. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1163. __func__, pip2_cmd->id, response_bit);
  1164. rc = -EINVAL;
  1165. goto exit;
  1166. }
  1167. /* Verify the command ID matches from command to response */
  1168. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1169. if (cmd_id != pip2_cmd->id) {
  1170. pt_debug(cd->dev, DL_ERROR,
  1171. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1172. __func__, pip2_cmd->id, cmd_id);
  1173. rc = -EINVAL;
  1174. goto exit;
  1175. }
  1176. /* Verify the SEQ number matches from command to response */
  1177. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1178. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1179. pt_debug(cd->dev, DL_ERROR,
  1180. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1181. __func__, pip2_cmd->id,
  1182. pip2_cmd->seq, response_seq);
  1183. rc = -EINVAL;
  1184. goto exit;
  1185. }
  1186. /* Verify the reserved bits are 0 */
  1187. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1188. if (reserved_bits)
  1189. pt_debug(cd->dev, DL_WARN,
  1190. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1191. __func__, pip2_cmd->id, reserved_bits);
  1192. exit:
  1193. if (rc)
  1194. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1195. "PIP RSP:");
  1196. return rc;
  1197. }
  1198. /*******************************************************************************
  1199. * FUNCTION: pt_hid_output_validate_bl_response
  1200. *
  1201. * SUMMARY: Validate the response of bootloader command.
  1202. *
  1203. * RETURN:
  1204. * 0 = success
  1205. * !0 = failure
  1206. *
  1207. * PARAMETERS:
  1208. * *cd - pointer to core data
  1209. * *hid_output - pointer to hid output data structure
  1210. ******************************************************************************/
  1211. static int pt_hid_output_validate_bl_response(
  1212. struct pt_core_data *cd,
  1213. struct pt_hid_output *hid_output)
  1214. {
  1215. u16 size;
  1216. u16 crc;
  1217. u8 status;
  1218. size = get_unaligned_le16(&cd->response_buf[0]);
  1219. if (hid_output->reset_expected && !size)
  1220. return 0;
  1221. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1222. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1223. pt_debug(cd->dev, DL_ERROR,
  1224. "%s: BL output response, wrong report_id\n", __func__);
  1225. return -EPROTO;
  1226. }
  1227. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1228. pt_debug(cd->dev, DL_ERROR,
  1229. "%s: BL output response, wrong SOP\n", __func__);
  1230. return -EPROTO;
  1231. }
  1232. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1233. pt_debug(cd->dev, DL_ERROR,
  1234. "%s: BL output response, wrong EOP\n", __func__);
  1235. return -EPROTO;
  1236. }
  1237. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1238. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1239. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1240. pt_debug(cd->dev, DL_ERROR,
  1241. "%s: BL output response, wrong CRC 0x%X\n",
  1242. __func__, crc);
  1243. return -EPROTO;
  1244. }
  1245. status = cd->response_buf[5];
  1246. if (status) {
  1247. pt_debug(cd->dev, DL_ERROR,
  1248. "%s: BL output response, ERROR:%d\n",
  1249. __func__, status);
  1250. return -EPROTO;
  1251. }
  1252. return 0;
  1253. }
  1254. /*******************************************************************************
  1255. * FUNCTION: pt_hid_output_validate_app_response
  1256. *
  1257. * SUMMARY: Validate the response of application command.
  1258. *
  1259. * RETURN:
  1260. * 0 = success
  1261. * !0 = failure
  1262. *
  1263. * PARAMETERS:
  1264. * *cd - pointer to core data
  1265. * *hid_output - pointer to hid output data structure
  1266. ******************************************************************************/
  1267. static int pt_hid_output_validate_app_response(
  1268. struct pt_core_data *cd,
  1269. struct pt_hid_output *hid_output)
  1270. {
  1271. int command_code;
  1272. u16 size;
  1273. size = get_unaligned_le16(&cd->response_buf[0]);
  1274. if (hid_output->reset_expected && !size)
  1275. return 0;
  1276. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1277. != PT_PIP_NON_HID_RESPONSE_ID) {
  1278. pt_debug(cd->dev, DL_ERROR,
  1279. "%s: APP output response, wrong report_id\n", __func__);
  1280. return -EPROTO;
  1281. }
  1282. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1283. & PIP1_RESP_COMMAND_ID_MASK;
  1284. if (command_code != hid_output->command_code) {
  1285. pt_debug(cd->dev, DL_ERROR,
  1286. "%s: APP output response, wrong command_code:%X\n",
  1287. __func__, command_code);
  1288. return -EPROTO;
  1289. }
  1290. return 0;
  1291. }
  1292. /*******************************************************************************
  1293. * FUNCTION: pt_check_set_parameter
  1294. *
  1295. * SUMMARY: Check command input and response for Set Parameter command.And
  1296. * store the parameter to the list for resume work if pass the check.
  1297. *
  1298. * PARAMETERS:
  1299. * *cd - pointer to core data
  1300. * *hid_output - pointer to hid output data structure
  1301. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1302. ******************************************************************************/
  1303. static void pt_check_set_parameter(struct pt_core_data *cd,
  1304. struct pt_hid_output *hid_output, bool raw)
  1305. {
  1306. u8 *param_buf;
  1307. u32 param_value = 0;
  1308. u8 param_size;
  1309. u8 param_id;
  1310. int i = 0;
  1311. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1312. return;
  1313. /* Check command input for Set Parameter command */
  1314. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1315. && !memcmp(&hid_output->write_buf[0],
  1316. &cd->hid_desc.output_register,
  1317. sizeof(cd->hid_desc.output_register))
  1318. && hid_output->write_buf[4] ==
  1319. PT_PIP_NON_HID_COMMAND_ID
  1320. && hid_output->write_buf[6] ==
  1321. PIP1_CMD_ID_SET_PARAM)
  1322. param_buf = &hid_output->write_buf[7];
  1323. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1324. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1325. && hid_output->write_length >= 3
  1326. && hid_output->write_length <= 6)
  1327. param_buf = &hid_output->write_buf[0];
  1328. else
  1329. return;
  1330. /* Get parameter ID, size and value */
  1331. param_id = param_buf[0];
  1332. param_size = param_buf[1];
  1333. if (param_size > 4) {
  1334. pt_debug(cd->dev, DL_ERROR,
  1335. "%s: Invalid parameter size\n", __func__);
  1336. return;
  1337. }
  1338. param_buf = &param_buf[2];
  1339. while (i < param_size)
  1340. param_value += *(param_buf++) << (8 * i++);
  1341. /* Check command response for Set Parameter command */
  1342. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1343. || (cd->response_buf[4] &
  1344. PIP1_RESP_COMMAND_ID_MASK) !=
  1345. PIP1_CMD_ID_SET_PARAM
  1346. || cd->response_buf[5] != param_id
  1347. || cd->response_buf[6] != param_size) {
  1348. pt_debug(cd->dev, DL_ERROR,
  1349. "%s: Set Parameter command not successful\n",
  1350. __func__);
  1351. return;
  1352. }
  1353. pt_add_parameter(cd, param_id, param_value, param_size);
  1354. }
  1355. /*******************************************************************************
  1356. * FUNCTION: pt_check_command
  1357. *
  1358. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1359. * called here to check output command and store parameter to the list.
  1360. *
  1361. * PARAMETERS:
  1362. * *cd - pointer to core data
  1363. * *hid_output - pointer to hid output data structure
  1364. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1365. ******************************************************************************/
  1366. static void pt_check_command(struct pt_core_data *cd,
  1367. struct pt_hid_output *hid_output, bool raw)
  1368. {
  1369. pt_check_set_parameter(cd, hid_output, raw);
  1370. }
  1371. /*******************************************************************************
  1372. * FUNCTION: pt_hid_output_validate_response
  1373. *
  1374. * SUMMARY: Validate the response of application or bootloader command.
  1375. *
  1376. * RETURN:
  1377. * 0 = success
  1378. * !0 = failure
  1379. *
  1380. * PARAMETERS:
  1381. * *cd - pointer to core data
  1382. * *hid_output - pointer to hid output data structure
  1383. ******************************************************************************/
  1384. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1385. struct pt_hid_output *hid_output)
  1386. {
  1387. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1388. return pt_hid_output_validate_bl_response(cd, hid_output);
  1389. return pt_hid_output_validate_app_response(cd, hid_output);
  1390. }
  1391. /*******************************************************************************
  1392. * FUNCTION: pt_hid_send_output_user_
  1393. *
  1394. * SUMMARY: Blindly send user data to the DUT.
  1395. *
  1396. * RETURN:
  1397. * 0 = success
  1398. * !0 = failure
  1399. *
  1400. * PARAMETERS:
  1401. * *cd - pointer to core data
  1402. * *hid_output - pointer to the command to send
  1403. ******************************************************************************/
  1404. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1405. struct pt_hid_output *hid_output)
  1406. {
  1407. int rc = 0;
  1408. int cmd;
  1409. if (!hid_output->length || !hid_output->write_buf)
  1410. return -EINVAL;
  1411. if (cd->pip2_prot_active) {
  1412. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1413. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1414. } else
  1415. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1416. pt_debug(cd->dev, DL_INFO,
  1417. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1418. __func__, hid_output->length, cmd);
  1419. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1420. hid_output->length, ">>> User CMD");
  1421. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1422. hid_output->write_buf, NULL);
  1423. if (rc)
  1424. pt_debug(cd->dev, DL_ERROR,
  1425. "%s: Fail pt_adap_transfer\n", __func__);
  1426. return rc;
  1427. }
  1428. /*******************************************************************************
  1429. * FUNCTION: pt_hid_send_output_user_and_wait_
  1430. *
  1431. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1432. *
  1433. * RETURN:
  1434. * 0 = success
  1435. * !0 = failure
  1436. *
  1437. * PARAMETERS:
  1438. * *cd - pointer to core data
  1439. * *hid_output - pointer to the command to send
  1440. ******************************************************************************/
  1441. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1442. struct pt_hid_output *hid_output)
  1443. {
  1444. int rc = 0;
  1445. int t;
  1446. mutex_lock(&cd->system_lock);
  1447. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1448. mutex_unlock(&cd->system_lock);
  1449. rc = pt_hid_send_output_user_(cd, hid_output);
  1450. if (rc)
  1451. goto error;
  1452. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1453. msecs_to_jiffies(cd->pip_cmd_timeout));
  1454. if (IS_TMO(t)) {
  1455. #ifdef TTDL_DIAGNOSTICS
  1456. cd->bus_transmit_error_count++;
  1457. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1458. #endif /* TTDL_DIAGNOSTICS */
  1459. pt_debug(cd->dev, DL_ERROR,
  1460. "%s: HID output cmd execution timed out\n",
  1461. __func__);
  1462. rc = -ETIME;
  1463. goto error;
  1464. }
  1465. pt_check_command(cd, hid_output, true);
  1466. goto exit;
  1467. error:
  1468. mutex_lock(&cd->system_lock);
  1469. cd->hid_cmd_state = 0;
  1470. mutex_unlock(&cd->system_lock);
  1471. exit:
  1472. return rc;
  1473. }
  1474. /*******************************************************************************
  1475. * FUNCTION: pt_check_irq_asserted
  1476. *
  1477. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1478. * the FW can hold the INT line low ~150us after the read is complete.
  1479. * NOTE: if irq_stat is not defined this function will return false
  1480. *
  1481. * RETURN:
  1482. * true = IRQ asserted
  1483. * false = IRQ not asserted
  1484. *
  1485. * PARAMETERS:
  1486. * *cd - pointer to core data
  1487. ******************************************************************************/
  1488. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1489. {
  1490. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1491. /*
  1492. * Workaround for FW defect, CDT165308
  1493. * bl_launch app creates a glitch in IRQ line
  1494. */
  1495. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1496. && cd->cpdata->irq_stat) {
  1497. /*
  1498. * in X1S panel and GC1546 panel, the width for the INT
  1499. * glitch is about 4us,the normal INT width of response
  1500. * will last more than 200us, so use 10us delay
  1501. * for distinguish the glitch the normal INT is enough.
  1502. */
  1503. udelay(10);
  1504. }
  1505. #endif
  1506. if (cd->cpdata->irq_stat) {
  1507. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1508. == PT_IRQ_ASSERTED_VALUE) {
  1509. /* Debounce to allow FW to release INT */
  1510. usleep_range(100, 200);
  1511. }
  1512. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1513. == PT_IRQ_ASSERTED_VALUE)
  1514. return true;
  1515. else
  1516. return false;
  1517. }
  1518. return true;
  1519. }
  1520. /*******************************************************************************
  1521. * FUNCTION: pt_flush_bus
  1522. *
  1523. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1524. * Used if IRQ is found to be stuck low
  1525. *
  1526. * RETURN: Length of bytes read from bus
  1527. *
  1528. * PARAMETERS:
  1529. * *cd - pointer to core data
  1530. * flush_type - type of flush
  1531. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1532. * - PT_FLUSH_BUS_FULL_256_READ
  1533. * *read_buf - pointer to store read data
  1534. ******************************************************************************/
  1535. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1536. u8 flush_type, u8 *read_buf)
  1537. {
  1538. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1539. u16 pip_len;
  1540. int bytes_read;
  1541. int rc = 0;
  1542. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1543. rc = pt_adap_read_default(cd, buf, 2);
  1544. if (rc) {
  1545. bytes_read = 0;
  1546. goto exit;
  1547. }
  1548. pip_len = get_unaligned_le16(&buf[0]);
  1549. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1550. #ifdef TTDL_DIAGNOSTICS
  1551. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1552. #endif
  1553. bytes_read = 2;
  1554. pt_debug(cd->dev, DL_INFO,
  1555. "%s: Empty buf detected - len=0x%04X\n",
  1556. __func__, pip_len);
  1557. } else if (pip_len == 0) {
  1558. bytes_read = 0;
  1559. pt_debug(cd->dev, DL_INFO,
  1560. "%s: Sentinel detected\n", __func__);
  1561. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1562. pt_debug(cd->dev, DL_ERROR,
  1563. "%s: Illegal len=0x%04x, force %d byte read\n",
  1564. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1565. rc = pt_adap_read_default(cd, buf,
  1566. PT_MAX_PIP2_MSG_SIZE);
  1567. if (!rc)
  1568. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1569. else
  1570. bytes_read = 0;
  1571. } else {
  1572. pt_debug(cd->dev, DL_INFO,
  1573. "%s: Flush read of %d bytes...\n",
  1574. __func__, pip_len);
  1575. rc = pt_adap_read_default(cd, buf, pip_len);
  1576. if (!rc)
  1577. bytes_read = pip_len;
  1578. else
  1579. bytes_read = 0;
  1580. }
  1581. } else {
  1582. pt_debug(cd->dev, DL_INFO,
  1583. "%s: Forced flush of max %d bytes...\n",
  1584. __func__, PT_MAX_PIP2_MSG_SIZE);
  1585. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1586. if (!rc)
  1587. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1588. else
  1589. bytes_read = 0;
  1590. }
  1591. if (read_buf && (bytes_read > 3))
  1592. memcpy(read_buf, buf, bytes_read);
  1593. exit:
  1594. return bytes_read;
  1595. }
  1596. /*******************************************************************************
  1597. * FUNCTION: pt_flush_bus_if_irq_asserted
  1598. *
  1599. * SUMMARY: This function will flush the active bus if the INT is found to be
  1600. * asserted.
  1601. *
  1602. * RETURN: bytes cleared from bus
  1603. *
  1604. * PARAMETERS:
  1605. * *cd - pointer the core data structure
  1606. * flush_type - type of flush
  1607. * - PT_FLUSH_BUS_BASED_ON_LEN
  1608. * - PT_FLUSH_BUS_FULL_256_READ
  1609. ******************************************************************************/
  1610. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1611. {
  1612. int count = 0;
  1613. int bytes_read = 0;
  1614. while (pt_check_irq_asserted(cd) && count < 5) {
  1615. count++;
  1616. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1617. if (bytes_read) {
  1618. pt_debug(cd->dev, DL_WARN,
  1619. "%s: Cleared %d bytes off bus\n",
  1620. __func__, bytes_read);
  1621. }
  1622. }
  1623. if (pt_check_irq_asserted(cd)) {
  1624. pt_debug(cd->dev, DL_ERROR,
  1625. "%s: IRQ still asserted, %d bytes read\n",
  1626. __func__, bytes_read);
  1627. } else {
  1628. pt_debug(cd->dev, DL_INFO,
  1629. "%s: IRQ cleared, %d bytes read\n",
  1630. __func__, bytes_read);
  1631. }
  1632. return bytes_read;
  1633. }
  1634. /*******************************************************************************
  1635. * FUNCTION: pt_hid_send_output_
  1636. *
  1637. * SUMMARY: Send a touch application command to the DUT
  1638. *
  1639. * RETURN:
  1640. * 0 = success
  1641. * !0 = failure
  1642. *
  1643. * PARAMETERS:
  1644. * *cd - pointer to core data
  1645. * *hid_output - pointer to the command to send
  1646. ******************************************************************************/
  1647. static int pt_hid_send_output_(struct pt_core_data *cd,
  1648. struct pt_hid_output *hid_output)
  1649. {
  1650. int rc = 0;
  1651. u8 *cmd;
  1652. u16 length;
  1653. u16 crc;
  1654. u8 report_id;
  1655. u8 cmd_offset = 0;
  1656. u8 cmd_allocated = 0;
  1657. switch (hid_output->cmd_type) {
  1658. case PIP1_CMD_TYPE_FW:
  1659. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1660. length = 5;
  1661. break;
  1662. case PIP1_CMD_TYPE_BL:
  1663. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1664. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1665. break;
  1666. default:
  1667. return -EINVAL;
  1668. }
  1669. length += hid_output->write_length;
  1670. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1671. cmd = kzalloc(length + 2, GFP_KERNEL);
  1672. if (!cmd)
  1673. return -ENOMEM;
  1674. cmd_allocated = 1;
  1675. } else {
  1676. cmd = cd->cmd_buf;
  1677. }
  1678. /* Set Output register */
  1679. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1680. sizeof(cd->hid_desc.output_register));
  1681. cmd_offset += sizeof(cd->hid_desc.output_register);
  1682. cmd[cmd_offset++] = LOW_BYTE(length);
  1683. cmd[cmd_offset++] = HI_BYTE(length);
  1684. cmd[cmd_offset++] = report_id;
  1685. cmd[cmd_offset++] = 0x0; /* reserved */
  1686. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1687. cmd[cmd_offset++] = PIP1_BL_SOP;
  1688. cmd[cmd_offset++] = hid_output->command_code;
  1689. /* Set Data Length for bootloader */
  1690. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1691. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1692. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1693. }
  1694. /* Set Data */
  1695. if (hid_output->write_length && hid_output->write_buf) {
  1696. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1697. hid_output->write_length);
  1698. cmd_offset += hid_output->write_length;
  1699. }
  1700. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1701. crc = _pt_compute_crc(&cmd[6],
  1702. hid_output->write_length + 4);
  1703. cmd[cmd_offset++] = LOW_BYTE(crc);
  1704. cmd[cmd_offset++] = HI_BYTE(crc);
  1705. cmd[cmd_offset++] = PIP1_BL_EOP;
  1706. }
  1707. pt_debug(cd->dev, DL_INFO,
  1708. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1709. __func__, length + 2, hid_output->command_code);
  1710. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1711. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1712. if (rc)
  1713. pt_debug(cd->dev, DL_ERROR,
  1714. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1715. if (cmd_allocated)
  1716. kfree(cmd);
  1717. return rc;
  1718. }
  1719. /*******************************************************************************
  1720. * FUNCTION: pt_pip1_send_output_and_wait_
  1721. *
  1722. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1723. *
  1724. * RETURN:
  1725. * 0 = success
  1726. * !0 = failure
  1727. *
  1728. * PARAMETERS:
  1729. * *cd - pointer to core data
  1730. * *hid_output - pointer to the command to send
  1731. ******************************************************************************/
  1732. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1733. struct pt_hid_output *hid_output)
  1734. {
  1735. int rc = 0;
  1736. int t;
  1737. u16 timeout_ms;
  1738. mutex_lock(&cd->system_lock);
  1739. cd->hid_cmd_state = hid_output->command_code + 1;
  1740. mutex_unlock(&cd->system_lock);
  1741. if (hid_output->timeout_ms)
  1742. timeout_ms = hid_output->timeout_ms;
  1743. else
  1744. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1745. rc = pt_hid_send_output_(cd, hid_output);
  1746. if (rc)
  1747. goto error;
  1748. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1749. msecs_to_jiffies(timeout_ms));
  1750. if (IS_TMO(t)) {
  1751. #ifdef TTDL_DIAGNOSTICS
  1752. cd->bus_transmit_error_count++;
  1753. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1754. #endif /* TTDL_DIAGNOSTICS */
  1755. pt_debug(cd->dev, DL_ERROR,
  1756. "%s: HID output cmd execution timed out (%dms)\n",
  1757. __func__, timeout_ms);
  1758. rc = -ETIME;
  1759. goto error;
  1760. }
  1761. if (!hid_output->novalidate)
  1762. rc = pt_hid_output_validate_response(cd, hid_output);
  1763. pt_check_command(cd, hid_output, false);
  1764. goto exit;
  1765. error:
  1766. mutex_lock(&cd->system_lock);
  1767. cd->hid_cmd_state = 0;
  1768. mutex_unlock(&cd->system_lock);
  1769. exit:
  1770. return rc;
  1771. }
  1772. /*******************************************************************************
  1773. * FUNCTION: pt_hid_output_user_cmd_
  1774. *
  1775. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1776. * to the DUT waiting for the response and loading it into the read buffer
  1777. *
  1778. * RETURN:
  1779. * 0 = success
  1780. * !0 = failure
  1781. *
  1782. * PARAMETERS:
  1783. * *cd - pointer to core data
  1784. * read_len - expected read length of the response
  1785. * *read_buf - pointer to where the response will be loaded
  1786. * write_len - length of the write buffer
  1787. * *write_buf - pointer to the write buffer
  1788. * *actual_read_len - pointer to the actual amount of data read back
  1789. ******************************************************************************/
  1790. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1791. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1792. u16 *actual_read_len)
  1793. {
  1794. int rc = 0;
  1795. u16 size;
  1796. struct pt_hid_output hid_output = {
  1797. .length = write_len,
  1798. .write_buf = write_buf,
  1799. };
  1800. #ifdef TTHE_TUNER_SUPPORT
  1801. if (!cd->pip2_send_user_cmd) {
  1802. int command_code = 0;
  1803. int len;
  1804. /* Print up to cmd ID */
  1805. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1806. if (write_len < len)
  1807. len = write_len;
  1808. else
  1809. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1810. & PIP1_CMD_COMMAND_ID_MASK;
  1811. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1812. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1813. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1814. tthe_print(cd, write_buf, len, "CMD=");
  1815. }
  1816. #endif
  1817. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1818. if (rc)
  1819. return rc;
  1820. /* Get the response size from the first 2 bytes in the response */
  1821. size = get_unaligned_le16(&cd->response_buf[0]);
  1822. /* Ensure size is not greater than max buffer size */
  1823. if (size > PT_MAX_PIP2_MSG_SIZE)
  1824. size = PT_MAX_PIP2_MSG_SIZE;
  1825. /* Minimum size to read is the 2 byte len field */
  1826. if (size == 0)
  1827. size = 2;
  1828. if (size > read_len) {
  1829. pt_debug(cd->dev, DL_ERROR,
  1830. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1831. __func__, size, read_len);
  1832. *actual_read_len = 0;
  1833. return -EIO;
  1834. }
  1835. memcpy(read_buf, cd->response_buf, size);
  1836. *actual_read_len = size;
  1837. return 0;
  1838. }
  1839. /*******************************************************************************
  1840. * FUNCTION: pt_hid_output_user_cmd
  1841. *
  1842. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1843. * the DUT.
  1844. *
  1845. * RETURN:
  1846. * 0 = success
  1847. * !0 = failure
  1848. *
  1849. * PARAMETERS:
  1850. * *cd - pointer to core data
  1851. * read_len - length of data to read
  1852. * *read_buf - pointer to store read data
  1853. * write_len - length of data to write
  1854. * *write_buf - pointer to buffer to write
  1855. * *actual_read_len - pointer to store data length actually read
  1856. ******************************************************************************/
  1857. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1858. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1859. u16 *actual_read_len)
  1860. {
  1861. int rc;
  1862. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1863. if (rc < 0) {
  1864. pt_debug(cd->dev, DL_ERROR,
  1865. "%s: fail get exclusive ex=%p own=%p\n",
  1866. __func__, cd->exclusive_dev, cd->dev);
  1867. return rc;
  1868. }
  1869. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1870. write_len, write_buf, actual_read_len);
  1871. if (release_exclusive(cd, cd->dev) < 0)
  1872. pt_debug(cd->dev, DL_ERROR,
  1873. "%s: fail to release exclusive\n", __func__);
  1874. return rc;
  1875. }
  1876. /*******************************************************************************
  1877. * FUNCTION: _pt_request_pip2_send_cmd
  1878. *
  1879. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1880. * interrupt and reads response data to read_buf
  1881. *
  1882. * RETURN:
  1883. * 0 = success
  1884. * !0 = failure
  1885. *
  1886. * PARAMETERS:
  1887. * *dev - pointer to device structure
  1888. * protect - flag to run in protected mode
  1889. * id - ID of PIP command
  1890. * *data - pointer to PIP data payload
  1891. * report_body_len - report length
  1892. * *read_buf - pointer to response buffer
  1893. * *actual_read_len - pointer to response buffer length
  1894. ******************************************************************************/
  1895. static int _pt_request_pip2_send_cmd(struct device *dev,
  1896. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1897. u16 *actual_read_len)
  1898. {
  1899. struct pt_core_data *cd = dev_get_drvdata(dev);
  1900. struct pip2_cmd_structure pip2_cmd;
  1901. int rc = 0;
  1902. int i = 0;
  1903. int j = 0;
  1904. u16 write_len;
  1905. u8 *write_buf = NULL;
  1906. u16 read_len;
  1907. u8 extra_bytes;
  1908. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1909. /* Hard coded register for PIP2.x */
  1910. pip2_cmd.reg[0] = 0x01;
  1911. pip2_cmd.reg[1] = 0x01;
  1912. /*
  1913. * For PIP2.1+ the length field value includes itself:
  1914. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1915. *
  1916. * The overall write length must include only the register:
  1917. * ADD 2: 2 (Register)
  1918. */
  1919. extra_bytes = 6;
  1920. write_len = 2;
  1921. /* PIP2 the CMD ID is a 7bit field */
  1922. if (id > PIP2_CMD_ID_END) {
  1923. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1924. __func__, id);
  1925. rc = -EINVAL;
  1926. goto exit;
  1927. }
  1928. pip2_cmd.len = report_body_len + extra_bytes;
  1929. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1930. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1931. pip2_cmd.data = data;
  1932. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1933. /* Add the command length to the extra bytes based on PIP version */
  1934. write_len += pip2_cmd.len;
  1935. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1936. __func__, pip2_cmd.len, write_len);
  1937. write_buf = kzalloc(write_len, GFP_KERNEL);
  1938. if (write_buf == NULL) {
  1939. rc = -ENOMEM;
  1940. goto exit;
  1941. }
  1942. write_buf[i++] = pip2_cmd.reg[0];
  1943. write_buf[i++] = pip2_cmd.reg[1];
  1944. write_buf[i++] = pip2_cmd.len & 0xff;
  1945. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1946. write_buf[i++] = pip2_cmd.seq;
  1947. write_buf[i++] = pip2_cmd.id;
  1948. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1949. write_buf[j] = pip2_cmd.data[j-i];
  1950. write_buf[j++] = pip2_cmd.crc[0];
  1951. write_buf[j++] = pip2_cmd.crc[1];
  1952. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1953. if (read_len < 0)
  1954. read_len = 255;
  1955. pt_debug(dev, DL_INFO,
  1956. "%s cmd_id[0x%02X] expected response length:%d ",
  1957. __func__, pip2_cmd.id, read_len);
  1958. /*
  1959. * All PIP2 commands come through this function.
  1960. * Set flag for PIP2.x interface to allow response parsing to know
  1961. * how to decode the protocol header.
  1962. */
  1963. mutex_lock(&cd->system_lock);
  1964. cd->pip2_prot_active = true;
  1965. cd->pip2_send_user_cmd = true;
  1966. mutex_unlock(&cd->system_lock);
  1967. if (protect == PT_CORE_CMD_PROTECTED)
  1968. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1969. write_len, write_buf, actual_read_len);
  1970. else {
  1971. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1972. write_len, write_buf, actual_read_len);
  1973. }
  1974. if (rc) {
  1975. pt_debug(dev, DL_ERROR,
  1976. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1977. __func__, rc);
  1978. goto exit;
  1979. }
  1980. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1981. *actual_read_len);
  1982. exit:
  1983. mutex_lock(&cd->system_lock);
  1984. cd->pip2_prot_active = false;
  1985. cd->pip2_send_user_cmd = false;
  1986. mutex_unlock(&cd->system_lock);
  1987. kfree(write_buf);
  1988. return rc;
  1989. }
  1990. /*******************************************************************************
  1991. * FUNCTION: _pt_pip2_send_cmd_no_int
  1992. *
  1993. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1994. * reads response data to read_buf if response is available.
  1995. *
  1996. * NOTE:
  1997. * Interrupt MUST be disabled before to call this function.
  1998. *
  1999. * RETURN:
  2000. * 0 = success
  2001. * !0 = failure
  2002. *
  2003. * PARAMETERS:
  2004. * *dev - pointer to device structure
  2005. * protect - flag to run in protected mode
  2006. * id - ID of PIP command
  2007. * *data - pointer to PIP data payload
  2008. * report_body_len - report length
  2009. * *read_buf - pointer to response buffer
  2010. * *actual_read_len - pointer to response buffer length
  2011. ******************************************************************************/
  2012. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2013. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2014. u16 *actual_read_len)
  2015. {
  2016. int max_retry = 0;
  2017. int retry = 0;
  2018. int rc = 0;
  2019. int i = 0;
  2020. int j = 0;
  2021. u16 write_len;
  2022. u8 *write_buf = NULL;
  2023. u16 read_len;
  2024. u16 size = 0;
  2025. u8 response_seq = 0;
  2026. u8 extra_bytes;
  2027. u32 retry_interval = 0;
  2028. u32 retry_total_time = 0;
  2029. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2030. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2031. struct pt_core_data *cd = dev_get_drvdata(dev);
  2032. struct pip2_cmd_structure pip2_cmd;
  2033. if (protect == PT_CORE_CMD_PROTECTED) {
  2034. rc = request_exclusive(cd,
  2035. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2036. if (rc < 0) {
  2037. pt_debug(cd->dev, DL_ERROR,
  2038. "%s: fail get exclusive ex=%p own=%p\n",
  2039. __func__, cd->exclusive_dev, cd->dev);
  2040. return rc;
  2041. }
  2042. }
  2043. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2044. /* Hard coded register for PIP2.x */
  2045. pip2_cmd.reg[0] = 0x01;
  2046. pip2_cmd.reg[1] = 0x01;
  2047. /*
  2048. * For PIP2.1+ the length field value includes itself:
  2049. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2050. *
  2051. * The overall write length must include only the register:
  2052. * ADD 2: 2 (Register)
  2053. */
  2054. extra_bytes = 6;
  2055. write_len = 2;
  2056. pip2_cmd.len = report_body_len + extra_bytes;
  2057. pip2_cmd.id = id;
  2058. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2059. pip2_cmd.data = data;
  2060. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2061. /* Add the command length to the extra bytes based on PIP version */
  2062. write_len += pip2_cmd.len;
  2063. write_buf = kzalloc(write_len, GFP_KERNEL);
  2064. if (write_buf == NULL) {
  2065. rc = -ENOMEM;
  2066. goto exit;
  2067. }
  2068. write_buf[i++] = pip2_cmd.reg[0];
  2069. write_buf[i++] = pip2_cmd.reg[1];
  2070. write_buf[i++] = pip2_cmd.len & 0xff;
  2071. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2072. write_buf[i++] = pip2_cmd.seq;
  2073. write_buf[i++] = pip2_cmd.id;
  2074. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2075. write_buf[j] = pip2_cmd.data[j-i];
  2076. write_buf[j++] = pip2_cmd.crc[0];
  2077. write_buf[j++] = pip2_cmd.crc[1];
  2078. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2079. if (read_len < 0)
  2080. read_len = 255;
  2081. pt_debug(dev, DL_INFO,
  2082. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2083. __func__, pip2_cmd.id, read_len);
  2084. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2085. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2086. if (rc) {
  2087. pt_debug(dev, DL_ERROR,
  2088. "%s: SPI write Error = %d\n",
  2089. __func__, rc);
  2090. goto exit;
  2091. }
  2092. #ifdef PT_POLL_RESP_BY_BUS
  2093. /*
  2094. * Frequent bus read can increase system load obviously. The expected
  2095. * first bus read should be valid and timely. The tollerance for
  2096. * usleep_range should be limited. The minimum response delay (between
  2097. * command finishes sending and INT pin falls) is less than 50
  2098. * microseconds. So the 10 microseconds should be maximum tollerance
  2099. * with the consideration that the unit to calculate the response delay
  2100. * is 10 microseconds and more precise is not necessary. Every
  2101. * additional 10 microseconds only contribute less than 3 milliseconds
  2102. * for whole BL.
  2103. */
  2104. usleep_range(resp_time_min, resp_time_min+10);
  2105. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2106. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2107. rc = pt_adap_read_default(cd, read_buf, read_len);
  2108. if (rc) {
  2109. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2110. __func__, rc);
  2111. break;
  2112. }
  2113. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2114. size = get_unaligned_le16(&read_buf[0]);
  2115. if ((size <= read_len) &&
  2116. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2117. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2118. break;
  2119. }
  2120. /*
  2121. * To reduce the bus and system load, increase the sleep
  2122. * step gradually:
  2123. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2124. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2125. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2126. */
  2127. retry++;
  2128. if (retry < 20) {
  2129. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2130. usleep_range(retry_interval,
  2131. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2132. } else if (retry < 40) {
  2133. retry_interval += 1000;
  2134. usleep_range(retry_interval, retry_interval + 1000);
  2135. } else {
  2136. retry_interval += 50000;
  2137. msleep(retry_interval/1000);
  2138. }
  2139. retry_total_time += retry_interval;
  2140. }
  2141. #else
  2142. /*
  2143. * Frequent GPIO read will not increase CPU/system load heavily if the
  2144. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2145. * fixed interval: 20 us.
  2146. */
  2147. usleep_range(resp_time_min, resp_time_min+10);
  2148. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2149. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2150. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2151. rc = pt_adap_read_default(cd, read_buf, read_len);
  2152. size = get_unaligned_le16(&read_buf[0]);
  2153. if (rc)
  2154. pt_debug(dev, DL_ERROR,
  2155. "%s: SPI read Error = %d\n",
  2156. __func__, rc);
  2157. else if (size > read_len) {
  2158. pt_debug(cd->dev, DL_ERROR,
  2159. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2160. __func__, size, read_len);
  2161. rc = -EIO;
  2162. }
  2163. break;
  2164. }
  2165. /*
  2166. * Poll GPIO with fixed interval 20 us, and tollerance is
  2167. * limited to 10 us to speed up the process.
  2168. */
  2169. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2170. usleep_range(retry_interval, retry_interval+10);
  2171. retry_total_time += retry_interval;
  2172. }
  2173. #endif
  2174. *actual_read_len = size;
  2175. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2176. pt_debug(dev, DL_ERROR,
  2177. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2178. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2179. *actual_read_len = 0;
  2180. rc = -EINVAL;
  2181. }
  2182. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2183. "<<< NO_INT Read");
  2184. exit:
  2185. kfree(write_buf);
  2186. if (protect == PT_CORE_CMD_PROTECTED) {
  2187. if (release_exclusive(cd, cd->dev) < 0)
  2188. pt_debug(cd->dev, DL_ERROR,
  2189. "%s: fail to release exclusive\n", __func__);
  2190. }
  2191. return rc;
  2192. }
  2193. /*******************************************************************************
  2194. * FUNCTION: pt_pip_null_
  2195. *
  2196. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2197. * This function is used by watchdog to check if the fw corrupts.
  2198. *
  2199. * RETURN:
  2200. * 0 = success
  2201. * !0 = failure
  2202. *
  2203. * PARAMETERS:
  2204. * *cd - pointer to core data
  2205. ******************************************************************************/
  2206. static int pt_pip_null_(struct pt_core_data *cd)
  2207. {
  2208. struct pt_hid_output hid_output = {
  2209. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2210. };
  2211. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2212. }
  2213. /*******************************************************************************
  2214. * FUNCTION: pt_pip_null
  2215. *
  2216. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2217. *
  2218. * RETURN:
  2219. * 0 = success
  2220. * !0 = failure
  2221. *
  2222. * PARAMETERS:
  2223. * *cd - pointer to core data
  2224. ******************************************************************************/
  2225. static int pt_pip_null(struct pt_core_data *cd)
  2226. {
  2227. int rc;
  2228. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2229. if (rc < 0) {
  2230. pt_debug(cd->dev, DL_ERROR,
  2231. "%s: fail get exclusive ex=%p own=%p\n",
  2232. __func__, cd->exclusive_dev, cd->dev);
  2233. return rc;
  2234. }
  2235. rc = pt_pip_null_(cd);
  2236. if (release_exclusive(cd, cd->dev) < 0)
  2237. pt_debug(cd->dev, DL_ERROR,
  2238. "%s: fail to release exclusive\n", __func__);
  2239. return rc;
  2240. }
  2241. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2242. /*******************************************************************************
  2243. * FUNCTION: pt_pip_start_bootloader_
  2244. *
  2245. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2246. *
  2247. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2248. * the WD active could cause this function to fail!
  2249. *
  2250. * RETURN:
  2251. * 0 = success
  2252. * !0 = failure
  2253. *
  2254. * PARAMETERS:
  2255. * *cd - pointer to core data
  2256. ******************************************************************************/
  2257. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2258. {
  2259. int rc = 0;
  2260. struct pt_hid_output hid_output = {
  2261. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2262. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2263. .reset_expected = 1,
  2264. };
  2265. if (cd->watchdog_enabled) {
  2266. pt_debug(cd->dev, DL_WARN,
  2267. "%s: watchdog isn't stopped before enter bl\n",
  2268. __func__);
  2269. goto exit;
  2270. }
  2271. /* Reset startup status after entering BL, new DUT enum required */
  2272. cd->startup_status = STARTUP_STATUS_START;
  2273. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2274. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2275. if (rc) {
  2276. pt_debug(cd->dev, DL_ERROR,
  2277. "%s: Start BL PIP cmd failed. rc = %d\n",
  2278. __func__, rc);
  2279. }
  2280. exit:
  2281. return rc;
  2282. }
  2283. /*******************************************************************************
  2284. * FUNCTION: pt_pip_start_bootloader
  2285. *
  2286. * SUMMARY: Protected function to force DUT to enter the BL
  2287. *
  2288. * RETURN:
  2289. * 0 = success
  2290. * !0 = failure
  2291. *
  2292. * PARAMETERS:
  2293. * *cd - pointer to core data structure
  2294. ******************************************************************************/
  2295. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2296. {
  2297. int rc;
  2298. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2299. if (rc < 0) {
  2300. pt_debug(cd->dev, DL_ERROR,
  2301. "%s: fail get exclusive ex=%p own=%p\n",
  2302. __func__, cd->exclusive_dev, cd->dev);
  2303. return rc;
  2304. }
  2305. rc = pt_pip_start_bootloader_(cd);
  2306. if (release_exclusive(cd, cd->dev) < 0)
  2307. pt_debug(cd->dev, DL_ERROR,
  2308. "%s: fail to release exclusive\n", __func__);
  2309. return rc;
  2310. }
  2311. /*******************************************************************************
  2312. * FUNCTION: _pt_request_pip_start_bl
  2313. *
  2314. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2315. * modules to request the DUT to enter the BL
  2316. *
  2317. * RETURN:
  2318. * 0 = success
  2319. * !0 = failure
  2320. *
  2321. * PARAMETERS:
  2322. * *dev - pointer to device structure
  2323. * protect - flag to run in protected mode
  2324. ******************************************************************************/
  2325. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2326. {
  2327. struct pt_core_data *cd = dev_get_drvdata(dev);
  2328. if (protect)
  2329. return pt_pip_start_bootloader(cd);
  2330. return pt_pip_start_bootloader_(cd);
  2331. }
  2332. /*******************************************************************************
  2333. * FUNCTION: pt_pip2_ver_load_ttdata
  2334. *
  2335. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2336. * command into the core data struct.
  2337. *
  2338. * RETURN: n/a
  2339. *
  2340. * PARAMETERS:
  2341. * *cd - pointer to core data structure
  2342. * len - Length of data in response_buf
  2343. ******************************************************************************/
  2344. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2345. {
  2346. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2347. struct pt_pip2_version_full *full_ver;
  2348. struct pt_pip2_version *ver;
  2349. /*
  2350. * The PIP2 VERSION command can return different lengths of data.
  2351. * The additional LOT fields are included when the packet
  2352. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2353. * NOTE:
  2354. * - The FW would swap the BL and FW versions when reporting
  2355. * the small packet.
  2356. * - Sub Lot bytes 16 and 17 are reserved.
  2357. */
  2358. if (len >= 0x1D) {
  2359. full_ver = (struct pt_pip2_version_full *)
  2360. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2361. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2362. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2363. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2364. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2365. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2366. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2367. /*
  2368. * BL PIP 2.02 and greater the version fields are
  2369. * swapped
  2370. */
  2371. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2372. ttdata->chip_rev =
  2373. get_unaligned_le16(&full_ver->chip_rev);
  2374. ttdata->chip_id =
  2375. get_unaligned_le16(&full_ver->chip_id);
  2376. } else {
  2377. ttdata->chip_rev =
  2378. get_unaligned_le16(&full_ver->chip_id);
  2379. ttdata->chip_id =
  2380. get_unaligned_le16(&full_ver->chip_rev);
  2381. }
  2382. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2383. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2384. sizeof(struct pt_pip2_version_full),
  2385. "PIP2 VERSION FULL");
  2386. } else {
  2387. ver = (struct pt_pip2_version *)
  2388. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2389. ttdata->pip_ver_major = ver->pip2_version_msb;
  2390. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2391. ttdata->bl_ver_major = ver->bl_version_msb;
  2392. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2393. ttdata->fw_ver_major = ver->fw_version_msb;
  2394. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2395. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2396. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2397. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2398. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2399. }
  2400. }
  2401. /*******************************************************************************
  2402. * FUNCTION: pt_si_get_ttdata
  2403. *
  2404. * SUMMARY: Function to load the version information from the system information
  2405. * PIP command into the core data struct.
  2406. *
  2407. * RETURN: n/a
  2408. *
  2409. * PARAMETERS:
  2410. * *cd - pointer to core data structure
  2411. ******************************************************************************/
  2412. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2413. {
  2414. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2415. struct pt_ttdata_dev *ttdata_dev =
  2416. (struct pt_ttdata_dev *)
  2417. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2418. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2419. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2420. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2421. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2422. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2423. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2424. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2425. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2426. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2427. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2428. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2429. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2430. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2431. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2432. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2433. }
  2434. /*******************************************************************************
  2435. * FUNCTION: pt_si_get_sensing_conf_data
  2436. *
  2437. * SUMMARY: Function to load the sensing information from the system information
  2438. * PIP command into the core data struct.
  2439. *
  2440. * RETURN: n/a
  2441. *
  2442. * PARAMETERS:
  2443. * *cd - pointer to core data structure
  2444. ******************************************************************************/
  2445. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2446. {
  2447. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2448. struct pt_sensing_conf_data_dev *scd_dev =
  2449. (struct pt_sensing_conf_data_dev *)
  2450. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2451. scd->electrodes_x = scd_dev->electrodes_x;
  2452. scd->electrodes_y = scd_dev->electrodes_y;
  2453. scd->origin_x = scd_dev->origin_x;
  2454. scd->origin_y = scd_dev->origin_y;
  2455. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2456. if (scd->origin_x & 0x02) {
  2457. scd->tx_num = scd->electrodes_x;
  2458. scd->rx_num = scd->electrodes_y;
  2459. } else {
  2460. scd->tx_num = scd->electrodes_y;
  2461. scd->rx_num = scd->electrodes_x;
  2462. }
  2463. /*
  2464. * When the Panel ID is coming from an XY pin and not a dedicated
  2465. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2466. * other DUTs as the loader will use cd->pid_for_loader to generate
  2467. * the bin file name but will ignore it if pid_for_loader is still
  2468. * set to PANEL_ID_NOT_ENABLED
  2469. */
  2470. if (cd->panel_id_support &
  2471. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2472. mutex_lock(&cd->system_lock);
  2473. cd->pid_for_loader = scd_dev->panel_id;
  2474. mutex_unlock(&cd->system_lock);
  2475. }
  2476. scd->panel_id = scd_dev->panel_id;
  2477. scd->btn = scd_dev->btn;
  2478. scd->scan_mode = scd_dev->scan_mode;
  2479. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2480. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2481. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2482. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2483. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2484. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2485. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2486. sizeof(struct pt_sensing_conf_data_dev),
  2487. "sensing_conf_data");
  2488. }
  2489. /*******************************************************************************
  2490. * FUNCTION: pt_si_setup
  2491. *
  2492. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2493. *
  2494. * RETURN:
  2495. * 0 = success
  2496. * !0 = failure
  2497. *
  2498. * PARAMETERS:
  2499. * *cd - pointer to core data structure
  2500. ******************************************************************************/
  2501. static int pt_si_setup(struct pt_core_data *cd)
  2502. {
  2503. struct pt_sysinfo *si = &cd->sysinfo;
  2504. int max_tch = si->sensing_conf_data.max_tch;
  2505. if (!si->xy_data)
  2506. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2507. GFP_KERNEL);
  2508. if (!si->xy_data)
  2509. return -ENOMEM;
  2510. if (!si->xy_mode)
  2511. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2512. if (!si->xy_mode) {
  2513. kfree(si->xy_data);
  2514. return -ENOMEM;
  2515. }
  2516. return 0;
  2517. }
  2518. /*******************************************************************************
  2519. * FUNCTION: pt_si_get_btn_data
  2520. *
  2521. * SUMMARY: Setup the core data button information based on the response of the
  2522. * System Information PIP command.
  2523. *
  2524. * RETURN:
  2525. * 0 = success
  2526. * !0 = failure
  2527. *
  2528. * PARAMETERS:
  2529. * *cd - pointer to core data structure
  2530. ******************************************************************************/
  2531. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2532. {
  2533. struct pt_sysinfo *si = &cd->sysinfo;
  2534. int num_btns = 0;
  2535. int num_defined_keys;
  2536. u16 *key_table;
  2537. int btn;
  2538. int i;
  2539. int rc = 0;
  2540. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2541. & PIP1_SYSINFO_BTN_MASK;
  2542. size_t btn_keys_size;
  2543. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2544. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2545. if (btns & (1 << i))
  2546. num_btns++;
  2547. }
  2548. si->num_btns = num_btns;
  2549. if (num_btns) {
  2550. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2551. if (!si->btn)
  2552. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2553. if (!si->btn)
  2554. return -ENOMEM;
  2555. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2556. num_defined_keys = 0;
  2557. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2558. num_defined_keys = 0;
  2559. else
  2560. num_defined_keys = cd->cpdata->sett
  2561. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2562. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2563. key_table = (u16 *)cd->cpdata->sett
  2564. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2565. si->btn[btn].key_code = key_table[btn];
  2566. si->btn[btn].enabled = true;
  2567. }
  2568. for (; btn < num_btns; btn++) {
  2569. si->btn[btn].key_code = KEY_RESERVED;
  2570. si->btn[btn].enabled = true;
  2571. }
  2572. return rc;
  2573. }
  2574. kfree(si->btn);
  2575. si->btn = NULL;
  2576. return rc;
  2577. }
  2578. /*******************************************************************************
  2579. * FUNCTION: pt_si_put_log_data
  2580. *
  2581. * SUMMARY: Prints all sys info data to kmsg log
  2582. *
  2583. * RETURN: n/a
  2584. *
  2585. * PARAMETERS:
  2586. * *cd - pointer to core data structure
  2587. ******************************************************************************/
  2588. static void pt_si_put_log_data(struct pt_core_data *cd)
  2589. {
  2590. struct pt_sysinfo *si = &cd->sysinfo;
  2591. struct pt_ttdata *ttdata = &si->ttdata;
  2592. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2593. int i;
  2594. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2595. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2596. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2597. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2599. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2601. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2603. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2605. __func__, ttdata->revctrl, ttdata->revctrl);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2607. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2608. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2609. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2610. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2611. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2612. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2613. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2614. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2615. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2616. for (i = 0; i < PT_NUM_MFGID; i++)
  2617. pt_debug(cd->dev, DL_DEBUG,
  2618. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2619. __func__, i, ttdata->mfg_id[i],
  2620. ttdata->mfg_id[i]);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2622. __func__, ttdata->post_code, ttdata->post_code);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2624. __func__, scd->electrodes_x, scd->electrodes_x);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2626. __func__, scd->electrodes_y, scd->electrodes_y);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2628. __func__, scd->len_x, scd->len_x);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2630. __func__, scd->len_y, scd->len_y);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2632. __func__, scd->res_x, scd->res_x);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2634. __func__, scd->res_y, scd->res_y);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2636. __func__, scd->max_z, scd->max_z);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2638. __func__, scd->origin_x, scd->origin_x);
  2639. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2640. __func__, scd->origin_y, scd->origin_y);
  2641. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2642. __func__, scd->panel_id, scd->panel_id);
  2643. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2644. __func__, scd->btn, scd->btn);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2646. __func__, scd->scan_mode, scd->scan_mode);
  2647. pt_debug(cd->dev, DL_DEBUG,
  2648. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2649. __func__, scd->max_tch, scd->max_tch);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2651. __func__, si->xy_mode);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2653. __func__, si->xy_data);
  2654. }
  2655. /*******************************************************************************
  2656. * FUNCTION: pt_get_sysinfo_regs
  2657. *
  2658. * SUMMARY: Setup all the core data System information based on the response
  2659. * of the System Information PIP command.
  2660. *
  2661. * RETURN:
  2662. * 0 = success
  2663. * !0 = failure
  2664. *
  2665. * PARAMETERS:
  2666. * *cd - pointer to core data structure
  2667. ******************************************************************************/
  2668. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2669. {
  2670. struct pt_sysinfo *si = &cd->sysinfo;
  2671. int rc;
  2672. rc = pt_si_get_btn_data(cd);
  2673. if (rc < 0)
  2674. return rc;
  2675. pt_si_get_ttdata(cd);
  2676. pt_si_get_sensing_conf_data(cd);
  2677. pt_si_setup(cd);
  2678. pt_si_put_log_data(cd);
  2679. si->ready = true;
  2680. return rc;
  2681. }
  2682. /*******************************************************************************
  2683. * FUNCTION: pt_free_si_ptrs
  2684. *
  2685. * SUMMARY: Frees all memory associated with the System Information within
  2686. * core data
  2687. *
  2688. * RETURN: n/a
  2689. *
  2690. * PARAMETERS:
  2691. * *cd - pointer to core data structure
  2692. ******************************************************************************/
  2693. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2694. {
  2695. struct pt_sysinfo *si = &cd->sysinfo;
  2696. kfree(si->btn);
  2697. kfree(si->xy_mode);
  2698. kfree(si->xy_data);
  2699. }
  2700. /*******************************************************************************
  2701. * FUNCTION: pt_hid_output_get_sysinfo_
  2702. *
  2703. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2704. * response.
  2705. *
  2706. * RETURN::
  2707. * 0 = success
  2708. * !0 = failure
  2709. *
  2710. * PARAMETERS:
  2711. * *cd - pointer to core data structure
  2712. ******************************************************************************/
  2713. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2714. {
  2715. int rc = 0;
  2716. struct pt_hid_output hid_output = {
  2717. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2718. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2719. };
  2720. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2721. if (rc)
  2722. return rc;
  2723. /* Parse the sysinfo data */
  2724. rc = pt_get_sysinfo_regs(cd);
  2725. if (rc)
  2726. pt_free_si_ptrs(cd);
  2727. return rc;
  2728. }
  2729. /*******************************************************************************
  2730. * FUNCTION: pt_hid_output_get_sysinfo
  2731. *
  2732. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2733. *
  2734. * RETURN::
  2735. * 0 = success
  2736. * !0 = failure
  2737. *
  2738. * PARAMETERS:
  2739. * *cd - pointer to core data structure
  2740. ******************************************************************************/
  2741. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2742. {
  2743. int rc;
  2744. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2745. if (rc < 0) {
  2746. pt_debug(cd->dev, DL_ERROR,
  2747. "%s: fail get exclusive ex=%p own=%p\n",
  2748. __func__, cd->exclusive_dev, cd->dev);
  2749. return rc;
  2750. }
  2751. rc = pt_hid_output_get_sysinfo_(cd);
  2752. if (release_exclusive(cd, cd->dev) < 0)
  2753. pt_debug(cd->dev, DL_ERROR,
  2754. "%s: fail to release exclusive\n", __func__);
  2755. return rc;
  2756. }
  2757. /*******************************************************************************
  2758. * FUNCTION: pt_pip_suspend_scanning_
  2759. *
  2760. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2761. *
  2762. * RETURN::
  2763. * 0 = success
  2764. * !0 = failure
  2765. *
  2766. * PARAMETERS:
  2767. * *cd - pointer to core data structure
  2768. ******************************************************************************/
  2769. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2770. {
  2771. int rc = 0;
  2772. struct pt_hid_output hid_output = {
  2773. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2774. };
  2775. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2776. if (rc) {
  2777. pt_debug(cd->dev, DL_ERROR,
  2778. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2779. __func__, rc);
  2780. }
  2781. return rc;
  2782. }
  2783. /*******************************************************************************
  2784. * FUNCTION: pt_pip_suspend_scanning
  2785. *
  2786. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2787. *
  2788. * RETURN::
  2789. * 0 = success
  2790. * !0 = failure
  2791. *
  2792. * PARAMETERS:
  2793. * *cd - pointer to core data structure
  2794. ******************************************************************************/
  2795. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2796. {
  2797. int rc;
  2798. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2799. if (rc < 0) {
  2800. pt_debug(cd->dev, DL_ERROR,
  2801. "%s: fail get exclusive ex=%p own=%p\n",
  2802. __func__, cd->exclusive_dev, cd->dev);
  2803. return rc;
  2804. }
  2805. rc = pt_pip_suspend_scanning_(cd);
  2806. if (release_exclusive(cd, cd->dev) < 0)
  2807. pt_debug(cd->dev, DL_ERROR,
  2808. "%s: fail to release exclusive\n", __func__);
  2809. return rc;
  2810. }
  2811. /*******************************************************************************
  2812. * FUNCTION: _pt_request_pip_suspend_scanning
  2813. *
  2814. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2815. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2816. *
  2817. * RETURN:
  2818. * 0 = success
  2819. * !0 = failure
  2820. *
  2821. * PARAMETERS:
  2822. * *dev - pointer to device structure
  2823. * protect - 0 = call non-protected function
  2824. * 1 = call protected function
  2825. ******************************************************************************/
  2826. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2827. int protect)
  2828. {
  2829. struct pt_core_data *cd = dev_get_drvdata(dev);
  2830. if (protect)
  2831. return pt_pip_suspend_scanning(cd);
  2832. return pt_pip_suspend_scanning_(cd);
  2833. }
  2834. /*******************************************************************************
  2835. * FUNCTION: pt_pip_resume_scanning_
  2836. *
  2837. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2838. *
  2839. * RETURN::
  2840. * 0 = success
  2841. * !0 = failure
  2842. *
  2843. * PARAMETERS:
  2844. * *cd - pointer to core data structure
  2845. ******************************************************************************/
  2846. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2847. {
  2848. int rc = 0;
  2849. struct pt_hid_output hid_output = {
  2850. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2851. };
  2852. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2853. if (rc) {
  2854. pt_debug(cd->dev, DL_ERROR,
  2855. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2856. __func__, rc);
  2857. }
  2858. return rc;
  2859. }
  2860. /*******************************************************************************
  2861. * FUNCTION: pt_pip_resume_scanning
  2862. *
  2863. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2864. *
  2865. * RETURN::
  2866. * 0 = success
  2867. * !0 = failure
  2868. *
  2869. * PARAMETERS:
  2870. * *cd - pointer to core data structure
  2871. ******************************************************************************/
  2872. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2873. {
  2874. int rc;
  2875. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2876. if (rc < 0) {
  2877. pt_debug(cd->dev, DL_ERROR,
  2878. "%s: fail get exclusive ex=%p own=%p\n",
  2879. __func__, cd->exclusive_dev, cd->dev);
  2880. return rc;
  2881. }
  2882. rc = pt_pip_resume_scanning_(cd);
  2883. if (release_exclusive(cd, cd->dev) < 0)
  2884. pt_debug(cd->dev, DL_ERROR,
  2885. "%s: fail to release exclusive\n", __func__);
  2886. return rc;
  2887. }
  2888. /*******************************************************************************
  2889. * FUNCTION: _pt_request_pip_resume_scanning
  2890. *
  2891. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2892. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2893. *
  2894. * RETURN::
  2895. * 0 = success
  2896. * !0 = failure
  2897. *
  2898. * PARAMETERS:
  2899. * *dev - pointer to device structure
  2900. * protect - 0 = call non-protected function
  2901. * 1 = call protected function
  2902. ******************************************************************************/
  2903. static int _pt_request_pip_resume_scanning(struct device *dev,
  2904. int protect)
  2905. {
  2906. struct pt_core_data *cd = dev_get_drvdata(dev);
  2907. if (protect)
  2908. return pt_pip_resume_scanning(cd);
  2909. return pt_pip_resume_scanning_(cd);
  2910. }
  2911. /*******************************************************************************
  2912. * FUNCTION: pt_pip_get_param_
  2913. *
  2914. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2915. * the 32bit parameter value
  2916. *
  2917. * RETURN:
  2918. * 0 = success
  2919. * !0 = failure
  2920. *
  2921. * PARAMETERS:
  2922. * *cd - pointer to core data
  2923. * param_id - parameter ID to retrieve
  2924. * *value - value of DUT parameter
  2925. ******************************************************************************/
  2926. static int pt_pip_get_param_(struct pt_core_data *cd,
  2927. u8 param_id, u32 *value)
  2928. {
  2929. int write_length = 1;
  2930. u8 param[1] = { param_id };
  2931. u8 read_param_id;
  2932. int param_size;
  2933. u8 *ptr;
  2934. int rc = 0;
  2935. int i;
  2936. struct pt_hid_output hid_output = {
  2937. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2938. .write_length = write_length,
  2939. .write_buf = param,
  2940. };
  2941. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2942. if (rc)
  2943. return rc;
  2944. read_param_id = cd->response_buf[5];
  2945. if (read_param_id != param_id)
  2946. return -EPROTO;
  2947. param_size = cd->response_buf[6];
  2948. ptr = &cd->response_buf[7];
  2949. *value = 0;
  2950. for (i = 0; i < param_size; i++)
  2951. *value += ptr[i] << (i * 8);
  2952. return 0;
  2953. }
  2954. /*******************************************************************************
  2955. * FUNCTION: pt_pip_get_param
  2956. *
  2957. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2958. *
  2959. * RETURN:
  2960. * 0 = success
  2961. * !0 = failure
  2962. *
  2963. * PARAMETERS:
  2964. * *cd - pointer to core data
  2965. * param_id - parameter ID to retrieve
  2966. * *value - value of DUT parameter
  2967. ******************************************************************************/
  2968. static int pt_pip_get_param(struct pt_core_data *cd,
  2969. u8 param_id, u32 *value)
  2970. {
  2971. int rc;
  2972. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2973. if (rc < 0) {
  2974. pt_debug(cd->dev, DL_ERROR,
  2975. "%s: fail get exclusive ex=%p own=%p\n",
  2976. __func__, cd->exclusive_dev, cd->dev);
  2977. return rc;
  2978. }
  2979. rc = pt_pip_get_param_(cd, param_id, value);
  2980. if (release_exclusive(cd, cd->dev) < 0)
  2981. pt_debug(cd->dev, DL_ERROR,
  2982. "%s: fail to release exclusive\n", __func__);
  2983. return rc;
  2984. }
  2985. /*******************************************************************************
  2986. * FUNCTION: _pt_request_pip_get_param
  2987. *
  2988. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2989. * calls to the protected or unprotected call to pt_pip_get_param
  2990. *
  2991. * RETURN:
  2992. * 0 = success
  2993. * !0 = failure
  2994. *
  2995. * PARAMETERS:
  2996. * *dev - pointer to device structure
  2997. * protect - flag to call protected or non protected function
  2998. * param_id - parameter ID to retrieve
  2999. * *value - value of DUT parameter
  3000. ******************************************************************************/
  3001. int _pt_request_pip_get_param(struct device *dev,
  3002. int protect, u8 param_id, u32 *value)
  3003. {
  3004. struct pt_core_data *cd = dev_get_drvdata(dev);
  3005. if (protect)
  3006. return pt_pip_get_param(cd, param_id, value);
  3007. return pt_pip_get_param_(cd, param_id, value);
  3008. }
  3009. /*******************************************************************************
  3010. * FUNCTION: pt_pip_set_param_
  3011. *
  3012. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3013. * passed in value to flash
  3014. *
  3015. * RETURN:
  3016. * 0 = success
  3017. * !0 = failure
  3018. *
  3019. * PARAMETERS:
  3020. * *cd - pointer to core data
  3021. * param_id - parameter ID to set
  3022. * value - value to write
  3023. * size - size to write
  3024. ******************************************************************************/
  3025. static int pt_pip_set_param_(struct pt_core_data *cd,
  3026. u8 param_id, u32 value, u8 size)
  3027. {
  3028. u8 write_buf[6];
  3029. u8 *ptr = &write_buf[2];
  3030. int rc = 0;
  3031. int i;
  3032. struct pt_hid_output hid_output = {
  3033. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3034. .write_buf = write_buf,
  3035. };
  3036. write_buf[0] = param_id;
  3037. write_buf[1] = size;
  3038. for (i = 0; i < size; i++) {
  3039. ptr[i] = value & 0xFF;
  3040. value = value >> 8;
  3041. }
  3042. hid_output.write_length = 2 + size;
  3043. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3044. if (rc)
  3045. return rc;
  3046. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3047. return -EPROTO;
  3048. return 0;
  3049. }
  3050. /*******************************************************************************
  3051. * FUNCTION: pt_pip_set_param
  3052. *
  3053. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3054. *
  3055. * RETURN:
  3056. * 0 = success
  3057. * !0 = failure
  3058. *
  3059. * PARAMETERS:
  3060. * *cd - pointer to core data
  3061. * param_id - parameter ID to set
  3062. * value - value to write
  3063. * size - size to write
  3064. ******************************************************************************/
  3065. static int pt_pip_set_param(struct pt_core_data *cd,
  3066. u8 param_id, u32 value, u8 size)
  3067. {
  3068. int rc;
  3069. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3070. if (rc < 0) {
  3071. pt_debug(cd->dev, DL_ERROR,
  3072. "%s: fail get exclusive ex=%p own=%p\n",
  3073. __func__, cd->exclusive_dev, cd->dev);
  3074. return rc;
  3075. }
  3076. rc = pt_pip_set_param_(cd, param_id, value, size);
  3077. if (release_exclusive(cd, cd->dev) < 0)
  3078. pt_debug(cd->dev, DL_ERROR,
  3079. "%s: fail to release exclusive\n", __func__);
  3080. return rc;
  3081. }
  3082. /*******************************************************************************
  3083. * FUNCTION: _pt_request_pip_set_param
  3084. *
  3085. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3086. * calls to the protected or unprotected call to pt_pip_set_param
  3087. *
  3088. * RETURN:
  3089. * 0 = success
  3090. * !0 = failure
  3091. *
  3092. * PARAMETERS:
  3093. * *dev - pointer to device structure
  3094. * protect - flag to call protected or non-protected
  3095. * param_id - parameter ID to set
  3096. * value - value to write
  3097. * size - size to write
  3098. ******************************************************************************/
  3099. int _pt_request_pip_set_param(struct device *dev, int protect,
  3100. u8 param_id, u32 value, u8 size)
  3101. {
  3102. struct pt_core_data *cd = dev_get_drvdata(dev);
  3103. if (protect)
  3104. return pt_pip_set_param(cd, param_id, value, size);
  3105. return pt_pip_set_param_(cd, param_id, value, size);
  3106. }
  3107. /*******************************************************************************
  3108. * FUNCTION: _pt_pip_enter_easywake_state_
  3109. *
  3110. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3111. *
  3112. * RETURN:
  3113. * 0 = success
  3114. * !0 = failure
  3115. *
  3116. * PARAMETERS:
  3117. * *dev - pointer to device structure
  3118. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3119. * *return_data - return status if easywake was entered
  3120. ******************************************************************************/
  3121. static int pt_hid_output_enter_easywake_state_(
  3122. struct pt_core_data *cd, u8 data, u8 *return_data)
  3123. {
  3124. int write_length = 1;
  3125. u8 param[1] = { data };
  3126. int rc = 0;
  3127. struct pt_hid_output hid_output = {
  3128. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3129. .write_length = write_length,
  3130. .write_buf = param,
  3131. };
  3132. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3133. if (rc)
  3134. return rc;
  3135. *return_data = cd->response_buf[5];
  3136. return rc;
  3137. }
  3138. /*******************************************************************************
  3139. * FUNCTION: pt_pip_verify_config_block_crc_
  3140. *
  3141. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3142. *
  3143. * RETURN:
  3144. * 0 = success
  3145. * !0 = failure
  3146. *
  3147. * PARAMETERS:
  3148. * *cd - pointer the core data structure
  3149. * ebid - enumerated block ID
  3150. * *status - PIP command status
  3151. * calculated_crc - calculated CRC
  3152. * stored_crc - stored CRC in config area
  3153. ******************************************************************************/
  3154. static int pt_pip_verify_config_block_crc_(
  3155. struct pt_core_data *cd, u8 ebid, u8 *status,
  3156. u16 *calculated_crc, u16 *stored_crc)
  3157. {
  3158. int write_length = 1;
  3159. u8 param[1] = { ebid };
  3160. u8 *ptr;
  3161. int rc = 0;
  3162. struct pt_hid_output hid_output = {
  3163. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3164. .write_length = write_length,
  3165. .write_buf = param,
  3166. };
  3167. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3168. if (rc)
  3169. return rc;
  3170. ptr = &cd->response_buf[5];
  3171. *status = ptr[0];
  3172. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3173. *stored_crc = get_unaligned_le16(&ptr[3]);
  3174. return 0;
  3175. }
  3176. /*******************************************************************************
  3177. * FUNCTION: pt_pip_verify_config_block_crc
  3178. *
  3179. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3180. * an exclusive access to the DUT.
  3181. *
  3182. * RETURN:
  3183. * 0 = success
  3184. * !0 = failure
  3185. *
  3186. * PARAMETERS:
  3187. * *cd - pointer the core data structure
  3188. * ebid - enumerated block ID
  3189. * *status - PIP command status
  3190. * calculated_crc - calculated CRC
  3191. * stored_crc - stored CRC in config area
  3192. ******************************************************************************/
  3193. static int pt_pip_verify_config_block_crc(
  3194. struct pt_core_data *cd, u8 ebid, u8 *status,
  3195. u16 *calculated_crc, u16 *stored_crc)
  3196. {
  3197. int rc;
  3198. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3199. if (rc < 0) {
  3200. pt_debug(cd->dev, DL_ERROR,
  3201. "%s: fail get exclusive ex=%p own=%p\n",
  3202. __func__, cd->exclusive_dev, cd->dev);
  3203. return rc;
  3204. }
  3205. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3206. calculated_crc, stored_crc);
  3207. if (release_exclusive(cd, cd->dev) < 0)
  3208. pt_debug(cd->dev, DL_ERROR,
  3209. "%s: fail to release exclusive\n", __func__);
  3210. return rc;
  3211. }
  3212. /*******************************************************************************
  3213. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3214. *
  3215. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3216. * calls to the protected or unprotected call to
  3217. * pt_pip_verify_config_block_crc_
  3218. *
  3219. * RETURN:
  3220. * 0 = success
  3221. * !0 = failure
  3222. *
  3223. * PARAMETERS:
  3224. * *dev - pointer to device structure
  3225. * protect - flag to call protected or non-protected
  3226. * ebid - enumerated block ID
  3227. * *status - PIP command status
  3228. * calculated_crc - calculated CRC
  3229. * stored_crc - stored CRC in config area
  3230. ******************************************************************************/
  3231. static int _pt_request_pip_verify_config_block_crc(
  3232. struct device *dev, int protect, u8 ebid, u8 *status,
  3233. u16 *calculated_crc, u16 *stored_crc)
  3234. {
  3235. struct pt_core_data *cd = dev_get_drvdata(dev);
  3236. if (protect)
  3237. return pt_pip_verify_config_block_crc(cd, ebid,
  3238. status, calculated_crc, stored_crc);
  3239. return pt_pip_verify_config_block_crc_(cd, ebid,
  3240. status, calculated_crc, stored_crc);
  3241. }
  3242. /*******************************************************************************
  3243. * FUNCTION: pt_pip_get_config_row_size_
  3244. *
  3245. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3246. *
  3247. * RETURN:
  3248. * 0 = success
  3249. * !0 = failure
  3250. *
  3251. * PARAMETERS:
  3252. * *cd - pointer to core data
  3253. * protect - flag to call protected or non-protected
  3254. * *row_size - pointer to store the retrieved row size
  3255. ******************************************************************************/
  3256. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3257. u16 *row_size)
  3258. {
  3259. int rc = 0;
  3260. struct pt_hid_output hid_output = {
  3261. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3262. };
  3263. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3264. if (rc)
  3265. return rc;
  3266. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3267. return 0;
  3268. }
  3269. /*******************************************************************************
  3270. * FUNCTION: pt_pip_get_config_row_size
  3271. *
  3272. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3273. * an exclusive access to the DUT.
  3274. *
  3275. * RETURN:
  3276. * 0 = success
  3277. * !0 = failure
  3278. *
  3279. * PARAMETERS:
  3280. * *cd - pointer to core data
  3281. * protect - flag to call protected or non-protected
  3282. * *row_size - pointer to store the retrieved row size
  3283. ******************************************************************************/
  3284. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3285. u16 *row_size)
  3286. {
  3287. int rc;
  3288. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3289. if (rc < 0) {
  3290. pt_debug(cd->dev, DL_ERROR,
  3291. "%s: fail get exclusive ex=%p own=%p\n",
  3292. __func__, cd->exclusive_dev, cd->dev);
  3293. return rc;
  3294. }
  3295. rc = pt_pip_get_config_row_size_(cd, row_size);
  3296. if (release_exclusive(cd, cd->dev) < 0)
  3297. pt_debug(cd->dev, DL_ERROR,
  3298. "%s: fail to release exclusive\n", __func__);
  3299. return rc;
  3300. }
  3301. /*******************************************************************************
  3302. * FUNCTION: _pt_request_pip_get_config_row_size
  3303. *
  3304. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3305. * calls to the protected or unprotected call to
  3306. * pt_pip_get_config_row_size_
  3307. *
  3308. * RETURN:
  3309. * 0 = success
  3310. * !0 = failure
  3311. *
  3312. * PARAMETERS:
  3313. * *dev - pointer to device structure
  3314. * protect - flag to call protected or non-protected
  3315. * *row_size - pointer to store the retrieved row size
  3316. ******************************************************************************/
  3317. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3318. int protect, u16 *row_size)
  3319. {
  3320. struct pt_core_data *cd = dev_get_drvdata(dev);
  3321. if (protect)
  3322. return pt_pip_get_config_row_size(cd, row_size);
  3323. return pt_pip_get_config_row_size_(cd, row_size);
  3324. }
  3325. /*******************************************************************************
  3326. * FUNCTION: pt_pip1_read_data_block_
  3327. *
  3328. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3329. * output data to the "read_buf" and update "crc".
  3330. *
  3331. * RETURN:
  3332. * 0 = success
  3333. * !0 = failure
  3334. *
  3335. * PARAMETERS:
  3336. * *cd - pointer to core data
  3337. * row_number - row number
  3338. * length - length of data to read
  3339. * ebid - block id
  3340. * *actual_read_len - Actual data length read
  3341. * *read_buf - pointer to the buffer to store read data
  3342. * read_buf_size - size of read_buf
  3343. * *crc - pointer to store CRC of row data
  3344. ******************************************************************************/
  3345. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3346. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3347. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3348. {
  3349. int read_ebid;
  3350. int status;
  3351. int rc = 0;
  3352. int write_length = 5;
  3353. u8 write_buf[5];
  3354. u8 cmd_offset = 0;
  3355. u16 calc_crc;
  3356. struct pt_hid_output hid_output = {
  3357. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3358. .write_length = write_length,
  3359. .write_buf = write_buf,
  3360. };
  3361. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3362. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3363. write_buf[cmd_offset++] = LOW_BYTE(length);
  3364. write_buf[cmd_offset++] = HI_BYTE(length);
  3365. write_buf[cmd_offset++] = ebid;
  3366. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3367. if (rc)
  3368. return rc;
  3369. status = cd->response_buf[5];
  3370. if (status)
  3371. return status;
  3372. read_ebid = cd->response_buf[6];
  3373. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3374. return -EPROTO;
  3375. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3376. if (length == 0 || *actual_read_len == 0)
  3377. return 0;
  3378. if (read_buf_size >= *actual_read_len)
  3379. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3380. else
  3381. return -EPROTO;
  3382. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3383. /* Validate Row Data CRC */
  3384. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3385. if (*crc == calc_crc) {
  3386. return 0;
  3387. } else {
  3388. pt_debug(cd->dev, DL_ERROR,
  3389. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3390. __func__, *crc, calc_crc);
  3391. return -EPROTO;
  3392. }
  3393. }
  3394. /*******************************************************************************
  3395. * FUNCTION: _pt_request_pip_read_data_block
  3396. *
  3397. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3398. * calls to pt_pip1_read_data_block_
  3399. *
  3400. * RETURN:
  3401. * 0 = success
  3402. * !0 = failure
  3403. *
  3404. * PARAMETERS:
  3405. * *dev - pointer to device structure
  3406. * row_number - row number
  3407. * length - length of data to read
  3408. * ebid - block id
  3409. * *actual_read_len - Actual data length read
  3410. * *read_buf - pointer to the buffer to store read data
  3411. * *crc - pointer to store CRC of row data
  3412. ******************************************************************************/
  3413. static int _pt_request_pip_read_data_block(struct device *dev,
  3414. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3415. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3416. {
  3417. struct pt_core_data *cd = dev_get_drvdata(dev);
  3418. return pt_pip1_read_data_block_(cd, row_number, length,
  3419. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3420. }
  3421. /*******************************************************************************
  3422. * FUNCTION: pt_pip1_write_data_block_
  3423. *
  3424. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3425. * write data to the data block.
  3426. *
  3427. * RETURN:
  3428. * 0 = success
  3429. * !0 = failure
  3430. *
  3431. * PARAMETERS:
  3432. * *cd - pointer to core data
  3433. * row_number - row in config block to write to
  3434. * write_length - length of data to write
  3435. * ebid - enumerated block ID
  3436. * *write_buf - pointer to buffer to write
  3437. * *security_key - pointer to security key to allow write
  3438. * *actual_write_len - pointer to store data length actually written
  3439. ******************************************************************************/
  3440. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3441. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3442. u8 *security_key, u16 *actual_write_len)
  3443. {
  3444. /* row_number + write_len + ebid + security_key + crc */
  3445. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3446. u8 *full_write_buf;
  3447. u8 cmd_offset = 0;
  3448. u16 crc;
  3449. int status;
  3450. int rc = 0;
  3451. int read_ebid;
  3452. u8 *data;
  3453. struct pt_hid_output hid_output = {
  3454. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3455. .write_length = full_write_length,
  3456. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3457. };
  3458. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3459. if (!full_write_buf)
  3460. return -ENOMEM;
  3461. hid_output.write_buf = full_write_buf;
  3462. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3463. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3464. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3465. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3466. full_write_buf[cmd_offset++] = ebid;
  3467. data = &full_write_buf[cmd_offset];
  3468. memcpy(data, write_buf, write_length);
  3469. cmd_offset += write_length;
  3470. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3471. cmd_offset += 8;
  3472. crc = _pt_compute_crc(data, write_length);
  3473. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3474. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3475. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3476. if (rc)
  3477. goto exit;
  3478. status = cd->response_buf[5];
  3479. if (status) {
  3480. rc = -EINVAL;
  3481. goto exit;
  3482. }
  3483. read_ebid = cd->response_buf[6];
  3484. if (read_ebid != ebid) {
  3485. rc = -EPROTO;
  3486. goto exit;
  3487. }
  3488. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3489. exit:
  3490. kfree(full_write_buf);
  3491. return rc;
  3492. }
  3493. /*******************************************************************************
  3494. * FUNCTION: _pt_request_pip_write_data_block
  3495. *
  3496. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3497. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3498. *
  3499. * RETURN:
  3500. * 0 = success
  3501. * !0 = failure
  3502. *
  3503. * PARAMETERS:
  3504. * *dev - pointer to device structure
  3505. * protect - flag to call protected or non-protected
  3506. * row_number - row in config block to write to
  3507. * write_length - length of data to write
  3508. * ebid - enumerated block ID
  3509. * *write_buf - pointer to buffer to write
  3510. * *security_key - pointer to security key to allow write
  3511. * *actual_write_len - pointer to store data length actually written
  3512. ******************************************************************************/
  3513. static int _pt_request_pip_write_data_block(struct device *dev,
  3514. u16 row_number, u16 write_length, u8 ebid,
  3515. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3516. {
  3517. struct pt_core_data *cd = dev_get_drvdata(dev);
  3518. return pt_pip1_write_data_block_(cd, row_number,
  3519. write_length, ebid, write_buf, security_key,
  3520. actual_write_len);
  3521. }
  3522. /*******************************************************************************
  3523. * FUNCTION: pt_pip_get_data_structure_
  3524. *
  3525. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3526. * returning a structure of data defined by data_id
  3527. *
  3528. * RETURN:
  3529. * 0 = success
  3530. * !0 = failure
  3531. *
  3532. * PARAMETERS:
  3533. * *cd - pointer to core data
  3534. * read_offset - read pointer offset
  3535. * read_length - length of data to read
  3536. * data_id - data ID to read
  3537. * *status - pointer to store the read response status
  3538. * *data_format - pointer to store format of data read
  3539. * *actual_read_len - pointer to store data length actually read
  3540. * *data - pointer to store data read
  3541. ******************************************************************************/
  3542. static int pt_pip_get_data_structure_(
  3543. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3544. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3545. u8 *data)
  3546. {
  3547. int rc = 0;
  3548. u16 total_read_len = 0;
  3549. u16 read_len;
  3550. u16 off_buf = 0;
  3551. u8 write_buf[5];
  3552. u8 read_data_id;
  3553. struct pt_hid_output hid_output = {
  3554. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3555. .write_length = 5,
  3556. .write_buf = write_buf,
  3557. };
  3558. again:
  3559. write_buf[0] = LOW_BYTE(read_offset);
  3560. write_buf[1] = HI_BYTE(read_offset);
  3561. write_buf[2] = LOW_BYTE(read_length);
  3562. write_buf[3] = HI_BYTE(read_length);
  3563. write_buf[4] = data_id;
  3564. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3565. if (rc)
  3566. return rc;
  3567. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3568. goto set_status;
  3569. read_data_id = cd->response_buf[6];
  3570. if (read_data_id != data_id)
  3571. return -EPROTO;
  3572. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3573. if (read_len && data) {
  3574. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3575. total_read_len += read_len;
  3576. if (read_len < read_length) {
  3577. read_offset += read_len;
  3578. off_buf += read_len;
  3579. read_length -= read_len;
  3580. goto again;
  3581. }
  3582. }
  3583. if (data_format)
  3584. *data_format = cd->response_buf[9];
  3585. if (actual_read_len)
  3586. *actual_read_len = total_read_len;
  3587. set_status:
  3588. if (status)
  3589. *status = cd->response_buf[5];
  3590. return rc;
  3591. }
  3592. /*******************************************************************************
  3593. * FUNCTION: pt_pip_get_data_structure
  3594. *
  3595. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3596. * an exclusive access to the DUT.
  3597. *
  3598. * RETURN:
  3599. * 0 = success
  3600. * !0 = failure
  3601. *
  3602. * PARAMETERS:
  3603. * *cd - pointer to core data
  3604. * read_offset - read pointer offset
  3605. * read_length - length of data to read
  3606. * data_id - data ID to read
  3607. * *status - pointer to store the read response status
  3608. * *data_format - pointer to store format of data read
  3609. * *actual_read_len - pointer to store data length actually read
  3610. * *data - pointer to store data read
  3611. ******************************************************************************/
  3612. static int pt_pip_get_data_structure(
  3613. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3614. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3615. u8 *data)
  3616. {
  3617. int rc;
  3618. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3619. if (rc < 0) {
  3620. pt_debug(cd->dev, DL_ERROR,
  3621. "%s: fail get exclusive ex=%p own=%p\n",
  3622. __func__, cd->exclusive_dev, cd->dev);
  3623. return rc;
  3624. }
  3625. rc = pt_pip_get_data_structure_(cd, read_offset,
  3626. read_length, data_id, status, data_format,
  3627. actual_read_len, data);
  3628. if (release_exclusive(cd, cd->dev) < 0)
  3629. pt_debug(cd->dev, DL_ERROR,
  3630. "%s: fail to release exclusive\n", __func__);
  3631. return rc;
  3632. }
  3633. /*******************************************************************************
  3634. * FUNCTION: _pt_request_pip_get_data_structure
  3635. *
  3636. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3637. * calls to the protected or unprotected call to
  3638. * pt_pip_get_data_structure
  3639. *
  3640. * RETURN:
  3641. * 0 = success
  3642. * !0 = failure
  3643. *
  3644. * PARAMETERS:
  3645. * *dev - pointer to device structure
  3646. * protect - flag to call protected or non-protected
  3647. * read_offset - read pointer offset
  3648. * read_length - length of data to read
  3649. * data_id - data ID to read
  3650. * *status - pointer to store the read response status
  3651. * *data_format - pointer to store format of data read
  3652. * *actual_read_len - pointer to store data length actually read
  3653. * *data - pointer to store data read
  3654. ******************************************************************************/
  3655. static int _pt_request_pip_get_data_structure(struct device *dev,
  3656. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3657. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3658. {
  3659. struct pt_core_data *cd = dev_get_drvdata(dev);
  3660. if (protect)
  3661. return pt_pip_get_data_structure(cd,
  3662. read_offset, read_length, data_id, status,
  3663. data_format, actual_read_len, data);
  3664. return pt_pip_get_data_structure_(cd,
  3665. read_offset, read_length, data_id, status,
  3666. data_format, actual_read_len, data);
  3667. }
  3668. /*******************************************************************************
  3669. * FUNCTION: _pt_manage_local_cal_data
  3670. *
  3671. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3672. * CALIBRATION data. It stores it in a local static array and can be
  3673. * cleared, loaded or used to restore the CAL data back to the running FW.
  3674. * The CAL data is read or restored by use of the PIP1 commands:
  3675. * - READ_DATA_BLOCK (0x22)
  3676. * - WRITE_DATA_BLOCK (0x23)
  3677. *
  3678. * RETURN:
  3679. * 0 = success
  3680. * !0 = failure
  3681. *
  3682. * PARAMETERS:
  3683. * *dev - pointer to device structure
  3684. * action - One of the following actions:
  3685. * - PT_CAL_DATA_SAVE
  3686. * - PT_CAL_DATA_RESTORE
  3687. * - PT_CAL_DATA_CLEAR
  3688. * - PT_CAL_DATA_SIZE
  3689. * *size - pointer to the number of bytes transferred
  3690. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3691. ******************************************************************************/
  3692. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3693. unsigned short *crc)
  3694. {
  3695. struct pt_core_data *cd = dev_get_drvdata(dev);
  3696. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3697. unsigned short calc_id_crc = 0;
  3698. static u8 *cal_cache_data;
  3699. static u16 cal_cache_len;
  3700. static unsigned short cal_cache_chip_id;
  3701. int rc = 0;
  3702. u8 *tmp_data = NULL;
  3703. u8 row_number = 0;
  3704. u8 prefix[20];
  3705. u16 cal_size = 0;
  3706. u16 transfer_size;
  3707. u16 act_trans_len = 0;
  3708. u16 byte_offset = 0;
  3709. u16 cal_blk_size;
  3710. u16 total_rows;
  3711. u16 remain_bytes;
  3712. u16 data_block_crc;
  3713. u16 buf_size = 12;
  3714. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3715. __func__, action);
  3716. switch (action) {
  3717. case PT_CAL_DATA_SAVE:
  3718. /* Read the size of the CAL block and calculate # rows */
  3719. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3720. if (!tmp_data) {
  3721. rc = -ENOMEM;
  3722. goto exit;
  3723. }
  3724. /*
  3725. * Don't check rc as doing a read size will give a false
  3726. * error on the CRC check.
  3727. */
  3728. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3729. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3730. cal_blk_size = act_trans_len;
  3731. kfree(tmp_data);
  3732. pt_debug(dev, DL_INFO,
  3733. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3734. __func__, cal_cache_len, cal_blk_size);
  3735. /* Safety net to ensure we didn't read incorrect size */
  3736. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3737. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3738. __func__);
  3739. rc = 1;
  3740. goto exit;
  3741. }
  3742. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3743. if (cal_blk_size != cal_cache_len) {
  3744. kfree(cal_cache_data);
  3745. cal_cache_data = kzalloc(cal_blk_size + 2,
  3746. GFP_KERNEL);
  3747. if (!cal_cache_data) {
  3748. rc = -ENOMEM;
  3749. goto exit;
  3750. }
  3751. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3752. __func__);
  3753. }
  3754. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3755. /* Calculate how many rows [0-n] (PIP Transactions) */
  3756. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3757. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3758. /* Add row if we have a last partial row */
  3759. if (remain_bytes > 0)
  3760. total_rows++;
  3761. pt_debug(dev, DL_INFO,
  3762. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3763. __func__, cal_blk_size, total_rows, remain_bytes);
  3764. /* Read all rows unless an error occurs */
  3765. rc = 0;
  3766. while (rc == 0 && row_number <= total_rows) {
  3767. act_trans_len = 0;
  3768. if (remain_bytes > 0 && row_number == total_rows)
  3769. transfer_size = remain_bytes;
  3770. else
  3771. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3772. rc = pt_pip1_read_data_block_(cd, row_number,
  3773. transfer_size, PT_CAL_EBID,
  3774. &act_trans_len,
  3775. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3776. &data_block_crc);
  3777. if (rc) {
  3778. /* Error occurred, exit loop */
  3779. cal_size = 0;
  3780. break;
  3781. }
  3782. pt_debug(dev, DL_INFO,
  3783. "%s: CAL read rc=%d actual read len=%d\n",
  3784. __func__, rc, act_trans_len);
  3785. byte_offset += act_trans_len;
  3786. cal_size = byte_offset;
  3787. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3788. pt_pr_buf(dev, DL_INFO,
  3789. &cal_cache_data[byte_offset - act_trans_len],
  3790. act_trans_len, prefix);
  3791. row_number++;
  3792. }
  3793. if (cal_size > 0) {
  3794. /* Save a CRC of the chip info the CAL was saved from */
  3795. calc_id_crc = crc_ccitt_calculate(
  3796. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3797. cal_cache_chip_id = calc_id_crc;
  3798. cal_cache_len = cal_size;
  3799. pt_debug(dev, DL_INFO,
  3800. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3801. __func__, calc_id_crc, cal_size);
  3802. }
  3803. *size = cal_size;
  3804. *crc = calc_id_crc;
  3805. break;
  3806. case PT_CAL_DATA_RESTORE:
  3807. cal_size = cal_cache_len;
  3808. while ((rc == 0) && (byte_offset < cal_size)) {
  3809. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3810. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3811. else
  3812. transfer_size = cal_size - byte_offset;
  3813. rc = pt_pip1_write_data_block_(cd, row_number,
  3814. transfer_size, PT_CAL_EBID,
  3815. &cal_cache_data[byte_offset],
  3816. (u8 *)pt_data_block_security_key,
  3817. &act_trans_len);
  3818. byte_offset += act_trans_len;
  3819. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3820. __func__, byte_offset);
  3821. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3822. pt_pr_buf(dev, DL_INFO,
  3823. &cal_cache_data[byte_offset - act_trans_len],
  3824. act_trans_len, prefix);
  3825. if ((byte_offset > cal_size) ||
  3826. (act_trans_len != transfer_size))
  3827. rc = -EIO;
  3828. row_number++;
  3829. }
  3830. *size = byte_offset;
  3831. *crc = cal_cache_chip_id;
  3832. break;
  3833. case PT_CAL_DATA_CLEAR:
  3834. if (cal_cache_data)
  3835. memset(&cal_cache_data[0], 0, cal_cache_len);
  3836. cal_cache_len = 0;
  3837. cal_cache_chip_id = 0;
  3838. *size = 0;
  3839. *crc = 0;
  3840. break;
  3841. case PT_CAL_DATA_INFO:
  3842. default:
  3843. *size = cal_cache_len;
  3844. *crc = cal_cache_chip_id;
  3845. pt_debug(dev, DL_INFO,
  3846. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3847. __func__, cal_cache_chip_id,
  3848. cal_cache_len);
  3849. break;
  3850. }
  3851. exit:
  3852. pt_debug(dev, DL_INFO,
  3853. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3854. __func__, rc, *crc, *size);
  3855. return rc;
  3856. }
  3857. /*******************************************************************************
  3858. * FUNCTION: pt_pip_run_selftest_
  3859. *
  3860. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3861. * to execute a FW built in self test
  3862. *
  3863. * RETURN:
  3864. * 0 = success
  3865. * !0 = failure
  3866. *
  3867. * PARAMETERS:
  3868. * *cd - pointer to core data
  3869. * test_id - enumerated test ID to run
  3870. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3871. * *status - pointer to store the read response status
  3872. * *summary_results - pointer to store the results summary
  3873. * *results_available - pointer to store if results are available
  3874. *****************************************************************************/
  3875. static int pt_pip_run_selftest_(
  3876. struct pt_core_data *cd, u8 test_id,
  3877. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3878. u8 *results_available)
  3879. {
  3880. int rc = 0;
  3881. u8 write_buf[2];
  3882. struct pt_hid_output hid_output = {
  3883. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3884. .write_length = 2,
  3885. .write_buf = write_buf,
  3886. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3887. };
  3888. write_buf[0] = test_id;
  3889. write_buf[1] = write_idacs_to_flash;
  3890. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3891. hid_output.write_length = 1;
  3892. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3893. if (rc)
  3894. return rc;
  3895. if (status)
  3896. *status = cd->response_buf[5];
  3897. if (summary_result)
  3898. *summary_result = cd->response_buf[6];
  3899. /* results_available only available before PIP 1.03 */
  3900. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3901. if (results_available)
  3902. *results_available = cd->response_buf[7];
  3903. }
  3904. return rc;
  3905. }
  3906. /*******************************************************************************
  3907. * FUNCTION: pt_pip_run_selftest
  3908. *
  3909. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3910. * an exclusive access to the DUT.
  3911. *
  3912. * RETURN:
  3913. * 0 = success
  3914. * !0 = failure
  3915. *
  3916. * PARAMETERS:
  3917. * *cd - pointer to core data
  3918. * test_id - enumerated test ID to run
  3919. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3920. * *status - pointer to store the read response status
  3921. * *summary_results - pointer to store the results summary
  3922. * *results_available - pointer to store if results are available
  3923. ******************************************************************************/
  3924. static int pt_pip_run_selftest(
  3925. struct pt_core_data *cd, u8 test_id,
  3926. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3927. u8 *results_available)
  3928. {
  3929. int rc;
  3930. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3931. if (rc < 0) {
  3932. pt_debug(cd->dev, DL_ERROR,
  3933. "%s: fail get exclusive ex=%p own=%p\n",
  3934. __func__, cd->exclusive_dev, cd->dev);
  3935. return rc;
  3936. }
  3937. rc = pt_pip_run_selftest_(cd, test_id,
  3938. write_idacs_to_flash, status, summary_result,
  3939. results_available);
  3940. if (release_exclusive(cd, cd->dev) < 0)
  3941. pt_debug(cd->dev, DL_ERROR,
  3942. "%s: fail to release exclusive\n", __func__);
  3943. return rc;
  3944. }
  3945. /*******************************************************************************
  3946. * FUNCTION: _pt_request_pip_run_selftest
  3947. *
  3948. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3949. * calls to the protected or unprotected call to pt_pip_run_selftest
  3950. *
  3951. * RETURN:
  3952. * 0 = success
  3953. * !0 = failure
  3954. *
  3955. * PARAMETERS:
  3956. * *dev - pointer to device structure
  3957. * protect - flag to call protected or non-protected
  3958. * test_id - enumerated test ID to run
  3959. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3960. * *status - pointer to store the read response status
  3961. * *summary_results - pointer to store the results summary
  3962. * *results_available - pointer to store if results are available
  3963. ******************************************************************************/
  3964. static int _pt_request_pip_run_selftest(struct device *dev,
  3965. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3966. u8 *summary_result, u8 *results_available)
  3967. {
  3968. struct pt_core_data *cd = dev_get_drvdata(dev);
  3969. if (protect)
  3970. return pt_pip_run_selftest(cd, test_id,
  3971. write_idacs_to_flash, status, summary_result,
  3972. results_available);
  3973. return pt_pip_run_selftest_(cd, test_id,
  3974. write_idacs_to_flash, status, summary_result,
  3975. results_available);
  3976. }
  3977. /*******************************************************************************
  3978. * FUNCTION: _pt_pip_get_selftest_result_
  3979. *
  3980. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3981. * to retrieve the self test results from the self test already executed
  3982. *
  3983. * RETURN:
  3984. * 0 = success
  3985. * !0 = failure
  3986. *
  3987. * PARAMETERS:
  3988. * *cd - pointer to core data
  3989. * read_offset - read pointer offset
  3990. * read_length - length of data to read
  3991. * test_id - enumerated test ID to read selftest results from
  3992. * *status - pointer to store the read response status
  3993. * *actual_read_len - pointer to store data length actually read
  3994. * *status - pointer to where the cmd response statas is stored
  3995. ******************************************************************************/
  3996. static int pt_pip_get_selftest_result_(
  3997. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3998. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3999. {
  4000. int rc = 0;
  4001. u16 total_read_len = 0;
  4002. u16 read_len;
  4003. u16 off_buf = 0;
  4004. u8 write_buf[5];
  4005. u8 read_test_id;
  4006. bool repeat;
  4007. struct pt_hid_output hid_output = {
  4008. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4009. .write_length = 5,
  4010. .write_buf = write_buf,
  4011. };
  4012. /*
  4013. * Do not repeat reading for Auto Shorts test
  4014. * when PIP version < 1.3
  4015. */
  4016. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4017. || test_id != PT_ST_ID_AUTOSHORTS;
  4018. again:
  4019. write_buf[0] = LOW_BYTE(read_offset);
  4020. write_buf[1] = HI_BYTE(read_offset);
  4021. write_buf[2] = LOW_BYTE(read_length);
  4022. write_buf[3] = HI_BYTE(read_length);
  4023. write_buf[4] = test_id;
  4024. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4025. if (rc)
  4026. return rc;
  4027. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4028. goto set_status;
  4029. read_test_id = cd->response_buf[6];
  4030. if (read_test_id != test_id)
  4031. return -EPROTO;
  4032. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4033. if (read_len && data) {
  4034. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4035. total_read_len += read_len;
  4036. if (repeat && read_len < read_length) {
  4037. read_offset += read_len;
  4038. off_buf += read_len;
  4039. read_length -= read_len;
  4040. goto again;
  4041. }
  4042. }
  4043. if (actual_read_len)
  4044. *actual_read_len = total_read_len;
  4045. set_status:
  4046. if (status)
  4047. *status = cd->response_buf[5];
  4048. return rc;
  4049. }
  4050. /*******************************************************************************
  4051. * FUNCTION: _pt_pip_get_selftest_result
  4052. *
  4053. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4054. * access to the DUT
  4055. *
  4056. * RETURN:
  4057. * 0 = success
  4058. * !0 = failure
  4059. *
  4060. * PARAMETERS:
  4061. * *cd - pointer to core data
  4062. * read_offset - read pointer offset
  4063. * read_length - length of data to read
  4064. * test_id - enumerated test ID to read selftest results from
  4065. * *status - pointer to store the read response status
  4066. * *actual_read_len - pointer to store data length actually read
  4067. * *status - pointer to where the cmd response statas is stored
  4068. ******************************************************************************/
  4069. static int pt_pip_get_selftest_result(
  4070. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4071. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4072. {
  4073. int rc;
  4074. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4075. if (rc < 0) {
  4076. pt_debug(cd->dev, DL_ERROR,
  4077. "%s: fail get exclusive ex=%p own=%p\n",
  4078. __func__, cd->exclusive_dev, cd->dev);
  4079. return rc;
  4080. }
  4081. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4082. read_length, test_id, status, actual_read_len, data);
  4083. if (release_exclusive(cd, cd->dev) < 0)
  4084. pt_debug(cd->dev, DL_ERROR,
  4085. "%s: fail to release exclusive\n", __func__);
  4086. return rc;
  4087. }
  4088. /*******************************************************************************
  4089. * FUNCTION: _pt_request_pip_get_selftest_result
  4090. *
  4091. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4092. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4093. *
  4094. * RETURN:
  4095. * 0 = success
  4096. * !0 = failure
  4097. *
  4098. * PARAMETERS:
  4099. * *dev - pointer to device structure
  4100. * protect - flag to call protected or non-protected
  4101. * read_offset - read pointer offset
  4102. * read_length - length of data to read
  4103. * test_id - enumerated test ID to read selftest results from
  4104. * *status - pointer to store the read response status
  4105. * *actual_read_len - pointer to store data length actually read
  4106. * *data - pointer to where the data read is stored
  4107. ******************************************************************************/
  4108. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4109. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4110. u8 *status, u16 *actual_read_len, u8 *data)
  4111. {
  4112. struct pt_core_data *cd = dev_get_drvdata(dev);
  4113. if (protect)
  4114. return pt_pip_get_selftest_result(cd, read_offset,
  4115. read_length, test_id, status, actual_read_len,
  4116. data);
  4117. return pt_pip_get_selftest_result_(cd, read_offset,
  4118. read_length, test_id, status, actual_read_len,
  4119. data);
  4120. }
  4121. /*******************************************************************************
  4122. * FUNCTION: _pt_pip_load_self_test_param
  4123. *
  4124. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4125. * to load parameters needed by a self test
  4126. *
  4127. * RETURN:
  4128. * 0 = success
  4129. * !0 = failure
  4130. *
  4131. * PARAMETERS:
  4132. * *cd - pointer to core data
  4133. * self_test_id - enumerated test ID for which the parmeters belong
  4134. * load_offset - mem offset to where to load parameters
  4135. * load_length - length of parameter data to load
  4136. * *parameters - pointer to list of parameter data
  4137. * *status - pointer to store the response status
  4138. * *ret_test_id - pointer to returned test id the parameters were stored
  4139. * *act_load_len - pointer to store the actual load length that was writen
  4140. ******************************************************************************/
  4141. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4142. u8 self_test_id, u16 load_offset, u16 load_length,
  4143. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4144. {
  4145. int rc = 0;
  4146. int i;
  4147. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4148. struct pt_hid_output hid_output = {
  4149. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4150. .write_length = 5 + load_length,
  4151. .write_buf = write_buf,
  4152. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4153. };
  4154. write_buf[0] = LOW_BYTE(load_offset);
  4155. write_buf[1] = HI_BYTE(load_offset);
  4156. write_buf[2] = LOW_BYTE(load_length);
  4157. write_buf[3] = HI_BYTE(load_length);
  4158. write_buf[4] = self_test_id;
  4159. for (i = 0; i < load_length; i++)
  4160. write_buf[i + 5] = parameters[i];
  4161. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4162. if (rc)
  4163. return rc;
  4164. if (status)
  4165. *status = cd->response_buf[5];
  4166. if (ret_test_id)
  4167. *ret_test_id = cd->response_buf[6];
  4168. if (act_load_len)
  4169. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4170. return rc;
  4171. }
  4172. /*******************************************************************************
  4173. * FUNCTION: pt_pip_load_self_test_param
  4174. *
  4175. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4176. * access to the DUT.
  4177. *
  4178. * RETURN:
  4179. * 0 = success
  4180. * !0 = failure
  4181. *
  4182. * PARAMETERS:
  4183. * *cd - pointer to core data
  4184. * self_test_id - enumerated test ID for which the parmeters belong
  4185. * load_offset - mem offset to where to load parameters
  4186. * load_length - length of parameter data to load
  4187. * *parameters - pointer to list of parameter data
  4188. * *status - pointer to store the response status
  4189. * *ret_test_id - pointer to returned test id the parameters were stored
  4190. * *act_load_len - pointer to store the actual load length that was writen
  4191. ******************************************************************************/
  4192. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4193. u8 self_test_id, u16 load_offset, u16 load_length,
  4194. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4195. {
  4196. int rc;
  4197. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4198. if (rc < 0) {
  4199. pt_debug(cd->dev, DL_ERROR,
  4200. "%s: fail get exclusive ex=%p own=%p\n",
  4201. __func__, cd->exclusive_dev, cd->dev);
  4202. return rc;
  4203. }
  4204. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4205. load_length, parameters, status, ret_test_id, act_load_len);
  4206. if (release_exclusive(cd, cd->dev) < 0)
  4207. pt_debug(cd->dev, DL_ERROR,
  4208. "%s: fail to release exclusive\n", __func__);
  4209. return rc;
  4210. }
  4211. /*******************************************************************************
  4212. * FUNCTION: _pt_request_pip_load_self_test_param
  4213. *
  4214. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4215. * calls to the protected or unprotected call to
  4216. * pt_pip_load_self_test_param
  4217. *
  4218. * RETURN:
  4219. * 0 = success
  4220. * !0 = failure
  4221. *
  4222. * PARAMETERS:
  4223. * *dev - pointer to device structure
  4224. * protect - flag to call protected or non-protected
  4225. * self_test_id - enumerated test ID for which the parmeters belong
  4226. * load_offset - mem offset to where to load parameters
  4227. * load_length - length of parameter data to load
  4228. * *parameters - pointer to list of parameter data
  4229. * *status - pointer to store the response status
  4230. * *ret_test_id - pointer to returned test id the parameters were stored
  4231. * *act_load_len - pointer to store the actual load length that was writen
  4232. ******************************************************************************/
  4233. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4234. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4235. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4236. {
  4237. struct pt_core_data *cd = dev_get_drvdata(dev);
  4238. if (protect)
  4239. return pt_pip_load_self_test_param(cd, self_test_id,
  4240. load_offset, load_length, parameters, status, ret_test_id,
  4241. act_load_len);
  4242. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4243. load_length, parameters, status, ret_test_id, act_load_len);
  4244. }
  4245. /*******************************************************************************
  4246. * FUNCTION: pt_pip_calibrate_ext_
  4247. *
  4248. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4249. * for the response
  4250. *
  4251. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4252. *
  4253. * RETURN:
  4254. * 0 = success
  4255. * !0 = failure
  4256. *
  4257. * PARAMETERS:
  4258. * *cd - pointer to core data
  4259. * *cal_data - pointer to extended calibration data structure
  4260. * *status - pointer to where the command response status is stored
  4261. ******************************************************************************/
  4262. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4263. struct pt_cal_ext_data *cal_data, u8 *status)
  4264. {
  4265. int rc = 0;
  4266. int write_length = 4;
  4267. u8 write_buf[4];
  4268. u16 size = 0;
  4269. unsigned short crc = 0;
  4270. struct pt_hid_output hid_output = {
  4271. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4272. .write_length = write_length,
  4273. .write_buf = write_buf,
  4274. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4275. };
  4276. if (cal_data == NULL)
  4277. return -EINVAL;
  4278. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4279. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4280. if (rc)
  4281. return rc;
  4282. if (status)
  4283. *status = cd->response_buf[5];
  4284. /*
  4285. * When doing a calibration on a flashless DUT, save CAL data in
  4286. * the TTDL cache on any successful calibration
  4287. */
  4288. if (*status == 0 && cd->cal_cache_in_host) {
  4289. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4290. __func__);
  4291. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4292. &size, &crc);
  4293. if (rc)
  4294. pt_debug(cd->dev, DL_ERROR,
  4295. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4296. else
  4297. pt_debug(cd->dev, DL_INFO,
  4298. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4299. __func__, crc, size);
  4300. }
  4301. return 0;
  4302. }
  4303. /*******************************************************************************
  4304. * FUNCTION: pt_pip_calibrate_ext
  4305. *
  4306. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4307. * DUT
  4308. *
  4309. * RETURN:
  4310. * 0 = success
  4311. * !0 = failure
  4312. *
  4313. * PARAMETERS:
  4314. * *cd - pointer to core data
  4315. * *cal_data - pointer to extended calibration data structure
  4316. * *status - pointer to where the command response status is stored
  4317. ******************************************************************************/
  4318. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4319. struct pt_cal_ext_data *cal_data, u8 *status)
  4320. {
  4321. int rc;
  4322. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4323. if (rc < 0) {
  4324. pt_debug(cd->dev, DL_ERROR,
  4325. "%s: fail get exclusive ex=%p own=%p\n",
  4326. __func__, cd->exclusive_dev, cd->dev);
  4327. return rc;
  4328. }
  4329. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4330. if (release_exclusive(cd, cd->dev) < 0)
  4331. pt_debug(cd->dev, DL_ERROR,
  4332. "%s: fail to release exclusive\n", __func__);
  4333. return rc;
  4334. }
  4335. /*******************************************************************************
  4336. * FUNCTION: _pt_request_pip_calibrate_ext
  4337. *
  4338. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4339. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4340. *
  4341. * RETURN:
  4342. * 0 = success
  4343. * !0 = failure
  4344. *
  4345. * PARAMETERS:
  4346. * *dev - pointer to device structure
  4347. * protect - flag to call protected or non-protected
  4348. * *cal_data - pointer to extended calibration data structure
  4349. * *status - pointer to where the command response status is stored
  4350. ******************************************************************************/
  4351. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4352. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4353. {
  4354. struct pt_core_data *cd = dev_get_drvdata(dev);
  4355. if (protect)
  4356. return pt_pip_calibrate_ext(cd, cal_data, status);
  4357. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4358. }
  4359. /*******************************************************************************
  4360. * FUNCTION: pt_pip_calibrate_idacs_
  4361. *
  4362. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4363. * for the response
  4364. *
  4365. * RETURN:
  4366. * 0 = success
  4367. * !0 = failure
  4368. *
  4369. * PARAMETERS:
  4370. * *cd - pointer to core data
  4371. * mode - sense mode to calibrate (0-5)
  4372. * *status - pointer to where the command response status is stored
  4373. ******************************************************************************/
  4374. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4375. u8 mode, u8 *status)
  4376. {
  4377. int rc = 0;
  4378. int write_length = 1;
  4379. u8 write_buf[1];
  4380. u8 cmd_offset = 0;
  4381. struct pt_hid_output hid_output = {
  4382. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4383. .write_length = write_length,
  4384. .write_buf = write_buf,
  4385. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4386. };
  4387. write_buf[cmd_offset++] = mode;
  4388. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4389. if (rc)
  4390. return rc;
  4391. *status = cd->response_buf[5];
  4392. if (*status)
  4393. return -EINVAL;
  4394. return 0;
  4395. }
  4396. /*******************************************************************************
  4397. * FUNCTION: pt_pip_calibrate_idacs
  4398. *
  4399. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4400. * access to the DUT
  4401. *
  4402. * RETURN:
  4403. * 0 = success
  4404. * !0 = failure
  4405. *
  4406. * PARAMETERS:
  4407. * *cd - pointer to core data
  4408. * mode - sense mode to calibrate (0-5)
  4409. * *status - pointer to where the command response status is stored
  4410. ******************************************************************************/
  4411. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4412. u8 mode, u8 *status)
  4413. {
  4414. int rc;
  4415. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4416. if (rc < 0) {
  4417. pt_debug(cd->dev, DL_ERROR,
  4418. "%s: fail get exclusive ex=%p own=%p\n",
  4419. __func__, cd->exclusive_dev, cd->dev);
  4420. return rc;
  4421. }
  4422. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4423. if (release_exclusive(cd, cd->dev) < 0)
  4424. pt_debug(cd->dev, DL_ERROR,
  4425. "%s: fail to release exclusive\n", __func__);
  4426. return rc;
  4427. }
  4428. /*******************************************************************************
  4429. * FUNCTION: _pt_request_pip_calibrate_idacs
  4430. *
  4431. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4432. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4433. *
  4434. * RETURN:
  4435. * 0 = success
  4436. * !0 = failure
  4437. *
  4438. * PARAMETERS:
  4439. * *dev - pointer to device structure
  4440. * protect - flag to call protected or non-protected
  4441. * mode - sense mode to calibrate (0-5)
  4442. * *status - pointer to where the command response status is stored
  4443. ******************************************************************************/
  4444. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4445. int protect, u8 mode, u8 *status)
  4446. {
  4447. struct pt_core_data *cd = dev_get_drvdata(dev);
  4448. if (protect)
  4449. return pt_pip_calibrate_idacs(cd, mode, status);
  4450. return pt_pip_calibrate_idacs_(cd, mode, status);
  4451. }
  4452. /*******************************************************************************
  4453. * FUNCTION: pt_hid_output_initialize_baselines_
  4454. *
  4455. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4456. * waiting for the response.
  4457. *
  4458. * RETURN:
  4459. * 0 = success
  4460. * !0 = failure
  4461. *
  4462. * PARAMETERS:
  4463. * *cd - pointer to core data
  4464. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4465. * each or together with a single command.
  4466. * *status - pointer to where the command response status is stored
  4467. ******************************************************************************/
  4468. static int pt_hid_output_initialize_baselines_(
  4469. struct pt_core_data *cd, u8 test_id, u8 *status)
  4470. {
  4471. int rc = 0;
  4472. int write_length = 1;
  4473. u8 write_buf[1];
  4474. u8 cmd_offset = 0;
  4475. struct pt_hid_output hid_output = {
  4476. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4477. .write_length = write_length,
  4478. .write_buf = write_buf,
  4479. };
  4480. write_buf[cmd_offset++] = test_id;
  4481. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4482. if (rc)
  4483. return rc;
  4484. *status = cd->response_buf[5];
  4485. if (*status)
  4486. return -EINVAL;
  4487. return rc;
  4488. }
  4489. /*******************************************************************************
  4490. * FUNCTION: pt_hid_output_initialize_baselines
  4491. *
  4492. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4493. * access to the DUT
  4494. *
  4495. * RETURN:
  4496. * 0 = success
  4497. * !0 = failure
  4498. *
  4499. * PARAMETERS:
  4500. * *cd - pointer to core data
  4501. * test_id - enumerated ID against which to initialize the baseline
  4502. * *status - pointer to where the command response status is stored
  4503. ******************************************************************************/
  4504. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4505. u8 test_id, u8 *status)
  4506. {
  4507. int rc;
  4508. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4509. if (rc < 0) {
  4510. pt_debug(cd->dev, DL_ERROR,
  4511. "%s: fail get exclusive ex=%p own=%p\n",
  4512. __func__, cd->exclusive_dev, cd->dev);
  4513. return rc;
  4514. }
  4515. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4516. if (release_exclusive(cd, cd->dev) < 0)
  4517. pt_debug(cd->dev, DL_ERROR,
  4518. "%s: fail to release exclusive\n", __func__);
  4519. return rc;
  4520. }
  4521. /*******************************************************************************
  4522. * FUNCTION: _pt_request_pip_initialize_baselines
  4523. *
  4524. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4525. * calls to the protected or unprotected call to
  4526. * pt_pip_initialize_baselines
  4527. *
  4528. * RETURN:
  4529. * 0 = success
  4530. * !0 = failure
  4531. *
  4532. * PARAMETERS:
  4533. * *dev - pointer to device structure
  4534. * protect - flag to call protected or non-protected
  4535. * test_id - enumerated ID against which to initialize the baseline
  4536. * *status - pointer to where the command response status is stored
  4537. ******************************************************************************/
  4538. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4539. int protect, u8 test_id, u8 *status)
  4540. {
  4541. struct pt_core_data *cd = dev_get_drvdata(dev);
  4542. if (protect)
  4543. return pt_hid_output_initialize_baselines(cd, test_id,
  4544. status);
  4545. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4546. }
  4547. /*******************************************************************************
  4548. * FUNCTION: pt_hid_output_exec_panel_scan_
  4549. *
  4550. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4551. * the response
  4552. *
  4553. * RETURN:
  4554. * 0 = success
  4555. * !0 = failure
  4556. *
  4557. * PARAMETERS:
  4558. * *cd - pointer to core data
  4559. ******************************************************************************/
  4560. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4561. {
  4562. struct pt_hid_output hid_output = {
  4563. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4564. };
  4565. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4566. }
  4567. /*******************************************************************************
  4568. * FUNCTION: pt_hid_output_exec_panel_scan
  4569. *
  4570. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4571. * access to the DUT.
  4572. *
  4573. * RETURN:
  4574. * 0 = success
  4575. * !0 = failure
  4576. *
  4577. * PARAMETERS:
  4578. * *cd - pointer to core data
  4579. ******************************************************************************/
  4580. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4581. {
  4582. int rc;
  4583. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4584. if (rc < 0) {
  4585. pt_debug(cd->dev, DL_ERROR,
  4586. "%s: fail get exclusive ex=%p own=%p\n",
  4587. __func__, cd->exclusive_dev, cd->dev);
  4588. return rc;
  4589. }
  4590. rc = pt_hid_output_exec_panel_scan_(cd);
  4591. if (release_exclusive(cd, cd->dev) < 0)
  4592. pt_debug(cd->dev, DL_ERROR,
  4593. "%s: fail to release exclusive\n", __func__);
  4594. return rc;
  4595. }
  4596. /*******************************************************************************
  4597. * FUNCTION: pt_pip2_exec_panel_scan_
  4598. *
  4599. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4600. * the response
  4601. *
  4602. * RETURN:
  4603. * 0 = success
  4604. * !0 = failure
  4605. *
  4606. * PARAMETERS:
  4607. * *cd - pointer to core data
  4608. * scan_type - type of panel scan to perform (PIP2 only)
  4609. ******************************************************************************/
  4610. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4611. {
  4612. int rc = 0;
  4613. u8 data[2];
  4614. u8 read_buf[10];
  4615. u16 actual_read_len;
  4616. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4617. __func__, scan_type);
  4618. data[0] = scan_type;
  4619. rc = _pt_request_pip2_send_cmd(cd->dev,
  4620. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4621. data, 1, read_buf, &actual_read_len);
  4622. if (rc) {
  4623. pt_debug(cd->dev, DL_ERROR,
  4624. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4625. __func__, scan_type, rc);
  4626. }
  4627. return rc;
  4628. }
  4629. /*******************************************************************************
  4630. * FUNCTION: pt_pip2_exec_panel_scan
  4631. *
  4632. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4633. * access to the DUT.
  4634. *
  4635. * RETURN:
  4636. * 0 = success
  4637. * !0 = failure
  4638. *
  4639. * PARAMETERS:
  4640. * *cd - pointer to core data
  4641. * scan_type - type of panel scan to perform (PIP2 only)
  4642. ******************************************************************************/
  4643. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4644. {
  4645. int rc;
  4646. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4647. if (rc < 0) {
  4648. pt_debug(cd->dev, DL_ERROR,
  4649. "%s: fail get exclusive ex=%p own=%p\n",
  4650. __func__, cd->exclusive_dev, cd->dev);
  4651. return rc;
  4652. }
  4653. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4654. if (release_exclusive(cd, cd->dev) < 0)
  4655. pt_debug(cd->dev, DL_ERROR,
  4656. "%s: fail to release exclusive\n", __func__);
  4657. return rc;
  4658. }
  4659. /*******************************************************************************
  4660. * FUNCTION: _pt_request_pip_exec_panel_scan
  4661. *
  4662. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4663. * calls to the protected or unprotected call to
  4664. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4665. *
  4666. * RETURN:
  4667. * 0 = success
  4668. * !0 = failure
  4669. *
  4670. * PARAMETERS:
  4671. * *dev - pointer to device structure
  4672. * protect - flag to call protected or non-protected
  4673. * scan_type - type of panel scan to perform (PIP2 only)
  4674. ******************************************************************************/
  4675. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4676. int protect, u8 scan_type)
  4677. {
  4678. struct pt_core_data *cd = dev_get_drvdata(dev);
  4679. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4680. if (protect)
  4681. return pt_pip2_exec_panel_scan(cd, scan_type);
  4682. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4683. }
  4684. if (protect)
  4685. return pt_hid_output_exec_panel_scan(cd);
  4686. return pt_hid_output_exec_panel_scan_(cd);
  4687. }
  4688. /*******************************************************************************
  4689. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4690. *
  4691. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4692. * to retrieve the specified data type for a the last successful Execute
  4693. * Panel Scan command.
  4694. *
  4695. * RETURN:
  4696. * 0 = success
  4697. * !0 = failure
  4698. *
  4699. * PARAMETERS:
  4700. * *dev - pointer to device structure
  4701. * protect - flag to call protected or non-protected
  4702. * read_offset - read pointer offset
  4703. * read_count - length of data to read
  4704. * data_id - enumerated test ID to read selftest results from
  4705. * *response - pointer to store the read response status
  4706. * *config - pointer to store config data
  4707. * *actual_read_len - pointer to store data length actually read
  4708. * *read_buf - pointer to the read buffer
  4709. ******************************************************************************/
  4710. static int pt_hid_output_retrieve_panel_scan_(
  4711. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4712. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4713. u8 *read_buf)
  4714. {
  4715. int status;
  4716. u8 read_data_id;
  4717. int rc = 0;
  4718. int write_length = 5;
  4719. u8 write_buf[5];
  4720. u8 cmd_offset = 0;
  4721. u8 data_elem_size;
  4722. int size;
  4723. int data_size;
  4724. struct pt_hid_output hid_output = {
  4725. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4726. .write_length = write_length,
  4727. .write_buf = write_buf,
  4728. };
  4729. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4730. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4731. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4732. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4733. write_buf[cmd_offset++] = data_id;
  4734. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4735. if (rc)
  4736. return rc;
  4737. status = cd->response_buf[5];
  4738. if (status)
  4739. return -EINVAL;
  4740. read_data_id = cd->response_buf[6];
  4741. if (read_data_id != data_id)
  4742. return -EPROTO;
  4743. size = get_unaligned_le16(&cd->response_buf[0]);
  4744. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4745. *config = cd->response_buf[9];
  4746. data_elem_size = *config & 0x07;
  4747. data_size = *actual_read_len * data_elem_size;
  4748. if (read_buf)
  4749. memcpy(read_buf, &cd->response_buf[10], data_size);
  4750. if (response)
  4751. memcpy(response, cd->response_buf, size);
  4752. return rc;
  4753. }
  4754. /*******************************************************************************
  4755. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4756. *
  4757. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4758. * access to the DUT.
  4759. *
  4760. * RETURN:
  4761. * 0 = success
  4762. * !0 = failure
  4763. *
  4764. * PARAMETERS:
  4765. * *dev - pointer to device structure
  4766. * protect - flag to call protected or non-protected
  4767. * read_offset - read pointer offset
  4768. * read_count - length of data to read
  4769. * data_id - enumerated test ID to read selftest results from
  4770. * *response - pointer to store the read response status
  4771. * *config - pointer to store config data
  4772. * *actual_read_len - pointer to store data length actually read
  4773. * *read_buf - pointer to the read buffer
  4774. ******************************************************************************/
  4775. static int pt_hid_output_retrieve_panel_scan(
  4776. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4777. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4778. u8 *read_buf)
  4779. {
  4780. int rc;
  4781. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4782. if (rc < 0) {
  4783. pt_debug(cd->dev, DL_ERROR,
  4784. "%s: fail get exclusive ex=%p own=%p\n",
  4785. __func__, cd->exclusive_dev, cd->dev);
  4786. return rc;
  4787. }
  4788. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4789. read_count, data_id, response, config,
  4790. actual_read_len, read_buf);
  4791. if (release_exclusive(cd, cd->dev) < 0)
  4792. pt_debug(cd->dev, DL_ERROR,
  4793. "%s: fail to release exclusive\n", __func__);
  4794. return rc;
  4795. }
  4796. /*******************************************************************************
  4797. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4798. *
  4799. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4800. * calls to the protected or unprotected call to
  4801. * pt_hid_output_retrieve_panel_scan
  4802. *
  4803. * RETURN:
  4804. * 0 = success
  4805. * !0 = failure
  4806. *
  4807. * PARAMETERS:
  4808. * *dev - pointer to device structure
  4809. * protect - flag to call protected or non-protected
  4810. * read_offset - read pointer offset
  4811. * read_count - length of data to read
  4812. * data_id - enumerated test ID to read selftest results from
  4813. * *response - pointer to store the read response status
  4814. * *config - pointer to store config data
  4815. * *actual_read_len - pointer to store data length actually read
  4816. * *read_buf - pointer to the read buffer
  4817. ******************************************************************************/
  4818. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4819. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4820. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4821. {
  4822. struct pt_core_data *cd = dev_get_drvdata(dev);
  4823. if (protect)
  4824. return pt_hid_output_retrieve_panel_scan(cd,
  4825. read_offset, read_count, data_id, response,
  4826. config, actual_read_len, read_buf);
  4827. return pt_hid_output_retrieve_panel_scan_(cd,
  4828. read_offset, read_count, data_id, response,
  4829. config, actual_read_len, read_buf);
  4830. }
  4831. /*******************************************************************************
  4832. * FUNCTION: _pt_request_pip_user_cmd
  4833. *
  4834. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4835. * calls to the protected or unprotected call to
  4836. * pt_hid_output_user_cmd
  4837. *
  4838. * RETURN:
  4839. * 0 = success
  4840. * !0 = failure
  4841. *
  4842. * PARAMETERS:
  4843. * *dev - pointer to device structure
  4844. * protect - flag to call protected or non-protected
  4845. * read_len - length of data to read
  4846. * *read_buf - pointer to store read data
  4847. * write_len - length of data to write
  4848. * *write_buf - pointer to buffer to write
  4849. * *actual_read_len - pointer to store data length actually read
  4850. ******************************************************************************/
  4851. static int _pt_request_pip_user_cmd(struct device *dev,
  4852. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4853. u8 *write_buf, u16 *actual_read_len)
  4854. {
  4855. struct pt_core_data *cd = dev_get_drvdata(dev);
  4856. if (protect)
  4857. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4858. write_len, write_buf, actual_read_len);
  4859. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4860. write_len, write_buf, actual_read_len);
  4861. }
  4862. /*******************************************************************************
  4863. * FUNCTION: pt_hid_output_bl_get_information_
  4864. *
  4865. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4866. * DUT to retrieve bootloader version and chip identification information.
  4867. *
  4868. * RETURN:
  4869. * 0 = success
  4870. * !0 = failure
  4871. *
  4872. * PARAMETERS:
  4873. * *cd - pointer to core data
  4874. * *return_data - pointer to store the return data
  4875. *****************************************************************************/
  4876. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4877. u8 *return_data)
  4878. {
  4879. int rc;
  4880. int data_len;
  4881. struct pt_hid_output hid_output = {
  4882. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4883. };
  4884. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4885. if (rc)
  4886. return rc;
  4887. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4888. if (!data_len)
  4889. return -EPROTO;
  4890. memcpy(return_data, &cd->response_buf[8], data_len);
  4891. return 0;
  4892. }
  4893. /*******************************************************************************
  4894. * FUNCTION: pt_hid_output_bl_get_information
  4895. *
  4896. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4897. * access to the DUT.
  4898. *
  4899. * RETURN:
  4900. * 0 = success
  4901. * !0 = failure
  4902. *
  4903. * PARAMETERS:
  4904. * *cd - pointer to core data
  4905. * *return_data - pointer to store the return data
  4906. ******************************************************************************/
  4907. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4908. u8 *return_data)
  4909. {
  4910. int rc;
  4911. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4912. if (rc < 0) {
  4913. pt_debug(cd->dev, DL_ERROR,
  4914. "%s: fail get exclusive ex=%p own=%p\n",
  4915. __func__, cd->exclusive_dev, cd->dev);
  4916. return rc;
  4917. }
  4918. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4919. if (release_exclusive(cd, cd->dev) < 0)
  4920. pt_debug(cd->dev, DL_ERROR,
  4921. "%s: fail to release exclusive\n", __func__);
  4922. return rc;
  4923. }
  4924. /*******************************************************************************
  4925. * FUNCTION: _pt_request_pip_bl_get_information
  4926. *
  4927. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4928. * calls to the protected or unprotected call to
  4929. * pt_hid_output_bl_get_information
  4930. *
  4931. * RETURN:
  4932. * 0 = success
  4933. * !0 = failure
  4934. *
  4935. * PARAMETERS:
  4936. * *dev - pointer to device structure
  4937. * protect - flag to call protected or non-protected
  4938. * *return_data - pointer to store bl data
  4939. ******************************************************************************/
  4940. static int _pt_request_pip_bl_get_information(struct device *dev,
  4941. int protect, u8 *return_data)
  4942. {
  4943. struct pt_core_data *cd = dev_get_drvdata(dev);
  4944. if (protect)
  4945. return pt_hid_output_bl_get_information(cd, return_data);
  4946. return pt_hid_output_bl_get_information_(cd, return_data);
  4947. }
  4948. /*******************************************************************************
  4949. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4950. *
  4951. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4952. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4953. * and Design Data block in flash and enables the host to execute the Program
  4954. * and Verify Row command to bootload the application image and data.
  4955. *
  4956. * RETURN:
  4957. * 0 = success
  4958. * !0 = failure
  4959. *
  4960. * PARAMETERS:
  4961. * *cd - pointer to core data
  4962. * protect - flag to call protected or non-protected
  4963. * key_size - size of key
  4964. * *key_buf - pointer to key data to allow operation
  4965. * row_size - size of the meta data row
  4966. * *metadata_row_buf - pointer to meta data to write
  4967. ******************************************************************************/
  4968. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4969. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4970. {
  4971. u16 write_length = key_size + row_size;
  4972. u8 *write_buf;
  4973. int rc = 0;
  4974. struct pt_hid_output hid_output = {
  4975. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4976. .write_length = write_length,
  4977. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4978. };
  4979. write_buf = kzalloc(write_length, GFP_KERNEL);
  4980. if (!write_buf)
  4981. return -ENOMEM;
  4982. hid_output.write_buf = write_buf;
  4983. if (key_size)
  4984. memcpy(write_buf, key_buf, key_size);
  4985. if (row_size)
  4986. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4987. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4988. kfree(write_buf);
  4989. return rc;
  4990. }
  4991. /*******************************************************************************
  4992. * FUNCTION: pt_hid_output_bl_initiate_bl
  4993. *
  4994. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4995. * access to the DUT.
  4996. *
  4997. * RETURN:
  4998. * 0 = success
  4999. * !0 = failure
  5000. *
  5001. * PARAMETERS:
  5002. * *cd - pointer to core data
  5003. * key_size - size of key
  5004. * *key_buf - pointer to key data to allow operation
  5005. * row_size - size of the meta data row
  5006. * *metadata_row_buf - pointer to meta data to write
  5007. ******************************************************************************/
  5008. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5009. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5010. {
  5011. int rc;
  5012. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5013. if (rc < 0) {
  5014. pt_debug(cd->dev, DL_ERROR,
  5015. "%s: fail get exclusive ex=%p own=%p\n",
  5016. __func__, cd->exclusive_dev, cd->dev);
  5017. return rc;
  5018. }
  5019. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5020. row_size, metadata_row_buf);
  5021. if (release_exclusive(cd, cd->dev) < 0)
  5022. pt_debug(cd->dev, DL_ERROR,
  5023. "%s: fail to release exclusive\n", __func__);
  5024. return rc;
  5025. }
  5026. /*******************************************************************************
  5027. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5028. *
  5029. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5030. * calls to the protected or unprotected call to
  5031. * pt_hid_output_bl_initiate_bl
  5032. *
  5033. * RETURN:
  5034. * 0 = success
  5035. * !0 = failure
  5036. *
  5037. * PARAMETERS:
  5038. * *dev - pointer to device structure
  5039. * protect - flag to call protected or non-protected
  5040. * key_size - size of key
  5041. * *key_buf - pointer to key data to allow operation
  5042. * row_size - size of the meta data row
  5043. * *metadata_row_buf - pointer to meta data to write
  5044. ******************************************************************************/
  5045. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5046. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5047. u8 *metadata_row_buf)
  5048. {
  5049. struct pt_core_data *cd = dev_get_drvdata(dev);
  5050. if (protect)
  5051. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5052. row_size, metadata_row_buf);
  5053. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5054. row_size, metadata_row_buf);
  5055. }
  5056. /*******************************************************************************
  5057. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5058. *
  5059. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5060. * and program a 128-byte row into the flash, and then verifies written data.
  5061. *
  5062. * RETURN:
  5063. * 0 = success
  5064. * !0 = failure
  5065. *
  5066. * PARAMETERS:
  5067. * *cd - pointer to core data
  5068. * data_len - length of data_buf
  5069. * *data_buf - firmware image to program
  5070. ******************************************************************************/
  5071. static int pt_hid_output_bl_program_and_verify_(
  5072. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5073. {
  5074. struct pt_hid_output hid_output = {
  5075. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5076. .write_length = data_len,
  5077. .write_buf = data_buf,
  5078. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5079. };
  5080. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5081. }
  5082. /*******************************************************************************
  5083. * FUNCTION: pt_hid_output_bl_program_and_verify
  5084. *
  5085. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5086. * access to the DUT.
  5087. *
  5088. * RETURN:
  5089. * 0 = success
  5090. * !0 = failure
  5091. *
  5092. * PARAMETERS:
  5093. * *cd - pointer to core data
  5094. * data_len - length of data_buf
  5095. * *data_buf - firmware image to program
  5096. ******************************************************************************/
  5097. static int pt_hid_output_bl_program_and_verify(
  5098. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5099. {
  5100. int rc;
  5101. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5102. if (rc < 0) {
  5103. pt_debug(cd->dev, DL_ERROR,
  5104. "%s: fail get exclusive ex=%p own=%p\n",
  5105. __func__, cd->exclusive_dev, cd->dev);
  5106. return rc;
  5107. }
  5108. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5109. if (release_exclusive(cd, cd->dev) < 0)
  5110. pt_debug(cd->dev, DL_ERROR,
  5111. "%s: fail to release exclusive\n", __func__);
  5112. return rc;
  5113. }
  5114. /*******************************************************************************
  5115. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5116. *
  5117. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5118. * to request to have the BL program and verify a FW image
  5119. *
  5120. * RETURN:
  5121. * 0 = success
  5122. * !0 = failure
  5123. *
  5124. * PARAMETERS:
  5125. * *dev - pointer to device structure
  5126. * protect - boolean to determine to call the protected function
  5127. * data_len - length of data_buf
  5128. * *data_buf - firmware image to program
  5129. ******************************************************************************/
  5130. static int _pt_request_pip_bl_program_and_verify(
  5131. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5132. {
  5133. struct pt_core_data *cd = dev_get_drvdata(dev);
  5134. if (protect)
  5135. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5136. data_buf);
  5137. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5138. data_buf);
  5139. }
  5140. /*******************************************************************************
  5141. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5142. *
  5143. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5144. * perform a full verification of the application integrity by calculating the
  5145. * CRC of the image in flash and compare it to the expected CRC stored in the
  5146. * Metadata row.
  5147. *
  5148. * RETURN:
  5149. * 0 = success
  5150. * !0 = failure
  5151. *
  5152. * PARAMETERS:
  5153. * *cd - pointer to core data
  5154. * *result - pointer to store result
  5155. ******************************************************************************/
  5156. static int pt_hid_output_bl_verify_app_integrity_(
  5157. struct pt_core_data *cd, u8 *result)
  5158. {
  5159. int rc;
  5160. struct pt_hid_output hid_output = {
  5161. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5162. };
  5163. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5164. if (rc) {
  5165. *result = 0;
  5166. return rc;
  5167. }
  5168. *result = cd->response_buf[8];
  5169. return 0;
  5170. }
  5171. /*******************************************************************************
  5172. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5173. *
  5174. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5175. * exclusive access to the DUT.
  5176. *
  5177. * RETURN:
  5178. * 0 = success
  5179. * !0 = failure
  5180. *
  5181. * PARAMETERS:
  5182. * *cd - pointer to core data
  5183. * *result - pointer to store result
  5184. ******************************************************************************/
  5185. static int pt_hid_output_bl_verify_app_integrity(
  5186. struct pt_core_data *cd, u8 *result)
  5187. {
  5188. int rc;
  5189. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5190. if (rc < 0) {
  5191. pt_debug(cd->dev, DL_ERROR,
  5192. "%s: fail get exclusive ex=%p own=%p\n",
  5193. __func__, cd->exclusive_dev, cd->dev);
  5194. return rc;
  5195. }
  5196. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5197. if (release_exclusive(cd, cd->dev) < 0)
  5198. pt_debug(cd->dev, DL_ERROR,
  5199. "%s: fail to release exclusive\n", __func__);
  5200. return rc;
  5201. }
  5202. /*******************************************************************************
  5203. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5204. *
  5205. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5206. * to request to have the BL verify the application integrity (PIP1.x only)
  5207. *
  5208. * RETURN:
  5209. * 0 = success
  5210. * !0 = failure
  5211. *
  5212. * PARAMETERS:
  5213. * *dev - pointer to device structure
  5214. * protect - boolean to determine to call the protected function
  5215. * *result - pointer to store result
  5216. ******************************************************************************/
  5217. static int _pt_request_pip_bl_verify_app_integrity(
  5218. struct device *dev, int protect, u8 *result)
  5219. {
  5220. struct pt_core_data *cd = dev_get_drvdata(dev);
  5221. if (protect)
  5222. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5223. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5224. }
  5225. /*******************************************************************************
  5226. * FUNCTION: pt_hid_output_bl_launch_app_
  5227. *
  5228. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5229. * application from bootloader (PIP1.x only).
  5230. *
  5231. * RETURN:
  5232. * 0 = success
  5233. * !0 = failure
  5234. *
  5235. * PARAMETERS:
  5236. * *cd - pointer to core data
  5237. ******************************************************************************/
  5238. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5239. {
  5240. struct pt_hid_output hid_output = {
  5241. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5242. .reset_expected = 1,
  5243. };
  5244. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5245. }
  5246. /*******************************************************************************
  5247. * FUNCTION: pt_hid_output_bl_launch_app
  5248. *
  5249. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5250. * to the DUT.
  5251. *
  5252. * RETURN:
  5253. * 0 = success
  5254. * !0 = failure
  5255. *
  5256. * PARAMETERS:
  5257. * *cd - pointer to core data
  5258. ******************************************************************************/
  5259. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5260. {
  5261. int rc;
  5262. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5263. if (rc < 0) {
  5264. pt_debug(cd->dev, DL_ERROR,
  5265. "%s: fail get exclusive ex=%p own=%p\n",
  5266. __func__, cd->exclusive_dev, cd->dev);
  5267. return rc;
  5268. }
  5269. rc = pt_hid_output_bl_launch_app_(cd);
  5270. if (release_exclusive(cd, cd->dev) < 0)
  5271. pt_debug(cd->dev, DL_ERROR,
  5272. "%s: fail to release exclusive\n", __func__);
  5273. return rc;
  5274. }
  5275. /*******************************************************************************
  5276. * FUNCTION: _pt_request_pip_launch_app
  5277. *
  5278. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5279. * to request to have the BL launch the application. (PIP1.x only)
  5280. *
  5281. * RETURN:
  5282. * 0 = success
  5283. * !0 = failure
  5284. *
  5285. * PARAMETERS:
  5286. * *dev - pointer to device structure
  5287. * protect - boolean to determine to call the protected function
  5288. ******************************************************************************/
  5289. static int _pt_request_pip_launch_app(struct device *dev,
  5290. int protect)
  5291. {
  5292. struct pt_core_data *cd = dev_get_drvdata(dev);
  5293. if (protect)
  5294. return pt_hid_output_bl_launch_app(cd);
  5295. return pt_hid_output_bl_launch_app_(cd);
  5296. }
  5297. /*******************************************************************************
  5298. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5299. *
  5300. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5301. * value store in the System Information.
  5302. *
  5303. * RETURN:
  5304. * 0 = success
  5305. * !0 = failure
  5306. *
  5307. * PARAMETERS:
  5308. * *cd - pointer to core data
  5309. * *panel_id - pointer to where the panel ID will be stored
  5310. ******************************************************************************/
  5311. static int pt_hid_output_bl_get_panel_id_(
  5312. struct pt_core_data *cd, u8 *panel_id)
  5313. {
  5314. int rc;
  5315. struct pt_hid_output hid_output = {
  5316. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5317. };
  5318. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5319. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5320. pt_debug(cd->dev, DL_ERROR,
  5321. "%s: Get Panel ID command not supported\n",
  5322. __func__);
  5323. *panel_id = PANEL_ID_NOT_ENABLED;
  5324. return 0;
  5325. } else if (rc < 0) {
  5326. pt_debug(cd->dev, DL_ERROR,
  5327. "%s: Error on Get Panel ID command\n", __func__);
  5328. return rc;
  5329. }
  5330. *panel_id = cd->response_buf[8];
  5331. return 0;
  5332. }
  5333. /*******************************************************************************
  5334. * FUNCTION: pt_hid_output_bl_get_panel_id
  5335. *
  5336. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5337. * to the DUT.
  5338. *
  5339. * RETURN:
  5340. * 0 = success
  5341. * !0 = failure
  5342. *
  5343. * PARAMETERS:
  5344. * *cd - pointer to core data
  5345. * *panel_id - pointer to where the panel ID will be stored
  5346. ******************************************************************************/
  5347. static int pt_hid_output_bl_get_panel_id(
  5348. struct pt_core_data *cd, u8 *panel_id)
  5349. {
  5350. int rc;
  5351. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5352. if (rc < 0) {
  5353. pt_debug(cd->dev, DL_ERROR,
  5354. "%s: fail get exclusive ex=%p own=%p\n",
  5355. __func__, cd->exclusive_dev, cd->dev);
  5356. return rc;
  5357. }
  5358. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5359. if (release_exclusive(cd, cd->dev) < 0)
  5360. pt_debug(cd->dev, DL_ERROR,
  5361. "%s: fail to release exclusive\n", __func__);
  5362. return rc;
  5363. }
  5364. /*******************************************************************************
  5365. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5366. *
  5367. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5368. * to have the BL retrieve the panel ID
  5369. *
  5370. * RETURN:
  5371. * 0 = success
  5372. * !0 = failure
  5373. *
  5374. * PARAMETERS:
  5375. * *dev - pointer to device structure
  5376. * protect - flag to run in protected mode
  5377. * *panel_id - pointer to where the panel ID will be stored
  5378. ******************************************************************************/
  5379. static int _pt_request_pip_bl_get_panel_id(
  5380. struct device *dev, int protect, u8 *panel_id)
  5381. {
  5382. struct pt_core_data *cd = dev_get_drvdata(dev);
  5383. if (protect)
  5384. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5385. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5386. }
  5387. /*******************************************************************************
  5388. * FUNCTION: pt_pip2_get_mode_sysmode_
  5389. *
  5390. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5391. * PIP2 STATUS command.
  5392. *
  5393. * RETURN:
  5394. * 0 = success
  5395. * !0 = failure
  5396. *
  5397. * PARAMETERS:
  5398. * *cd - pointer to core data structure
  5399. * *mode - pointer to store the retrieved mode
  5400. * *sys_mode - pointer to store the FW system mode
  5401. ******************************************************************************/
  5402. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5403. u8 *mode, u8 *sys_mode)
  5404. {
  5405. u16 actual_read_len;
  5406. u8 read_buf[12];
  5407. u8 status, boot;
  5408. int rc = 0;
  5409. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5410. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5411. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5412. __func__, rc);
  5413. if (!rc) {
  5414. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5415. "PIP2 STATUS");
  5416. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5417. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5418. if (sys_mode) {
  5419. if (status == PIP2_RSP_ERR_NONE &&
  5420. boot == PIP2_STATUS_APP_EXEC)
  5421. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5422. else
  5423. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5424. }
  5425. if (mode) {
  5426. if (status == PIP2_RSP_ERR_NONE &&
  5427. boot == PIP2_STATUS_BOOT_EXEC)
  5428. *mode = PT_MODE_BOOTLOADER;
  5429. else if (status == PIP2_RSP_ERR_NONE &&
  5430. boot == PIP2_STATUS_APP_EXEC)
  5431. *mode = PT_MODE_OPERATIONAL;
  5432. else
  5433. *mode = PT_MODE_UNKNOWN;
  5434. }
  5435. } else {
  5436. if (mode)
  5437. *mode = PT_MODE_UNKNOWN;
  5438. if (sys_mode)
  5439. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5440. pt_debug(cd->dev, DL_WARN,
  5441. "%s: Mode and sys_mode could not be determined\n",
  5442. __func__);
  5443. }
  5444. return rc;
  5445. }
  5446. /*******************************************************************************
  5447. * FUNCTION: pt_pip2_get_mode_sysmode
  5448. *
  5449. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5450. * access to the DUT.
  5451. *
  5452. * RETURN:
  5453. * 0 = success
  5454. * !0 = failure
  5455. *
  5456. * PARAMETERS:
  5457. * *cd - pointer to core data structure
  5458. * *mode - pointer to store the retrieved mode
  5459. * *sys_mode - pointer to store the FW system mode
  5460. ******************************************************************************/
  5461. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5462. u8 *mode, u8 *sys_mode)
  5463. {
  5464. int rc;
  5465. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5466. if (rc < 0) {
  5467. pt_debug(cd->dev, DL_ERROR,
  5468. "%s: fail get exclusive ex=%p own=%p\n",
  5469. __func__, cd->exclusive_dev, cd->dev);
  5470. return rc;
  5471. }
  5472. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5473. if (release_exclusive(cd, cd->dev) < 0)
  5474. pt_debug(cd->dev, DL_ERROR,
  5475. "%s: fail to release exclusive\n", __func__);
  5476. return rc;
  5477. }
  5478. /*******************************************************************************
  5479. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5480. *
  5481. * SUMMARY: Function pointer included in core_commands struct for external
  5482. * calls to the protected or unprotected call to
  5483. * pt_pip2_get_mode_sysmode
  5484. *
  5485. * RETURN:
  5486. * 0 = success
  5487. * !0 = failure
  5488. *
  5489. * PARAMETERS:
  5490. * *dev - pointer to device structure
  5491. * *mode - pointer to store the retrieved mode
  5492. * *sys_mode - pointer to store the FW system mode
  5493. ******************************************************************************/
  5494. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5495. int protect, u8 *mode, u8 *sys_mode)
  5496. {
  5497. struct pt_core_data *cd = dev_get_drvdata(dev);
  5498. if (protect)
  5499. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5500. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5501. }
  5502. /*******************************************************************************
  5503. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5504. *
  5505. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5506. * mode only the following PIP commands will be serviced, any other PIP
  5507. * command the FW will respond with an "Invalid PIP Command" response.
  5508. * - Get HID Descriptor (Register 0x0001, no command ID)
  5509. * - Reset (Register 0x0005, RESET HID request)
  5510. * - Ping (Register 0x0004, Command ID 0x00
  5511. * - Get System Information (Register 0x0004, Command ID 0x02)
  5512. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5513. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5514. * This function will loop on the results of the STATUS command until
  5515. * the FW reports it is out of BOOT mode.
  5516. *
  5517. * NOTE:
  5518. * - This function will update cd->fw_system_mode
  5519. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5520. *
  5521. * RETURN:
  5522. * 0 = success
  5523. * !0 = failure
  5524. *
  5525. * PARAMETERS:
  5526. * *cd - pointer to core data
  5527. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5528. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5529. ******************************************************************************/
  5530. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5531. int *actual_wait)
  5532. {
  5533. int loop = 0;
  5534. u8 sys_mode = cd->fw_system_mode;
  5535. u8 pause = 10; /* in ms */
  5536. int rc = 0;
  5537. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5538. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5539. /*
  5540. * For PIP <1.11, no support for polling wait so do a hard
  5541. * coded wait and assume the FW is out of BOOT. Added 1 to
  5542. * timeout to make it clear in kmsg if non polling was done.
  5543. */
  5544. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5545. pt_debug(cd->dev, DL_ERROR,
  5546. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5547. __func__,
  5548. cd->sysinfo.ttdata.pip_ver_major,
  5549. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5550. msleep(*actual_wait);
  5551. sys_mode = FW_SYS_MODE_SCANNING;
  5552. }
  5553. if (sys_mode == FW_SYS_MODE_BOOT) {
  5554. while (!rc && loop <= max_loop &&
  5555. (sys_mode == FW_SYS_MODE_BOOT)) {
  5556. loop++;
  5557. usleep_range(9000, pause * 1000);
  5558. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5559. pt_debug(cd->dev, DL_DEBUG,
  5560. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5561. __func__, loop * pause, sys_mode);
  5562. }
  5563. *actual_wait = (int)(loop * pause);
  5564. pt_debug(cd->dev, DL_WARN,
  5565. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5566. __func__, *actual_wait, sys_mode);
  5567. if (rc)
  5568. sys_mode = FW_SYS_MODE_UNDEFINED;
  5569. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5570. sys_mode == FW_SYS_MODE_UNDEFINED)
  5571. rc = -EBUSY;
  5572. }
  5573. mutex_lock(&cd->system_lock);
  5574. cd->fw_system_mode = sys_mode;
  5575. mutex_unlock(&cd->system_lock);
  5576. return rc;
  5577. }
  5578. /*******************************************************************************
  5579. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5580. *
  5581. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5582. * access to the DUT.
  5583. *
  5584. * RETURN:
  5585. * 0 = success
  5586. * !0 = failure
  5587. *
  5588. * PARAMETERS:
  5589. * *cd - pointer to core data
  5590. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5591. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5592. ******************************************************************************/
  5593. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5594. int *actual_wait)
  5595. {
  5596. int rc;
  5597. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5598. if (rc < 0) {
  5599. pt_debug(cd->dev, DL_ERROR,
  5600. "%s: fail get exclusive ex=%p own=%p\n",
  5601. __func__, cd->exclusive_dev, cd->dev);
  5602. return rc;
  5603. }
  5604. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5605. if (release_exclusive(cd, cd->dev) < 0)
  5606. pt_debug(cd->dev, DL_ERROR,
  5607. "%s: fail to release exclusive\n", __func__);
  5608. return rc;
  5609. }
  5610. /*******************************************************************************
  5611. * FUNCTION: _pt_get_fw_sys_mode
  5612. *
  5613. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5614. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5615. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5616. * information but any PIP command above 0x1F requires scanning to be disabled
  5617. * before it will be operational. If scanning was not disabled before sending
  5618. * these PIP commands the FW will respond with a 6 byte error response. So to
  5619. * safely determine the scanning state, a PIP message that will not affect the
  5620. * operation of the FW was chosen:
  5621. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5622. * received scanning is active.
  5623. *
  5624. * RETURN:
  5625. * 0 = success
  5626. * !0 = failure
  5627. *
  5628. * PARAMETERS:
  5629. * *cd - pointer to core data
  5630. * *sys_mode - pointer to FW System mode
  5631. * *mode - pointer to mode (BL/FW)
  5632. ******************************************************************************/
  5633. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5634. {
  5635. int write_length = 1;
  5636. int report_length;
  5637. int rc = 0;
  5638. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5639. u8 tmp_mode = PT_MODE_UNKNOWN;
  5640. u8 param[1] = { PT_TCH_PARM_EBID };
  5641. struct pt_hid_output hid_output = {
  5642. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5643. .write_length = write_length,
  5644. .write_buf = param,
  5645. .novalidate = true,
  5646. };
  5647. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5648. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5649. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5650. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5651. __func__, tmp_sys_mode, tmp_mode);
  5652. if (!rc) {
  5653. if (tmp_mode != PT_MODE_OPERATIONAL)
  5654. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5655. }
  5656. goto exit;
  5657. }
  5658. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5659. if (cd->mode != PT_MODE_OPERATIONAL) {
  5660. tmp_mode = cd->mode;
  5661. goto exit;
  5662. }
  5663. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5664. if (rc)
  5665. goto exit;
  5666. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5667. if ((report_length == 0x06) &&
  5668. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5669. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5670. tmp_mode = PIP2_STATUS_APP_EXEC;
  5671. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5672. } else if ((report_length == 0x0A) &&
  5673. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5674. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5675. tmp_mode = PIP2_STATUS_APP_EXEC;
  5676. tmp_sys_mode = FW_SYS_MODE_TEST;
  5677. }
  5678. exit:
  5679. if (mode)
  5680. *mode = tmp_mode;
  5681. if (sys_mode)
  5682. *sys_mode = tmp_sys_mode;
  5683. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5684. __func__, tmp_mode, tmp_sys_mode);
  5685. return rc;
  5686. }
  5687. /*******************************************************************************
  5688. * FUNCTION: pt_get_fw_sys_mode
  5689. *
  5690. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5691. * is active or not.
  5692. *
  5693. * RETURN:
  5694. * 0 = success
  5695. * !0 = failure
  5696. *
  5697. * PARAMETERS:
  5698. * *cd - pointer to core data
  5699. * *sys_mode - pointer to fw system mode
  5700. * *mode - pointer to mode
  5701. ******************************************************************************/
  5702. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5703. {
  5704. int rc;
  5705. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5706. if (rc < 0) {
  5707. pt_debug(cd->dev, DL_ERROR,
  5708. "%s: fail get exclusive ex=%p own=%p\n",
  5709. __func__, cd->exclusive_dev, cd->dev);
  5710. return rc;
  5711. }
  5712. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5713. if (release_exclusive(cd, cd->dev) < 0)
  5714. pt_debug(cd->dev, DL_ERROR,
  5715. "%s: fail to release exclusive\n", __func__);
  5716. return rc;
  5717. }
  5718. /*******************************************************************************
  5719. * FUNCTION: _pt_request_get_fw_sys_mode
  5720. *
  5721. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5722. * to request to get scan state
  5723. *
  5724. * RETURN:
  5725. * 0 = success
  5726. * !0 = failure
  5727. *
  5728. * PARAMETERS:
  5729. * *dev - pointer to device structure
  5730. * protect - flag to call protected or non-protected
  5731. * *sys_mode - pointer to FW system mode
  5732. * *mode - pointer to mode
  5733. ******************************************************************************/
  5734. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5735. u8 *sys_mode, u8 *mode)
  5736. {
  5737. struct pt_core_data *cd = dev_get_drvdata(dev);
  5738. if (protect)
  5739. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5740. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5741. }
  5742. /* Default hid descriptor to provide basic register map */
  5743. const struct pt_hid_desc hid_desc_default = {
  5744. 230, /* hid_desc_len */
  5745. HID_APP_REPORT_ID, /* packet_id */
  5746. 0x00, /* reserved_byte */
  5747. 0x0100, /* bcd_version */
  5748. 0x00EC, /* report_desc_len */
  5749. 0x0002, /* report_desc_register */
  5750. 0x0003, /* input_register */
  5751. 0x00FE, /* max_input_len */
  5752. 0x0004, /* output_register */
  5753. 0x00FE, /* max_output_len */
  5754. 0x0005, /* command_register */
  5755. 0x0006, /* data_register */
  5756. 0x04B4, /* vendor_id */
  5757. 0xC101, /* product_id */
  5758. 0x0100, /* version_id */
  5759. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5760. };
  5761. /*******************************************************************************
  5762. * FUNCTION: pt_init_hid_descriptor
  5763. *
  5764. * SUMMARY: Setup default values for HID descriptor structure
  5765. *
  5766. *
  5767. * PARAMETERS:
  5768. * *desc - pointer to the HID descriptor data read back from DUT
  5769. ******************************************************************************/
  5770. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5771. {
  5772. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5773. }
  5774. /*******************************************************************************
  5775. * FUNCTION: pt_get_hid_descriptor_
  5776. *
  5777. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5778. * into the HID descriptor structure
  5779. *
  5780. * RETURN:
  5781. * 0 = success
  5782. * !0 = failure
  5783. *
  5784. * PARAMETERS:
  5785. * *cd - pointer to core data
  5786. * *desc - pointer to the HID descriptor data read back from DUT
  5787. ******************************************************************************/
  5788. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5789. struct pt_hid_desc *desc)
  5790. {
  5791. struct device *dev = cd->dev;
  5792. int rc = 0;
  5793. int t;
  5794. u8 cmd[2];
  5795. /*
  5796. * During startup the HID descriptor is required for all future
  5797. * processing. If IRQ is already asserted due to an early touch report
  5798. * the report must be cleared before sending command.
  5799. */
  5800. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5801. /* Read HID descriptor length and version */
  5802. mutex_lock(&cd->system_lock);
  5803. cd->hid_cmd_state = 1;
  5804. mutex_unlock(&cd->system_lock);
  5805. /* Set HID descriptor register */
  5806. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5807. sizeof(cd->hid_core.hid_desc_register));
  5808. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5809. __func__, sizeof(cmd));
  5810. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5811. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5812. if (rc) {
  5813. pt_debug(dev, DL_ERROR,
  5814. "%s: failed to get HID descriptor, rc=%d\n",
  5815. __func__, rc);
  5816. goto error;
  5817. }
  5818. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5819. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5820. if (IS_TMO(t)) {
  5821. #ifdef TTDL_DIAGNOSTICS
  5822. cd->bus_transmit_error_count++;
  5823. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5824. #endif /* TTDL_DIAGNOSTICS */
  5825. pt_debug(cd->dev, DL_ERROR,
  5826. "%s: HID get descriptor timed out\n", __func__);
  5827. rc = -ETIME;
  5828. goto error;
  5829. } else {
  5830. cd->hw_detected = true;
  5831. }
  5832. /* Load the HID descriptor including all registers */
  5833. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5834. /* Check HID descriptor length and version */
  5835. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5836. le16_to_cpu(desc->hid_desc_len),
  5837. le16_to_cpu(desc->bcd_version));
  5838. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5839. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5840. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5841. __func__);
  5842. return -ENODEV;
  5843. }
  5844. goto exit;
  5845. error:
  5846. mutex_lock(&cd->system_lock);
  5847. cd->hid_cmd_state = 0;
  5848. mutex_unlock(&cd->system_lock);
  5849. exit:
  5850. return rc;
  5851. }
  5852. /*******************************************************************************
  5853. * FUNCTION: pt_get_hid_descriptor
  5854. *
  5855. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5856. *
  5857. * RETURN:
  5858. * 0 = success
  5859. * !0 = failure
  5860. *
  5861. * PARAMETERS:
  5862. * *cd - pointer to core data
  5863. * *desc - pointer to the HID descriptor data read back from DUT
  5864. ******************************************************************************/
  5865. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5866. struct pt_hid_desc *desc)
  5867. {
  5868. int rc;
  5869. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5870. if (rc < 0) {
  5871. pt_debug(cd->dev, DL_ERROR,
  5872. "%s: fail get exclusive ex=%p own=%p\n",
  5873. __func__, cd->exclusive_dev, cd->dev);
  5874. return rc;
  5875. }
  5876. rc = pt_get_hid_descriptor_(cd, desc);
  5877. if (release_exclusive(cd, cd->dev) < 0)
  5878. pt_debug(cd->dev, DL_ERROR,
  5879. "%s: fail to release exclusive\n", __func__);
  5880. return rc;
  5881. }
  5882. /*******************************************************************************
  5883. * FUNCTION: pt_pip2_get_version_
  5884. *
  5885. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5886. * cd-ttdata
  5887. *
  5888. * RETURN:
  5889. * 0 = success
  5890. * !0 = failure
  5891. *
  5892. * PARAMETERS:
  5893. * *cd - pointer to core data
  5894. ******************************************************************************/
  5895. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5896. {
  5897. int rc = 0;
  5898. int status;
  5899. u8 read_buf[64];
  5900. u16 actual_read_len;
  5901. rc = _pt_request_pip2_send_cmd(cd->dev,
  5902. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5903. NULL, 0, read_buf, &actual_read_len);
  5904. if (rc) {
  5905. pt_debug(cd->dev, DL_ERROR,
  5906. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5907. __func__, rc);
  5908. return rc;
  5909. }
  5910. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5911. if (status == 0) {
  5912. /* Parse the PIP2 VERSION response into ttdata */
  5913. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5914. } else {
  5915. pt_debug(cd->dev, DL_ERROR,
  5916. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5917. __func__, status);
  5918. return status;
  5919. }
  5920. return rc;
  5921. }
  5922. /*******************************************************************************
  5923. * FUNCTION: pt_pip2_get_version
  5924. *
  5925. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5926. * access to the DUT.
  5927. *
  5928. * RETURN:
  5929. * 0 = success
  5930. * !0 = failure
  5931. *
  5932. * PARAMETERS:
  5933. * *cd - pointer to core data structure
  5934. ******************************************************************************/
  5935. static int pt_pip2_get_version(struct pt_core_data *cd)
  5936. {
  5937. int rc;
  5938. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5939. if (rc < 0) {
  5940. pt_debug(cd->dev, DL_ERROR,
  5941. "%s: fail get exclusive ex=%p own=%p\n",
  5942. __func__, cd->exclusive_dev, cd->dev);
  5943. return rc;
  5944. }
  5945. rc = pt_pip2_get_version_(cd);
  5946. if (release_exclusive(cd, cd->dev) < 0)
  5947. pt_debug(cd->dev, DL_ERROR,
  5948. "%s: fail to release exclusive\n", __func__);
  5949. return rc;
  5950. }
  5951. /*******************************************************************************
  5952. * FUNCTION: _pt_request_active_pip_protocol
  5953. *
  5954. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5955. * Function will return PIP version of BL or application based on
  5956. * when it's called.
  5957. *
  5958. * RETURN:
  5959. * 0 = success
  5960. * !0 = failure
  5961. *
  5962. * PARAMETERS:
  5963. * *dev - pointer to device structure
  5964. * protect - flag to run in protected mode
  5965. * *pip_version_major - pointer to store PIP major version
  5966. * *pip_version_minor - pointer to store PIP minor version
  5967. ******************************************************************************/
  5968. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5969. u8 *pip_version_major, u8 *pip_version_minor)
  5970. {
  5971. struct pt_core_data *cd = dev_get_drvdata(dev);
  5972. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5973. int rc = 0;
  5974. struct pt_hid_output sys_info = {
  5975. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5976. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5977. };
  5978. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5979. /* Skip PIP2 command if DUT generation is confirmed */
  5980. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5981. goto skip_pip2_command;
  5982. rc = pt_pip2_get_version_(cd);
  5983. if (!rc) {
  5984. *pip_version_major = ttdata->pip_ver_major;
  5985. *pip_version_minor = ttdata->pip_ver_minor;
  5986. pt_debug(dev, DL_INFO,
  5987. "%s: pip_version = %d.%d\n", __func__,
  5988. *pip_version_major, *pip_version_minor);
  5989. } else {
  5990. /*
  5991. * Legacy products do not support the pip2 protocol to get
  5992. * pip version. However, they do support the "get sysinfo"
  5993. * command to get pip version from FW, but the bootloader
  5994. * does not support it. This function will try "get sysinfo"
  5995. * command if the pip2 command failed but this cmd could also
  5996. * fail if DUT is stuck in bootloader mode.
  5997. */
  5998. pt_debug(dev, DL_INFO,
  5999. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6000. __func__, rc);
  6001. skip_pip2_command:
  6002. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6003. if (!rc) {
  6004. *pip_version_minor =
  6005. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6006. *pip_version_major =
  6007. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6008. pt_debug(dev, DL_INFO,
  6009. "%s: pip_version = %d.%d\n", __func__,
  6010. *pip_version_major, *pip_version_minor);
  6011. } else {
  6012. *pip_version_major = 0;
  6013. *pip_version_minor = 0;
  6014. pt_debug(dev, DL_ERROR,
  6015. "%s: pip_version Not Detected\n", __func__);
  6016. }
  6017. }
  6018. return rc;
  6019. }
  6020. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6021. /*******************************************************************************
  6022. * FUNCTION: _pt_detect_dut_generation
  6023. *
  6024. * SUMMARY: Determine the generation of device that we are communicating with:
  6025. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6026. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6027. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6028. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6029. * coming form the BL or the FW:
  6030. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6031. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6032. * This function will return a modified status if it detects the DUT
  6033. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6034. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6035. *
  6036. * RETURN:
  6037. * 0 = success
  6038. * !0 = failure
  6039. *
  6040. * PARAMETERS:
  6041. * *dev - pointer to device structure
  6042. * *status - pointer to status bitmask
  6043. * *dut_gen - pointer to store the dut_generation
  6044. * *mode - pointer to store the PT_MODE
  6045. ******************************************************************************/
  6046. static int _pt_detect_dut_generation(struct device *dev,
  6047. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6048. {
  6049. int rc = 0;
  6050. u8 dut_gen_tmp = DUT_UNKNOWN;
  6051. u8 mode_tmp = PT_MODE_UNKNOWN;
  6052. u8 attempt = 1;
  6053. u32 status_tmp = STARTUP_STATUS_START;
  6054. struct pt_core_data *cd = dev_get_drvdata(dev);
  6055. struct pt_hid_desc hid_desc;
  6056. memset(&hid_desc, 0, sizeof(hid_desc));
  6057. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6058. while (rc && attempt < 3) {
  6059. attempt++;
  6060. usleep_range(2000, 5000);
  6061. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6062. }
  6063. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6064. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6065. mode_tmp = PT_MODE_BOOTLOADER;
  6066. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6067. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6068. rc = pt_pip2_get_version_(cd);
  6069. if (!rc)
  6070. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6071. else
  6072. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6073. mode_tmp = PT_MODE_OPERATIONAL;
  6074. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6075. rc = 0; /* To return success instead of error code */
  6076. } else if (rc) {
  6077. rc = pt_pip2_get_version_(cd);
  6078. if (!rc) {
  6079. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6080. mode_tmp = PT_MODE_BOOTLOADER;
  6081. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6082. }
  6083. }
  6084. mutex_lock(&cd->system_lock);
  6085. if (dut_gen)
  6086. *dut_gen = dut_gen_tmp;
  6087. if (mode)
  6088. *mode = mode_tmp;
  6089. if (status)
  6090. *status = status_tmp;
  6091. mutex_unlock(&cd->system_lock);
  6092. #ifdef TTDL_DIAGNOSTICS
  6093. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6094. __func__, dut_gen_tmp, mode_tmp);
  6095. #endif /* TTDL_DIAGNOSTICS */
  6096. return rc;
  6097. }
  6098. /*******************************************************************************
  6099. * FUNCTION: _pt_request_dut_generation
  6100. *
  6101. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6102. * to get current dut generation.
  6103. *
  6104. * NOTE: This function WILL NOT try to determine dut generation.
  6105. *
  6106. * RETURN:
  6107. * The current dut generation.
  6108. *
  6109. * PARAMETERS:
  6110. * *dev - pointer to device structure
  6111. ******************************************************************************/
  6112. static int _pt_request_dut_generation(struct device *dev)
  6113. {
  6114. struct pt_core_data *cd = dev_get_drvdata(dev);
  6115. return cd->active_dut_generation;
  6116. }
  6117. #define HW_VERSION_LEN_MAX 13
  6118. /*******************************************************************************
  6119. * FUNCTION: _legacy_generate_hw_version
  6120. *
  6121. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6122. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6123. * command) to the hw_version.
  6124. *
  6125. * RETURN:
  6126. * 0 = success
  6127. * !0 = failure
  6128. *
  6129. * PARAMETERS:
  6130. * *cd - pointer to core data
  6131. * *hw_version - pointer to store the hardware version
  6132. ******************************************************************************/
  6133. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6134. char *hw_version)
  6135. {
  6136. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6137. if (cd->sysinfo.ready) {
  6138. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6139. ttdata->jtag_id_h, cd->pid_for_loader);
  6140. return 0;
  6141. } else if (cd->bl_info.ready) {
  6142. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6143. cd->bl_info.chip_id, cd->pid_for_loader);
  6144. return 0;
  6145. } else {
  6146. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6147. pt_debug(cd->dev, DL_ERROR,
  6148. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6149. return -ENODATA;
  6150. }
  6151. }
  6152. /*******************************************************************************
  6153. * FUNCTION: _pip2_generate_hw_version
  6154. *
  6155. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6156. * VERSION command) to the hw_version.
  6157. *
  6158. * RETURN:
  6159. * 0 = success
  6160. * !0 = failure
  6161. *
  6162. * PARAMETERS:
  6163. * *cd - pointer to core data
  6164. * *hw_version - pointer to store the hardware version
  6165. ******************************************************************************/
  6166. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6167. {
  6168. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6169. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6170. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6171. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6172. return 0;
  6173. } else {
  6174. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6175. pt_debug(cd->dev, DL_ERROR,
  6176. "%s: PIP Version are not ready\n", __func__);
  6177. return -ENODATA;
  6178. }
  6179. }
  6180. /*******************************************************************************
  6181. * FUNCTION: pt_generate_hw_version
  6182. *
  6183. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6184. * hw_version from static data.
  6185. *
  6186. * RETURN:
  6187. * 0 = success
  6188. * !0 = failure
  6189. *
  6190. * PARAMETERS:
  6191. * *cd - pointer to core data
  6192. * *hw_version - pointer to store the hardware version
  6193. ******************************************************************************/
  6194. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6195. {
  6196. int rc = 0;
  6197. if (!hw_version)
  6198. return -ENOMEM;
  6199. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6200. rc = _legacy_generate_hw_version(cd, hw_version);
  6201. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6202. rc = _pip2_generate_hw_version(cd, hw_version);
  6203. else {
  6204. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6205. rc = -ENODATA;
  6206. }
  6207. return rc;
  6208. }
  6209. /*******************************************************************************
  6210. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6211. *
  6212. * NOTE: The calling function must ensure to free *hw_version
  6213. *
  6214. * RETURN:
  6215. * 0 = success
  6216. * !0 = Failure
  6217. *
  6218. * PARAMETERS:
  6219. * *dev - pointer to device structure
  6220. * *hw_version - pointer to where the hw_version string will be stored
  6221. ******************************************************************************/
  6222. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6223. {
  6224. int rc = 0;
  6225. u16 actual_read_len;
  6226. u16 pip_ver;
  6227. u8 rd_buf[256];
  6228. u8 status;
  6229. u8 index = PIP2_RESP_STATUS_OFFSET;
  6230. u8 return_data[8];
  6231. u8 panel_id;
  6232. struct pt_core_data *cd = dev_get_drvdata(dev);
  6233. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6234. if (!hw_version)
  6235. return -ENOMEM;
  6236. if (!cd->hw_detected) {
  6237. /* No HW detected */
  6238. rc = -ENODEV;
  6239. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6240. __func__);
  6241. goto exit_error;
  6242. }
  6243. /* For Parade TC or TT parts */
  6244. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6245. rc = _pt_request_pip2_send_cmd(dev,
  6246. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6247. NULL, 0, rd_buf, &actual_read_len);
  6248. if (rc) {
  6249. pt_debug(dev, DL_ERROR,
  6250. "%s: Failed to send PIP2 VERSION cmd\n",
  6251. __func__);
  6252. goto exit_error;
  6253. }
  6254. status = rd_buf[index];
  6255. if (status == 0) {
  6256. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6257. /*
  6258. * BL PIP 2.02 and greater the version fields are
  6259. * swapped
  6260. */
  6261. if (pip_ver >= 0x0202) {
  6262. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6263. "%02X%02X.%02X%02X.FF",
  6264. rd_buf[index + 10], rd_buf[index + 9],
  6265. rd_buf[index + 8], rd_buf[index + 7]);
  6266. } else {
  6267. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6268. "%02X%02X.%02X%02X.FF",
  6269. rd_buf[index + 8], rd_buf[index + 7],
  6270. rd_buf[index + 10], rd_buf[index + 9]);
  6271. }
  6272. return STATUS_SUCCESS;
  6273. } else {
  6274. rc = status;
  6275. pt_debug(dev, DL_WARN,
  6276. "%s: PIP2 VERSION cmd response error\n",
  6277. __func__);
  6278. }
  6279. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6280. /*
  6281. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6282. * hard coded to FFFF
  6283. */
  6284. if (cd->mode == PT_MODE_OPERATIONAL) {
  6285. rc = pt_hid_output_get_sysinfo(cd);
  6286. if (!rc) {
  6287. panel_id =
  6288. cd->sysinfo.sensing_conf_data.panel_id;
  6289. } else {
  6290. panel_id = PANEL_ID_NOT_ENABLED;
  6291. }
  6292. /* In FW - simply retrieve from ttdata struct */
  6293. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6294. "%04X.FFFF.%02X",
  6295. ttdata->jtag_id_h,
  6296. panel_id);
  6297. return STATUS_SUCCESS;
  6298. } else {
  6299. /*
  6300. * Return the stored value if PT_PANEL_ID_BY_BL
  6301. * is not supported while other feature is.
  6302. */
  6303. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6304. rc = pt_hid_output_bl_get_information(
  6305. cd, return_data);
  6306. if (!rc) {
  6307. rc = pt_hid_output_bl_get_panel_id(
  6308. cd, &panel_id);
  6309. }
  6310. } else
  6311. panel_id = cd->pid_for_loader;
  6312. if (!rc) {
  6313. snprintf(hw_version,
  6314. HW_VERSION_LEN_MAX,
  6315. "%02X%02X.FFFF.%02X",
  6316. return_data[3], return_data[2],
  6317. panel_id);
  6318. return STATUS_SUCCESS;
  6319. }
  6320. }
  6321. } else {
  6322. /* Unknown generation */
  6323. rc = -ENODEV;
  6324. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6325. __func__);
  6326. }
  6327. exit_error:
  6328. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6329. return rc;
  6330. }
  6331. /*******************************************************************************
  6332. * FUNCTION: pt_start_wd_timer
  6333. *
  6334. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6335. *
  6336. * RETURN: void
  6337. *
  6338. * PARAMETERS:
  6339. * *cd - pointer to core data
  6340. ******************************************************************************/
  6341. static void pt_start_wd_timer(struct pt_core_data *cd)
  6342. {
  6343. if (cd->watchdog_interval < 100) {
  6344. pt_debug(cd->dev, DL_ERROR,
  6345. "%s: WARNING: Invalid watchdog interval: %d\n",
  6346. __func__, cd->watchdog_interval);
  6347. return;
  6348. }
  6349. if (cd->watchdog_force_stop) {
  6350. pt_debug(cd->dev, DL_INFO,
  6351. "%s: TTDL WD Forced stop\n", __func__);
  6352. return;
  6353. }
  6354. mod_timer(&cd->watchdog_timer, jiffies +
  6355. msecs_to_jiffies(cd->watchdog_interval));
  6356. cd->watchdog_enabled = 1;
  6357. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6358. }
  6359. /*******************************************************************************
  6360. * FUNCTION: pt_stop_wd_timer
  6361. *
  6362. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6363. *
  6364. * RETURN: void
  6365. *
  6366. * PARAMETERS:
  6367. * *cd - pointer to core data
  6368. ******************************************************************************/
  6369. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6370. {
  6371. if (!cd->watchdog_interval)
  6372. return;
  6373. /*
  6374. * Ensure we wait until the watchdog timer
  6375. * running on a different CPU finishes
  6376. */
  6377. del_timer_sync(&cd->watchdog_timer);
  6378. cancel_work_sync(&cd->watchdog_work);
  6379. del_timer_sync(&cd->watchdog_timer);
  6380. cd->watchdog_enabled = 0;
  6381. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6382. }
  6383. /*******************************************************************************
  6384. * FUNCTION: pt_hw_soft_reset
  6385. *
  6386. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6387. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6388. * schedule an enum if it fires when the DUT is being reset.
  6389. * This can cause a double reset.
  6390. *
  6391. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6392. * the WD active could cause this function to fail!
  6393. *
  6394. * RETURN:
  6395. * 0 = success
  6396. * !0 = failure
  6397. *
  6398. * PARAMETERS:
  6399. * *cd - pointer to core data struct
  6400. * protect - flag to call protected or non-protected
  6401. ******************************************************************************/
  6402. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6403. {
  6404. int rc = 0;
  6405. mutex_lock(&cd->system_lock);
  6406. cd->startup_status = STARTUP_STATUS_START;
  6407. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6408. mutex_unlock(&cd->system_lock);
  6409. if (protect)
  6410. rc = pt_hid_cmd_reset(cd);
  6411. else
  6412. rc = pt_hid_cmd_reset_(cd);
  6413. if (rc < 0) {
  6414. pt_debug(cd->dev, DL_ERROR,
  6415. "%s: FAILED to execute SOFT reset\n", __func__);
  6416. return rc;
  6417. }
  6418. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6419. __func__);
  6420. return 0;
  6421. }
  6422. /*******************************************************************************
  6423. * FUNCTION: pt_hw_hard_reset
  6424. *
  6425. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6426. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6427. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6428. * schedule an enum if it fires when the DUT is being reset.
  6429. * This can cause a double reset.
  6430. *
  6431. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6432. * the WD active could cause this function to fail!
  6433. *
  6434. * RETURN:
  6435. * 0 = success
  6436. * !0 = failure
  6437. *
  6438. * PARAMETERS:
  6439. * *cd - pointer to core data struct
  6440. ******************************************************************************/
  6441. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6442. {
  6443. if (cd->cpdata->xres) {
  6444. cd->startup_status = STARTUP_STATUS_START;
  6445. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6446. __func__);
  6447. cd->cpdata->xres(cd->cpdata, cd->dev);
  6448. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6449. __func__);
  6450. return 0;
  6451. }
  6452. pt_debug(cd->dev, DL_ERROR,
  6453. "%s: FAILED to execute HARD reset\n", __func__);
  6454. return -ENODEV;
  6455. }
  6456. /*******************************************************************************
  6457. * FUNCTION: pt_dut_reset
  6458. *
  6459. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6460. * soft reset.
  6461. *
  6462. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6463. * the WD active could cause this function to fail!
  6464. * NOTE: "protect" flag is only used for soft reset.
  6465. *
  6466. * RETURN:
  6467. * 0 = success
  6468. * !0 = failure
  6469. *
  6470. * PARAMETERS:
  6471. * *cd - pointer to core data structure
  6472. * protect - flag to call protected or non-protected
  6473. ******************************************************************************/
  6474. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6475. {
  6476. int rc = 0;
  6477. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6478. mutex_lock(&cd->system_lock);
  6479. cd->hid_reset_cmd_state = 1;
  6480. rc = pt_hw_hard_reset(cd);
  6481. mutex_unlock(&cd->system_lock);
  6482. if (rc == -ENODEV) {
  6483. mutex_lock(&cd->system_lock);
  6484. cd->hid_reset_cmd_state = 0;
  6485. mutex_unlock(&cd->system_lock);
  6486. pt_debug(cd->dev, DL_ERROR,
  6487. "%s: Hard reset failed, try soft reset\n", __func__);
  6488. rc = pt_hw_soft_reset(cd, protect);
  6489. }
  6490. if (rc)
  6491. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6492. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6493. return rc;
  6494. }
  6495. /*******************************************************************************
  6496. * FUNCTION: pt_dut_reset_and_wait
  6497. *
  6498. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6499. * complete
  6500. *
  6501. * RETURN:
  6502. * 0 = success
  6503. * !0 = failure
  6504. *
  6505. * PARAMETERS:
  6506. * *cd - pointer to core data structure
  6507. ******************************************************************************/
  6508. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6509. {
  6510. int rc = 0;
  6511. int t;
  6512. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6513. if (rc < 0)
  6514. goto exit;
  6515. t = wait_event_timeout(cd->wait_q,
  6516. (cd->hid_reset_cmd_state == 0),
  6517. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6518. if (IS_TMO(t)) {
  6519. #ifdef TTDL_DIAGNOSTICS
  6520. cd->bus_transmit_error_count++;
  6521. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6522. #endif /* TTDL_DIAGNOSTICS */
  6523. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6524. __func__);
  6525. rc = -ETIME;
  6526. goto exit;
  6527. }
  6528. exit:
  6529. return rc;
  6530. }
  6531. /*
  6532. * touch default parameters (from report descriptor) to resolve protocol for
  6533. * touch report
  6534. */
  6535. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6536. /* byte offset, size, min, max, bit offset, report */
  6537. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6538. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6539. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6540. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6541. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6542. };
  6543. /*
  6544. * button default parameters (from report descriptor) to resolve protocol for
  6545. * button report
  6546. */
  6547. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6548. /* byte offset, size, min, max, bit offset, report */
  6549. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6550. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6551. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6552. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6553. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6554. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6555. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6556. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6557. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6558. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6559. };
  6560. /*******************************************************************************
  6561. * FUNCTION: pt_init_pip_report_fields
  6562. *
  6563. * SUMMARY: Setup default values for touch/button report parsing.
  6564. *
  6565. * PARAMETERS:
  6566. * *cd - pointer to core data structure
  6567. ******************************************************************************/
  6568. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6569. {
  6570. struct pt_sysinfo *si = &cd->sysinfo;
  6571. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6572. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6573. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6574. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6575. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6576. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6577. cd->features.easywake = 1;
  6578. cd->features.noise_metric = 1;
  6579. cd->features.tracking_heatmap = 1;
  6580. cd->features.sensor_data = 1;
  6581. }
  6582. /*******************************************************************************
  6583. * FUNCTION: pt_get_mode
  6584. *
  6585. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6586. * message
  6587. *
  6588. * RETURN: Enum of the current mode
  6589. *
  6590. * PARAMETERS:
  6591. * *cd - pointer to the Core Data structure
  6592. * protect - run command in protected mode
  6593. * *mode - pointer to store the retrieved mode
  6594. ******************************************************************************/
  6595. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6596. {
  6597. if (desc->packet_id == HID_APP_REPORT_ID)
  6598. return PT_MODE_OPERATIONAL;
  6599. else if (desc->packet_id == HID_BL_REPORT_ID)
  6600. return PT_MODE_BOOTLOADER;
  6601. return PT_MODE_UNKNOWN;
  6602. }
  6603. /*******************************************************************************
  6604. * FUNCTION: _pt_request_get_mode
  6605. *
  6606. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6607. * to determine the current mode of the DUT by use of the Get HID
  6608. * Descriptor command.
  6609. *
  6610. * RETURN:
  6611. * 0 = success
  6612. * !0 = failure
  6613. *
  6614. * PARAMETERS:
  6615. * *dev - pointer to device structure
  6616. * protect - run command in protected mode
  6617. * *mode - pointer to store the retrieved mode
  6618. ******************************************************************************/
  6619. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6620. {
  6621. struct pt_core_data *cd = dev_get_drvdata(dev);
  6622. struct pt_hid_desc hid_desc;
  6623. int rc = 0;
  6624. memset(&hid_desc, 0, sizeof(hid_desc));
  6625. if (protect)
  6626. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6627. else
  6628. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6629. if (rc)
  6630. *mode = PT_MODE_UNKNOWN;
  6631. else
  6632. *mode = pt_get_mode(cd, &hid_desc);
  6633. return rc;
  6634. }
  6635. /*******************************************************************************
  6636. * FUNCTION: pt_queue_enum_
  6637. *
  6638. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6639. * function. It won't try to add/delete sysfs node or modules.
  6640. *
  6641. * RETURN: void
  6642. *
  6643. * PARAMETERS:
  6644. * *cd - pointer to core data
  6645. ******************************************************************************/
  6646. static void pt_queue_enum_(struct pt_core_data *cd)
  6647. {
  6648. if (cd->startup_state == STARTUP_NONE) {
  6649. cd->startup_state = STARTUP_QUEUED;
  6650. #ifdef TTDL_DIAGNOSTICS
  6651. if (!cd->bridge_mode)
  6652. schedule_work(&cd->enum_work);
  6653. else
  6654. cd->startup_state = STARTUP_NONE;
  6655. #else
  6656. schedule_work(&cd->enum_work);
  6657. #endif
  6658. pt_debug(cd->dev, DL_INFO,
  6659. "%s: enum_work queued\n", __func__);
  6660. } else {
  6661. pt_debug(cd->dev, DL_WARN,
  6662. "%s: Enum not queued - startup_state = %d\n",
  6663. __func__, cd->startup_state);
  6664. }
  6665. }
  6666. /*******************************************************************************
  6667. * FUNCTION: pt_queue_enum
  6668. *
  6669. * SUMMARY: Queues a TTDL enum within a mutex lock
  6670. *
  6671. * RETURN: void
  6672. *
  6673. * PARAMETERS:
  6674. * *cd - pointer to core data
  6675. ******************************************************************************/
  6676. static void pt_queue_enum(struct pt_core_data *cd)
  6677. {
  6678. mutex_lock(&cd->system_lock);
  6679. pt_queue_enum_(cd);
  6680. mutex_unlock(&cd->system_lock);
  6681. }
  6682. static void remove_sysfs_and_modules(struct device *dev);
  6683. /*******************************************************************************
  6684. * FUNCTION: pt_queue_restart
  6685. *
  6686. * SUMMARY: Queues a TTDL restart within a mutex lock
  6687. *
  6688. * RETURN: void
  6689. *
  6690. * PARAMETERS:
  6691. * *cd - pointer to core data
  6692. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6693. * False: will not perform remove action
  6694. ******************************************************************************/
  6695. static void pt_queue_restart(struct pt_core_data *cd)
  6696. {
  6697. mutex_lock(&cd->system_lock);
  6698. if (cd->startup_state == STARTUP_NONE) {
  6699. cd->startup_state = STARTUP_QUEUED;
  6700. schedule_work(&cd->ttdl_restart_work);
  6701. pt_debug(cd->dev, DL_INFO,
  6702. "%s: pt_ttdl_restart queued\n", __func__);
  6703. } else {
  6704. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6705. __func__, cd->startup_state);
  6706. }
  6707. mutex_unlock(&cd->system_lock);
  6708. }
  6709. /*******************************************************************************
  6710. * FUNCTION: call_atten_cb
  6711. *
  6712. * SUMMARY: Iterate over attention list call the function that registered.
  6713. *
  6714. * RETURN: void
  6715. *
  6716. * PARAMETERS:
  6717. * *cd - pointer to core data
  6718. * type - type of attention list
  6719. * mode - condition for execution
  6720. ******************************************************************************/
  6721. static void call_atten_cb(struct pt_core_data *cd,
  6722. enum pt_atten_type type, int mode)
  6723. {
  6724. struct atten_node *atten, *atten_n;
  6725. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6726. __func__, type, mode);
  6727. spin_lock(&cd->spinlock);
  6728. list_for_each_entry_safe(atten, atten_n,
  6729. &cd->atten_list[type], node) {
  6730. if (!mode || atten->mode & mode) {
  6731. spin_unlock(&cd->spinlock);
  6732. pt_debug(cd->dev, DL_DEBUG,
  6733. "%s: attention for '%s'",
  6734. __func__, dev_name(atten->dev));
  6735. atten->func(atten->dev);
  6736. spin_lock(&cd->spinlock);
  6737. }
  6738. }
  6739. spin_unlock(&cd->spinlock);
  6740. }
  6741. /*******************************************************************************
  6742. * FUNCTION: start_fw_upgrade
  6743. *
  6744. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6745. * start firmware upgrade.
  6746. *
  6747. * RETURN:
  6748. * 0 = success
  6749. *
  6750. * PARAMETERS:
  6751. * *data - pointer to core data
  6752. ******************************************************************************/
  6753. static int start_fw_upgrade(void *data)
  6754. {
  6755. struct pt_core_data *cd = (struct pt_core_data *)data;
  6756. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6757. return 0;
  6758. }
  6759. /*******************************************************************************
  6760. * FUNCTION: pt_put_device_into_easy_wakeup_
  6761. *
  6762. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6763. * wake up state.
  6764. *
  6765. * RETURN:
  6766. * 0 = success
  6767. * !0 = failure
  6768. *
  6769. * PARAMETERS:
  6770. * *cd - pointer to core data
  6771. ******************************************************************************/
  6772. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6773. {
  6774. int rc = 0;
  6775. u8 status = 0;
  6776. mutex_lock(&cd->system_lock);
  6777. cd->wait_until_wake = 0;
  6778. mutex_unlock(&cd->system_lock);
  6779. rc = pt_hid_output_enter_easywake_state_(cd,
  6780. cd->easy_wakeup_gesture, &status);
  6781. if (rc || status == 0)
  6782. return -EBUSY;
  6783. return rc;
  6784. }
  6785. /*******************************************************************************
  6786. * FUNCTION: pt_put_device_into_deep_sleep_
  6787. *
  6788. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6789. *
  6790. * RETURN:
  6791. * 0 = success
  6792. * !0 = error
  6793. *
  6794. * PARAMETERS:
  6795. * *cd - pointer to core data
  6796. ******************************************************************************/
  6797. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6798. {
  6799. int rc = 0;
  6800. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6801. if (rc)
  6802. rc = -EBUSY;
  6803. return rc;
  6804. }
  6805. /*******************************************************************************
  6806. * FUNCTION: pt_put_device_into_deep_standby_
  6807. *
  6808. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6809. *
  6810. * RETURN:
  6811. * 0 = success
  6812. * !0 = error
  6813. *
  6814. * PARAMETERS:
  6815. * *cd - pointer to core data
  6816. ******************************************************************************/
  6817. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6818. {
  6819. int rc = 0;
  6820. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6821. if (rc)
  6822. rc = -EBUSY;
  6823. return rc;
  6824. }
  6825. /*******************************************************************************
  6826. * FUNCTION: pt_core_poweroff_device_
  6827. *
  6828. * SUMMARY: Disable IRQ and HW power down the device.
  6829. *
  6830. * RETURN:
  6831. * 0 = success
  6832. * !0 = failure
  6833. *
  6834. * PARAMETERS:
  6835. * *cd - pointer to core data
  6836. ******************************************************************************/
  6837. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6838. {
  6839. int rc;
  6840. if (cd->irq_enabled) {
  6841. cd->irq_enabled = false;
  6842. disable_irq_nosync(cd->irq);
  6843. }
  6844. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6845. if (rc < 0)
  6846. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6847. __func__, rc);
  6848. return rc;
  6849. }
  6850. /*******************************************************************************
  6851. * FUNCTION: pt_core_sleep_
  6852. *
  6853. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6854. * configuration in the core platform data structure.
  6855. *
  6856. * RETURN:
  6857. * 0 = success
  6858. * !0 = failure
  6859. *
  6860. * PARAMETERS:
  6861. * *cd - pointer to core data
  6862. ******************************************************************************/
  6863. static int pt_core_sleep_(struct pt_core_data *cd)
  6864. {
  6865. int rc = 0;
  6866. mutex_lock(&cd->system_lock);
  6867. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6868. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_SLEEP_NONE) {
  6869. cd->sleep_state = SS_SLEEPING;
  6870. } else {
  6871. mutex_unlock(&cd->system_lock);
  6872. pt_debug(cd->dev, DL_INFO,
  6873. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6874. return 0;
  6875. }
  6876. mutex_unlock(&cd->system_lock);
  6877. /* Ensure watchdog and startup works stopped */
  6878. pt_stop_wd_timer(cd);
  6879. cancel_work_sync(&cd->enum_work);
  6880. pt_stop_wd_timer(cd);
  6881. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6882. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6883. rc = pt_core_poweroff_device_(cd);
  6884. if (rc)
  6885. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6886. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6887. pt_debug(cd->dev, DL_INFO,
  6888. "%s: Entering into deep standby mode:\n", __func__);
  6889. rc = pt_put_device_into_deep_standby_(cd);
  6890. if (rc)
  6891. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6892. } else {
  6893. pt_debug(cd->dev, DL_INFO,
  6894. "%s: Entering into deep sleep mode:\n", __func__);
  6895. rc = pt_put_device_into_deep_sleep_(cd);
  6896. if (rc)
  6897. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6898. }
  6899. mutex_lock(&cd->system_lock);
  6900. cd->sleep_state = SS_SLEEP_ON;
  6901. mutex_unlock(&cd->system_lock);
  6902. return rc;
  6903. }
  6904. /*******************************************************************************
  6905. * FUNCTION: pt_core_easywake_on_
  6906. *
  6907. * SUMMARY: Suspend the device with easy wake on the
  6908. * configuration in the core platform data structure.
  6909. *
  6910. * RETURN:
  6911. * 0 = success
  6912. * !0 = failure
  6913. *
  6914. * PARAMETERS:
  6915. * *cd - pointer to core data
  6916. ******************************************************************************/
  6917. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6918. {
  6919. int rc = 0;
  6920. mutex_lock(&cd->system_lock);
  6921. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6922. mutex_unlock(&cd->system_lock);
  6923. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6924. __func__, cd->sleep_state);
  6925. return 0;
  6926. }
  6927. mutex_unlock(&cd->system_lock);
  6928. /* Ensure watchdog and startup works stopped */
  6929. pt_stop_wd_timer(cd);
  6930. cancel_work_sync(&cd->enum_work);
  6931. pt_stop_wd_timer(cd);
  6932. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6933. rc = pt_put_device_into_easy_wakeup_(cd);
  6934. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6935. if (rc)
  6936. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6937. }
  6938. mutex_lock(&cd->system_lock);
  6939. cd->sleep_state = SS_EASY_WAKING_ON;
  6940. mutex_unlock(&cd->system_lock);
  6941. return rc;
  6942. }
  6943. /*******************************************************************************
  6944. * FUNCTION: pt_core_easywake_on
  6945. *
  6946. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6947. *
  6948. * RETURN:
  6949. * 0 = success
  6950. * !0 = failure
  6951. *
  6952. * PARAMETERS:
  6953. * *cd - pointer to core data
  6954. ******************************************************************************/
  6955. static int pt_core_easywake_on(struct pt_core_data *cd)
  6956. {
  6957. int rc = 0;
  6958. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6959. if (rc < 0) {
  6960. pt_debug(cd->dev, DL_ERROR,
  6961. "%s: fail get exclusive ex=%p own=%p\n",
  6962. __func__, cd->exclusive_dev, cd->dev);
  6963. return rc;
  6964. }
  6965. rc = pt_core_easywake_on_(cd);
  6966. if (release_exclusive(cd, cd->dev) < 0)
  6967. pt_debug(cd->dev, DL_ERROR,
  6968. "%s: fail to release exclusive\n", __func__);
  6969. else
  6970. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6971. __func__);
  6972. return rc;
  6973. }
  6974. /*******************************************************************************
  6975. * FUNCTION: pt_core_sleep
  6976. *
  6977. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6978. *
  6979. * RETURN:
  6980. * 0 = success
  6981. * !0 = failure
  6982. *
  6983. * PARAMETERS:
  6984. * *cd - pointer to core data
  6985. ******************************************************************************/
  6986. static int pt_core_sleep(struct pt_core_data *cd)
  6987. {
  6988. int rc = 0;
  6989. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6990. if (rc < 0) {
  6991. pt_debug(cd->dev, DL_ERROR,
  6992. "%s: fail get exclusive ex=%p own=%p\n",
  6993. __func__, cd->exclusive_dev, cd->dev);
  6994. return rc;
  6995. }
  6996. rc = pt_core_sleep_(cd);
  6997. if (release_exclusive(cd, cd->dev) < 0)
  6998. pt_debug(cd->dev, DL_ERROR,
  6999. "%s: fail to release exclusive\n", __func__);
  7000. else
  7001. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7002. __func__);
  7003. return rc;
  7004. }
  7005. /*******************************************************************************
  7006. * FUNCTION: pt_wakeup_host
  7007. *
  7008. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7009. *
  7010. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7011. *
  7012. * RETURN:
  7013. * 0 = success
  7014. * !0 = failure
  7015. *
  7016. * PARAMETERS:
  7017. * *cd - pointer to core data
  7018. ******************************************************************************/
  7019. static int pt_wakeup_host(struct pt_core_data *cd)
  7020. {
  7021. #ifndef EASYWAKE_TSG6
  7022. /* TSG5 EasyWake */
  7023. int rc = 0;
  7024. int event_id;
  7025. int size = get_unaligned_le16(&cd->input_buf[0]);
  7026. /* Validate report */
  7027. if (size != 4 || cd->input_buf[2] != 4)
  7028. rc = -EINVAL;
  7029. event_id = cd->input_buf[3];
  7030. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7031. __func__, event_id, rc);
  7032. if (rc) {
  7033. pt_core_sleep_(cd);
  7034. goto exit;
  7035. }
  7036. /* attention WAKE */
  7037. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7038. exit:
  7039. return rc;
  7040. #else
  7041. /* TSG6 FW1.3 EasyWake */
  7042. int rc = 0;
  7043. int i = 0;
  7044. int report_length;
  7045. /* Validate report */
  7046. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7047. rc = -EINVAL;
  7048. pt_core_sleep_(cd);
  7049. goto exit;
  7050. }
  7051. /* Get gesture id and gesture data length */
  7052. cd->gesture_id = cd->input_buf[3];
  7053. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7054. cd->gesture_data_length = report_length - 4;
  7055. pt_debug(cd->dev, DL_INFO,
  7056. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7057. __func__, cd->gesture_id, cd->gesture_data_length);
  7058. for (i = 0; i < cd->gesture_data_length; i++)
  7059. cd->gesture_data[i] = cd->input_buf[4 + i];
  7060. /* attention WAKE */
  7061. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7062. exit:
  7063. return rc;
  7064. #endif
  7065. }
  7066. /*******************************************************************************
  7067. * FUNCTION: pt_get_touch_axis
  7068. *
  7069. * SUMMARY: Function to calculate touch axis
  7070. *
  7071. * PARAMETERS:
  7072. * *cd - pointer to core data structure
  7073. * *axis - pointer to axis calculation result
  7074. * size - size in bytes
  7075. * max - max value of result
  7076. * *xy_data - pointer to input data to be parsed
  7077. * bofs - bit offset
  7078. ******************************************************************************/
  7079. static void pt_get_touch_axis(struct pt_core_data *cd,
  7080. int *axis, int size, int max, u8 *data, int bofs)
  7081. {
  7082. int nbyte;
  7083. int next;
  7084. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7085. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7086. next++;
  7087. }
  7088. *axis &= max - 1;
  7089. }
  7090. /*******************************************************************************
  7091. * FUNCTION: move_tracking_heatmap_data
  7092. *
  7093. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7094. * system information structure, xy_mode and xy_data.
  7095. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7096. * the tthe_tuner sysfs node under the label "THM"
  7097. *
  7098. * RETURN:
  7099. * 0 = success
  7100. * !0 = failure
  7101. *
  7102. * PARAMETERS:
  7103. * *cd - pointer to core data
  7104. * *si - pointer to the system information structure
  7105. ******************************************************************************/
  7106. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7107. struct pt_sysinfo *si)
  7108. {
  7109. #ifdef TTHE_TUNER_SUPPORT
  7110. int size = get_unaligned_le16(&cd->input_buf[0]);
  7111. if (size)
  7112. tthe_print(cd, cd->input_buf, size, "THM=");
  7113. #endif
  7114. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7115. return 0;
  7116. }
  7117. /*******************************************************************************
  7118. * FUNCTION: move_sensor_data
  7119. *
  7120. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7121. * information structure, xy_mode and xy_data.
  7122. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7123. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7124. *
  7125. * RETURN:
  7126. * 0 = success
  7127. * !0 = failure
  7128. *
  7129. * PARAMETERS:
  7130. * *cd - pointer to core data
  7131. * *si - pointer to the system information structure
  7132. ******************************************************************************/
  7133. static int move_sensor_data(struct pt_core_data *cd,
  7134. struct pt_sysinfo *si)
  7135. {
  7136. #ifdef TTHE_TUNER_SUPPORT
  7137. int size = get_unaligned_le16(&cd->input_buf[0]);
  7138. if (size)
  7139. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7140. #endif
  7141. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7142. return 0;
  7143. }
  7144. /*******************************************************************************
  7145. * FUNCTION: move_button_data
  7146. *
  7147. * SUMMARY: Move the valid button data from the input buffer into the system
  7148. * information structure, xy_mode and xy_data.
  7149. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7150. * the tthe_tuner sysfs node under the label "OpModeData"
  7151. *
  7152. * RETURN:
  7153. * 0 = success
  7154. * !0 = failure
  7155. *
  7156. * PARAMETERS:
  7157. * *cd - pointer to core data
  7158. * *si - pointer to the system information structure
  7159. ******************************************************************************/
  7160. static int move_button_data(struct pt_core_data *cd,
  7161. struct pt_sysinfo *si)
  7162. {
  7163. #ifdef TTHE_TUNER_SUPPORT
  7164. int size = get_unaligned_le16(&cd->input_buf[0]);
  7165. if (size)
  7166. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7167. #endif
  7168. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7169. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7170. "xy_mode");
  7171. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7172. BTN_REPORT_SIZE);
  7173. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7174. "xy_data");
  7175. return 0;
  7176. }
  7177. /*******************************************************************************
  7178. * FUNCTION: move_touch_data
  7179. *
  7180. * SUMMARY: Move the valid touch data from the input buffer into the system
  7181. * information structure, xy_mode and xy_data.
  7182. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7183. * the tthe_tuner sysfs node under the label "OpModeData"
  7184. *
  7185. * RETURN:
  7186. * 0 = success
  7187. * !0 = failure
  7188. *
  7189. * PARAMETERS:
  7190. * *cd - pointer to core data
  7191. * *si - pointer to the system information structure
  7192. ******************************************************************************/
  7193. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7194. {
  7195. int max_tch = si->sensing_conf_data.max_tch;
  7196. int num_cur_tch;
  7197. int length;
  7198. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7199. #ifdef TTHE_TUNER_SUPPORT
  7200. int size = get_unaligned_le16(&cd->input_buf[0]);
  7201. if (size)
  7202. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7203. #endif
  7204. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7205. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7206. si->desc.tch_header_size, "xy_mode");
  7207. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7208. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7209. if (unlikely(num_cur_tch > max_tch))
  7210. num_cur_tch = max_tch;
  7211. length = num_cur_tch * si->desc.tch_record_size;
  7212. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7213. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7214. return 0;
  7215. }
  7216. /*******************************************************************************
  7217. * FUNCTION: move_hid_pen_data
  7218. *
  7219. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7220. * information structure, xy_mode and xy_data.
  7221. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7222. * the tthe_tuner sysfs node under the label "HID" starting with the
  7223. * report ID.
  7224. *
  7225. * RETURN:
  7226. * 0 = success
  7227. * !0 = failure
  7228. *
  7229. * PARAMETERS:
  7230. * *cd - pointer to core data
  7231. * *si - pointer to the system information structure
  7232. ******************************************************************************/
  7233. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7234. {
  7235. #ifdef TTHE_TUNER_SUPPORT
  7236. int size = get_unaligned_le16(&cd->input_buf[0]);
  7237. if (size) {
  7238. /*
  7239. * HID over USB does not require the two byte length field, so
  7240. * this should print from input_buf[2] but to keep both finger
  7241. * and pen reports the same the length is included
  7242. */
  7243. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7244. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7245. "HID-USB=");
  7246. else
  7247. tthe_print(cd, &(cd->input_buf[0]), size,
  7248. "HID-I2C=");
  7249. }
  7250. #endif
  7251. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7252. return 0;
  7253. }
  7254. /*******************************************************************************
  7255. * FUNCTION: parse_touch_input
  7256. *
  7257. * SUMMARY: Parse the touch report and take action based on the touch
  7258. * report_id.
  7259. *
  7260. * RETURN:
  7261. * 0 = success
  7262. * !0 = failure
  7263. *
  7264. * PARAMETERS:
  7265. * *cd - pointer to core data
  7266. * size - size of touch record
  7267. ******************************************************************************/
  7268. static int parse_touch_input(struct pt_core_data *cd, int size)
  7269. {
  7270. struct pt_sysinfo *si = &cd->sysinfo;
  7271. int report_id = cd->input_buf[2];
  7272. int rc = -EINVAL;
  7273. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7274. __func__);
  7275. if (!si->ready) {
  7276. pt_debug(cd->dev, DL_ERROR,
  7277. "%s: Need system information to parse touches\n",
  7278. __func__);
  7279. return 0;
  7280. }
  7281. if (!si->xy_mode || !si->xy_data)
  7282. return rc;
  7283. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7284. rc = move_touch_data(cd, si);
  7285. else if (report_id == PT_HID_PEN_REPORT_ID)
  7286. rc = move_hid_pen_data(cd, si);
  7287. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7288. rc = move_button_data(cd, si);
  7289. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7290. rc = move_sensor_data(cd, si);
  7291. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7292. rc = move_tracking_heatmap_data(cd, si);
  7293. if (rc)
  7294. return rc;
  7295. /* attention IRQ */
  7296. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7297. return 0;
  7298. }
  7299. /*******************************************************************************
  7300. * FUNCTION: parse_command_input
  7301. *
  7302. * SUMMARY: Move the response data from the input buffer to the response buffer
  7303. *
  7304. * RETURN:
  7305. * 0 = success
  7306. * !0 = failure
  7307. *
  7308. * PARAMETERS:
  7309. * *cd - pointer to core data
  7310. * size - size of response data
  7311. ******************************************************************************/
  7312. static int parse_command_input(struct pt_core_data *cd, int size)
  7313. {
  7314. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7315. __func__);
  7316. memcpy(cd->response_buf, cd->input_buf, size);
  7317. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7318. if (size && cd->show_tt_data) {
  7319. if (cd->pip2_prot_active)
  7320. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7321. else
  7322. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7323. }
  7324. #endif
  7325. mutex_lock(&cd->system_lock);
  7326. cd->hid_cmd_state = 0;
  7327. mutex_unlock(&cd->system_lock);
  7328. wake_up(&cd->wait_q);
  7329. return 0;
  7330. }
  7331. /*******************************************************************************
  7332. * FUNCTION: pt_allow_enumeration
  7333. *
  7334. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7335. * FW sentinel is seen.
  7336. *
  7337. * RETURN:
  7338. * true = allow enumeration or fully re-probe
  7339. * false = skip enumeration and fully re-probe
  7340. *
  7341. * PARAMETERS:
  7342. * *cd - pointer to core data
  7343. ******************************************************************************/
  7344. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7345. {
  7346. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7347. (!cd->hid_reset_cmd_state) &&
  7348. (cd->core_probe_complete) &&
  7349. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7350. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7351. (cd->mode == PT_MODE_OPERATIONAL)) {
  7352. return true;
  7353. }
  7354. if ((!cd->hid_reset_cmd_state) &&
  7355. (cd->core_probe_complete) &&
  7356. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7357. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7358. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7359. return true;
  7360. }
  7361. pt_debug(cd->dev, DL_INFO,
  7362. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7363. __func__,
  7364. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7365. "core_probe_complete(1)", cd->core_probe_complete,
  7366. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7367. "active_dut_gen(0,2)", cd->active_dut_generation);
  7368. return false;
  7369. }
  7370. /*******************************************************************************
  7371. * FUNCTION: pt_is_touch_report
  7372. *
  7373. * SUMMARY: Determine if a report ID should be treated as a touch report
  7374. *
  7375. * RETURN:
  7376. * true = report ID is a touch report
  7377. * false = report ID is not a touch report
  7378. *
  7379. * PARAMETERS:
  7380. * id - Report ID
  7381. ******************************************************************************/
  7382. static bool pt_is_touch_report(int id)
  7383. {
  7384. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7385. id == PT_HID_PEN_REPORT_ID ||
  7386. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7387. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7388. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7389. return true;
  7390. else
  7391. return false;
  7392. }
  7393. /*******************************************************************************
  7394. * FUNCTION: pt_parse_input
  7395. *
  7396. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7397. * on if its a response to a command or asynchronous touch data / reset
  7398. * sentinel. PIP2.x messages have additional error checking that is
  7399. * parsed (SEQ match from cmd to rsp, CRC valid).
  7400. * Look for special packets based on unique lengths:
  7401. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7402. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7403. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7404. * 0xFFXX - Empty buffer (PIP 1.7+)
  7405. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7406. * the initial probe has not yet been done.
  7407. *
  7408. * RETURN:
  7409. * 0 = success
  7410. * !0 = failure
  7411. *
  7412. * PARAMETERS:
  7413. * *cd - pointer to core data
  7414. ******************************************************************************/
  7415. static int pt_parse_input(struct pt_core_data *cd)
  7416. {
  7417. int report_id;
  7418. int cmd_id;
  7419. int is_command = 0;
  7420. int size;
  7421. int print_size;
  7422. bool touch_report = true;
  7423. unsigned short calc_crc;
  7424. unsigned short resp_crc;
  7425. cd->fw_sys_mode_in_standby_state = false;
  7426. size = get_unaligned_le16(&cd->input_buf[0]);
  7427. print_size = size;
  7428. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7429. __func__, print_size);
  7430. if (print_size <= PT_MAX_INPUT)
  7431. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7432. "<<< Read buf");
  7433. if (size == 0 ||
  7434. (size == 11 &&
  7435. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7436. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7437. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7438. PIP2_CMD_COMMAND_ID_MASK) ==
  7439. PIP2_CMD_ID_STATUS)) {
  7440. touch_report = false;
  7441. cd->hw_detected = true;
  7442. cd->bl_pip_ver_ready = false;
  7443. cd->app_pip_ver_ready = false;
  7444. if (size == 0) {
  7445. mutex_lock(&cd->system_lock);
  7446. cd->pip2_prot_active = false;
  7447. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7448. /*
  7449. * For Gen5/6 this sentinel could be from
  7450. * the BL or FW. Attempt to set the correct
  7451. * mode based on the previous PIP command.
  7452. */
  7453. if (cd->hid_cmd_state ==
  7454. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7455. cd->mode = PT_MODE_OPERATIONAL;
  7456. cd->startup_status =
  7457. STARTUP_STATUS_FW_RESET_SENTINEL;
  7458. } else if (cd->hid_cmd_state ==
  7459. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7460. cd->hid_reset_cmd_state) {
  7461. cd->mode = PT_MODE_BOOTLOADER;
  7462. cd->startup_status =
  7463. STARTUP_STATUS_BL_RESET_SENTINEL;
  7464. } else {
  7465. cd->mode = PT_MODE_UNKNOWN;
  7466. cd->startup_status =
  7467. STARTUP_STATUS_START;
  7468. }
  7469. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7470. pt_debug(cd->dev, DL_INFO,
  7471. "%s: ATM Gen5/6 %s sentinel received\n",
  7472. __func__,
  7473. (cd->mode == PT_MODE_OPERATIONAL ?
  7474. "FW" :
  7475. (cd->mode == PT_MODE_BOOTLOADER ?
  7476. "BL" : "Unknown")));
  7477. } else {
  7478. cd->mode = PT_MODE_OPERATIONAL;
  7479. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7480. cd->startup_status =
  7481. STARTUP_STATUS_FW_RESET_SENTINEL;
  7482. pt_debug(cd->dev, DL_INFO,
  7483. "%s: ATM PT/TT FW sentinel received\n",
  7484. __func__);
  7485. }
  7486. mutex_unlock(&cd->system_lock);
  7487. if (pt_allow_enumeration(cd)) {
  7488. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7489. pt_debug(cd->dev, DL_INFO,
  7490. "%s: Queue Restart\n", __func__);
  7491. pt_queue_restart(cd);
  7492. } else {
  7493. pt_debug(cd->dev, DL_INFO,
  7494. "%s: Queue Enum\n", __func__);
  7495. pt_queue_enum(cd);
  7496. }
  7497. } else {
  7498. pt_debug(cd->dev, DL_INFO,
  7499. "%s: Sentinel - No Queued Action\n",
  7500. __func__);
  7501. }
  7502. } else {
  7503. /* Sentinel must be from TT/TC BL */
  7504. mutex_lock(&cd->system_lock);
  7505. cd->pip2_prot_active = true;
  7506. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7507. cd->mode = PT_MODE_BOOTLOADER;
  7508. cd->sysinfo.ready = false;
  7509. mutex_unlock(&cd->system_lock);
  7510. pt_debug(cd->dev, DL_INFO,
  7511. "%s: BL Reset sentinel received\n", __func__);
  7512. if (cd->flashless_dut &&
  7513. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7514. pt_debug(cd->dev, DL_INFO,
  7515. "%s: BL to RAM for flashless DUT\n",
  7516. __func__);
  7517. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7518. }
  7519. }
  7520. mutex_lock(&cd->system_lock);
  7521. memcpy(cd->response_buf, cd->input_buf, 2);
  7522. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7523. mutex_unlock(&cd->system_lock);
  7524. pt_debug(cd->dev, DL_WARN,
  7525. "%s: Device Initiated Reset\n", __func__);
  7526. wake_up(&cd->wait_q);
  7527. return 0;
  7528. }
  7529. cd->hid_reset_cmd_state = 0;
  7530. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7531. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7532. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7533. cd->hid_cmd_state = 0;
  7534. wake_up(&cd->wait_q);
  7535. mutex_unlock(&cd->system_lock);
  7536. return 0;
  7537. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7538. /*
  7539. * This debug message below is used by PBATS to calculate the
  7540. * time from the last lift off IRQ to when FW exits LFT mode.
  7541. */
  7542. touch_report = false;
  7543. pt_debug(cd->dev, DL_WARN,
  7544. "%s: DUT - Empty buffer detected\n", __func__);
  7545. return 0;
  7546. } else if (size > PT_MAX_INPUT) {
  7547. pt_debug(cd->dev, DL_ERROR,
  7548. "%s: DUT - Unexpected len field in active bus data!\n",
  7549. __func__);
  7550. return -EINVAL;
  7551. }
  7552. if (cd->pip2_prot_active) {
  7553. pt_debug(cd->dev, DL_DEBUG,
  7554. "%s: Decode PIP2.x Response\n", __func__);
  7555. /* PIP2 does not have a report id, hard code it */
  7556. report_id = 0x00;
  7557. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7558. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7559. resp_crc = cd->input_buf[size - 2] << 8;
  7560. resp_crc |= cd->input_buf[size - 1];
  7561. if ((cd->pip2_cmd_tag_seq !=
  7562. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7563. (resp_crc != calc_crc) &&
  7564. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7565. == PT_PIP_TOUCH_REPORT_ID) ||
  7566. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7567. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7568. pt_debug(cd->dev, DL_WARN,
  7569. "%s: %s %d %s\n",
  7570. __func__,
  7571. "Received PIP1 report id =",
  7572. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7573. "when expecting a PIP2 report - IGNORE report");
  7574. return 0;
  7575. }
  7576. is_command = 1;
  7577. touch_report = false;
  7578. } else {
  7579. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7580. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7581. }
  7582. #ifdef TTDL_DIAGNOSTICS
  7583. pt_debug(cd->dev, DL_DEBUG,
  7584. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7585. __func__, cd->pip2_prot_active, report_id,
  7586. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7587. #endif /* TTDL_DIAGNOSTICS */
  7588. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7589. pt_wakeup_host(cd);
  7590. #ifdef TTHE_TUNER_SUPPORT
  7591. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7592. #endif
  7593. return 0;
  7594. }
  7595. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7596. msecs_to_jiffies(cd->watchdog_interval));
  7597. /*
  7598. * If it is PIP2 response, the report_id has been set to 0,
  7599. * so it will not be parsed as a touch packet.
  7600. */
  7601. if (!pt_is_touch_report(report_id)) {
  7602. is_command = 1;
  7603. touch_report = false;
  7604. }
  7605. if (unlikely(is_command)) {
  7606. parse_command_input(cd, size);
  7607. return 0;
  7608. }
  7609. if (touch_report)
  7610. parse_touch_input(cd, size);
  7611. return 0;
  7612. }
  7613. /*******************************************************************************
  7614. * FUNCTION: pt_read_input
  7615. *
  7616. * SUMMARY: Reads incoming data off of the active bus
  7617. *
  7618. * RETURN:
  7619. * 0 = success
  7620. * !0 = failure
  7621. *
  7622. * PARAMETERS:
  7623. * *cd - pointer to core data
  7624. ******************************************************************************/
  7625. static int pt_read_input(struct pt_core_data *cd)
  7626. {
  7627. struct device *dev = cd->dev;
  7628. int rc = 0;
  7629. int t;
  7630. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7631. /*
  7632. * Workaround for easywake failure
  7633. * Interrupt for easywake, wait for bus controller to wake
  7634. */
  7635. mutex_lock(&cd->system_lock);
  7636. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7637. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7638. mutex_unlock(&cd->system_lock);
  7639. if (!dev->power.is_suspended)
  7640. goto read;
  7641. t = wait_event_timeout(cd->wait_q,
  7642. (cd->wait_until_wake == 1),
  7643. msecs_to_jiffies(2000));
  7644. #ifdef TTDL_DIAGNOSTICS
  7645. if (IS_TMO(t)) {
  7646. cd->bus_transmit_error_count++;
  7647. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7648. pt_debug(dev, DL_ERROR,
  7649. "%s: !!!I2C Transmission Error %d\n",
  7650. __func__,
  7651. cd->bus_transmit_error_count);
  7652. }
  7653. #else
  7654. if (IS_TMO(t))
  7655. pt_queue_enum(cd);
  7656. #endif /* TTDL_DIAGNOSTICS */
  7657. goto read;
  7658. }
  7659. }
  7660. mutex_unlock(&cd->system_lock);
  7661. read:
  7662. /* Try reading up to 'retry' times */
  7663. while (retry-- != 0) {
  7664. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7665. PT_MAX_INPUT);
  7666. if (!rc) {
  7667. pt_debug(dev, DL_DEBUG,
  7668. "%s: Read input successfully\n", __func__);
  7669. goto read_exit;
  7670. }
  7671. usleep_range(1000, 2000);
  7672. }
  7673. pt_debug(dev, DL_ERROR,
  7674. "%s: Error getting report, rc=%d\n", __func__, rc);
  7675. read_exit:
  7676. return rc;
  7677. }
  7678. /*******************************************************************************
  7679. * FUNCTION: pt_irq
  7680. *
  7681. * SUMMARY: Process all detected interrupts
  7682. *
  7683. * RETURN:
  7684. * IRQ_HANDLED - Finished processing the interrupt
  7685. *
  7686. * PARAMETERS:
  7687. * irq - IRQ number
  7688. * *handle - pointer to core data struct
  7689. ******************************************************************************/
  7690. irqreturn_t pt_irq(int irq, void *handle)
  7691. {
  7692. struct pt_core_data *cd = handle;
  7693. int rc = 0;
  7694. if (!pt_check_irq_asserted(cd))
  7695. return IRQ_HANDLED;
  7696. rc = pt_read_input(cd);
  7697. #ifdef TTDL_DIAGNOSTICS
  7698. cd->irq_count++;
  7699. /* Used to calculate T-Refresh */
  7700. if (cd->t_refresh_active) {
  7701. if (cd->t_refresh_count == 0) {
  7702. cd->t_refresh_time = jiffies;
  7703. cd->t_refresh_count++;
  7704. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7705. cd->t_refresh_count++;
  7706. } else {
  7707. cd->t_refresh_active = 0;
  7708. cd->t_refresh_time =
  7709. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7710. }
  7711. }
  7712. #endif /* TTDL_DIAGNOSTICS */
  7713. if (!rc)
  7714. pt_parse_input(cd);
  7715. return IRQ_HANDLED;
  7716. }
  7717. /*******************************************************************************
  7718. * FUNCTION: _pt_subscribe_attention
  7719. *
  7720. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7721. * to subscribe themselves into the TTDL attention list
  7722. *
  7723. * RETURN:
  7724. * 0 = success
  7725. * !0 = failure
  7726. *
  7727. * PARAMETERS:
  7728. * *dev - pointer to device structure
  7729. * type - attention type enum
  7730. * *id - ID of the module calling this function
  7731. * *func - callback function pointer to be called when notified
  7732. * mode - attention mode
  7733. ******************************************************************************/
  7734. int _pt_subscribe_attention(struct device *dev,
  7735. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7736. int mode)
  7737. {
  7738. struct pt_core_data *cd = dev_get_drvdata(dev);
  7739. struct atten_node *atten, *atten_new;
  7740. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7741. if (!atten_new)
  7742. return -ENOMEM;
  7743. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7744. dev_name(cd->dev));
  7745. spin_lock(&cd->spinlock);
  7746. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7747. if (atten->id == id && atten->mode == mode) {
  7748. spin_unlock(&cd->spinlock);
  7749. kfree(atten_new);
  7750. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7751. __func__,
  7752. "already subscribed attention",
  7753. dev, "mode", mode);
  7754. return 0;
  7755. }
  7756. }
  7757. atten_new->id = id;
  7758. atten_new->dev = dev;
  7759. atten_new->mode = mode;
  7760. atten_new->func = func;
  7761. list_add(&atten_new->node, &cd->atten_list[type]);
  7762. spin_unlock(&cd->spinlock);
  7763. return 0;
  7764. }
  7765. /*******************************************************************************
  7766. * FUNCTION: _pt_unsubscribe_attention
  7767. *
  7768. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7769. * to unsubscribe themselves from the TTDL attention list
  7770. *
  7771. * RETURN:
  7772. * 0 = success
  7773. * !0 = failure
  7774. *
  7775. * PARAMETERS:
  7776. * *dev - pointer to device structure
  7777. * type - attention type enum
  7778. * *id - ID of the module calling this function
  7779. * *func - function pointer
  7780. * mode - attention mode
  7781. ******************************************************************************/
  7782. int _pt_unsubscribe_attention(struct device *dev,
  7783. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7784. int mode)
  7785. {
  7786. struct pt_core_data *cd = dev_get_drvdata(dev);
  7787. struct atten_node *atten, *atten_n;
  7788. spin_lock(&cd->spinlock);
  7789. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7790. if (atten->id == id && atten->mode == mode) {
  7791. list_del(&atten->node);
  7792. spin_unlock(&cd->spinlock);
  7793. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7794. __func__,
  7795. "unsub for atten->dev", atten->dev,
  7796. "atten->mode", atten->mode);
  7797. kfree(atten);
  7798. return 0;
  7799. }
  7800. }
  7801. spin_unlock(&cd->spinlock);
  7802. return -ENODEV;
  7803. }
  7804. /*******************************************************************************
  7805. * FUNCTION: _pt_request_exclusive
  7806. *
  7807. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7808. * to request exclusive access
  7809. *
  7810. * RETURN:
  7811. * 0 = success
  7812. * !0 = failure
  7813. *
  7814. * PARAMETERS:
  7815. * *dev - pointer to device structure
  7816. * timeout_ms - timeout to wait for exclusive access
  7817. ******************************************************************************/
  7818. static int _pt_request_exclusive(struct device *dev,
  7819. int timeout_ms)
  7820. {
  7821. struct pt_core_data *cd = dev_get_drvdata(dev);
  7822. return request_exclusive(cd, (void *)dev, timeout_ms);
  7823. }
  7824. /*******************************************************************************
  7825. * FUNCTION: _pt_release_exclusive
  7826. *
  7827. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7828. * to release exclusive access
  7829. *
  7830. * RETURN:
  7831. * 0 = success
  7832. * !0 = failure
  7833. *
  7834. * PARAMETERS:
  7835. * *dev - pointer to device structure
  7836. ******************************************************************************/
  7837. static int _pt_release_exclusive(struct device *dev)
  7838. {
  7839. struct pt_core_data *cd = dev_get_drvdata(dev);
  7840. return release_exclusive(cd, (void *)dev);
  7841. }
  7842. /*******************************************************************************
  7843. * FUNCTION: _pt_request_reset
  7844. *
  7845. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7846. * to request the DUT to be reset. Function returns err if refused or
  7847. * timeout occurs (Note: core uses fixed timeout period).
  7848. *
  7849. * NOTE: Function blocks until ISR occurs.
  7850. *
  7851. * RETURN:
  7852. * 0 = success
  7853. * !0 = failure
  7854. *
  7855. * PARAMETERS:
  7856. * *dev - pointer to device structure
  7857. * protect - flag to call protected or non-protected
  7858. ******************************************************************************/
  7859. static int _pt_request_reset(struct device *dev, int protect)
  7860. {
  7861. struct pt_core_data *cd = dev_get_drvdata(dev);
  7862. int rc;
  7863. rc = pt_dut_reset(cd, protect);
  7864. if (rc < 0) {
  7865. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7866. __func__, rc);
  7867. }
  7868. return rc;
  7869. }
  7870. /*******************************************************************************
  7871. * FUNCTION: _pt_request_enum
  7872. *
  7873. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7874. * to request TTDL to queue an enum. This function will return err
  7875. * if refused; if no error then enum has completed and system is in
  7876. * normal operation mode.
  7877. *
  7878. * RETURN:
  7879. * 0 = success
  7880. * !0 = failure
  7881. *
  7882. * PARAMETERS:
  7883. * *dev - pointer to device structure
  7884. * wait - boolean to determine if to wait for startup event
  7885. ******************************************************************************/
  7886. static int _pt_request_enum(struct device *dev, bool wait)
  7887. {
  7888. struct pt_core_data *cd = dev_get_drvdata(dev);
  7889. pt_queue_enum(cd);
  7890. if (wait)
  7891. wait_event_timeout(cd->wait_q,
  7892. cd->startup_state == STARTUP_NONE,
  7893. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7894. return 0;
  7895. }
  7896. /*******************************************************************************
  7897. * FUNCTION: _pt_request_sysinfo
  7898. *
  7899. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7900. * to request the pointer to the system information structure. This
  7901. * function will return NULL if sysinfo has not been acquired from the
  7902. * DUT yet.
  7903. *
  7904. * RETURN: Pointer to the system information struct
  7905. *
  7906. * PARAMETERS:
  7907. * *dev - pointer to device structure
  7908. ******************************************************************************/
  7909. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7910. {
  7911. struct pt_core_data *cd = dev_get_drvdata(dev);
  7912. int rc = 0;
  7913. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7914. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7915. !cd->sysinfo.ready) {
  7916. rc = pt_hid_output_get_sysinfo_(cd);
  7917. if (rc) {
  7918. pt_debug(cd->dev, DL_ERROR,
  7919. "%s: Error getting sysinfo rc=%d\n",
  7920. __func__, rc);
  7921. }
  7922. }
  7923. if (cd->sysinfo.ready)
  7924. return &cd->sysinfo;
  7925. return NULL;
  7926. }
  7927. /*******************************************************************************
  7928. * FUNCTION: _pt_request_loader_pdata
  7929. *
  7930. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7931. * to request the pointer to the loader platform data
  7932. *
  7933. * RETURN: Pointer to the loader platform data struct
  7934. *
  7935. * PARAMETERS:
  7936. * *dev - pointer to device structure
  7937. ******************************************************************************/
  7938. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7939. struct device *dev)
  7940. {
  7941. struct pt_core_data *cd = dev_get_drvdata(dev);
  7942. return cd->pdata->loader_pdata;
  7943. }
  7944. /*******************************************************************************
  7945. * FUNCTION: _pt_request_start_wd
  7946. *
  7947. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7948. * to request to start the TTDL watchdog
  7949. *
  7950. * RETURN:
  7951. * 0 = success
  7952. * !0 = failure
  7953. *
  7954. * PARAMETERS:
  7955. * *dev - pointer to device structure
  7956. ******************************************************************************/
  7957. static int _pt_request_start_wd(struct device *dev)
  7958. {
  7959. struct pt_core_data *cd = dev_get_drvdata(dev);
  7960. pt_start_wd_timer(cd);
  7961. return 0;
  7962. }
  7963. /*******************************************************************************
  7964. * FUNCTION: _pt_request_stop_wd
  7965. *
  7966. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7967. * to request to stop the TTDL watchdog
  7968. *
  7969. * RETURN:
  7970. * 0 = success
  7971. * !0 = failure
  7972. *
  7973. * PARAMETERS:
  7974. * *dev - pointer to device structure
  7975. ******************************************************************************/
  7976. static int _pt_request_stop_wd(struct device *dev)
  7977. {
  7978. struct pt_core_data *cd = dev_get_drvdata(dev);
  7979. pt_stop_wd_timer(cd);
  7980. return 0;
  7981. }
  7982. /*******************************************************************************
  7983. * FUNCTION: pt_pip2_launch_app
  7984. *
  7985. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7986. * the FW reset sentinel to indicate the function succeeded.
  7987. *
  7988. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7989. * in a timeout delay and with the DUT being reset with an XRES.
  7990. *
  7991. * RETURN:
  7992. * 0 = success
  7993. * !0 = failure
  7994. *
  7995. * PARAMETERS:
  7996. * *cd - pointer to core data structure
  7997. * protect - flag to call protected or non-protected
  7998. ******************************************************************************/
  7999. static int pt_pip2_launch_app(struct device *dev, int protect)
  8000. {
  8001. struct pt_core_data *cd = dev_get_drvdata(dev);
  8002. u16 actual_read_len;
  8003. u16 tmp_startup_status = cd->startup_status;
  8004. u8 read_buf[12];
  8005. u8 status;
  8006. int time = 0;
  8007. int rc = 0;
  8008. mutex_lock(&cd->system_lock);
  8009. cd->startup_status = STARTUP_STATUS_START;
  8010. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8011. mutex_unlock(&cd->system_lock);
  8012. rc = _pt_request_pip2_send_cmd(dev, protect,
  8013. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8014. &actual_read_len);
  8015. if (rc) {
  8016. pt_debug(dev, DL_ERROR,
  8017. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8018. __func__, rc);
  8019. } else {
  8020. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8021. /* Test for no or invalid image in FLASH, no point to reset */
  8022. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8023. rc = status;
  8024. goto exit;
  8025. }
  8026. /* Any other boot failure */
  8027. if (status != 0) {
  8028. pt_debug(dev, DL_ERROR,
  8029. "%s: FW did not EXECUTE, status = %d\n",
  8030. __func__, status);
  8031. rc = status;
  8032. }
  8033. }
  8034. if (rc) {
  8035. pt_debug(dev, DL_ERROR,
  8036. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8037. __func__, rc);
  8038. goto exit;
  8039. }
  8040. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8041. msleep(20);
  8042. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8043. __func__, time, cd->startup_status);
  8044. time += 20;
  8045. }
  8046. if (cd->startup_status == STARTUP_STATUS_START) {
  8047. pt_debug(cd->dev, DL_WARN,
  8048. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8049. rc = -ETIME;
  8050. }
  8051. exit:
  8052. if (cd->startup_status == STARTUP_STATUS_START) {
  8053. /* Reset to original state because we could be stuck in BL */
  8054. mutex_lock(&cd->system_lock);
  8055. cd->startup_status = tmp_startup_status;
  8056. mutex_unlock(&cd->system_lock);
  8057. }
  8058. return rc;
  8059. }
  8060. /*******************************************************************************
  8061. * FUNCTION: _pt_request_pip2_launch_app
  8062. *
  8063. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8064. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8065. * consumed.
  8066. *
  8067. * RETURN:
  8068. * 0 = success
  8069. * !0 = failure
  8070. *
  8071. * PARAMETERS:
  8072. * *cd - pointer to core data structure
  8073. ******************************************************************************/
  8074. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8075. {
  8076. return pt_pip2_launch_app(dev, protect);
  8077. }
  8078. /*******************************************************************************
  8079. * FUNCTION: _pt_request_wait_for_enum_state
  8080. *
  8081. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8082. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8083. *
  8084. * RETURN:
  8085. * 0 = success
  8086. * !0 = failure
  8087. *
  8088. * PARAMETERS:
  8089. * *cd - pointer to core data structure
  8090. * timeout - timeout for how long to wait
  8091. * state - enum state to wait for
  8092. ******************************************************************************/
  8093. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8094. int state)
  8095. {
  8096. int rc = 0;
  8097. int t;
  8098. struct pt_core_data *cd = dev_get_drvdata(dev);
  8099. t = wait_event_timeout(cd->wait_q,
  8100. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8101. msecs_to_jiffies(timeout));
  8102. if (IS_TMO(t)) {
  8103. pt_debug(cd->dev, DL_ERROR,
  8104. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8105. __func__, state, timeout);
  8106. pt_debug(cd->dev, DL_WARN,
  8107. "%s: enum state reached 0x%04X\n",
  8108. __func__, cd->startup_status);
  8109. rc = -ETIME;
  8110. } else if (cd->startup_status & state) {
  8111. pt_debug(cd->dev, DL_INFO,
  8112. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8113. __func__, cd->startup_status,
  8114. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8115. } else {
  8116. if (t == 1) {
  8117. pt_debug(
  8118. cd->dev, DL_ERROR,
  8119. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8120. __func__, state, timeout);
  8121. rc = -ETIME;
  8122. } else {
  8123. pt_debug(
  8124. cd->dev, DL_ERROR,
  8125. "%s: Enum state 0x%04X not reached in %dms\n",
  8126. __func__, state, timeout - jiffies_to_msecs(t));
  8127. rc = -EINVAL;
  8128. }
  8129. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8130. __func__, cd->startup_status);
  8131. }
  8132. return rc;
  8133. }
  8134. /*******************************************************************************
  8135. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8136. *
  8137. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8138. * deep sleep.
  8139. *
  8140. * RETURN:
  8141. * 0 = success
  8142. * !0 = error
  8143. *
  8144. * PARAMETERS:
  8145. * *cd - pointer to core data
  8146. ******************************************************************************/
  8147. static int pt_core_wake_device_from_deep_sleep_(
  8148. struct pt_core_data *cd)
  8149. {
  8150. int rc;
  8151. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8152. if (rc)
  8153. rc = -EAGAIN;
  8154. /* Prevent failure on sequential wake/sleep requests from OS */
  8155. msleep(20);
  8156. return rc;
  8157. }
  8158. /*******************************************************************************
  8159. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8160. *
  8161. * SUMMARY: Wake up device from Easy-Wake state.
  8162. *
  8163. * RETURN:
  8164. * 0 = success
  8165. * !0 = failure
  8166. *
  8167. * PARAMETERS:
  8168. * *cd - pointer to core data
  8169. ******************************************************************************/
  8170. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8171. {
  8172. int rc = 0;
  8173. mutex_lock(&cd->system_lock);
  8174. cd->wait_until_wake = 1;
  8175. mutex_unlock(&cd->system_lock);
  8176. wake_up(&cd->wait_q);
  8177. msleep(20);
  8178. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8179. return rc;
  8180. }
  8181. /*******************************************************************************
  8182. * FUNCTION: pt_restore_parameters_
  8183. *
  8184. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8185. * back to the DUT
  8186. *
  8187. * RETURN:
  8188. * 0 = success
  8189. * !0 = failure
  8190. *
  8191. * PARAMETERS:
  8192. * *cd - pointer the core data structure
  8193. ******************************************************************************/
  8194. static int pt_restore_parameters_(struct pt_core_data *cd)
  8195. {
  8196. struct param_node *param;
  8197. int rc = 0;
  8198. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8199. goto exit;
  8200. spin_lock(&cd->spinlock);
  8201. list_for_each_entry(param, &cd->param_list, node) {
  8202. spin_unlock(&cd->spinlock);
  8203. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8204. __func__, param->id, param->value);
  8205. rc = pt_pip_set_param_(cd, param->id,
  8206. param->value, param->size);
  8207. if (rc)
  8208. goto exit;
  8209. spin_lock(&cd->spinlock);
  8210. }
  8211. spin_unlock(&cd->spinlock);
  8212. exit:
  8213. return rc;
  8214. }
  8215. /*******************************************************************************
  8216. * FUNCTION: pt_pip2_exit_bl_
  8217. *
  8218. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8219. * a DUT that may not have flash and will need FW to be loaded into RAM
  8220. *
  8221. * RETURN:
  8222. * 0 = success
  8223. * !0 = failure
  8224. *
  8225. * PARAMETERS:
  8226. * *cd - pointer the core data structure
  8227. * *status_str - pointer to optional status string buffer
  8228. * buf_size - size of status_str buffer
  8229. ******************************************************************************/
  8230. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8231. {
  8232. int rc;
  8233. int wait_time = 0;
  8234. u8 mode = PT_MODE_UNKNOWN;
  8235. bool load_status_str = false;
  8236. /*
  8237. * Below function has protective call to ensure no enum is still on
  8238. * going, while this kind of protection should be applied widely in
  8239. * future (TODO).
  8240. */
  8241. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8242. if (status_str && buf_size <= 50)
  8243. load_status_str = true;
  8244. if (mode == PT_MODE_BOOTLOADER) {
  8245. if (cd->flashless_dut == 1) {
  8246. rc = pt_hw_hard_reset(cd);
  8247. } else {
  8248. rc = pt_pip2_launch_app(cd->dev,
  8249. PT_CORE_CMD_UNPROTECTED);
  8250. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8251. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8252. __func__, "Invalid image in FLASH rc", rc);
  8253. } else if (rc) {
  8254. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8255. __func__, "Failed to launch app rc", rc);
  8256. }
  8257. }
  8258. if (!rc) {
  8259. if (cd->flashless_dut == 1) {
  8260. /* Wait for BL to complete before enum */
  8261. rc = _pt_request_wait_for_enum_state(cd->dev,
  8262. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8263. if (rc && load_status_str) {
  8264. strlcpy(status_str, "No FW sentinel after BL",
  8265. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8266. goto exit;
  8267. }
  8268. }
  8269. /*
  8270. * If the host wants to interact with the FW or do a
  8271. * forced calibration, the FW must be out of BOOT mode
  8272. * and the system information must have been retrieved.
  8273. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8274. * If, however, the enumeration does not reach this
  8275. * point, the DUT may still be in APP mode so test
  8276. * for all conditions.
  8277. */
  8278. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8279. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8280. if (!rc || cd->startup_status >=
  8281. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8282. mutex_lock(&cd->system_lock);
  8283. cd->mode = PT_MODE_OPERATIONAL;
  8284. mutex_unlock(&cd->system_lock);
  8285. }
  8286. if (rc) {
  8287. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8288. __func__, "Failed to enum with DUT",
  8289. cd->startup_status);
  8290. if (load_status_str && !(cd->startup_status &
  8291. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8292. strlcpy(status_str, "FW Stuck in Boot mode",
  8293. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8294. goto exit;
  8295. }
  8296. }
  8297. /*
  8298. * The coming FW sentinel could wake up the event
  8299. * queue, which has chance to be taken by next command
  8300. * wrongly. Following delay is a workaround to avoid
  8301. * this issue for most situations.
  8302. */
  8303. msleep(20);
  8304. pt_start_wd_timer(cd);
  8305. }
  8306. if (load_status_str) {
  8307. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8308. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8309. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8310. else if (!rc)
  8311. strlcpy(status_str, "Entered APP from BL mode",
  8312. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8313. else
  8314. strlcpy(status_str, "Failed to enter APP from BL mode",
  8315. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8316. }
  8317. } else if (mode == PT_MODE_OPERATIONAL) {
  8318. mutex_lock(&cd->system_lock);
  8319. cd->mode = mode;
  8320. mutex_unlock(&cd->system_lock);
  8321. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8322. if (load_status_str) {
  8323. if (!rc)
  8324. strlcpy(status_str, "Already in APP mode",
  8325. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8326. else
  8327. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8328. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8329. }
  8330. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8331. mutex_lock(&cd->system_lock);
  8332. cd->mode = mode;
  8333. mutex_unlock(&cd->system_lock);
  8334. if (load_status_str)
  8335. strlcpy(status_str, "Failed to determine active mode",
  8336. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8337. }
  8338. exit:
  8339. if (!rc)
  8340. pt_start_wd_timer(cd);
  8341. return rc;
  8342. }
  8343. /*******************************************************************************
  8344. * FUNCTION: pt_pip2_exit_bl
  8345. *
  8346. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8347. * access.
  8348. *
  8349. * RETURN:
  8350. * 0 = success
  8351. * !0 = failure
  8352. *
  8353. * PARAMETERS:
  8354. * *cd - pointer the core data structure
  8355. * *status_str - pointer to optional status string buffer
  8356. * buf_size - size of status_str buffer
  8357. ******************************************************************************/
  8358. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8359. {
  8360. int rc;
  8361. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8362. if (rc < 0) {
  8363. pt_debug(cd->dev, DL_ERROR,
  8364. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8365. cd->exclusive_dev, cd->dev);
  8366. return rc;
  8367. }
  8368. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8369. if (release_exclusive(cd, cd->dev) < 0)
  8370. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8371. __func__);
  8372. return rc;
  8373. }
  8374. /*******************************************************************************
  8375. * FUNCTION: _fast_startup
  8376. *
  8377. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8378. *
  8379. * RETURN:
  8380. * 0 = success
  8381. * !0 = failure
  8382. *
  8383. * PARAMETERS:
  8384. * *cd - pointer the core data structure
  8385. ******************************************************************************/
  8386. static int _fast_startup(struct pt_core_data *cd)
  8387. {
  8388. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8389. int rc = 0;
  8390. u8 mode = PT_MODE_UNKNOWN;
  8391. struct pt_hid_desc hid_desc;
  8392. int wait_time = 0;
  8393. memset(&hid_desc, 0, sizeof(hid_desc));
  8394. reset:
  8395. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8396. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8397. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8398. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8399. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8400. __func__);
  8401. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8402. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8403. if (rc < 0) {
  8404. pt_debug(cd->dev, DL_ERROR,
  8405. "%s: Error on getting HID descriptor r=%d\n",
  8406. __func__, rc);
  8407. if (retry--)
  8408. goto reset;
  8409. goto exit;
  8410. }
  8411. cd->mode = pt_get_mode(cd, &hid_desc);
  8412. if (cd->mode == PT_MODE_BOOTLOADER) {
  8413. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8414. __func__);
  8415. rc = pt_hid_output_bl_launch_app_(cd);
  8416. if (rc < 0) {
  8417. pt_debug(cd->dev, DL_ERROR,
  8418. "%s: Error on launch app r=%d\n",
  8419. __func__, rc);
  8420. if (retry--)
  8421. goto reset;
  8422. goto exit;
  8423. }
  8424. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8425. if (rc < 0) {
  8426. pt_debug(cd->dev, DL_ERROR,
  8427. "%s: Error on getting HID descriptor r=%d\n",
  8428. __func__, rc);
  8429. if (retry--)
  8430. goto reset;
  8431. goto exit;
  8432. }
  8433. cd->mode = pt_get_mode(cd, &hid_desc);
  8434. if (cd->mode == PT_MODE_BOOTLOADER) {
  8435. if (retry--)
  8436. goto reset;
  8437. goto exit;
  8438. }
  8439. }
  8440. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8441. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8442. } else {
  8443. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8444. __func__);
  8445. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8446. /* Wait for any sentinel before first try */
  8447. rc = _pt_request_wait_for_enum_state(
  8448. cd->dev, 150,
  8449. STARTUP_STATUS_BL_RESET_SENTINEL |
  8450. STARTUP_STATUS_FW_RESET_SENTINEL);
  8451. if (rc)
  8452. pt_debug(cd->dev, DL_ERROR,
  8453. "%s: No Sentinel detected rc = %d\n",
  8454. __func__, rc);
  8455. } else
  8456. pt_flush_bus_if_irq_asserted(cd,
  8457. PT_FLUSH_BUS_BASED_ON_LEN);
  8458. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8459. if (rc) {
  8460. pt_debug(cd->dev, DL_ERROR,
  8461. "%s: Get mode failed, mode unknown\n",
  8462. __func__);
  8463. mode = PT_MODE_UNKNOWN;
  8464. }
  8465. cd->mode = mode;
  8466. if (cd->mode == PT_MODE_BOOTLOADER) {
  8467. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8468. __func__);
  8469. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8470. if (rc) {
  8471. pt_debug(cd->dev, DL_ERROR,
  8472. "%s Failed to exit bootloader\n",
  8473. __func__);
  8474. msleep(50);
  8475. rc = -ENODEV;
  8476. if (retry--)
  8477. goto reset;
  8478. goto exit;
  8479. } else {
  8480. pt_debug(cd->dev, DL_INFO,
  8481. "%s: Exit bootloader successfully\n",
  8482. __func__);
  8483. }
  8484. if (cd->mode != PT_MODE_OPERATIONAL) {
  8485. pt_debug(cd->dev, DL_WARN,
  8486. "%s: restore mode failure mode = %d\n",
  8487. __func__, cd->mode);
  8488. if (retry--)
  8489. goto reset;
  8490. goto exit;
  8491. }
  8492. }
  8493. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8494. }
  8495. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8496. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8497. if (!rc) {
  8498. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8499. pt_debug(cd->dev, DL_WARN,
  8500. "%s: Exit FW BOOT Mode after %dms\n",
  8501. __func__, wait_time);
  8502. } else {
  8503. pt_debug(cd->dev, DL_WARN,
  8504. "%s: FW stuck in BOOT Mode after %dms\n",
  8505. __func__, wait_time);
  8506. goto exit;
  8507. }
  8508. if (!cd->sysinfo.ready) {
  8509. rc = pt_hid_output_get_sysinfo_(cd);
  8510. if (rc) {
  8511. pt_debug(cd->dev, DL_ERROR,
  8512. "%s: Error on getting sysinfo r=%d\n",
  8513. __func__, rc);
  8514. if (retry--)
  8515. goto reset;
  8516. goto exit;
  8517. }
  8518. }
  8519. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8520. rc = pt_restore_parameters_(cd);
  8521. if (rc)
  8522. pt_debug(cd->dev, DL_ERROR,
  8523. "%s: failed to restore parameters rc=%d\n",
  8524. __func__, rc);
  8525. else
  8526. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8527. exit:
  8528. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8529. return rc;
  8530. }
  8531. /*******************************************************************************
  8532. * FUNCTION: pt_core_poweron_device_
  8533. *
  8534. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8535. *
  8536. * RETURN:
  8537. * 0 = success
  8538. * !0 = failure
  8539. *
  8540. * PARAMETERS:
  8541. * *cd - pointer to core data
  8542. ******************************************************************************/
  8543. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8544. {
  8545. struct device *dev = cd->dev;
  8546. int rc = 0;
  8547. /*
  8548. * After power on action, the chip can general FW sentinel. It can
  8549. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8550. * _fast_startup() can perform a quick enumeration too, here doesn't
  8551. * need another enumeration.
  8552. */
  8553. mutex_lock(&cd->system_lock);
  8554. cd->startup_status = STARTUP_STATUS_START;
  8555. cd->hid_reset_cmd_state = 1;
  8556. mutex_unlock(&cd->system_lock);
  8557. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8558. if (rc < 0) {
  8559. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8560. __func__, rc);
  8561. goto exit;
  8562. }
  8563. if (!cd->irq_enabled) {
  8564. cd->irq_enabled = true;
  8565. enable_irq(cd->irq);
  8566. }
  8567. /* TBD: following function doesn't update startup_status */
  8568. rc = _fast_startup(cd);
  8569. exit:
  8570. return rc;
  8571. }
  8572. /*******************************************************************************
  8573. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8574. *
  8575. * SUMMARY: Reset device, and then trigger a full enumeration.
  8576. *
  8577. * RETURN:
  8578. * 0 = success
  8579. * !0 = failure
  8580. *
  8581. * PARAMETERS:
  8582. * *cd - pointer to core data
  8583. ******************************************************************************/
  8584. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8585. {
  8586. int rc;
  8587. rc = pt_dut_reset_and_wait(cd);
  8588. if (rc < 0) {
  8589. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8590. __func__, rc);
  8591. goto exit;
  8592. }
  8593. rc = _fast_startup(cd);
  8594. exit:
  8595. return rc;
  8596. }
  8597. /*******************************************************************************
  8598. * FUNCTION: pt_core_easywake_off_
  8599. *
  8600. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8601. * the configuration in the core platform data structure.
  8602. *
  8603. * RETURN:
  8604. * 0 = success
  8605. * !0 = failure
  8606. *
  8607. * PARAMETERS:
  8608. * *cd - pointer to core data
  8609. ******************************************************************************/
  8610. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8611. {
  8612. int rc = 0;
  8613. mutex_lock(&cd->system_lock);
  8614. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8615. mutex_unlock(&cd->system_lock);
  8616. pt_debug(cd->dev, DL_INFO,
  8617. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8618. return 0;
  8619. }
  8620. mutex_unlock(&cd->system_lock);
  8621. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8622. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8623. rc = pt_core_wake_device_from_easy_wake_(cd);
  8624. if (rc < 0)
  8625. pt_debug(cd->dev, DL_ERROR,
  8626. "%s - %d failed %d\n", __func__, rc);
  8627. }
  8628. mutex_lock(&cd->system_lock);
  8629. cd->sleep_state = SS_EASY_WAKING_OFF;
  8630. mutex_unlock(&cd->system_lock);
  8631. pt_start_wd_timer(cd);
  8632. return rc;
  8633. }
  8634. /*******************************************************************************
  8635. * FUNCTION: pt_core_easywake_off
  8636. *
  8637. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8638. *
  8639. * RETURN:
  8640. * 0 = success
  8641. * !0 = failure
  8642. *
  8643. * PARAMETERS:
  8644. * *cd - pointer to core data
  8645. ******************************************************************************/
  8646. static int pt_core_easywake_off(struct pt_core_data *cd)
  8647. {
  8648. int rc;
  8649. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8650. if (rc < 0) {
  8651. pt_debug(cd->dev, DL_ERROR,
  8652. "%s: fail get exclusive ex=%p own=%p\n",
  8653. __func__, cd->exclusive_dev, cd->dev);
  8654. return rc;
  8655. }
  8656. rc = pt_core_easywake_off_(cd);
  8657. if (release_exclusive(cd, cd->dev) < 0)
  8658. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8659. __func__);
  8660. else
  8661. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8662. __func__);
  8663. return rc;
  8664. }
  8665. /*******************************************************************************
  8666. * FUNCTION: pt_core_wake_
  8667. *
  8668. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8669. * the configuration in the core platform data structure.
  8670. *
  8671. * RETURN:
  8672. * 0 = success
  8673. * !0 = failure
  8674. *
  8675. * PARAMETERS:
  8676. * *cd - pointer to core data
  8677. ******************************************************************************/
  8678. static int pt_core_wake_(struct pt_core_data *cd)
  8679. {
  8680. int rc = 0;
  8681. mutex_lock(&cd->system_lock);
  8682. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEP_NONE) {
  8683. cd->sleep_state = SS_WAKING;
  8684. } else {
  8685. mutex_unlock(&cd->system_lock);
  8686. pt_debug(cd->dev, DL_INFO,
  8687. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8688. return 0;
  8689. }
  8690. mutex_unlock(&cd->system_lock);
  8691. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8692. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8693. pt_debug(cd->dev, DL_INFO,
  8694. "%s: Entering into poweron mode:\n", __func__);
  8695. rc = pt_core_poweron_device_(cd);
  8696. if (rc < 0)
  8697. pr_err("%s: Poweron error detected: rc=%d\n",
  8698. __func__, rc);
  8699. }
  8700. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8701. rc = pt_core_wake_device_from_deep_standby_(cd);
  8702. else /* Default action to exit DeepSleep */
  8703. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8704. }
  8705. mutex_lock(&cd->system_lock);
  8706. cd->sleep_state = SS_SLEEP_OFF;
  8707. mutex_unlock(&cd->system_lock);
  8708. pt_start_wd_timer(cd);
  8709. return rc;
  8710. }
  8711. /*******************************************************************************
  8712. * FUNCTION: pt_core_wake_
  8713. *
  8714. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8715. *
  8716. * RETURN:
  8717. * 0 = success
  8718. * !0 = failure
  8719. *
  8720. * PARAMETERS:
  8721. * *cd - pointer to core data
  8722. ******************************************************************************/
  8723. static int pt_core_wake(struct pt_core_data *cd)
  8724. {
  8725. int rc = 0;
  8726. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8727. if (rc < 0) {
  8728. pt_debug(cd->dev, DL_ERROR,
  8729. "%s: fail get exclusive ex=%p own=%p\n",
  8730. __func__, cd->exclusive_dev, cd->dev);
  8731. return rc;
  8732. }
  8733. rc = pt_core_wake_(cd);
  8734. if (release_exclusive(cd, cd->dev) < 0)
  8735. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8736. __func__);
  8737. else
  8738. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8739. __func__);
  8740. return rc;
  8741. }
  8742. /*******************************************************************************
  8743. * FUNCTION: pt_get_ic_crc_
  8744. *
  8745. * SUMMARY: This function retrieves the config block CRC
  8746. *
  8747. * NOTE: The post condition of calling this function will be that the DUT will
  8748. * be in SCANNINING mode if no failures occur
  8749. *
  8750. * RETURN:
  8751. * 0 = success
  8752. * !0 = failure
  8753. *
  8754. * PARAMETERS:
  8755. * *cd - pointer the core data structure
  8756. * ebid - enumerated block ID
  8757. ******************************************************************************/
  8758. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8759. {
  8760. struct pt_sysinfo *si = &cd->sysinfo;
  8761. int rc = 0;
  8762. u8 status;
  8763. u16 calculated_crc = 0;
  8764. u16 stored_crc = 0;
  8765. rc = pt_pip_suspend_scanning_(cd);
  8766. if (rc)
  8767. goto error;
  8768. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8769. &calculated_crc, &stored_crc);
  8770. if (rc)
  8771. goto exit;
  8772. if (status) {
  8773. rc = -EINVAL;
  8774. goto exit;
  8775. }
  8776. si->ttconfig.crc = stored_crc;
  8777. exit:
  8778. pt_pip_resume_scanning_(cd);
  8779. error:
  8780. pt_debug(cd->dev, DL_INFO,
  8781. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8782. __func__, ebid, calculated_crc, stored_crc, rc);
  8783. return rc;
  8784. }
  8785. /*******************************************************************************
  8786. * FUNCTION: pt_pip2_read_gpio
  8787. *
  8788. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8789. * bits into the passed in variable
  8790. *
  8791. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8792. *
  8793. * RETURN:
  8794. * 0 = success
  8795. * !0 = failure
  8796. *
  8797. * PARAMETERS:
  8798. * *dev - pointer to device structure
  8799. * *status - pointer to where the command response status is stored
  8800. * *gpio - pointer to device attributes structure
  8801. ******************************************************************************/
  8802. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8803. {
  8804. int rc = 0;
  8805. u16 actual_read_len;
  8806. u8 read_buf[12];
  8807. u8 tmp_status = 0;
  8808. u8 index = PIP2_RESP_STATUS_OFFSET;
  8809. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8810. rc = _pt_request_pip2_send_cmd(dev,
  8811. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8812. NULL, 0, read_buf, &actual_read_len);
  8813. if (rc) {
  8814. pt_debug(dev, DL_ERROR,
  8815. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8816. rc = -ECOMM;
  8817. } else {
  8818. tmp_status = read_buf[index];
  8819. }
  8820. if (status)
  8821. *status = tmp_status;
  8822. if (!rc && gpio && (tmp_status == 0)) {
  8823. *gpio = ((read_buf[index + 4] << 24) |
  8824. (read_buf[index + 3] << 16) |
  8825. (read_buf[index + 2] << 8) |
  8826. (read_buf[index + 1]));
  8827. }
  8828. return rc;
  8829. }
  8830. /*******************************************************************************
  8831. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8832. *
  8833. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8834. *
  8835. * RETURN:
  8836. * 0 = success
  8837. * !0 = failure
  8838. *
  8839. * PARAMETERS:
  8840. * *cd - pointer the core data structure
  8841. * *pid - pointer to store panel ID
  8842. ******************************************************************************/
  8843. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8844. {
  8845. u32 gpio_value = 0;
  8846. u8 status = 0;
  8847. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8848. int rc = 0;
  8849. if (!pid)
  8850. return -ENOMEM;
  8851. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8852. if (!rc) {
  8853. if (status == 0) {
  8854. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8855. PT_PANEL_ID_SHIFT;
  8856. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8857. __func__,
  8858. "BL mode PID", panel_id, "gpio", gpio_value);
  8859. *pid = panel_id;
  8860. } else {
  8861. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8862. __func__,
  8863. "BL read gpio failed status", status);
  8864. }
  8865. } else {
  8866. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8867. __func__,
  8868. "BL read gpio failed status", status);
  8869. }
  8870. return rc;
  8871. }
  8872. /*******************************************************************************
  8873. * FUNCTION: pt_enum_with_dut_
  8874. *
  8875. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8876. * The core data (cd) startup_status will store, as a bitmask, each
  8877. * state of the enumeration process. The startup will be attempted
  8878. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8879. *
  8880. * RETURN:
  8881. * 0 = success
  8882. * !0 = failure
  8883. *
  8884. * PARAMETERS:
  8885. * *cd - pointer the core data structure
  8886. * reset - Flag to reset the DUT before attempting to enumerate
  8887. * *status - poionter to store the enum status bitmask flags
  8888. ******************************************************************************/
  8889. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8890. u32 *enum_status)
  8891. {
  8892. int try = 1;
  8893. int rc = 0;
  8894. int wait_time = 0;
  8895. bool detected = false;
  8896. u8 return_data[8];
  8897. u8 mode = PT_MODE_UNKNOWN;
  8898. u8 pid = PANEL_ID_NOT_ENABLED;
  8899. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8900. struct pt_hid_desc hid_desc;
  8901. memset(&hid_desc, 0, sizeof(hid_desc));
  8902. #ifdef TTHE_TUNER_SUPPORT
  8903. tthe_print(cd, NULL, 0, "enter startup");
  8904. #endif
  8905. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8906. __func__, cd->startup_status, reset);
  8907. pt_stop_wd_timer(cd);
  8908. reset:
  8909. if (try > 1)
  8910. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8911. __func__, try);
  8912. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8913. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8914. pt_debug(cd->dev, DL_INFO,
  8915. "%s: PIP1 Enumeration start\n", __func__);
  8916. /* Only reset the DUT after the first try */
  8917. if (reset || (try > 1)) {
  8918. /*
  8919. * Reset hardware only for Legacy parts. Skip for TT/TC
  8920. * parts because if the FW image was loaded directly
  8921. * to SRAM issueing a reset ill wipe out what was just
  8922. * loaded.
  8923. */
  8924. rc = pt_dut_reset_and_wait(cd);
  8925. if (rc < 0) {
  8926. pt_debug(cd->dev, DL_ERROR,
  8927. "%s: Error on h/w reset r=%d\n",
  8928. __func__, rc);
  8929. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8930. goto reset;
  8931. goto exit;
  8932. }
  8933. /* sleep to allow FW to be launched if available */
  8934. msleep(120);
  8935. }
  8936. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8937. if (rc < 0) {
  8938. pt_debug(cd->dev, DL_ERROR,
  8939. "%s: Error getting HID Descriptor r=%d\n",
  8940. __func__, rc);
  8941. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8942. goto reset;
  8943. rc = -EIO;
  8944. goto exit;
  8945. }
  8946. detected = true;
  8947. cd->mode = pt_get_mode(cd, &hid_desc);
  8948. /*
  8949. * Most systems do not use an XY pin as the panel_id and so
  8950. * the BL is used to retrieve the panel_id, however for
  8951. * systems that do use an XY pin, the panel_id MUST be
  8952. * retrieved from the system information when running FW
  8953. * (done below) and so this section of code is skipped.
  8954. * Entering the BL here is only needed on XY_PIN systems.
  8955. */
  8956. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8957. if (cd->mode == PT_MODE_OPERATIONAL) {
  8958. rc = pt_pip_start_bootloader_(cd);
  8959. if (rc < 0) {
  8960. pt_debug(cd->dev, DL_ERROR,
  8961. "%s: Error on start bootloader r=%d\n",
  8962. __func__, rc);
  8963. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8964. goto reset;
  8965. goto exit;
  8966. }
  8967. cd->mode = PT_MODE_BOOTLOADER;
  8968. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8969. __func__);
  8970. }
  8971. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8972. if (!rc) {
  8973. cd->bl_info.ready = true;
  8974. cd->bl_info.chip_id =
  8975. get_unaligned_le16(&return_data[2]);
  8976. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8977. __func__, cd->bl_info.chip_id);
  8978. } else {
  8979. pt_debug(cd->dev, DL_ERROR,
  8980. "%s: failed to get chip ID, r=%d\n",
  8981. __func__, rc);
  8982. }
  8983. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8984. mutex_lock(&cd->system_lock);
  8985. if (!rc) {
  8986. cd->pid_for_loader = pid;
  8987. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8988. __func__, cd->pid_for_loader);
  8989. } else {
  8990. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8991. pt_debug(cd->dev, DL_WARN,
  8992. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8993. __func__, cd->pid_for_loader);
  8994. }
  8995. mutex_unlock(&cd->system_lock);
  8996. }
  8997. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8998. if (cd->mode == PT_MODE_BOOTLOADER) {
  8999. rc = pt_hid_output_bl_launch_app_(cd);
  9000. if (rc < 0) {
  9001. pt_debug(cd->dev, DL_ERROR,
  9002. "%s: Error on launch app r=%d\n",
  9003. __func__, rc);
  9004. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9005. goto reset;
  9006. rc = -ENODEV;
  9007. goto exit;
  9008. }
  9009. /* sleep to allow FW to be launched if available */
  9010. msleep(120);
  9011. /* Ensure the DUT is now in Application mode */
  9012. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9013. if (rc < 0) {
  9014. pt_debug(cd->dev, DL_ERROR,
  9015. "%s: Error getting HID Desc r=%d\n",
  9016. __func__, rc);
  9017. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9018. goto reset;
  9019. rc = -EIO;
  9020. goto exit;
  9021. }
  9022. cd->mode = pt_get_mode(cd, &hid_desc);
  9023. if (cd->mode == PT_MODE_BOOTLOADER) {
  9024. pt_debug(cd->dev, DL_WARN,
  9025. "%s: Error confiming exit BL\n",
  9026. __func__);
  9027. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9028. goto reset;
  9029. rc = -ENODEV;
  9030. goto exit;
  9031. }
  9032. }
  9033. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9034. cd->mode = PT_MODE_OPERATIONAL;
  9035. *enum_status |= STARTUP_STATUS_GET_DESC;
  9036. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9037. } else {
  9038. /* Generation is PIP2 Capable */
  9039. pt_debug(cd->dev, DL_INFO,
  9040. "%s: PIP2 Enumeration start\n", __func__);
  9041. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9042. if (rc) {
  9043. pt_debug(cd->dev, DL_ERROR,
  9044. "%s: Get mode failed, mode unknown\n",
  9045. __func__);
  9046. mode = PT_MODE_UNKNOWN;
  9047. } else
  9048. detected = true;
  9049. cd->mode = mode;
  9050. switch (cd->mode) {
  9051. case PT_MODE_OPERATIONAL:
  9052. pt_debug(cd->dev, DL_INFO,
  9053. "%s: Operational mode\n", __func__);
  9054. if (cd->app_pip_ver_ready == false) {
  9055. rc = pt_pip2_get_version_(cd);
  9056. if (!rc)
  9057. cd->app_pip_ver_ready = true;
  9058. else {
  9059. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9060. goto reset;
  9061. goto exit;
  9062. }
  9063. }
  9064. break;
  9065. case PT_MODE_BOOTLOADER:
  9066. pt_debug(cd->dev, DL_INFO,
  9067. "%s: Bootloader mode\n", __func__);
  9068. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9069. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9070. mutex_lock(&cd->system_lock);
  9071. if (!rc) {
  9072. cd->pid_for_loader = pid;
  9073. pt_debug(cd->dev, DL_INFO,
  9074. "%s: Panel ID: 0x%02X\n",
  9075. __func__, cd->pid_for_loader);
  9076. } else {
  9077. cd->pid_for_loader =
  9078. PANEL_ID_NOT_ENABLED;
  9079. pt_debug(cd->dev, DL_WARN,
  9080. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9081. __func__, cd->pid_for_loader);
  9082. }
  9083. mutex_unlock(&cd->system_lock);
  9084. }
  9085. if (cd->bl_pip_ver_ready == false) {
  9086. rc = pt_pip2_get_version_(cd);
  9087. if (!rc)
  9088. cd->bl_pip_ver_ready = true;
  9089. else {
  9090. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9091. goto reset;
  9092. goto exit;
  9093. }
  9094. }
  9095. /*
  9096. * Launch app command will fail in flashless mode.
  9097. * Skip launch app command here to save time for
  9098. * enumeration flow.
  9099. */
  9100. if (cd->flashless_dut)
  9101. goto exit;
  9102. /*
  9103. * pt_pip2_launch_app() is needed here instead of
  9104. * pt_pip2_exit_bl() because exit_bl will cause several
  9105. * hw_resets to occur and the auto BL on a flashless
  9106. * DUT will fail.
  9107. */
  9108. rc = pt_pip2_launch_app(cd->dev,
  9109. PT_CORE_CMD_UNPROTECTED);
  9110. if (rc) {
  9111. pt_debug(cd->dev, DL_ERROR,
  9112. "%s: Error on launch app r=%d\n",
  9113. __func__, rc);
  9114. msleep(50);
  9115. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9116. goto reset;
  9117. goto exit;
  9118. }
  9119. /*
  9120. * IRQ thread can be delayed if the serial log print is
  9121. * enabled. It causes next command to get wrong response
  9122. * Here the delay is to ensure pt_parse_input() to be
  9123. * finished.
  9124. */
  9125. msleep(60);
  9126. /* Check and update the mode */
  9127. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9128. if (rc) {
  9129. pt_debug(cd->dev, DL_ERROR,
  9130. "%s: Get mode failed, mode unknown\n",
  9131. __func__);
  9132. mode = PT_MODE_UNKNOWN;
  9133. }
  9134. cd->mode = mode;
  9135. if (cd->mode == PT_MODE_OPERATIONAL) {
  9136. pt_debug(cd->dev, DL_INFO,
  9137. "%s: Launched to Operational mode\n",
  9138. __func__);
  9139. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9140. pt_debug(cd->dev, DL_ERROR,
  9141. "%s: Launch failed, Bootloader mode\n",
  9142. __func__);
  9143. goto exit;
  9144. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9145. pt_debug(cd->dev, DL_ERROR,
  9146. "%s: Launch failed, Unknown mode\n",
  9147. __func__);
  9148. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9149. goto reset;
  9150. goto exit;
  9151. }
  9152. if (cd->app_pip_ver_ready == false) {
  9153. rc = pt_pip2_get_version_(cd);
  9154. if (!rc)
  9155. cd->app_pip_ver_ready = true;
  9156. else {
  9157. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9158. goto reset;
  9159. goto exit;
  9160. }
  9161. }
  9162. break;
  9163. default:
  9164. pt_debug(cd->dev, DL_ERROR,
  9165. "%s: Unknown mode\n", __func__);
  9166. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9167. goto reset;
  9168. goto exit;
  9169. }
  9170. *enum_status |= STARTUP_STATUS_GET_DESC;
  9171. }
  9172. pt_init_pip_report_fields(cd);
  9173. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9174. if (!cd->features.easywake)
  9175. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9176. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9177. rc = pt_hid_output_get_sysinfo_(cd);
  9178. if (rc) {
  9179. pt_debug(cd->dev, DL_ERROR,
  9180. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9181. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9182. goto reset;
  9183. goto exit;
  9184. }
  9185. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9186. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9187. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9188. if (!rc) {
  9189. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9190. pt_debug(cd->dev, DL_WARN,
  9191. "%s: Exit FW BOOT Mode after %dms\n",
  9192. __func__, wait_time);
  9193. } else {
  9194. pt_debug(cd->dev, DL_WARN,
  9195. "%s: FW stuck in BOOT Mode after %dms\n",
  9196. __func__, wait_time);
  9197. goto exit;
  9198. }
  9199. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9200. __func__,
  9201. cd->sysinfo.ttdata.pip_ver_major,
  9202. cd->sysinfo.ttdata.pip_ver_minor);
  9203. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9204. if (rc) {
  9205. pt_debug(cd->dev, DL_ERROR,
  9206. "%s: DUT Config block CRC failure rc=%d\n",
  9207. __func__, rc);
  9208. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9209. goto reset;
  9210. } else {
  9211. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9212. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9213. pt_debug(cd->dev, DL_ERROR,
  9214. "%s: scan state: %d, retry: %d\n",
  9215. __func__, sys_mode, try);
  9216. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9217. goto reset;
  9218. } else
  9219. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9220. }
  9221. rc = pt_restore_parameters_(cd);
  9222. if (rc) {
  9223. pt_debug(cd->dev, DL_ERROR,
  9224. "%s: Failed to restore parameters rc=%d\n",
  9225. __func__, rc);
  9226. } else
  9227. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9228. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9229. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9230. cd->startup_retry_count = 0;
  9231. exit:
  9232. /* Generate the HW Version of the connected DUT and store in cd */
  9233. pt_generate_hw_version(cd, cd->hw_version);
  9234. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9235. cd->hw_version);
  9236. pt_start_wd_timer(cd);
  9237. if (!detected)
  9238. rc = -ENODEV;
  9239. #ifdef TTHE_TUNER_SUPPORT
  9240. tthe_print(cd, NULL, 0, "exit startup");
  9241. #endif
  9242. pt_debug(cd->dev, DL_WARN,
  9243. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9244. __func__, rc, try);
  9245. return rc;
  9246. }
  9247. /*******************************************************************************
  9248. * FUNCTION: pt_enum_with_dut
  9249. *
  9250. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9251. * requesting exclusive access around the call.
  9252. *
  9253. * RETURN:
  9254. * 0 = success
  9255. * !0 = failure
  9256. *
  9257. * PARAMETERS:
  9258. * *cd - pointer the core data structure
  9259. * reset - Flag to reset the DUT before attempting to enumerate
  9260. * *status - pointer to store the enum status bitmask flags
  9261. ******************************************************************************/
  9262. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9263. {
  9264. int rc = 0;
  9265. mutex_lock(&cd->system_lock);
  9266. cd->startup_state = STARTUP_RUNNING;
  9267. mutex_unlock(&cd->system_lock);
  9268. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9269. if (rc) {
  9270. pt_debug(cd->dev, DL_ERROR,
  9271. "%s: fail get exclusive ex=%p own=%p\n",
  9272. __func__, cd->exclusive_dev, cd->dev);
  9273. goto exit;
  9274. }
  9275. rc = pt_enum_with_dut_(cd, reset, status);
  9276. if (release_exclusive(cd, cd->dev) < 0)
  9277. /* Don't return fail code, mode is already changed. */
  9278. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9279. __func__);
  9280. else
  9281. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9282. __func__);
  9283. exit:
  9284. mutex_lock(&cd->system_lock);
  9285. /* Clear startup state for any tasks waiting for startup completion */
  9286. cd->startup_state = STARTUP_NONE;
  9287. mutex_unlock(&cd->system_lock);
  9288. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9289. *status |= STARTUP_STATUS_COMPLETE;
  9290. /* Wake the waiters for end of startup */
  9291. wake_up(&cd->wait_q);
  9292. return rc;
  9293. }
  9294. static int add_sysfs_interfaces(struct device *dev);
  9295. static void remove_sysfs_interfaces(struct device *dev);
  9296. static void remove_sysfs_and_modules(struct device *dev);
  9297. static void pt_release_modules(struct pt_core_data *cd);
  9298. static void pt_probe_modules(struct pt_core_data *cd);
  9299. /*******************************************************************************
  9300. * FUNCTION: _pt_ttdl_restart
  9301. *
  9302. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9303. *
  9304. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9305. * existing sysfs nodes will produce an error.
  9306. *
  9307. * RETURN:
  9308. * 0 = success
  9309. * !0 = failure
  9310. *
  9311. * PARAMETERS:
  9312. * *dev - pointer to core device
  9313. ******************************************************************************/
  9314. static int _pt_ttdl_restart(struct device *dev)
  9315. {
  9316. int rc = 0;
  9317. struct pt_core_data *cd = dev_get_drvdata(dev);
  9318. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9319. struct i2c_client *client =
  9320. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9321. #endif
  9322. /*
  9323. * Make sure the device is awake, pt_mt_release function will
  9324. * cause pm sleep function and lead to deadlock.
  9325. */
  9326. pm_runtime_get_sync(dev);
  9327. /* Use ttdl_restart_lock to avoid reentry */
  9328. mutex_lock(&cd->ttdl_restart_lock);
  9329. remove_sysfs_and_modules(cd->dev);
  9330. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9331. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9332. pt_debug(dev, DL_ERROR,
  9333. "%s I2C functionality not Supported\n", __func__);
  9334. rc = -EIO;
  9335. goto ttdl_no_error;
  9336. }
  9337. #endif
  9338. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9339. rc = _pt_detect_dut_generation(cd->dev,
  9340. &cd->startup_status, &cd->active_dut_generation,
  9341. &cd->mode);
  9342. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9343. pt_debug(dev, DL_ERROR,
  9344. "%s: Error, Unknown DUT Generation rc=%d\n",
  9345. __func__, rc);
  9346. }
  9347. }
  9348. rc = add_sysfs_interfaces(cd->dev);
  9349. if (rc < 0)
  9350. pt_debug(cd->dev, DL_ERROR,
  9351. "%s: Error, failed sysfs nodes rc=%d\n",
  9352. __func__, rc);
  9353. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9354. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9355. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9356. if (rc < 0)
  9357. pt_debug(dev, DL_ERROR,
  9358. "%s: Error, Failed to Enumerate\n", __func__);
  9359. }
  9360. rc = pt_mt_probe(dev);
  9361. if (rc < 0) {
  9362. pt_debug(dev, DL_ERROR,
  9363. "%s: Error, fail mt probe\n", __func__);
  9364. }
  9365. rc = pt_btn_probe(dev);
  9366. if (rc < 0) {
  9367. pt_debug(dev, DL_ERROR,
  9368. "%s: Error, fail btn probe\n", __func__);
  9369. }
  9370. pt_probe_modules(cd);
  9371. pt_debug(cd->dev, DL_WARN,
  9372. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9373. rc = 0;
  9374. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9375. ttdl_no_error:
  9376. #endif
  9377. mutex_unlock(&cd->ttdl_restart_lock);
  9378. mutex_lock(&cd->system_lock);
  9379. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9380. cd->startup_state = STARTUP_NONE;
  9381. mutex_unlock(&cd->system_lock);
  9382. pm_runtime_put(dev);
  9383. return rc;
  9384. }
  9385. /*******************************************************************************
  9386. * FUNCTION: pt_restart_work_function
  9387. *
  9388. * SUMMARY: This is the wrapper function placed in a work queue to call
  9389. * _pt_ttdl_restart()
  9390. *
  9391. * RETURN: none
  9392. *
  9393. * PARAMETERS:
  9394. * *work - pointer to the work_struct
  9395. ******************************************************************************/
  9396. static void pt_restart_work_function(struct work_struct *work)
  9397. {
  9398. struct pt_core_data *cd = container_of(work,
  9399. struct pt_core_data, ttdl_restart_work);
  9400. int rc = 0;
  9401. rc = _pt_ttdl_restart(cd->dev);
  9402. if (rc < 0)
  9403. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9404. __func__, rc);
  9405. }
  9406. /*******************************************************************************
  9407. * FUNCTION: pt_enum_work_function
  9408. *
  9409. * SUMMARY: This is the wrapper function placed in a work queue to call
  9410. * pt_enum_with_dut()
  9411. *
  9412. * RETURN: none
  9413. *
  9414. * PARAMETERS:
  9415. * *work - pointer to the work_struct
  9416. ******************************************************************************/
  9417. static void pt_enum_work_function(struct work_struct *work)
  9418. {
  9419. struct pt_core_data *cd = container_of(work,
  9420. struct pt_core_data, enum_work);
  9421. int rc;
  9422. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9423. if (rc < 0)
  9424. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9425. __func__, rc);
  9426. }
  9427. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9428. {
  9429. int rc;
  9430. if (!get) {
  9431. rc = 0;
  9432. goto regulator_put;
  9433. }
  9434. cd->vdd = regulator_get(cd->dev, "vdd");
  9435. if (IS_ERR(cd->vdd)) {
  9436. rc = PTR_ERR(cd->vdd);
  9437. dev_err(cd->dev,
  9438. "Regulator get failed vdd rc=%d\n", rc);
  9439. goto regulator_put;
  9440. }
  9441. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9442. if (IS_ERR(cd->vcc_i2c)) {
  9443. rc = PTR_ERR(cd->vcc_i2c);
  9444. dev_err(cd->dev,
  9445. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9446. goto regulator_put;
  9447. }
  9448. return 0;
  9449. regulator_put:
  9450. if (cd->vdd) {
  9451. regulator_put(cd->vdd);
  9452. cd->vdd = NULL;
  9453. }
  9454. if (cd->vcc_i2c) {
  9455. regulator_put(cd->vcc_i2c);
  9456. cd->vcc_i2c = NULL;
  9457. }
  9458. return rc;
  9459. }
  9460. static int pt_enable_vdd_regulator(struct pt_core_data *cd, bool en)
  9461. {
  9462. int rc;
  9463. if (!en) {
  9464. rc = 0;
  9465. goto disable_vdd_reg;
  9466. }
  9467. if (cd->vdd) {
  9468. if (regulator_count_voltages(cd->vdd) > 0) {
  9469. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9470. FT_VTG_MAX_UV);
  9471. if (rc) {
  9472. dev_err(cd->dev,
  9473. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9474. goto exit;
  9475. }
  9476. }
  9477. rc = regulator_enable(cd->vdd);
  9478. if (rc) {
  9479. dev_err(cd->dev,
  9480. "Regulator vdd enable failed rc=%d\n", rc);
  9481. goto exit;
  9482. }
  9483. }
  9484. return 0;
  9485. disable_vdd_reg:
  9486. if (cd->vdd) {
  9487. if (regulator_count_voltages(cd->vdd) > 0)
  9488. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9489. FT_VTG_MAX_UV);
  9490. regulator_disable(cd->vdd);
  9491. }
  9492. exit:
  9493. return rc;
  9494. }
  9495. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9496. {
  9497. int rc;
  9498. if (!en) {
  9499. rc = 0;
  9500. goto disable_vcc_i2c_reg;
  9501. }
  9502. if (cd->vdd) {
  9503. if (regulator_count_voltages(cd->vdd) > 0) {
  9504. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9505. FT_VTG_MAX_UV);
  9506. if (rc) {
  9507. dev_err(cd->dev,
  9508. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9509. goto exit;
  9510. }
  9511. }
  9512. rc = regulator_enable(cd->vdd);
  9513. if (rc) {
  9514. dev_err(cd->dev,
  9515. "Regulator vdd enable failed rc=%d\n", rc);
  9516. goto exit;
  9517. }
  9518. }
  9519. if (cd->vcc_i2c) {
  9520. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9521. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9522. FT_I2C_VTG_MAX_UV);
  9523. if (rc) {
  9524. dev_err(cd->dev,
  9525. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9526. goto disable_vdd_reg;
  9527. }
  9528. }
  9529. rc = regulator_enable(cd->vcc_i2c);
  9530. if (rc) {
  9531. dev_err(cd->dev,
  9532. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9533. goto disable_vdd_reg;
  9534. }
  9535. }
  9536. return 0;
  9537. disable_vcc_i2c_reg:
  9538. if (cd->vcc_i2c) {
  9539. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9540. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9541. FT_I2C_VTG_MAX_UV);
  9542. regulator_disable(cd->vcc_i2c);
  9543. }
  9544. disable_vdd_reg:
  9545. if (cd->vdd) {
  9546. if (regulator_count_voltages(cd->vdd) > 0)
  9547. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9548. FT_VTG_MAX_UV);
  9549. regulator_disable(cd->vdd);
  9550. }
  9551. exit:
  9552. return rc;
  9553. }
  9554. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9555. #define KERNEL_VER_GT_3_19
  9556. #endif
  9557. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9558. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9559. #if defined(CONFIG_PM_SLEEP)
  9560. /*******************************************************************************
  9561. * FUNCTION: pt_core_rt_suspend
  9562. *
  9563. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9564. *
  9565. * RETURN:
  9566. * 0 = success
  9567. * !0 = failure
  9568. *
  9569. * PARAMETERS:
  9570. * *dev - pointer to core device
  9571. ******************************************************************************/
  9572. static int pt_core_rt_suspend(struct device *dev)
  9573. {
  9574. struct pt_core_data *cd = dev_get_drvdata(dev);
  9575. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9576. __func__, cd->core_probe_complete);
  9577. return 0;
  9578. }
  9579. /*******************************************************************************
  9580. * FUNCTION: pt_core_rt_resume
  9581. *
  9582. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9583. *
  9584. * RETURN:
  9585. * 0 = success
  9586. * !0 = failure
  9587. *
  9588. * PARAMETERS:
  9589. * *dev - pointer to core device
  9590. ******************************************************************************/
  9591. static int pt_core_rt_resume(struct device *dev)
  9592. {
  9593. struct pt_core_data *cd = dev_get_drvdata(dev);
  9594. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9595. __func__, cd->core_probe_complete);
  9596. return 0;
  9597. }
  9598. #endif /* CONFIG_PM_SLEEP */
  9599. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9600. #if defined(CONFIG_PM_SLEEP)
  9601. /*******************************************************************************
  9602. * FUNCTION: pt_core_suspend_
  9603. *
  9604. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9605. * pt_core_sleep. This function may disable IRQ during sleep state.
  9606. *
  9607. * RETURN:
  9608. * 0 = success
  9609. * !0 = failure
  9610. *
  9611. * PARAMETERS:
  9612. * *dev - pointer to core device
  9613. ******************************************************************************/
  9614. static int pt_core_suspend_(struct device *dev)
  9615. {
  9616. int rc;
  9617. struct pt_core_data *cd = dev_get_drvdata(dev);
  9618. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9619. __func__);
  9620. rc = pt_core_sleep(cd);
  9621. if (rc) {
  9622. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9623. return -EAGAIN;
  9624. }
  9625. rc = pt_enable_vdd_regulator(cd, false);
  9626. if (rc) {
  9627. dev_err(dev, "%s: Failed to disable vdd regulators: rc=%d\n",
  9628. __func__, rc);
  9629. }
  9630. rc = pt_lpm_regulator(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9631. if (rc) {
  9632. dev_err(dev, "%s: Failed to enter to lpm mode rc=%d\n",
  9633. __func__, rc);
  9634. }
  9635. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9636. return 0;
  9637. /* Required to prevent interrupts before bus awake */
  9638. disable_irq(cd->irq);
  9639. cd->irq_disabled = 1;
  9640. if (device_may_wakeup(dev)) {
  9641. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9642. __func__);
  9643. if (!enable_irq_wake(cd->irq))
  9644. cd->irq_wake = 1;
  9645. } else {
  9646. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9647. __func__);
  9648. }
  9649. return rc;
  9650. }
  9651. static int pt_lpm_regulator(struct regulator *reg, int load_uA)
  9652. {
  9653. return (regulator_count_voltages(reg) > 0) ?
  9654. regulator_set_load(reg, load_uA) : 0;
  9655. }
  9656. /*******************************************************************************
  9657. * FUNCTION: pt_core_suspend
  9658. *
  9659. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9660. * woke up or put to sleep based on Kernel power state even when the display
  9661. * is off based on the check of TTDL core platform flag.
  9662. *
  9663. * RETURN:
  9664. * 0 = success
  9665. * !0 = failure
  9666. *
  9667. * PARAMETERS:
  9668. * *dev - pointer to core device
  9669. ******************************************************************************/
  9670. static int pt_core_suspend(struct device *dev)
  9671. {
  9672. struct pt_core_data *cd = dev_get_drvdata(dev);
  9673. int rc = 0;
  9674. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9675. return 0;
  9676. rc = pt_core_suspend_(dev);
  9677. pt_debug(dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9678. return 0;
  9679. }
  9680. /*******************************************************************************
  9681. * FUNCTION: pt_core_resume_
  9682. *
  9683. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9684. * pt_core_wake. This function may enable IRQ before wake up.
  9685. *
  9686. * RETURN:
  9687. * 0 = success
  9688. * !0 = failure
  9689. *
  9690. * PARAMETERS:
  9691. * *dev - pointer to core device
  9692. ******************************************************************************/
  9693. static int pt_core_resume_(struct device *dev)
  9694. {
  9695. int rc = 0;
  9696. struct pt_core_data *cd = dev_get_drvdata(dev);
  9697. dev_info(dev, "%s: Entering into resume mode:\n",
  9698. __func__);
  9699. rc = pt_lpm_regulator(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9700. if (rc < 0) {
  9701. dev_err(dev, "%s: Failed to exit lpm mode: rc=%d\n",
  9702. __func__, rc);
  9703. }
  9704. rc = pt_enable_vdd_regulator(cd, true);
  9705. if (rc < 0) {
  9706. dev_err(dev, "%s: Failed to enable vdd regulators: rc=%d\n",
  9707. __func__, rc);
  9708. }
  9709. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9710. __func__, rc);
  9711. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9712. goto exit;
  9713. /*
  9714. * Bus pm does not call suspend if device runtime suspended
  9715. * This flag is covers that case
  9716. */
  9717. if (cd->irq_disabled) {
  9718. enable_irq(cd->irq);
  9719. cd->irq_disabled = 0;
  9720. }
  9721. if (device_may_wakeup(dev)) {
  9722. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9723. __func__);
  9724. if (cd->irq_wake) {
  9725. disable_irq_wake(cd->irq);
  9726. cd->irq_wake = 0;
  9727. }
  9728. } else {
  9729. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9730. __func__);
  9731. }
  9732. exit:
  9733. rc = pt_core_wake(cd);
  9734. if (rc) {
  9735. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9736. __func__, rc);
  9737. return -EAGAIN;
  9738. }
  9739. return rc;
  9740. }
  9741. /*******************************************************************************
  9742. * FUNCTION: resume_offload_work
  9743. *
  9744. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9745. * along with kernel power state even the display is off based on the check of
  9746. * TTDL core platform flag.
  9747. *
  9748. * RETURN:
  9749. * 0 = success
  9750. * !0 = failure
  9751. *
  9752. * PARAMETERS:
  9753. * *dev - pointer to core device
  9754. ******************************************************************************/
  9755. static void pt_resume_offload_work(struct work_struct *work)
  9756. {
  9757. int rc = 0;
  9758. int retry_count = 10;
  9759. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9760. resume_offload_work);
  9761. do {
  9762. retry_count--;
  9763. rc = pt_core_resume_(pt_data->dev);
  9764. if (rc < 0)
  9765. pt_debug(pt_data->dev, DL_ERROR,
  9766. "%s: Error on wake\n", __func__);
  9767. } while (retry_count && rc < 0);
  9768. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9769. rc = pt_core_easywake_on(pt_data);
  9770. if (rc < 0) {
  9771. pt_debug(pt_data->dev, DL_ERROR,
  9772. "%s: Error on enable touch to wake key\n",
  9773. __func__);
  9774. return;
  9775. }
  9776. pt_data->fb_state = FB_OFF;
  9777. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9778. #endif
  9779. }
  9780. /*******************************************************************************
  9781. * FUNCTION: pt_core_resume
  9782. *
  9783. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9784. * along with kernel power state even the display is off based on the check of
  9785. * TTDL core platform flag.
  9786. *
  9787. * RETURN:
  9788. * 0 = success
  9789. * !0 = failure
  9790. *
  9791. * PARAMETERS:
  9792. * *dev - pointer to core device
  9793. ******************************************************************************/
  9794. static int pt_core_resume(struct device *dev)
  9795. {
  9796. struct pt_core_data *cd = dev_get_drvdata(dev);
  9797. int rc = 0;
  9798. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9799. return 0;
  9800. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9801. pt_debug(cd->dev, DL_ERROR, "%s End\n", __func__);
  9802. return rc;
  9803. }
  9804. #endif
  9805. #ifdef NEED_SUSPEND_NOTIFIER
  9806. /*******************************************************************************
  9807. * FUNCTION: pt_pm_notifier
  9808. *
  9809. * SUMMARY: This function is registered to notifier chain and will perform
  9810. * suspend operation if match event PM_SUSPEND_PREPARE.
  9811. *
  9812. * RETURN:
  9813. * 0 = success
  9814. * !0 = failure
  9815. *
  9816. * PARAMETERS:
  9817. * *nb - pointer to notifier_block structure
  9818. * action - notifier event type
  9819. * *data - void pointer
  9820. ******************************************************************************/
  9821. static int pt_pm_notifier(struct notifier_block *nb,
  9822. unsigned long action, void *data)
  9823. {
  9824. struct pt_core_data *cd = container_of(nb,
  9825. struct pt_core_data, pm_notifier);
  9826. if (action == PM_SUSPEND_PREPARE) {
  9827. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9828. __func__);
  9829. /*
  9830. * If PM runtime is not suspended, either call runtime
  9831. * PM suspend callback or wait until it finishes
  9832. */
  9833. if (!pm_runtime_suspended(cd->dev))
  9834. pm_runtime_suspend(cd->dev);
  9835. (void) pt_core_suspend(cd->dev);
  9836. }
  9837. return NOTIFY_DONE;
  9838. }
  9839. #endif
  9840. const struct dev_pm_ops pt_pm_ops = {
  9841. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9842. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9843. NULL)
  9844. };
  9845. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9846. /*******************************************************************************
  9847. * FUNCTION: _pt_request_pip2_enter_bl
  9848. *
  9849. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9850. * XRES pin or a soft reset.
  9851. *
  9852. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9853. * the WD active could cause this function to fail!
  9854. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9855. * will not try to determine the current mode but will proceed with
  9856. * resetting the DUT and entering the BL.
  9857. *
  9858. * NOTE: The definition of result code:
  9859. * PT_ENTER_BL_PASS (0)
  9860. * PT_ENTER_BL_ERROR (1)
  9861. * PT_ENTER_BL_RESET_FAIL (2)
  9862. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9863. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9864. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9865. *
  9866. * RETURNS:
  9867. * 0 = success
  9868. * !0 = failure
  9869. *
  9870. *
  9871. * PARAMETERS:
  9872. * *dev - pointer to device structure
  9873. * *start_mode - pointer to the mode the DUT was in when this function
  9874. * starts
  9875. * *result - pointer to store the result when to enter BL
  9876. ******************************************************************************/
  9877. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9878. {
  9879. int rc = 0;
  9880. int t;
  9881. int tmp_result = PT_ENTER_BL_ERROR;
  9882. int flash_info_retry = 2;
  9883. u8 mode = PT_MODE_UNKNOWN;
  9884. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9885. u8 read_buf[32];
  9886. u16 actual_read_len;
  9887. struct pt_core_data *cd = dev_get_drvdata(dev);
  9888. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9889. u8 time = 0;
  9890. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9891. if (cd->watchdog_enabled) {
  9892. pt_debug(dev, DL_WARN,
  9893. "%s: Watchdog must be stopped before entering BL\n",
  9894. __func__);
  9895. goto exit;
  9896. }
  9897. cancel_work_sync(&cd->enum_work);
  9898. cancel_work_sync(&cd->watchdog_work);
  9899. /* if undefined assume operational/test to bypass all checks */
  9900. if (*start_mode == PT_MODE_IGNORE) {
  9901. mode = PT_MODE_OPERATIONAL;
  9902. sys_mode = FW_SYS_MODE_TEST;
  9903. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9904. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9905. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9906. if (rc) {
  9907. pt_debug(dev, DL_ERROR,
  9908. "%s: Get mode failed, mode unknown\n",
  9909. __func__);
  9910. }
  9911. *start_mode = mode;
  9912. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9913. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9914. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9915. sys_mode = FW_SYS_MODE_SCANNING;
  9916. }
  9917. _retry:
  9918. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9919. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9920. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9921. switch (mode) {
  9922. case PT_MODE_UNKNOWN:
  9923. /*
  9924. * When the mode could not be determined the DUT could be
  9925. * in App mode running corrupted FW or FW that is not
  9926. * responding to the mode request, assume no communication
  9927. * and do a hard reset
  9928. */
  9929. mutex_lock(&cd->system_lock);
  9930. cd->startup_status = STARTUP_STATUS_START;
  9931. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9932. mutex_unlock(&cd->system_lock);
  9933. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9934. if (rc) {
  9935. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9936. goto exit;
  9937. }
  9938. break;
  9939. case PT_MODE_OPERATIONAL:
  9940. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9941. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9942. __func__);
  9943. rc = pt_pip_suspend_scanning_(cd);
  9944. if (rc) {
  9945. /*
  9946. * Print to log but don't exit, the FW could be
  9947. * running but be hung or fail to respond to
  9948. * this request
  9949. */
  9950. pt_debug(dev, DL_ERROR,
  9951. "%s Suspend Scan Failed\n", __func__);
  9952. }
  9953. /* sleep to allow the suspend scan to be processed */
  9954. usleep_range(1000, 2000);
  9955. }
  9956. mutex_lock(&cd->system_lock);
  9957. cd->startup_status = STARTUP_STATUS_START;
  9958. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9959. mutex_unlock(&cd->system_lock);
  9960. /* Reset device to enter the BL */
  9961. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9962. if (rc) {
  9963. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9964. goto exit;
  9965. }
  9966. break;
  9967. case PT_MODE_BOOTLOADER:
  9968. /* Do nothing as we are already in the BL */
  9969. tmp_result = PT_ENTER_BL_PASS;
  9970. goto exit;
  9971. default:
  9972. /* Should NEVER get here */
  9973. tmp_result = PT_ENTER_BL_ERROR;
  9974. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9975. goto exit;
  9976. }
  9977. if (!cd->flashless_dut &&
  9978. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9979. /*
  9980. * Sending the special "Host Mode" command will instruct the
  9981. * BL to not execute the FW it has loaded into RAM.
  9982. * The command must be sent within a 40ms window from releasing
  9983. * the XRES pin. If the messages is sent too early it will NAK,
  9984. * so keep sending it every 2ms until it is accepted by the BL.
  9985. * A no-flash DUT does not require this command as there is no
  9986. * FW for the BL to load and execute.
  9987. */
  9988. usleep_range(4000, 6000);
  9989. pt_debug(cd->dev, DL_INFO,
  9990. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9991. __func__, (int)sizeof(host_mode_cmd));
  9992. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9993. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9994. rc = 1;
  9995. while (rc && time < 34) {
  9996. rc = pt_adap_write_read_specific(cd, 4,
  9997. host_mode_cmd, NULL);
  9998. usleep_range(1800, 2000);
  9999. time += 2;
  10000. }
  10001. /* Sleep to allow the BL to come up */
  10002. usleep_range(1000, 2000);
  10003. }
  10004. /*
  10005. * To avoid the case that next PIP command can be confused by BL/FW
  10006. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10007. * followed by "wakeup event" function can lower the failure rate.
  10008. */
  10009. t = wait_event_timeout(cd->wait_q,
  10010. ((cd->startup_status != STARTUP_STATUS_START)
  10011. && (cd->hid_reset_cmd_state == 0)),
  10012. msecs_to_jiffies(300));
  10013. if (IS_TMO(t)) {
  10014. pt_debug(cd->dev, DL_ERROR,
  10015. "%s: TMO waiting for BL sentinel\n", __func__);
  10016. }
  10017. /* Check if device is now in BL mode */
  10018. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10019. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10020. cd->startup_status);
  10021. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10022. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10023. mutex_lock(&cd->system_lock);
  10024. cd->pip2_prot_active = true;
  10025. cd->mode = PT_MODE_BOOTLOADER;
  10026. mutex_unlock(&cd->system_lock);
  10027. tmp_result = PT_ENTER_BL_PASS;
  10028. } else {
  10029. /*
  10030. * If the device doesn't enter BL mode as expected and rc is
  10031. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10032. * the function should return an error code to indicate this
  10033. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10034. */
  10035. if (!rc)
  10036. rc = -EINVAL;
  10037. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10038. mutex_lock(&cd->system_lock);
  10039. cd->mode = mode;
  10040. mutex_unlock(&cd->system_lock);
  10041. pt_debug(dev, DL_ERROR,
  10042. "%s ERROR: Not in BL as expected", __func__);
  10043. }
  10044. exit:
  10045. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10046. /* Check to get (buffered) flash information */
  10047. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10048. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10049. read_buf, &actual_read_len);
  10050. if (!rc) {
  10051. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10052. pt_debug(
  10053. dev, DL_WARN,
  10054. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10055. __func__,
  10056. read_buf[PIP2_RESP_BODY_OFFSET]);
  10057. /*
  10058. * If the BL was unable to cache the correct
  10059. * values when entering the first time due to
  10060. * the Flash part not having been powered up
  10061. * long enough, re-enter the BL to trigger the
  10062. * BL to re-attempt to cache the values.
  10063. */
  10064. if (flash_info_retry-- > 0) {
  10065. mode = PT_MODE_UNKNOWN;
  10066. pt_debug(dev, DL_WARN,
  10067. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10068. __func__, flash_info_retry);
  10069. goto _retry;
  10070. } else {
  10071. pt_debug(dev, DL_WARN,
  10072. "%s Manufacturer ID Unknown\n",
  10073. __func__);
  10074. tmp_result = PT_ENTER_BL_PASS;
  10075. }
  10076. }
  10077. } else {
  10078. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10079. pt_debug(
  10080. dev, DL_ERROR,
  10081. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10082. __func__);
  10083. }
  10084. }
  10085. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10086. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10087. "SUPPRESS");
  10088. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10089. if (result)
  10090. *result = tmp_result;
  10091. return rc;
  10092. }
  10093. /*******************************************************************************
  10094. * FUNCTION: _pt_pip2_file_open
  10095. *
  10096. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10097. *
  10098. * NOTE: The DUT must be in BL mode for this command to work
  10099. *
  10100. * RETURNS:
  10101. * <0 = Error
  10102. * >0 = file handle opened
  10103. *
  10104. * PARAMETERS:
  10105. * *dev - pointer to device structure
  10106. * file_no - PIP2 file number to open
  10107. ******************************************************************************/
  10108. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10109. {
  10110. int ret = 0;
  10111. u16 status;
  10112. u16 actual_read_len;
  10113. u8 file_handle;
  10114. u8 data[2];
  10115. u8 read_buf[10];
  10116. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10117. data[0] = file_no;
  10118. ret = _pt_request_pip2_send_cmd(dev,
  10119. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10120. data, 1, read_buf, &actual_read_len);
  10121. if (ret) {
  10122. pt_debug(dev, DL_ERROR,
  10123. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10124. __func__, file_no);
  10125. return -PIP2_RSP_ERR_NOT_OPEN;
  10126. }
  10127. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10128. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10129. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10130. (file_handle != file_no)) {
  10131. pt_debug(dev, DL_ERROR,
  10132. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10133. __func__, status, file_handle);
  10134. return -status;
  10135. }
  10136. return file_handle;
  10137. }
  10138. /*******************************************************************************
  10139. * FUNCTION: _pt_pip2_file_close
  10140. *
  10141. * SUMMARY: Using the BL PIP2 commands close a file
  10142. *
  10143. * NOTE: The DUT must be in BL mode for this command to work
  10144. *
  10145. * RETURNS:
  10146. * <0 = Error
  10147. * >0 = file handle closed
  10148. *
  10149. * PARAMETERS:
  10150. * *dev - pointer to device structure
  10151. * file_handle - handle to the file to be closed
  10152. ******************************************************************************/
  10153. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10154. {
  10155. int ret = 0;
  10156. u16 status;
  10157. u16 actual_read_len;
  10158. u8 data[2];
  10159. u8 read_buf[32];
  10160. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10161. data[0] = file_handle;
  10162. ret = _pt_request_pip2_send_cmd(dev,
  10163. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10164. data, 1, read_buf, &actual_read_len);
  10165. if (ret) {
  10166. pt_debug(dev, DL_ERROR,
  10167. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10168. __func__, file_handle);
  10169. return -ETIME;
  10170. }
  10171. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10172. if (status != 0x00) {
  10173. pt_debug(dev, DL_ERROR,
  10174. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10175. __func__, status, file_handle);
  10176. return -status;
  10177. }
  10178. return file_handle;
  10179. }
  10180. /*******************************************************************************
  10181. * FUNCTION: _pt_pip2_file_erase
  10182. *
  10183. * SUMMARY: Using the BL PIP2 commands erase a file
  10184. *
  10185. * NOTE: The DUT must be in BL mode for this command to work
  10186. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10187. * one retry is attempted for each sector in a file.
  10188. *
  10189. * RETURNS:
  10190. * <0 = Error
  10191. * >0 = file handle closed
  10192. *
  10193. * PARAMETERS:
  10194. * *dev - pointer to device structure
  10195. * file_handle - handle to the file to be erased
  10196. * *status - PIP2 erase status code
  10197. ******************************************************************************/
  10198. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10199. {
  10200. int ret = 0;
  10201. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10202. int retry = 1;
  10203. u16 actual_read_len;
  10204. u8 data[2];
  10205. u8 read_buf[10];
  10206. struct pt_core_data *cd = dev_get_drvdata(dev);
  10207. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10208. data[0] = file_handle;
  10209. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10210. *status = PIP2_RSP_ERR_TIMEOUT;
  10211. /* Increase waiting time for large file erase */
  10212. mutex_lock(&cd->system_lock);
  10213. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10214. mutex_unlock(&cd->system_lock);
  10215. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10216. ret = _pt_request_pip2_send_cmd(dev,
  10217. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10218. data, 2, read_buf, &actual_read_len);
  10219. if (ret)
  10220. break;
  10221. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10222. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10223. #ifdef TTDL_DIAGNOSTICS
  10224. cd->file_erase_timeout_count++;
  10225. #endif
  10226. pt_debug(dev, DL_WARN,
  10227. "%s: ERASE timeout %d for file = %d\n",
  10228. __func__, retry, file_handle);
  10229. }
  10230. retry++;
  10231. }
  10232. mutex_lock(&cd->system_lock);
  10233. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10234. mutex_unlock(&cd->system_lock);
  10235. if (ret) {
  10236. pt_debug(dev, DL_ERROR,
  10237. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10238. __func__, ret, file_handle);
  10239. return -EIO;
  10240. }
  10241. if (*status != 0x00) {
  10242. pt_debug(dev, DL_ERROR,
  10243. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10244. __func__, *status, file_handle);
  10245. return -EIO;
  10246. }
  10247. return file_handle;
  10248. }
  10249. /*******************************************************************************
  10250. * FUNCTION: _pt_pip2_file_read
  10251. *
  10252. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10253. *
  10254. * NOTE: The DUT must be in BL mode for this command to work
  10255. *
  10256. * RETURNS:
  10257. * <0 = Error
  10258. * >0 = number of bytes read
  10259. *
  10260. * PARAMETERS:
  10261. * *dev - pointer to device structure
  10262. * file_handle - File handle to read from
  10263. * num_bytes - number of bytes to read
  10264. ******************************************************************************/
  10265. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10266. u8 *read_buf)
  10267. {
  10268. int ret = 0;
  10269. u16 status;
  10270. u16 actual_read_len;
  10271. u8 data[3];
  10272. data[0] = file_handle;
  10273. data[1] = (num_bytes & 0x00FF);
  10274. data[2] = (num_bytes & 0xFF00) >> 8;
  10275. ret = _pt_request_pip2_send_cmd(dev,
  10276. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10277. data, 3, read_buf, &actual_read_len);
  10278. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10279. if (ret || ((status != 0x00) && (status != 0x03))) {
  10280. pt_debug(dev, DL_ERROR,
  10281. "%s File open failure with error code = %d\n",
  10282. __func__, status);
  10283. return -EPERM;
  10284. }
  10285. ret = num_bytes;
  10286. return ret;
  10287. }
  10288. /*******************************************************************************
  10289. * FUNCTION: _pt_read_us_file
  10290. *
  10291. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10292. * then read the entire file.
  10293. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10294. *
  10295. * RETURN:
  10296. * 0 = success
  10297. * !0 = failure
  10298. *
  10299. * PARAMETERS:
  10300. * *file_path - pointer to the file path
  10301. * *buf - pointer to the buffer to store the file contents
  10302. * *size - pointer to the size of the file
  10303. ******************************************************************************/
  10304. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10305. {
  10306. struct file *filp = NULL;
  10307. struct inode *inode = NULL;
  10308. unsigned int file_len = 0;
  10309. unsigned int read_len = 0;
  10310. mm_segment_t oldfs;
  10311. int rc = 0;
  10312. if (file_path == NULL || buf == NULL) {
  10313. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10314. return -EINVAL;
  10315. }
  10316. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10317. oldfs = get_fs();
  10318. set_fs(KERNEL_DS);
  10319. filp = filp_open(file_path, O_RDONLY, 0400);
  10320. if (IS_ERR(filp)) {
  10321. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10322. file_path);
  10323. rc = -ENOENT;
  10324. goto err;
  10325. }
  10326. if (filp->f_op == NULL) {
  10327. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10328. __func__);
  10329. rc = -EINVAL;
  10330. goto exit;
  10331. }
  10332. inode = filp->f_path.dentry->d_inode;
  10333. if (inode == NULL) {
  10334. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10335. __func__);
  10336. rc = -EINVAL;
  10337. goto exit;
  10338. }
  10339. file_len = i_size_read(inode->i_mapping->host);
  10340. if (file_len == 0) {
  10341. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10342. __func__, file_len);
  10343. rc = -EINVAL;
  10344. goto exit;
  10345. }
  10346. if (*size == 0)
  10347. read_len = file_len;
  10348. else
  10349. read_len = *size;
  10350. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10351. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10352. __func__, read_len);
  10353. rc = -EINVAL;
  10354. goto exit;
  10355. }
  10356. filp->private_data = inode->i_private;
  10357. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10358. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10359. rc = -EINVAL;
  10360. goto exit;
  10361. }
  10362. *size = read_len;
  10363. exit:
  10364. if (filp_close(filp, NULL) != 0)
  10365. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10366. err:
  10367. set_fs(oldfs);
  10368. return rc;
  10369. }
  10370. /*******************************************************************************
  10371. * FUNCTION: _pt_request_pip2_bin_hdr
  10372. *
  10373. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10374. * in the case of a flashless DUT, and parse the contents
  10375. *
  10376. * RETURNS:
  10377. * 0 = Success
  10378. * !0 = Error condition
  10379. *
  10380. * PARAMETERS:
  10381. * *dev - pointer to device structure
  10382. ******************************************************************************/
  10383. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10384. {
  10385. struct pt_core_data *cd = dev_get_drvdata(dev);
  10386. u8 file_handle;
  10387. u8 read_buf[255];
  10388. u8 hdr_len = 0;
  10389. u8 i;
  10390. int bytes_read;
  10391. int read_size;
  10392. int ret = 0;
  10393. int rc = 0;
  10394. bool load_hdr_struct = false;
  10395. if (cd->flashless_dut) {
  10396. read_size = sizeof(read_buf);
  10397. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10398. read_buf, &read_size);
  10399. if (rc) {
  10400. ret = rc;
  10401. pt_debug(dev, DL_ERROR,
  10402. "%s Failed to read fw image from US, rc=%d\n",
  10403. __func__, rc);
  10404. goto exit;
  10405. }
  10406. load_hdr_struct = true;
  10407. hdr_len = read_buf[0];
  10408. i = 0;
  10409. } else {
  10410. if (cd->mode != PT_MODE_BOOTLOADER) {
  10411. ret = -EPERM;
  10412. goto exit;
  10413. }
  10414. /* Open the bin file in Flash */
  10415. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10416. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10417. if (file_handle != PIP2_FW_FILE) {
  10418. ret = -ENOENT;
  10419. pt_debug(dev, DL_ERROR,
  10420. "%s Failed to open bin file\n", __func__);
  10421. goto exit;
  10422. }
  10423. /* Read the header length from the file */
  10424. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10425. read_size = 1;
  10426. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10427. read_buf);
  10428. if (bytes_read != read_size) {
  10429. ret = -EX_ERR_FREAD;
  10430. pt_debug(dev, DL_ERROR,
  10431. "%s Failed to bin file header len\n", __func__);
  10432. goto exit_close_file;
  10433. }
  10434. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10435. if (hdr_len == 0xFF) {
  10436. ret = -EX_ERR_FLEN;
  10437. pt_debug(dev, DL_ERROR,
  10438. "%s Bin header len is invalid\n", __func__);
  10439. goto exit_close_file;
  10440. }
  10441. /* Read the rest of the header from the bin file */
  10442. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10443. memset(read_buf, 0, sizeof(read_buf));
  10444. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10445. read_buf);
  10446. if (bytes_read != hdr_len) {
  10447. ret = -EX_ERR_FREAD;
  10448. pt_debug(dev, DL_ERROR,
  10449. "%s Failed to read bin file\n", __func__);
  10450. goto exit_close_file;
  10451. }
  10452. load_hdr_struct = true;
  10453. exit_close_file:
  10454. /* Close the file */
  10455. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10456. ret = -EX_ERR_FCLOSE;
  10457. pt_debug(dev, DL_ERROR,
  10458. "%s Failed to close bin file\n", __func__);
  10459. }
  10460. /*
  10461. * The length was already read so subtract 1 to make the rest of
  10462. * the offsets match the spec
  10463. */
  10464. i = PIP2_RESP_BODY_OFFSET - 1;
  10465. }
  10466. if (load_hdr_struct) {
  10467. hdr->length = hdr_len;
  10468. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10469. hdr->fw_major = (read_buf[i+3]);
  10470. hdr->fw_minor = (read_buf[i+4]);
  10471. hdr->fw_crc = (read_buf[i+5] << 24) |
  10472. (read_buf[i+6] << 16) |
  10473. (read_buf[i+7] << 8) |
  10474. (read_buf[i+8]);
  10475. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10476. (read_buf[i+10] << 16) |
  10477. (read_buf[i+11] << 8) |
  10478. (read_buf[i+12]);
  10479. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10480. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10481. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10482. if (hdr_len >= 22) {
  10483. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10484. (read_buf[i+20] << 16) |
  10485. (read_buf[i+21] << 8) |
  10486. (read_buf[i+22]);
  10487. } else {
  10488. hdr->hex_file_size = 0;
  10489. }
  10490. }
  10491. exit:
  10492. return ret;
  10493. }
  10494. /*******************************************************************************
  10495. * FUNCTION: _pt_pip2_file_get_stats
  10496. *
  10497. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10498. * opened file
  10499. *
  10500. * NOTE: The DUT must be in BL mode for this command to work
  10501. *
  10502. * RETURNS:
  10503. * !0 = Error
  10504. * 0 = Success
  10505. *
  10506. * PARAMETERS:
  10507. * *dev - pointer to device structure
  10508. * file_handle - File handle to read from
  10509. * *address - pointer to store address of file
  10510. * *file_size _ pointer to store size of file
  10511. ******************************************************************************/
  10512. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10513. u32 *file_size)
  10514. {
  10515. int ret = 1;
  10516. u16 status;
  10517. u16 actual_read_len;
  10518. u8 data[2];
  10519. u8 read_buf[16];
  10520. data[0] = file_handle;
  10521. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10522. ret = _pt_request_pip2_send_cmd(dev,
  10523. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10524. data, 2, read_buf, &actual_read_len);
  10525. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10526. if (ret || (status != 0x00)) {
  10527. pt_debug(dev, DL_ERROR,
  10528. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10529. __func__, status, file_handle, ret);
  10530. ret = -EIO;
  10531. goto exit;
  10532. }
  10533. pt_debug(dev, DL_DEBUG,
  10534. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10535. if (address) {
  10536. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10537. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10538. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10539. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10540. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10541. __func__, *address);
  10542. }
  10543. if (file_size) {
  10544. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10545. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10546. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10547. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10548. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10549. __func__, *file_size);
  10550. }
  10551. exit:
  10552. return ret;
  10553. }
  10554. /*******************************************************************************
  10555. * FUNCTION: _pt_pip2_file_seek_offset
  10556. *
  10557. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10558. * opened file
  10559. *
  10560. * NOTE: The DUT must be in BL mode for this command to work
  10561. * NOTE: File open/erase command can reset the offset
  10562. *
  10563. * RETURNS:
  10564. * !0 = Error
  10565. * 0 = Success
  10566. *
  10567. * PARAMETERS:
  10568. * *dev - pointer to device structure
  10569. * file_handle - File handle to read from
  10570. * read_offset - read offset of file
  10571. * write_offset - write offset of file
  10572. ******************************************************************************/
  10573. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10574. u32 read_offset, u32 write_offset)
  10575. {
  10576. int ret = 1;
  10577. u16 status;
  10578. u16 actual_read_len;
  10579. u8 data[10];
  10580. u8 read_buf[16];
  10581. data[0] = file_handle;
  10582. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10583. data[2] = 0xff & read_offset;
  10584. data[3] = 0xff & (read_offset >> 8);
  10585. data[4] = 0xff & (read_offset >> 16);
  10586. data[5] = 0xff & (read_offset >> 24);
  10587. data[6] = 0xff & write_offset;
  10588. data[7] = 0xff & (write_offset >> 8);
  10589. data[8] = 0xff & (write_offset >> 16);
  10590. data[9] = 0xff & (write_offset >> 24);
  10591. ret = _pt_request_pip2_send_cmd(dev,
  10592. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10593. data, 10, read_buf, &actual_read_len);
  10594. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10595. if (ret || (status != 0x00)) {
  10596. pt_debug(dev, DL_ERROR,
  10597. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10598. __func__, status, ret, file_handle);
  10599. ret = -EIO;
  10600. }
  10601. return ret;
  10602. }
  10603. /*******************************************************************************
  10604. * FUNCTION: _pt_pip2_file_crc
  10605. *
  10606. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10607. * the file.
  10608. *
  10609. * NOTE: The DUT must be in BL mode for this command to work
  10610. * NOTE: This command only can be used for BL version 1.8 or greater.
  10611. * BL version 1.8 added this change according to PGV-173.
  10612. *
  10613. * RETURNS:
  10614. * !0 = Error
  10615. * 0 = Success
  10616. *
  10617. * PARAMETERS:
  10618. * *dev - pointer to device structure
  10619. * file_handle - File handle to read from
  10620. * offset - start offset for CRC calculation
  10621. * length - number of bytes to calculate CRC over
  10622. * read_buf - pointer to the read buffer
  10623. ******************************************************************************/
  10624. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10625. u32 offset, u32 length, u8 *read_buf)
  10626. {
  10627. int rc = 1;
  10628. u16 actual_read_len;
  10629. u8 data[10];
  10630. data[0] = file_handle;
  10631. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10632. data[2] = 0xff & offset;
  10633. data[3] = 0xff & (offset >> 8);
  10634. data[4] = 0xff & (offset >> 16);
  10635. data[5] = 0xff & (offset >> 24);
  10636. data[6] = 0xff & length;
  10637. data[7] = 0xff & (length >> 8);
  10638. data[8] = 0xff & (length >> 16);
  10639. data[9] = 0xff & (length >> 24);
  10640. rc = _pt_request_pip2_send_cmd(dev,
  10641. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10642. data, 10, read_buf, &actual_read_len);
  10643. if (rc)
  10644. pt_debug(dev, DL_ERROR,
  10645. "%s Return FILE_CRC failure, rc = %d\n",
  10646. __func__, rc);
  10647. return rc;
  10648. }
  10649. /*******************************************************************************
  10650. * FUNCTION: pt_pip2_ping_test
  10651. *
  10652. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10653. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10654. * response payload matches what was sent.
  10655. * The max payload size is 247:
  10656. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10657. *
  10658. * RETURN:
  10659. * 0 = success
  10660. * !0 = failure
  10661. *
  10662. * PARAMETERS:
  10663. * *dev - pointer to device structure
  10664. * *attr - pointer to device attributes
  10665. * *buf - pointer to output buffer
  10666. ******************************************************************************/
  10667. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10668. {
  10669. u16 actual_read_len;
  10670. u8 *read_buf = NULL;
  10671. u8 *data = NULL;
  10672. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10673. int i = 1;
  10674. int j = 0;
  10675. int rc = 0;
  10676. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10677. if (!read_buf)
  10678. goto ping_test_exit;
  10679. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10680. if (!data)
  10681. goto ping_test_exit;
  10682. /* Load data payload with an array of walking 1's */
  10683. for (i = 0; i < 255; i++)
  10684. data[i] = 0x01 << (i % 8);
  10685. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10686. for (i = 0; i <= max_bytes; i++) {
  10687. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10688. PIP2_CMD_ID_PING, data, i, read_buf,
  10689. &actual_read_len);
  10690. if (rc) {
  10691. pt_debug(dev, DL_ERROR,
  10692. "%s: PING failed with %d byte payload\n",
  10693. __func__, i);
  10694. break;
  10695. }
  10696. /* Verify data returned matches data sent */
  10697. for (j = 0; j < i; j++) {
  10698. if (read_buf[data_offset + j] != data[j]) {
  10699. pt_debug(dev, DL_DEBUG,
  10700. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10701. __func__, i, j, data[j], j,
  10702. read_buf[data_offset + j]);
  10703. goto ping_test_exit;
  10704. }
  10705. }
  10706. }
  10707. ping_test_exit:
  10708. *last_packet_size = i - 1;
  10709. kfree(read_buf);
  10710. kfree(data);
  10711. return rc;
  10712. }
  10713. /*******************************************************************************
  10714. * FUNCTION: _pt_ic_parse_input_hex
  10715. *
  10716. * SUMMARY: Parse a char data array as space delimited hex values into
  10717. * an int array.
  10718. *
  10719. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10720. * buffer to store data is "u32". This function is still needed by the
  10721. * "command" sysfs node because the buffer type to store data is "u8".
  10722. *
  10723. * RETURN: Length of parsed data
  10724. *
  10725. * PARAMETERS:
  10726. * *dev - pointer to device structure
  10727. * *buf - pointer to buffer that holds the input array to parse
  10728. * buf_size - size of buf
  10729. * *ic_buf - pointer to array to store parsed data
  10730. * ic_buf_size - max size of ic_buf
  10731. ******************************************************************************/
  10732. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10733. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10734. {
  10735. const char *pbuf = buf;
  10736. unsigned long value;
  10737. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10738. u32 i = 0;
  10739. u32 j;
  10740. int last = 0;
  10741. int ret;
  10742. pt_debug(dev, DL_DEBUG,
  10743. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10744. __func__, pbuf, buf, buf_size, "scan buf size",
  10745. PT_MAX_PIP2_MSG_SIZE, buf);
  10746. while (pbuf <= (buf + buf_size)) {
  10747. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10748. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10749. __func__, "Max cmd size exceeded", i,
  10750. PT_MAX_PIP2_MSG_SIZE);
  10751. return -EINVAL;
  10752. }
  10753. if (i >= ic_buf_size) {
  10754. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10755. __func__, "Buffer size exceeded", i,
  10756. ic_buf_size);
  10757. return -EINVAL;
  10758. }
  10759. while (((*pbuf == ' ') || (*pbuf == ','))
  10760. && (pbuf < (buf + buf_size))) {
  10761. last = *pbuf;
  10762. pbuf++;
  10763. }
  10764. if (pbuf >= (buf + buf_size))
  10765. break;
  10766. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10767. if ((last == ',') && (*pbuf == ',')) {
  10768. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10769. __func__, "Invalid data format.");
  10770. return -EINVAL;
  10771. }
  10772. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10773. && (pbuf < (buf + buf_size))
  10774. && (*pbuf != ' ')
  10775. && (*pbuf != ','); j++) {
  10776. last = *pbuf;
  10777. scan_buf[j] = *pbuf++;
  10778. }
  10779. ret = kstrtoul(scan_buf, 16, &value);
  10780. if (ret < 0) {
  10781. pt_debug(dev, DL_ERROR,
  10782. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10783. "Invalid data format. ", scan_buf,
  10784. "Use \"0xHH,...,0xHH\"", " instead.",
  10785. i, ret);
  10786. return ret;
  10787. }
  10788. ic_buf[i] = value;
  10789. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10790. __func__, i, value);
  10791. i++;
  10792. }
  10793. return i;
  10794. }
  10795. /*******************************************************************************
  10796. * FUNCTION: _pt_ic_parse_input
  10797. *
  10798. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10799. * hex values or dec values into an int array with the following rules:
  10800. * 1) Hex values must have a "0x" prefix for each element or the first element
  10801. * only
  10802. * 2) Dec values do not have any prefix
  10803. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10804. * string
  10805. *
  10806. * RETURN: Number of values parsed
  10807. *
  10808. * PARAMETERS:
  10809. * *dev - pointer to device structure
  10810. * *buf - pointer to buffer that holds the input array to parse
  10811. * buf_size - size of buf
  10812. * *out_buf - pointer to array to store parsed data
  10813. * out_buf_size - max size of buffer to be stored
  10814. ******************************************************************************/
  10815. static int _pt_ic_parse_input(struct device *dev,
  10816. const char *buf, size_t buf_size,
  10817. u32 *out_buf, size_t out_buf_size)
  10818. {
  10819. const char *pbuf = buf;
  10820. unsigned long value;
  10821. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10822. u32 i = 0;
  10823. u32 j;
  10824. int last = 0;
  10825. int ret = 0;
  10826. u8 str_base = 0;
  10827. pt_debug(dev, DL_DEBUG,
  10828. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10829. __func__, buf_size, out_buf_size, "scan buf size",
  10830. PT_MAX_PIP2_MSG_SIZE, buf);
  10831. while (pbuf <= (buf + buf_size)) {
  10832. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10833. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10834. __func__, "Max cmd size exceeded", i,
  10835. PT_MAX_PIP2_MSG_SIZE);
  10836. ret = -EINVAL;
  10837. goto error;
  10838. }
  10839. if (i >= out_buf_size) {
  10840. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10841. __func__, "Buffer size exceeded", i,
  10842. out_buf_size);
  10843. ret = -EINVAL;
  10844. goto error;
  10845. }
  10846. while (((*pbuf == ' ') || (*pbuf == ','))
  10847. && (pbuf < (buf + buf_size))) {
  10848. last = *pbuf;
  10849. pbuf++;
  10850. }
  10851. if (pbuf >= (buf + buf_size))
  10852. break;
  10853. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10854. if ((last == ',') && (*pbuf == ',')) {
  10855. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10856. __func__, "Invalid data format.");
  10857. ret = -EINVAL;
  10858. goto error;
  10859. }
  10860. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10861. && (pbuf < (buf + buf_size))
  10862. && (*pbuf != ' ')
  10863. && (*pbuf != ','); j++) {
  10864. last = *pbuf;
  10865. scan_buf[j] = *pbuf++;
  10866. }
  10867. if (i == 0) {
  10868. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10869. (strncmp(scan_buf, "0X", 2) == 0))
  10870. str_base = 16;
  10871. else
  10872. str_base = 10;
  10873. } else {
  10874. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10875. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10876. (str_base == 10)) {
  10877. pt_debug(dev, DL_ERROR,
  10878. "%s: Decimal and Heximal data mixed\n",
  10879. __func__);
  10880. ret = -EINVAL;
  10881. goto error;
  10882. }
  10883. }
  10884. ret = kstrtoul(scan_buf, str_base, &value);
  10885. if (ret < 0) {
  10886. pt_debug(dev, DL_ERROR,
  10887. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10888. "Invalid data format. ", scan_buf,
  10889. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10890. " instead.", i, ret);
  10891. goto error;
  10892. }
  10893. out_buf[i] = value;
  10894. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10895. __func__, i, value, value);
  10896. i++;
  10897. }
  10898. ret = i;
  10899. error:
  10900. return ret;
  10901. }
  10902. #ifdef TTHE_TUNER_SUPPORT
  10903. /*******************************************************************************
  10904. * FUNCTION: tthe_debugfs_open
  10905. *
  10906. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10907. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10908. * printing a large number of fingers and sensor data without overflowing
  10909. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10910. * even multiple of PAGE_SIZE
  10911. *
  10912. * RETURN:
  10913. * 0 = success
  10914. * !0 = failure
  10915. *
  10916. * PARAMETERS:
  10917. * *inode - file inode number
  10918. * *filp - file pointer to debugfs file
  10919. ******************************************************************************/
  10920. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10921. {
  10922. struct pt_core_data *cd = inode->i_private;
  10923. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10924. filp->private_data = inode->i_private;
  10925. if (cd->tthe_buf)
  10926. return -EBUSY;
  10927. while (buf_size < 4096)
  10928. buf_size = buf_size << 1;
  10929. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10930. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10931. cd->tthe_buf_size = buf_size;
  10932. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10933. if (!cd->tthe_buf)
  10934. return -ENOMEM;
  10935. return 0;
  10936. }
  10937. /*******************************************************************************
  10938. * FUNCTION: tthe_debugfs_close
  10939. *
  10940. * SUMMARY: Close method for tthe_tuner debugfs node.
  10941. *
  10942. * RETURN:
  10943. * 0 = success
  10944. * !0 = failure
  10945. *
  10946. * PARAMETERS:
  10947. * *inode - file inode number
  10948. * *filp - file pointer to debugfs file
  10949. ******************************************************************************/
  10950. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10951. {
  10952. struct pt_core_data *cd = filp->private_data;
  10953. filp->private_data = NULL;
  10954. kfree(cd->tthe_buf);
  10955. cd->tthe_buf = NULL;
  10956. return 0;
  10957. }
  10958. /*******************************************************************************
  10959. * FUNCTION: tthe_debugfs_read
  10960. *
  10961. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10962. * tthe_buf to user buffer.
  10963. *
  10964. * RETURN: Size of debugfs data print
  10965. *
  10966. * PARAMETERS:
  10967. * *filp - file pointer to debugfs file
  10968. * *buf - the user space buffer to read to
  10969. * count - the maximum number of bytes to read
  10970. * *ppos - the current position in the buffer
  10971. ******************************************************************************/
  10972. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10973. size_t count, loff_t *ppos)
  10974. {
  10975. struct pt_core_data *cd = filp->private_data;
  10976. int size;
  10977. int ret;
  10978. static int partial_read;
  10979. wait_event_interruptible(cd->wait_q,
  10980. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10981. mutex_lock(&cd->tthe_lock);
  10982. if (cd->tthe_exit) {
  10983. mutex_unlock(&cd->tthe_lock);
  10984. return 0;
  10985. }
  10986. if (count > cd->tthe_buf_len)
  10987. size = cd->tthe_buf_len;
  10988. else
  10989. size = count;
  10990. if (!size) {
  10991. mutex_unlock(&cd->tthe_lock);
  10992. return 0;
  10993. }
  10994. if (partial_read) {
  10995. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10996. partial_read = 0;
  10997. } else {
  10998. ret = copy_to_user(buf, cd->tthe_buf, size);
  10999. }
  11000. if (size == count)
  11001. partial_read = count;
  11002. if (ret == size)
  11003. return -EFAULT;
  11004. size -= ret;
  11005. cd->tthe_buf_len -= size;
  11006. mutex_unlock(&cd->tthe_lock);
  11007. *ppos += size;
  11008. return size;
  11009. }
  11010. static const struct file_operations tthe_debugfs_fops = {
  11011. .open = tthe_debugfs_open,
  11012. .release = tthe_debugfs_close,
  11013. .read = tthe_debugfs_read,
  11014. };
  11015. #endif
  11016. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11017. .start_bl = _pt_request_pip_start_bl,
  11018. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11019. .resume_scanning = _pt_request_pip_resume_scanning,
  11020. .get_param = _pt_request_pip_get_param,
  11021. .set_param = _pt_request_pip_set_param,
  11022. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11023. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11024. .get_data_structure = _pt_request_pip_get_data_structure,
  11025. .run_selftest = _pt_request_pip_run_selftest,
  11026. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11027. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11028. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11029. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11030. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11031. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11032. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11033. .read_data_block = _pt_request_pip_read_data_block,
  11034. .write_data_block = _pt_request_pip_write_data_block,
  11035. .user_cmd = _pt_request_pip_user_cmd,
  11036. .get_bl_info = _pt_request_pip_bl_get_information,
  11037. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11038. .launch_app = _pt_request_pip_launch_app,
  11039. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11040. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11041. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11042. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11043. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11044. .pip2_file_open = _pt_pip2_file_open,
  11045. .pip2_file_close = _pt_pip2_file_close,
  11046. .pip2_file_erase = _pt_pip2_file_erase,
  11047. .read_us_file = _pt_read_us_file,
  11048. .manage_cal_data = _pt_manage_local_cal_data,
  11049. .calc_crc = crc_ccitt_calculate,
  11050. #ifdef TTDL_DIAGNOSTICS
  11051. .pip2_file_read = _pt_pip2_file_read,
  11052. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11053. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11054. .pip2_file_crc = _pt_pip2_file_crc,
  11055. #endif
  11056. };
  11057. static struct pt_core_commands _pt_core_commands = {
  11058. .subscribe_attention = _pt_subscribe_attention,
  11059. .unsubscribe_attention = _pt_unsubscribe_attention,
  11060. .request_exclusive = _pt_request_exclusive,
  11061. .release_exclusive = _pt_release_exclusive,
  11062. .request_reset = _pt_request_reset,
  11063. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11064. .request_enum = _pt_request_enum,
  11065. .request_sysinfo = _pt_request_sysinfo,
  11066. .request_loader_pdata = _pt_request_loader_pdata,
  11067. .request_stop_wd = _pt_request_stop_wd,
  11068. .request_start_wd = _pt_request_start_wd,
  11069. .request_get_mode = _pt_request_get_mode,
  11070. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11071. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11072. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11073. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11074. .request_dut_generation = _pt_request_dut_generation,
  11075. .request_hw_version = _pt_request_hw_version,
  11076. .parse_sysfs_input = _pt_ic_parse_input,
  11077. #ifdef TTHE_TUNER_SUPPORT
  11078. .request_tthe_print = _pt_request_tthe_print,
  11079. #endif
  11080. #ifdef TTDL_DIAGNOSTICS
  11081. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11082. #endif
  11083. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11084. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11085. };
  11086. struct pt_core_commands *pt_get_commands(void)
  11087. {
  11088. return &_pt_core_commands;
  11089. }
  11090. EXPORT_SYMBOL_GPL(pt_get_commands);
  11091. static DEFINE_MUTEX(core_list_lock);
  11092. static LIST_HEAD(core_list);
  11093. static DEFINE_MUTEX(module_list_lock);
  11094. static LIST_HEAD(module_list);
  11095. static int core_number;
  11096. /*******************************************************************************
  11097. * FUNCTION: pt_probe_module
  11098. *
  11099. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11100. *
  11101. * RETURN:
  11102. * 0 = success
  11103. * !0 = failure
  11104. *
  11105. * PARAMETERS:
  11106. * *cd - pointer to core data
  11107. * *module - pointer to module structure
  11108. ******************************************************************************/
  11109. static int pt_probe_module(struct pt_core_data *cd,
  11110. struct pt_module *module)
  11111. {
  11112. struct module_node *module_node;
  11113. int rc = 0;
  11114. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11115. if (!module_node)
  11116. return -ENOMEM;
  11117. module_node->module = module;
  11118. mutex_lock(&cd->module_list_lock);
  11119. list_add(&module_node->node, &cd->module_list);
  11120. mutex_unlock(&cd->module_list_lock);
  11121. rc = module->probe(cd->dev, &module_node->data);
  11122. if (rc) {
  11123. /*
  11124. * Remove from the list when probe fails
  11125. * in order not to call release
  11126. */
  11127. mutex_lock(&cd->module_list_lock);
  11128. list_del(&module_node->node);
  11129. mutex_unlock(&cd->module_list_lock);
  11130. kfree(module_node);
  11131. goto exit;
  11132. }
  11133. exit:
  11134. return rc;
  11135. }
  11136. /*******************************************************************************
  11137. * FUNCTION: pt_release_module
  11138. *
  11139. * SUMMARY: Call the release pointer and remove the module pointer from
  11140. * module_list.
  11141. *
  11142. * PARAMETERS:
  11143. * *cd - pointer to core data
  11144. * *module - pointer to module structure
  11145. ******************************************************************************/
  11146. static void pt_release_module(struct pt_core_data *cd,
  11147. struct pt_module *module)
  11148. {
  11149. struct module_node *m, *m_n;
  11150. mutex_lock(&cd->module_list_lock);
  11151. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11152. if (m->module == module) {
  11153. module->release(cd->dev, m->data);
  11154. list_del(&m->node);
  11155. kfree(m);
  11156. break;
  11157. }
  11158. mutex_unlock(&cd->module_list_lock);
  11159. }
  11160. /*******************************************************************************
  11161. * FUNCTION: pt_probe_modules
  11162. *
  11163. * SUMMARY: Iterate module_list and probe each module.
  11164. *
  11165. * PARAMETERS:
  11166. * *cd - pointer to core data
  11167. ******************************************************************************/
  11168. static void pt_probe_modules(struct pt_core_data *cd)
  11169. {
  11170. struct pt_module *m;
  11171. int rc = 0;
  11172. mutex_lock(&module_list_lock);
  11173. list_for_each_entry(m, &module_list, node) {
  11174. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11175. __func__, m->name);
  11176. rc = pt_probe_module(cd, m);
  11177. if (rc)
  11178. pt_debug(cd->dev, DL_ERROR,
  11179. "%s: Probe fails for module %s\n",
  11180. __func__, m->name);
  11181. }
  11182. mutex_unlock(&module_list_lock);
  11183. }
  11184. /*******************************************************************************
  11185. * FUNCTION: pt_release_modules
  11186. *
  11187. * SUMMARY: Iterate module_list and remove each module.
  11188. *
  11189. * PARAMETERS:
  11190. * *cd - pointer to core data
  11191. ******************************************************************************/
  11192. static void pt_release_modules(struct pt_core_data *cd)
  11193. {
  11194. struct pt_module *m;
  11195. mutex_lock(&module_list_lock);
  11196. list_for_each_entry(m, &module_list, node)
  11197. pt_release_module(cd, m);
  11198. mutex_unlock(&module_list_lock);
  11199. }
  11200. /*******************************************************************************
  11201. * FUNCTION: pt_get_core_data
  11202. *
  11203. * SUMMARY: Iterate core_list and get core data.
  11204. *
  11205. * RETURN:
  11206. * pointer to core data or null pointer if fail
  11207. *
  11208. * PARAMETERS:
  11209. * *id - pointer to core id
  11210. ******************************************************************************/
  11211. struct pt_core_data *pt_get_core_data(char *id)
  11212. {
  11213. struct pt_core_data *d;
  11214. list_for_each_entry(d, &core_list, node)
  11215. if (!strncmp(d->core_id, id, 20))
  11216. return d;
  11217. return NULL;
  11218. }
  11219. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11220. /*******************************************************************************
  11221. * FUNCTION: pt_add_core
  11222. *
  11223. * SUMMARY: Add core data to the core_list.
  11224. *
  11225. * PARAMETERS:
  11226. * *dev - pointer to device structure
  11227. ******************************************************************************/
  11228. static void pt_add_core(struct device *dev)
  11229. {
  11230. struct pt_core_data *d;
  11231. struct pt_core_data *cd = dev_get_drvdata(dev);
  11232. mutex_lock(&core_list_lock);
  11233. list_for_each_entry(d, &core_list, node)
  11234. if (d->dev == dev)
  11235. goto unlock;
  11236. list_add(&cd->node, &core_list);
  11237. unlock:
  11238. mutex_unlock(&core_list_lock);
  11239. }
  11240. /*******************************************************************************
  11241. * FUNCTION: pt_del_core
  11242. *
  11243. * SUMMARY: Remove core data from the core_list.
  11244. *
  11245. * PARAMETERS:
  11246. * *dev - pointer to device structure
  11247. ******************************************************************************/
  11248. static void pt_del_core(struct device *dev)
  11249. {
  11250. struct pt_core_data *d, *d_n;
  11251. mutex_lock(&core_list_lock);
  11252. list_for_each_entry_safe(d, d_n, &core_list, node)
  11253. if (d->dev == dev) {
  11254. list_del(&d->node);
  11255. goto unlock;
  11256. }
  11257. unlock:
  11258. mutex_unlock(&core_list_lock);
  11259. }
  11260. /*******************************************************************************
  11261. * FUNCTION: pt_register_module
  11262. *
  11263. * SUMMARY: Register the module to module_list and probe the module for each
  11264. * core.
  11265. *
  11266. * RETURN:
  11267. * 0 = success
  11268. * !0 = failure
  11269. *
  11270. * PARAMETERS:
  11271. * *module - pointer to module structure
  11272. ******************************************************************************/
  11273. int pt_register_module(struct pt_module *module)
  11274. {
  11275. struct pt_module *m;
  11276. struct pt_core_data *cd;
  11277. int rc = 0;
  11278. if (!module || !module->probe || !module->release)
  11279. return -EINVAL;
  11280. mutex_lock(&module_list_lock);
  11281. list_for_each_entry(m, &module_list, node)
  11282. if (m == module) {
  11283. rc = -EEXIST;
  11284. goto unlock;
  11285. }
  11286. list_add(&module->node, &module_list);
  11287. /* Probe the module for each core */
  11288. mutex_lock(&core_list_lock);
  11289. list_for_each_entry(cd, &core_list, node)
  11290. pt_probe_module(cd, module);
  11291. mutex_unlock(&core_list_lock);
  11292. unlock:
  11293. mutex_unlock(&module_list_lock);
  11294. return rc;
  11295. }
  11296. EXPORT_SYMBOL_GPL(pt_register_module);
  11297. /*******************************************************************************
  11298. * FUNCTION: pt_unregister_module
  11299. *
  11300. * SUMMARY: Release the module for each core and remove the module from
  11301. * module_list.
  11302. *
  11303. * RETURN:
  11304. * 0 = success
  11305. * !0 = failure
  11306. *
  11307. * PARAMETERS:
  11308. * *module - pointer to module structure
  11309. ******************************************************************************/
  11310. void pt_unregister_module(struct pt_module *module)
  11311. {
  11312. struct pt_module *m, *m_n;
  11313. struct pt_core_data *cd;
  11314. if (!module)
  11315. return;
  11316. mutex_lock(&module_list_lock);
  11317. /* Release the module for each core */
  11318. mutex_lock(&core_list_lock);
  11319. list_for_each_entry(cd, &core_list, node)
  11320. pt_release_module(cd, module);
  11321. mutex_unlock(&core_list_lock);
  11322. list_for_each_entry_safe(m, m_n, &module_list, node)
  11323. if (m == module) {
  11324. list_del(&m->node);
  11325. break;
  11326. }
  11327. mutex_unlock(&module_list_lock);
  11328. }
  11329. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11330. /*******************************************************************************
  11331. * FUNCTION: pt_get_module_data
  11332. *
  11333. * SUMMARY: Get module data from module_node by module_list.
  11334. *
  11335. * RETURN:
  11336. * pointer to module data
  11337. *
  11338. * PARAMETERS:
  11339. * *dev - pointer to device structure
  11340. * *module - pointer to module structure
  11341. ******************************************************************************/
  11342. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11343. {
  11344. struct pt_core_data *cd = dev_get_drvdata(dev);
  11345. struct module_node *m;
  11346. void *data = NULL;
  11347. mutex_lock(&cd->module_list_lock);
  11348. list_for_each_entry(m, &cd->module_list, node)
  11349. if (m->module == module) {
  11350. data = m->data;
  11351. break;
  11352. }
  11353. mutex_unlock(&cd->module_list_lock);
  11354. return data;
  11355. }
  11356. EXPORT_SYMBOL(pt_get_module_data);
  11357. #ifdef CONFIG_HAS_EARLYSUSPEND
  11358. /*******************************************************************************
  11359. * FUNCTION: pt_early_suspend
  11360. *
  11361. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11362. * attention list.
  11363. *
  11364. * PARAMETERS:
  11365. * *h - pointer to early_suspend structure
  11366. ******************************************************************************/
  11367. static void pt_early_suspend(struct early_suspend *h)
  11368. {
  11369. struct pt_core_data *cd =
  11370. container_of(h, struct pt_core_data, es);
  11371. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11372. }
  11373. /*******************************************************************************
  11374. * FUNCTION: pt_late_resume
  11375. *
  11376. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11377. * attention list.
  11378. *
  11379. * PARAMETERS:
  11380. * *h - pointer to early_suspend structure
  11381. ******************************************************************************/
  11382. static void pt_late_resume(struct early_suspend *h)
  11383. {
  11384. struct pt_core_data *cd =
  11385. container_of(h, struct pt_core_data, es);
  11386. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11387. }
  11388. /*******************************************************************************
  11389. * FUNCTION: pt_setup_early_suspend
  11390. *
  11391. * SUMMARY: Register early/suspend function to the system.
  11392. *
  11393. * PARAMETERS:
  11394. * *cd - pointer to core data
  11395. ******************************************************************************/
  11396. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11397. {
  11398. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11399. cd->es.suspend = pt_early_suspend;
  11400. cd->es.resume = pt_late_resume;
  11401. register_early_suspend(&cd->es);
  11402. }
  11403. #elif defined(CONFIG_DRM)
  11404. static void pt_resume_work(struct work_struct *work)
  11405. {
  11406. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11407. resume_work);
  11408. int rc = 0;
  11409. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11410. return;
  11411. rc = pt_core_easywake_off(pt_data);
  11412. if (rc < 0) {
  11413. pt_debug(pt_data->dev, DL_ERROR,
  11414. "%s: Error on wake\n", __func__);
  11415. }
  11416. return;
  11417. }
  11418. static void pt_suspend_work(struct work_struct *work)
  11419. {
  11420. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11421. suspend_work);
  11422. int rc = 0;
  11423. pt_debug(pt_data->dev, DL_INFO, "%s Start\n", __func__);
  11424. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11425. return;
  11426. rc = pt_core_easywake_on(pt_data);
  11427. if (rc < 0) {
  11428. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11429. return;
  11430. }
  11431. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  11432. return;
  11433. }
  11434. /*******************************************************************************
  11435. * FUNCTION: drm_notifier_callback
  11436. *
  11437. * SUMMARY: Call back function for DRM notifier to allow to call
  11438. * resume/suspend attention list.
  11439. *
  11440. * RETURN:
  11441. * 0 = success
  11442. *
  11443. * PARAMETERS:
  11444. * *self - pointer to notifier_block structure
  11445. * event - event type of fb notifier
  11446. * *data - pointer to fb_event structure
  11447. ******************************************************************************/
  11448. static int drm_notifier_callback(struct notifier_block *self,
  11449. unsigned long event, void *data)
  11450. {
  11451. struct pt_core_data *cd =
  11452. container_of(self, struct pt_core_data, fb_notifier);
  11453. struct drm_panel_notifier *evdata = data;
  11454. int *blank;
  11455. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11456. if (!evdata)
  11457. goto exit;
  11458. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11459. event == DRM_PANEL_EVENT_BLANK)) {
  11460. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11461. __func__, event);
  11462. goto exit;
  11463. }
  11464. blank = evdata->data;
  11465. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11466. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11467. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11468. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11469. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11470. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11471. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11472. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11473. __func__, event);
  11474. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11475. if (cd->fb_state != FB_ON) {
  11476. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11477. __func__);
  11478. queue_work(cd->pt_workqueue, &cd->resume_work);
  11479. #if defined(CONFIG_PM_SLEEP)
  11480. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11481. __func__);
  11482. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11483. pt_core_resume_(cd->dev);
  11484. #endif
  11485. cd->fb_state = FB_ON;
  11486. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11487. }
  11488. }
  11489. } else if (*blank == DRM_PANEL_BLANK_LP) {
  11490. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11491. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11492. if (cd->fb_state != FB_OFF) {
  11493. #if defined(CONFIG_PM_SLEEP)
  11494. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11495. __func__);
  11496. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11497. pt_core_suspend_(cd->dev);
  11498. #endif
  11499. cancel_work_sync(&cd->resume_work);
  11500. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11501. cd->fb_state = FB_OFF;
  11502. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11503. }
  11504. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11505. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11506. __func__, event);
  11507. }
  11508. } else {
  11509. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11510. __func__, *blank);
  11511. }
  11512. exit:
  11513. return 0;
  11514. }
  11515. /*******************************************************************************
  11516. * FUNCTION: pt_setup_drm_notifier
  11517. *
  11518. * SUMMARY: Set up call back function into drm notifier.
  11519. *
  11520. * PARAMETERS:
  11521. * *cd - pointer to core data
  11522. ******************************************************************************/
  11523. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11524. {
  11525. cd->fb_state = FB_NONE;
  11526. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11527. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11528. if (!active_panel)
  11529. pt_debug(cd->dev, DL_ERROR,
  11530. "%s: Active panel not registered!\n", __func__);
  11531. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11532. if (!cd->pt_workqueue) {
  11533. pt_debug(cd->dev, DL_ERROR,
  11534. "%s: worker thread creation failed !\n", __func__);
  11535. }
  11536. if (cd->pt_workqueue) {
  11537. INIT_WORK(&cd->resume_work, pt_resume_work);
  11538. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11539. }
  11540. if (active_panel &&
  11541. drm_panel_notifier_register(active_panel,
  11542. &cd->fb_notifier) < 0)
  11543. pt_debug(cd->dev, DL_ERROR,
  11544. "%s: Register notifier failed!\n", __func__);
  11545. }
  11546. #elif defined(CONFIG_FB)
  11547. /*******************************************************************************
  11548. * FUNCTION: fb_notifier_callback
  11549. *
  11550. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11551. * resume/suspend attention list.
  11552. *
  11553. * RETURN:
  11554. * 0 = success
  11555. *
  11556. * PARAMETERS:
  11557. * *self - pointer to notifier_block structure
  11558. * event - event type of fb notifier
  11559. * *data - pointer to fb_event structure
  11560. ******************************************************************************/
  11561. static int fb_notifier_callback(struct notifier_block *self,
  11562. unsigned long event, void *data)
  11563. {
  11564. struct pt_core_data *cd =
  11565. container_of(self, struct pt_core_data, fb_notifier);
  11566. struct fb_event *evdata = data;
  11567. int *blank;
  11568. if (event != FB_EVENT_BLANK || !evdata)
  11569. goto exit;
  11570. blank = evdata->data;
  11571. if (*blank == FB_BLANK_UNBLANK) {
  11572. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11573. if (cd->fb_state != FB_ON) {
  11574. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11575. #if defined(CONFIG_PM_SLEEP)
  11576. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11577. pt_core_resume_(cd->dev);
  11578. #endif
  11579. cd->fb_state = FB_ON;
  11580. }
  11581. } else if (*blank == FB_BLANK_POWERDOWN) {
  11582. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11583. if (cd->fb_state != FB_OFF) {
  11584. #if defined(CONFIG_PM_SLEEP)
  11585. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11586. pt_core_suspend_(cd->dev);
  11587. #endif
  11588. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11589. cd->fb_state = FB_OFF;
  11590. }
  11591. }
  11592. exit:
  11593. return 0;
  11594. }
  11595. /*******************************************************************************
  11596. * FUNCTION: pt_setup_fb_notifier
  11597. *
  11598. * SUMMARY: Set up call back function into fb notifier.
  11599. *
  11600. * PARAMETERS:
  11601. * *cd - pointer to core data
  11602. ******************************************************************************/
  11603. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11604. {
  11605. int rc = 0;
  11606. cd->fb_state = FB_ON;
  11607. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11608. rc = fb_register_client(&cd->fb_notifier);
  11609. if (rc)
  11610. pt_debug(cd->dev, DL_ERROR,
  11611. "Unable to register fb_notifier: %d\n", rc);
  11612. }
  11613. #endif
  11614. /*******************************************************************************
  11615. * FUNCTION: pt_watchdog_work
  11616. *
  11617. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11618. * sleeping then this function simply returns. If the DUT is awake the
  11619. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11620. * somehow a response is waiting on the DUT that has not been read. If
  11621. * this occurs the message is simply consumed. If or once the IRQ is
  11622. * cleared, a PIP PING message is sent to the DUT and if the response
  11623. * is received the watchdog succeeds and exits, if no response is seen
  11624. * a startup is queued unless the maximum number of startups have already
  11625. * been attempted, in that case a BL is attempted.
  11626. *
  11627. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11628. * work thread
  11629. *
  11630. * RETURN: void
  11631. *
  11632. * PARAMETERS:
  11633. * *work - pointer to a work structure for the watchdog work queue
  11634. ******************************************************************************/
  11635. static void pt_watchdog_work(struct work_struct *work)
  11636. {
  11637. int rc = 0;
  11638. struct pt_core_data *cd = container_of(work,
  11639. struct pt_core_data, watchdog_work);
  11640. /*
  11641. * if found the current sleep_state is SS_SLEEPING
  11642. * then no need to request_exclusive, directly return
  11643. */
  11644. if (cd->sleep_state == SS_SLEEPING)
  11645. return;
  11646. #ifdef TTDL_DIAGNOSTICS
  11647. cd->watchdog_count++;
  11648. #endif /* TTDL_DIAGNOSTICS */
  11649. /*
  11650. * The first WD interval was extended to allow DDI to come up.
  11651. * If the WD interval is not the default then adjust timer to the
  11652. * current setting. The user can override value with drv_debug sysfs.
  11653. */
  11654. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11655. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11656. msecs_to_jiffies(cd->watchdog_interval));
  11657. }
  11658. if (pt_check_irq_asserted(cd)) {
  11659. #ifdef TTDL_DIAGNOSTICS
  11660. cd->watchdog_irq_stuck_count++;
  11661. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11662. #endif /* TTDL_DIAGNOSTICS */
  11663. pt_debug(cd->dev, DL_ERROR,
  11664. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11665. __func__);
  11666. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11667. }
  11668. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11669. if (rc < 0) {
  11670. pt_debug(cd->dev, DL_ERROR,
  11671. "%s: fail get exclusive ex=%p own=%p\n",
  11672. __func__, cd->exclusive_dev, cd->dev);
  11673. goto queue_startup;
  11674. }
  11675. rc = pt_pip_null_(cd);
  11676. if (release_exclusive(cd, cd->dev) < 0)
  11677. pt_debug(cd->dev, DL_ERROR,
  11678. "%s: fail to release exclusive\n", __func__);
  11679. queue_startup:
  11680. if (rc) {
  11681. #ifdef TTDL_DIAGNOSTICS
  11682. cd->watchdog_failed_access_count++;
  11683. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11684. #endif /* TTDL_DIAGNOSTICS */
  11685. pt_debug(cd->dev, DL_ERROR,
  11686. "%s: failed to access device in WD, retry count=%d\n",
  11687. __func__, cd->startup_retry_count);
  11688. /* Already tried FW upgrade because of watchdog but failed */
  11689. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11690. return;
  11691. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11692. /*
  11693. * Any wrapper function that trys to disable the
  11694. * WD killing this worker cannot be called here.
  11695. */
  11696. rc = request_exclusive(cd, cd->dev,
  11697. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11698. if (rc < 0) {
  11699. pt_debug(cd->dev, DL_ERROR,
  11700. "%s: fail get exclusive ex=%p own=%p\n",
  11701. __func__, cd->exclusive_dev, cd->dev);
  11702. goto exit;
  11703. }
  11704. cd->hw_detected = false;
  11705. cd->startup_status = STARTUP_STATUS_START;
  11706. pt_debug(cd->dev, DL_DEBUG,
  11707. "%s: Startup Status Reset\n", __func__);
  11708. rc = pt_dut_reset_and_wait(cd);
  11709. if (release_exclusive(cd, cd->dev) < 0)
  11710. pt_debug(cd->dev, DL_ERROR,
  11711. "%s: fail to release exclusive\n",
  11712. __func__);
  11713. if (!rc) {
  11714. cd->hw_detected = true;
  11715. if (!cd->flashless_dut)
  11716. pt_queue_enum(cd);
  11717. }
  11718. #ifdef TTDL_DIAGNOSTICS
  11719. cd->wd_xres_count++;
  11720. pt_debug(cd->dev, DL_ERROR,
  11721. "%s: Comm Failed - DUT reset [#%d]\n",
  11722. __func__, cd->wd_xres_count);
  11723. #endif /* TTDL_DIAGNOSTICS */
  11724. } else {
  11725. /*
  11726. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11727. * reset the part to regain communications, try to BL
  11728. */
  11729. pt_debug(cd->dev, DL_ERROR,
  11730. "%s: WD DUT access failure, Start FW Upgrade\n",
  11731. __func__);
  11732. #ifdef TTDL_DIAGNOSTICS
  11733. /*
  11734. * When diagnostics is enabled allow TTDL to keep
  11735. * trying to find the DUT. This allows the DUT to be
  11736. * hot swap-able while the host stays running. In
  11737. * production this may not be wanted as a customer
  11738. * may have several touch drivers and any driver
  11739. * that doesn't match the current DUT should give
  11740. * up trying and give up using the bus.
  11741. */
  11742. pt_debug(cd->dev, DL_INFO,
  11743. "%s: Resetting startup_retry_count\n",
  11744. __func__);
  11745. cd->startup_retry_count = 0;
  11746. #endif /* TTDL_DIAGNOSTICS */
  11747. /*
  11748. * Since fw may be broken,reset sysinfo ready flag
  11749. * to let upgrade function work.
  11750. */
  11751. mutex_lock(&cd->system_lock);
  11752. cd->sysinfo.ready = false;
  11753. mutex_unlock(&cd->system_lock);
  11754. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11755. pt_debug(cd->dev, DL_ERROR,
  11756. "%s: Queue Restart\n", __func__);
  11757. pt_queue_restart(cd);
  11758. } else
  11759. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11760. }
  11761. } else {
  11762. cd->hw_detected = true;
  11763. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11764. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11765. pt_debug(cd->dev, DL_ERROR,
  11766. "%s: HW detected but not enumerated\n",
  11767. __func__);
  11768. pt_queue_enum(cd);
  11769. }
  11770. }
  11771. exit:
  11772. pt_start_wd_timer(cd);
  11773. }
  11774. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11775. /*******************************************************************************
  11776. * FUNCTION: pt_watchdog_timer
  11777. *
  11778. * SUMMARY: The function that is called when the WD timer expires. If the
  11779. * watchdog work is not already busy schedule the watchdog work queue.
  11780. *
  11781. * RETURN: void
  11782. *
  11783. * PARAMETERS:
  11784. * handle - Handle to the watchdog timer
  11785. ******************************************************************************/
  11786. static void pt_watchdog_timer(unsigned long handle)
  11787. {
  11788. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11789. if (!cd)
  11790. return;
  11791. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11792. __func__);
  11793. if (!work_pending(&cd->watchdog_work))
  11794. schedule_work(&cd->watchdog_work);
  11795. }
  11796. #else
  11797. /*******************************************************************************
  11798. * FUNCTION: pt_watchdog_timer
  11799. *
  11800. * SUMMARY: The function that is called when the WD timer expires. If the
  11801. * watchdog work is not already busy schedule the watchdog work queue.
  11802. *
  11803. * RETURN: void
  11804. *
  11805. * PARAMETERS:
  11806. * *t - Pointer to timer list
  11807. ******************************************************************************/
  11808. static void pt_watchdog_timer(struct timer_list *t)
  11809. {
  11810. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11811. if (!cd)
  11812. return;
  11813. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11814. __func__);
  11815. if (!work_pending(&cd->watchdog_work))
  11816. schedule_work(&cd->watchdog_work);
  11817. }
  11818. #endif
  11819. /*******************************************************************************
  11820. * Core sysfs show and store functions
  11821. ******************************************************************************/
  11822. /*******************************************************************************
  11823. * FUNCTION: pt_hw_version_show
  11824. *
  11825. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11826. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11827. *
  11828. * RETURN: size of data written to sysfs node
  11829. *
  11830. * PARAMETERS:
  11831. * *dev - pointer to device structure
  11832. * *attr - pointer to device attributes structure
  11833. * *buf - pointer to print output buffer
  11834. ******************************************************************************/
  11835. static ssize_t pt_hw_version_show(struct device *dev,
  11836. struct device_attribute *attr, char *buf)
  11837. {
  11838. struct pt_core_data *cd = dev_get_drvdata(dev);
  11839. _pt_request_hw_version(dev, cd->hw_version);
  11840. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11841. }
  11842. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11843. /*******************************************************************************
  11844. * FUNCTION: pt_drv_version_show
  11845. *
  11846. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11847. * TTDL version information
  11848. *
  11849. * RETURN: Char buffer with printed TTDL version information
  11850. *
  11851. * PARAMETERS:
  11852. * *dev - pointer to device structure
  11853. * *attr - pointer to device attributes
  11854. * *buf - pointer to output buffer
  11855. ******************************************************************************/
  11856. static ssize_t pt_drv_version_show(struct device *dev,
  11857. struct device_attribute *attr, char *buf)
  11858. {
  11859. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11860. "Driver: %s\nVersion: %s\nDate: %s\n",
  11861. pt_driver_core_name, pt_driver_core_version,
  11862. pt_driver_core_date);
  11863. }
  11864. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11865. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11866. /*******************************************************************************
  11867. * FUNCTION: pt_fw_version_show
  11868. *
  11869. * SUMMARY: Show method for the fw_version sysfs node that will
  11870. * show the firmware, bootloader and PIP version information
  11871. *
  11872. * RETURN: Size of printed buffer
  11873. *
  11874. * PARAMETERS:
  11875. * *dev - pointer to device structure
  11876. * *attr - pointer to device attributes
  11877. * *buf - pointer to output buffer
  11878. ******************************************************************************/
  11879. static ssize_t pt_fw_version_show(struct device *dev,
  11880. struct device_attribute *attr, char *buf)
  11881. {
  11882. struct pt_core_data *cd = dev_get_drvdata(dev);
  11883. struct pt_ttdata *ttdata;
  11884. int rc = 0;
  11885. if (cd->mode == PT_MODE_OPERATIONAL)
  11886. rc = pt_hid_output_get_sysinfo_(cd);
  11887. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11888. __func__, cd->mode, cd->sysinfo.ready);
  11889. if (cd->sysinfo.ready)
  11890. ttdata = &cd->sysinfo.ttdata;
  11891. else
  11892. rc = -ENODATA;
  11893. if (!rc) {
  11894. return scnprintf(buf, strlen(buf),
  11895. "Status: %d\n"
  11896. "FW : %d.%d.%d\n"
  11897. "Config: %d\n"
  11898. "BL : %d.%d\n"
  11899. "PIP : %d.%d\n",
  11900. rc,
  11901. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11902. ttdata->revctrl,
  11903. ttdata->fw_ver_conf,
  11904. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11905. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11906. } else {
  11907. return scnprintf(buf, strlen(buf),
  11908. "Status: %d\n"
  11909. "FW : n/a\n"
  11910. "Config: n/a\n"
  11911. "BL : n/a\n"
  11912. "PIP : n/a\n",
  11913. rc);
  11914. }
  11915. }
  11916. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11917. /*******************************************************************************
  11918. * FUNCTION: pt_sysinfo_show
  11919. *
  11920. * SUMMARY: Show method for the sysinfo sysfs node that will
  11921. * show all the information from get system information command.
  11922. *
  11923. * RETURN: Size of printed buffer
  11924. *
  11925. * PARAMETERS:
  11926. * *dev - pointer to device structure
  11927. * *attr - pointer to device attributes
  11928. * *buf - pointer to output buffer
  11929. ******************************************************************************/
  11930. static ssize_t pt_sysinfo_show(struct device *dev,
  11931. struct device_attribute *attr, char *buf)
  11932. {
  11933. struct pt_core_data *cd = dev_get_drvdata(dev);
  11934. struct pt_sysinfo *si;
  11935. struct pt_ttdata *ttdata = NULL;
  11936. struct pt_sensing_conf_data *scd = NULL;
  11937. int rc = 0;
  11938. if (cd->mode == PT_MODE_OPERATIONAL) {
  11939. rc = pt_hid_output_get_sysinfo_(cd);
  11940. if (cd->sysinfo.ready) {
  11941. si = &cd->sysinfo;
  11942. ttdata = &si->ttdata;
  11943. scd = &si->sensing_conf_data;
  11944. } else
  11945. rc = -ENODATA;
  11946. } else
  11947. rc = -EPERM;
  11948. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11949. __func__, cd->mode, cd->sysinfo.ready);
  11950. if (!rc && ttdata && scd) {
  11951. return scnprintf(buf, strlen(buf),
  11952. "Status: %d\n"
  11953. "pip_ver_major: 0x%02X\n"
  11954. "pip_ver_minor: 0x%02X\n"
  11955. "fw_pid : 0x%04X\n"
  11956. "fw_ver_major : 0x%02X\n"
  11957. "fw_ver_minor : 0x%02X\n"
  11958. "revctrl : 0x%08X\n"
  11959. "fw_ver_conf : 0x%04X\n"
  11960. "bl_ver_major : 0x%02X\n"
  11961. "bl_ver_minor : 0x%02X\n"
  11962. "jtag_id_h : 0x%04X\n"
  11963. "jtag_id_l : 0x%04X\n"
  11964. "mfg_id[0] : 0x%02X\n"
  11965. "mfg_id[1] : 0x%02X\n"
  11966. "mfg_id[2] : 0x%02X\n"
  11967. "mfg_id[3] : 0x%02X\n"
  11968. "mfg_id[4] : 0x%02X\n"
  11969. "mfg_id[5] : 0x%02X\n"
  11970. "mfg_id[6] : 0x%02X\n"
  11971. "mfg_id[7] : 0x%02X\n"
  11972. "post_code : 0x%04X\n"
  11973. "electrodes_x : 0x%02X\n"
  11974. "electrodes_y : 0x%02X\n"
  11975. "len_x : 0x%04X\n"
  11976. "len_y : 0x%04X\n"
  11977. "res_x : 0x%04X\n"
  11978. "res_y : 0x%04X\n"
  11979. "max_z : 0x%04X\n"
  11980. "origin_x : 0x%02X\n"
  11981. "origin_y : 0x%02X\n"
  11982. "panel_id : 0x%02X\n"
  11983. "btn : 0x%02X\n"
  11984. "scan_mode : 0x%02X\n"
  11985. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11986. rc,
  11987. ttdata->pip_ver_major,
  11988. ttdata->pip_ver_minor,
  11989. ttdata->fw_pid,
  11990. ttdata->fw_ver_major,
  11991. ttdata->fw_ver_minor,
  11992. ttdata->revctrl,
  11993. ttdata->fw_ver_conf,
  11994. ttdata->bl_ver_major,
  11995. ttdata->bl_ver_minor,
  11996. ttdata->jtag_id_h,
  11997. ttdata->jtag_id_l,
  11998. ttdata->mfg_id[0],
  11999. ttdata->mfg_id[1],
  12000. ttdata->mfg_id[2],
  12001. ttdata->mfg_id[3],
  12002. ttdata->mfg_id[4],
  12003. ttdata->mfg_id[5],
  12004. ttdata->mfg_id[6],
  12005. ttdata->mfg_id[7],
  12006. ttdata->post_code,
  12007. scd->electrodes_x,
  12008. scd->electrodes_y,
  12009. scd->len_x,
  12010. scd->len_y,
  12011. scd->res_x,
  12012. scd->res_y,
  12013. scd->max_z,
  12014. scd->origin_x,
  12015. scd->origin_y,
  12016. scd->panel_id,
  12017. scd->btn,
  12018. scd->scan_mode,
  12019. scd->max_tch);
  12020. } else {
  12021. return scnprintf(buf, strlen(buf),
  12022. "Status: %d\n",
  12023. rc);
  12024. }
  12025. }
  12026. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12027. /*******************************************************************************
  12028. * FUNCTION: pt_hw_reset_show
  12029. *
  12030. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12031. * by toggling the XRES line and then calls the startup function to
  12032. * allow TTDL to re-enumerate the DUT.
  12033. * The printed value reflects the status of the full reset/enum.
  12034. *
  12035. * PARAMETERS:
  12036. * *dev - pointer to Device structure
  12037. * *attr - pointer to the device attribute structure
  12038. * *buf - pointer to buffer to print
  12039. ******************************************************************************/
  12040. static ssize_t pt_hw_reset_show(struct device *dev,
  12041. struct device_attribute *attr, char *buf)
  12042. {
  12043. struct pt_core_data *cd = dev_get_drvdata(dev);
  12044. int rc = 0;
  12045. int time = 0;
  12046. u8 reset_status = 0;
  12047. int t;
  12048. struct pt_hid_desc hid_desc;
  12049. memset(&hid_desc, 0, sizeof(hid_desc));
  12050. /* Only allow DUT reset if no active BL in progress */
  12051. mutex_lock(&cd->firmware_class_lock);
  12052. mutex_lock(&cd->system_lock);
  12053. cd->startup_state = STARTUP_NONE;
  12054. mutex_unlock(&(cd->system_lock));
  12055. pt_stop_wd_timer(cd);
  12056. /* ensure no left over exclusive access is still locked */
  12057. release_exclusive(cd, cd->dev);
  12058. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12059. if (rc) {
  12060. mutex_unlock(&cd->firmware_class_lock);
  12061. pt_debug(cd->dev, DL_ERROR,
  12062. "%s: HW reset failed rc = %d\n", __func__, rc);
  12063. goto exit_hw_reset;
  12064. }
  12065. reset_status |= 0x01 << 0;
  12066. if (cd->flashless_dut) {
  12067. mutex_unlock(&cd->firmware_class_lock);
  12068. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12069. msecs_to_jiffies(200));
  12070. if (IS_TMO(t)) {
  12071. pt_debug(dev, DL_ERROR,
  12072. "%s: Timeout waiting for FW update",
  12073. __func__);
  12074. rc = -ETIME;
  12075. goto exit_hw_reset;
  12076. } else {
  12077. pt_debug(dev, DL_INFO,
  12078. "%s: ----- Wait FW Loading ----",
  12079. __func__);
  12080. rc = _pt_request_wait_for_enum_state(
  12081. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12082. if (rc) {
  12083. pt_debug(cd->dev, DL_ERROR,
  12084. "%s: No FW Sentinel detected rc = %d\n",
  12085. __func__, rc);
  12086. goto exit_hw_reset;
  12087. }
  12088. reset_status |= 0x01 << 1;
  12089. }
  12090. } else {
  12091. /* Wait for any sentinel */
  12092. rc = _pt_request_wait_for_enum_state(dev, 150,
  12093. STARTUP_STATUS_BL_RESET_SENTINEL |
  12094. STARTUP_STATUS_FW_RESET_SENTINEL);
  12095. if (rc) {
  12096. mutex_unlock(&cd->firmware_class_lock);
  12097. pt_debug(cd->dev, DL_ERROR,
  12098. "%s: No Sentinel detected rc = %d\n",
  12099. __func__, rc);
  12100. goto exit_hw_reset;
  12101. }
  12102. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12103. msleep(30);
  12104. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12105. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12106. if (rc < 0) {
  12107. pt_debug(cd->dev, DL_ERROR,
  12108. "%s: Error on getting HID descriptor r=%d\n",
  12109. __func__, rc);
  12110. goto exit_hw_reset;
  12111. }
  12112. cd->mode = pt_get_mode(cd, &hid_desc);
  12113. if (cd->mode == PT_MODE_BOOTLOADER)
  12114. rc = pt_hid_output_bl_launch_app_(cd);
  12115. } else {
  12116. if (cd->mode == PT_MODE_BOOTLOADER)
  12117. rc = pt_pip2_launch_app(dev,
  12118. PT_CORE_CMD_UNPROTECTED);
  12119. }
  12120. if (rc) {
  12121. mutex_unlock(&cd->firmware_class_lock);
  12122. pt_debug(cd->dev, DL_ERROR,
  12123. "%s: PIP launch app failed rc = %d\n",
  12124. __func__, rc);
  12125. goto exit_hw_reset;
  12126. }
  12127. mutex_unlock(&cd->firmware_class_lock);
  12128. reset_status |= 0x01 << 1;
  12129. msleep(20);
  12130. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12131. (cd->mode != PT_MODE_OPERATIONAL))
  12132. pt_queue_restart(cd);
  12133. else
  12134. pt_queue_enum(cd);
  12135. }
  12136. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12137. msleep(50);
  12138. pt_debug(cd->dev, DL_INFO,
  12139. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12140. __func__, time, STARTUP_STATUS_COMPLETE,
  12141. cd->startup_status);
  12142. time += 50;
  12143. }
  12144. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12145. rc = -ETIME;
  12146. goto exit_hw_reset;
  12147. }
  12148. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12149. __func__, cd->startup_status);
  12150. reset_status |= 0x01 << 2;
  12151. pt_start_wd_timer(cd);
  12152. exit_hw_reset:
  12153. return scnprintf(buf, strlen(buf),
  12154. "Status: %d\n"
  12155. "Reset Status: 0x%02X\n", rc, reset_status);
  12156. }
  12157. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12158. /*******************************************************************************
  12159. * FUNCTION: pt_pip2_cmd_rsp_store
  12160. *
  12161. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12162. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12163. * Command byte order:
  12164. * Byte [0] - PIP2 command ID
  12165. * Byte [1-n] - PIP2 command payload
  12166. *
  12167. * RETURN: Size of passed in buffer
  12168. *
  12169. * PARAMETERS:
  12170. * *dev - pointer to device structure
  12171. * *attr - pointer to device attributes
  12172. * *buf - pointer to buffer that hold the command parameters
  12173. * size - size of buf
  12174. ******************************************************************************/
  12175. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12176. struct device_attribute *attr, const char *buf, size_t size)
  12177. {
  12178. struct pt_core_data *cd = dev_get_drvdata(dev);
  12179. u16 actual_read_len;
  12180. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12181. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12182. u8 pip2_cmd_id = 0x00;
  12183. u8 *pip2_cmd_data = NULL;
  12184. int data_len = 0;
  12185. int length;
  12186. int rc = 0;
  12187. /* clear shared data */
  12188. mutex_lock(&cd->sysfs_lock);
  12189. cd->raw_cmd_status = 0;
  12190. cd->cmd_rsp_buf_len = 0;
  12191. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12192. mutex_unlock(&cd->sysfs_lock);
  12193. length = _pt_ic_parse_input_hex(dev, buf, size,
  12194. input_data, PT_MAX_PIP2_MSG_SIZE);
  12195. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12196. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12197. __func__);
  12198. rc = -EINVAL;
  12199. goto exit;
  12200. }
  12201. /* Send PIP2 command if enough data was provided */
  12202. if (length >= 1) {
  12203. pip2_cmd_id = input_data[0];
  12204. pip2_cmd_data = &input_data[1];
  12205. data_len = length - 1;
  12206. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12207. pip2_cmd_id, pip2_cmd_data, data_len,
  12208. read_buf, &actual_read_len);
  12209. cd->raw_cmd_status = rc;
  12210. if (rc) {
  12211. pt_debug(dev, DL_ERROR,
  12212. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12213. __func__, pip2_cmd_id, rc);
  12214. goto exit;
  12215. } else {
  12216. cd->cmd_rsp_buf_len = actual_read_len;
  12217. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12218. pt_debug(dev, DL_ERROR,
  12219. "%s: PIP2 actual_read_len = %d\n",
  12220. __func__, actual_read_len);
  12221. }
  12222. } else {
  12223. rc = -EINVAL;
  12224. pt_debug(dev, DL_ERROR,
  12225. "%s: Insufficient data provided for PIP2 cmd\n",
  12226. __func__);
  12227. }
  12228. exit:
  12229. if (rc)
  12230. return rc;
  12231. return size;
  12232. }
  12233. /*******************************************************************************
  12234. * FUNCTION: pt_pip2_cmd_rsp_show
  12235. *
  12236. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12237. * response generated after using the store method of the pip2_cmd_rsp
  12238. * sysfs node, are available to be read here.
  12239. *
  12240. * PARAMETERS:
  12241. * *dev - pointer to Device structure
  12242. * *attr - pointer to the device attribute structure
  12243. * *buf - pointer to buffer to print
  12244. ******************************************************************************/
  12245. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12246. struct device_attribute *attr, char *buf)
  12247. {
  12248. struct pt_core_data *cd = dev_get_drvdata(dev);
  12249. int i;
  12250. ssize_t data_len;
  12251. int index;
  12252. mutex_lock(&cd->sysfs_lock);
  12253. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12254. "Status: %d\n", cd->raw_cmd_status);
  12255. if (cd->raw_cmd_status)
  12256. goto error;
  12257. /* Remove the CRC from the length of the response */
  12258. data_len = cd->cmd_rsp_buf_len - 2;
  12259. /* Start printing from the data payload */
  12260. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12261. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12262. "%02X ", cd->cmd_rsp_buf[i]);
  12263. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12264. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12265. "\n(%zd bytes)\n",
  12266. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12267. } else {
  12268. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12269. "\n(%zd bytes)\n", 0);
  12270. }
  12271. error:
  12272. mutex_unlock(&cd->sysfs_lock);
  12273. return index;
  12274. }
  12275. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12276. pt_pip2_cmd_rsp_store);
  12277. /*******************************************************************************
  12278. * FUNCTION: pt_command_store
  12279. *
  12280. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12281. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12282. * TTDL will not parse the command.
  12283. *
  12284. * RETURN: Size of passed in buffer
  12285. *
  12286. * PARAMETERS:
  12287. * *dev - pointer to device structure
  12288. * *attr - pointer to device attributes
  12289. * *buf - pointer to buffer that hold the command parameters
  12290. * size - size of buf
  12291. ******************************************************************************/
  12292. static ssize_t pt_command_store(struct device *dev,
  12293. struct device_attribute *attr, const char *buf, size_t size)
  12294. {
  12295. struct pt_core_data *cd = dev_get_drvdata(dev);
  12296. unsigned short crc;
  12297. u16 actual_read_len;
  12298. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12299. int length;
  12300. int len_field;
  12301. int rc = 0;
  12302. mutex_lock(&cd->sysfs_lock);
  12303. cd->cmd_rsp_buf_len = 0;
  12304. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12305. mutex_unlock(&cd->sysfs_lock);
  12306. length = _pt_ic_parse_input_hex(dev, buf, size,
  12307. input_data, PT_MAX_PIP2_MSG_SIZE);
  12308. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12309. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12310. __func__);
  12311. rc = -EINVAL;
  12312. goto pt_command_store_exit;
  12313. }
  12314. /* PIP2 messages begin with 01 01 */
  12315. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12316. cd->pip2_prot_active = 1;
  12317. /* Override next seq tag with what was sent */
  12318. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12319. /* For PIP2 cmd if length does not include crc, add it */
  12320. len_field = (input_data[3] << 8) | input_data[2];
  12321. if (len_field == length && length <= 254) {
  12322. crc = crc_ccitt_calculate(&input_data[2],
  12323. length - 2);
  12324. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12325. __func__, length, crc);
  12326. input_data[length] = (crc & 0xFF00) >> 8;
  12327. input_data[length + 1] = crc & 0x00FF;
  12328. length = length + 2;
  12329. }
  12330. }
  12331. /* write PIP command to log */
  12332. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12333. pm_runtime_get_sync(dev);
  12334. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12335. length, input_data, &actual_read_len);
  12336. pm_runtime_put(dev);
  12337. mutex_lock(&cd->sysfs_lock);
  12338. cd->raw_cmd_status = rc;
  12339. if (rc) {
  12340. cd->cmd_rsp_buf_len = 0;
  12341. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12342. __func__, buf);
  12343. } else {
  12344. cd->cmd_rsp_buf_len = actual_read_len;
  12345. }
  12346. cd->pip2_prot_active = 0;
  12347. mutex_unlock(&cd->sysfs_lock);
  12348. pt_command_store_exit:
  12349. if (rc)
  12350. return rc;
  12351. return size;
  12352. }
  12353. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12354. /*******************************************************************************
  12355. * FUNCTION: pt_response_show
  12356. *
  12357. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12358. * response generated after using the pt_command_store sysfs node, are
  12359. * available to be read here.
  12360. *
  12361. * PARAMETERS:
  12362. * *dev - pointer to Device structure
  12363. * *attr - pointer to the device attribute structure
  12364. * *buf - pointer to buffer to print
  12365. ******************************************************************************/
  12366. static ssize_t pt_response_show(struct device *dev,
  12367. struct device_attribute *attr, char *buf)
  12368. {
  12369. struct pt_core_data *cd = dev_get_drvdata(dev);
  12370. int i;
  12371. ssize_t num_read;
  12372. int index;
  12373. mutex_lock(&cd->sysfs_lock);
  12374. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12375. "Status: %d\n", cd->raw_cmd_status);
  12376. if (cd->raw_cmd_status)
  12377. goto error;
  12378. num_read = cd->cmd_rsp_buf_len;
  12379. for (i = 0; i < num_read; i++)
  12380. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12381. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12382. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12383. "(%zd bytes)\n", num_read);
  12384. error:
  12385. mutex_unlock(&cd->sysfs_lock);
  12386. return index;
  12387. }
  12388. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12389. /*******************************************************************************
  12390. * FUNCTION: pt_dut_debug_show
  12391. *
  12392. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12393. * are available for the store method.
  12394. *
  12395. * RETURN:
  12396. * 0 = success
  12397. * !0 = failure
  12398. *
  12399. * PARAMETERS:
  12400. * *dev - pointer to device structure
  12401. * *attr - pointer to device attributes
  12402. * *buf - pointer to output buffer
  12403. ******************************************************************************/
  12404. static ssize_t pt_dut_debug_show(struct device *dev,
  12405. struct device_attribute *attr, char *buf)
  12406. {
  12407. ssize_t ret;
  12408. ret = scnprintf(buf, strlen(buf),
  12409. "Status: 0\n"
  12410. "dut_debug sends the following commands to the DUT:\n"
  12411. "%d %s \t- %s\n"
  12412. "%d %s \t- %s\n"
  12413. "%d %s \t- %s\n"
  12414. "%d %s \t- %s\n"
  12415. "%d %s \t- %s\n"
  12416. "%d %s \t- %s\n"
  12417. "%d %s \t- %s\n"
  12418. "%d %s \t- %s\n"
  12419. "%d %s \t- %s\n"
  12420. "%d %s \t- %s\n"
  12421. "%d %s \t- %s\n"
  12422. "%d %s \t- %s\n"
  12423. "%d %s \t- %s\n"
  12424. "%d %s \t- %s\n"
  12425. "%d %s \t- %s\n"
  12426. "%d %s \t- %s\n"
  12427. "%d %s \t- %s\n"
  12428. ,
  12429. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12430. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12431. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12432. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12433. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12434. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12435. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12436. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12437. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12438. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12439. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12440. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12441. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12442. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12443. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12444. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12445. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12446. );
  12447. return ret;
  12448. }
  12449. /*******************************************************************************
  12450. * FUNCTION: pt_drv_debug_show
  12451. *
  12452. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12453. * are available for the store method.
  12454. *
  12455. * RETURN:
  12456. * 0 = success
  12457. * !0 = failure
  12458. *
  12459. * PARAMETERS:
  12460. * *dev - pointer to device structure
  12461. * *attr - pointer to device attributes
  12462. * *buf - pointer to output buffer
  12463. ******************************************************************************/
  12464. static ssize_t pt_drv_debug_show(struct device *dev,
  12465. struct device_attribute *attr, char *buf)
  12466. {
  12467. ssize_t ret;
  12468. ret = scnprintf(buf, strlen(buf),
  12469. "Status: 0\n"
  12470. "drv_debug supports the following values:\n"
  12471. "%d %s \t- %s\n"
  12472. "%d %s \t- %s\n"
  12473. "%d %s \t- %s\n"
  12474. "%d %s \t- %s\n"
  12475. "%d %s \t- %s\n"
  12476. "%d %s \t- %s\n"
  12477. "%d %s \t- %s\n"
  12478. "%d %s \t- %s\n"
  12479. "%d %s \t- %s\n"
  12480. "%d %s - %s\n"
  12481. "%d %s \t- %s\n"
  12482. "%d %s \t- %s\n"
  12483. #ifdef TTDL_DIAGNOSTICS
  12484. "%d %s \t- %s\n"
  12485. "%d %s \t- %s\n"
  12486. "%d %s \t- %s\n"
  12487. #endif /* TTDL_DIAGNOSTICS */
  12488. "%d %s \t- %s\n"
  12489. #ifdef TTDL_DIAGNOSTICS
  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. "%d %s \t- %s\n"
  12498. "%d %s \t- %s\n"
  12499. #endif /* TTDL_DIAGNOSTICS */
  12500. ,
  12501. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12502. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12503. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12504. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12505. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12506. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12507. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12508. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12509. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12510. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12511. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12512. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12513. #ifdef TTDL_DIAGNOSTICS
  12514. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12515. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12516. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12517. #endif /* TTDL_DIAGNOSTICS */
  12518. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12519. #ifdef TTDL_DIAGNOSTICS
  12520. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12521. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12522. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12523. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12524. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12525. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12526. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12527. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12528. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12529. "TTHE_TUNER HID USB Format"
  12530. #endif /* TTDL_DIAGNOSTICS */
  12531. );
  12532. return ret;
  12533. }
  12534. /*******************************************************************************
  12535. * FUNCTION: pt_drv_debug_store
  12536. *
  12537. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12538. * dut_debug. Drv_debug will contain all functionality that can be run
  12539. * without a DUT preset and is available anytime TTDL is running.
  12540. * Dut_debug requires a DUT to be available and will only be created after
  12541. * a DUT has been detected.
  12542. * This function will eventually be split into two but until the overlap
  12543. * has been depricated this function contains all commands that can be
  12544. * used for TTDL/DUT debugging status and control.
  12545. * All commands require at least one value to be passed in *buf with some
  12546. * requiring two.
  12547. *
  12548. * RETURN: Size of passed in buffer
  12549. *
  12550. * PARAMETERS:
  12551. * *dev - pointer to device structure
  12552. * *attr - pointer to device attributes
  12553. * *buf - pointer to buffer that hold the command parameters
  12554. * size - size of buf
  12555. ******************************************************************************/
  12556. static ssize_t pt_drv_debug_store(struct device *dev,
  12557. struct device_attribute *attr, const char *buf, size_t size)
  12558. {
  12559. struct pt_core_data *cd = dev_get_drvdata(dev);
  12560. unsigned long value;
  12561. int rc = 0;
  12562. u8 return_data[8];
  12563. static u8 wd_disabled;
  12564. u32 input_data[3];
  12565. int length;
  12566. #ifdef TTDL_DIAGNOSTICS
  12567. struct i2c_client *client = to_i2c_client(dev);
  12568. unsigned short crc = 0;
  12569. u16 cal_size;
  12570. #endif
  12571. input_data[0] = 0;
  12572. input_data[1] = 0;
  12573. /* Maximmum input is two elements */
  12574. length = _pt_ic_parse_input(dev, buf, size,
  12575. input_data, ARRAY_SIZE(input_data));
  12576. if (length < 1 || length > 2) {
  12577. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12578. __func__);
  12579. rc = -EINVAL;
  12580. goto pt_drv_debug_store_exit;
  12581. }
  12582. value = input_data[0];
  12583. if (length == 1) {
  12584. pt_debug(dev, DL_DEBUG,
  12585. "%s: Debug Cmd Received (id=%d)\n",
  12586. __func__, input_data[0]);
  12587. } else if (length == 2) {
  12588. pt_debug(dev, DL_DEBUG,
  12589. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12590. __func__, input_data[0], input_data[1]);
  12591. } else {
  12592. pt_debug(dev, DL_DEBUG,
  12593. "%s: Invalid arguments received\n", __func__);
  12594. rc = -EINVAL;
  12595. goto pt_drv_debug_store_exit;
  12596. }
  12597. /* Start watchdog timer command */
  12598. if (value == PT_DRV_DBG_START_WD) {
  12599. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12600. wd_disabled = 0;
  12601. cd->watchdog_force_stop = false;
  12602. pt_start_wd_timer(cd);
  12603. goto pt_drv_debug_store_exit;
  12604. }
  12605. /* Stop watchdog timer temporarily */
  12606. pt_stop_wd_timer(cd);
  12607. if (value == PT_DRV_DBG_STOP_WD) {
  12608. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12609. wd_disabled = 1;
  12610. cd->watchdog_force_stop = true;
  12611. goto pt_drv_debug_store_exit;
  12612. }
  12613. switch (value) {
  12614. case PT_DRV_DBG_SUSPEND: /* 4 */
  12615. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12616. wd_disabled = 1;
  12617. rc = pt_core_sleep(cd);
  12618. if (rc)
  12619. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12620. __func__, rc);
  12621. else
  12622. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12623. __func__);
  12624. break;
  12625. case PT_DRV_DBG_RESUME: /* 5 */
  12626. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12627. rc = pt_core_wake(cd);
  12628. if (rc)
  12629. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12630. __func__, rc);
  12631. else
  12632. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12633. __func__);
  12634. break;
  12635. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12636. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12637. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12638. break;
  12639. case PT_DUT_DBG_HID_RESET: /* 50 */
  12640. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12641. pt_hid_cmd_reset(cd);
  12642. break;
  12643. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12644. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12645. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12646. wd_disabled = 0;
  12647. break;
  12648. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12649. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12650. __func__);
  12651. wd_disabled = 1;
  12652. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12653. break;
  12654. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12655. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12656. __func__);
  12657. wd_disabled = 1;
  12658. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12659. break;
  12660. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12661. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12662. pt_hid_output_bl_get_information(cd, return_data);
  12663. break;
  12664. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12665. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12666. __func__);
  12667. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12668. break;
  12669. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12670. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12671. pt_hid_output_bl_launch_app(cd);
  12672. break;
  12673. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12674. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12675. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12676. break;
  12677. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12678. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12679. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12680. break;
  12681. case PT_DUT_DBG_RESET: /* 98 */
  12682. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12683. rc = pt_hw_hard_reset(cd);
  12684. break;
  12685. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12686. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12687. pt_pip_null(cd);
  12688. break;
  12689. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12690. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12691. rc = pt_pip_start_bootloader(cd);
  12692. if (!rc) {
  12693. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12694. cd->mode = PT_MODE_BOOTLOADER;
  12695. }
  12696. break;
  12697. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12698. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12699. pt_hid_output_get_sysinfo(cd);
  12700. break;
  12701. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12702. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12703. pt_pip_suspend_scanning(cd);
  12704. break;
  12705. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12706. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12707. pt_pip_resume_scanning(cd);
  12708. break;
  12709. #ifdef TTHE_TUNER_SUPPORT
  12710. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12711. cd->tthe_exit = 1;
  12712. wake_up(&cd->wait_q);
  12713. kfree(cd->tthe_buf);
  12714. cd->tthe_buf = NULL;
  12715. cd->tthe_exit = 0;
  12716. break;
  12717. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12718. if (cd->tthe_buf)
  12719. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12720. else
  12721. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12722. __func__);
  12723. break;
  12724. #endif
  12725. #ifdef TTDL_DIAGNOSTICS
  12726. case PT_DUT_DBG_HID_DESC: /* 109 */
  12727. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12728. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12729. break;
  12730. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12731. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12732. __func__);
  12733. pt_erase_parameter_list(cd);
  12734. break;
  12735. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12736. rc = pt_read_input(cd);
  12737. if (!rc)
  12738. pt_parse_input(cd);
  12739. break;
  12740. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12741. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12742. &cal_size, &crc);
  12743. if (rc)
  12744. pt_debug(dev, DL_ERROR,
  12745. "%s: CAL Data clear failed rc=%d\n",
  12746. __func__, rc);
  12747. else
  12748. pt_debug(dev, DL_INFO,
  12749. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12750. __func__, crc, size);
  12751. break;
  12752. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12753. mutex_lock(&cd->system_lock);
  12754. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12755. cd->debug_level = input_data[1];
  12756. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12757. __func__, cd->debug_level);
  12758. } else {
  12759. rc = -EINVAL;
  12760. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12761. __func__, input_data[1]);
  12762. }
  12763. mutex_unlock(&(cd->system_lock));
  12764. break;
  12765. #endif
  12766. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12767. mutex_lock(&cd->system_lock);
  12768. if (input_data[1] > 100) {
  12769. cd->watchdog_interval = input_data[1];
  12770. pt_debug(dev, DL_INFO,
  12771. "%s: Set watchdog_ interval to: %d\n",
  12772. __func__, cd->watchdog_interval);
  12773. pt_start_wd_timer(cd);
  12774. } else {
  12775. rc = -EINVAL;
  12776. pt_debug(dev, DL_ERROR,
  12777. "%s: Invalid watchdog interval: %d\n",
  12778. __func__, input_data[1]);
  12779. }
  12780. mutex_unlock(&(cd->system_lock));
  12781. break;
  12782. #ifdef TTDL_DIAGNOSTICS
  12783. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12784. mutex_lock(&cd->system_lock);
  12785. if (input_data[1] == 0) {
  12786. cd->show_timestamp = 0;
  12787. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12788. __func__);
  12789. } else if (input_data[1] == 1) {
  12790. cd->show_timestamp = 1;
  12791. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12792. __func__);
  12793. } else {
  12794. rc = -EINVAL;
  12795. pt_debug(dev, DL_ERROR,
  12796. "%s: Invalid parameter: %d\n",
  12797. __func__, input_data[1]);
  12798. }
  12799. mutex_unlock(&(cd->system_lock));
  12800. break;
  12801. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12802. if (input_data[1] == 0) {
  12803. cd->cpdata->setup_power(cd->cpdata,
  12804. PT_MT_POWER_OFF, cd->dev);
  12805. pt_debug(dev, DL_INFO,
  12806. "%s: Initiate Power Off\n", __func__);
  12807. } else if (input_data[1] == 1) {
  12808. cd->cpdata->setup_power(cd->cpdata,
  12809. PT_MT_POWER_ON, cd->dev);
  12810. pt_debug(dev, DL_INFO,
  12811. "%s: Initiate Power On\n", __func__);
  12812. } else {
  12813. rc = -EINVAL;
  12814. pt_debug(dev, DL_ERROR,
  12815. "%s: Invalid parameter: %d\n",
  12816. __func__, input_data[1]);
  12817. }
  12818. break;
  12819. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12820. if (input_data[1] == 0) {
  12821. pm_runtime_put(dev);
  12822. pt_debug(dev, DL_ERROR,
  12823. "%s: Force call pm_runtime_put()\n", __func__);
  12824. } else if (input_data[1] == 1) {
  12825. pm_runtime_get_sync(dev);
  12826. pt_debug(dev, DL_ERROR,
  12827. "%s: Force call pm_runtime_get_sync()\n",
  12828. __func__);
  12829. } else {
  12830. rc = -EINVAL;
  12831. pt_debug(dev, DL_ERROR,
  12832. "%s: WARNING: Invalid parameter: %d\n",
  12833. __func__, input_data[1]);
  12834. }
  12835. break;
  12836. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12837. mutex_lock(&cd->system_lock);
  12838. if (input_data[1] == 0) {
  12839. cd->show_tt_data = false;
  12840. pt_debug(dev, DL_INFO,
  12841. "%s: Disable TT_DATA\n", __func__);
  12842. } else if (input_data[1] == 1) {
  12843. cd->show_tt_data = true;
  12844. pt_debug(dev, DL_INFO,
  12845. "%s: Enable TT_DATA\n", __func__);
  12846. } else {
  12847. rc = -EINVAL;
  12848. pt_debug(dev, DL_ERROR,
  12849. "%s: Invalid parameter: %d\n",
  12850. __func__, input_data[1]);
  12851. }
  12852. mutex_unlock(&(cd->system_lock));
  12853. break;
  12854. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12855. if (input_data[1] == cd->active_dut_generation) {
  12856. mutex_lock(&cd->system_lock);
  12857. cd->set_dut_generation = true;
  12858. mutex_unlock(&(cd->system_lock));
  12859. } else {
  12860. mutex_lock(&cd->system_lock);
  12861. if (input_data[1] == 0) {
  12862. cd->active_dut_generation = DUT_UNKNOWN;
  12863. cd->set_dut_generation = false;
  12864. } else if (input_data[1] == 1) {
  12865. cd->active_dut_generation = DUT_PIP1_ONLY;
  12866. cd->set_dut_generation = true;
  12867. } else if (input_data[1] == 2) {
  12868. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12869. cd->set_dut_generation = true;
  12870. } else {
  12871. rc = -EINVAL;
  12872. pt_debug(dev, DL_ERROR,
  12873. "%s: Invalid parameter: %d\n",
  12874. __func__, input_data[1]);
  12875. break;
  12876. }
  12877. cd->startup_status = STARTUP_STATUS_START;
  12878. pt_debug(cd->dev, DL_DEBUG,
  12879. "%s: Startup Status Reset\n", __func__);
  12880. mutex_unlock(&(cd->system_lock));
  12881. pt_debug(dev, DL_INFO,
  12882. "%s: Active DUT Generation Set to: %d\n",
  12883. __func__, cd->active_dut_generation);
  12884. /* Changing DUT generations full restart needed */
  12885. pt_queue_restart(cd);
  12886. }
  12887. break;
  12888. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12889. mutex_lock(&cd->system_lock);
  12890. if (input_data[1] == 0) {
  12891. cd->bridge_mode = false;
  12892. pt_debug(dev, DL_INFO,
  12893. "%s: Disable Bridge Mode\n", __func__);
  12894. } else if (input_data[1] == 1) {
  12895. cd->bridge_mode = true;
  12896. pt_debug(dev, DL_INFO,
  12897. "%s: Enable Bridge Mode\n", __func__);
  12898. } else {
  12899. rc = -EINVAL;
  12900. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12901. __func__, input_data[1]);
  12902. }
  12903. mutex_unlock(&(cd->system_lock));
  12904. break;
  12905. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12906. mutex_lock(&cd->system_lock);
  12907. /* Only a 7bit address is valid */
  12908. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12909. client->addr = input_data[1];
  12910. pt_debug(dev, DL_INFO,
  12911. "%s: Set I2C Address: 0x%2X\n",
  12912. __func__, client->addr);
  12913. } else {
  12914. rc = -EINVAL;
  12915. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12916. __func__, input_data[1]);
  12917. client->addr = 0x24;
  12918. }
  12919. mutex_unlock(&(cd->system_lock));
  12920. break;
  12921. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12922. mutex_lock(&cd->system_lock);
  12923. if (input_data[1] == 0) {
  12924. cd->flashless_dut = 0;
  12925. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12926. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12927. __func__);
  12928. } else if (input_data[1] == 1) {
  12929. cd->flashless_dut = 1;
  12930. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12931. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12932. __func__);
  12933. }
  12934. mutex_unlock(&(cd->system_lock));
  12935. break;
  12936. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12937. mutex_lock(&cd->system_lock);
  12938. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12939. cd->force_pip2_seq = input_data[1];
  12940. cd->pip2_cmd_tag_seq = input_data[1];
  12941. pt_debug(dev, DL_INFO,
  12942. "%s: Force PIP2 Seq to: 0x%02X\n",
  12943. __func__, input_data[1]);
  12944. } else {
  12945. cd->force_pip2_seq = 0;
  12946. pt_debug(dev, DL_INFO,
  12947. "%s: Clear Forced PIP2 Seq\n", __func__);
  12948. }
  12949. mutex_unlock(&(cd->system_lock));
  12950. break;
  12951. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12952. mutex_lock(&cd->system_lock);
  12953. if (input_data[1] == 0) {
  12954. cd->bl_with_no_int = 0;
  12955. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12956. } else if (input_data[1] == 1) {
  12957. cd->bl_with_no_int = 1;
  12958. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12959. __func__);
  12960. } else {
  12961. rc = -EINVAL;
  12962. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12963. __func__, input_data[1]);
  12964. }
  12965. mutex_unlock(&(cd->system_lock));
  12966. break;
  12967. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12968. mutex_lock(&cd->system_lock);
  12969. if (input_data[1] == 0) {
  12970. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12971. pt_debug(dev, DL_INFO,
  12972. "%s: Disable Calibration cache in host\n",
  12973. __func__);
  12974. } else if (input_data[1] == 1) {
  12975. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12976. pt_debug(dev, DL_INFO,
  12977. "%s: Enable Calibration cache in host\n",
  12978. __func__);
  12979. } else {
  12980. rc = -EINVAL;
  12981. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12982. __func__, input_data[1]);
  12983. }
  12984. mutex_unlock(&(cd->system_lock));
  12985. break;
  12986. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12987. mutex_lock(&cd->system_lock);
  12988. if (input_data[1] == 0) {
  12989. cd->multi_chip = PT_FEATURE_DISABLE;
  12990. cd->ttdl_bist_select = 0x07;
  12991. pt_debug(dev, DL_INFO,
  12992. "%s: Disable Multi-chip support\n", __func__);
  12993. } else if (input_data[1] == 1) {
  12994. cd->multi_chip = PT_FEATURE_ENABLE;
  12995. cd->ttdl_bist_select = 0x3F;
  12996. pt_debug(dev, DL_INFO,
  12997. "%s: Enable Multi-chip support\n", __func__);
  12998. } else {
  12999. rc = -EINVAL;
  13000. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13001. __func__, input_data[1]);
  13002. }
  13003. mutex_unlock(&(cd->system_lock));
  13004. break;
  13005. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13006. mutex_lock(&cd->system_lock);
  13007. if (input_data[1] <= 0x07) {
  13008. cd->panel_id_support = input_data[1];
  13009. pt_debug(dev, DL_INFO,
  13010. "%s: Set panel_id_support to %d\n",
  13011. __func__, cd->panel_id_support);
  13012. } else {
  13013. rc = -EINVAL;
  13014. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13015. __func__, input_data[1]);
  13016. }
  13017. mutex_unlock(&(cd->system_lock));
  13018. break;
  13019. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13020. mutex_lock(&cd->system_lock);
  13021. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13022. /*
  13023. * The timeout is changed for some cases so the
  13024. * pip_cmd_timeout_default is used to retore back to
  13025. * what the user requested as the new timeout.
  13026. */
  13027. cd->pip_cmd_timeout_default = input_data[1];
  13028. cd->pip_cmd_timeout = input_data[1];
  13029. pt_debug(dev, DL_INFO,
  13030. "%s: PIP Timeout = %d\n", __func__,
  13031. cd->pip_cmd_timeout_default);
  13032. } else {
  13033. rc = -EINVAL;
  13034. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13035. __func__, input_data[1]);
  13036. }
  13037. mutex_unlock(&(cd->system_lock));
  13038. break;
  13039. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13040. mutex_lock(&cd->system_lock);
  13041. if (input_data[1] == 0) {
  13042. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13043. pt_debug(dev, DL_INFO,
  13044. "%s: Disable tthe_tuner HID-USB format\n",
  13045. __func__);
  13046. } else if (input_data[1] == 1) {
  13047. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13048. pt_debug(dev, DL_INFO,
  13049. "%s: Enable tthe_tuner HID-USB format\n",
  13050. __func__);
  13051. } else {
  13052. rc = -EINVAL;
  13053. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13054. __func__, input_data[1]);
  13055. }
  13056. mutex_unlock(&(cd->system_lock));
  13057. break;
  13058. #endif /* TTDL_DIAGNOSTICS */
  13059. default:
  13060. rc = -EINVAL;
  13061. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13062. }
  13063. /* Enable watchdog timer */
  13064. if (!wd_disabled)
  13065. pt_start_wd_timer(cd);
  13066. pt_drv_debug_store_exit:
  13067. if (rc)
  13068. return rc;
  13069. return size;
  13070. }
  13071. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13072. pt_drv_debug_store);
  13073. /*******************************************************************************
  13074. * FUNCTION: pt_sleep_status_show
  13075. *
  13076. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13077. * sleep status as either ON or OFF
  13078. *
  13079. * RETURN:
  13080. * 0 = success
  13081. * !0 = failure
  13082. *
  13083. * PARAMETERS:
  13084. * *dev - pointer to device structure
  13085. * *attr - pointer to device attributes
  13086. * *buf - pointer to output buffer
  13087. ******************************************************************************/
  13088. static ssize_t pt_sleep_status_show(struct device *dev,
  13089. struct device_attribute *attr, char *buf)
  13090. {
  13091. struct pt_core_data *cd = dev_get_drvdata(dev);
  13092. ssize_t ret;
  13093. mutex_lock(&cd->system_lock);
  13094. if (cd->sleep_state == SS_SLEEP_ON)
  13095. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13096. else
  13097. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13098. mutex_unlock(&cd->system_lock);
  13099. return ret;
  13100. }
  13101. /*******************************************************************************
  13102. * FUNCTION: pt_panel_id_show
  13103. *
  13104. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13105. * detected panel ID from the DUT
  13106. *
  13107. * RETURN:
  13108. * 0 = success
  13109. * !0 = failure
  13110. *
  13111. * PARAMETERS:
  13112. * *dev - pointer to device structure
  13113. * *attr - pointer to device attributes
  13114. * *buf - pointer to output buffer
  13115. ******************************************************************************/
  13116. static ssize_t pt_panel_id_show(struct device *dev,
  13117. struct device_attribute *attr, char *buf)
  13118. {
  13119. struct pt_core_data *cd = dev_get_drvdata(dev);
  13120. ssize_t ret;
  13121. u8 pid = PANEL_ID_NOT_ENABLED;
  13122. int rc = 0;
  13123. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13124. /*
  13125. * The DUT should report the same panel ID from both the BL and
  13126. * the FW unless the panel_id feature is set to only
  13127. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13128. * to retrieve the panel_id.
  13129. */
  13130. if (cd->mode == PT_MODE_BOOTLOADER) {
  13131. /*
  13132. * Return the stored value if PT_PANEL_ID_BY_BL
  13133. * is not supported while other feature exits.
  13134. */
  13135. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13136. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13137. if (rc) {
  13138. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13139. "Failed to retrieve Panel ID. ",
  13140. "Using cached value\n",
  13141. __func__);
  13142. }
  13143. }
  13144. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13145. if (cd->panel_id_support &
  13146. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13147. /* For all systems sysinfo has the panel_id */
  13148. rc = pt_hid_output_get_sysinfo(cd);
  13149. if (!rc)
  13150. pid =
  13151. cd->sysinfo.sensing_conf_data.panel_id;
  13152. pt_debug(dev, DL_ERROR,
  13153. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13154. __func__, rc, pid);
  13155. }
  13156. } else {
  13157. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13158. __func__);
  13159. rc = -EPERM;
  13160. }
  13161. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13162. if (cd->mode == PT_MODE_BOOTLOADER) {
  13163. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13164. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13165. if (rc) {
  13166. pt_debug(dev, DL_ERROR,
  13167. "%s: BL get panel ID failed rc=%d\n",
  13168. __func__, rc);
  13169. }
  13170. }
  13171. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13172. if (cd->panel_id_support &
  13173. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13174. rc = pt_hid_output_get_sysinfo(cd);
  13175. if (!rc)
  13176. pid =
  13177. cd->sysinfo.sensing_conf_data.panel_id;
  13178. pt_debug(dev, DL_ERROR,
  13179. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13180. __func__, rc, pid);
  13181. }
  13182. } else {
  13183. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13184. __func__);
  13185. rc = -EPERM;
  13186. }
  13187. } else {
  13188. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13189. __func__);
  13190. rc = -EPERM;
  13191. }
  13192. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13193. rc, pid);
  13194. return ret;
  13195. }
  13196. /*******************************************************************************
  13197. * FUNCTION: pt_get_param_store
  13198. *
  13199. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13200. * ID to retrieve with the show method.
  13201. *
  13202. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13203. *
  13204. * RETURN: Size of passed in buffer if successful
  13205. *
  13206. * PARAMETERS:
  13207. * *dev - pointer to device structure
  13208. * *attr - pointer to device attributes
  13209. * *buf - pointer to output buffer
  13210. * size - size of buf
  13211. ******************************************************************************/
  13212. static ssize_t pt_get_param_store(struct device *dev,
  13213. struct device_attribute *attr, const char *buf, size_t size)
  13214. {
  13215. struct pt_core_data *cd = dev_get_drvdata(dev);
  13216. u32 input_data[2];
  13217. int length;
  13218. int rc = 0;
  13219. /* Maximum input of one value */
  13220. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13221. ARRAY_SIZE(input_data));
  13222. if (length != 1) {
  13223. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13224. __func__);
  13225. rc = -EINVAL;
  13226. goto exit;
  13227. }
  13228. mutex_lock(&cd->system_lock);
  13229. cd->get_param_id = input_data[0];
  13230. mutex_unlock(&(cd->system_lock));
  13231. exit:
  13232. if (rc)
  13233. return rc;
  13234. return size;
  13235. }
  13236. /*******************************************************************************
  13237. * FUNCTION: pt_get_param_show
  13238. *
  13239. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13240. * parameter data from the DUT based on the ID stored in the core
  13241. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13242. * communicate or some other error occures, an error status is returned
  13243. * with no value following.
  13244. * Output is in the form:
  13245. * Status: x
  13246. * 0xyyyyyyyy
  13247. * The 32bit data will only follow the status code if the status == 0
  13248. *
  13249. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13250. *
  13251. * RETURN:
  13252. * 0 = success
  13253. * !0 = failure
  13254. *
  13255. * PARAMETERS:
  13256. * *dev - pointer to device structure
  13257. * *attr - pointer to device attributes
  13258. * *buf - pointer to output buffer
  13259. ******************************************************************************/
  13260. static ssize_t pt_get_param_show(struct device *dev,
  13261. struct device_attribute *attr, char *buf)
  13262. {
  13263. struct pt_core_data *cd = dev_get_drvdata(dev);
  13264. ssize_t ret = 0;
  13265. int status;
  13266. u32 value = 0;
  13267. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13268. if (status) {
  13269. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13270. __func__, "pt_get_param", status);
  13271. ret = scnprintf(buf, strlen(buf),
  13272. "%s %d\n",
  13273. "Status:", status);
  13274. } else {
  13275. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13276. __func__, cd->get_param_id, value);
  13277. ret = scnprintf(buf, strlen(buf),
  13278. "Status: %d\n"
  13279. "0x%04X\n",
  13280. status, value);
  13281. }
  13282. return ret;
  13283. }
  13284. /*******************************************************************************
  13285. * FUNCTION: pt_ttdl_restart_show
  13286. *
  13287. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13288. * probed modules, calls startup() and then re-probes modules.
  13289. *
  13290. * RETURN: size of data written to sysfs node
  13291. *
  13292. * PARAMETERS:
  13293. * *dev - pointer to device structure
  13294. * *attr - pointer to device attributes structure
  13295. * *buf - pointer to print output buffer
  13296. ******************************************************************************/
  13297. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13298. struct device_attribute *attr, char *buf)
  13299. {
  13300. struct pt_core_data *cd = dev_get_drvdata(dev);
  13301. int t;
  13302. int rc = 0;
  13303. mutex_lock(&cd->system_lock);
  13304. cd->startup_state = STARTUP_NONE;
  13305. mutex_unlock(&(cd->system_lock));
  13306. /* ensure no left over exclusive access is still locked */
  13307. release_exclusive(cd, cd->dev);
  13308. pt_queue_restart(cd);
  13309. t = wait_event_timeout(cd->wait_q,
  13310. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13311. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13312. if (IS_TMO(t)) {
  13313. pt_debug(cd->dev, DL_ERROR,
  13314. "%s: TMO waiting for FW sentinel\n", __func__);
  13315. rc = -ETIME;
  13316. }
  13317. return scnprintf(buf, strlen(buf),
  13318. "Status: %d\n"
  13319. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13320. }
  13321. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13322. /*******************************************************************************
  13323. * FUNCTION: pt_pip2_gpio_read_show
  13324. *
  13325. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13326. * contents of the response to the passed in output buffer.
  13327. *
  13328. * RETURN: size of data written to sysfs node
  13329. *
  13330. * PARAMETERS:
  13331. * *dev - pointer to device structure
  13332. * *attr - pointer to device attributes structure
  13333. * *buf - pointer to print output buffer
  13334. ******************************************************************************/
  13335. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13336. struct device_attribute *attr, char *buf)
  13337. {
  13338. struct pt_core_data *cd = dev_get_drvdata(dev);
  13339. u8 status = 0;
  13340. u32 gpio_value = 0;
  13341. int rc = 0;
  13342. /* This functionality is only available in the BL */
  13343. if (cd->mode == PT_MODE_BOOTLOADER)
  13344. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13345. else
  13346. rc = -EPERM;
  13347. if (!rc) {
  13348. if (status == 0)
  13349. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13350. "Status: %d\n"
  13351. "DUT GPIO Reg: 0x%08X\n",
  13352. rc, gpio_value);
  13353. else
  13354. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13355. "Status: %d\n"
  13356. "DUT GPIO Reg: n/a\n",
  13357. status);
  13358. } else
  13359. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13360. "Status: %d\n"
  13361. "DUT GPIO Reg: n/a\n",
  13362. rc);
  13363. }
  13364. /*******************************************************************************
  13365. * FUNCTION: pt_pip2_version_show
  13366. *
  13367. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13368. * contents of the response to the passed in output buffer.
  13369. *
  13370. * RETURN: size of data written to sysfs node
  13371. *
  13372. * PARAMETERS:
  13373. * *dev - pointer to device structure
  13374. * *attr - pointer to device attributes structure
  13375. * *buf - pointer to print output buffer
  13376. ******************************************************************************/
  13377. static ssize_t pt_pip2_version_show(struct device *dev,
  13378. struct device_attribute *attr, char *buf)
  13379. {
  13380. int rc = 0;
  13381. struct pt_core_data *cd = dev_get_drvdata(dev);
  13382. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13383. rc = pt_pip2_get_version(cd);
  13384. if (!rc) {
  13385. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13386. "PIP VERSION : %02X.%02X\n"
  13387. "BL VERSION : %02X.%02X\n"
  13388. "FW VERSION : %02X.%02X\n"
  13389. "SILICON ID : %04X.%04X\n"
  13390. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13391. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13392. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13393. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13394. ttdata->chip_id, ttdata->chip_rev,
  13395. ttdata->uid[0], ttdata->uid[1],
  13396. ttdata->uid[2], ttdata->uid[3],
  13397. ttdata->uid[4], ttdata->uid[5],
  13398. ttdata->uid[6], ttdata->uid[7],
  13399. ttdata->uid[8], ttdata->uid[9],
  13400. ttdata->uid[10], ttdata->uid[11]);
  13401. } else {
  13402. pt_debug(dev, DL_ERROR,
  13403. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13404. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13405. "PIP VERSION : -\n"
  13406. "BL VERSION : -\n"
  13407. "FW VERSION : -\n"
  13408. "SILICON ID : -\n"
  13409. "UID : -\n");
  13410. }
  13411. }
  13412. #ifdef TTDL_DIAGNOSTICS
  13413. /*******************************************************************************
  13414. * FUNCTION: pt_ttdl_status_show
  13415. *
  13416. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13417. * variable states and GPIO levels. Additional information printed when
  13418. * TTDL_DIAGNOSTICS is enabled.
  13419. *
  13420. * NOTE: All counters will be reset to 0 when this function is called.
  13421. *
  13422. * RETURN:
  13423. * 0 = success
  13424. * !0 = failure
  13425. *
  13426. * PARAMETERS:
  13427. * *dev - pointer to device structure
  13428. * *attr - pointer to device attributes
  13429. * *buf - pointer to output buffer
  13430. ******************************************************************************/
  13431. static ssize_t pt_ttdl_status_show(struct device *dev,
  13432. struct device_attribute *attr, char *buf)
  13433. {
  13434. struct pt_core_data *cd = dev_get_drvdata(dev);
  13435. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13436. struct i2c_client *client = to_i2c_client(dev);
  13437. ssize_t ret;
  13438. u16 cal_size = 0;
  13439. unsigned short crc = 0;
  13440. if (cd->cal_cache_in_host)
  13441. _pt_manage_local_cal_data(dev,
  13442. PT_CAL_DATA_INFO, &cal_size, &crc);
  13443. ret = scnprintf(buf, strlen(buf),
  13444. "%s: 0x%04X\n"
  13445. "%s: %d\n"
  13446. "%s: %s\n"
  13447. "%s: %s %s\n"
  13448. "%s: %s\n"
  13449. "%s: 0x%02X\n"
  13450. "%s: %s\n"
  13451. "%s: %s\n"
  13452. "%s: %s\n"
  13453. "%s: %s\n"
  13454. "%s: %d\n"
  13455. "%s: %d\n"
  13456. "%s: %s\n"
  13457. "%s: %s\n"
  13458. "%s: %d\n"
  13459. #ifdef TTDL_DIAGNOSTICS
  13460. "%s: %d\n"
  13461. "%s: %d\n"
  13462. "%s: %d\n"
  13463. "%s: %d\n"
  13464. "%s: %d\n"
  13465. "%s: %d\n"
  13466. "%s: %d\n"
  13467. "%s: %d\n"
  13468. "%s: %d\n"
  13469. "%s: %d\n"
  13470. "%s: %s\n"
  13471. "%s: %s\n"
  13472. "%s: %s\n"
  13473. "%s: %d\n"
  13474. "%s: 0x%04X\n"
  13475. "%s: %s\n"
  13476. #endif /* TTDL_DIAGNOSTICS */
  13477. ,
  13478. "Startup Status ", cd->startup_status,
  13479. "TTDL Debug Level ", cd->debug_level,
  13480. "Mode ",
  13481. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13482. "Operational" : "BL") : "Unknown",
  13483. "DUT Generation ",
  13484. cd->active_dut_generation ?
  13485. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13486. "PT TC/TT" : "Gen5/6") : "Unknown",
  13487. cd->active_dut_generation ?
  13488. (cd->set_dut_generation == true ?
  13489. "(Set)" : "(Detected)") : "",
  13490. "HW Detected ",
  13491. cd->hw_detected ? "True" : "False",
  13492. "I2C Address ",
  13493. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13494. "Active Bus Module ",
  13495. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13496. "Flashless Mode ",
  13497. cd->flashless_dut == 1 ? "Yes" : "No",
  13498. "GPIO state - IRQ ",
  13499. cd->cpdata->irq_stat ?
  13500. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13501. "High" : "Low") : "not defined",
  13502. "GPIO state - TP_XRES ",
  13503. pdata->core_pdata->rst_gpio ?
  13504. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13505. "High" : "Low") : "not defined",
  13506. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13507. "Startup Retry Count ", cd->startup_retry_count,
  13508. "WD - Manual Force Stop ",
  13509. cd->watchdog_force_stop ? "True" : "False",
  13510. "WD - Enabled ",
  13511. cd->watchdog_enabled ? "True" : "False",
  13512. "WD - Interval (ms) ", cd->watchdog_interval
  13513. #ifdef TTDL_DIAGNOSTICS
  13514. , "WD - Triggered Count ", cd->watchdog_count,
  13515. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13516. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13517. "WD - XRES Count ", cd->wd_xres_count,
  13518. "IRQ Triggered Count ", cd->irq_count,
  13519. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13520. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13521. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13522. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13523. "Error GPIO trigger type ", cd->err_gpio_type,
  13524. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13525. "Suppress No-Flash Auto BL ",
  13526. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13527. "Yes" : "No",
  13528. "Calibration Cache on host ",
  13529. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13530. "Yes" : "No",
  13531. "Calibration Cache size ", cal_size,
  13532. "Calibration Cache chip ID ", crc,
  13533. "Multi-Chip Support ",
  13534. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13535. #endif /* TTDL_DIAGNOSTICS */
  13536. );
  13537. #ifdef TTDL_DIAGNOSTICS
  13538. /* Reset all diagnostic counters */
  13539. cd->watchdog_count = 0;
  13540. cd->watchdog_irq_stuck_count = 0;
  13541. cd->watchdog_failed_access_count = 0;
  13542. cd->wd_xres_count = 0;
  13543. cd->irq_count = 0;
  13544. cd->bl_retry_packet_count = 0;
  13545. cd->pip2_crc_error_count = 0;
  13546. cd->bus_transmit_error_count = 0;
  13547. #endif
  13548. return ret;
  13549. }
  13550. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13551. /*******************************************************************************
  13552. * FUNCTION: pt_pip2_enter_bl_show
  13553. *
  13554. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13555. * the DUT into the BL and show the success or failure of entering the BL
  13556. *
  13557. * RETURN:
  13558. * 0 = success
  13559. * !0 = failure
  13560. *
  13561. * PARAMETERS:
  13562. * *dev - pointer to device structure
  13563. * *attr - pointer to device attributes
  13564. * *buf - pointer to output buffer
  13565. ******************************************************************************/
  13566. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13567. struct device_attribute *attr, char *buf)
  13568. {
  13569. ssize_t ret = 0;
  13570. int rc = 0;
  13571. int result = 0;
  13572. u8 mode = PT_MODE_UNKNOWN;
  13573. struct pt_core_data *cd = dev_get_drvdata(dev);
  13574. /* Turn off the TTDL WD before enter bootloader */
  13575. pt_stop_wd_timer(cd);
  13576. /* Ensure NO enumeration work is queued or will be queued */
  13577. cancel_work_sync(&cd->enum_work);
  13578. mutex_lock(&cd->system_lock);
  13579. cd->bridge_mode = true;
  13580. mutex_unlock(&cd->system_lock);
  13581. /* set mode to operational to avoid any extra PIP traffic */
  13582. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13583. switch (result) {
  13584. case PT_ENTER_BL_PASS:
  13585. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13586. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13587. break;
  13588. case PT_ENTER_BL_ERROR:
  13589. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13590. rc,
  13591. " Unknown Error");
  13592. break;
  13593. case PT_ENTER_BL_RESET_FAIL:
  13594. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13595. rc,
  13596. " Soft Reset Failed");
  13597. break;
  13598. case PT_ENTER_BL_HID_START_BL_FAIL:
  13599. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13600. rc,
  13601. " PIP Start BL Cmd Failed");
  13602. break;
  13603. case PT_ENTER_BL_CONFIRM_FAIL:
  13604. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13605. rc,
  13606. " Error confirming DUT entered BL");
  13607. break;
  13608. default:
  13609. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13610. rc, " Unknown Error");
  13611. break;
  13612. };
  13613. /* Allow enumeration work to be queued again */
  13614. cd->bridge_mode = false;
  13615. return ret;
  13616. }
  13617. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13618. /*******************************************************************************
  13619. * FUNCTION: pt_pip2_exit_bl_show
  13620. *
  13621. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13622. * launch the APP and put the DUT Application mode
  13623. *
  13624. * RETURN:
  13625. * 0 = success
  13626. * !0 = failure
  13627. *
  13628. * PARAMETERS:
  13629. * *dev - pointer to device structure
  13630. * *attr - pointer to device attributes
  13631. * *buf - pointer to output buffer
  13632. ******************************************************************************/
  13633. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13634. struct device_attribute *attr, char *buf)
  13635. {
  13636. struct pt_core_data *cd = dev_get_drvdata(dev);
  13637. ssize_t ret = 0;
  13638. int rc = 0;
  13639. u8 status_str[PT_STATUS_STR_LEN];
  13640. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13641. /*
  13642. * Perform enum if startup_status doesn't reach to
  13643. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13644. */
  13645. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13646. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13647. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13648. strlcpy(status_str,
  13649. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13650. }
  13651. }
  13652. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13653. rc, status_str);
  13654. return ret;
  13655. }
  13656. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13657. #endif
  13658. #ifdef EASYWAKE_TSG6
  13659. /*******************************************************************************
  13660. * FUNCTION: pt_easy_wakeup_gesture_show
  13661. *
  13662. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13663. * current easy wakeup gesture
  13664. *
  13665. * RETURN:
  13666. * 0 = success
  13667. * !0 = failure
  13668. *
  13669. * PARAMETERS:
  13670. * *dev - pointer to device structure
  13671. * *attr - pointer to device attributes
  13672. * *buf - pointer to output buffer
  13673. ******************************************************************************/
  13674. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13675. struct device_attribute *attr, char *buf)
  13676. {
  13677. struct pt_core_data *cd = dev_get_drvdata(dev);
  13678. ssize_t ret;
  13679. mutex_lock(&cd->system_lock);
  13680. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13681. cd->easy_wakeup_gesture);
  13682. mutex_unlock(&cd->system_lock);
  13683. return ret;
  13684. }
  13685. /*******************************************************************************
  13686. * FUNCTION: pt_easy_wakeup_gesture_store
  13687. *
  13688. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13689. * allows the wake gesture to be set to a custom value.
  13690. *
  13691. * RETURN: Size of passed in buffer is success
  13692. *
  13693. * PARAMETERS:
  13694. * *dev - pointer to device structure
  13695. * *attr - pointer to device attributes
  13696. * *buf - pointer to buffer that hold the command parameters
  13697. * size - size of buf
  13698. ******************************************************************************/
  13699. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13700. struct device_attribute *attr, const char *buf, size_t size)
  13701. {
  13702. struct pt_core_data *cd = dev_get_drvdata(dev);
  13703. u32 input_data[2];
  13704. int length;
  13705. int rc = 0;
  13706. /* Maximum input of one value */
  13707. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13708. ARRAY_SIZE(input_data));
  13709. if (length != 1) {
  13710. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13711. __func__);
  13712. rc = -EINVAL;
  13713. goto exit;
  13714. }
  13715. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13716. __func__, cd->features.easywake);
  13717. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13718. rc = -EINVAL;
  13719. goto exit;
  13720. }
  13721. pm_runtime_get_sync(dev);
  13722. mutex_lock(&cd->system_lock);
  13723. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13724. cd->easy_wakeup_gesture = (u8)input_data[0];
  13725. pt_debug(dev, DL_INFO,
  13726. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13727. __func__, cd->easy_wakeup_gesture);
  13728. } else
  13729. rc = -ENODEV;
  13730. mutex_unlock(&cd->system_lock);
  13731. pm_runtime_put(dev);
  13732. exit:
  13733. if (rc)
  13734. return rc;
  13735. return size;
  13736. }
  13737. /*******************************************************************************
  13738. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13739. *
  13740. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13741. * show the TSG6 easywake gesture ID
  13742. *
  13743. * RETURN:
  13744. * 0 = success
  13745. * !0 = failure
  13746. *
  13747. * PARAMETERS:
  13748. * *dev - pointer to device structure
  13749. * *attr - pointer to device attributes
  13750. * *buf - pointer to output buffer
  13751. ******************************************************************************/
  13752. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13753. struct device_attribute *attr, char *buf)
  13754. {
  13755. struct pt_core_data *cd = dev_get_drvdata(dev);
  13756. ssize_t ret;
  13757. mutex_lock(&cd->system_lock);
  13758. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13759. cd->gesture_id);
  13760. mutex_unlock(&cd->system_lock);
  13761. return ret;
  13762. }
  13763. /*******************************************************************************
  13764. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13765. *
  13766. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13767. * show the TSG6 easywake gesture data in the following format:
  13768. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13769. *
  13770. * RETURN:
  13771. * 0 = success
  13772. * !0 = failure
  13773. *
  13774. * PARAMETERS:
  13775. * *dev - pointer to device structure
  13776. * *attr - pointer to device attributes
  13777. * *buf - pointer to output buffer
  13778. ******************************************************************************/
  13779. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13780. struct device_attribute *attr, char *buf)
  13781. {
  13782. struct pt_core_data *cd = dev_get_drvdata(dev);
  13783. ssize_t ret = 0;
  13784. int i;
  13785. mutex_lock(&cd->system_lock);
  13786. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13787. for (i = 0; i < cd->gesture_data_length; i++)
  13788. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13789. "0x%02X\n", cd->gesture_data[i]);
  13790. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13791. "(%d bytes)\n", cd->gesture_data_length);
  13792. mutex_unlock(&cd->system_lock);
  13793. return ret;
  13794. }
  13795. #endif /* EASYWAKE_TSG6 */
  13796. #ifdef TTDL_DIAGNOSTICS
  13797. /*******************************************************************************
  13798. * FUNCTION: pt_err_gpio_show
  13799. *
  13800. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13801. * setting up the gpio was successful
  13802. *
  13803. * RETURN: Char buffer with printed GPIO creation state
  13804. *
  13805. * PARAMETERS:
  13806. * *dev - pointer to device structure
  13807. * *attr - pointer to device attributes
  13808. * *buf - pointer to output buffer
  13809. ******************************************************************************/
  13810. static ssize_t pt_err_gpio_show(struct device *dev,
  13811. struct device_attribute *attr, char *buf)
  13812. {
  13813. struct pt_core_data *cd = dev_get_drvdata(dev);
  13814. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13815. "Err GPIO (%d) : %s\n"
  13816. "Err GPIO trig type: %d\n",
  13817. cd->err_gpio,
  13818. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13819. "HIGH" : "low") : "not defined"),
  13820. cd->err_gpio_type);
  13821. }
  13822. /*******************************************************************************
  13823. * FUNCTION: pt_err_gpio_store
  13824. *
  13825. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13826. * available host GPIO to be used to trigger when TTDL detects a PIP
  13827. * command/response timeout.
  13828. *
  13829. * RETURN:
  13830. * 0 = success
  13831. * !0 = failure
  13832. *
  13833. * PARAMETERS:
  13834. * *dev - pointer to device structure
  13835. * *attr - pointer to device attributes
  13836. * *buf - pointer to buffer that hold the command parameters
  13837. * size - size of buf
  13838. ******************************************************************************/
  13839. static ssize_t pt_err_gpio_store(struct device *dev,
  13840. struct device_attribute *attr, const char *buf, size_t size)
  13841. {
  13842. struct pt_core_data *cd = dev_get_drvdata(dev);
  13843. unsigned long gpio;
  13844. int rc = 0;
  13845. u32 input_data[3];
  13846. int length;
  13847. u8 err_type;
  13848. input_data[0] = 0;
  13849. input_data[1] = 0;
  13850. /* Maximmum input is two elements */
  13851. length = _pt_ic_parse_input(dev, buf, size,
  13852. input_data, ARRAY_SIZE(input_data));
  13853. if (length < 1 || length > 2) {
  13854. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13855. __func__);
  13856. rc = -EINVAL;
  13857. goto exit;
  13858. }
  13859. gpio = input_data[0];
  13860. err_type = (u8)input_data[1];
  13861. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13862. rc = -EINVAL;
  13863. goto exit;
  13864. }
  13865. mutex_lock(&cd->system_lock);
  13866. gpio_free(gpio);
  13867. rc = gpio_request(gpio, NULL);
  13868. if (rc) {
  13869. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13870. rc = -ENODEV;
  13871. } else {
  13872. cd->err_gpio = gpio;
  13873. cd->err_gpio_type = err_type;
  13874. gpio_direction_output(gpio, 0);
  13875. }
  13876. mutex_unlock(&cd->system_lock);
  13877. exit:
  13878. if (rc)
  13879. return rc;
  13880. return size;
  13881. }
  13882. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13883. pt_err_gpio_store);
  13884. /*******************************************************************************
  13885. * FUNCTION: pt_drv_irq_show
  13886. *
  13887. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13888. * TTDL interrupt is enabled/disabled
  13889. *
  13890. * RETURN:
  13891. * 0 = success
  13892. * !0 = failure
  13893. *
  13894. * PARAMETERS:
  13895. * *dev - pointer to device structure
  13896. * *attr - pointer to device attributes
  13897. * *buf - pointer to output buffer
  13898. ******************************************************************************/
  13899. static ssize_t pt_drv_irq_show(struct device *dev,
  13900. struct device_attribute *attr, char *buf)
  13901. {
  13902. struct pt_core_data *cd = dev_get_drvdata(dev);
  13903. ssize_t ret = 0;
  13904. mutex_lock(&cd->system_lock);
  13905. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13906. if (cd->irq_enabled)
  13907. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13908. "Driver interrupt: ENABLED\n");
  13909. else
  13910. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13911. "Driver interrupt: DISABLED\n");
  13912. mutex_unlock(&cd->system_lock);
  13913. return ret;
  13914. }
  13915. /*******************************************************************************
  13916. * FUNCTION: pt_drv_irq_store
  13917. *
  13918. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13919. * IRQ to be enabled/disabled.
  13920. *
  13921. * RETURN: Size of passed in buffer
  13922. *
  13923. * PARAMETERS:
  13924. * *dev - pointer to device structure
  13925. * *attr - pointer to device attributes
  13926. * *buf - pointer to buffer that hold the command parameters
  13927. * size - size of buf
  13928. ******************************************************************************/
  13929. static ssize_t pt_drv_irq_store(struct device *dev,
  13930. struct device_attribute *attr, const char *buf, size_t size)
  13931. {
  13932. struct pt_core_data *cd = dev_get_drvdata(dev);
  13933. u32 input_data[2];
  13934. int length;
  13935. int rc = 0;
  13936. /* Maximum input of one value */
  13937. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13938. ARRAY_SIZE(input_data));
  13939. if (length != 1) {
  13940. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13941. __func__);
  13942. rc = -EINVAL;
  13943. goto exit;
  13944. }
  13945. mutex_lock(&cd->system_lock);
  13946. switch (input_data[0]) {
  13947. case 0:
  13948. if (cd->irq_enabled) {
  13949. cd->irq_enabled = false;
  13950. /* Disable IRQ has no return value to check */
  13951. disable_irq_nosync(cd->irq);
  13952. pt_debug(dev, DL_INFO,
  13953. "%s: Driver IRQ now disabled\n",
  13954. __func__);
  13955. } else
  13956. pt_debug(dev, DL_INFO,
  13957. "%s: Driver IRQ already disabled\n",
  13958. __func__);
  13959. break;
  13960. case 1:
  13961. if (cd->irq_enabled == false) {
  13962. cd->irq_enabled = true;
  13963. enable_irq(cd->irq);
  13964. pt_debug(dev, DL_INFO,
  13965. "%s: Driver IRQ now enabled\n",
  13966. __func__);
  13967. } else
  13968. pt_debug(dev, DL_INFO,
  13969. "%s: Driver IRQ already enabled\n",
  13970. __func__);
  13971. break;
  13972. default:
  13973. rc = -EINVAL;
  13974. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13975. }
  13976. mutex_unlock(&(cd->system_lock));
  13977. exit:
  13978. if (rc)
  13979. return rc;
  13980. return size;
  13981. }
  13982. /*******************************************************************************
  13983. * FUNCTION: pt_pip2_bin_hdr_show
  13984. *
  13985. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13986. * the bin file header from flash and show each field
  13987. *
  13988. * RETURN:
  13989. * 0 = success
  13990. * !0 = failure
  13991. *
  13992. * PARAMETERS:
  13993. * *dev - pointer to device structure
  13994. * *attr - pointer to device attributes
  13995. * *buf - pointer to output buffer
  13996. ******************************************************************************/
  13997. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13998. struct device_attribute *attr, char *buf)
  13999. {
  14000. ssize_t ret = 0;
  14001. struct pt_bin_file_hdr hdr = {0};
  14002. int rc;
  14003. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14004. ret = scnprintf(buf, strlen(buf),
  14005. "%s: %d\n"
  14006. "%s: %d\n"
  14007. "%s: 0x%04X\n"
  14008. "%s: %d\n"
  14009. "%s: %d\n"
  14010. "%s: %d\n"
  14011. "%s: 0x%08X\n"
  14012. "%s: 0x%04X\n"
  14013. "%s: 0x%04X\n"
  14014. "%s: %d\n"
  14015. "%s: %d\n",
  14016. "Status", rc,
  14017. "Header Length ", hdr.length,
  14018. "TTPID ", hdr.ttpid,
  14019. "FW Major Ver ", hdr.fw_major,
  14020. "FW Minor Ver ", hdr.fw_minor,
  14021. "FW Rev Control ", hdr.fw_rev_ctrl,
  14022. "FW CRC ", hdr.fw_crc,
  14023. "Silicon Rev ", hdr.si_rev,
  14024. "Silicon ID ", hdr.si_id,
  14025. "Config Ver ", hdr.config_ver,
  14026. "HEX File Size ", hdr.hex_file_size
  14027. );
  14028. return ret;
  14029. }
  14030. /*******************************************************************************
  14031. * FUNCTION: pt_platform_data_show
  14032. *
  14033. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14034. * active platform data including: GPIOs, Vendor and Product IDs,
  14035. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14036. * HID registers, and Easy wake gesture
  14037. *
  14038. * RETURN: Size of printed data
  14039. *
  14040. * PARAMETERS:
  14041. * *dev - pointer to device structure
  14042. * *attr - pointer to device attributes
  14043. * *buf - pointer to output buffer
  14044. ******************************************************************************/
  14045. static ssize_t pt_platform_data_show(struct device *dev,
  14046. struct device_attribute *attr, char *buf)
  14047. {
  14048. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14049. struct pt_core_data *cd = dev_get_drvdata(dev);
  14050. ssize_t ret;
  14051. ret = scnprintf(buf, strlen(buf),
  14052. "%s: %d\n"
  14053. "%s: %d\n"
  14054. "%s: %d\n"
  14055. "%s: %d\n"
  14056. "%s: %d\n"
  14057. "%s: %d\n"
  14058. "%s: %d\n"
  14059. "%s: %d\n"
  14060. "%s: %d\n"
  14061. "%s: %d\n"
  14062. "%s: %d\n"
  14063. "%s: %d\n"
  14064. "%s: %d\n"
  14065. "%s: %d\n"
  14066. "%s: %d\n"
  14067. "%s: %d\n"
  14068. "%s: %d\n"
  14069. "%s: %d\n"
  14070. "%s: %d\n"
  14071. "%s: %d\n"
  14072. "%s: %d\n"
  14073. "%s: %d\n"
  14074. "%s: %d\n"
  14075. "%s: %d\n"
  14076. "%s: %d\n"
  14077. "%s: %d\n"
  14078. "%s: %d\n"
  14079. "%s: %s\n"
  14080. "%s: %s\n"
  14081. "%s: %d\n",
  14082. "Status", 0,
  14083. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14084. "Interrupt GPIO Value ",
  14085. pdata->core_pdata->irq_gpio ?
  14086. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14087. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14088. "Reset GPIO Value ",
  14089. pdata->core_pdata->rst_gpio ?
  14090. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14091. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14092. "DDI Reset GPIO Value ",
  14093. pdata->core_pdata->ddi_rst_gpio ?
  14094. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14095. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14096. "VDDI GPIO Value ",
  14097. pdata->core_pdata->vddi_gpio ?
  14098. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14099. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14100. "VCC GPIO Value ",
  14101. pdata->core_pdata->vcc_gpio ?
  14102. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14103. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14104. "AVDD GPIO Value ",
  14105. pdata->core_pdata->avdd_gpio ?
  14106. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14107. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14108. "AVEE GPIO Value ",
  14109. pdata->core_pdata->avee_gpio ?
  14110. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14111. "Vendor ID ", pdata->core_pdata->vendor_id,
  14112. "Product ID ", pdata->core_pdata->product_id,
  14113. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14114. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14115. "Core data flags ", pdata->core_pdata->flags,
  14116. "MT data flags ", pdata->mt_pdata->flags,
  14117. "Loader data flags ", pdata->loader_pdata->flags,
  14118. "Level trigger delay (us) ",
  14119. pdata->core_pdata->level_irq_udelay,
  14120. "HID Descriptor Register ",
  14121. pdata->core_pdata->hid_desc_register,
  14122. "HID Output Register ",
  14123. cd->hid_desc.output_register,
  14124. "HID Command Register ",
  14125. cd->hid_desc.command_register,
  14126. "Easy wakeup gesture ",
  14127. pdata->core_pdata->easy_wakeup_gesture,
  14128. "Config DUT generation ",
  14129. pdata->core_pdata->config_dut_generation ?
  14130. (pdata->core_pdata->config_dut_generation ==
  14131. CONFIG_DUT_PIP2_CAPABLE ?
  14132. "PT TC/TT" : "Gen5/6") : "Auto",
  14133. "Watchdog Force Stop ",
  14134. pdata->core_pdata->watchdog_force_stop ?
  14135. "True" : "False",
  14136. "Panel ID Support ",
  14137. pdata->core_pdata->panel_id_support);
  14138. return ret;
  14139. }
  14140. #define PT_ERR_STR_SIZE 64
  14141. /*******************************************************************************
  14142. * FUNCTION: pt_bist_bus_test
  14143. *
  14144. * SUMMARY: Tests the connectivity of the active bus pins:
  14145. * I2C - SDA and SCL
  14146. * SPI - MOSI, MISO, CLK
  14147. *
  14148. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14149. * bus. If any data is read we know the I2C/SPI pins are connected
  14150. *
  14151. * RETURN:
  14152. * 0 = Success
  14153. * !0 = Failure
  14154. *
  14155. * PARAMETERS:
  14156. * *dev - pointer to device structure
  14157. * *net_toggled - pointer to where to store if bus toggled
  14158. * *err_str - pointer to error string buffer
  14159. ******************************************************************************/
  14160. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14161. {
  14162. struct pt_core_data *cd = dev_get_drvdata(dev);
  14163. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14164. u8 *read_buf = NULL;
  14165. int bytes_read = 0;
  14166. int rc = 0;
  14167. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14168. if (read_buf == NULL) {
  14169. rc = -ENOMEM;
  14170. goto exit;
  14171. }
  14172. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14173. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14174. disable_irq(cd->irq);
  14175. mutex_lock(&cd->system_lock);
  14176. cd->irq_disabled = true;
  14177. mutex_unlock(&cd->system_lock);
  14178. /*
  14179. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14180. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14181. */
  14182. usleep_range(5000, 6000);
  14183. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14184. __func__, (int)sizeof(ver_cmd));
  14185. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14186. ">>> User CMD");
  14187. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14188. if (rc) {
  14189. pt_debug(dev, DL_ERROR,
  14190. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14191. *net_toggled = 0;
  14192. strlcpy(err_str,
  14193. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14194. goto exit_enable_irq;
  14195. }
  14196. usleep_range(4000, 5000);
  14197. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14198. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14199. __func__, bytes_read);
  14200. if (bytes_read == 0) {
  14201. *net_toggled = 0;
  14202. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14203. __func__);
  14204. strlcpy(err_str,
  14205. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14206. rc = -EIO;
  14207. goto exit_enable_irq;
  14208. } else {
  14209. if (cd->bus_ops->bustype == BUS_I2C)
  14210. *net_toggled = 1;
  14211. else {
  14212. if ((bytes_read > 3) &&
  14213. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14214. PIP2_CMD_ID_VERSION)
  14215. *net_toggled = 1;
  14216. else {
  14217. *net_toggled = 0;
  14218. pt_debug(dev, DL_INFO,
  14219. "%s: BUS Read Failed, %d bytes read\n",
  14220. __func__, bytes_read);
  14221. strlcpy(err_str,
  14222. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14223. }
  14224. }
  14225. }
  14226. exit_enable_irq:
  14227. enable_irq(cd->irq);
  14228. usleep_range(5000, 6000);
  14229. mutex_lock(&cd->system_lock);
  14230. cd->irq_disabled = false;
  14231. mutex_unlock(&cd->system_lock);
  14232. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14233. exit:
  14234. kfree(read_buf);
  14235. return rc;
  14236. }
  14237. /*******************************************************************************
  14238. * FUNCTION: pt_bist_irq_test
  14239. *
  14240. * SUMMARY: Tests the connectivity of the IRQ net
  14241. *
  14242. * This test will ensure there is a good connection between the host
  14243. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14244. * asserted and if so keep reading messages off of the bus until
  14245. * it de-asserts. Possible outcomes:
  14246. * - IRQ was already de-asserted: Send a PIP command and if an
  14247. * interrupt is generated the test passes.
  14248. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14249. * test passes.
  14250. * - IRQ stays asserted: After reading the bus multiple times
  14251. * the IRQ stays asserted. Likely open or shorted to GND
  14252. *
  14253. * RETURN:
  14254. * 0 = Success
  14255. * !0 = Failure
  14256. *
  14257. * PARAMETERS:
  14258. * *dev - pointer to device structure
  14259. * *bus_toggled - pointer to where to store if bus toggled
  14260. * *irq_toggled - pointer to where to store if IRQ toggled
  14261. * *xres_toggled - pointer to where to store if XRES toggled
  14262. * *err_str - pointer to error string buffer
  14263. ******************************************************************************/
  14264. static int pt_bist_irq_test(struct device *dev,
  14265. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14266. {
  14267. struct pt_core_data *cd = dev_get_drvdata(dev);
  14268. u8 *read_buf = NULL;
  14269. u8 mode = PT_MODE_UNKNOWN;
  14270. u16 actual_read_len;
  14271. int bytes_read = 0;
  14272. int count = 0;
  14273. int rc = 0;
  14274. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14275. if (read_buf == NULL) {
  14276. rc = -ENOMEM;
  14277. goto exit;
  14278. }
  14279. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14280. cd->irq_count = 0;
  14281. /*
  14282. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14283. * the bus based on the 2 byte initial length read. Try up to 5x.
  14284. */
  14285. while (pt_check_irq_asserted(cd) && count < 5) {
  14286. count++;
  14287. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14288. }
  14289. if (count > 1 && count < 5 && bytes_read > 0) {
  14290. /*
  14291. * IRQ was stuck but data was successfully read from the
  14292. * bus releasing the IRQ line.
  14293. */
  14294. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14295. __func__, count, bytes_read);
  14296. *bus_toggled = 1;
  14297. *irq_toggled = 1;
  14298. goto exit;
  14299. }
  14300. if (count == 5 && bytes_read == 0) {
  14301. /*
  14302. * Looped 5x and read nothing off the bus yet the IRQ is still
  14303. * asserted. Possible conditions:
  14304. * - IRQ open circuit
  14305. * - IRQ shorted to GND
  14306. * - I2C/SPI bus is disconnected
  14307. * - FW holding the pin low
  14308. * Try entering the BL to see if communication works there.
  14309. */
  14310. mode = PT_MODE_IGNORE;
  14311. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14312. if (rc) {
  14313. pt_debug(dev, DL_ERROR,
  14314. "%s Failed to enter BL\n", __func__);
  14315. strlcpy(err_str,
  14316. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14317. *irq_toggled = 0;
  14318. goto exit;
  14319. }
  14320. /*
  14321. * If original mode was operational and we successfully
  14322. * entered the BL, then the XRES net must have toggled
  14323. */
  14324. if (mode == PT_MODE_OPERATIONAL)
  14325. *xres_toggled = 1;
  14326. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14327. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14328. &actual_read_len);
  14329. if (rc) {
  14330. /*
  14331. * Could not communicate to DUT in BL mode. Save the
  14332. * error string, slim chance but the XRES test below may
  14333. * show the IRQ is actually working.
  14334. */
  14335. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14336. pt_debug(dev, DL_ERROR,
  14337. "%s: %s, count=%d bytes_read=%d\n",
  14338. __func__, err_str, count, bytes_read);
  14339. *irq_toggled = 0;
  14340. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14341. goto exit;
  14342. } else {
  14343. *bus_toggled = 1;
  14344. *irq_toggled = 1;
  14345. goto exit;
  14346. }
  14347. }
  14348. if (pt_check_irq_asserted(cd)) {
  14349. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14350. rc = -EIO;
  14351. *irq_toggled = 0;
  14352. goto exit;
  14353. }
  14354. /* Try sending a PIP command to see if we get a response */
  14355. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14356. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14357. if (rc) {
  14358. /*
  14359. * Potential IRQ issue, no communication in App mode, attempt
  14360. * the same command in the BL
  14361. */
  14362. mode = PT_MODE_IGNORE;
  14363. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14364. if (rc) {
  14365. pt_debug(dev, DL_ERROR,
  14366. "%s Failed to enter BL\n", __func__);
  14367. *irq_toggled = 0;
  14368. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14369. goto exit;
  14370. }
  14371. /*
  14372. * If original mode was operational and we successfully
  14373. * entered the BL, this will be useful info for the tp_xres
  14374. * test below.
  14375. */
  14376. if (mode == PT_MODE_OPERATIONAL)
  14377. *xres_toggled = 1;
  14378. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14379. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14380. &actual_read_len);
  14381. if (rc) {
  14382. /*
  14383. * Could not communicate in FW mode or BL mode. Save the
  14384. * error string, slim chance but the XRES test below may
  14385. * show the IRQ is actually working.
  14386. */
  14387. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14388. pt_debug(dev, DL_ERROR,
  14389. "%s: request_active_pip_prot failed\n",
  14390. __func__);
  14391. *irq_toggled = 0;
  14392. goto exit;
  14393. }
  14394. }
  14395. if (cd->irq_count > 0) {
  14396. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14397. cd->irq_count);
  14398. *bus_toggled = 1;
  14399. *irq_toggled = 1;
  14400. goto exit;
  14401. }
  14402. exit:
  14403. kfree(read_buf);
  14404. return rc;
  14405. }
  14406. /*******************************************************************************
  14407. * FUNCTION: pt_bist_xres_test
  14408. *
  14409. * SUMMARY: Tests the connectivity of the TP_XRES net
  14410. *
  14411. * This test will ensure there is a good connection between the host
  14412. * and the DUT on the tp_xres pin. The pin will be toggled to
  14413. * generate a TP reset which will cause the DUT to output a reset
  14414. * sentinel. If the reset sentinel is seen the test passes. If it is
  14415. * not seen the test will attempt to send a soft reset to simply gain
  14416. * some additional information on the failure:
  14417. * - soft reset fails to send: XRES and IRQ likely open
  14418. * - soft reset passes: XRES likely open or stuck de-asserted
  14419. * - soft reset fails: XRES likely stuck asserted
  14420. *
  14421. * RETURN:
  14422. * 0 = Success
  14423. * !0 = Failure
  14424. *
  14425. * PARAMETERS:
  14426. * *dev - pointer to device structure
  14427. * *bus_toggled - pointer to where to store if bus toggled
  14428. * *irq_toggled - pointer to where to store if IRQ toggled
  14429. * *xres_toggled - pointer to where to store if XRES toggled
  14430. * *err_str - pointer to error string buffer
  14431. ******************************************************************************/
  14432. static int pt_bist_xres_test(struct device *dev,
  14433. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14434. {
  14435. struct pt_core_data *cd = dev_get_drvdata(dev);
  14436. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14437. u8 *read_buf = NULL;
  14438. u8 mode = PT_MODE_UNKNOWN;
  14439. int rc = 0;
  14440. int t = 0;
  14441. int timeout = 300;
  14442. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14443. if (read_buf == NULL) {
  14444. rc = -ENOMEM;
  14445. goto exit;
  14446. }
  14447. /* Clear the startup bit mask, reset and enum will re-populate it */
  14448. cd->startup_status = STARTUP_STATUS_START;
  14449. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14450. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14451. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14452. rc = -ENODEV;
  14453. goto exit;
  14454. }
  14455. /* Ensure we have nothing pending on active bus */
  14456. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14457. /* Perform a hard XRES toggle and wait for reset sentinel */
  14458. mutex_lock(&cd->system_lock);
  14459. cd->hid_reset_cmd_state = 1;
  14460. mutex_unlock(&cd->system_lock);
  14461. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14462. rc = pt_hw_hard_reset(cd);
  14463. /* Set timeout to 1s for the flashless case where a BL could be done */
  14464. if (cd->flashless_dut)
  14465. timeout = 1000;
  14466. /*
  14467. * To avoid the case that next PIP command can be confused by BL/FW
  14468. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14469. * followed by "wakeup event" function can lower the failure rate.
  14470. */
  14471. t = wait_event_timeout(cd->wait_q,
  14472. ((cd->startup_status != STARTUP_STATUS_START)
  14473. && (cd->hid_reset_cmd_state == 0)),
  14474. msecs_to_jiffies(timeout));
  14475. if (IS_TMO(t)) {
  14476. pt_debug(cd->dev, DL_ERROR,
  14477. "%s: TMO waiting for sentinel\n", __func__);
  14478. *xres_toggled = 0;
  14479. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14480. /*
  14481. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14482. * To enter the BL we need to generate an XRES so first try to
  14483. * launch the applicaiton
  14484. */
  14485. if (cd->mode == PT_MODE_BOOTLOADER)
  14486. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14487. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14488. __func__);
  14489. mode = PT_MODE_IGNORE;
  14490. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14491. if (rc) {
  14492. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14493. __func__);
  14494. *xres_toggled = 0;
  14495. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14496. goto exit;
  14497. } else {
  14498. /* Wait for the BL sentinel */
  14499. t = wait_event_timeout(cd->wait_q,
  14500. (cd->startup_status != STARTUP_STATUS_START),
  14501. msecs_to_jiffies(500));
  14502. if (IS_TMO(t)) {
  14503. pt_debug(cd->dev, DL_ERROR,
  14504. "%s: TMO waiting for BL sentinel\n",
  14505. __func__);
  14506. *xres_toggled = 0;
  14507. strlcpy(err_str,
  14508. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14509. rc = -ETIME;
  14510. goto exit;
  14511. }
  14512. }
  14513. }
  14514. mutex_lock(&cd->system_lock);
  14515. cd->hid_reset_cmd_state = 0;
  14516. mutex_unlock(&cd->system_lock);
  14517. /* Look for BL or FW reset sentinels */
  14518. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14519. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14520. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14521. /* If a sentinel was seen, all nets are working */
  14522. *xres_toggled = 1;
  14523. *irq_toggled = 1;
  14524. /*
  14525. * For SPI test, bus read result can be confused as FW sentinel
  14526. * if MISO(slave) is connected to MISO(host).
  14527. */
  14528. if (cd->bus_ops->bustype == BUS_I2C)
  14529. *bus_toggled = 1;
  14530. } else {
  14531. /*
  14532. * Hard reset failed, however some additional information
  14533. * could be determined. Try a soft reset to see if DUT resets
  14534. * with the possible outcomes:
  14535. * - if it resets the line is not stuck asserted
  14536. * - if it does not reset the line could be stuck asserted
  14537. */
  14538. *xres_toggled = 0;
  14539. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14540. msleep(30);
  14541. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14542. __func__, rc);
  14543. if (rc) {
  14544. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14545. pt_debug(dev, DL_ERROR,
  14546. "%s: Hard reset failed, soft reset failed %s\n",
  14547. __func__, err_str);
  14548. goto exit;
  14549. }
  14550. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14551. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14552. strlcpy(err_str,
  14553. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14554. pt_debug(dev, DL_ERROR,
  14555. "%s: Hard reset failed, soft reset passed-%s\n",
  14556. __func__, err_str);
  14557. } else if (cd->startup_status == 0) {
  14558. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14559. pt_debug(dev, DL_ERROR,
  14560. "%s: Hard reset failed, soft reset failed-%s\n",
  14561. __func__, err_str);
  14562. } else {
  14563. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14564. pt_debug(dev, DL_ERROR,
  14565. "%s: Hard and Soft reset failed - %s\n",
  14566. __func__, err_str);
  14567. }
  14568. }
  14569. exit:
  14570. kfree(read_buf);
  14571. return rc;
  14572. }
  14573. /*******************************************************************************
  14574. * FUNCTION: pt_bist_slave_irq_test
  14575. *
  14576. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14577. *
  14578. * This test will ensure there is a good IRQ connection between the master
  14579. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14580. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14581. * saw each slave trigger the IRQ with it's reset sentinel.
  14582. *
  14583. * RETURN:
  14584. * 0 = Success
  14585. * !0 = Failure
  14586. *
  14587. * PARAMETERS:
  14588. * *dev - pointer to device structure
  14589. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14590. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14591. * *err_str - pointer to error string buffer
  14592. * *slave_detect - pointer to slave detect buffer
  14593. * *boot_err - pointer to boot_err buffer
  14594. ******************************************************************************/
  14595. static int pt_bist_slave_irq_test(struct device *dev,
  14596. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14597. u8 *slave_detect, u8 *boot_err)
  14598. {
  14599. struct pt_core_data *cd = dev_get_drvdata(dev);
  14600. u8 mode = PT_MODE_UNKNOWN;
  14601. u8 status;
  14602. u8 boot;
  14603. u8 read_buf[12];
  14604. u8 detected = 0;
  14605. u8 last_err = -1;
  14606. u16 actual_read_len;
  14607. int result = 0;
  14608. int rc = 0;
  14609. /*
  14610. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14611. * detect bits. If the DUT was in FW, entering the BL will cause an
  14612. * XRES signal which will inadvertently test the XRES net as well
  14613. */
  14614. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14615. if (rc) {
  14616. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14617. __func__, rc);
  14618. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14619. goto exit;
  14620. }
  14621. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14622. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14623. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14624. &actual_read_len);
  14625. if (!rc) {
  14626. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14627. "PIP2 STATUS");
  14628. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14629. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14630. /* Slave detect is only valid if status ok and in boot exec */
  14631. if (status == PIP2_RSP_ERR_NONE &&
  14632. boot == PIP2_STATUS_BOOT_EXEC) {
  14633. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14634. SLAVE_DETECT_MASK;
  14635. } else {
  14636. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14637. rc = -EPERM;
  14638. }
  14639. } else {
  14640. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14641. __func__);
  14642. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14643. goto exit;
  14644. }
  14645. /*
  14646. * Retrieve boot error regardless of the state of the slave detect
  14647. * bit because the IRQ could have been stuck high or low.
  14648. */
  14649. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14650. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14651. read_buf, &actual_read_len);
  14652. if (!rc) {
  14653. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14654. "PIP2 GET_LAST_ERRNO");
  14655. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14656. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14657. if (last_err) {
  14658. pt_debug(cd->dev, DL_ERROR,
  14659. "%s: Master Boot Last Err = 0x%02X\n",
  14660. __func__, last_err);
  14661. }
  14662. } else {
  14663. pt_debug(cd->dev, DL_ERROR,
  14664. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14665. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14666. }
  14667. exit:
  14668. pt_debug(cd->dev, DL_INFO,
  14669. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14670. __func__, rc, detected, last_err);
  14671. if (err_str && last_err) {
  14672. if (detected)
  14673. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14674. "- Likely stuck low. Boot Error:",
  14675. last_err);
  14676. else
  14677. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14678. "- Likely stuck high. Boot Error:",
  14679. last_err);
  14680. }
  14681. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14682. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14683. last_err = PIP2_RSP_ERR_NONE;
  14684. if (slave_irq_toggled)
  14685. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14686. if (slave_bus_toggled) {
  14687. /* Leave as UNTEST if slave not detected */
  14688. if (detected)
  14689. *slave_bus_toggled = !last_err ? true : false;
  14690. }
  14691. if (slave_detect)
  14692. *slave_detect = detected;
  14693. if (boot_err)
  14694. *boot_err = last_err;
  14695. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14696. __func__,
  14697. "Detected", detected,
  14698. "slave_irq_toggled", *slave_irq_toggled,
  14699. "slave_bus_toggled", *slave_bus_toggled);
  14700. return rc;
  14701. }
  14702. /*******************************************************************************
  14703. * FUNCTION: pt_bist_slave_xres_test
  14704. *
  14705. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14706. *
  14707. * This test will ensure there is a good TP_XRES connection between the
  14708. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14709. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14710. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14711. * it's reset sentinel.
  14712. *
  14713. * RETURN:
  14714. * 0 = Success
  14715. * !0 = Failure
  14716. *
  14717. * PARAMETERS:
  14718. * *dev - pointer to device structure
  14719. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14720. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14721. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14722. * *err_str - pointer to error string buffer
  14723. ******************************************************************************/
  14724. static int pt_bist_slave_xres_test(struct device *dev,
  14725. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14726. u8 *err_str)
  14727. {
  14728. struct pt_core_data *cd = dev_get_drvdata(dev);
  14729. u8 slave_detect = 0;
  14730. u8 boot_err = 0;
  14731. int rc = 0;
  14732. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14733. mutex_lock(&cd->system_lock);
  14734. cd->hid_reset_cmd_state = 1;
  14735. mutex_unlock(&cd->system_lock);
  14736. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14737. pt_hw_hard_reset(cd);
  14738. msleep(100);
  14739. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14740. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14741. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14742. if (!rc && *slave_irq_toggled == false) {
  14743. /*
  14744. * If the slave IRQ did not toggle, either the slave_detect
  14745. * bit was not set or we had a boot error. If the slave
  14746. * detect was not set the slave did not reset causing a boot
  14747. * error.
  14748. */
  14749. if (!slave_detect)
  14750. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14751. else
  14752. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14753. "- likely open or an IRQ issue. Boot Error:",
  14754. boot_err);
  14755. }
  14756. if (slave_xres_toggled) {
  14757. if (!rc)
  14758. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14759. else
  14760. *slave_xres_toggled = false;
  14761. }
  14762. return rc;
  14763. }
  14764. /*******************************************************************************
  14765. * FUNCTION: pt_bist_slave_bus_test
  14766. *
  14767. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14768. *
  14769. * This test will ensure a good SPI bus connection between the
  14770. * master DUT and the slave DUT(s). This bus connection is ensured by
  14771. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14772. * then there is a master/slave communication issue. Opening file 0 on
  14773. * the master will open it on the slave as well if the slave was detected.
  14774. *
  14775. * RETURN:
  14776. * 0 = Success
  14777. * !0 = Failure
  14778. *
  14779. * PARAMETERS:
  14780. * *dev - pointer to device structure
  14781. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14782. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14783. * *err_str - pointer to error string buffer
  14784. ******************************************************************************/
  14785. static int pt_bist_slave_bus_test(struct device *dev,
  14786. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14787. {
  14788. struct pt_core_data *cd = dev_get_drvdata(dev);
  14789. u8 mode = PT_MODE_UNKNOWN;
  14790. u8 bus_toggled = false;
  14791. u8 file_handle;
  14792. int result = 0;
  14793. int rc = 0;
  14794. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14795. if (rc) {
  14796. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14797. __func__, rc);
  14798. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14799. goto exit;
  14800. }
  14801. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14802. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14803. if (file_handle != PIP2_RAM_FILE) {
  14804. rc = -ENOENT;
  14805. bus_toggled = false;
  14806. pt_debug(dev, DL_ERROR,
  14807. "%s Failed to open bin file\n", __func__);
  14808. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14809. goto exit;
  14810. } else {
  14811. bus_toggled = true;
  14812. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14813. pt_debug(dev, DL_ERROR,
  14814. "%s: File Close failed, file_handle=%d\n",
  14815. __func__, file_handle);
  14816. }
  14817. }
  14818. exit:
  14819. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14820. if (slave_irq_toggled)
  14821. *slave_irq_toggled = bus_toggled;
  14822. if (slave_bus_toggled)
  14823. *slave_bus_toggled = bus_toggled;
  14824. return rc;
  14825. }
  14826. /*******************************************************************************
  14827. * FUNCTION: pt_ttdl_bist_show
  14828. *
  14829. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14830. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14831. *
  14832. * NOTE: This function will reset the DUT and the startup_status bit
  14833. * mask. A pt_enum will be queued after completion.
  14834. *
  14835. * NOTE: The order of the net tests is done to optimize the time it takes
  14836. * to run. The first test is capable of verifying all nets, each subsequent
  14837. * test is only run if the previous was not able to see all nets toggle.
  14838. *
  14839. * RETURN:
  14840. * 0 = success
  14841. * !0 = failure
  14842. *
  14843. * PARAMETERS:
  14844. * *dev - pointer to device structure
  14845. * *attr - pointer to device attributes
  14846. * *buf - pointer to output buffer
  14847. ******************************************************************************/
  14848. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14849. struct device_attribute *attr, char *buf)
  14850. {
  14851. struct pt_core_data *cd = dev_get_drvdata(dev);
  14852. ssize_t ret;
  14853. char *bus_err_str = NULL;
  14854. char *irq_err_str = NULL;
  14855. char *xres_err_str = NULL;
  14856. char *slave_bus_err_str = NULL;
  14857. char *slave_irq_err_str = NULL;
  14858. char *slave_xres_err_str = NULL;
  14859. u8 tests;
  14860. int rc = 0;
  14861. int num_tests = 0;
  14862. int status = 1; /* 0 = Pass, !0 = fail */
  14863. u8 bus_toggled = 0x0F; /* default to untested */
  14864. u8 i2c_toggled = 0x0F; /* default to untested */
  14865. u8 spi_toggled = 0x0F; /* default to untested */
  14866. u8 irq_toggled = 0x0F; /* default to untested */
  14867. u8 xres_toggled = 0x0F; /* default to untested */
  14868. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14869. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14870. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14871. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14872. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14873. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14874. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14875. goto print_results;
  14876. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14877. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14878. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14879. if (cd->multi_chip) {
  14880. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14881. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14882. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14883. if (!slave_bus_err_str ||
  14884. !slave_irq_err_str ||
  14885. !slave_xres_err_str)
  14886. goto print_results;
  14887. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14888. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14889. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14890. }
  14891. /* Turn off the TTDL WD during the test */
  14892. pt_stop_wd_timer(cd);
  14893. /* Shorten default PIP cmd timeout while running BIST */
  14894. cd->pip_cmd_timeout = 200;
  14895. /* Count the number of tests to run */
  14896. tests = cd->ttdl_bist_select;
  14897. while (tests) {
  14898. num_tests += tests & 1;
  14899. tests >>= 1;
  14900. }
  14901. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14902. __func__, cd->ttdl_bist_select, num_tests);
  14903. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14904. if (cd->flashless_dut) {
  14905. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14906. __func__);
  14907. mutex_lock(&cd->system_lock);
  14908. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14909. mutex_unlock(&cd->system_lock);
  14910. }
  14911. /* --------------- TP_XRES BIST TEST --------------- */
  14912. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14913. pt_debug(dev, DL_INFO,
  14914. "%s: ----- Start TP_XRES BIST -----", __func__);
  14915. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14916. &xres_toggled, xres_err_str);
  14917. /* Done if the rest of all nets toggled */
  14918. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14919. goto host_nets_complete;
  14920. }
  14921. /* Flush bus in case a PIP response is waiting from previous test */
  14922. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14923. /* --------------- IRQ BIST TEST --------------- */
  14924. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14925. pt_debug(dev, DL_INFO,
  14926. "%s: ----- Start IRQ BIST -----", __func__);
  14927. bus_toggled = 0xFF;
  14928. irq_toggled = 0xFF;
  14929. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14930. &xres_toggled, irq_err_str);
  14931. /* If this net failed clear results from previous net */
  14932. if (irq_toggled != 1) {
  14933. xres_toggled = 0x0F;
  14934. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14935. }
  14936. if (bus_toggled == 1 && irq_toggled == 1)
  14937. goto host_nets_complete;
  14938. }
  14939. /* Flush bus in case a PIP response is waiting from previous test */
  14940. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14941. /* --------------- BUS BIST TEST --------------- */
  14942. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14943. pt_debug(dev, DL_INFO,
  14944. "%s: ----- Start BUS BIST -----", __func__);
  14945. bus_toggled = 0xFF;
  14946. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14947. /* If this net failed clear results from previous net */
  14948. if (bus_toggled == 0) {
  14949. irq_toggled = 0x0F;
  14950. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14951. }
  14952. }
  14953. host_nets_complete:
  14954. /* --------------- SLAVE XRES BIST TEST --------------- */
  14955. if (cd->multi_chip &&
  14956. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14957. pt_debug(dev, DL_INFO,
  14958. "%s: ----- Start Slave XRES BIST -----", __func__);
  14959. slave_xres_toggled = 0xFF;
  14960. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14961. &slave_bus_toggled, &slave_xres_toggled,
  14962. slave_xres_err_str);
  14963. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14964. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14965. goto print_results;
  14966. }
  14967. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14968. if (cd->multi_chip &&
  14969. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14970. pt_debug(dev, DL_INFO,
  14971. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14972. slave_irq_toggled = 0xFF;
  14973. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14974. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14975. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14976. __func__, slave_irq_toggled);
  14977. if (slave_irq_toggled == 1) {
  14978. slave_bus_toggled = 1;
  14979. goto print_results;
  14980. }
  14981. }
  14982. /* --------------- SLAVE BUS BIST TEST --------------- */
  14983. if (cd->multi_chip &&
  14984. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14985. pt_debug(dev, DL_INFO,
  14986. "%s: ----- Start Slave BUS BIST -----", __func__);
  14987. slave_bus_toggled = 0xFF;
  14988. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14989. &slave_bus_toggled, slave_bus_err_str);
  14990. }
  14991. print_results:
  14992. /* Restore PIP command timeout */
  14993. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14994. /*
  14995. * We're done! - Perform a hard XRES toggle, allowing BL
  14996. * to load FW if there is any in Flash
  14997. */
  14998. mutex_lock(&cd->system_lock);
  14999. cd->hid_reset_cmd_state = 0;
  15000. mutex_unlock(&cd->system_lock);
  15001. pt_debug(dev, DL_INFO,
  15002. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15003. if (cd->flashless_dut) {
  15004. /*
  15005. * For Flashless solution, FW update is triggered after BL is
  15006. * seen that several miliseconds delay is needed.
  15007. */
  15008. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15009. __func__);
  15010. mutex_lock(&cd->system_lock);
  15011. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15012. mutex_unlock(&cd->system_lock);
  15013. /* Reset DUT and wait 100ms to see if loader started */
  15014. pt_hw_hard_reset(cd);
  15015. msleep(100);
  15016. if (cd->fw_updating) {
  15017. pt_debug(dev, DL_INFO,
  15018. "%s: ----- BIST Wait FW Loading ----",
  15019. __func__);
  15020. rc = _pt_request_wait_for_enum_state(
  15021. dev, 4000, STARTUP_STATUS_COMPLETE);
  15022. }
  15023. } else {
  15024. if (cd->mode == PT_MODE_BOOTLOADER) {
  15025. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15026. if (rc) {
  15027. pt_debug(dev, DL_ERROR,
  15028. "%s Failed to launch app\n", __func__);
  15029. rc = pt_hw_hard_reset(cd);
  15030. }
  15031. }
  15032. /*
  15033. * If FW exists the BL may have just started or will start soon,
  15034. * so the FW sentinel may be on it's way but with no FW it will
  15035. * not arrive, wait for it before deciding if we need to queue
  15036. * an enum.
  15037. */
  15038. rc = _pt_request_wait_for_enum_state(
  15039. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15040. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15041. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15042. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15043. __func__);
  15044. pt_queue_enum(cd);
  15045. rc = _pt_request_wait_for_enum_state(
  15046. dev, 2000, STARTUP_STATUS_COMPLETE);
  15047. }
  15048. }
  15049. msleep(20);
  15050. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15051. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15052. pt_start_wd_timer(cd);
  15053. /* Canned print if any memory allocation issues */
  15054. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15055. ret = scnprintf(buf, strlen(buf),
  15056. "Status: %d\n"
  15057. "I2C (SDA,SCL): [UNTEST]\n"
  15058. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15059. "IRQ connection: [UNTEST]\n"
  15060. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15061. if (cd->multi_chip) {
  15062. ret += scnprintf(buf + ret, strlen(buf),
  15063. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15064. "I/P IRQ connection: [UNTEST]\n"
  15065. "I/P TP_XRES connection: [UNTEST]\n");
  15066. }
  15067. } else {
  15068. status = 0;
  15069. if (bus_toggled == 1)
  15070. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15071. if (irq_toggled == 1)
  15072. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15073. if (xres_toggled == 1)
  15074. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15075. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15076. status += bus_toggled;
  15077. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15078. status += irq_toggled;
  15079. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15080. status += xres_toggled;
  15081. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15082. __func__, status, bus_toggled, irq_toggled,
  15083. xres_toggled);
  15084. if (cd->multi_chip) {
  15085. if (slave_irq_toggled == 1)
  15086. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15087. if (slave_xres_toggled == 1)
  15088. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15089. if (slave_bus_toggled == 1)
  15090. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15091. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15092. status += slave_bus_toggled;
  15093. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15094. status += slave_irq_toggled;
  15095. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15096. status += slave_xres_toggled;
  15097. pt_debug(dev, DL_ERROR,
  15098. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15099. __func__, status, bus_toggled, irq_toggled,
  15100. xres_toggled, slave_bus_toggled,
  15101. slave_irq_toggled, slave_xres_toggled);
  15102. }
  15103. if (cd->bus_ops->bustype == BUS_I2C)
  15104. i2c_toggled = bus_toggled;
  15105. else
  15106. spi_toggled = bus_toggled;
  15107. ret = scnprintf(buf, strlen(buf),
  15108. "Status: %d\n"
  15109. "I2C (SDA,SCL): %s %s\n"
  15110. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15111. "IRQ connection: %s %s\n"
  15112. "TP_XRES connection: %s %s\n",
  15113. status == num_tests ? 0 : 1,
  15114. i2c_toggled == 0x0F ? "[UNTEST]" :
  15115. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15116. i2c_toggled == 0x0F ? "" : bus_err_str,
  15117. spi_toggled == 0x0F ? "[UNTEST]" :
  15118. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15119. spi_toggled == 0x0F ? "" : bus_err_str,
  15120. irq_toggled == 0x0F ? "[UNTEST]" :
  15121. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15122. irq_err_str,
  15123. xres_toggled == 0x0F ? "[UNTEST]" :
  15124. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15125. xres_err_str);
  15126. if (cd->multi_chip) {
  15127. ret += scnprintf(buf + ret, strlen(buf),
  15128. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15129. "I/P IRQ connection: %s %s\n"
  15130. "I/P TP_XRES connection: %s %s\n",
  15131. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15132. slave_bus_toggled == 1 ? "[ OK ]" :
  15133. "[FAILED]", slave_bus_err_str,
  15134. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15135. slave_irq_toggled == 1 ? "[ OK ]" :
  15136. "[FAILED]", slave_irq_err_str,
  15137. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15138. slave_xres_toggled == 1 ? "[ OK ]" :
  15139. "[FAILED]", slave_xres_err_str);
  15140. }
  15141. }
  15142. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15143. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15144. __func__, cd->startup_status);
  15145. kfree(bus_err_str);
  15146. kfree(irq_err_str);
  15147. kfree(xres_err_str);
  15148. return ret;
  15149. }
  15150. /*******************************************************************************
  15151. * FUNCTION: pt_ttdl_bist_store
  15152. *
  15153. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15154. *
  15155. * RETURN: Size of passed in buffer if successful
  15156. *
  15157. * PARAMETERS:
  15158. * *dev - pointer to device structure
  15159. * *attr - pointer to device attributes
  15160. * *buf - pointer to command buffer
  15161. * size - size of buf
  15162. ******************************************************************************/
  15163. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15164. struct device_attribute *attr, const char *buf, size_t size)
  15165. {
  15166. struct pt_core_data *cd = dev_get_drvdata(dev);
  15167. u32 input_data[2] = {0};
  15168. int length;
  15169. int rc = 0;
  15170. /* Maximum input of one value */
  15171. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15172. ARRAY_SIZE(input_data));
  15173. if (length != 1) {
  15174. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15175. __func__);
  15176. rc = -EINVAL;
  15177. goto exit;
  15178. } else {
  15179. mutex_lock(&cd->system_lock);
  15180. cd->ttdl_bist_select = input_data[0];
  15181. mutex_unlock(&cd->system_lock);
  15182. }
  15183. exit:
  15184. if (rc)
  15185. return rc;
  15186. return size;
  15187. }
  15188. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15189. pt_ttdl_bist_store);
  15190. /*******************************************************************************
  15191. * FUNCTION: pt_flush_bus_store
  15192. *
  15193. * SUMMARY: Store method for the flush_bus sysfs node.
  15194. *
  15195. * RETURN: Size of passed in buffer if successful
  15196. *
  15197. * PARAMETERS:
  15198. * *dev - pointer to device structure
  15199. * *attr - pointer to device attributes
  15200. * *buf - pointer to command buffer
  15201. * size - size of buf
  15202. ******************************************************************************/
  15203. static ssize_t pt_flush_bus_store(struct device *dev,
  15204. struct device_attribute *attr, const char *buf, size_t size)
  15205. {
  15206. struct pt_core_data *cd = dev_get_drvdata(dev);
  15207. u32 input_data[2] = {0};
  15208. int length;
  15209. int rc = 0;
  15210. /* Maximum input of one value */
  15211. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15212. ARRAY_SIZE(input_data));
  15213. if (length != 1) {
  15214. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15215. __func__);
  15216. rc = -EINVAL;
  15217. goto exit;
  15218. }
  15219. mutex_lock(&cd->system_lock);
  15220. if (input_data[0] == 0)
  15221. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15222. else if (input_data[0] == 1)
  15223. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15224. else
  15225. rc = -EINVAL;
  15226. mutex_unlock(&cd->system_lock);
  15227. exit:
  15228. if (rc)
  15229. return rc;
  15230. return size;
  15231. }
  15232. /*******************************************************************************
  15233. * FUNCTION: pt_flush_bus_show
  15234. *
  15235. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15236. * based on either the size of the first two bytes or a blind 256 bytes.
  15237. *
  15238. * RETURN:
  15239. * 0 = success
  15240. * !0 = failure
  15241. *
  15242. * PARAMETERS:
  15243. * *dev - pointer to device structure
  15244. * *attr - pointer to device attributes
  15245. * *buf - pointer to output buffer
  15246. ******************************************************************************/
  15247. static ssize_t pt_flush_bus_show(struct device *dev,
  15248. struct device_attribute *attr, char *buf)
  15249. {
  15250. ssize_t ret = 0;
  15251. ssize_t bytes = 0;
  15252. struct pt_core_data *cd = dev_get_drvdata(dev);
  15253. mutex_lock(&cd->system_lock);
  15254. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15255. ret = scnprintf(buf, strlen(buf),
  15256. "Status: 0\n"
  15257. "%s: %zd\n",
  15258. "Bytes flushed", bytes);
  15259. mutex_unlock(&cd->system_lock);
  15260. return ret;
  15261. }
  15262. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15263. pt_flush_bus_store);
  15264. /*******************************************************************************
  15265. * FUNCTION: pt_pip2_ping_test_store
  15266. *
  15267. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15268. *
  15269. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15270. * someone may want to test sending invalid packet lengths so any values
  15271. * up to 255 are allowed.
  15272. *
  15273. * RETURN: Size of passed in buffer if successful
  15274. *
  15275. * PARAMETERS:
  15276. * *dev - pointer to device structure
  15277. * *attr - pointer to device attributes
  15278. * *buf - pointer to command buffer
  15279. * size - size of buf
  15280. ******************************************************************************/
  15281. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15282. struct device_attribute *attr, const char *buf, size_t size)
  15283. {
  15284. struct pt_core_data *cd = dev_get_drvdata(dev);
  15285. u32 input_data[2];
  15286. int length;
  15287. int rc = 0;
  15288. /* Maximum input of one value */
  15289. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15290. ARRAY_SIZE(input_data));
  15291. if (length != 1) {
  15292. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15293. __func__);
  15294. rc = -EINVAL;
  15295. goto exit;
  15296. }
  15297. mutex_lock(&cd->system_lock);
  15298. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15299. cd->ping_test_size = input_data[0];
  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_ping_test_show
  15310. *
  15311. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15312. * PING command and ramps up the optional payload from 0 to
  15313. * ping_test_size.
  15314. * The max payload size is 247:
  15315. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15316. *
  15317. * RETURN:
  15318. * 0 = success
  15319. * !0 = failure
  15320. *
  15321. * PARAMETERS:
  15322. * *dev - pointer to device structure
  15323. * *attr - pointer to device attributes
  15324. * *buf - pointer to output buffer
  15325. ******************************************************************************/
  15326. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15327. struct device_attribute *attr, char *buf)
  15328. {
  15329. struct pt_core_data *cd = dev_get_drvdata(dev);
  15330. ssize_t ret;
  15331. int last_packet_size;
  15332. int rc = 0;
  15333. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15334. if (rc) {
  15335. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15336. return ret;
  15337. }
  15338. ret = scnprintf(buf, strlen(buf),
  15339. "Status: %d\n"
  15340. "PING payload test passed with packet sizes 0 - %d\n",
  15341. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15342. last_packet_size);
  15343. return ret;
  15344. }
  15345. /*******************************************************************************
  15346. * FUNCTION: pt_t_refresh_store
  15347. *
  15348. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15349. * in integer as the number of interrupts to count. A timer is started to
  15350. * calculate the total time it takes to see that number of interrupts.
  15351. *
  15352. * RETURN: Size of passed in buffer if successful
  15353. *
  15354. * PARAMETERS:
  15355. * *dev - pointer to device structure
  15356. * *attr - pointer to device attributes
  15357. * *buf - pointer to buffer that hold the command parameters
  15358. * size - size of buf
  15359. ******************************************************************************/
  15360. static ssize_t pt_t_refresh_store(struct device *dev,
  15361. struct device_attribute *attr, const char *buf, size_t size)
  15362. {
  15363. struct pt_core_data *cd = dev_get_drvdata(dev);
  15364. u32 input_data[2];
  15365. int length;
  15366. int rc = 0;
  15367. /* Maximum input of one value */
  15368. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15369. ARRAY_SIZE(input_data));
  15370. if (length != 1) {
  15371. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15372. __func__);
  15373. rc = -EINVAL;
  15374. goto exit;
  15375. }
  15376. mutex_lock(&cd->system_lock);
  15377. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15378. input_data[0]);
  15379. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15380. cd->t_refresh_total = input_data[0];
  15381. cd->t_refresh_count = 0;
  15382. cd->t_refresh_active = 1;
  15383. } else {
  15384. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15385. rc = -EINVAL;
  15386. }
  15387. mutex_unlock(&cd->system_lock);
  15388. exit:
  15389. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15390. if (rc)
  15391. return rc;
  15392. return size;
  15393. }
  15394. /*******************************************************************************
  15395. * FUNCTION: pt_t_refresh_show
  15396. *
  15397. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15398. * of the T-Refresh timer counting the time it takes to see a user defined
  15399. * number of interrupts.
  15400. *
  15401. * RETURN:
  15402. * 0 = success
  15403. * !0 = failure
  15404. *
  15405. * PARAMETERS:
  15406. * *dev - pointer to device structure
  15407. * *attr - pointer to device attributes
  15408. * *buf - pointer to output buffer
  15409. ******************************************************************************/
  15410. static ssize_t pt_t_refresh_show(struct device *dev,
  15411. struct device_attribute *attr, char *buf)
  15412. {
  15413. ssize_t ret = 0;
  15414. struct pt_core_data *cd = dev_get_drvdata(dev);
  15415. u32 whole;
  15416. u16 fraction;
  15417. mutex_lock(&cd->system_lock);
  15418. /* Check if we have counted the number requested */
  15419. if (cd->t_refresh_count != cd->t_refresh_total) {
  15420. ret = scnprintf(buf, strlen(buf),
  15421. "Status: 0\n"
  15422. "%s: %d\n",
  15423. "Still counting... current IRQ count",
  15424. cd->t_refresh_count);
  15425. } else {
  15426. /* Ensure T-Refresh is de-activated */
  15427. cd->t_refresh_active = 0;
  15428. whole = cd->t_refresh_time / cd->t_refresh_count;
  15429. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15430. fraction = fraction * 1000 / cd->t_refresh_count;
  15431. ret = scnprintf(buf, strlen(buf),
  15432. "Status: 0\n"
  15433. "%s: %d\n"
  15434. "%s: %d\n"
  15435. "%s: %d\n"
  15436. "%s: %d.%02d\n",
  15437. "Requested IRQ Count ", cd->t_refresh_total,
  15438. "IRQ Counted ", cd->t_refresh_count,
  15439. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15440. "Average T-Refresh (ms) ", whole, fraction);
  15441. }
  15442. mutex_unlock(&cd->system_lock);
  15443. return ret;
  15444. }
  15445. /*******************************************************************************
  15446. * FUNCTION: pt_dut_status_show
  15447. *
  15448. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15449. * more items such as operation mode,easywake state are added in the future.
  15450. *
  15451. * RETURN: Char buffer with printed scan status information
  15452. *
  15453. * PARAMETERS:
  15454. * *dev - pointer to device structure
  15455. * *attr - pointer to device attributes
  15456. * *buf - pointer to output buffer
  15457. ******************************************************************************/
  15458. static ssize_t pt_dut_status_show(struct device *dev,
  15459. struct device_attribute *attr, char *buf)
  15460. {
  15461. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15462. u8 mode = PT_MODE_UNKNOWN;
  15463. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15464. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15465. struct pt_core_data *cd = dev_get_drvdata(dev);
  15466. ssize_t ret;
  15467. u16 calculated_crc = 0;
  15468. u16 stored_crc = 0;
  15469. u8 status;
  15470. int rc = 0;
  15471. /* In STANDBY the DUT will not repond to any PIP cmd */
  15472. if (cd->fw_sys_mode_in_standby_state) {
  15473. mode = PT_MODE_OPERATIONAL;
  15474. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15475. goto print_limited_results;
  15476. }
  15477. /* Retrieve mode and FW system mode which can only be 0-4 */
  15478. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15479. if (rc || mode == PT_MODE_UNKNOWN) {
  15480. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15481. "%s: %d\n"
  15482. "%s: n/a\n"
  15483. "%s: n/a\n"
  15484. "%s: n/a\n"
  15485. "%s: n/a\n",
  15486. "Status", rc,
  15487. "Active Exec ",
  15488. "FW System Mode ",
  15489. "Stored CRC ",
  15490. "Calculated CRC ");
  15491. return ret;
  15492. } else {
  15493. if (mode == PT_MODE_OPERATIONAL) {
  15494. if (sys_mode > FW_SYS_MODE_MAX)
  15495. sys_mode = FW_SYS_MODE_UNDEFINED;
  15496. if (sys_mode != FW_SYS_MODE_TEST)
  15497. goto print_limited_results;
  15498. rc = pt_pip_verify_config_block_crc_(cd,
  15499. PT_TCH_PARM_EBID, &status,
  15500. &calculated_crc, &stored_crc);
  15501. if (rc)
  15502. goto print_limited_results;
  15503. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15504. "%s: %d\n"
  15505. "%s: %s\n"
  15506. "%s: %s\n"
  15507. "%s: 0x%04X\n"
  15508. "%s: 0x%04X\n",
  15509. "Status", rc,
  15510. "Active Exec ", "FW",
  15511. "FW System Mode ", outputstring[sys_mode],
  15512. "Stored CRC ", stored_crc,
  15513. "Calculated CRC ", calculated_crc);
  15514. return ret;
  15515. } else {
  15516. /* When in BL or unknon mode Active Exec is "n/a" */
  15517. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15518. }
  15519. }
  15520. print_limited_results:
  15521. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15522. "%s: %d\n"
  15523. "%s: %s\n"
  15524. "%s: %s\n"
  15525. "%s: n/a\n"
  15526. "%s: n/a\n",
  15527. "Status", rc,
  15528. "Active Exec ",
  15529. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15530. "FW System Mode ", outputstring[sys_mode],
  15531. "Stored CRC ",
  15532. "Calculated CRC ");
  15533. return ret;
  15534. }
  15535. #endif /* TTDL_DIAGNOSTICS */
  15536. /*******************************************************************************
  15537. * Structures of sysfs attributes for all DUT dependent sysfs node
  15538. ******************************************************************************/
  15539. static struct attribute *early_attrs[] = {
  15540. &dev_attr_hw_version.attr,
  15541. &dev_attr_drv_version.attr,
  15542. &dev_attr_drv_ver.attr,
  15543. &dev_attr_fw_version.attr,
  15544. &dev_attr_sysinfo.attr,
  15545. &dev_attr_pip2_cmd_rsp.attr,
  15546. &dev_attr_command.attr,
  15547. &dev_attr_drv_debug.attr,
  15548. &dev_attr_hw_reset.attr,
  15549. &dev_attr_response.attr,
  15550. &dev_attr_ttdl_restart.attr,
  15551. #ifdef TTDL_DIAGNOSTICS
  15552. &dev_attr_ttdl_status.attr,
  15553. &dev_attr_pip2_enter_bl.attr,
  15554. &dev_attr_pip2_exit_bl.attr,
  15555. &dev_attr_err_gpio.attr,
  15556. &dev_attr_flush_bus.attr,
  15557. &dev_attr_ttdl_bist.attr,
  15558. #endif /* TTDL_DIAGNOSTICS */
  15559. NULL,
  15560. };
  15561. static struct attribute_group early_attr_group = {
  15562. .attrs = early_attrs,
  15563. };
  15564. static struct device_attribute pip2_attributes[] = {
  15565. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15566. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15567. #ifdef TTDL_DIAGNOSTICS
  15568. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15569. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15570. pt_pip2_ping_test_store),
  15571. #endif
  15572. };
  15573. static struct device_attribute attributes[] = {
  15574. __ATTR(dut_debug, 0644,
  15575. pt_dut_debug_show, pt_drv_debug_store),
  15576. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15577. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15578. __ATTR(get_param, 0644,
  15579. pt_get_param_show, pt_get_param_store),
  15580. #ifdef EASYWAKE_TSG6
  15581. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15582. pt_easy_wakeup_gesture_store),
  15583. __ATTR(easy_wakeup_gesture_id, 0444,
  15584. pt_easy_wakeup_gesture_id_show, NULL),
  15585. __ATTR(easy_wakeup_gesture_data, 0444,
  15586. pt_easy_wakeup_gesture_data_show, NULL),
  15587. #endif
  15588. #ifdef TTDL_DIAGNOSTICS
  15589. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15590. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15591. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15592. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15593. #endif /* TTDL_DIAGNOSTICS */
  15594. };
  15595. /*******************************************************************************
  15596. * FUNCTION: add_sysfs_interfaces
  15597. *
  15598. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15599. *
  15600. * RETURN:
  15601. * 0 = success
  15602. * !0 = failure
  15603. *
  15604. * PARAMETERS:
  15605. * *dev - pointer to device structure
  15606. ******************************************************************************/
  15607. static int add_sysfs_interfaces(struct device *dev)
  15608. {
  15609. int i;
  15610. int j = 0;
  15611. struct pt_core_data *cd = dev_get_drvdata(dev);
  15612. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15613. if (device_create_file(dev, attributes + i))
  15614. goto undo;
  15615. }
  15616. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15617. __func__, cd->active_dut_generation);
  15618. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15619. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15620. if (device_create_file(dev, pip2_attributes + j))
  15621. goto undo;
  15622. }
  15623. }
  15624. return 0;
  15625. undo:
  15626. for (i--; i >= 0; i--)
  15627. device_remove_file(dev, attributes + i);
  15628. for (j--; j >= 0; j--)
  15629. device_remove_file(dev, pip2_attributes + j);
  15630. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15631. __func__);
  15632. return -ENODEV;
  15633. }
  15634. /*******************************************************************************
  15635. * FUNCTION: remove_sysfs_interfaces
  15636. *
  15637. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15638. *
  15639. * RETURN: void
  15640. *
  15641. * PARAMETERS:
  15642. * *dev - pointer to device structure
  15643. ******************************************************************************/
  15644. static void remove_sysfs_interfaces(struct device *dev)
  15645. {
  15646. int i;
  15647. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15648. device_remove_file(dev, attributes + i);
  15649. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15650. device_remove_file(dev, pip2_attributes + i);
  15651. }
  15652. /*******************************************************************************
  15653. * FUNCTION: remove_sysfs_and_modules
  15654. *
  15655. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15656. *
  15657. * RETURN: void
  15658. *
  15659. * PARAMETERS:
  15660. * *dev - pointer to device structure
  15661. ******************************************************************************/
  15662. static void remove_sysfs_and_modules(struct device *dev)
  15663. {
  15664. struct pt_core_data *cd = dev_get_drvdata(dev);
  15665. /* Queued work should be removed before to release loader module */
  15666. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15667. pt_release_modules(cd);
  15668. pt_btn_release(dev);
  15669. pt_mt_release(dev);
  15670. remove_sysfs_interfaces(dev);
  15671. }
  15672. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15673. {
  15674. int retval;
  15675. /* Get pinctrl if target uses pinctrl */
  15676. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15677. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15678. retval = PTR_ERR(cd->ts_pinctrl);
  15679. dev_dbg(cd->dev,
  15680. "Target does not use pinctrl %d\n", retval);
  15681. goto err_pinctrl_get;
  15682. }
  15683. cd->pinctrl_state_active
  15684. = pinctrl_lookup_state(cd->ts_pinctrl,
  15685. PINCTRL_STATE_ACTIVE);
  15686. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15687. retval = PTR_ERR(cd->pinctrl_state_active);
  15688. dev_err(cd->dev,
  15689. "Can not lookup %s pinstate %d\n",
  15690. PINCTRL_STATE_ACTIVE, retval);
  15691. goto err_pinctrl_lookup;
  15692. }
  15693. cd->pinctrl_state_suspend
  15694. = pinctrl_lookup_state(cd->ts_pinctrl,
  15695. PINCTRL_STATE_SUSPEND);
  15696. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15697. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15698. dev_err(cd->dev,
  15699. "Can not lookup %s pinstate %d\n",
  15700. PINCTRL_STATE_SUSPEND, retval);
  15701. goto err_pinctrl_lookup;
  15702. }
  15703. cd->pinctrl_state_release
  15704. = pinctrl_lookup_state(cd->ts_pinctrl,
  15705. PINCTRL_STATE_RELEASE);
  15706. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15707. retval = PTR_ERR(cd->pinctrl_state_release);
  15708. dev_dbg(cd->dev,
  15709. "Can not lookup %s pinstate %d\n",
  15710. PINCTRL_STATE_RELEASE, retval);
  15711. }
  15712. return 0;
  15713. err_pinctrl_lookup:
  15714. devm_pinctrl_put(cd->ts_pinctrl);
  15715. err_pinctrl_get:
  15716. cd->ts_pinctrl = NULL;
  15717. return retval;
  15718. }
  15719. /*******************************************************************************
  15720. *******************************************************************************
  15721. * FUNCTION: pt_probe
  15722. *
  15723. * SUMMARY: Probe of the core module.
  15724. *
  15725. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15726. * probe functionality is split into two functions; pt_probe() and
  15727. * pt_probe_complete(). the initial setup is done in this function which
  15728. * then creates a WORK task which runs after the probe timer expires. This
  15729. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15730. *
  15731. * RETURN:
  15732. * 0 = success
  15733. * !0 = failure
  15734. *
  15735. * PARAMETERS:
  15736. * *ops - pointer to the bus
  15737. * *dev - pointer to the device structure
  15738. * irq - IRQ
  15739. * xfer_buf_size - size of the buffer
  15740. ******************************************************************************/
  15741. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15742. u16 irq, size_t xfer_buf_size)
  15743. {
  15744. struct pt_core_data *cd;
  15745. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15746. enum pt_atten_type type;
  15747. struct i2c_client *client = to_i2c_client(dev);
  15748. int rc = 0;
  15749. u8 pip_ver_major;
  15750. u8 pip_ver_minor;
  15751. u32 status = STARTUP_STATUS_START;
  15752. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15753. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15754. __func__);
  15755. rc = -ENODEV;
  15756. goto error_no_pdata;
  15757. }
  15758. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15759. if (!pdata->core_pdata->power) {
  15760. pt_debug(dev, DL_ERROR,
  15761. "%s: Missing platform data function\n",
  15762. __func__);
  15763. rc = -ENODEV;
  15764. goto error_no_pdata;
  15765. }
  15766. }
  15767. /* get context and debug print buffers */
  15768. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15769. if (!cd) {
  15770. rc = -ENOMEM;
  15771. goto error_no_pdata;
  15772. }
  15773. /* Initialize device info */
  15774. cd->dev = dev;
  15775. cd->pdata = pdata;
  15776. cd->cpdata = pdata->core_pdata;
  15777. cd->bus_ops = ops;
  15778. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15779. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15780. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15781. cd->hw_detected = false;
  15782. cd->pip2_prot_active = false;
  15783. cd->pip2_send_user_cmd = false;
  15784. cd->bl_pip_ver_ready = false;
  15785. cd->app_pip_ver_ready = false;
  15786. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15787. cd->get_param_id = 0;
  15788. cd->watchdog_enabled = 0;
  15789. cd->startup_retry_count = 0;
  15790. cd->core_probe_complete = 0;
  15791. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15792. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15793. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15794. cd->flashless_dut = 0;
  15795. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15796. cd->bl_with_no_int = 0;
  15797. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15798. cd->multi_chip = PT_FEATURE_DISABLE;
  15799. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15800. cd->sleep_state = SS_SLEEP_NONE;
  15801. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15802. cd->set_dut_generation = true;
  15803. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15804. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15805. cd->set_dut_generation = true;
  15806. cd->active_dut_generation = DUT_PIP1_ONLY;
  15807. } else {
  15808. cd->set_dut_generation = false;
  15809. cd->active_dut_generation = DUT_UNKNOWN;
  15810. }
  15811. /* Initialize with platform data */
  15812. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15813. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15814. cd->hid_cmd_state = 1;
  15815. cd->fw_updating = false;
  15816. cd->multi_chip = 0;
  15817. #ifdef TTDL_DIAGNOSTICS
  15818. cd->t_refresh_active = 0;
  15819. cd->t_refresh_count = 0;
  15820. cd->pip2_crc_error_count = 0;
  15821. cd->wd_xres_count = 0;
  15822. cd->bl_retry_packet_count = 0;
  15823. cd->file_erase_timeout_count = 0;
  15824. cd->show_tt_data = false;
  15825. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15826. cd->err_gpio = 0;
  15827. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15828. cd->ttdl_bist_select = 0x07;
  15829. cd->force_pip2_seq = 0;
  15830. #endif /* TTDL_DIAGNOSTICS */
  15831. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15832. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15833. sizeof(PT_PIP2_BIN_FILE_PATH));
  15834. pt_init_hid_descriptor(&cd->hid_desc);
  15835. /* Read and store the descriptor lengths */
  15836. cd->hid_core.hid_report_desc_len =
  15837. le16_to_cpu(cd->hid_desc.report_desc_len);
  15838. cd->hid_core.hid_max_input_len =
  15839. le16_to_cpu(cd->hid_desc.max_input_len);
  15840. cd->hid_core.hid_max_output_len =
  15841. le16_to_cpu(cd->hid_desc.max_output_len);
  15842. /* Initialize mutexes and spinlocks */
  15843. mutex_init(&cd->module_list_lock);
  15844. mutex_init(&cd->system_lock);
  15845. mutex_init(&cd->sysfs_lock);
  15846. mutex_init(&cd->ttdl_restart_lock);
  15847. mutex_init(&cd->firmware_class_lock);
  15848. spin_lock_init(&cd->spinlock);
  15849. /* Initialize module list */
  15850. INIT_LIST_HEAD(&cd->module_list);
  15851. /* Initialize attention lists */
  15852. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15853. INIT_LIST_HEAD(&cd->atten_list[type]);
  15854. /* Initialize parameter list */
  15855. INIT_LIST_HEAD(&cd->param_list);
  15856. /* Initialize wait queue */
  15857. init_waitqueue_head(&cd->wait_q);
  15858. rc = pt_ts_pinctrl_init(cd);
  15859. if (!rc && cd->ts_pinctrl) {
  15860. /*
  15861. * Pinctrl handle is optional. If pinctrl handle is found
  15862. * let pins to be configured in active state. If not
  15863. * found continue further without error.
  15864. */
  15865. rc = pinctrl_select_state(cd->ts_pinctrl,
  15866. cd->pinctrl_state_active);
  15867. if (rc < 0)
  15868. dev_err(&client->dev, "failed to select pin to active state\n");
  15869. }
  15870. rc = pt_get_regulator(cd, true);
  15871. if (rc) {
  15872. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15873. goto error_alloc_data;
  15874. }
  15875. rc = pt_enable_regulator(cd, true);
  15876. if (rc) {
  15877. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15878. goto error_get_regulator;
  15879. }
  15880. /* Initialize works */
  15881. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15882. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15883. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15884. /* Initialize HID specific data */
  15885. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15886. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15887. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15888. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15889. cd->hid_core.hid_desc_register =
  15890. cpu_to_le16(cd->cpdata->hid_desc_register);
  15891. /* Set platform easywake value */
  15892. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15893. #ifdef CONFIG_DRM
  15894. /* Setup active dsi panel */
  15895. active_panel = cd->cpdata->active_panel;
  15896. #endif
  15897. /* Set platform panel_id value */
  15898. cd->panel_id_support = cd->cpdata->panel_id_support;
  15899. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15900. /* Set Panel ID to default to 0 */
  15901. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15902. else
  15903. /* Set Panel ID to Not Enabled */
  15904. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15905. /* Initialize hw_version default to FFFF.FFFF.FF */
  15906. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15907. dev_set_drvdata(dev, cd);
  15908. /* PtSBC builds will call this function in pt_probe_complete() */
  15909. pt_add_core(dev);
  15910. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15911. if (rc) {
  15912. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15913. __func__);
  15914. goto error_enable_regulator;
  15915. }
  15916. /*
  15917. * Save the pointer to a global value, which will be used
  15918. * in ttdl_restart function
  15919. */
  15920. cd->bus_ops = ops;
  15921. /*
  15922. * When the IRQ GPIO is not direclty accessible and no function is
  15923. * defined to get the IRQ status, the IRQ passed in must be assigned
  15924. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15925. */
  15926. if (!cd->cpdata->irq_stat) {
  15927. cd->irq = irq;
  15928. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15929. __func__, cd->irq);
  15930. }
  15931. /* Call platform init function before setting up the GPIO's */
  15932. if (cd->cpdata->init) {
  15933. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15934. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15935. } else {
  15936. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15937. __func__);
  15938. rc = 0;
  15939. }
  15940. if (rc < 0) {
  15941. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15942. __func__, rc);
  15943. }
  15944. /* Power on any needed regulator(s) */
  15945. if (cd->cpdata->setup_power) {
  15946. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15947. rc = cd->cpdata->setup_power(cd->cpdata,
  15948. PT_MT_POWER_ON, cd->dev);
  15949. } else {
  15950. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15951. __func__);
  15952. rc = 0;
  15953. }
  15954. if (rc < 0)
  15955. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15956. __func__, rc);
  15957. #ifdef TTDL_DIAGNOSTICS
  15958. cd->watchdog_irq_stuck_count = 0;
  15959. cd->bus_transmit_error_count = 0;
  15960. #endif /* TTDL_DIAGNOSTICS */
  15961. if (cd->cpdata->detect) {
  15962. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15963. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15964. pt_platform_detect_read);
  15965. if (!rc) {
  15966. cd->hw_detected = true;
  15967. pt_debug(cd->dev, DL_INFO,
  15968. "%s: HW detected\n", __func__);
  15969. } else {
  15970. cd->hw_detected = false;
  15971. pt_debug(cd->dev, DL_INFO,
  15972. "%s: No HW detected\n", __func__);
  15973. rc = -ENODEV;
  15974. goto error_detect;
  15975. }
  15976. } else {
  15977. pt_debug(dev, DL_ERROR,
  15978. "%s: PARADE No HW detect function pointer\n",
  15979. __func__);
  15980. /*
  15981. * "hw_reset" is not needed in the "if" statement,
  15982. * because "hw_reset" is already included in "hw_detect"
  15983. * function.
  15984. */
  15985. rc = pt_hw_hard_reset(cd);
  15986. if (rc)
  15987. pt_debug(cd->dev, DL_ERROR,
  15988. "%s: FAILED to execute HARD reset\n",
  15989. __func__);
  15990. }
  15991. if (cd->cpdata->setup_irq) {
  15992. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15993. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15994. if (rc) {
  15995. pt_debug(dev, DL_ERROR,
  15996. "%s: Error, couldn't setup IRQ\n", __func__);
  15997. goto error_setup_irq;
  15998. }
  15999. } else {
  16000. pt_debug(dev, DL_ERROR,
  16001. "%s: IRQ function pointer not setup\n",
  16002. __func__);
  16003. goto error_setup_irq;
  16004. }
  16005. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16006. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16007. (unsigned long)cd);
  16008. #else
  16009. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16010. #endif
  16011. pt_stop_wd_timer(cd);
  16012. #ifdef TTHE_TUNER_SUPPORT
  16013. mutex_init(&cd->tthe_lock);
  16014. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16015. 0644, NULL, cd, &tthe_debugfs_fops);
  16016. #endif
  16017. rc = device_init_wakeup(dev, 1);
  16018. if (rc < 0)
  16019. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16020. __func__, rc);
  16021. pm_runtime_get_noresume(dev);
  16022. pm_runtime_set_active(dev);
  16023. pm_runtime_enable(dev);
  16024. /* If IRQ asserted, read out all from buffer to release INT pin */
  16025. if (cd->cpdata->irq_stat) {
  16026. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16027. } else {
  16028. /* Force a read in case the reset sentinel already arrived */
  16029. rc = pt_read_input(cd);
  16030. if (!rc)
  16031. pt_parse_input(cd);
  16032. }
  16033. /* Without sleep DUT is not ready and will NAK the first write */
  16034. msleep(150);
  16035. /* Attempt to set the DUT generation if not yet set */
  16036. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16037. if (cd->bl_pip_ver_ready ||
  16038. (cd->app_pip_ver_ready &&
  16039. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16040. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16041. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16042. __func__, cd->active_dut_generation);
  16043. } else {
  16044. rc = _pt_detect_dut_generation(cd->dev,
  16045. &status, &cd->active_dut_generation,
  16046. &cd->mode);
  16047. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16048. || rc) {
  16049. pt_debug(cd->dev, DL_ERROR,
  16050. " === DUT Gen unknown, Skip Enum ===\n");
  16051. goto skip_enum;
  16052. }
  16053. }
  16054. }
  16055. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16056. &pip_ver_major, &pip_ver_minor);
  16057. if (pip_ver_major == 2) {
  16058. cd->bl_pip_ver_ready = true;
  16059. pt_debug(dev, DL_ERROR,
  16060. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16061. pip_ver_minor);
  16062. cd->hw_detected = true;
  16063. } else if (pip_ver_major == 1) {
  16064. cd->app_pip_ver_ready = true;
  16065. pt_debug(dev, DL_ERROR,
  16066. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16067. cd->hw_detected = true;
  16068. } else {
  16069. cd->sysinfo.ttdata.pip_ver_major = 0;
  16070. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16071. cd->app_pip_ver_ready = false;
  16072. cd->hw_detected = false;
  16073. pt_debug(dev, DL_ERROR,
  16074. " === PIP Version Not Detected, Skip Enum ===\n");
  16075. /* For legacy DUTS proceed, enum will attempt to launch app */
  16076. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16077. goto skip_enum;
  16078. }
  16079. rc = pt_enum_with_dut(cd, false, &status);
  16080. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16081. __func__, cd->startup_status, status);
  16082. if (rc == -ENODEV) {
  16083. pt_debug(cd->dev, DL_ERROR,
  16084. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16085. /* For PtSBC don't error out, allow TTDL to stay up */
  16086. goto error_after_startup;
  16087. }
  16088. /* Suspend scanning until probe is complete to avoid asyc touches */
  16089. pt_pip_suspend_scanning_(cd);
  16090. if (cd->hw_detected) {
  16091. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16092. __func__);
  16093. rc = add_sysfs_interfaces(dev);
  16094. if (rc < 0) {
  16095. pt_debug(dev, DL_ERROR,
  16096. "%s: Error, fail sysfs init\n", __func__);
  16097. goto error_after_startup;
  16098. }
  16099. } else {
  16100. pt_debug(dev, DL_ERROR,
  16101. "%s: No HW detected, sysfs interfaces not added\n",
  16102. __func__);
  16103. }
  16104. skip_enum:
  16105. pm_runtime_put_sync(dev);
  16106. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16107. rc = pt_mt_probe(dev);
  16108. if (rc < 0) {
  16109. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16110. __func__);
  16111. goto error_after_sysfs_create;
  16112. }
  16113. rc = pt_btn_probe(dev);
  16114. if (rc < 0) {
  16115. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16116. __func__);
  16117. goto error_after_startup_mt;
  16118. }
  16119. pt_probe_modules(cd);
  16120. #ifdef CONFIG_HAS_EARLYSUSPEND
  16121. pt_setup_early_suspend(cd);
  16122. #elif defined(CONFIG_DRM)
  16123. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16124. pt_setup_drm_notifier(cd);
  16125. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16126. #elif defined(CONFIG_FB)
  16127. pt_setup_fb_notifier(cd);
  16128. #endif
  16129. #ifdef NEED_SUSPEND_NOTIFIER
  16130. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16131. register_pm_notifier(&cd->pm_notifier);
  16132. #endif
  16133. pt_pip_resume_scanning_(cd);
  16134. mutex_lock(&cd->system_lock);
  16135. cd->startup_status |= status;
  16136. cd->core_probe_complete = 1;
  16137. mutex_unlock(&cd->system_lock);
  16138. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16139. __func__);
  16140. return 0;
  16141. error_after_startup_mt:
  16142. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16143. pt_mt_release(dev);
  16144. error_after_sysfs_create:
  16145. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16146. pm_runtime_disable(dev);
  16147. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16148. device_wakeup_disable(dev);
  16149. #endif
  16150. device_init_wakeup(dev, 0);
  16151. cancel_work_sync(&cd->enum_work);
  16152. pt_stop_wd_timer(cd);
  16153. pt_free_si_ptrs(cd);
  16154. remove_sysfs_interfaces(dev);
  16155. error_after_startup:
  16156. pr_err("%s PARADE error_after_startup\n", __func__);
  16157. del_timer(&cd->watchdog_timer);
  16158. if (cd->cpdata->setup_irq)
  16159. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16160. error_setup_irq:
  16161. error_detect:
  16162. if (cd->cpdata->init)
  16163. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16164. if (cd->cpdata->setup_power)
  16165. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16166. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16167. error_enable_regulator:
  16168. pt_del_core(dev);
  16169. dev_set_drvdata(dev, NULL);
  16170. pt_enable_regulator(cd, false);
  16171. error_get_regulator:
  16172. pt_get_regulator(cd, false);
  16173. error_alloc_data:
  16174. kfree(cd);
  16175. error_no_pdata:
  16176. pr_err("%s failed.\n", __func__);
  16177. return rc;
  16178. }
  16179. EXPORT_SYMBOL_GPL(pt_probe);
  16180. /*******************************************************************************
  16181. * FUNCTION: pt_release
  16182. *
  16183. * SUMMARY: This function does the following cleanup:
  16184. * - Releases all probed modules
  16185. * - Stops the watchdog
  16186. * - Cancels all pending work tasks
  16187. * - Removes all created sysfs nodes
  16188. * - Removes all created debugfs nodes
  16189. * - Frees the IRQ
  16190. * - Deletes the core
  16191. * - Frees all pointers and HID reports
  16192. *
  16193. * RETURN:
  16194. * 0 = success
  16195. * !0 = failure
  16196. *
  16197. * PARAMETERS:
  16198. * *cd - pointer to the core data structure
  16199. ******************************************************************************/
  16200. int pt_release(struct pt_core_data *cd)
  16201. {
  16202. struct device *dev = cd->dev;
  16203. /*
  16204. * Suspend the device before freeing the startup_work and stopping
  16205. * the watchdog since sleep function restarts watchdog on failure
  16206. */
  16207. pm_runtime_suspend(dev);
  16208. pm_runtime_disable(dev);
  16209. /*
  16210. * Any 'work' that can trigger a new thread should be canceled first.
  16211. * The watchdog is also stopped again because another thread could have
  16212. * restarted it. The irq_work is cancelled last because it is used for
  16213. * all I2C/SPI communication.
  16214. */
  16215. pt_stop_wd_timer(cd);
  16216. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16217. cancel_work_sync(&cd->ttdl_restart_work);
  16218. cancel_work_sync(&cd->enum_work);
  16219. pt_stop_wd_timer(cd);
  16220. pt_release_modules(cd);
  16221. pt_proximity_release(dev);
  16222. pt_btn_release(dev);
  16223. pt_mt_release(dev);
  16224. #ifdef CONFIG_HAS_EARLYSUSPEND
  16225. unregister_early_suspend(&cd->es);
  16226. #elif defined(CONFIG_DRM)
  16227. if (active_panel)
  16228. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16229. #elif defined(CONFIG_FB)
  16230. fb_unregister_client(&cd->fb_notifier);
  16231. #endif
  16232. #ifdef NEED_SUSPEND_NOTIFIER
  16233. unregister_pm_notifier(&cd->pm_notifier);
  16234. #endif
  16235. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16236. device_wakeup_disable(dev);
  16237. #endif
  16238. device_init_wakeup(dev, 0);
  16239. #ifdef TTHE_TUNER_SUPPORT
  16240. mutex_lock(&cd->tthe_lock);
  16241. cd->tthe_exit = 1;
  16242. wake_up(&cd->wait_q);
  16243. mutex_unlock(&cd->tthe_lock);
  16244. debugfs_remove(cd->tthe_debugfs);
  16245. #endif
  16246. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16247. remove_sysfs_interfaces(dev);
  16248. disable_irq_nosync(cd->irq);
  16249. if (cd->cpdata->setup_irq)
  16250. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16251. if (cd->cpdata->init)
  16252. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16253. if (cd->cpdata->setup_power)
  16254. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16255. dev_set_drvdata(dev, NULL);
  16256. pt_del_core(dev);
  16257. pt_enable_regulator(cd, false);
  16258. pt_get_regulator(cd, false);
  16259. pt_free_si_ptrs(cd);
  16260. kfree(cd);
  16261. return 0;
  16262. }
  16263. EXPORT_SYMBOL_GPL(pt_release);
  16264. MODULE_LICENSE("GPL");
  16265. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16266. MODULE_AUTHOR("Parade Technologies <[email protected]>");