pt_core.c 518 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include <linux/suspend.h>
  33. #include "pt_regs.h"
  34. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  35. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  36. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  37. #define FT_VTG_MIN_UV 2800000
  38. #define FT_VTG_MAX_UV 2800000
  39. #define FT_I2C_VTG_MIN_UV 1800000
  40. #define FT_I2C_VTG_MAX_UV 1800000
  41. #define PWR_SUSPEND_LOAD_UA 165
  42. #define I2C_SUSPEND_LOAD_UA 100
  43. #define PWR_ACTIVE_LOAD_MA 12000
  44. #define I2C_ACTIVE_LOAD_MA 30000
  45. #define PT_CORE_STARTUP_RETRY_COUNT 3
  46. #define PT_STATUS_STR_LEN (50)
  47. #if defined(CONFIG_DRM)
  48. static struct drm_panel *active_panel;
  49. #endif
  50. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  51. #define ENABLE_I2C_REG_ONLY
  52. #ifdef ENABLE_I2C_REG_ONLY
  53. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  54. #endif
  55. static const char *pt_driver_core_name = PT_CORE_NAME;
  56. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  57. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  58. struct pt_hid_field {
  59. int report_count;
  60. int report_size;
  61. int size; /* report_count * report_size */
  62. int offset;
  63. int data_type;
  64. int logical_min;
  65. int logical_max;
  66. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  67. u32 usage_page;
  68. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  69. struct pt_hid_report *report;
  70. bool record_field;
  71. };
  72. struct pt_hid_report {
  73. u8 id;
  74. u8 type;
  75. int size;
  76. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  77. int num_fields;
  78. int record_field_index;
  79. int header_size;
  80. int record_size;
  81. u32 usage_page;
  82. };
  83. struct atten_node {
  84. struct list_head node;
  85. char *id;
  86. struct device *dev;
  87. int (*func)(struct device *dev);
  88. int mode;
  89. };
  90. struct param_node {
  91. struct list_head node;
  92. u8 id;
  93. u32 value;
  94. u8 size;
  95. };
  96. struct module_node {
  97. struct list_head node;
  98. struct pt_module *module;
  99. void *data;
  100. };
  101. struct pt_hid_cmd {
  102. u8 opcode;
  103. u8 report_type;
  104. union {
  105. u8 report_id;
  106. u8 power_state;
  107. };
  108. u8 has_data_register;
  109. size_t write_length;
  110. u8 *write_buf;
  111. u8 *read_buf;
  112. u8 wait_interrupt;
  113. u8 reset_cmd;
  114. u16 timeout_ms;
  115. };
  116. struct pt_hid_output {
  117. u8 cmd_type;
  118. u16 length;
  119. u8 command_code;
  120. size_t write_length;
  121. u8 *write_buf;
  122. u8 novalidate;
  123. u8 reset_expected;
  124. u16 timeout_ms;
  125. };
  126. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  127. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  128. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  129. #define CREATE_PIP1_FW_CMD(command) \
  130. .cmd_type = PIP1_CMD_TYPE_FW, \
  131. .command_code = command
  132. #define CREATE_PIP1_BL_CMD(command) \
  133. .cmd_type = PIP1_CMD_TYPE_BL, \
  134. .command_code = command
  135. #define PT_MAX_PR_BUF_SIZE 2048
  136. /*******************************************************************************
  137. * FUNCTION: pt_pr_buf
  138. *
  139. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  140. *
  141. * RETURN: Void
  142. *
  143. * PARAMETERS:
  144. * *dev - pointer to Device structure
  145. * debug_level - requested debug level to print at
  146. * *buf - pointer to buffer to print
  147. * buf_len - size of buf
  148. * *data_name - Descriptive name of data prefixed to data
  149. ******************************************************************************/
  150. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  151. u16 buf_len, const char *data_name)
  152. {
  153. struct pt_core_data *cd = dev_get_drvdata(dev);
  154. int i;
  155. ssize_t pr_buf_index = 0;
  156. int max_size;
  157. /* only proceed if valid debug level and there is data to print */
  158. if (debug_level <= cd->debug_level && buf_len > 0) {
  159. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  160. if (!pr_buf)
  161. return;
  162. /*
  163. * With a space each printed char takes 3 bytes, subtract
  164. * the length of the data_name prefix as well as 11 bytes
  165. * for the " [0..xxx]: " printed before the data.
  166. */
  167. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  168. /* Ensure pr_buf_index stays within the 1018 size */
  169. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  170. data_name);
  171. for (i = 0; i < buf_len && i < max_size; i++)
  172. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  173. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  174. pt_debug(dev, debug_level, "%s\n", pr_buf);
  175. kfree(pr_buf);
  176. }
  177. }
  178. EXPORT_SYMBOL_GPL(pt_pr_buf);
  179. #ifdef TTHE_TUNER_SUPPORT
  180. /*******************************************************************************
  181. * FUNCTION: tthe_print
  182. *
  183. * SUMMARY: Format data name and time stamp as the header and format the
  184. * content of input buffer with hex base to "tthe_buf". And then wake up event
  185. * semaphore for tthe debugfs node.
  186. *
  187. * RETURN:
  188. * 0 = success
  189. * !0 = failure
  190. *
  191. * PARAMETERS:
  192. * *cd - pointer to core data
  193. * *buf - pointer to input buffer
  194. * buf_len - size of input buffer
  195. * *data_name - pointer to data name
  196. ******************************************************************************/
  197. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  198. const u8 *data_name)
  199. {
  200. int name_len = strlen(data_name);
  201. int i, n;
  202. u8 *p;
  203. int remain;
  204. u8 data_name_with_time_stamp[100];
  205. /* Prepend timestamp, if requested, to data_name */
  206. if (cd->show_timestamp) {
  207. scnprintf(data_name_with_time_stamp,
  208. sizeof(data_name_with_time_stamp),
  209. "[%u] %s", pt_get_time_stamp(), data_name);
  210. data_name = data_name_with_time_stamp;
  211. name_len = strlen(data_name);
  212. }
  213. mutex_lock(&cd->tthe_lock);
  214. if (!cd->tthe_buf)
  215. goto exit;
  216. /* Add 1 due to the '\n' that is appended at the end */
  217. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  218. goto exit;
  219. if (name_len + buf_len == 0)
  220. goto exit;
  221. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  222. if (remain < name_len)
  223. name_len = remain;
  224. p = cd->tthe_buf + cd->tthe_buf_len;
  225. memcpy(p, data_name, name_len);
  226. cd->tthe_buf_len += name_len;
  227. p += name_len;
  228. remain -= name_len;
  229. *p = 0;
  230. for (i = 0; i < buf_len; i++) {
  231. n = scnprintf(p, remain, "%02X ", buf[i]);
  232. if (n <= 0)
  233. break;
  234. p += n;
  235. remain -= n;
  236. cd->tthe_buf_len += n;
  237. }
  238. n = scnprintf(p, remain, "\n");
  239. cd->tthe_buf_len += n;
  240. exit:
  241. wake_up(&cd->wait_q);
  242. mutex_unlock(&cd->tthe_lock);
  243. return 0;
  244. }
  245. /*******************************************************************************
  246. * FUNCTION: _pt_request_tthe_print
  247. *
  248. * SUMMARY: Function pointer included in core_cmds to allow other modules
  249. * to request to print data to the "tthe_buffer".
  250. *
  251. * RETURN:
  252. * 0 = success
  253. * !0 = failure
  254. *
  255. * PARAMETERS:
  256. * *dev - pointer to device structure
  257. ******************************************************************************/
  258. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  259. int buf_len, const u8 *data_name)
  260. {
  261. struct pt_core_data *cd = dev_get_drvdata(dev);
  262. return tthe_print(cd, buf, buf_len, data_name);
  263. }
  264. #endif
  265. /*******************************************************************************
  266. * FUNCTION: pt_platform_detect_read
  267. *
  268. * SUMMARY: To be passed to platform dectect function to perform a read
  269. * operation.
  270. *
  271. * RETURN:
  272. * 0 = success
  273. * !0 = failure
  274. *
  275. * PARAMETERS:
  276. * *dev - pointer to Device structure
  277. * *buf - pointer to buffer where the data read will be stored
  278. * size - size to be read
  279. ******************************************************************************/
  280. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  281. {
  282. struct pt_core_data *cd = dev_get_drvdata(dev);
  283. return pt_adap_read_default(cd, buf, size);
  284. }
  285. /*******************************************************************************
  286. * FUNCTION: pt_add_parameter
  287. *
  288. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  289. * On every reset of the DUT this linked list is traversed and all
  290. * parameters in it are restored to the DUT.
  291. *
  292. * RETURN:
  293. * 0 = success
  294. * !0 = failure
  295. *
  296. * PARAMETERS:
  297. * *cd - pointer to core data
  298. * param_id - parameter ID to add
  299. * param_value - Value corresponding to the ID
  300. * param_size - Size of param_value
  301. ******************************************************************************/
  302. static int pt_add_parameter(struct pt_core_data *cd,
  303. u8 param_id, u32 param_value, u8 param_size)
  304. {
  305. struct param_node *param, *param_new;
  306. /* Check if parameter already exists in the list */
  307. spin_lock(&cd->spinlock);
  308. list_for_each_entry(param, &cd->param_list, node) {
  309. if (param->id == param_id) {
  310. /* Update parameter */
  311. param->value = param_value;
  312. pt_debug(cd->dev, DL_INFO,
  313. "%s: Update parameter id:%d value:%d size:%d\n",
  314. __func__, param_id, param_value, param_size);
  315. goto exit_unlock;
  316. }
  317. }
  318. spin_unlock(&cd->spinlock);
  319. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  320. if (!param_new)
  321. return -ENOMEM;
  322. param_new->id = param_id;
  323. param_new->value = param_value;
  324. param_new->size = param_size;
  325. pt_debug(cd->dev, DL_INFO,
  326. "%s: Add parameter id:%d value:%d size:%d\n",
  327. __func__, param_id, param_value, param_size);
  328. spin_lock(&cd->spinlock);
  329. list_add(&param_new->node, &cd->param_list);
  330. exit_unlock:
  331. spin_unlock(&cd->spinlock);
  332. return 0;
  333. }
  334. #ifdef TTDL_DIAGNOSTICS
  335. /*******************************************************************************
  336. * FUNCTION: pt_erase_parameter_list
  337. *
  338. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  339. * pairs. In some test cases this functionality is needed to ensure DUT
  340. * returns to a virgin state after a reset and no parameters are restored.
  341. *
  342. * RETURN:
  343. * 0 = success
  344. * !0 = failure
  345. *
  346. * PARAMETERS:
  347. * *cd - pointer to core data
  348. ******************************************************************************/
  349. static int pt_erase_parameter_list(struct pt_core_data *cd)
  350. {
  351. struct param_node *pos, *temp;
  352. spin_lock(&cd->spinlock);
  353. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  354. pt_debug(cd->dev, DL_INFO,
  355. "%s: Parameter Restore List - remove 0x%02x\n",
  356. __func__, pos->id);
  357. list_del(&pos->node);
  358. kfree(pos);
  359. }
  360. spin_unlock(&cd->spinlock);
  361. return 0;
  362. }
  363. /*******************************************************************************
  364. * FUNCTION: pt_count_parameter_list
  365. *
  366. * SUMMARY: Count the items in the RAM parameter restor list
  367. *
  368. * RETURN:
  369. * 0 = success
  370. * !0 = failure
  371. *
  372. * PARAMETERS:
  373. * *cd - pointer to core data
  374. ******************************************************************************/
  375. static int pt_count_parameter_list(struct pt_core_data *cd)
  376. {
  377. struct param_node *pos, *temp;
  378. int entries = 0;
  379. spin_lock(&cd->spinlock);
  380. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  381. entries++;
  382. spin_unlock(&cd->spinlock);
  383. return entries;
  384. }
  385. #endif /* TTDL_DIAGNOSTICS */
  386. /*******************************************************************************
  387. * FUNCTION: request_exclusive
  388. *
  389. * SUMMARY: Request exclusive access to the DUT
  390. *
  391. * RETURN:
  392. * 0 = success
  393. * !0 = failure
  394. *
  395. * PARAMETERS:
  396. * *cd - pointer to core data
  397. * *ownptr - pointer to device
  398. * timeout_ms - Timeout value
  399. ******************************************************************************/
  400. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  401. int timeout_ms)
  402. {
  403. int t = msecs_to_jiffies(timeout_ms);
  404. bool with_timeout = (timeout_ms != 0);
  405. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  406. __func__, timeout_ms);
  407. mutex_lock(&cd->system_lock);
  408. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  409. cd->exclusive_dev = ownptr;
  410. goto exit;
  411. }
  412. cd->exclusive_waits++;
  413. wait:
  414. mutex_unlock(&cd->system_lock);
  415. if (with_timeout) {
  416. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  417. if (IS_TMO(t)) {
  418. pt_debug(cd->dev, DL_ERROR,
  419. "%s: tmo waiting exclusive access\n", __func__);
  420. return -ETIME;
  421. }
  422. } else {
  423. wait_event(cd->wait_q, !cd->exclusive_dev);
  424. }
  425. mutex_lock(&cd->system_lock);
  426. if (cd->exclusive_dev)
  427. goto wait;
  428. cd->exclusive_dev = ownptr;
  429. cd->exclusive_waits--;
  430. exit:
  431. mutex_unlock(&cd->system_lock);
  432. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  433. __func__, ownptr);
  434. return 0;
  435. }
  436. /*******************************************************************************
  437. * FUNCTION: release_exclusive_
  438. *
  439. * SUMMARY: Release exclusive access to the DUT
  440. *
  441. * RETURN:
  442. * 0 = success
  443. *
  444. * PARAMETERS:
  445. * *cd - pointer to core data
  446. * *ownptr - pointer to device
  447. ******************************************************************************/
  448. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  449. {
  450. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  451. __func__);
  452. if (cd->exclusive_dev != ownptr)
  453. return -EINVAL;
  454. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  455. __func__, cd->exclusive_dev);
  456. cd->exclusive_dev = NULL;
  457. wake_up(&cd->wait_q);
  458. return 0;
  459. }
  460. /*******************************************************************************
  461. * FUNCTION: release_exclusive
  462. *
  463. * SUMMARY: Protected wrapper to release_exclusive_()
  464. *
  465. * RETURN:
  466. * 0 = success
  467. * !0 = failure
  468. *
  469. * PARAMETERS:
  470. * *cd - pointer to core data
  471. * *ownptr - pointer to device
  472. ******************************************************************************/
  473. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  474. {
  475. int rc;
  476. mutex_lock(&cd->system_lock);
  477. rc = release_exclusive_(cd, ownptr);
  478. mutex_unlock(&cd->system_lock);
  479. return rc;
  480. }
  481. /*******************************************************************************
  482. * FUNCTION: pt_hid_exec_cmd_
  483. *
  484. * SUMMARY: Send the HID command to the DUT
  485. *
  486. * RETURN:
  487. * 0 = success
  488. * !0 = failure
  489. *
  490. * PARAMETERS:
  491. * *cd - pointer to core data
  492. * *hid_cmd - pointer to the HID command to send
  493. ******************************************************************************/
  494. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  495. struct pt_hid_cmd *hid_cmd)
  496. {
  497. int rc = 0;
  498. u8 *cmd;
  499. u16 cmd_length;
  500. u8 cmd_offset = 0;
  501. cmd_length = 2 /* command register */
  502. + 2 /* command */
  503. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  504. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  505. + hid_cmd->write_length; /* Data length */
  506. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  507. return -EPROTO;
  508. cmd = kzalloc(cmd_length, GFP_KERNEL);
  509. if (!cmd)
  510. return -ENOMEM;
  511. /* Set Command register */
  512. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  513. sizeof(cd->hid_desc.command_register));
  514. cmd_offset += sizeof(cd->hid_desc.command_register);
  515. /* Set Command */
  516. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  517. if (hid_cmd->report_id >= 0XF)
  518. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  519. else
  520. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  521. cmd_offset++;
  522. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  523. cmd_offset++;
  524. if (hid_cmd->report_id >= 0XF) {
  525. cmd[cmd_offset] = hid_cmd->report_id;
  526. cmd_offset++;
  527. }
  528. /* Set Data register */
  529. if (hid_cmd->has_data_register) {
  530. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  531. sizeof(cd->hid_desc.data_register));
  532. cmd_offset += sizeof(cd->hid_desc.data_register);
  533. }
  534. /* Set Data */
  535. if (hid_cmd->write_length && hid_cmd->write_buf) {
  536. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  537. hid_cmd->write_length);
  538. cmd_offset += hid_cmd->write_length;
  539. }
  540. pt_debug(cd->dev, DL_INFO,
  541. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  542. __func__, cmd_length, hid_cmd->report_id);
  543. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  544. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  545. hid_cmd->read_buf);
  546. if (rc)
  547. pt_debug(cd->dev, DL_ERROR,
  548. "%s: Fail pt_adap_transfer\n", __func__);
  549. kfree(cmd);
  550. return rc;
  551. }
  552. #ifdef TTDL_DIAGNOSTICS
  553. /*******************************************************************************
  554. * FUNCTION: pt_toggle_err_gpio
  555. *
  556. * SUMMARY: Toggles the pre-defined error GPIO
  557. *
  558. * RETURN: n/a
  559. *
  560. * PARAMETERS:
  561. * *cd - pointer to core data
  562. * type - type of err that occurred
  563. ******************************************************************************/
  564. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  565. {
  566. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  567. __func__, type);
  568. if (cd->err_gpio && type == cd->err_gpio_type) {
  569. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  570. gpio_direction_output(cd->err_gpio,
  571. !gpio_get_value(cd->err_gpio));
  572. }
  573. }
  574. /*******************************************************************************
  575. * FUNCTION: _pt_request_toggle_err_gpio
  576. *
  577. * SUMMARY: Function pointer included in core_cmds to allow other modules
  578. * to request to toggle the err_gpio
  579. *
  580. * RETURN: n/a
  581. *
  582. * PARAMETERS:
  583. * *cd - pointer to core data
  584. * type - type of err that occurred
  585. ******************************************************************************/
  586. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  587. {
  588. struct pt_core_data *cd = dev_get_drvdata(dev);
  589. pt_toggle_err_gpio(cd, type);
  590. }
  591. #endif /* TTDL_DIAGNOSTICS */
  592. /*******************************************************************************
  593. * FUNCTION: pt_hid_exec_cmd_and_wait_
  594. *
  595. * SUMMARY: Send the HID command to the DUT and wait for the response
  596. *
  597. * RETURN:
  598. * 0 = success
  599. * !0 = failure
  600. *
  601. * PARAMETERS:
  602. * *cd - pointer to core data
  603. * *hid_cmd - pointer to the HID command to send
  604. ******************************************************************************/
  605. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  606. struct pt_hid_cmd *hid_cmd)
  607. {
  608. int rc = 0;
  609. int t;
  610. u16 timeout_ms;
  611. int *cmd_state;
  612. if (hid_cmd->reset_cmd)
  613. cmd_state = &cd->hid_reset_cmd_state;
  614. else
  615. cmd_state = &cd->hid_cmd_state;
  616. if (hid_cmd->wait_interrupt) {
  617. mutex_lock(&cd->system_lock);
  618. *cmd_state = 1;
  619. mutex_unlock(&cd->system_lock);
  620. }
  621. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  622. if (rc) {
  623. if (hid_cmd->wait_interrupt)
  624. goto error;
  625. goto exit;
  626. }
  627. if (!hid_cmd->wait_interrupt)
  628. goto exit;
  629. if (hid_cmd->timeout_ms)
  630. timeout_ms = hid_cmd->timeout_ms;
  631. else
  632. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  633. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  634. msecs_to_jiffies(timeout_ms));
  635. if (IS_TMO(t)) {
  636. #ifdef TTDL_DIAGNOSTICS
  637. cd->bus_transmit_error_count++;
  638. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  639. #endif /* TTDL_DIAGNOSTICS */
  640. pt_debug(cd->dev, DL_ERROR,
  641. "%s: HID output cmd execution timed out\n",
  642. __func__);
  643. rc = -ETIME;
  644. goto error;
  645. }
  646. goto exit;
  647. error:
  648. mutex_lock(&cd->system_lock);
  649. *cmd_state = 0;
  650. mutex_unlock(&cd->system_lock);
  651. exit:
  652. return rc;
  653. }
  654. /*******************************************************************************
  655. * FUNCTION: pt_hid_cmd_reset_
  656. *
  657. * SUMMARY: Send the HID RESET command to the DUT
  658. *
  659. * RETURN:
  660. * 0 = success
  661. * !0 = failure
  662. *
  663. * PARAMETERS:
  664. * *cd - pointer to core data
  665. ******************************************************************************/
  666. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  667. {
  668. struct pt_hid_cmd hid_cmd = {
  669. .opcode = HID_CMD_RESET,
  670. .wait_interrupt = 1,
  671. .reset_cmd = 1,
  672. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  673. };
  674. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  675. }
  676. /*******************************************************************************
  677. * FUNCTION: pt_hid_cmd_reset
  678. *
  679. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  680. * access.
  681. *
  682. * RETURN:
  683. * 0 = success
  684. * !0 = failure
  685. *
  686. * PARAMETERS:
  687. * *cd - pointer to core data
  688. ******************************************************************************/
  689. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  690. {
  691. int rc = 0;
  692. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  693. if (rc < 0) {
  694. pt_debug(cd->dev, DL_ERROR,
  695. "%s: fail get exclusive ex=%p own=%p\n",
  696. __func__, cd->exclusive_dev, cd->dev);
  697. return rc;
  698. }
  699. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  700. rc = pt_hid_cmd_reset_(cd);
  701. if (release_exclusive(cd, cd->dev) < 0)
  702. pt_debug(cd->dev, DL_ERROR,
  703. "%s: fail to release exclusive\n", __func__);
  704. return rc;
  705. }
  706. /*******************************************************************************
  707. * FUNCTION: pt_hid_cmd_set_power_
  708. *
  709. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  710. *
  711. * RETURN:
  712. * 0 = success
  713. * !0 = failure
  714. *
  715. * PARAMETERS:
  716. * *cd - pointer to core data
  717. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  718. ******************************************************************************/
  719. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  720. u8 power_state)
  721. {
  722. int rc = 0;
  723. struct pt_hid_cmd hid_cmd = {
  724. .opcode = HID_CMD_SET_POWER,
  725. .wait_interrupt = 1,
  726. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  727. };
  728. hid_cmd.power_state = power_state;
  729. /* The chip won't give response if goes to Deep Standby */
  730. if (power_state == HID_POWER_STANDBY) {
  731. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  732. if (rc)
  733. pt_debug(cd->dev, DL_ERROR,
  734. "%s: Failed to set power to state:%d\n",
  735. __func__, power_state);
  736. else
  737. cd->fw_sys_mode_in_standby_state = true;
  738. return rc;
  739. }
  740. cd->fw_sys_mode_in_standby_state = false;
  741. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  742. if (rc) {
  743. pt_debug(cd->dev, DL_ERROR,
  744. "%s: Failed to set power to state:%d\n",
  745. __func__, power_state);
  746. return rc;
  747. }
  748. /* validate */
  749. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  750. || ((cd->response_buf[3] & 0x3) != power_state)
  751. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  752. rc = -EINVAL;
  753. return rc;
  754. }
  755. /*******************************************************************************
  756. * FUNCTION: pt_hid_cmd_set_power
  757. *
  758. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  759. * exclusive access.
  760. *
  761. * RETURN:
  762. * 0 = success
  763. * !0 = failure
  764. *
  765. * PARAMETERS:
  766. * *cd - pointer to core data
  767. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  768. ******************************************************************************/
  769. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  770. u8 power_state)
  771. {
  772. int rc;
  773. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  774. if (rc < 0) {
  775. pt_debug(cd->dev, DL_ERROR,
  776. "%s: fail get exclusive ex=%p own=%p\n",
  777. __func__, cd->exclusive_dev, cd->dev);
  778. return rc;
  779. }
  780. rc = pt_hid_cmd_set_power_(cd, power_state);
  781. if (release_exclusive(cd, cd->dev) < 0)
  782. pt_debug(cd->dev, DL_ERROR,
  783. "%s: fail to release exclusive\n", __func__);
  784. return rc;
  785. }
  786. static const u16 crc_table[16] = {
  787. 0x0000, 0x1021, 0x2042, 0x3063,
  788. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  789. 0x8108, 0x9129, 0xa14a, 0xb16b,
  790. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  791. };
  792. /*******************************************************************************
  793. * FUNCTION: _pt_compute_crc
  794. *
  795. * SUMMARY: Calculate CRC by CRC table.
  796. *
  797. * RETURN:
  798. * CRC calculation result
  799. *
  800. * PARAMETERS:
  801. * *buf - pointer to the data array to be calculated
  802. * size - size of data array
  803. ******************************************************************************/
  804. static u16 _pt_compute_crc(u8 *buf, u32 size)
  805. {
  806. u16 remainder = 0xFFFF;
  807. u16 xor_mask = 0x0000;
  808. u32 index;
  809. u32 byte_value;
  810. u32 table_index;
  811. u32 crc_bit_width = sizeof(u16) * 8;
  812. /* Divide the message by polynomial, via the table. */
  813. for (index = 0; index < size; index++) {
  814. byte_value = buf[index];
  815. table_index = ((byte_value >> 4) & 0x0F)
  816. ^ (remainder >> (crc_bit_width - 4));
  817. remainder = crc_table[table_index] ^ (remainder << 4);
  818. table_index = (byte_value & 0x0F)
  819. ^ (remainder >> (crc_bit_width - 4));
  820. remainder = crc_table[table_index] ^ (remainder << 4);
  821. }
  822. /* Perform the final remainder CRC. */
  823. return remainder ^ xor_mask;
  824. }
  825. u16 ccitt_Table[] = {
  826. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  827. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  828. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  829. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  830. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  831. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  832. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  833. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  834. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  835. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  836. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  837. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  838. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  839. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  840. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  841. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  842. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  843. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  844. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  845. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  846. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  847. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  848. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  849. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  850. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  851. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  852. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  853. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  854. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  855. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  856. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  857. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  858. };
  859. /*******************************************************************************
  860. * FUNCTION: crc_ccitt_calculate
  861. *
  862. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  863. *
  864. * RETURN:
  865. * CRC calculation result
  866. *
  867. * PARAMETERS:
  868. * *q - pointer to the data array to be calculated
  869. * len - size of data array
  870. ******************************************************************************/
  871. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  872. {
  873. unsigned short crc = 0xffff;
  874. while (len-- > 0)
  875. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  876. return crc;
  877. }
  878. /*******************************************************************************
  879. * FUNCTION: pt_pip2_cmd_calculate_crc
  880. *
  881. * SUMMARY: Calculate the CRC of a command packet
  882. *
  883. * RETURN: void
  884. *
  885. * PARAMETERS:
  886. * *cmd - pointer to command data
  887. * extra_bytes - Extra bytes included in command length
  888. ******************************************************************************/
  889. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  890. u8 extra_bytes)
  891. {
  892. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  893. unsigned short crc;
  894. buf[0] = cmd->len & 0xff;
  895. buf[1] = (cmd->len & 0xff00) >> 8;
  896. buf[2] = cmd->seq;
  897. buf[3] = cmd->id;
  898. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  899. /* Calculate the CRC for the first 4 bytes above and the data payload */
  900. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  901. cmd->crc[0] = (crc & 0xff00) >> 8;
  902. cmd->crc[1] = (crc & 0xff);
  903. }
  904. /*******************************************************************************
  905. * FUNCTION: pt_pip2_get_next_cmd_seq
  906. *
  907. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  908. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  909. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  910. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  911. * increment, just use the forced value.
  912. *
  913. * RETURN: Next command sequence number [0x08-0x0F]
  914. *
  915. * PARAMETERS:
  916. * *cd - pointer to core data
  917. ******************************************************************************/
  918. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  919. {
  920. #ifdef TTDL_DIAGNOSTICS
  921. if (cd->force_pip2_seq <= 0x07) {
  922. cd->pip2_cmd_tag_seq++;
  923. if (cd->pip2_cmd_tag_seq > 0x0F)
  924. cd->pip2_cmd_tag_seq = 0x08;
  925. } else {
  926. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  927. }
  928. #else
  929. cd->pip2_cmd_tag_seq++;
  930. if (cd->pip2_cmd_tag_seq > 0x0F)
  931. cd->pip2_cmd_tag_seq = 0x08;
  932. #endif
  933. return cd->pip2_cmd_tag_seq;
  934. }
  935. /*
  936. * Following macros are to define the response time (the interval between PIP2
  937. * command finishes sending and INT pin falls). The unit is in microsecond.
  938. * It has different time settings between the solution GPIO polling and Bus
  939. * polling due to the considration for system load.
  940. */
  941. #ifdef PT_POLL_RESP_BY_BUS
  942. #define POLL_RETRY_DEFAULT_INTERVAL 50
  943. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  944. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  945. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  946. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  947. #else
  948. #define POLL_RETRY_DEFAULT_INTERVAL 20
  949. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  950. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  951. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  952. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  953. #endif
  954. /*
  955. * id: the command id defined in PIP2
  956. * response_len: the (maximum) length of response.
  957. * response_time_min: minimum response time in microsecond
  958. * response_time_max: maximum response time in microsecond
  959. */
  960. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  961. {.id = PIP2_CMD_ID_PING,
  962. .response_len = 255,
  963. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  964. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  965. {.id = PIP2_CMD_ID_STATUS,
  966. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  967. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  968. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  969. {.id = PIP2_CMD_ID_CTRL,
  970. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  971. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  972. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  973. {.id = PIP2_CMD_ID_CONFIG,
  974. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  975. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  976. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  977. {.id = PIP2_CMD_ID_CLEAR,
  978. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  979. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  980. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  981. {.id = PIP2_CMD_ID_RESET,
  982. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  983. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  984. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  985. {.id = PIP2_CMD_ID_VERSION,
  986. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  987. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  988. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  989. {.id = PIP2_CMD_ID_FILE_OPEN,
  990. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  991. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  992. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  993. {.id = PIP2_CMD_ID_FILE_CLOSE,
  994. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  995. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  996. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  997. {.id = PIP2_CMD_ID_FILE_READ,
  998. .response_len = 255,
  999. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1000. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1001. {.id = PIP2_CMD_ID_FILE_WRITE,
  1002. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1003. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1004. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1005. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1006. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1007. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1008. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1009. {.id = PIP2_CMD_ID_FLASH_INFO,
  1010. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1011. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1012. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1013. {.id = PIP2_CMD_ID_EXECUTE,
  1014. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1015. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1016. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1017. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1018. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1019. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1020. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1021. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1022. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1023. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1024. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1025. {.id = PIP2_CMD_ID_READ_GPIO,
  1026. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1027. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1028. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1029. {.id = PIP2_CMD_EXECUTE_SCAN,
  1030. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1031. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1032. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1033. {.id = PIP2_CMD_SET_PARAMETER,
  1034. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1035. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1036. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1037. {.id = PIP2_CMD_GET_PARAMETER,
  1038. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1039. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1040. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1041. {.id = PIP2_CMD_SET_DDI_REG,
  1042. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1043. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1044. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1045. {.id = PIP2_CMD_GET_DDI_REG,
  1046. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1047. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1048. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1049. {.id = PIP2_CMD_ID_END,
  1050. .response_len = 255,
  1051. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1052. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1053. };
  1054. /*******************************************************************************
  1055. * FUNCTION: pt_pip2_get_cmd_response_len
  1056. *
  1057. * SUMMARY: Gets the expected response length based on the command ID
  1058. *
  1059. * RETURN: Expected response length
  1060. *
  1061. * PARAMETERS:
  1062. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1063. ******************************************************************************/
  1064. static int pt_pip2_get_cmd_response_len(u8 id)
  1065. {
  1066. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1067. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1068. p++;
  1069. if (p->id != PIP2_CMD_ID_END)
  1070. return p->response_len;
  1071. else
  1072. return -EPERM;
  1073. }
  1074. /*******************************************************************************
  1075. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1076. *
  1077. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1078. * finishes sending and INT pin falls) based on the command ID
  1079. *
  1080. * RETURN: Estimated minimum response time in microsecond
  1081. *
  1082. * PARAMETERS:
  1083. * id - Command ID
  1084. ******************************************************************************/
  1085. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1086. {
  1087. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1088. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1089. p++;
  1090. if (p->id != PIP2_CMD_ID_END)
  1091. return p->response_time_min;
  1092. else
  1093. return PIP2_RESP_DEFAULT_TIME_MIN;
  1094. }
  1095. /*******************************************************************************
  1096. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1097. *
  1098. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1099. * finishes sending and INT pin falls) based on the command ID
  1100. *
  1101. * RETURN: Estimated maximum response time in microsecond
  1102. *
  1103. * PARAMETERS:
  1104. * id - Command ID
  1105. ******************************************************************************/
  1106. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1107. {
  1108. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1109. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1110. p++;
  1111. if (p->id != PIP2_CMD_ID_END)
  1112. return p->response_time_max;
  1113. else
  1114. return PIP2_RESP_DEFAULT_TIME_MAX;
  1115. }
  1116. /*******************************************************************************
  1117. * FUNCTION: pt_pip2_validate_response
  1118. *
  1119. * SUMMARY: Validate the response of PIP2 command.
  1120. *
  1121. * RETURN:
  1122. * 0 = success
  1123. * !0 = failure
  1124. *
  1125. * PARAMETERS:
  1126. * *cd - pointer to core data
  1127. * *pip2_cmd - pointer to PIP2 command to send
  1128. * *read_buf - pointer to response buffer
  1129. * actual_read_len - actual read length of the response
  1130. ******************************************************************************/
  1131. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1132. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1133. u16 actual_read_len)
  1134. {
  1135. int rc = 0;
  1136. u8 response_seq = 0;
  1137. u8 reserved_bits = 0;
  1138. u8 cmd_id = 0;
  1139. u8 response_bit = 0;
  1140. unsigned short calc_crc = 0;
  1141. unsigned short resp_crc = 0;
  1142. /* Verify the length of response buffer */
  1143. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1144. pt_debug(cd->dev, DL_ERROR,
  1145. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1146. __func__, pip2_cmd->id, actual_read_len);
  1147. rc = -EINVAL;
  1148. goto exit;
  1149. }
  1150. /* Verify the CRC */
  1151. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1152. resp_crc = read_buf[actual_read_len - 2] << 8;
  1153. resp_crc |= read_buf[actual_read_len - 1];
  1154. if (resp_crc != calc_crc) {
  1155. pt_debug(cd->dev, DL_ERROR,
  1156. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1157. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1158. #ifdef TTDL_DIAGNOSTICS
  1159. cd->pip2_crc_error_count++;
  1160. #endif /* TTDL_DIAGNOSTICS */
  1161. rc = -EINVAL;
  1162. goto exit;
  1163. }
  1164. /* Verify the response bit is set */
  1165. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1166. if (!response_bit) {
  1167. pt_debug(cd->dev, DL_ERROR,
  1168. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1169. __func__, pip2_cmd->id, response_bit);
  1170. rc = -EINVAL;
  1171. goto exit;
  1172. }
  1173. /* Verify the command ID matches from command to response */
  1174. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1175. if (cmd_id != pip2_cmd->id) {
  1176. pt_debug(cd->dev, DL_ERROR,
  1177. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1178. __func__, pip2_cmd->id, cmd_id);
  1179. rc = -EINVAL;
  1180. goto exit;
  1181. }
  1182. /* Verify the SEQ number matches from command to response */
  1183. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1184. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1185. pt_debug(cd->dev, DL_ERROR,
  1186. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1187. __func__, pip2_cmd->id,
  1188. pip2_cmd->seq, response_seq);
  1189. rc = -EINVAL;
  1190. goto exit;
  1191. }
  1192. /* Verify the reserved bits are 0 */
  1193. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1194. if (reserved_bits)
  1195. pt_debug(cd->dev, DL_WARN,
  1196. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1197. __func__, pip2_cmd->id, reserved_bits);
  1198. exit:
  1199. if (rc)
  1200. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1201. "PIP RSP:");
  1202. return rc;
  1203. }
  1204. /*******************************************************************************
  1205. * FUNCTION: pt_hid_output_validate_bl_response
  1206. *
  1207. * SUMMARY: Validate the response of bootloader command.
  1208. *
  1209. * RETURN:
  1210. * 0 = success
  1211. * !0 = failure
  1212. *
  1213. * PARAMETERS:
  1214. * *cd - pointer to core data
  1215. * *hid_output - pointer to hid output data structure
  1216. ******************************************************************************/
  1217. static int pt_hid_output_validate_bl_response(
  1218. struct pt_core_data *cd,
  1219. struct pt_hid_output *hid_output)
  1220. {
  1221. u16 size;
  1222. u16 crc;
  1223. u8 status;
  1224. size = get_unaligned_le16(&cd->response_buf[0]);
  1225. if (hid_output->reset_expected && !size)
  1226. return 0;
  1227. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1228. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1229. pt_debug(cd->dev, DL_ERROR,
  1230. "%s: BL output response, wrong report_id\n", __func__);
  1231. return -EPROTO;
  1232. }
  1233. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1234. pt_debug(cd->dev, DL_ERROR,
  1235. "%s: BL output response, wrong SOP\n", __func__);
  1236. return -EPROTO;
  1237. }
  1238. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1239. pt_debug(cd->dev, DL_ERROR,
  1240. "%s: BL output response, wrong EOP\n", __func__);
  1241. return -EPROTO;
  1242. }
  1243. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1244. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1245. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1246. pt_debug(cd->dev, DL_ERROR,
  1247. "%s: BL output response, wrong CRC 0x%X\n",
  1248. __func__, crc);
  1249. return -EPROTO;
  1250. }
  1251. status = cd->response_buf[5];
  1252. if (status) {
  1253. pt_debug(cd->dev, DL_ERROR,
  1254. "%s: BL output response, ERROR:%d\n",
  1255. __func__, status);
  1256. return -EPROTO;
  1257. }
  1258. return 0;
  1259. }
  1260. /*******************************************************************************
  1261. * FUNCTION: pt_hid_output_validate_app_response
  1262. *
  1263. * SUMMARY: Validate the response of application command.
  1264. *
  1265. * RETURN:
  1266. * 0 = success
  1267. * !0 = failure
  1268. *
  1269. * PARAMETERS:
  1270. * *cd - pointer to core data
  1271. * *hid_output - pointer to hid output data structure
  1272. ******************************************************************************/
  1273. static int pt_hid_output_validate_app_response(
  1274. struct pt_core_data *cd,
  1275. struct pt_hid_output *hid_output)
  1276. {
  1277. int command_code;
  1278. u16 size;
  1279. size = get_unaligned_le16(&cd->response_buf[0]);
  1280. if (hid_output->reset_expected && !size)
  1281. return 0;
  1282. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1283. != PT_PIP_NON_HID_RESPONSE_ID) {
  1284. pt_debug(cd->dev, DL_ERROR,
  1285. "%s: APP output response, wrong report_id\n", __func__);
  1286. return -EPROTO;
  1287. }
  1288. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1289. & PIP1_RESP_COMMAND_ID_MASK;
  1290. if (command_code != hid_output->command_code) {
  1291. pt_debug(cd->dev, DL_ERROR,
  1292. "%s: APP output response, wrong command_code:%X\n",
  1293. __func__, command_code);
  1294. return -EPROTO;
  1295. }
  1296. return 0;
  1297. }
  1298. /*******************************************************************************
  1299. * FUNCTION: pt_check_set_parameter
  1300. *
  1301. * SUMMARY: Check command input and response for Set Parameter command.And
  1302. * store the parameter to the list for resume work if pass the check.
  1303. *
  1304. * PARAMETERS:
  1305. * *cd - pointer to core data
  1306. * *hid_output - pointer to hid output data structure
  1307. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1308. ******************************************************************************/
  1309. static void pt_check_set_parameter(struct pt_core_data *cd,
  1310. struct pt_hid_output *hid_output, bool raw)
  1311. {
  1312. u8 *param_buf;
  1313. u32 param_value = 0;
  1314. u8 param_size;
  1315. u8 param_id;
  1316. int i = 0;
  1317. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1318. return;
  1319. /* Check command input for Set Parameter command */
  1320. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1321. && !memcmp(&hid_output->write_buf[0],
  1322. &cd->hid_desc.output_register,
  1323. sizeof(cd->hid_desc.output_register))
  1324. && hid_output->write_buf[4] ==
  1325. PT_PIP_NON_HID_COMMAND_ID
  1326. && hid_output->write_buf[6] ==
  1327. PIP1_CMD_ID_SET_PARAM)
  1328. param_buf = &hid_output->write_buf[7];
  1329. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1330. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1331. && hid_output->write_length >= 3
  1332. && hid_output->write_length <= 6)
  1333. param_buf = &hid_output->write_buf[0];
  1334. else
  1335. return;
  1336. /* Get parameter ID, size and value */
  1337. param_id = param_buf[0];
  1338. param_size = param_buf[1];
  1339. if (param_size > 4) {
  1340. pt_debug(cd->dev, DL_ERROR,
  1341. "%s: Invalid parameter size\n", __func__);
  1342. return;
  1343. }
  1344. param_buf = &param_buf[2];
  1345. while (i < param_size)
  1346. param_value += *(param_buf++) << (8 * i++);
  1347. /* Check command response for Set Parameter command */
  1348. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1349. || (cd->response_buf[4] &
  1350. PIP1_RESP_COMMAND_ID_MASK) !=
  1351. PIP1_CMD_ID_SET_PARAM
  1352. || cd->response_buf[5] != param_id
  1353. || cd->response_buf[6] != param_size) {
  1354. pt_debug(cd->dev, DL_ERROR,
  1355. "%s: Set Parameter command not successful\n",
  1356. __func__);
  1357. return;
  1358. }
  1359. pt_add_parameter(cd, param_id, param_value, param_size);
  1360. }
  1361. /*******************************************************************************
  1362. * FUNCTION: pt_check_command
  1363. *
  1364. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1365. * called here to check output command and store parameter to the list.
  1366. *
  1367. * PARAMETERS:
  1368. * *cd - pointer to core data
  1369. * *hid_output - pointer to hid output data structure
  1370. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1371. ******************************************************************************/
  1372. static void pt_check_command(struct pt_core_data *cd,
  1373. struct pt_hid_output *hid_output, bool raw)
  1374. {
  1375. pt_check_set_parameter(cd, hid_output, raw);
  1376. }
  1377. /*******************************************************************************
  1378. * FUNCTION: pt_hid_output_validate_response
  1379. *
  1380. * SUMMARY: Validate the response of application or bootloader command.
  1381. *
  1382. * RETURN:
  1383. * 0 = success
  1384. * !0 = failure
  1385. *
  1386. * PARAMETERS:
  1387. * *cd - pointer to core data
  1388. * *hid_output - pointer to hid output data structure
  1389. ******************************************************************************/
  1390. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1391. struct pt_hid_output *hid_output)
  1392. {
  1393. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1394. return pt_hid_output_validate_bl_response(cd, hid_output);
  1395. return pt_hid_output_validate_app_response(cd, hid_output);
  1396. }
  1397. /*******************************************************************************
  1398. * FUNCTION: pt_hid_send_output_user_
  1399. *
  1400. * SUMMARY: Blindly send user data to the DUT.
  1401. *
  1402. * RETURN:
  1403. * 0 = success
  1404. * !0 = failure
  1405. *
  1406. * PARAMETERS:
  1407. * *cd - pointer to core data
  1408. * *hid_output - pointer to the command to send
  1409. ******************************************************************************/
  1410. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1411. struct pt_hid_output *hid_output)
  1412. {
  1413. int rc = 0;
  1414. int cmd;
  1415. if (!hid_output->length || !hid_output->write_buf)
  1416. return -EINVAL;
  1417. if (cd->pip2_prot_active) {
  1418. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1419. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1420. } else
  1421. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1422. pt_debug(cd->dev, DL_INFO,
  1423. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1424. __func__, hid_output->length, cmd);
  1425. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1426. hid_output->length, ">>> User CMD");
  1427. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1428. hid_output->write_buf, NULL);
  1429. if (rc)
  1430. pt_debug(cd->dev, DL_ERROR,
  1431. "%s: Fail pt_adap_transfer\n", __func__);
  1432. return rc;
  1433. }
  1434. /*******************************************************************************
  1435. * FUNCTION: pt_hid_send_output_user_and_wait_
  1436. *
  1437. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1438. *
  1439. * RETURN:
  1440. * 0 = success
  1441. * !0 = failure
  1442. *
  1443. * PARAMETERS:
  1444. * *cd - pointer to core data
  1445. * *hid_output - pointer to the command to send
  1446. ******************************************************************************/
  1447. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1448. struct pt_hid_output *hid_output)
  1449. {
  1450. int rc = 0;
  1451. int t;
  1452. mutex_lock(&cd->system_lock);
  1453. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1454. mutex_unlock(&cd->system_lock);
  1455. rc = pt_hid_send_output_user_(cd, hid_output);
  1456. if (rc)
  1457. goto error;
  1458. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1459. msecs_to_jiffies(cd->pip_cmd_timeout));
  1460. if (IS_TMO(t)) {
  1461. #ifdef TTDL_DIAGNOSTICS
  1462. cd->bus_transmit_error_count++;
  1463. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1464. #endif /* TTDL_DIAGNOSTICS */
  1465. pt_debug(cd->dev, DL_ERROR,
  1466. "%s: HID output cmd execution timed out\n",
  1467. __func__);
  1468. rc = -ETIME;
  1469. goto error;
  1470. }
  1471. pt_check_command(cd, hid_output, true);
  1472. goto exit;
  1473. error:
  1474. mutex_lock(&cd->system_lock);
  1475. cd->hid_cmd_state = 0;
  1476. mutex_unlock(&cd->system_lock);
  1477. exit:
  1478. return rc;
  1479. }
  1480. /*******************************************************************************
  1481. * FUNCTION: pt_check_irq_asserted
  1482. *
  1483. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1484. * the FW can hold the INT line low ~150us after the read is complete.
  1485. * NOTE: if irq_stat is not defined this function will return false
  1486. *
  1487. * RETURN:
  1488. * true = IRQ asserted
  1489. * false = IRQ not asserted
  1490. *
  1491. * PARAMETERS:
  1492. * *cd - pointer to core data
  1493. ******************************************************************************/
  1494. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1495. {
  1496. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1497. /*
  1498. * Workaround for FW defect, CDT165308
  1499. * bl_launch app creates a glitch in IRQ line
  1500. */
  1501. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1502. && cd->cpdata->irq_stat) {
  1503. /*
  1504. * in X1S panel and GC1546 panel, the width for the INT
  1505. * glitch is about 4us,the normal INT width of response
  1506. * will last more than 200us, so use 10us delay
  1507. * for distinguish the glitch the normal INT is enough.
  1508. */
  1509. udelay(10);
  1510. }
  1511. #endif
  1512. if (cd->cpdata->irq_stat) {
  1513. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1514. == PT_IRQ_ASSERTED_VALUE) {
  1515. /* Debounce to allow FW to release INT */
  1516. usleep_range(100, 200);
  1517. }
  1518. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1519. == PT_IRQ_ASSERTED_VALUE)
  1520. return true;
  1521. else
  1522. return false;
  1523. }
  1524. return true;
  1525. }
  1526. /*******************************************************************************
  1527. * FUNCTION: pt_flush_bus
  1528. *
  1529. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1530. * Used if IRQ is found to be stuck low
  1531. *
  1532. * RETURN: Length of bytes read from bus
  1533. *
  1534. * PARAMETERS:
  1535. * *cd - pointer to core data
  1536. * flush_type - type of flush
  1537. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1538. * - PT_FLUSH_BUS_FULL_256_READ
  1539. * *read_buf - pointer to store read data
  1540. ******************************************************************************/
  1541. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1542. u8 flush_type, u8 *read_buf)
  1543. {
  1544. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1545. u16 pip_len;
  1546. int bytes_read;
  1547. int rc = 0;
  1548. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1549. rc = pt_adap_read_default(cd, buf, 2);
  1550. if (rc) {
  1551. bytes_read = 0;
  1552. goto exit;
  1553. }
  1554. pip_len = get_unaligned_le16(&buf[0]);
  1555. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1556. #ifdef TTDL_DIAGNOSTICS
  1557. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1558. #endif
  1559. bytes_read = 2;
  1560. pt_debug(cd->dev, DL_INFO,
  1561. "%s: Empty buf detected - len=0x%04X\n",
  1562. __func__, pip_len);
  1563. } else if (pip_len == 0) {
  1564. bytes_read = 0;
  1565. pt_debug(cd->dev, DL_INFO,
  1566. "%s: Sentinel detected\n", __func__);
  1567. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1568. pt_debug(cd->dev, DL_ERROR,
  1569. "%s: Illegal len=0x%04x, force %d byte read\n",
  1570. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1571. rc = pt_adap_read_default(cd, buf,
  1572. PT_MAX_PIP2_MSG_SIZE);
  1573. if (!rc)
  1574. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1575. else
  1576. bytes_read = 0;
  1577. } else {
  1578. pt_debug(cd->dev, DL_INFO,
  1579. "%s: Flush read of %d bytes...\n",
  1580. __func__, pip_len);
  1581. rc = pt_adap_read_default(cd, buf, pip_len);
  1582. if (!rc)
  1583. bytes_read = pip_len;
  1584. else
  1585. bytes_read = 0;
  1586. }
  1587. } else {
  1588. pt_debug(cd->dev, DL_INFO,
  1589. "%s: Forced flush of max %d bytes...\n",
  1590. __func__, PT_MAX_PIP2_MSG_SIZE);
  1591. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1592. if (!rc)
  1593. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1594. else
  1595. bytes_read = 0;
  1596. }
  1597. if (read_buf && (bytes_read > 3))
  1598. memcpy(read_buf, buf, bytes_read);
  1599. exit:
  1600. return bytes_read;
  1601. }
  1602. /*******************************************************************************
  1603. * FUNCTION: pt_flush_bus_if_irq_asserted
  1604. *
  1605. * SUMMARY: This function will flush the active bus if the INT is found to be
  1606. * asserted.
  1607. *
  1608. * RETURN: bytes cleared from bus
  1609. *
  1610. * PARAMETERS:
  1611. * *cd - pointer the core data structure
  1612. * flush_type - type of flush
  1613. * - PT_FLUSH_BUS_BASED_ON_LEN
  1614. * - PT_FLUSH_BUS_FULL_256_READ
  1615. ******************************************************************************/
  1616. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1617. {
  1618. int count = 0;
  1619. int bytes_read = 0;
  1620. while (pt_check_irq_asserted(cd) && count < 5) {
  1621. count++;
  1622. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1623. if (bytes_read) {
  1624. pt_debug(cd->dev, DL_WARN,
  1625. "%s: Cleared %d bytes off bus\n",
  1626. __func__, bytes_read);
  1627. }
  1628. }
  1629. if (pt_check_irq_asserted(cd)) {
  1630. pt_debug(cd->dev, DL_ERROR,
  1631. "%s: IRQ still asserted, %d bytes read\n",
  1632. __func__, bytes_read);
  1633. } else {
  1634. pt_debug(cd->dev, DL_INFO,
  1635. "%s: IRQ cleared, %d bytes read\n",
  1636. __func__, bytes_read);
  1637. }
  1638. return bytes_read;
  1639. }
  1640. /*******************************************************************************
  1641. * FUNCTION: pt_hid_send_output_
  1642. *
  1643. * SUMMARY: Send a touch application command to the DUT
  1644. *
  1645. * RETURN:
  1646. * 0 = success
  1647. * !0 = failure
  1648. *
  1649. * PARAMETERS:
  1650. * *cd - pointer to core data
  1651. * *hid_output - pointer to the command to send
  1652. ******************************************************************************/
  1653. static int pt_hid_send_output_(struct pt_core_data *cd,
  1654. struct pt_hid_output *hid_output)
  1655. {
  1656. int rc = 0;
  1657. u8 *cmd;
  1658. u16 length;
  1659. u16 crc;
  1660. u8 report_id;
  1661. u8 cmd_offset = 0;
  1662. u8 cmd_allocated = 0;
  1663. switch (hid_output->cmd_type) {
  1664. case PIP1_CMD_TYPE_FW:
  1665. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1666. length = 5;
  1667. break;
  1668. case PIP1_CMD_TYPE_BL:
  1669. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1670. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1671. break;
  1672. default:
  1673. return -EINVAL;
  1674. }
  1675. length += hid_output->write_length;
  1676. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1677. cmd = kzalloc(length + 2, GFP_KERNEL);
  1678. if (!cmd)
  1679. return -ENOMEM;
  1680. cmd_allocated = 1;
  1681. } else {
  1682. cmd = cd->cmd_buf;
  1683. }
  1684. /* Set Output register */
  1685. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1686. sizeof(cd->hid_desc.output_register));
  1687. cmd_offset += sizeof(cd->hid_desc.output_register);
  1688. cmd[cmd_offset++] = LOW_BYTE(length);
  1689. cmd[cmd_offset++] = HI_BYTE(length);
  1690. cmd[cmd_offset++] = report_id;
  1691. cmd[cmd_offset++] = 0x0; /* reserved */
  1692. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1693. cmd[cmd_offset++] = PIP1_BL_SOP;
  1694. cmd[cmd_offset++] = hid_output->command_code;
  1695. /* Set Data Length for bootloader */
  1696. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1697. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1698. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1699. }
  1700. /* Set Data */
  1701. if (hid_output->write_length && hid_output->write_buf) {
  1702. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1703. hid_output->write_length);
  1704. cmd_offset += hid_output->write_length;
  1705. }
  1706. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1707. crc = _pt_compute_crc(&cmd[6],
  1708. hid_output->write_length + 4);
  1709. cmd[cmd_offset++] = LOW_BYTE(crc);
  1710. cmd[cmd_offset++] = HI_BYTE(crc);
  1711. cmd[cmd_offset++] = PIP1_BL_EOP;
  1712. }
  1713. pt_debug(cd->dev, DL_INFO,
  1714. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1715. __func__, length + 2, hid_output->command_code);
  1716. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1717. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1718. if (rc)
  1719. pt_debug(cd->dev, DL_ERROR,
  1720. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1721. if (cmd_allocated)
  1722. kfree(cmd);
  1723. return rc;
  1724. }
  1725. /*******************************************************************************
  1726. * FUNCTION: pt_pip1_send_output_and_wait_
  1727. *
  1728. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1729. *
  1730. * RETURN:
  1731. * 0 = success
  1732. * !0 = failure
  1733. *
  1734. * PARAMETERS:
  1735. * *cd - pointer to core data
  1736. * *hid_output - pointer to the command to send
  1737. ******************************************************************************/
  1738. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1739. struct pt_hid_output *hid_output)
  1740. {
  1741. int rc = 0;
  1742. int t;
  1743. u16 timeout_ms;
  1744. mutex_lock(&cd->system_lock);
  1745. cd->hid_cmd_state = hid_output->command_code + 1;
  1746. mutex_unlock(&cd->system_lock);
  1747. if (hid_output->timeout_ms)
  1748. timeout_ms = hid_output->timeout_ms;
  1749. else
  1750. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1751. rc = pt_hid_send_output_(cd, hid_output);
  1752. if (rc)
  1753. goto error;
  1754. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1755. msecs_to_jiffies(timeout_ms));
  1756. if (IS_TMO(t)) {
  1757. #ifdef TTDL_DIAGNOSTICS
  1758. cd->bus_transmit_error_count++;
  1759. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1760. #endif /* TTDL_DIAGNOSTICS */
  1761. pt_debug(cd->dev, DL_ERROR,
  1762. "%s: HID output cmd execution timed out (%dms)\n",
  1763. __func__, timeout_ms);
  1764. rc = -ETIME;
  1765. goto error;
  1766. }
  1767. if (!hid_output->novalidate)
  1768. rc = pt_hid_output_validate_response(cd, hid_output);
  1769. pt_check_command(cd, hid_output, false);
  1770. goto exit;
  1771. error:
  1772. mutex_lock(&cd->system_lock);
  1773. cd->hid_cmd_state = 0;
  1774. mutex_unlock(&cd->system_lock);
  1775. exit:
  1776. return rc;
  1777. }
  1778. /*******************************************************************************
  1779. * FUNCTION: pt_hid_output_user_cmd_
  1780. *
  1781. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1782. * to the DUT waiting for the response and loading it into the read buffer
  1783. *
  1784. * RETURN:
  1785. * 0 = success
  1786. * !0 = failure
  1787. *
  1788. * PARAMETERS:
  1789. * *cd - pointer to core data
  1790. * read_len - expected read length of the response
  1791. * *read_buf - pointer to where the response will be loaded
  1792. * write_len - length of the write buffer
  1793. * *write_buf - pointer to the write buffer
  1794. * *actual_read_len - pointer to the actual amount of data read back
  1795. ******************************************************************************/
  1796. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1797. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1798. u16 *actual_read_len)
  1799. {
  1800. int rc = 0;
  1801. u16 size;
  1802. struct pt_hid_output hid_output = {
  1803. .length = write_len,
  1804. .write_buf = write_buf,
  1805. };
  1806. #ifdef TTHE_TUNER_SUPPORT
  1807. if (!cd->pip2_send_user_cmd) {
  1808. int command_code = 0;
  1809. int len;
  1810. /* Print up to cmd ID */
  1811. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1812. if (write_len < len)
  1813. len = write_len;
  1814. else
  1815. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1816. & PIP1_CMD_COMMAND_ID_MASK;
  1817. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1818. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1819. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1820. tthe_print(cd, write_buf, len, "CMD=");
  1821. }
  1822. #endif
  1823. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1824. if (rc)
  1825. return rc;
  1826. /* Get the response size from the first 2 bytes in the response */
  1827. size = get_unaligned_le16(&cd->response_buf[0]);
  1828. /* Ensure size is not greater than max buffer size */
  1829. if (size > PT_MAX_PIP2_MSG_SIZE)
  1830. size = PT_MAX_PIP2_MSG_SIZE;
  1831. /* Minimum size to read is the 2 byte len field */
  1832. if (size == 0)
  1833. size = 2;
  1834. if (size > read_len) {
  1835. pt_debug(cd->dev, DL_ERROR,
  1836. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1837. __func__, size, read_len);
  1838. *actual_read_len = 0;
  1839. return -EIO;
  1840. }
  1841. memcpy(read_buf, cd->response_buf, size);
  1842. *actual_read_len = size;
  1843. return 0;
  1844. }
  1845. /*******************************************************************************
  1846. * FUNCTION: pt_hid_output_user_cmd
  1847. *
  1848. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1849. * the DUT.
  1850. *
  1851. * RETURN:
  1852. * 0 = success
  1853. * !0 = failure
  1854. *
  1855. * PARAMETERS:
  1856. * *cd - pointer to core data
  1857. * read_len - length of data to read
  1858. * *read_buf - pointer to store read data
  1859. * write_len - length of data to write
  1860. * *write_buf - pointer to buffer to write
  1861. * *actual_read_len - pointer to store data length actually read
  1862. ******************************************************************************/
  1863. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1864. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1865. u16 *actual_read_len)
  1866. {
  1867. int rc;
  1868. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1869. if (rc < 0) {
  1870. pt_debug(cd->dev, DL_ERROR,
  1871. "%s: fail get exclusive ex=%p own=%p\n",
  1872. __func__, cd->exclusive_dev, cd->dev);
  1873. return rc;
  1874. }
  1875. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1876. write_len, write_buf, actual_read_len);
  1877. if (release_exclusive(cd, cd->dev) < 0)
  1878. pt_debug(cd->dev, DL_ERROR,
  1879. "%s: fail to release exclusive\n", __func__);
  1880. return rc;
  1881. }
  1882. /*******************************************************************************
  1883. * FUNCTION: _pt_request_pip2_send_cmd
  1884. *
  1885. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1886. * interrupt and reads response data to read_buf
  1887. *
  1888. * RETURN:
  1889. * 0 = success
  1890. * !0 = failure
  1891. *
  1892. * PARAMETERS:
  1893. * *dev - pointer to device structure
  1894. * protect - flag to run in protected mode
  1895. * id - ID of PIP command
  1896. * *data - pointer to PIP data payload
  1897. * report_body_len - report length
  1898. * *read_buf - pointer to response buffer
  1899. * *actual_read_len - pointer to response buffer length
  1900. ******************************************************************************/
  1901. static int _pt_request_pip2_send_cmd(struct device *dev,
  1902. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1903. u16 *actual_read_len)
  1904. {
  1905. struct pt_core_data *cd = dev_get_drvdata(dev);
  1906. struct pip2_cmd_structure pip2_cmd;
  1907. int rc = 0;
  1908. u16 i = 0;
  1909. u16 j = 0;
  1910. u16 write_len;
  1911. u8 *write_buf = NULL;
  1912. u16 read_len;
  1913. u8 extra_bytes;
  1914. if (report_body_len > 247)
  1915. return -EPROTO;
  1916. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1917. /* Hard coded register for PIP2.x */
  1918. pip2_cmd.reg[0] = 0x01;
  1919. pip2_cmd.reg[1] = 0x01;
  1920. /*
  1921. * For PIP2.1+ the length field value includes itself:
  1922. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1923. *
  1924. * The overall write length must include only the register:
  1925. * ADD 2: 2 (Register)
  1926. */
  1927. extra_bytes = 6;
  1928. write_len = 2;
  1929. /* PIP2 the CMD ID is a 7bit field */
  1930. if (id > PIP2_CMD_ID_END) {
  1931. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1932. __func__, id);
  1933. rc = -EINVAL;
  1934. goto exit;
  1935. }
  1936. pip2_cmd.len = report_body_len + extra_bytes;
  1937. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1938. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1939. pip2_cmd.data = data;
  1940. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1941. /* Add the command length to the extra bytes based on PIP version */
  1942. write_len += pip2_cmd.len;
  1943. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1944. __func__, pip2_cmd.len, write_len);
  1945. write_buf = kzalloc(write_len, GFP_KERNEL);
  1946. if (write_buf == NULL) {
  1947. rc = -ENOMEM;
  1948. goto exit;
  1949. }
  1950. write_buf[i++] = pip2_cmd.reg[0];
  1951. write_buf[i++] = pip2_cmd.reg[1];
  1952. write_buf[i++] = pip2_cmd.len & 0xff;
  1953. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1954. write_buf[i++] = pip2_cmd.seq;
  1955. write_buf[i++] = pip2_cmd.id;
  1956. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1957. write_buf[j] = pip2_cmd.data[j-i];
  1958. write_buf[j++] = pip2_cmd.crc[0];
  1959. write_buf[j++] = pip2_cmd.crc[1];
  1960. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1961. if (read_len < 0)
  1962. read_len = 255;
  1963. pt_debug(dev, DL_INFO,
  1964. "%s cmd_id[0x%02X] expected response length:%d ",
  1965. __func__, pip2_cmd.id, read_len);
  1966. /*
  1967. * All PIP2 commands come through this function.
  1968. * Set flag for PIP2.x interface to allow response parsing to know
  1969. * how to decode the protocol header.
  1970. */
  1971. mutex_lock(&cd->system_lock);
  1972. cd->pip2_prot_active = true;
  1973. cd->pip2_send_user_cmd = true;
  1974. mutex_unlock(&cd->system_lock);
  1975. if (protect == PT_CORE_CMD_PROTECTED)
  1976. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1977. write_len, write_buf, actual_read_len);
  1978. else {
  1979. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1980. write_len, write_buf, actual_read_len);
  1981. }
  1982. if (rc) {
  1983. pt_debug(dev, DL_ERROR,
  1984. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1985. __func__, rc);
  1986. goto exit;
  1987. }
  1988. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1989. *actual_read_len);
  1990. exit:
  1991. mutex_lock(&cd->system_lock);
  1992. cd->pip2_prot_active = false;
  1993. cd->pip2_send_user_cmd = false;
  1994. mutex_unlock(&cd->system_lock);
  1995. kfree(write_buf);
  1996. return rc;
  1997. }
  1998. /*******************************************************************************
  1999. * FUNCTION: _pt_pip2_send_cmd_no_int
  2000. *
  2001. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2002. * reads response data to read_buf if response is available.
  2003. *
  2004. * NOTE:
  2005. * Interrupt MUST be disabled before to call this function.
  2006. *
  2007. * RETURN:
  2008. * 0 = success
  2009. * !0 = failure
  2010. *
  2011. * PARAMETERS:
  2012. * *dev - pointer to device structure
  2013. * protect - flag to run in protected mode
  2014. * id - ID of PIP command
  2015. * *data - pointer to PIP data payload
  2016. * report_body_len - report length
  2017. * *read_buf - pointer to response buffer
  2018. * *actual_read_len - pointer to response buffer length
  2019. ******************************************************************************/
  2020. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2021. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2022. u16 *actual_read_len)
  2023. {
  2024. int max_retry = 0;
  2025. int retry = 0;
  2026. int rc = 0;
  2027. u16 i = 0;
  2028. u16 j = 0;
  2029. u16 write_len;
  2030. u8 *write_buf = NULL;
  2031. u16 read_len;
  2032. u16 size = 0;
  2033. u8 response_seq = 0;
  2034. u8 extra_bytes;
  2035. u32 retry_interval = 0;
  2036. u32 retry_total_time = 0;
  2037. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2038. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2039. struct pt_core_data *cd = dev_get_drvdata(dev);
  2040. struct pip2_cmd_structure pip2_cmd;
  2041. if (report_body_len > 247)
  2042. return -EPROTO;
  2043. if (protect == PT_CORE_CMD_PROTECTED) {
  2044. rc = request_exclusive(cd,
  2045. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2046. if (rc < 0) {
  2047. pt_debug(cd->dev, DL_ERROR,
  2048. "%s: fail get exclusive ex=%p own=%p\n",
  2049. __func__, cd->exclusive_dev, cd->dev);
  2050. return rc;
  2051. }
  2052. }
  2053. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2054. /* Hard coded register for PIP2.x */
  2055. pip2_cmd.reg[0] = 0x01;
  2056. pip2_cmd.reg[1] = 0x01;
  2057. /*
  2058. * For PIP2.1+ the length field value includes itself:
  2059. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2060. *
  2061. * The overall write length must include only the register:
  2062. * ADD 2: 2 (Register)
  2063. */
  2064. extra_bytes = 6;
  2065. write_len = 2;
  2066. pip2_cmd.len = report_body_len + extra_bytes;
  2067. pip2_cmd.id = id;
  2068. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2069. pip2_cmd.data = data;
  2070. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2071. /* Add the command length to the extra bytes based on PIP version */
  2072. write_len += pip2_cmd.len;
  2073. write_buf = kzalloc(write_len, GFP_KERNEL);
  2074. if (write_buf == NULL) {
  2075. rc = -ENOMEM;
  2076. goto exit;
  2077. }
  2078. write_buf[i++] = pip2_cmd.reg[0];
  2079. write_buf[i++] = pip2_cmd.reg[1];
  2080. write_buf[i++] = pip2_cmd.len & 0xff;
  2081. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2082. write_buf[i++] = pip2_cmd.seq;
  2083. write_buf[i++] = pip2_cmd.id;
  2084. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2085. write_buf[j] = pip2_cmd.data[j-i];
  2086. write_buf[j++] = pip2_cmd.crc[0];
  2087. write_buf[j++] = pip2_cmd.crc[1];
  2088. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2089. if (read_len < 0)
  2090. read_len = 255;
  2091. pt_debug(dev, DL_INFO,
  2092. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2093. __func__, pip2_cmd.id, read_len);
  2094. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2095. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2096. if (rc) {
  2097. pt_debug(dev, DL_ERROR,
  2098. "%s: SPI write Error = %d\n",
  2099. __func__, rc);
  2100. goto exit;
  2101. }
  2102. #ifdef PT_POLL_RESP_BY_BUS
  2103. /*
  2104. * Frequent bus read can increase system load obviously. The expected
  2105. * first bus read should be valid and timely. The tollerance for
  2106. * usleep_range should be limited. The minimum response delay (between
  2107. * command finishes sending and INT pin falls) is less than 50
  2108. * microseconds. So the 10 microseconds should be maximum tollerance
  2109. * with the consideration that the unit to calculate the response delay
  2110. * is 10 microseconds and more precise is not necessary. Every
  2111. * additional 10 microseconds only contribute less than 3 milliseconds
  2112. * for whole BL.
  2113. */
  2114. usleep_range(resp_time_min, resp_time_min+10);
  2115. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2116. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2117. rc = pt_adap_read_default(cd, read_buf, read_len);
  2118. if (rc) {
  2119. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2120. __func__, rc);
  2121. break;
  2122. }
  2123. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2124. size = get_unaligned_le16(&read_buf[0]);
  2125. if ((size <= read_len) &&
  2126. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2127. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2128. break;
  2129. }
  2130. /*
  2131. * To reduce the bus and system load, increase the sleep
  2132. * step gradually:
  2133. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2134. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2135. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2136. */
  2137. retry++;
  2138. if (retry < 20) {
  2139. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2140. usleep_range(retry_interval,
  2141. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2142. } else if (retry < 40) {
  2143. retry_interval += 1000;
  2144. usleep_range(retry_interval, retry_interval + 1000);
  2145. } else {
  2146. retry_interval += 50000;
  2147. msleep(retry_interval/1000);
  2148. }
  2149. retry_total_time += retry_interval;
  2150. }
  2151. #else
  2152. /*
  2153. * Frequent GPIO read will not increase CPU/system load heavily if the
  2154. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2155. * fixed interval: 20 us.
  2156. */
  2157. usleep_range(resp_time_min, resp_time_min+10);
  2158. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2159. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2160. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2161. rc = pt_adap_read_default(cd, read_buf, read_len);
  2162. size = get_unaligned_le16(&read_buf[0]);
  2163. if (rc)
  2164. pt_debug(dev, DL_ERROR,
  2165. "%s: SPI read Error = %d\n",
  2166. __func__, rc);
  2167. else if (size > read_len) {
  2168. pt_debug(cd->dev, DL_ERROR,
  2169. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2170. __func__, size, read_len);
  2171. rc = -EIO;
  2172. }
  2173. break;
  2174. }
  2175. /*
  2176. * Poll GPIO with fixed interval 20 us, and tollerance is
  2177. * limited to 10 us to speed up the process.
  2178. */
  2179. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2180. usleep_range(retry_interval, retry_interval+10);
  2181. retry_total_time += retry_interval;
  2182. }
  2183. #endif
  2184. *actual_read_len = size;
  2185. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2186. pt_debug(dev, DL_ERROR,
  2187. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2188. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2189. *actual_read_len = 0;
  2190. rc = -EINVAL;
  2191. }
  2192. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2193. "<<< NO_INT Read");
  2194. exit:
  2195. kfree(write_buf);
  2196. if (protect == PT_CORE_CMD_PROTECTED) {
  2197. if (release_exclusive(cd, cd->dev) < 0)
  2198. pt_debug(cd->dev, DL_ERROR,
  2199. "%s: fail to release exclusive\n", __func__);
  2200. }
  2201. return rc;
  2202. }
  2203. /*******************************************************************************
  2204. * FUNCTION: pt_pip_null_
  2205. *
  2206. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2207. * This function is used by watchdog to check if the fw corrupts.
  2208. *
  2209. * RETURN:
  2210. * 0 = success
  2211. * !0 = failure
  2212. *
  2213. * PARAMETERS:
  2214. * *cd - pointer to core data
  2215. ******************************************************************************/
  2216. static int pt_pip_null_(struct pt_core_data *cd)
  2217. {
  2218. struct pt_hid_output hid_output = {
  2219. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2220. };
  2221. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2222. }
  2223. /*******************************************************************************
  2224. * FUNCTION: pt_pip_null
  2225. *
  2226. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2227. *
  2228. * RETURN:
  2229. * 0 = success
  2230. * !0 = failure
  2231. *
  2232. * PARAMETERS:
  2233. * *cd - pointer to core data
  2234. ******************************************************************************/
  2235. static int pt_pip_null(struct pt_core_data *cd)
  2236. {
  2237. int rc;
  2238. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2239. if (rc < 0) {
  2240. pt_debug(cd->dev, DL_ERROR,
  2241. "%s: fail get exclusive ex=%p own=%p\n",
  2242. __func__, cd->exclusive_dev, cd->dev);
  2243. return rc;
  2244. }
  2245. rc = pt_pip_null_(cd);
  2246. if (release_exclusive(cd, cd->dev) < 0)
  2247. pt_debug(cd->dev, DL_ERROR,
  2248. "%s: fail to release exclusive\n", __func__);
  2249. return rc;
  2250. }
  2251. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2252. /*******************************************************************************
  2253. * FUNCTION: pt_pip_start_bootloader_
  2254. *
  2255. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2256. *
  2257. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2258. * the WD active could cause this function to fail!
  2259. *
  2260. * RETURN:
  2261. * 0 = success
  2262. * !0 = failure
  2263. *
  2264. * PARAMETERS:
  2265. * *cd - pointer to core data
  2266. ******************************************************************************/
  2267. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2268. {
  2269. int rc = 0;
  2270. struct pt_hid_output hid_output = {
  2271. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2272. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2273. .reset_expected = 1,
  2274. };
  2275. if (cd->watchdog_enabled) {
  2276. pt_debug(cd->dev, DL_WARN,
  2277. "%s: watchdog isn't stopped before enter bl\n",
  2278. __func__);
  2279. goto exit;
  2280. }
  2281. /* Reset startup status after entering BL, new DUT enum required */
  2282. cd->startup_status = STARTUP_STATUS_START;
  2283. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2284. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2285. if (rc) {
  2286. pt_debug(cd->dev, DL_ERROR,
  2287. "%s: Start BL PIP cmd failed. rc = %d\n",
  2288. __func__, rc);
  2289. }
  2290. exit:
  2291. return rc;
  2292. }
  2293. /*******************************************************************************
  2294. * FUNCTION: pt_pip_start_bootloader
  2295. *
  2296. * SUMMARY: Protected function to force DUT to enter the BL
  2297. *
  2298. * RETURN:
  2299. * 0 = success
  2300. * !0 = failure
  2301. *
  2302. * PARAMETERS:
  2303. * *cd - pointer to core data structure
  2304. ******************************************************************************/
  2305. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2306. {
  2307. int rc;
  2308. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2309. if (rc < 0) {
  2310. pt_debug(cd->dev, DL_ERROR,
  2311. "%s: fail get exclusive ex=%p own=%p\n",
  2312. __func__, cd->exclusive_dev, cd->dev);
  2313. return rc;
  2314. }
  2315. rc = pt_pip_start_bootloader_(cd);
  2316. if (release_exclusive(cd, cd->dev) < 0)
  2317. pt_debug(cd->dev, DL_ERROR,
  2318. "%s: fail to release exclusive\n", __func__);
  2319. return rc;
  2320. }
  2321. /*******************************************************************************
  2322. * FUNCTION: _pt_request_pip_start_bl
  2323. *
  2324. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2325. * modules to request the DUT to enter the BL
  2326. *
  2327. * RETURN:
  2328. * 0 = success
  2329. * !0 = failure
  2330. *
  2331. * PARAMETERS:
  2332. * *dev - pointer to device structure
  2333. * protect - flag to run in protected mode
  2334. ******************************************************************************/
  2335. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2336. {
  2337. struct pt_core_data *cd = dev_get_drvdata(dev);
  2338. if (protect)
  2339. return pt_pip_start_bootloader(cd);
  2340. return pt_pip_start_bootloader_(cd);
  2341. }
  2342. /*******************************************************************************
  2343. * FUNCTION: pt_pip2_ver_load_ttdata
  2344. *
  2345. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2346. * command into the core data struct.
  2347. *
  2348. * RETURN: n/a
  2349. *
  2350. * PARAMETERS:
  2351. * *cd - pointer to core data structure
  2352. * len - Length of data in response_buf
  2353. ******************************************************************************/
  2354. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2355. {
  2356. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2357. struct pt_pip2_version_full *full_ver;
  2358. struct pt_pip2_version *ver;
  2359. /*
  2360. * The PIP2 VERSION command can return different lengths of data.
  2361. * The additional LOT fields are included when the packet
  2362. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2363. * NOTE:
  2364. * - The FW would swap the BL and FW versions when reporting
  2365. * the small packet.
  2366. * - Sub Lot bytes 16 and 17 are reserved.
  2367. */
  2368. if (len >= 0x1D) {
  2369. full_ver = (struct pt_pip2_version_full *)
  2370. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2371. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2372. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2373. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2374. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2375. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2376. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2377. /*
  2378. * BL PIP 2.02 and greater the version fields are
  2379. * swapped
  2380. */
  2381. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2382. ttdata->chip_rev =
  2383. get_unaligned_le16(&full_ver->chip_rev);
  2384. ttdata->chip_id =
  2385. get_unaligned_le16(&full_ver->chip_id);
  2386. } else {
  2387. ttdata->chip_rev =
  2388. get_unaligned_le16(&full_ver->chip_id);
  2389. ttdata->chip_id =
  2390. get_unaligned_le16(&full_ver->chip_rev);
  2391. }
  2392. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2393. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2394. sizeof(struct pt_pip2_version_full),
  2395. "PIP2 VERSION FULL");
  2396. } else {
  2397. ver = (struct pt_pip2_version *)
  2398. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2399. ttdata->pip_ver_major = ver->pip2_version_msb;
  2400. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2401. ttdata->bl_ver_major = ver->bl_version_msb;
  2402. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2403. ttdata->fw_ver_major = ver->fw_version_msb;
  2404. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2405. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2406. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2407. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2408. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2409. }
  2410. }
  2411. /*******************************************************************************
  2412. * FUNCTION: pt_si_get_ttdata
  2413. *
  2414. * SUMMARY: Function to load the version information from the system information
  2415. * PIP command into the core data struct.
  2416. *
  2417. * RETURN: n/a
  2418. *
  2419. * PARAMETERS:
  2420. * *cd - pointer to core data structure
  2421. ******************************************************************************/
  2422. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2423. {
  2424. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2425. struct pt_ttdata_dev *ttdata_dev =
  2426. (struct pt_ttdata_dev *)
  2427. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2428. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2429. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2430. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2431. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2432. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2433. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2434. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2435. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2436. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2437. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2438. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2439. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2440. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2441. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2442. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2443. }
  2444. /*******************************************************************************
  2445. * FUNCTION: pt_si_get_sensing_conf_data
  2446. *
  2447. * SUMMARY: Function to load the sensing information from the system information
  2448. * PIP command into the core data struct.
  2449. *
  2450. * RETURN: n/a
  2451. *
  2452. * PARAMETERS:
  2453. * *cd - pointer to core data structure
  2454. ******************************************************************************/
  2455. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2456. {
  2457. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2458. struct pt_sensing_conf_data_dev *scd_dev =
  2459. (struct pt_sensing_conf_data_dev *)
  2460. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2461. scd->electrodes_x = scd_dev->electrodes_x;
  2462. scd->electrodes_y = scd_dev->electrodes_y;
  2463. scd->origin_x = scd_dev->origin_x;
  2464. scd->origin_y = scd_dev->origin_y;
  2465. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2466. if (scd->origin_x & 0x02) {
  2467. scd->tx_num = scd->electrodes_x;
  2468. scd->rx_num = scd->electrodes_y;
  2469. } else {
  2470. scd->tx_num = scd->electrodes_y;
  2471. scd->rx_num = scd->electrodes_x;
  2472. }
  2473. /*
  2474. * When the Panel ID is coming from an XY pin and not a dedicated
  2475. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2476. * other DUTs as the loader will use cd->pid_for_loader to generate
  2477. * the bin file name but will ignore it if pid_for_loader is still
  2478. * set to PANEL_ID_NOT_ENABLED
  2479. */
  2480. if (cd->panel_id_support &
  2481. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2482. mutex_lock(&cd->system_lock);
  2483. cd->pid_for_loader = scd_dev->panel_id;
  2484. mutex_unlock(&cd->system_lock);
  2485. }
  2486. scd->panel_id = scd_dev->panel_id;
  2487. scd->btn = scd_dev->btn;
  2488. scd->scan_mode = scd_dev->scan_mode;
  2489. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2490. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2491. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2492. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2493. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2494. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2495. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2496. sizeof(struct pt_sensing_conf_data_dev),
  2497. "sensing_conf_data");
  2498. }
  2499. /*******************************************************************************
  2500. * FUNCTION: pt_si_setup
  2501. *
  2502. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2503. *
  2504. * RETURN:
  2505. * 0 = success
  2506. * !0 = failure
  2507. *
  2508. * PARAMETERS:
  2509. * *cd - pointer to core data structure
  2510. ******************************************************************************/
  2511. static int pt_si_setup(struct pt_core_data *cd)
  2512. {
  2513. struct pt_sysinfo *si = &cd->sysinfo;
  2514. int max_tch = si->sensing_conf_data.max_tch;
  2515. if (!si->xy_data)
  2516. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2517. GFP_KERNEL);
  2518. if (!si->xy_data)
  2519. return -ENOMEM;
  2520. if (!si->xy_mode)
  2521. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2522. if (!si->xy_mode) {
  2523. kfree(si->xy_data);
  2524. return -ENOMEM;
  2525. }
  2526. return 0;
  2527. }
  2528. /*******************************************************************************
  2529. * FUNCTION: pt_si_get_btn_data
  2530. *
  2531. * SUMMARY: Setup the core data button information based on the response of the
  2532. * System Information PIP command.
  2533. *
  2534. * RETURN:
  2535. * 0 = success
  2536. * !0 = failure
  2537. *
  2538. * PARAMETERS:
  2539. * *cd - pointer to core data structure
  2540. ******************************************************************************/
  2541. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2542. {
  2543. struct pt_sysinfo *si = &cd->sysinfo;
  2544. int num_btns = 0;
  2545. int num_defined_keys;
  2546. u16 *key_table;
  2547. int btn;
  2548. int i;
  2549. int rc = 0;
  2550. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2551. & PIP1_SYSINFO_BTN_MASK;
  2552. size_t btn_keys_size;
  2553. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2554. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2555. if (btns & (1 << i))
  2556. num_btns++;
  2557. }
  2558. si->num_btns = num_btns;
  2559. if (num_btns) {
  2560. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2561. if (!si->btn)
  2562. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2563. if (!si->btn)
  2564. return -ENOMEM;
  2565. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2566. num_defined_keys = 0;
  2567. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2568. num_defined_keys = 0;
  2569. else
  2570. num_defined_keys = cd->cpdata->sett
  2571. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2572. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2573. key_table = (u16 *)cd->cpdata->sett
  2574. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2575. si->btn[btn].key_code = key_table[btn];
  2576. si->btn[btn].enabled = true;
  2577. }
  2578. for (; btn < num_btns; btn++) {
  2579. si->btn[btn].key_code = KEY_RESERVED;
  2580. si->btn[btn].enabled = true;
  2581. }
  2582. return rc;
  2583. }
  2584. kfree(si->btn);
  2585. si->btn = NULL;
  2586. return rc;
  2587. }
  2588. /*******************************************************************************
  2589. * FUNCTION: pt_si_put_log_data
  2590. *
  2591. * SUMMARY: Prints all sys info data to kmsg log
  2592. *
  2593. * RETURN: n/a
  2594. *
  2595. * PARAMETERS:
  2596. * *cd - pointer to core data structure
  2597. ******************************************************************************/
  2598. static void pt_si_put_log_data(struct pt_core_data *cd)
  2599. {
  2600. struct pt_sysinfo *si = &cd->sysinfo;
  2601. struct pt_ttdata *ttdata = &si->ttdata;
  2602. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2603. int i;
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2605. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2607. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2608. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2609. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2610. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2611. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2612. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2613. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2614. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2615. __func__, ttdata->revctrl, ttdata->revctrl);
  2616. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2617. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2618. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2619. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2620. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2621. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2622. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2623. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2624. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2625. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2626. for (i = 0; i < PT_NUM_MFGID; i++)
  2627. pt_debug(cd->dev, DL_DEBUG,
  2628. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2629. __func__, i, ttdata->mfg_id[i],
  2630. ttdata->mfg_id[i]);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2632. __func__, ttdata->post_code, ttdata->post_code);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2634. __func__, scd->electrodes_x, scd->electrodes_x);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2636. __func__, scd->electrodes_y, scd->electrodes_y);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2638. __func__, scd->len_x, scd->len_x);
  2639. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2640. __func__, scd->len_y, scd->len_y);
  2641. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2642. __func__, scd->res_x, scd->res_x);
  2643. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2644. __func__, scd->res_y, scd->res_y);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2646. __func__, scd->max_z, scd->max_z);
  2647. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2648. __func__, scd->origin_x, scd->origin_x);
  2649. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2650. __func__, scd->origin_y, scd->origin_y);
  2651. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2652. __func__, scd->panel_id, scd->panel_id);
  2653. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2654. __func__, scd->btn, scd->btn);
  2655. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2656. __func__, scd->scan_mode, scd->scan_mode);
  2657. pt_debug(cd->dev, DL_DEBUG,
  2658. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2659. __func__, scd->max_tch, scd->max_tch);
  2660. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2661. __func__, si->xy_mode);
  2662. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2663. __func__, si->xy_data);
  2664. }
  2665. /*******************************************************************************
  2666. * FUNCTION: pt_get_sysinfo_regs
  2667. *
  2668. * SUMMARY: Setup all the core data System information based on the response
  2669. * of the System Information PIP command.
  2670. *
  2671. * RETURN:
  2672. * 0 = success
  2673. * !0 = failure
  2674. *
  2675. * PARAMETERS:
  2676. * *cd - pointer to core data structure
  2677. ******************************************************************************/
  2678. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2679. {
  2680. struct pt_sysinfo *si = &cd->sysinfo;
  2681. int rc;
  2682. rc = pt_si_get_btn_data(cd);
  2683. if (rc < 0)
  2684. return rc;
  2685. pt_si_get_ttdata(cd);
  2686. pt_si_get_sensing_conf_data(cd);
  2687. pt_si_setup(cd);
  2688. pt_si_put_log_data(cd);
  2689. si->ready = true;
  2690. return rc;
  2691. }
  2692. /*******************************************************************************
  2693. * FUNCTION: pt_free_si_ptrs
  2694. *
  2695. * SUMMARY: Frees all memory associated with the System Information within
  2696. * core data
  2697. *
  2698. * RETURN: n/a
  2699. *
  2700. * PARAMETERS:
  2701. * *cd - pointer to core data structure
  2702. ******************************************************************************/
  2703. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2704. {
  2705. struct pt_sysinfo *si = &cd->sysinfo;
  2706. kfree(si->btn);
  2707. kfree(si->xy_mode);
  2708. kfree(si->xy_data);
  2709. }
  2710. /*******************************************************************************
  2711. * FUNCTION: pt_hid_output_get_sysinfo_
  2712. *
  2713. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2714. * response.
  2715. *
  2716. * RETURN::
  2717. * 0 = success
  2718. * !0 = failure
  2719. *
  2720. * PARAMETERS:
  2721. * *cd - pointer to core data structure
  2722. ******************************************************************************/
  2723. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2724. {
  2725. int rc = 0;
  2726. struct pt_hid_output hid_output = {
  2727. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2728. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2729. };
  2730. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2731. if (rc)
  2732. return rc;
  2733. /* Parse the sysinfo data */
  2734. rc = pt_get_sysinfo_regs(cd);
  2735. if (rc)
  2736. pt_free_si_ptrs(cd);
  2737. return rc;
  2738. }
  2739. /*******************************************************************************
  2740. * FUNCTION: pt_hid_output_get_sysinfo
  2741. *
  2742. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2743. *
  2744. * RETURN::
  2745. * 0 = success
  2746. * !0 = failure
  2747. *
  2748. * PARAMETERS:
  2749. * *cd - pointer to core data structure
  2750. ******************************************************************************/
  2751. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2752. {
  2753. int rc;
  2754. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2755. if (rc < 0) {
  2756. pt_debug(cd->dev, DL_ERROR,
  2757. "%s: fail get exclusive ex=%p own=%p\n",
  2758. __func__, cd->exclusive_dev, cd->dev);
  2759. return rc;
  2760. }
  2761. rc = pt_hid_output_get_sysinfo_(cd);
  2762. if (release_exclusive(cd, cd->dev) < 0)
  2763. pt_debug(cd->dev, DL_ERROR,
  2764. "%s: fail to release exclusive\n", __func__);
  2765. return rc;
  2766. }
  2767. /*******************************************************************************
  2768. * FUNCTION: pt_pip_suspend_scanning_
  2769. *
  2770. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2771. *
  2772. * RETURN::
  2773. * 0 = success
  2774. * !0 = failure
  2775. *
  2776. * PARAMETERS:
  2777. * *cd - pointer to core data structure
  2778. ******************************************************************************/
  2779. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2780. {
  2781. int rc = 0;
  2782. struct pt_hid_output hid_output = {
  2783. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2784. };
  2785. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2786. if (rc) {
  2787. pt_debug(cd->dev, DL_ERROR,
  2788. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2789. __func__, rc);
  2790. }
  2791. return rc;
  2792. }
  2793. /*******************************************************************************
  2794. * FUNCTION: pt_pip_suspend_scanning
  2795. *
  2796. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2797. *
  2798. * RETURN::
  2799. * 0 = success
  2800. * !0 = failure
  2801. *
  2802. * PARAMETERS:
  2803. * *cd - pointer to core data structure
  2804. ******************************************************************************/
  2805. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2806. {
  2807. int rc;
  2808. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2809. if (rc < 0) {
  2810. pt_debug(cd->dev, DL_ERROR,
  2811. "%s: fail get exclusive ex=%p own=%p\n",
  2812. __func__, cd->exclusive_dev, cd->dev);
  2813. return rc;
  2814. }
  2815. rc = pt_pip_suspend_scanning_(cd);
  2816. if (release_exclusive(cd, cd->dev) < 0)
  2817. pt_debug(cd->dev, DL_ERROR,
  2818. "%s: fail to release exclusive\n", __func__);
  2819. return rc;
  2820. }
  2821. /*******************************************************************************
  2822. * FUNCTION: _pt_request_pip_suspend_scanning
  2823. *
  2824. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2825. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2826. *
  2827. * RETURN:
  2828. * 0 = success
  2829. * !0 = failure
  2830. *
  2831. * PARAMETERS:
  2832. * *dev - pointer to device structure
  2833. * protect - 0 = call non-protected function
  2834. * 1 = call protected function
  2835. ******************************************************************************/
  2836. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2837. int protect)
  2838. {
  2839. struct pt_core_data *cd = dev_get_drvdata(dev);
  2840. if (protect)
  2841. return pt_pip_suspend_scanning(cd);
  2842. return pt_pip_suspend_scanning_(cd);
  2843. }
  2844. /*******************************************************************************
  2845. * FUNCTION: pt_pip_resume_scanning_
  2846. *
  2847. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2848. *
  2849. * RETURN::
  2850. * 0 = success
  2851. * !0 = failure
  2852. *
  2853. * PARAMETERS:
  2854. * *cd - pointer to core data structure
  2855. ******************************************************************************/
  2856. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2857. {
  2858. int rc = 0;
  2859. struct pt_hid_output hid_output = {
  2860. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2861. };
  2862. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2863. if (rc) {
  2864. pt_debug(cd->dev, DL_ERROR,
  2865. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2866. __func__, rc);
  2867. }
  2868. return rc;
  2869. }
  2870. /*******************************************************************************
  2871. * FUNCTION: pt_pip_resume_scanning
  2872. *
  2873. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2874. *
  2875. * RETURN::
  2876. * 0 = success
  2877. * !0 = failure
  2878. *
  2879. * PARAMETERS:
  2880. * *cd - pointer to core data structure
  2881. ******************************************************************************/
  2882. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2883. {
  2884. int rc;
  2885. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2886. if (rc < 0) {
  2887. pt_debug(cd->dev, DL_ERROR,
  2888. "%s: fail get exclusive ex=%p own=%p\n",
  2889. __func__, cd->exclusive_dev, cd->dev);
  2890. return rc;
  2891. }
  2892. rc = pt_pip_resume_scanning_(cd);
  2893. if (release_exclusive(cd, cd->dev) < 0)
  2894. pt_debug(cd->dev, DL_ERROR,
  2895. "%s: fail to release exclusive\n", __func__);
  2896. return rc;
  2897. }
  2898. /*******************************************************************************
  2899. * FUNCTION: _pt_request_pip_resume_scanning
  2900. *
  2901. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2902. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2903. *
  2904. * RETURN::
  2905. * 0 = success
  2906. * !0 = failure
  2907. *
  2908. * PARAMETERS:
  2909. * *dev - pointer to device structure
  2910. * protect - 0 = call non-protected function
  2911. * 1 = call protected function
  2912. ******************************************************************************/
  2913. static int _pt_request_pip_resume_scanning(struct device *dev,
  2914. int protect)
  2915. {
  2916. struct pt_core_data *cd = dev_get_drvdata(dev);
  2917. if (protect)
  2918. return pt_pip_resume_scanning(cd);
  2919. return pt_pip_resume_scanning_(cd);
  2920. }
  2921. /*******************************************************************************
  2922. * FUNCTION: pt_pip_get_param_
  2923. *
  2924. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2925. * the 32bit parameter value
  2926. *
  2927. * RETURN:
  2928. * 0 = success
  2929. * !0 = failure
  2930. *
  2931. * PARAMETERS:
  2932. * *cd - pointer to core data
  2933. * param_id - parameter ID to retrieve
  2934. * *value - value of DUT parameter
  2935. ******************************************************************************/
  2936. static int pt_pip_get_param_(struct pt_core_data *cd,
  2937. u8 param_id, u32 *value)
  2938. {
  2939. int write_length = 1;
  2940. u8 param[1] = { param_id };
  2941. u8 read_param_id;
  2942. int param_size;
  2943. u8 *ptr;
  2944. int rc = 0;
  2945. int i;
  2946. struct pt_hid_output hid_output = {
  2947. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2948. .write_length = write_length,
  2949. .write_buf = param,
  2950. };
  2951. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2952. if (rc)
  2953. return rc;
  2954. read_param_id = cd->response_buf[5];
  2955. if (read_param_id != param_id)
  2956. return -EPROTO;
  2957. param_size = cd->response_buf[6];
  2958. ptr = &cd->response_buf[7];
  2959. *value = 0;
  2960. for (i = 0; i < param_size; i++)
  2961. *value += ptr[i] << (i * 8);
  2962. return 0;
  2963. }
  2964. /*******************************************************************************
  2965. * FUNCTION: pt_pip_get_param
  2966. *
  2967. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2968. *
  2969. * RETURN:
  2970. * 0 = success
  2971. * !0 = failure
  2972. *
  2973. * PARAMETERS:
  2974. * *cd - pointer to core data
  2975. * param_id - parameter ID to retrieve
  2976. * *value - value of DUT parameter
  2977. ******************************************************************************/
  2978. static int pt_pip_get_param(struct pt_core_data *cd,
  2979. u8 param_id, u32 *value)
  2980. {
  2981. int rc;
  2982. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2983. if (rc < 0) {
  2984. pt_debug(cd->dev, DL_ERROR,
  2985. "%s: fail get exclusive ex=%p own=%p\n",
  2986. __func__, cd->exclusive_dev, cd->dev);
  2987. return rc;
  2988. }
  2989. rc = pt_pip_get_param_(cd, param_id, value);
  2990. if (release_exclusive(cd, cd->dev) < 0)
  2991. pt_debug(cd->dev, DL_ERROR,
  2992. "%s: fail to release exclusive\n", __func__);
  2993. return rc;
  2994. }
  2995. /*******************************************************************************
  2996. * FUNCTION: _pt_request_pip_get_param
  2997. *
  2998. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2999. * calls to the protected or unprotected call to pt_pip_get_param
  3000. *
  3001. * RETURN:
  3002. * 0 = success
  3003. * !0 = failure
  3004. *
  3005. * PARAMETERS:
  3006. * *dev - pointer to device structure
  3007. * protect - flag to call protected or non protected function
  3008. * param_id - parameter ID to retrieve
  3009. * *value - value of DUT parameter
  3010. ******************************************************************************/
  3011. int _pt_request_pip_get_param(struct device *dev,
  3012. int protect, u8 param_id, u32 *value)
  3013. {
  3014. struct pt_core_data *cd = dev_get_drvdata(dev);
  3015. if (protect)
  3016. return pt_pip_get_param(cd, param_id, value);
  3017. return pt_pip_get_param_(cd, param_id, value);
  3018. }
  3019. /*******************************************************************************
  3020. * FUNCTION: pt_pip_set_param_
  3021. *
  3022. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3023. * passed in value to flash
  3024. *
  3025. * RETURN:
  3026. * 0 = success
  3027. * !0 = failure
  3028. *
  3029. * PARAMETERS:
  3030. * *cd - pointer to core data
  3031. * param_id - parameter ID to set
  3032. * value - value to write
  3033. * size - size to write
  3034. ******************************************************************************/
  3035. static int pt_pip_set_param_(struct pt_core_data *cd,
  3036. u8 param_id, u32 value, u8 size)
  3037. {
  3038. u8 write_buf[6];
  3039. u8 *ptr = &write_buf[2];
  3040. int rc = 0;
  3041. int i;
  3042. struct pt_hid_output hid_output = {
  3043. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3044. .write_buf = write_buf,
  3045. };
  3046. write_buf[0] = param_id;
  3047. write_buf[1] = size;
  3048. for (i = 0; i < size; i++) {
  3049. ptr[i] = value & 0xFF;
  3050. value = value >> 8;
  3051. }
  3052. hid_output.write_length = 2 + size;
  3053. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3054. if (rc)
  3055. return rc;
  3056. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3057. return -EPROTO;
  3058. return 0;
  3059. }
  3060. /*******************************************************************************
  3061. * FUNCTION: pt_pip_set_param
  3062. *
  3063. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3064. *
  3065. * RETURN:
  3066. * 0 = success
  3067. * !0 = failure
  3068. *
  3069. * PARAMETERS:
  3070. * *cd - pointer to core data
  3071. * param_id - parameter ID to set
  3072. * value - value to write
  3073. * size - size to write
  3074. ******************************************************************************/
  3075. static int pt_pip_set_param(struct pt_core_data *cd,
  3076. u8 param_id, u32 value, u8 size)
  3077. {
  3078. int rc;
  3079. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3080. if (rc < 0) {
  3081. pt_debug(cd->dev, DL_ERROR,
  3082. "%s: fail get exclusive ex=%p own=%p\n",
  3083. __func__, cd->exclusive_dev, cd->dev);
  3084. return rc;
  3085. }
  3086. rc = pt_pip_set_param_(cd, param_id, value, size);
  3087. if (release_exclusive(cd, cd->dev) < 0)
  3088. pt_debug(cd->dev, DL_ERROR,
  3089. "%s: fail to release exclusive\n", __func__);
  3090. return rc;
  3091. }
  3092. /*******************************************************************************
  3093. * FUNCTION: _pt_request_pip_set_param
  3094. *
  3095. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3096. * calls to the protected or unprotected call to pt_pip_set_param
  3097. *
  3098. * RETURN:
  3099. * 0 = success
  3100. * !0 = failure
  3101. *
  3102. * PARAMETERS:
  3103. * *dev - pointer to device structure
  3104. * protect - flag to call protected or non-protected
  3105. * param_id - parameter ID to set
  3106. * value - value to write
  3107. * size - size to write
  3108. ******************************************************************************/
  3109. int _pt_request_pip_set_param(struct device *dev, int protect,
  3110. u8 param_id, u32 value, u8 size)
  3111. {
  3112. struct pt_core_data *cd = dev_get_drvdata(dev);
  3113. if (protect)
  3114. return pt_pip_set_param(cd, param_id, value, size);
  3115. return pt_pip_set_param_(cd, param_id, value, size);
  3116. }
  3117. /*******************************************************************************
  3118. * FUNCTION: _pt_pip_enter_easywake_state_
  3119. *
  3120. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3121. *
  3122. * RETURN:
  3123. * 0 = success
  3124. * !0 = failure
  3125. *
  3126. * PARAMETERS:
  3127. * *dev - pointer to device structure
  3128. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3129. * *return_data - return status if easywake was entered
  3130. ******************************************************************************/
  3131. static int pt_hid_output_enter_easywake_state_(
  3132. struct pt_core_data *cd, u8 data, u8 *return_data)
  3133. {
  3134. int write_length = 1;
  3135. u8 param[1] = { data };
  3136. int rc = 0;
  3137. struct pt_hid_output hid_output = {
  3138. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3139. .write_length = write_length,
  3140. .write_buf = param,
  3141. };
  3142. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3143. if (rc)
  3144. return rc;
  3145. *return_data = cd->response_buf[5];
  3146. return rc;
  3147. }
  3148. /*******************************************************************************
  3149. * FUNCTION: pt_pip_verify_config_block_crc_
  3150. *
  3151. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3152. *
  3153. * RETURN:
  3154. * 0 = success
  3155. * !0 = failure
  3156. *
  3157. * PARAMETERS:
  3158. * *cd - pointer the core data structure
  3159. * ebid - enumerated block ID
  3160. * *status - PIP command status
  3161. * calculated_crc - calculated CRC
  3162. * stored_crc - stored CRC in config area
  3163. ******************************************************************************/
  3164. static int pt_pip_verify_config_block_crc_(
  3165. struct pt_core_data *cd, u8 ebid, u8 *status,
  3166. u16 *calculated_crc, u16 *stored_crc)
  3167. {
  3168. int write_length = 1;
  3169. u8 param[1] = { ebid };
  3170. u8 *ptr;
  3171. int rc = 0;
  3172. struct pt_hid_output hid_output = {
  3173. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3174. .write_length = write_length,
  3175. .write_buf = param,
  3176. };
  3177. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3178. if (rc)
  3179. return rc;
  3180. ptr = &cd->response_buf[5];
  3181. *status = ptr[0];
  3182. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3183. *stored_crc = get_unaligned_le16(&ptr[3]);
  3184. return 0;
  3185. }
  3186. /*******************************************************************************
  3187. * FUNCTION: pt_pip_verify_config_block_crc
  3188. *
  3189. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3190. * an exclusive access to the DUT.
  3191. *
  3192. * RETURN:
  3193. * 0 = success
  3194. * !0 = failure
  3195. *
  3196. * PARAMETERS:
  3197. * *cd - pointer the core data structure
  3198. * ebid - enumerated block ID
  3199. * *status - PIP command status
  3200. * calculated_crc - calculated CRC
  3201. * stored_crc - stored CRC in config area
  3202. ******************************************************************************/
  3203. static int pt_pip_verify_config_block_crc(
  3204. struct pt_core_data *cd, u8 ebid, u8 *status,
  3205. u16 *calculated_crc, u16 *stored_crc)
  3206. {
  3207. int rc;
  3208. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3209. if (rc < 0) {
  3210. pt_debug(cd->dev, DL_ERROR,
  3211. "%s: fail get exclusive ex=%p own=%p\n",
  3212. __func__, cd->exclusive_dev, cd->dev);
  3213. return rc;
  3214. }
  3215. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3216. calculated_crc, stored_crc);
  3217. if (release_exclusive(cd, cd->dev) < 0)
  3218. pt_debug(cd->dev, DL_ERROR,
  3219. "%s: fail to release exclusive\n", __func__);
  3220. return rc;
  3221. }
  3222. /*******************************************************************************
  3223. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3224. *
  3225. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3226. * calls to the protected or unprotected call to
  3227. * pt_pip_verify_config_block_crc_
  3228. *
  3229. * RETURN:
  3230. * 0 = success
  3231. * !0 = failure
  3232. *
  3233. * PARAMETERS:
  3234. * *dev - pointer to device structure
  3235. * protect - flag to call protected or non-protected
  3236. * ebid - enumerated block ID
  3237. * *status - PIP command status
  3238. * calculated_crc - calculated CRC
  3239. * stored_crc - stored CRC in config area
  3240. ******************************************************************************/
  3241. static int _pt_request_pip_verify_config_block_crc(
  3242. struct device *dev, int protect, u8 ebid, u8 *status,
  3243. u16 *calculated_crc, u16 *stored_crc)
  3244. {
  3245. struct pt_core_data *cd = dev_get_drvdata(dev);
  3246. if (protect)
  3247. return pt_pip_verify_config_block_crc(cd, ebid,
  3248. status, calculated_crc, stored_crc);
  3249. return pt_pip_verify_config_block_crc_(cd, ebid,
  3250. status, calculated_crc, stored_crc);
  3251. }
  3252. /*******************************************************************************
  3253. * FUNCTION: pt_pip_get_config_row_size_
  3254. *
  3255. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3256. *
  3257. * RETURN:
  3258. * 0 = success
  3259. * !0 = failure
  3260. *
  3261. * PARAMETERS:
  3262. * *cd - pointer to core data
  3263. * protect - flag to call protected or non-protected
  3264. * *row_size - pointer to store the retrieved row size
  3265. ******************************************************************************/
  3266. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3267. u16 *row_size)
  3268. {
  3269. int rc = 0;
  3270. struct pt_hid_output hid_output = {
  3271. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3272. };
  3273. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3274. if (rc)
  3275. return rc;
  3276. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3277. return 0;
  3278. }
  3279. /*******************************************************************************
  3280. * FUNCTION: pt_pip_get_config_row_size
  3281. *
  3282. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3283. * an exclusive access to the DUT.
  3284. *
  3285. * RETURN:
  3286. * 0 = success
  3287. * !0 = failure
  3288. *
  3289. * PARAMETERS:
  3290. * *cd - pointer to core data
  3291. * protect - flag to call protected or non-protected
  3292. * *row_size - pointer to store the retrieved row size
  3293. ******************************************************************************/
  3294. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3295. u16 *row_size)
  3296. {
  3297. int rc;
  3298. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3299. if (rc < 0) {
  3300. pt_debug(cd->dev, DL_ERROR,
  3301. "%s: fail get exclusive ex=%p own=%p\n",
  3302. __func__, cd->exclusive_dev, cd->dev);
  3303. return rc;
  3304. }
  3305. rc = pt_pip_get_config_row_size_(cd, row_size);
  3306. if (release_exclusive(cd, cd->dev) < 0)
  3307. pt_debug(cd->dev, DL_ERROR,
  3308. "%s: fail to release exclusive\n", __func__);
  3309. return rc;
  3310. }
  3311. /*******************************************************************************
  3312. * FUNCTION: _pt_request_pip_get_config_row_size
  3313. *
  3314. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3315. * calls to the protected or unprotected call to
  3316. * pt_pip_get_config_row_size_
  3317. *
  3318. * RETURN:
  3319. * 0 = success
  3320. * !0 = failure
  3321. *
  3322. * PARAMETERS:
  3323. * *dev - pointer to device structure
  3324. * protect - flag to call protected or non-protected
  3325. * *row_size - pointer to store the retrieved row size
  3326. ******************************************************************************/
  3327. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3328. int protect, u16 *row_size)
  3329. {
  3330. struct pt_core_data *cd = dev_get_drvdata(dev);
  3331. if (protect)
  3332. return pt_pip_get_config_row_size(cd, row_size);
  3333. return pt_pip_get_config_row_size_(cd, row_size);
  3334. }
  3335. /*******************************************************************************
  3336. * FUNCTION: pt_pip1_read_data_block_
  3337. *
  3338. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3339. * output data to the "read_buf" and update "crc".
  3340. *
  3341. * RETURN:
  3342. * 0 = success
  3343. * !0 = failure
  3344. *
  3345. * PARAMETERS:
  3346. * *cd - pointer to core data
  3347. * row_number - row number
  3348. * length - length of data to read
  3349. * ebid - block id
  3350. * *actual_read_len - Actual data length read
  3351. * *read_buf - pointer to the buffer to store read data
  3352. * read_buf_size - size of read_buf
  3353. * *crc - pointer to store CRC of row data
  3354. ******************************************************************************/
  3355. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3356. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3357. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3358. {
  3359. int read_ebid;
  3360. int status;
  3361. int rc = 0;
  3362. int write_length = 5;
  3363. u8 write_buf[5];
  3364. u8 cmd_offset = 0;
  3365. u16 calc_crc;
  3366. struct pt_hid_output hid_output = {
  3367. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3368. .write_length = write_length,
  3369. .write_buf = write_buf,
  3370. };
  3371. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3372. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3373. write_buf[cmd_offset++] = LOW_BYTE(length);
  3374. write_buf[cmd_offset++] = HI_BYTE(length);
  3375. write_buf[cmd_offset++] = ebid;
  3376. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3377. if (rc)
  3378. return rc;
  3379. status = cd->response_buf[5];
  3380. if (status)
  3381. return status;
  3382. read_ebid = cd->response_buf[6];
  3383. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3384. return -EPROTO;
  3385. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3386. if (length == 0 || *actual_read_len == 0)
  3387. return 0;
  3388. if (read_buf_size >= *actual_read_len &&
  3389. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3390. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3391. else
  3392. return -EPROTO;
  3393. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3394. /* Validate Row Data CRC */
  3395. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3396. if (*crc == calc_crc) {
  3397. return 0;
  3398. } else {
  3399. pt_debug(cd->dev, DL_ERROR,
  3400. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3401. __func__, *crc, calc_crc);
  3402. return -EPROTO;
  3403. }
  3404. }
  3405. /*******************************************************************************
  3406. * FUNCTION: _pt_request_pip_read_data_block
  3407. *
  3408. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3409. * calls to pt_pip1_read_data_block_
  3410. *
  3411. * RETURN:
  3412. * 0 = success
  3413. * !0 = failure
  3414. *
  3415. * PARAMETERS:
  3416. * *dev - pointer to device structure
  3417. * row_number - row number
  3418. * length - length of data to read
  3419. * ebid - block id
  3420. * *actual_read_len - Actual data length read
  3421. * *read_buf - pointer to the buffer to store read data
  3422. * *crc - pointer to store CRC of row data
  3423. ******************************************************************************/
  3424. static int _pt_request_pip_read_data_block(struct device *dev,
  3425. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3426. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3427. {
  3428. struct pt_core_data *cd = dev_get_drvdata(dev);
  3429. return pt_pip1_read_data_block_(cd, row_number, length,
  3430. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3431. }
  3432. /*******************************************************************************
  3433. * FUNCTION: pt_pip1_write_data_block_
  3434. *
  3435. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3436. * write data to the data block.
  3437. *
  3438. * RETURN:
  3439. * 0 = success
  3440. * !0 = failure
  3441. *
  3442. * PARAMETERS:
  3443. * *cd - pointer to core data
  3444. * row_number - row in config block to write to
  3445. * write_length - length of data to write
  3446. * ebid - enumerated block ID
  3447. * *write_buf - pointer to buffer to write
  3448. * *security_key - pointer to security key to allow write
  3449. * *actual_write_len - pointer to store data length actually written
  3450. ******************************************************************************/
  3451. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3452. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3453. u8 *security_key, u16 *actual_write_len)
  3454. {
  3455. /* row_number + write_len + ebid + security_key + crc */
  3456. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3457. u8 *full_write_buf;
  3458. u8 cmd_offset = 0;
  3459. u16 crc;
  3460. int status;
  3461. int rc = 0;
  3462. int read_ebid;
  3463. u8 *data;
  3464. struct pt_hid_output hid_output = {
  3465. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3466. .write_length = full_write_length,
  3467. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3468. };
  3469. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3470. return -EINVAL;
  3471. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3472. if (!full_write_buf)
  3473. return -ENOMEM;
  3474. hid_output.write_buf = full_write_buf;
  3475. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3476. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3477. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3478. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3479. full_write_buf[cmd_offset++] = ebid;
  3480. data = &full_write_buf[cmd_offset];
  3481. memcpy(data, write_buf, write_length);
  3482. cmd_offset += write_length;
  3483. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3484. cmd_offset += 8;
  3485. crc = _pt_compute_crc(data, write_length);
  3486. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3487. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3488. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3489. if (rc)
  3490. goto exit;
  3491. status = cd->response_buf[5];
  3492. if (status) {
  3493. rc = -EINVAL;
  3494. goto exit;
  3495. }
  3496. read_ebid = cd->response_buf[6];
  3497. if (read_ebid != ebid) {
  3498. rc = -EPROTO;
  3499. goto exit;
  3500. }
  3501. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3502. exit:
  3503. kfree(full_write_buf);
  3504. return rc;
  3505. }
  3506. /*******************************************************************************
  3507. * FUNCTION: _pt_request_pip_write_data_block
  3508. *
  3509. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3510. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3511. *
  3512. * RETURN:
  3513. * 0 = success
  3514. * !0 = failure
  3515. *
  3516. * PARAMETERS:
  3517. * *dev - pointer to device structure
  3518. * protect - flag to call protected or non-protected
  3519. * row_number - row in config block to write to
  3520. * write_length - length of data to write
  3521. * ebid - enumerated block ID
  3522. * *write_buf - pointer to buffer to write
  3523. * *security_key - pointer to security key to allow write
  3524. * *actual_write_len - pointer to store data length actually written
  3525. ******************************************************************************/
  3526. static int _pt_request_pip_write_data_block(struct device *dev,
  3527. u16 row_number, u16 write_length, u8 ebid,
  3528. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3529. {
  3530. struct pt_core_data *cd = dev_get_drvdata(dev);
  3531. return pt_pip1_write_data_block_(cd, row_number,
  3532. write_length, ebid, write_buf, security_key,
  3533. actual_write_len);
  3534. }
  3535. /*******************************************************************************
  3536. * FUNCTION: pt_pip_get_data_structure_
  3537. *
  3538. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3539. * returning a structure of data defined by data_id
  3540. *
  3541. * RETURN:
  3542. * 0 = success
  3543. * !0 = failure
  3544. *
  3545. * PARAMETERS:
  3546. * *cd - pointer to core data
  3547. * read_offset - read pointer offset
  3548. * read_length - length of data to read
  3549. * data_id - data ID to read
  3550. * *status - pointer to store the read response status
  3551. * *data_format - pointer to store format of data read
  3552. * *actual_read_len - pointer to store data length actually read
  3553. * *data - pointer to store data read
  3554. ******************************************************************************/
  3555. static int pt_pip_get_data_structure_(
  3556. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3557. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3558. u8 *data)
  3559. {
  3560. int rc = 0;
  3561. u16 total_read_len = 0;
  3562. u16 read_len;
  3563. u16 off_buf = 0;
  3564. u8 write_buf[5];
  3565. u8 read_data_id;
  3566. struct pt_hid_output hid_output = {
  3567. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3568. .write_length = 5,
  3569. .write_buf = write_buf,
  3570. };
  3571. again:
  3572. write_buf[0] = LOW_BYTE(read_offset);
  3573. write_buf[1] = HI_BYTE(read_offset);
  3574. write_buf[2] = LOW_BYTE(read_length);
  3575. write_buf[3] = HI_BYTE(read_length);
  3576. write_buf[4] = data_id;
  3577. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3578. if (rc)
  3579. return rc;
  3580. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3581. goto set_status;
  3582. read_data_id = cd->response_buf[6];
  3583. if (read_data_id != data_id)
  3584. return -EPROTO;
  3585. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3586. if (read_len && data) {
  3587. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3588. total_read_len += read_len;
  3589. if (read_len < read_length) {
  3590. read_offset += read_len;
  3591. off_buf += read_len;
  3592. read_length -= read_len;
  3593. goto again;
  3594. }
  3595. }
  3596. if (data_format)
  3597. *data_format = cd->response_buf[9];
  3598. if (actual_read_len)
  3599. *actual_read_len = total_read_len;
  3600. set_status:
  3601. if (status)
  3602. *status = cd->response_buf[5];
  3603. return rc;
  3604. }
  3605. /*******************************************************************************
  3606. * FUNCTION: pt_pip_get_data_structure
  3607. *
  3608. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3609. * an exclusive access to the DUT.
  3610. *
  3611. * RETURN:
  3612. * 0 = success
  3613. * !0 = failure
  3614. *
  3615. * PARAMETERS:
  3616. * *cd - pointer to core data
  3617. * read_offset - read pointer offset
  3618. * read_length - length of data to read
  3619. * data_id - data ID to read
  3620. * *status - pointer to store the read response status
  3621. * *data_format - pointer to store format of data read
  3622. * *actual_read_len - pointer to store data length actually read
  3623. * *data - pointer to store data read
  3624. ******************************************************************************/
  3625. static int pt_pip_get_data_structure(
  3626. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3627. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3628. u8 *data)
  3629. {
  3630. int rc;
  3631. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3632. if (rc < 0) {
  3633. pt_debug(cd->dev, DL_ERROR,
  3634. "%s: fail get exclusive ex=%p own=%p\n",
  3635. __func__, cd->exclusive_dev, cd->dev);
  3636. return rc;
  3637. }
  3638. rc = pt_pip_get_data_structure_(cd, read_offset,
  3639. read_length, data_id, status, data_format,
  3640. actual_read_len, data);
  3641. if (release_exclusive(cd, cd->dev) < 0)
  3642. pt_debug(cd->dev, DL_ERROR,
  3643. "%s: fail to release exclusive\n", __func__);
  3644. return rc;
  3645. }
  3646. /*******************************************************************************
  3647. * FUNCTION: _pt_request_pip_get_data_structure
  3648. *
  3649. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3650. * calls to the protected or unprotected call to
  3651. * pt_pip_get_data_structure
  3652. *
  3653. * RETURN:
  3654. * 0 = success
  3655. * !0 = failure
  3656. *
  3657. * PARAMETERS:
  3658. * *dev - pointer to device structure
  3659. * protect - flag to call protected or non-protected
  3660. * read_offset - read pointer offset
  3661. * read_length - length of data to read
  3662. * data_id - data ID to read
  3663. * *status - pointer to store the read response status
  3664. * *data_format - pointer to store format of data read
  3665. * *actual_read_len - pointer to store data length actually read
  3666. * *data - pointer to store data read
  3667. ******************************************************************************/
  3668. static int _pt_request_pip_get_data_structure(struct device *dev,
  3669. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3670. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3671. {
  3672. struct pt_core_data *cd = dev_get_drvdata(dev);
  3673. if (protect)
  3674. return pt_pip_get_data_structure(cd,
  3675. read_offset, read_length, data_id, status,
  3676. data_format, actual_read_len, data);
  3677. return pt_pip_get_data_structure_(cd,
  3678. read_offset, read_length, data_id, status,
  3679. data_format, actual_read_len, data);
  3680. }
  3681. /*******************************************************************************
  3682. * FUNCTION: _pt_manage_local_cal_data
  3683. *
  3684. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3685. * CALIBRATION data. It stores it in a local static array and can be
  3686. * cleared, loaded or used to restore the CAL data back to the running FW.
  3687. * The CAL data is read or restored by use of the PIP1 commands:
  3688. * - READ_DATA_BLOCK (0x22)
  3689. * - WRITE_DATA_BLOCK (0x23)
  3690. *
  3691. * RETURN:
  3692. * 0 = success
  3693. * !0 = failure
  3694. *
  3695. * PARAMETERS:
  3696. * *dev - pointer to device structure
  3697. * action - One of the following actions:
  3698. * - PT_CAL_DATA_SAVE
  3699. * - PT_CAL_DATA_RESTORE
  3700. * - PT_CAL_DATA_CLEAR
  3701. * - PT_CAL_DATA_SIZE
  3702. * *size - pointer to the number of bytes transferred
  3703. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3704. ******************************************************************************/
  3705. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3706. unsigned short *crc)
  3707. {
  3708. struct pt_core_data *cd = dev_get_drvdata(dev);
  3709. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3710. unsigned short calc_id_crc = 0;
  3711. static u8 *cal_cache_data;
  3712. static u16 cal_cache_len;
  3713. static unsigned short cal_cache_chip_id;
  3714. int rc = 0;
  3715. u8 *tmp_data = NULL;
  3716. u8 row_number = 0;
  3717. u8 prefix[20];
  3718. u16 cal_size = 0;
  3719. u16 transfer_size;
  3720. u16 act_trans_len = 0;
  3721. u16 byte_offset = 0;
  3722. u16 cal_blk_size;
  3723. u16 total_rows;
  3724. u16 remain_bytes;
  3725. u16 data_block_crc;
  3726. u16 buf_size = 12;
  3727. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3728. __func__, action);
  3729. switch (action) {
  3730. case PT_CAL_DATA_SAVE:
  3731. /* Read the size of the CAL block and calculate # rows */
  3732. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3733. if (!tmp_data) {
  3734. rc = -ENOMEM;
  3735. goto exit;
  3736. }
  3737. /*
  3738. * Don't check rc as doing a read size will give a false
  3739. * error on the CRC check.
  3740. */
  3741. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3742. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3743. cal_blk_size = act_trans_len;
  3744. kfree(tmp_data);
  3745. pt_debug(dev, DL_INFO,
  3746. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3747. __func__, cal_cache_len, cal_blk_size);
  3748. /* Safety net to ensure we didn't read incorrect size */
  3749. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3750. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3751. __func__);
  3752. rc = 1;
  3753. goto exit;
  3754. }
  3755. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3756. if (cal_blk_size != cal_cache_len) {
  3757. kfree(cal_cache_data);
  3758. cal_cache_data = kzalloc(cal_blk_size + 2,
  3759. GFP_KERNEL);
  3760. if (!cal_cache_data) {
  3761. rc = -ENOMEM;
  3762. goto exit;
  3763. }
  3764. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3765. __func__);
  3766. }
  3767. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3768. /* Calculate how many rows [0-n] (PIP Transactions) */
  3769. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3770. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3771. /* Add row if we have a last partial row */
  3772. if (remain_bytes > 0)
  3773. total_rows++;
  3774. pt_debug(dev, DL_INFO,
  3775. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3776. __func__, cal_blk_size, total_rows, remain_bytes);
  3777. /* Read all rows unless an error occurs */
  3778. rc = 0;
  3779. while (rc == 0 && row_number <= total_rows) {
  3780. act_trans_len = 0;
  3781. if (remain_bytes > 0 && row_number == total_rows)
  3782. transfer_size = remain_bytes;
  3783. else
  3784. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3785. rc = pt_pip1_read_data_block_(cd, row_number,
  3786. transfer_size, PT_CAL_EBID,
  3787. &act_trans_len,
  3788. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3789. &data_block_crc);
  3790. if (rc) {
  3791. /* Error occurred, exit loop */
  3792. cal_size = 0;
  3793. break;
  3794. }
  3795. pt_debug(dev, DL_INFO,
  3796. "%s: CAL read rc=%d actual read len=%d\n",
  3797. __func__, rc, act_trans_len);
  3798. byte_offset += act_trans_len;
  3799. cal_size = byte_offset;
  3800. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3801. pt_pr_buf(dev, DL_INFO,
  3802. &cal_cache_data[byte_offset - act_trans_len],
  3803. act_trans_len, prefix);
  3804. row_number++;
  3805. }
  3806. if (cal_size > 0) {
  3807. /* Save a CRC of the chip info the CAL was saved from */
  3808. calc_id_crc = crc_ccitt_calculate(
  3809. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3810. cal_cache_chip_id = calc_id_crc;
  3811. cal_cache_len = cal_size;
  3812. pt_debug(dev, DL_INFO,
  3813. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3814. __func__, calc_id_crc, cal_size);
  3815. }
  3816. *size = cal_size;
  3817. *crc = calc_id_crc;
  3818. break;
  3819. case PT_CAL_DATA_RESTORE:
  3820. cal_size = cal_cache_len;
  3821. while ((rc == 0) && (byte_offset < cal_size)) {
  3822. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3823. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3824. else
  3825. transfer_size = cal_size - byte_offset;
  3826. rc = pt_pip1_write_data_block_(cd, row_number,
  3827. transfer_size, PT_CAL_EBID,
  3828. &cal_cache_data[byte_offset],
  3829. (u8 *)pt_data_block_security_key,
  3830. &act_trans_len);
  3831. byte_offset += act_trans_len;
  3832. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3833. __func__, byte_offset);
  3834. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3835. pt_pr_buf(dev, DL_INFO,
  3836. &cal_cache_data[byte_offset - act_trans_len],
  3837. act_trans_len, prefix);
  3838. if ((byte_offset > cal_size) ||
  3839. (act_trans_len != transfer_size))
  3840. rc = -EIO;
  3841. row_number++;
  3842. }
  3843. *size = byte_offset;
  3844. *crc = cal_cache_chip_id;
  3845. break;
  3846. case PT_CAL_DATA_CLEAR:
  3847. if (cal_cache_data)
  3848. memset(&cal_cache_data[0], 0, cal_cache_len);
  3849. cal_cache_len = 0;
  3850. cal_cache_chip_id = 0;
  3851. *size = 0;
  3852. *crc = 0;
  3853. break;
  3854. case PT_CAL_DATA_INFO:
  3855. default:
  3856. *size = cal_cache_len;
  3857. *crc = cal_cache_chip_id;
  3858. pt_debug(dev, DL_INFO,
  3859. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3860. __func__, cal_cache_chip_id,
  3861. cal_cache_len);
  3862. break;
  3863. }
  3864. exit:
  3865. pt_debug(dev, DL_INFO,
  3866. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3867. __func__, rc, *crc, *size);
  3868. return rc;
  3869. }
  3870. /*******************************************************************************
  3871. * FUNCTION: pt_pip_run_selftest_
  3872. *
  3873. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3874. * to execute a FW built in self test
  3875. *
  3876. * RETURN:
  3877. * 0 = success
  3878. * !0 = failure
  3879. *
  3880. * PARAMETERS:
  3881. * *cd - pointer to core data
  3882. * test_id - enumerated test ID to run
  3883. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3884. * *status - pointer to store the read response status
  3885. * *summary_results - pointer to store the results summary
  3886. * *results_available - pointer to store if results are available
  3887. *****************************************************************************/
  3888. static int pt_pip_run_selftest_(
  3889. struct pt_core_data *cd, u8 test_id,
  3890. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3891. u8 *results_available)
  3892. {
  3893. int rc = 0;
  3894. u8 write_buf[2];
  3895. struct pt_hid_output hid_output = {
  3896. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3897. .write_length = 2,
  3898. .write_buf = write_buf,
  3899. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3900. };
  3901. write_buf[0] = test_id;
  3902. write_buf[1] = write_idacs_to_flash;
  3903. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3904. hid_output.write_length = 1;
  3905. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3906. if (rc)
  3907. return rc;
  3908. if (status)
  3909. *status = cd->response_buf[5];
  3910. if (summary_result)
  3911. *summary_result = cd->response_buf[6];
  3912. /* results_available only available before PIP 1.03 */
  3913. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3914. if (results_available)
  3915. *results_available = cd->response_buf[7];
  3916. }
  3917. return rc;
  3918. }
  3919. /*******************************************************************************
  3920. * FUNCTION: pt_pip_run_selftest
  3921. *
  3922. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3923. * an exclusive access to the DUT.
  3924. *
  3925. * RETURN:
  3926. * 0 = success
  3927. * !0 = failure
  3928. *
  3929. * PARAMETERS:
  3930. * *cd - pointer to core data
  3931. * test_id - enumerated test ID to run
  3932. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3933. * *status - pointer to store the read response status
  3934. * *summary_results - pointer to store the results summary
  3935. * *results_available - pointer to store if results are available
  3936. ******************************************************************************/
  3937. static int pt_pip_run_selftest(
  3938. struct pt_core_data *cd, u8 test_id,
  3939. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3940. u8 *results_available)
  3941. {
  3942. int rc;
  3943. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3944. if (rc < 0) {
  3945. pt_debug(cd->dev, DL_ERROR,
  3946. "%s: fail get exclusive ex=%p own=%p\n",
  3947. __func__, cd->exclusive_dev, cd->dev);
  3948. return rc;
  3949. }
  3950. rc = pt_pip_run_selftest_(cd, test_id,
  3951. write_idacs_to_flash, status, summary_result,
  3952. results_available);
  3953. if (release_exclusive(cd, cd->dev) < 0)
  3954. pt_debug(cd->dev, DL_ERROR,
  3955. "%s: fail to release exclusive\n", __func__);
  3956. return rc;
  3957. }
  3958. /*******************************************************************************
  3959. * FUNCTION: _pt_request_pip_run_selftest
  3960. *
  3961. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3962. * calls to the protected or unprotected call to pt_pip_run_selftest
  3963. *
  3964. * RETURN:
  3965. * 0 = success
  3966. * !0 = failure
  3967. *
  3968. * PARAMETERS:
  3969. * *dev - pointer to device structure
  3970. * protect - flag to call protected or non-protected
  3971. * test_id - enumerated test ID to run
  3972. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3973. * *status - pointer to store the read response status
  3974. * *summary_results - pointer to store the results summary
  3975. * *results_available - pointer to store if results are available
  3976. ******************************************************************************/
  3977. static int _pt_request_pip_run_selftest(struct device *dev,
  3978. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3979. u8 *summary_result, u8 *results_available)
  3980. {
  3981. struct pt_core_data *cd = dev_get_drvdata(dev);
  3982. if (protect)
  3983. return pt_pip_run_selftest(cd, test_id,
  3984. write_idacs_to_flash, status, summary_result,
  3985. results_available);
  3986. return pt_pip_run_selftest_(cd, test_id,
  3987. write_idacs_to_flash, status, summary_result,
  3988. results_available);
  3989. }
  3990. /*******************************************************************************
  3991. * FUNCTION: _pt_pip_get_selftest_result_
  3992. *
  3993. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3994. * to retrieve the self test results from the self test already executed
  3995. *
  3996. * RETURN:
  3997. * 0 = success
  3998. * !0 = failure
  3999. *
  4000. * PARAMETERS:
  4001. * *cd - pointer to core data
  4002. * read_offset - read pointer offset
  4003. * read_length - length of data to read
  4004. * test_id - enumerated test ID to read selftest results from
  4005. * *status - pointer to store the read response status
  4006. * *actual_read_len - pointer to store data length actually read
  4007. * *status - pointer to where the cmd response statas is stored
  4008. ******************************************************************************/
  4009. static int pt_pip_get_selftest_result_(
  4010. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4011. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4012. {
  4013. int rc = 0;
  4014. u16 total_read_len = 0;
  4015. u16 read_len;
  4016. u16 off_buf = 0;
  4017. u8 write_buf[5];
  4018. u8 read_test_id;
  4019. bool repeat;
  4020. struct pt_hid_output hid_output = {
  4021. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4022. .write_length = 5,
  4023. .write_buf = write_buf,
  4024. };
  4025. /*
  4026. * Do not repeat reading for Auto Shorts test
  4027. * when PIP version < 1.3
  4028. */
  4029. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4030. || test_id != PT_ST_ID_AUTOSHORTS;
  4031. again:
  4032. write_buf[0] = LOW_BYTE(read_offset);
  4033. write_buf[1] = HI_BYTE(read_offset);
  4034. write_buf[2] = LOW_BYTE(read_length);
  4035. write_buf[3] = HI_BYTE(read_length);
  4036. write_buf[4] = test_id;
  4037. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4038. if (rc)
  4039. return rc;
  4040. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4041. goto set_status;
  4042. read_test_id = cd->response_buf[6];
  4043. if (read_test_id != test_id)
  4044. return -EPROTO;
  4045. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4046. if (read_len && data) {
  4047. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4048. total_read_len += read_len;
  4049. if (repeat && read_len < read_length) {
  4050. read_offset += read_len;
  4051. off_buf += read_len;
  4052. read_length -= read_len;
  4053. goto again;
  4054. }
  4055. }
  4056. if (actual_read_len)
  4057. *actual_read_len = total_read_len;
  4058. set_status:
  4059. if (status)
  4060. *status = cd->response_buf[5];
  4061. return rc;
  4062. }
  4063. /*******************************************************************************
  4064. * FUNCTION: _pt_pip_get_selftest_result
  4065. *
  4066. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4067. * access to the DUT
  4068. *
  4069. * RETURN:
  4070. * 0 = success
  4071. * !0 = failure
  4072. *
  4073. * PARAMETERS:
  4074. * *cd - pointer to core data
  4075. * read_offset - read pointer offset
  4076. * read_length - length of data to read
  4077. * test_id - enumerated test ID to read selftest results from
  4078. * *status - pointer to store the read response status
  4079. * *actual_read_len - pointer to store data length actually read
  4080. * *status - pointer to where the cmd response statas is stored
  4081. ******************************************************************************/
  4082. static int pt_pip_get_selftest_result(
  4083. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4084. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4085. {
  4086. int rc;
  4087. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4088. if (rc < 0) {
  4089. pt_debug(cd->dev, DL_ERROR,
  4090. "%s: fail get exclusive ex=%p own=%p\n",
  4091. __func__, cd->exclusive_dev, cd->dev);
  4092. return rc;
  4093. }
  4094. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4095. read_length, test_id, status, actual_read_len, data);
  4096. if (release_exclusive(cd, cd->dev) < 0)
  4097. pt_debug(cd->dev, DL_ERROR,
  4098. "%s: fail to release exclusive\n", __func__);
  4099. return rc;
  4100. }
  4101. /*******************************************************************************
  4102. * FUNCTION: _pt_request_pip_get_selftest_result
  4103. *
  4104. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4105. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4106. *
  4107. * RETURN:
  4108. * 0 = success
  4109. * !0 = failure
  4110. *
  4111. * PARAMETERS:
  4112. * *dev - pointer to device structure
  4113. * protect - flag to call protected or non-protected
  4114. * read_offset - read pointer offset
  4115. * read_length - length of data to read
  4116. * test_id - enumerated test ID to read selftest results from
  4117. * *status - pointer to store the read response status
  4118. * *actual_read_len - pointer to store data length actually read
  4119. * *data - pointer to where the data read is stored
  4120. ******************************************************************************/
  4121. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4122. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4123. u8 *status, u16 *actual_read_len, u8 *data)
  4124. {
  4125. struct pt_core_data *cd = dev_get_drvdata(dev);
  4126. if (protect)
  4127. return pt_pip_get_selftest_result(cd, read_offset,
  4128. read_length, test_id, status, actual_read_len,
  4129. data);
  4130. return pt_pip_get_selftest_result_(cd, read_offset,
  4131. read_length, test_id, status, actual_read_len,
  4132. data);
  4133. }
  4134. /*******************************************************************************
  4135. * FUNCTION: _pt_pip_load_self_test_param
  4136. *
  4137. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4138. * to load parameters needed by a self test
  4139. *
  4140. * RETURN:
  4141. * 0 = success
  4142. * !0 = failure
  4143. *
  4144. * PARAMETERS:
  4145. * *cd - pointer to core data
  4146. * self_test_id - enumerated test ID for which the parmeters belong
  4147. * load_offset - mem offset to where to load parameters
  4148. * load_length - length of parameter data to load
  4149. * *parameters - pointer to list of parameter data
  4150. * *status - pointer to store the response status
  4151. * *ret_test_id - pointer to returned test id the parameters were stored
  4152. * *act_load_len - pointer to store the actual load length that was writen
  4153. ******************************************************************************/
  4154. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4155. u8 self_test_id, u16 load_offset, u16 load_length,
  4156. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4157. {
  4158. int rc = 0;
  4159. int i;
  4160. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4161. struct pt_hid_output hid_output = {
  4162. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4163. .write_length = 5 + load_length,
  4164. .write_buf = write_buf,
  4165. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4166. };
  4167. write_buf[0] = LOW_BYTE(load_offset);
  4168. write_buf[1] = HI_BYTE(load_offset);
  4169. write_buf[2] = LOW_BYTE(load_length);
  4170. write_buf[3] = HI_BYTE(load_length);
  4171. write_buf[4] = self_test_id;
  4172. for (i = 0; i < load_length; i++)
  4173. write_buf[i + 5] = parameters[i];
  4174. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4175. if (rc)
  4176. return rc;
  4177. if (status)
  4178. *status = cd->response_buf[5];
  4179. if (ret_test_id)
  4180. *ret_test_id = cd->response_buf[6];
  4181. if (act_load_len)
  4182. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4183. return rc;
  4184. }
  4185. /*******************************************************************************
  4186. * FUNCTION: pt_pip_load_self_test_param
  4187. *
  4188. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4189. * access to the DUT.
  4190. *
  4191. * RETURN:
  4192. * 0 = success
  4193. * !0 = failure
  4194. *
  4195. * PARAMETERS:
  4196. * *cd - pointer to core data
  4197. * self_test_id - enumerated test ID for which the parmeters belong
  4198. * load_offset - mem offset to where to load parameters
  4199. * load_length - length of parameter data to load
  4200. * *parameters - pointer to list of parameter data
  4201. * *status - pointer to store the response status
  4202. * *ret_test_id - pointer to returned test id the parameters were stored
  4203. * *act_load_len - pointer to store the actual load length that was writen
  4204. ******************************************************************************/
  4205. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4206. u8 self_test_id, u16 load_offset, u16 load_length,
  4207. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4208. {
  4209. int rc;
  4210. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4211. if (rc < 0) {
  4212. pt_debug(cd->dev, DL_ERROR,
  4213. "%s: fail get exclusive ex=%p own=%p\n",
  4214. __func__, cd->exclusive_dev, cd->dev);
  4215. return rc;
  4216. }
  4217. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4218. load_length, parameters, status, ret_test_id, act_load_len);
  4219. if (release_exclusive(cd, cd->dev) < 0)
  4220. pt_debug(cd->dev, DL_ERROR,
  4221. "%s: fail to release exclusive\n", __func__);
  4222. return rc;
  4223. }
  4224. /*******************************************************************************
  4225. * FUNCTION: _pt_request_pip_load_self_test_param
  4226. *
  4227. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4228. * calls to the protected or unprotected call to
  4229. * pt_pip_load_self_test_param
  4230. *
  4231. * RETURN:
  4232. * 0 = success
  4233. * !0 = failure
  4234. *
  4235. * PARAMETERS:
  4236. * *dev - pointer to device structure
  4237. * protect - flag to call protected or non-protected
  4238. * self_test_id - enumerated test ID for which the parmeters belong
  4239. * load_offset - mem offset to where to load parameters
  4240. * load_length - length of parameter data to load
  4241. * *parameters - pointer to list of parameter data
  4242. * *status - pointer to store the response status
  4243. * *ret_test_id - pointer to returned test id the parameters were stored
  4244. * *act_load_len - pointer to store the actual load length that was writen
  4245. ******************************************************************************/
  4246. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4247. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4248. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4249. {
  4250. struct pt_core_data *cd = dev_get_drvdata(dev);
  4251. if (protect)
  4252. return pt_pip_load_self_test_param(cd, self_test_id,
  4253. load_offset, load_length, parameters, status, ret_test_id,
  4254. act_load_len);
  4255. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4256. load_length, parameters, status, ret_test_id, act_load_len);
  4257. }
  4258. /*******************************************************************************
  4259. * FUNCTION: pt_pip_calibrate_ext_
  4260. *
  4261. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4262. * for the response
  4263. *
  4264. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4265. *
  4266. * RETURN:
  4267. * 0 = success
  4268. * !0 = failure
  4269. *
  4270. * PARAMETERS:
  4271. * *cd - pointer to core data
  4272. * *cal_data - pointer to extended calibration data structure
  4273. * *status - pointer to where the command response status is stored
  4274. ******************************************************************************/
  4275. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4276. struct pt_cal_ext_data *cal_data, u8 *status)
  4277. {
  4278. int rc = 0;
  4279. int write_length = 4;
  4280. u8 write_buf[4];
  4281. u16 size = 0;
  4282. unsigned short crc = 0;
  4283. struct pt_hid_output hid_output = {
  4284. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4285. .write_length = write_length,
  4286. .write_buf = write_buf,
  4287. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4288. };
  4289. if (cal_data == NULL)
  4290. return -EINVAL;
  4291. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4292. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4293. if (rc)
  4294. return rc;
  4295. if (status)
  4296. *status = cd->response_buf[5];
  4297. /*
  4298. * When doing a calibration on a flashless DUT, save CAL data in
  4299. * the TTDL cache on any successful calibration
  4300. */
  4301. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4302. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4303. __func__);
  4304. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4305. &size, &crc);
  4306. if (rc)
  4307. pt_debug(cd->dev, DL_ERROR,
  4308. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4309. else
  4310. pt_debug(cd->dev, DL_INFO,
  4311. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4312. __func__, crc, size);
  4313. }
  4314. return 0;
  4315. }
  4316. /*******************************************************************************
  4317. * FUNCTION: pt_pip_calibrate_ext
  4318. *
  4319. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4320. * DUT
  4321. *
  4322. * RETURN:
  4323. * 0 = success
  4324. * !0 = failure
  4325. *
  4326. * PARAMETERS:
  4327. * *cd - pointer to core data
  4328. * *cal_data - pointer to extended calibration data structure
  4329. * *status - pointer to where the command response status is stored
  4330. ******************************************************************************/
  4331. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4332. struct pt_cal_ext_data *cal_data, u8 *status)
  4333. {
  4334. int rc;
  4335. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4336. if (rc < 0) {
  4337. pt_debug(cd->dev, DL_ERROR,
  4338. "%s: fail get exclusive ex=%p own=%p\n",
  4339. __func__, cd->exclusive_dev, cd->dev);
  4340. return rc;
  4341. }
  4342. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4343. if (release_exclusive(cd, cd->dev) < 0)
  4344. pt_debug(cd->dev, DL_ERROR,
  4345. "%s: fail to release exclusive\n", __func__);
  4346. return rc;
  4347. }
  4348. /*******************************************************************************
  4349. * FUNCTION: _pt_request_pip_calibrate_ext
  4350. *
  4351. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4352. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4353. *
  4354. * RETURN:
  4355. * 0 = success
  4356. * !0 = failure
  4357. *
  4358. * PARAMETERS:
  4359. * *dev - pointer to device structure
  4360. * protect - flag to call protected or non-protected
  4361. * *cal_data - pointer to extended calibration data structure
  4362. * *status - pointer to where the command response status is stored
  4363. ******************************************************************************/
  4364. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4365. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4366. {
  4367. struct pt_core_data *cd = dev_get_drvdata(dev);
  4368. if (protect)
  4369. return pt_pip_calibrate_ext(cd, cal_data, status);
  4370. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4371. }
  4372. /*******************************************************************************
  4373. * FUNCTION: pt_pip_calibrate_idacs_
  4374. *
  4375. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4376. * for the response
  4377. *
  4378. * RETURN:
  4379. * 0 = success
  4380. * !0 = failure
  4381. *
  4382. * PARAMETERS:
  4383. * *cd - pointer to core data
  4384. * mode - sense mode to calibrate (0-5)
  4385. * *status - pointer to where the command response status is stored
  4386. ******************************************************************************/
  4387. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4388. u8 mode, u8 *status)
  4389. {
  4390. int rc = 0;
  4391. int write_length = 1;
  4392. u8 write_buf[1];
  4393. u8 cmd_offset = 0;
  4394. struct pt_hid_output hid_output = {
  4395. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4396. .write_length = write_length,
  4397. .write_buf = write_buf,
  4398. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4399. };
  4400. write_buf[cmd_offset++] = mode;
  4401. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4402. if (rc)
  4403. return rc;
  4404. *status = cd->response_buf[5];
  4405. if (*status)
  4406. return -EINVAL;
  4407. return 0;
  4408. }
  4409. /*******************************************************************************
  4410. * FUNCTION: pt_pip_calibrate_idacs
  4411. *
  4412. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4413. * access to the DUT
  4414. *
  4415. * RETURN:
  4416. * 0 = success
  4417. * !0 = failure
  4418. *
  4419. * PARAMETERS:
  4420. * *cd - pointer to core data
  4421. * mode - sense mode to calibrate (0-5)
  4422. * *status - pointer to where the command response status is stored
  4423. ******************************************************************************/
  4424. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4425. u8 mode, u8 *status)
  4426. {
  4427. int rc;
  4428. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4429. if (rc < 0) {
  4430. pt_debug(cd->dev, DL_ERROR,
  4431. "%s: fail get exclusive ex=%p own=%p\n",
  4432. __func__, cd->exclusive_dev, cd->dev);
  4433. return rc;
  4434. }
  4435. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4436. if (release_exclusive(cd, cd->dev) < 0)
  4437. pt_debug(cd->dev, DL_ERROR,
  4438. "%s: fail to release exclusive\n", __func__);
  4439. return rc;
  4440. }
  4441. /*******************************************************************************
  4442. * FUNCTION: _pt_request_pip_calibrate_idacs
  4443. *
  4444. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4445. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4446. *
  4447. * RETURN:
  4448. * 0 = success
  4449. * !0 = failure
  4450. *
  4451. * PARAMETERS:
  4452. * *dev - pointer to device structure
  4453. * protect - flag to call protected or non-protected
  4454. * mode - sense mode to calibrate (0-5)
  4455. * *status - pointer to where the command response status is stored
  4456. ******************************************************************************/
  4457. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4458. int protect, u8 mode, u8 *status)
  4459. {
  4460. struct pt_core_data *cd = dev_get_drvdata(dev);
  4461. if (protect)
  4462. return pt_pip_calibrate_idacs(cd, mode, status);
  4463. return pt_pip_calibrate_idacs_(cd, mode, status);
  4464. }
  4465. /*******************************************************************************
  4466. * FUNCTION: pt_hid_output_initialize_baselines_
  4467. *
  4468. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4469. * waiting for the response.
  4470. *
  4471. * RETURN:
  4472. * 0 = success
  4473. * !0 = failure
  4474. *
  4475. * PARAMETERS:
  4476. * *cd - pointer to core data
  4477. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4478. * each or together with a single command.
  4479. * *status - pointer to where the command response status is stored
  4480. ******************************************************************************/
  4481. static int pt_hid_output_initialize_baselines_(
  4482. struct pt_core_data *cd, u8 test_id, u8 *status)
  4483. {
  4484. int rc = 0;
  4485. int write_length = 1;
  4486. u8 write_buf[1];
  4487. u8 cmd_offset = 0;
  4488. struct pt_hid_output hid_output = {
  4489. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4490. .write_length = write_length,
  4491. .write_buf = write_buf,
  4492. };
  4493. write_buf[cmd_offset++] = test_id;
  4494. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4495. if (rc)
  4496. return rc;
  4497. *status = cd->response_buf[5];
  4498. if (*status)
  4499. return -EINVAL;
  4500. return rc;
  4501. }
  4502. /*******************************************************************************
  4503. * FUNCTION: pt_hid_output_initialize_baselines
  4504. *
  4505. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4506. * access to the DUT
  4507. *
  4508. * RETURN:
  4509. * 0 = success
  4510. * !0 = failure
  4511. *
  4512. * PARAMETERS:
  4513. * *cd - pointer to core data
  4514. * test_id - enumerated ID against which to initialize the baseline
  4515. * *status - pointer to where the command response status is stored
  4516. ******************************************************************************/
  4517. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4518. u8 test_id, u8 *status)
  4519. {
  4520. int rc;
  4521. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4522. if (rc < 0) {
  4523. pt_debug(cd->dev, DL_ERROR,
  4524. "%s: fail get exclusive ex=%p own=%p\n",
  4525. __func__, cd->exclusive_dev, cd->dev);
  4526. return rc;
  4527. }
  4528. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4529. if (release_exclusive(cd, cd->dev) < 0)
  4530. pt_debug(cd->dev, DL_ERROR,
  4531. "%s: fail to release exclusive\n", __func__);
  4532. return rc;
  4533. }
  4534. /*******************************************************************************
  4535. * FUNCTION: _pt_request_pip_initialize_baselines
  4536. *
  4537. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4538. * calls to the protected or unprotected call to
  4539. * pt_pip_initialize_baselines
  4540. *
  4541. * RETURN:
  4542. * 0 = success
  4543. * !0 = failure
  4544. *
  4545. * PARAMETERS:
  4546. * *dev - pointer to device structure
  4547. * protect - flag to call protected or non-protected
  4548. * test_id - enumerated ID against which to initialize the baseline
  4549. * *status - pointer to where the command response status is stored
  4550. ******************************************************************************/
  4551. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4552. int protect, u8 test_id, u8 *status)
  4553. {
  4554. struct pt_core_data *cd = dev_get_drvdata(dev);
  4555. if (protect)
  4556. return pt_hid_output_initialize_baselines(cd, test_id,
  4557. status);
  4558. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4559. }
  4560. /*******************************************************************************
  4561. * FUNCTION: pt_hid_output_exec_panel_scan_
  4562. *
  4563. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4564. * the response
  4565. *
  4566. * RETURN:
  4567. * 0 = success
  4568. * !0 = failure
  4569. *
  4570. * PARAMETERS:
  4571. * *cd - pointer to core data
  4572. ******************************************************************************/
  4573. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4574. {
  4575. struct pt_hid_output hid_output = {
  4576. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4577. };
  4578. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4579. }
  4580. /*******************************************************************************
  4581. * FUNCTION: pt_hid_output_exec_panel_scan
  4582. *
  4583. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4584. * access to the DUT.
  4585. *
  4586. * RETURN:
  4587. * 0 = success
  4588. * !0 = failure
  4589. *
  4590. * PARAMETERS:
  4591. * *cd - pointer to core data
  4592. ******************************************************************************/
  4593. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4594. {
  4595. int rc;
  4596. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4597. if (rc < 0) {
  4598. pt_debug(cd->dev, DL_ERROR,
  4599. "%s: fail get exclusive ex=%p own=%p\n",
  4600. __func__, cd->exclusive_dev, cd->dev);
  4601. return rc;
  4602. }
  4603. rc = pt_hid_output_exec_panel_scan_(cd);
  4604. if (release_exclusive(cd, cd->dev) < 0)
  4605. pt_debug(cd->dev, DL_ERROR,
  4606. "%s: fail to release exclusive\n", __func__);
  4607. return rc;
  4608. }
  4609. /*******************************************************************************
  4610. * FUNCTION: pt_pip2_exec_panel_scan_
  4611. *
  4612. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4613. * the response
  4614. *
  4615. * RETURN:
  4616. * 0 = success
  4617. * !0 = failure
  4618. *
  4619. * PARAMETERS:
  4620. * *cd - pointer to core data
  4621. * scan_type - type of panel scan to perform (PIP2 only)
  4622. ******************************************************************************/
  4623. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4624. {
  4625. int rc = 0;
  4626. u8 data[2];
  4627. u8 read_buf[10];
  4628. u16 actual_read_len;
  4629. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4630. __func__, scan_type);
  4631. data[0] = scan_type;
  4632. rc = _pt_request_pip2_send_cmd(cd->dev,
  4633. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4634. data, 1, read_buf, &actual_read_len);
  4635. if (rc) {
  4636. pt_debug(cd->dev, DL_ERROR,
  4637. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4638. __func__, scan_type, rc);
  4639. }
  4640. return rc;
  4641. }
  4642. /*******************************************************************************
  4643. * FUNCTION: pt_pip2_exec_panel_scan
  4644. *
  4645. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4646. * access to the DUT.
  4647. *
  4648. * RETURN:
  4649. * 0 = success
  4650. * !0 = failure
  4651. *
  4652. * PARAMETERS:
  4653. * *cd - pointer to core data
  4654. * scan_type - type of panel scan to perform (PIP2 only)
  4655. ******************************************************************************/
  4656. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4657. {
  4658. int rc;
  4659. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4660. if (rc < 0) {
  4661. pt_debug(cd->dev, DL_ERROR,
  4662. "%s: fail get exclusive ex=%p own=%p\n",
  4663. __func__, cd->exclusive_dev, cd->dev);
  4664. return rc;
  4665. }
  4666. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4667. if (release_exclusive(cd, cd->dev) < 0)
  4668. pt_debug(cd->dev, DL_ERROR,
  4669. "%s: fail to release exclusive\n", __func__);
  4670. return rc;
  4671. }
  4672. /*******************************************************************************
  4673. * FUNCTION: _pt_request_pip_exec_panel_scan
  4674. *
  4675. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4676. * calls to the protected or unprotected call to
  4677. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4678. *
  4679. * RETURN:
  4680. * 0 = success
  4681. * !0 = failure
  4682. *
  4683. * PARAMETERS:
  4684. * *dev - pointer to device structure
  4685. * protect - flag to call protected or non-protected
  4686. * scan_type - type of panel scan to perform (PIP2 only)
  4687. ******************************************************************************/
  4688. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4689. int protect, u8 scan_type)
  4690. {
  4691. struct pt_core_data *cd = dev_get_drvdata(dev);
  4692. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4693. if (protect)
  4694. return pt_pip2_exec_panel_scan(cd, scan_type);
  4695. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4696. }
  4697. if (protect)
  4698. return pt_hid_output_exec_panel_scan(cd);
  4699. return pt_hid_output_exec_panel_scan_(cd);
  4700. }
  4701. /*******************************************************************************
  4702. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4703. *
  4704. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4705. * to retrieve the specified data type for a the last successful Execute
  4706. * Panel Scan command.
  4707. *
  4708. * RETURN:
  4709. * 0 = success
  4710. * !0 = failure
  4711. *
  4712. * PARAMETERS:
  4713. * *dev - pointer to device structure
  4714. * protect - flag to call protected or non-protected
  4715. * read_offset - read pointer offset
  4716. * read_count - length of data to read
  4717. * data_id - enumerated test ID to read selftest results from
  4718. * *response - pointer to store the read response status
  4719. * *config - pointer to store config data
  4720. * *actual_read_len - pointer to store data length actually read
  4721. * *read_buf - pointer to the read buffer
  4722. ******************************************************************************/
  4723. static int pt_hid_output_retrieve_panel_scan_(
  4724. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4725. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4726. u8 *read_buf)
  4727. {
  4728. int status;
  4729. u8 read_data_id;
  4730. int rc = 0;
  4731. int write_length = 5;
  4732. u8 write_buf[5];
  4733. u8 cmd_offset = 0;
  4734. u8 data_elem_size;
  4735. int size;
  4736. int data_size;
  4737. struct pt_hid_output hid_output = {
  4738. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4739. .write_length = write_length,
  4740. .write_buf = write_buf,
  4741. };
  4742. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4743. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4744. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4745. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4746. write_buf[cmd_offset++] = data_id;
  4747. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4748. if (rc)
  4749. return rc;
  4750. status = cd->response_buf[5];
  4751. if (status)
  4752. return -EINVAL;
  4753. read_data_id = cd->response_buf[6];
  4754. if (read_data_id != data_id)
  4755. return -EPROTO;
  4756. size = get_unaligned_le16(&cd->response_buf[0]);
  4757. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4758. *config = cd->response_buf[9];
  4759. data_elem_size = *config & 0x07;
  4760. data_size = *actual_read_len * data_elem_size;
  4761. if (read_buf)
  4762. memcpy(read_buf, &cd->response_buf[10], data_size);
  4763. if (response)
  4764. memcpy(response, cd->response_buf, size);
  4765. return rc;
  4766. }
  4767. /*******************************************************************************
  4768. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4769. *
  4770. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4771. * access to the DUT.
  4772. *
  4773. * RETURN:
  4774. * 0 = success
  4775. * !0 = failure
  4776. *
  4777. * PARAMETERS:
  4778. * *dev - pointer to device structure
  4779. * protect - flag to call protected or non-protected
  4780. * read_offset - read pointer offset
  4781. * read_count - length of data to read
  4782. * data_id - enumerated test ID to read selftest results from
  4783. * *response - pointer to store the read response status
  4784. * *config - pointer to store config data
  4785. * *actual_read_len - pointer to store data length actually read
  4786. * *read_buf - pointer to the read buffer
  4787. ******************************************************************************/
  4788. static int pt_hid_output_retrieve_panel_scan(
  4789. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4790. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4791. u8 *read_buf)
  4792. {
  4793. int rc;
  4794. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4795. if (rc < 0) {
  4796. pt_debug(cd->dev, DL_ERROR,
  4797. "%s: fail get exclusive ex=%p own=%p\n",
  4798. __func__, cd->exclusive_dev, cd->dev);
  4799. return rc;
  4800. }
  4801. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4802. read_count, data_id, response, config,
  4803. actual_read_len, read_buf);
  4804. if (release_exclusive(cd, cd->dev) < 0)
  4805. pt_debug(cd->dev, DL_ERROR,
  4806. "%s: fail to release exclusive\n", __func__);
  4807. return rc;
  4808. }
  4809. /*******************************************************************************
  4810. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4811. *
  4812. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4813. * calls to the protected or unprotected call to
  4814. * pt_hid_output_retrieve_panel_scan
  4815. *
  4816. * RETURN:
  4817. * 0 = success
  4818. * !0 = failure
  4819. *
  4820. * PARAMETERS:
  4821. * *dev - pointer to device structure
  4822. * protect - flag to call protected or non-protected
  4823. * read_offset - read pointer offset
  4824. * read_count - length of data to read
  4825. * data_id - enumerated test ID to read selftest results from
  4826. * *response - pointer to store the read response status
  4827. * *config - pointer to store config data
  4828. * *actual_read_len - pointer to store data length actually read
  4829. * *read_buf - pointer to the read buffer
  4830. ******************************************************************************/
  4831. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4832. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4833. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4834. {
  4835. struct pt_core_data *cd = dev_get_drvdata(dev);
  4836. if (protect)
  4837. return pt_hid_output_retrieve_panel_scan(cd,
  4838. read_offset, read_count, data_id, response,
  4839. config, actual_read_len, read_buf);
  4840. return pt_hid_output_retrieve_panel_scan_(cd,
  4841. read_offset, read_count, data_id, response,
  4842. config, actual_read_len, read_buf);
  4843. }
  4844. /*******************************************************************************
  4845. * FUNCTION: _pt_request_pip_user_cmd
  4846. *
  4847. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4848. * calls to the protected or unprotected call to
  4849. * pt_hid_output_user_cmd
  4850. *
  4851. * RETURN:
  4852. * 0 = success
  4853. * !0 = failure
  4854. *
  4855. * PARAMETERS:
  4856. * *dev - pointer to device structure
  4857. * protect - flag to call protected or non-protected
  4858. * read_len - length of data to read
  4859. * *read_buf - pointer to store read data
  4860. * write_len - length of data to write
  4861. * *write_buf - pointer to buffer to write
  4862. * *actual_read_len - pointer to store data length actually read
  4863. ******************************************************************************/
  4864. static int _pt_request_pip_user_cmd(struct device *dev,
  4865. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4866. u8 *write_buf, u16 *actual_read_len)
  4867. {
  4868. struct pt_core_data *cd = dev_get_drvdata(dev);
  4869. if (protect)
  4870. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4871. write_len, write_buf, actual_read_len);
  4872. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4873. write_len, write_buf, actual_read_len);
  4874. }
  4875. /*******************************************************************************
  4876. * FUNCTION: pt_hid_output_bl_get_information_
  4877. *
  4878. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4879. * DUT to retrieve bootloader version and chip identification information.
  4880. *
  4881. * RETURN:
  4882. * 0 = success
  4883. * !0 = failure
  4884. *
  4885. * PARAMETERS:
  4886. * *cd - pointer to core data
  4887. * *return_data - pointer to store the return data
  4888. *****************************************************************************/
  4889. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4890. u8 *return_data)
  4891. {
  4892. int rc;
  4893. int data_len;
  4894. struct pt_hid_output hid_output = {
  4895. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4896. };
  4897. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4898. if (rc)
  4899. return rc;
  4900. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4901. if (!data_len)
  4902. return -EPROTO;
  4903. memcpy(return_data, &cd->response_buf[8], data_len);
  4904. return 0;
  4905. }
  4906. /*******************************************************************************
  4907. * FUNCTION: pt_hid_output_bl_get_information
  4908. *
  4909. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4910. * access to the DUT.
  4911. *
  4912. * RETURN:
  4913. * 0 = success
  4914. * !0 = failure
  4915. *
  4916. * PARAMETERS:
  4917. * *cd - pointer to core data
  4918. * *return_data - pointer to store the return data
  4919. ******************************************************************************/
  4920. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4921. u8 *return_data)
  4922. {
  4923. int rc;
  4924. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4925. if (rc < 0) {
  4926. pt_debug(cd->dev, DL_ERROR,
  4927. "%s: fail get exclusive ex=%p own=%p\n",
  4928. __func__, cd->exclusive_dev, cd->dev);
  4929. return rc;
  4930. }
  4931. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4932. if (release_exclusive(cd, cd->dev) < 0)
  4933. pt_debug(cd->dev, DL_ERROR,
  4934. "%s: fail to release exclusive\n", __func__);
  4935. return rc;
  4936. }
  4937. /*******************************************************************************
  4938. * FUNCTION: _pt_request_pip_bl_get_information
  4939. *
  4940. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4941. * calls to the protected or unprotected call to
  4942. * pt_hid_output_bl_get_information
  4943. *
  4944. * RETURN:
  4945. * 0 = success
  4946. * !0 = failure
  4947. *
  4948. * PARAMETERS:
  4949. * *dev - pointer to device structure
  4950. * protect - flag to call protected or non-protected
  4951. * *return_data - pointer to store bl data
  4952. ******************************************************************************/
  4953. static int _pt_request_pip_bl_get_information(struct device *dev,
  4954. int protect, u8 *return_data)
  4955. {
  4956. struct pt_core_data *cd = dev_get_drvdata(dev);
  4957. if (protect)
  4958. return pt_hid_output_bl_get_information(cd, return_data);
  4959. return pt_hid_output_bl_get_information_(cd, return_data);
  4960. }
  4961. /*******************************************************************************
  4962. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4963. *
  4964. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4965. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4966. * and Design Data block in flash and enables the host to execute the Program
  4967. * and Verify Row command to bootload the application image and data.
  4968. *
  4969. * RETURN:
  4970. * 0 = success
  4971. * !0 = failure
  4972. *
  4973. * PARAMETERS:
  4974. * *cd - pointer to core data
  4975. * protect - flag to call protected or non-protected
  4976. * key_size - size of key
  4977. * *key_buf - pointer to key data to allow operation
  4978. * row_size - size of the meta data row
  4979. * *metadata_row_buf - pointer to meta data to write
  4980. ******************************************************************************/
  4981. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4982. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4983. {
  4984. u16 write_length = key_size + row_size;
  4985. u8 *write_buf;
  4986. int rc = 0;
  4987. struct pt_hid_output hid_output = {
  4988. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4989. .write_length = write_length,
  4990. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4991. };
  4992. write_buf = kzalloc(write_length, GFP_KERNEL);
  4993. if (!write_buf)
  4994. return -ENOMEM;
  4995. hid_output.write_buf = write_buf;
  4996. if (key_size)
  4997. memcpy(write_buf, key_buf, key_size);
  4998. if (row_size)
  4999. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5000. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5001. kfree(write_buf);
  5002. return rc;
  5003. }
  5004. /*******************************************************************************
  5005. * FUNCTION: pt_hid_output_bl_initiate_bl
  5006. *
  5007. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5008. * access to the DUT.
  5009. *
  5010. * RETURN:
  5011. * 0 = success
  5012. * !0 = failure
  5013. *
  5014. * PARAMETERS:
  5015. * *cd - pointer to core data
  5016. * key_size - size of key
  5017. * *key_buf - pointer to key data to allow operation
  5018. * row_size - size of the meta data row
  5019. * *metadata_row_buf - pointer to meta data to write
  5020. ******************************************************************************/
  5021. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5022. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5023. {
  5024. int rc;
  5025. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5026. if (rc < 0) {
  5027. pt_debug(cd->dev, DL_ERROR,
  5028. "%s: fail get exclusive ex=%p own=%p\n",
  5029. __func__, cd->exclusive_dev, cd->dev);
  5030. return rc;
  5031. }
  5032. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5033. row_size, metadata_row_buf);
  5034. if (release_exclusive(cd, cd->dev) < 0)
  5035. pt_debug(cd->dev, DL_ERROR,
  5036. "%s: fail to release exclusive\n", __func__);
  5037. return rc;
  5038. }
  5039. /*******************************************************************************
  5040. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5041. *
  5042. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5043. * calls to the protected or unprotected call to
  5044. * pt_hid_output_bl_initiate_bl
  5045. *
  5046. * RETURN:
  5047. * 0 = success
  5048. * !0 = failure
  5049. *
  5050. * PARAMETERS:
  5051. * *dev - pointer to device structure
  5052. * protect - flag to call protected or non-protected
  5053. * key_size - size of key
  5054. * *key_buf - pointer to key data to allow operation
  5055. * row_size - size of the meta data row
  5056. * *metadata_row_buf - pointer to meta data to write
  5057. ******************************************************************************/
  5058. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5059. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5060. u8 *metadata_row_buf)
  5061. {
  5062. struct pt_core_data *cd = dev_get_drvdata(dev);
  5063. if (protect)
  5064. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5065. row_size, metadata_row_buf);
  5066. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5067. row_size, metadata_row_buf);
  5068. }
  5069. /*******************************************************************************
  5070. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5071. *
  5072. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5073. * and program a 128-byte row into the flash, and then verifies written data.
  5074. *
  5075. * RETURN:
  5076. * 0 = success
  5077. * !0 = failure
  5078. *
  5079. * PARAMETERS:
  5080. * *cd - pointer to core data
  5081. * data_len - length of data_buf
  5082. * *data_buf - firmware image to program
  5083. ******************************************************************************/
  5084. static int pt_hid_output_bl_program_and_verify_(
  5085. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5086. {
  5087. struct pt_hid_output hid_output = {
  5088. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5089. .write_length = data_len,
  5090. .write_buf = data_buf,
  5091. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5092. };
  5093. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5094. }
  5095. /*******************************************************************************
  5096. * FUNCTION: pt_hid_output_bl_program_and_verify
  5097. *
  5098. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5099. * access to the DUT.
  5100. *
  5101. * RETURN:
  5102. * 0 = success
  5103. * !0 = failure
  5104. *
  5105. * PARAMETERS:
  5106. * *cd - pointer to core data
  5107. * data_len - length of data_buf
  5108. * *data_buf - firmware image to program
  5109. ******************************************************************************/
  5110. static int pt_hid_output_bl_program_and_verify(
  5111. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5112. {
  5113. int rc;
  5114. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5115. if (rc < 0) {
  5116. pt_debug(cd->dev, DL_ERROR,
  5117. "%s: fail get exclusive ex=%p own=%p\n",
  5118. __func__, cd->exclusive_dev, cd->dev);
  5119. return rc;
  5120. }
  5121. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5122. if (release_exclusive(cd, cd->dev) < 0)
  5123. pt_debug(cd->dev, DL_ERROR,
  5124. "%s: fail to release exclusive\n", __func__);
  5125. return rc;
  5126. }
  5127. /*******************************************************************************
  5128. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5129. *
  5130. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5131. * to request to have the BL program and verify a FW image
  5132. *
  5133. * RETURN:
  5134. * 0 = success
  5135. * !0 = failure
  5136. *
  5137. * PARAMETERS:
  5138. * *dev - pointer to device structure
  5139. * protect - boolean to determine to call the protected function
  5140. * data_len - length of data_buf
  5141. * *data_buf - firmware image to program
  5142. ******************************************************************************/
  5143. static int _pt_request_pip_bl_program_and_verify(
  5144. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5145. {
  5146. struct pt_core_data *cd = dev_get_drvdata(dev);
  5147. if (protect)
  5148. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5149. data_buf);
  5150. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5151. data_buf);
  5152. }
  5153. /*******************************************************************************
  5154. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5155. *
  5156. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5157. * perform a full verification of the application integrity by calculating the
  5158. * CRC of the image in flash and compare it to the expected CRC stored in the
  5159. * Metadata row.
  5160. *
  5161. * RETURN:
  5162. * 0 = success
  5163. * !0 = failure
  5164. *
  5165. * PARAMETERS:
  5166. * *cd - pointer to core data
  5167. * *result - pointer to store result
  5168. ******************************************************************************/
  5169. static int pt_hid_output_bl_verify_app_integrity_(
  5170. struct pt_core_data *cd, u8 *result)
  5171. {
  5172. int rc;
  5173. struct pt_hid_output hid_output = {
  5174. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5175. };
  5176. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5177. if (rc) {
  5178. *result = 0;
  5179. return rc;
  5180. }
  5181. *result = cd->response_buf[8];
  5182. return 0;
  5183. }
  5184. /*******************************************************************************
  5185. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5186. *
  5187. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5188. * exclusive access to the DUT.
  5189. *
  5190. * RETURN:
  5191. * 0 = success
  5192. * !0 = failure
  5193. *
  5194. * PARAMETERS:
  5195. * *cd - pointer to core data
  5196. * *result - pointer to store result
  5197. ******************************************************************************/
  5198. static int pt_hid_output_bl_verify_app_integrity(
  5199. struct pt_core_data *cd, u8 *result)
  5200. {
  5201. int rc;
  5202. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5203. if (rc < 0) {
  5204. pt_debug(cd->dev, DL_ERROR,
  5205. "%s: fail get exclusive ex=%p own=%p\n",
  5206. __func__, cd->exclusive_dev, cd->dev);
  5207. return rc;
  5208. }
  5209. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5210. if (release_exclusive(cd, cd->dev) < 0)
  5211. pt_debug(cd->dev, DL_ERROR,
  5212. "%s: fail to release exclusive\n", __func__);
  5213. return rc;
  5214. }
  5215. /*******************************************************************************
  5216. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5217. *
  5218. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5219. * to request to have the BL verify the application integrity (PIP1.x only)
  5220. *
  5221. * RETURN:
  5222. * 0 = success
  5223. * !0 = failure
  5224. *
  5225. * PARAMETERS:
  5226. * *dev - pointer to device structure
  5227. * protect - boolean to determine to call the protected function
  5228. * *result - pointer to store result
  5229. ******************************************************************************/
  5230. static int _pt_request_pip_bl_verify_app_integrity(
  5231. struct device *dev, int protect, u8 *result)
  5232. {
  5233. struct pt_core_data *cd = dev_get_drvdata(dev);
  5234. if (protect)
  5235. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5236. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5237. }
  5238. /*******************************************************************************
  5239. * FUNCTION: pt_hid_output_bl_launch_app_
  5240. *
  5241. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5242. * application from bootloader (PIP1.x only).
  5243. *
  5244. * RETURN:
  5245. * 0 = success
  5246. * !0 = failure
  5247. *
  5248. * PARAMETERS:
  5249. * *cd - pointer to core data
  5250. ******************************************************************************/
  5251. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5252. {
  5253. struct pt_hid_output hid_output = {
  5254. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5255. .reset_expected = 1,
  5256. };
  5257. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5258. }
  5259. /*******************************************************************************
  5260. * FUNCTION: pt_hid_output_bl_launch_app
  5261. *
  5262. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5263. * to the DUT.
  5264. *
  5265. * RETURN:
  5266. * 0 = success
  5267. * !0 = failure
  5268. *
  5269. * PARAMETERS:
  5270. * *cd - pointer to core data
  5271. ******************************************************************************/
  5272. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5273. {
  5274. int rc;
  5275. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5276. if (rc < 0) {
  5277. pt_debug(cd->dev, DL_ERROR,
  5278. "%s: fail get exclusive ex=%p own=%p\n",
  5279. __func__, cd->exclusive_dev, cd->dev);
  5280. return rc;
  5281. }
  5282. rc = pt_hid_output_bl_launch_app_(cd);
  5283. if (release_exclusive(cd, cd->dev) < 0)
  5284. pt_debug(cd->dev, DL_ERROR,
  5285. "%s: fail to release exclusive\n", __func__);
  5286. return rc;
  5287. }
  5288. /*******************************************************************************
  5289. * FUNCTION: _pt_request_pip_launch_app
  5290. *
  5291. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5292. * to request to have the BL launch the application. (PIP1.x only)
  5293. *
  5294. * RETURN:
  5295. * 0 = success
  5296. * !0 = failure
  5297. *
  5298. * PARAMETERS:
  5299. * *dev - pointer to device structure
  5300. * protect - boolean to determine to call the protected function
  5301. ******************************************************************************/
  5302. static int _pt_request_pip_launch_app(struct device *dev,
  5303. int protect)
  5304. {
  5305. struct pt_core_data *cd = dev_get_drvdata(dev);
  5306. if (protect)
  5307. return pt_hid_output_bl_launch_app(cd);
  5308. return pt_hid_output_bl_launch_app_(cd);
  5309. }
  5310. /*******************************************************************************
  5311. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5312. *
  5313. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5314. * value store in the System Information.
  5315. *
  5316. * RETURN:
  5317. * 0 = success
  5318. * !0 = failure
  5319. *
  5320. * PARAMETERS:
  5321. * *cd - pointer to core data
  5322. * *panel_id - pointer to where the panel ID will be stored
  5323. ******************************************************************************/
  5324. static int pt_hid_output_bl_get_panel_id_(
  5325. struct pt_core_data *cd, u8 *panel_id)
  5326. {
  5327. int rc;
  5328. struct pt_hid_output hid_output = {
  5329. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5330. };
  5331. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5332. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5333. pt_debug(cd->dev, DL_ERROR,
  5334. "%s: Get Panel ID command not supported\n",
  5335. __func__);
  5336. *panel_id = PANEL_ID_NOT_ENABLED;
  5337. return 0;
  5338. } else if (rc < 0) {
  5339. pt_debug(cd->dev, DL_ERROR,
  5340. "%s: Error on Get Panel ID command\n", __func__);
  5341. return rc;
  5342. }
  5343. *panel_id = cd->response_buf[8];
  5344. return 0;
  5345. }
  5346. /*******************************************************************************
  5347. * FUNCTION: pt_hid_output_bl_get_panel_id
  5348. *
  5349. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5350. * to the DUT.
  5351. *
  5352. * RETURN:
  5353. * 0 = success
  5354. * !0 = failure
  5355. *
  5356. * PARAMETERS:
  5357. * *cd - pointer to core data
  5358. * *panel_id - pointer to where the panel ID will be stored
  5359. ******************************************************************************/
  5360. static int pt_hid_output_bl_get_panel_id(
  5361. struct pt_core_data *cd, u8 *panel_id)
  5362. {
  5363. int rc;
  5364. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5365. if (rc < 0) {
  5366. pt_debug(cd->dev, DL_ERROR,
  5367. "%s: fail get exclusive ex=%p own=%p\n",
  5368. __func__, cd->exclusive_dev, cd->dev);
  5369. return rc;
  5370. }
  5371. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5372. if (release_exclusive(cd, cd->dev) < 0)
  5373. pt_debug(cd->dev, DL_ERROR,
  5374. "%s: fail to release exclusive\n", __func__);
  5375. return rc;
  5376. }
  5377. /*******************************************************************************
  5378. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5379. *
  5380. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5381. * to have the BL retrieve the panel ID
  5382. *
  5383. * RETURN:
  5384. * 0 = success
  5385. * !0 = failure
  5386. *
  5387. * PARAMETERS:
  5388. * *dev - pointer to device structure
  5389. * protect - flag to run in protected mode
  5390. * *panel_id - pointer to where the panel ID will be stored
  5391. ******************************************************************************/
  5392. static int _pt_request_pip_bl_get_panel_id(
  5393. struct device *dev, int protect, u8 *panel_id)
  5394. {
  5395. struct pt_core_data *cd = dev_get_drvdata(dev);
  5396. if (protect)
  5397. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5398. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5399. }
  5400. /*******************************************************************************
  5401. * FUNCTION: pt_pip2_get_mode_sysmode_
  5402. *
  5403. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5404. * PIP2 STATUS command.
  5405. *
  5406. * RETURN:
  5407. * 0 = success
  5408. * !0 = failure
  5409. *
  5410. * PARAMETERS:
  5411. * *cd - pointer to core data structure
  5412. * *mode - pointer to store the retrieved mode
  5413. * *sys_mode - pointer to store the FW system mode
  5414. ******************************************************************************/
  5415. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5416. u8 *mode, u8 *sys_mode)
  5417. {
  5418. u16 actual_read_len;
  5419. u8 read_buf[12];
  5420. u8 status, boot;
  5421. int rc = 0;
  5422. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5423. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5424. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5425. __func__, rc);
  5426. if (!rc) {
  5427. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5428. "PIP2 STATUS");
  5429. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5430. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5431. if (sys_mode) {
  5432. if (status == PIP2_RSP_ERR_NONE &&
  5433. boot == PIP2_STATUS_APP_EXEC)
  5434. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5435. else
  5436. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5437. }
  5438. if (mode) {
  5439. if (status == PIP2_RSP_ERR_NONE &&
  5440. boot == PIP2_STATUS_BOOT_EXEC)
  5441. *mode = PT_MODE_BOOTLOADER;
  5442. else if (status == PIP2_RSP_ERR_NONE &&
  5443. boot == PIP2_STATUS_APP_EXEC)
  5444. *mode = PT_MODE_OPERATIONAL;
  5445. else
  5446. *mode = PT_MODE_UNKNOWN;
  5447. }
  5448. } else {
  5449. if (mode)
  5450. *mode = PT_MODE_UNKNOWN;
  5451. if (sys_mode)
  5452. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5453. pt_debug(cd->dev, DL_WARN,
  5454. "%s: Mode and sys_mode could not be determined\n",
  5455. __func__);
  5456. }
  5457. return rc;
  5458. }
  5459. /*******************************************************************************
  5460. * FUNCTION: pt_pip2_get_mode_sysmode
  5461. *
  5462. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5463. * access to the DUT.
  5464. *
  5465. * RETURN:
  5466. * 0 = success
  5467. * !0 = failure
  5468. *
  5469. * PARAMETERS:
  5470. * *cd - pointer to core data structure
  5471. * *mode - pointer to store the retrieved mode
  5472. * *sys_mode - pointer to store the FW system mode
  5473. ******************************************************************************/
  5474. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5475. u8 *mode, u8 *sys_mode)
  5476. {
  5477. int rc;
  5478. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5479. if (rc < 0) {
  5480. pt_debug(cd->dev, DL_ERROR,
  5481. "%s: fail get exclusive ex=%p own=%p\n",
  5482. __func__, cd->exclusive_dev, cd->dev);
  5483. return rc;
  5484. }
  5485. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5486. if (release_exclusive(cd, cd->dev) < 0)
  5487. pt_debug(cd->dev, DL_ERROR,
  5488. "%s: fail to release exclusive\n", __func__);
  5489. return rc;
  5490. }
  5491. /*******************************************************************************
  5492. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5493. *
  5494. * SUMMARY: Function pointer included in core_commands struct for external
  5495. * calls to the protected or unprotected call to
  5496. * pt_pip2_get_mode_sysmode
  5497. *
  5498. * RETURN:
  5499. * 0 = success
  5500. * !0 = failure
  5501. *
  5502. * PARAMETERS:
  5503. * *dev - pointer to device structure
  5504. * *mode - pointer to store the retrieved mode
  5505. * *sys_mode - pointer to store the FW system mode
  5506. ******************************************************************************/
  5507. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5508. int protect, u8 *mode, u8 *sys_mode)
  5509. {
  5510. struct pt_core_data *cd = dev_get_drvdata(dev);
  5511. if (protect)
  5512. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5513. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5514. }
  5515. /*******************************************************************************
  5516. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5517. *
  5518. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5519. * mode only the following PIP commands will be serviced, any other PIP
  5520. * command the FW will respond with an "Invalid PIP Command" response.
  5521. * - Get HID Descriptor (Register 0x0001, no command ID)
  5522. * - Reset (Register 0x0005, RESET HID request)
  5523. * - Ping (Register 0x0004, Command ID 0x00
  5524. * - Get System Information (Register 0x0004, Command ID 0x02)
  5525. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5526. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5527. * This function will loop on the results of the STATUS command until
  5528. * the FW reports it is out of BOOT mode.
  5529. *
  5530. * NOTE:
  5531. * - This function will update cd->fw_system_mode
  5532. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5533. *
  5534. * RETURN:
  5535. * 0 = success
  5536. * !0 = failure
  5537. *
  5538. * PARAMETERS:
  5539. * *cd - pointer to core data
  5540. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5541. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5542. ******************************************************************************/
  5543. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5544. int *actual_wait)
  5545. {
  5546. int loop = 0;
  5547. u8 sys_mode = cd->fw_system_mode;
  5548. u8 pause = 10; /* in ms */
  5549. int rc = 0;
  5550. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5551. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5552. /*
  5553. * For PIP <1.11, no support for polling wait so do a hard
  5554. * coded wait and assume the FW is out of BOOT. Added 1 to
  5555. * timeout to make it clear in kmsg if non polling was done.
  5556. */
  5557. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5558. pt_debug(cd->dev, DL_ERROR,
  5559. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5560. __func__,
  5561. cd->sysinfo.ttdata.pip_ver_major,
  5562. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5563. msleep(*actual_wait);
  5564. sys_mode = FW_SYS_MODE_SCANNING;
  5565. }
  5566. if (sys_mode == FW_SYS_MODE_BOOT) {
  5567. while (!rc && loop <= max_loop &&
  5568. (sys_mode == FW_SYS_MODE_BOOT)) {
  5569. loop++;
  5570. usleep_range(9000, pause * 1000);
  5571. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5572. pt_debug(cd->dev, DL_DEBUG,
  5573. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5574. __func__, loop * pause, sys_mode);
  5575. }
  5576. *actual_wait = (int)(loop * pause);
  5577. pt_debug(cd->dev, DL_WARN,
  5578. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5579. __func__, *actual_wait, sys_mode);
  5580. if (rc)
  5581. sys_mode = FW_SYS_MODE_UNDEFINED;
  5582. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5583. sys_mode == FW_SYS_MODE_UNDEFINED)
  5584. rc = -EBUSY;
  5585. }
  5586. mutex_lock(&cd->system_lock);
  5587. cd->fw_system_mode = sys_mode;
  5588. mutex_unlock(&cd->system_lock);
  5589. return rc;
  5590. }
  5591. /*******************************************************************************
  5592. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5593. *
  5594. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5595. * access to the DUT.
  5596. *
  5597. * RETURN:
  5598. * 0 = success
  5599. * !0 = failure
  5600. *
  5601. * PARAMETERS:
  5602. * *cd - pointer to core data
  5603. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5604. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5605. ******************************************************************************/
  5606. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5607. int *actual_wait)
  5608. {
  5609. int rc;
  5610. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5611. if (rc < 0) {
  5612. pt_debug(cd->dev, DL_ERROR,
  5613. "%s: fail get exclusive ex=%p own=%p\n",
  5614. __func__, cd->exclusive_dev, cd->dev);
  5615. return rc;
  5616. }
  5617. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5618. if (release_exclusive(cd, cd->dev) < 0)
  5619. pt_debug(cd->dev, DL_ERROR,
  5620. "%s: fail to release exclusive\n", __func__);
  5621. return rc;
  5622. }
  5623. /*******************************************************************************
  5624. * FUNCTION: _pt_get_fw_sys_mode
  5625. *
  5626. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5627. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5628. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5629. * information but any PIP command above 0x1F requires scanning to be disabled
  5630. * before it will be operational. If scanning was not disabled before sending
  5631. * these PIP commands the FW will respond with a 6 byte error response. So to
  5632. * safely determine the scanning state, a PIP message that will not affect the
  5633. * operation of the FW was chosen:
  5634. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5635. * received scanning is active.
  5636. *
  5637. * RETURN:
  5638. * 0 = success
  5639. * !0 = failure
  5640. *
  5641. * PARAMETERS:
  5642. * *cd - pointer to core data
  5643. * *sys_mode - pointer to FW System mode
  5644. * *mode - pointer to mode (BL/FW)
  5645. ******************************************************************************/
  5646. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5647. {
  5648. int write_length = 1;
  5649. int report_length;
  5650. int rc = 0;
  5651. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5652. u8 tmp_mode = PT_MODE_UNKNOWN;
  5653. u8 param[1] = { PT_TCH_PARM_EBID };
  5654. struct pt_hid_output hid_output = {
  5655. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5656. .write_length = write_length,
  5657. .write_buf = param,
  5658. .novalidate = true,
  5659. };
  5660. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5661. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5662. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5663. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5664. __func__, tmp_sys_mode, tmp_mode);
  5665. if (!rc) {
  5666. if (tmp_mode != PT_MODE_OPERATIONAL)
  5667. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5668. }
  5669. goto exit;
  5670. }
  5671. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5672. if (cd->mode != PT_MODE_OPERATIONAL) {
  5673. tmp_mode = cd->mode;
  5674. goto exit;
  5675. }
  5676. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5677. if (rc)
  5678. goto exit;
  5679. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5680. if ((report_length == 0x06) &&
  5681. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5682. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5683. tmp_mode = PIP2_STATUS_APP_EXEC;
  5684. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5685. } else if ((report_length == 0x0A) &&
  5686. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5687. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5688. tmp_mode = PIP2_STATUS_APP_EXEC;
  5689. tmp_sys_mode = FW_SYS_MODE_TEST;
  5690. }
  5691. exit:
  5692. if (mode)
  5693. *mode = tmp_mode;
  5694. if (sys_mode)
  5695. *sys_mode = tmp_sys_mode;
  5696. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5697. __func__, tmp_mode, tmp_sys_mode);
  5698. return rc;
  5699. }
  5700. /*******************************************************************************
  5701. * FUNCTION: pt_get_fw_sys_mode
  5702. *
  5703. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5704. * is active or not.
  5705. *
  5706. * RETURN:
  5707. * 0 = success
  5708. * !0 = failure
  5709. *
  5710. * PARAMETERS:
  5711. * *cd - pointer to core data
  5712. * *sys_mode - pointer to fw system mode
  5713. * *mode - pointer to mode
  5714. ******************************************************************************/
  5715. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5716. {
  5717. int rc;
  5718. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5719. if (rc < 0) {
  5720. pt_debug(cd->dev, DL_ERROR,
  5721. "%s: fail get exclusive ex=%p own=%p\n",
  5722. __func__, cd->exclusive_dev, cd->dev);
  5723. return rc;
  5724. }
  5725. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5726. if (release_exclusive(cd, cd->dev) < 0)
  5727. pt_debug(cd->dev, DL_ERROR,
  5728. "%s: fail to release exclusive\n", __func__);
  5729. return rc;
  5730. }
  5731. /*******************************************************************************
  5732. * FUNCTION: _pt_request_get_fw_sys_mode
  5733. *
  5734. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5735. * to request to get scan state
  5736. *
  5737. * RETURN:
  5738. * 0 = success
  5739. * !0 = failure
  5740. *
  5741. * PARAMETERS:
  5742. * *dev - pointer to device structure
  5743. * protect - flag to call protected or non-protected
  5744. * *sys_mode - pointer to FW system mode
  5745. * *mode - pointer to mode
  5746. ******************************************************************************/
  5747. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5748. u8 *sys_mode, u8 *mode)
  5749. {
  5750. struct pt_core_data *cd = dev_get_drvdata(dev);
  5751. if (protect)
  5752. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5753. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5754. }
  5755. /* Default hid descriptor to provide basic register map */
  5756. const struct pt_hid_desc hid_desc_default = {
  5757. 230, /* hid_desc_len */
  5758. HID_APP_REPORT_ID, /* packet_id */
  5759. 0x00, /* reserved_byte */
  5760. 0x0100, /* bcd_version */
  5761. 0x00EC, /* report_desc_len */
  5762. 0x0002, /* report_desc_register */
  5763. 0x0003, /* input_register */
  5764. 0x00FE, /* max_input_len */
  5765. 0x0004, /* output_register */
  5766. 0x00FE, /* max_output_len */
  5767. 0x0005, /* command_register */
  5768. 0x0006, /* data_register */
  5769. 0x04B4, /* vendor_id */
  5770. 0xC101, /* product_id */
  5771. 0x0100, /* version_id */
  5772. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5773. };
  5774. /*******************************************************************************
  5775. * FUNCTION: pt_init_hid_descriptor
  5776. *
  5777. * SUMMARY: Setup default values for HID descriptor structure
  5778. *
  5779. *
  5780. * PARAMETERS:
  5781. * *desc - pointer to the HID descriptor data read back from DUT
  5782. ******************************************************************************/
  5783. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5784. {
  5785. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5786. }
  5787. /*******************************************************************************
  5788. * FUNCTION: pt_get_hid_descriptor_
  5789. *
  5790. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5791. * into the HID descriptor structure
  5792. *
  5793. * RETURN:
  5794. * 0 = success
  5795. * !0 = failure
  5796. *
  5797. * PARAMETERS:
  5798. * *cd - pointer to core data
  5799. * *desc - pointer to the HID descriptor data read back from DUT
  5800. ******************************************************************************/
  5801. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5802. struct pt_hid_desc *desc)
  5803. {
  5804. struct device *dev = cd->dev;
  5805. int rc = 0;
  5806. int t;
  5807. u8 cmd[2];
  5808. /*
  5809. * During startup the HID descriptor is required for all future
  5810. * processing. If IRQ is already asserted due to an early touch report
  5811. * the report must be cleared before sending command.
  5812. */
  5813. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5814. /* Read HID descriptor length and version */
  5815. mutex_lock(&cd->system_lock);
  5816. cd->hid_cmd_state = 1;
  5817. mutex_unlock(&cd->system_lock);
  5818. /* Set HID descriptor register */
  5819. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5820. sizeof(cd->hid_core.hid_desc_register));
  5821. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5822. __func__, sizeof(cmd));
  5823. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5824. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5825. if (rc) {
  5826. pt_debug(dev, DL_ERROR,
  5827. "%s: failed to get HID descriptor, rc=%d\n",
  5828. __func__, rc);
  5829. goto error;
  5830. }
  5831. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5832. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5833. if (IS_TMO(t)) {
  5834. #ifdef TTDL_DIAGNOSTICS
  5835. cd->bus_transmit_error_count++;
  5836. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5837. #endif /* TTDL_DIAGNOSTICS */
  5838. pt_debug(cd->dev, DL_ERROR,
  5839. "%s: HID get descriptor timed out\n", __func__);
  5840. rc = -ETIME;
  5841. goto error;
  5842. } else {
  5843. cd->hw_detected = true;
  5844. }
  5845. /* Load the HID descriptor including all registers */
  5846. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5847. /* Check HID descriptor length and version */
  5848. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5849. le16_to_cpu(desc->hid_desc_len),
  5850. le16_to_cpu(desc->bcd_version));
  5851. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5852. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5853. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5854. __func__);
  5855. return -ENODEV;
  5856. }
  5857. goto exit;
  5858. error:
  5859. mutex_lock(&cd->system_lock);
  5860. cd->hid_cmd_state = 0;
  5861. mutex_unlock(&cd->system_lock);
  5862. exit:
  5863. return rc;
  5864. }
  5865. /*******************************************************************************
  5866. * FUNCTION: pt_get_hid_descriptor
  5867. *
  5868. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5869. *
  5870. * RETURN:
  5871. * 0 = success
  5872. * !0 = failure
  5873. *
  5874. * PARAMETERS:
  5875. * *cd - pointer to core data
  5876. * *desc - pointer to the HID descriptor data read back from DUT
  5877. ******************************************************************************/
  5878. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5879. struct pt_hid_desc *desc)
  5880. {
  5881. int rc;
  5882. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5883. if (rc < 0) {
  5884. pt_debug(cd->dev, DL_ERROR,
  5885. "%s: fail get exclusive ex=%p own=%p\n",
  5886. __func__, cd->exclusive_dev, cd->dev);
  5887. return rc;
  5888. }
  5889. rc = pt_get_hid_descriptor_(cd, desc);
  5890. if (release_exclusive(cd, cd->dev) < 0)
  5891. pt_debug(cd->dev, DL_ERROR,
  5892. "%s: fail to release exclusive\n", __func__);
  5893. return rc;
  5894. }
  5895. /*******************************************************************************
  5896. * FUNCTION: pt_pip2_get_version_
  5897. *
  5898. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5899. * cd-ttdata
  5900. *
  5901. * RETURN:
  5902. * 0 = success
  5903. * !0 = failure
  5904. *
  5905. * PARAMETERS:
  5906. * *cd - pointer to core data
  5907. ******************************************************************************/
  5908. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5909. {
  5910. int rc = 0;
  5911. int status;
  5912. u8 read_buf[64];
  5913. u16 actual_read_len;
  5914. rc = _pt_request_pip2_send_cmd(cd->dev,
  5915. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5916. NULL, 0, read_buf, &actual_read_len);
  5917. if (rc) {
  5918. pt_debug(cd->dev, DL_ERROR,
  5919. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5920. __func__, rc);
  5921. return rc;
  5922. }
  5923. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5924. if (status == 0) {
  5925. /* Parse the PIP2 VERSION response into ttdata */
  5926. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5927. } else {
  5928. pt_debug(cd->dev, DL_ERROR,
  5929. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5930. __func__, status);
  5931. return status;
  5932. }
  5933. return rc;
  5934. }
  5935. /*******************************************************************************
  5936. * FUNCTION: pt_pip2_get_version
  5937. *
  5938. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5939. * access to the DUT.
  5940. *
  5941. * RETURN:
  5942. * 0 = success
  5943. * !0 = failure
  5944. *
  5945. * PARAMETERS:
  5946. * *cd - pointer to core data structure
  5947. ******************************************************************************/
  5948. static int pt_pip2_get_version(struct pt_core_data *cd)
  5949. {
  5950. int rc;
  5951. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5952. if (rc < 0) {
  5953. pt_debug(cd->dev, DL_ERROR,
  5954. "%s: fail get exclusive ex=%p own=%p\n",
  5955. __func__, cd->exclusive_dev, cd->dev);
  5956. return rc;
  5957. }
  5958. rc = pt_pip2_get_version_(cd);
  5959. if (release_exclusive(cd, cd->dev) < 0)
  5960. pt_debug(cd->dev, DL_ERROR,
  5961. "%s: fail to release exclusive\n", __func__);
  5962. return rc;
  5963. }
  5964. /*******************************************************************************
  5965. * FUNCTION: _pt_request_active_pip_protocol
  5966. *
  5967. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5968. * Function will return PIP version of BL or application based on
  5969. * when it's called.
  5970. *
  5971. * RETURN:
  5972. * 0 = success
  5973. * !0 = failure
  5974. *
  5975. * PARAMETERS:
  5976. * *dev - pointer to device structure
  5977. * protect - flag to run in protected mode
  5978. * *pip_version_major - pointer to store PIP major version
  5979. * *pip_version_minor - pointer to store PIP minor version
  5980. ******************************************************************************/
  5981. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5982. u8 *pip_version_major, u8 *pip_version_minor)
  5983. {
  5984. struct pt_core_data *cd = dev_get_drvdata(dev);
  5985. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5986. int rc = 0;
  5987. struct pt_hid_output sys_info = {
  5988. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5989. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5990. };
  5991. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5992. /* Skip PIP2 command if DUT generation is confirmed */
  5993. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5994. goto skip_pip2_command;
  5995. rc = pt_pip2_get_version_(cd);
  5996. if (!rc) {
  5997. *pip_version_major = ttdata->pip_ver_major;
  5998. *pip_version_minor = ttdata->pip_ver_minor;
  5999. pt_debug(dev, DL_INFO,
  6000. "%s: pip_version = %d.%d\n", __func__,
  6001. *pip_version_major, *pip_version_minor);
  6002. } else {
  6003. /*
  6004. * Legacy products do not support the pip2 protocol to get
  6005. * pip version. However, they do support the "get sysinfo"
  6006. * command to get pip version from FW, but the bootloader
  6007. * does not support it. This function will try "get sysinfo"
  6008. * command if the pip2 command failed but this cmd could also
  6009. * fail if DUT is stuck in bootloader mode.
  6010. */
  6011. pt_debug(dev, DL_INFO,
  6012. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6013. __func__, rc);
  6014. skip_pip2_command:
  6015. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6016. if (!rc) {
  6017. *pip_version_minor =
  6018. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6019. *pip_version_major =
  6020. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6021. pt_debug(dev, DL_INFO,
  6022. "%s: pip_version = %d.%d\n", __func__,
  6023. *pip_version_major, *pip_version_minor);
  6024. } else {
  6025. *pip_version_major = 0;
  6026. *pip_version_minor = 0;
  6027. pt_debug(dev, DL_ERROR,
  6028. "%s: pip_version Not Detected\n", __func__);
  6029. }
  6030. }
  6031. return rc;
  6032. }
  6033. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6034. /*******************************************************************************
  6035. * FUNCTION: _pt_detect_dut_generation
  6036. *
  6037. * SUMMARY: Determine the generation of device that we are communicating with:
  6038. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6039. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6040. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6041. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6042. * coming form the BL or the FW:
  6043. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6044. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6045. * This function will return a modified status if it detects the DUT
  6046. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6047. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6048. *
  6049. * RETURN:
  6050. * 0 = success
  6051. * !0 = failure
  6052. *
  6053. * PARAMETERS:
  6054. * *dev - pointer to device structure
  6055. * *status - pointer to status bitmask
  6056. * *dut_gen - pointer to store the dut_generation
  6057. * *mode - pointer to store the PT_MODE
  6058. ******************************************************************************/
  6059. static int _pt_detect_dut_generation(struct device *dev,
  6060. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6061. {
  6062. int rc = 0;
  6063. u8 dut_gen_tmp = DUT_UNKNOWN;
  6064. u8 mode_tmp = PT_MODE_UNKNOWN;
  6065. u8 attempt = 1;
  6066. u32 status_tmp = STARTUP_STATUS_START;
  6067. struct pt_core_data *cd = dev_get_drvdata(dev);
  6068. struct pt_hid_desc hid_desc;
  6069. memset(&hid_desc, 0, sizeof(hid_desc));
  6070. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6071. while (rc && attempt < 3) {
  6072. attempt++;
  6073. usleep_range(2000, 5000);
  6074. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6075. }
  6076. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6077. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6078. mode_tmp = PT_MODE_BOOTLOADER;
  6079. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6080. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6081. rc = pt_pip2_get_version_(cd);
  6082. if (!rc)
  6083. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6084. else
  6085. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6086. mode_tmp = PT_MODE_OPERATIONAL;
  6087. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6088. rc = 0; /* To return success instead of error code */
  6089. } else if (rc) {
  6090. rc = pt_pip2_get_version_(cd);
  6091. if (!rc) {
  6092. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6093. mode_tmp = PT_MODE_BOOTLOADER;
  6094. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6095. }
  6096. }
  6097. mutex_lock(&cd->system_lock);
  6098. if (dut_gen)
  6099. *dut_gen = dut_gen_tmp;
  6100. if (mode)
  6101. *mode = mode_tmp;
  6102. if (status)
  6103. *status = status_tmp;
  6104. mutex_unlock(&cd->system_lock);
  6105. #ifdef TTDL_DIAGNOSTICS
  6106. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6107. __func__, dut_gen_tmp, mode_tmp);
  6108. #endif /* TTDL_DIAGNOSTICS */
  6109. return rc;
  6110. }
  6111. /*******************************************************************************
  6112. * FUNCTION: _pt_request_dut_generation
  6113. *
  6114. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6115. * to get current dut generation.
  6116. *
  6117. * NOTE: This function WILL NOT try to determine dut generation.
  6118. *
  6119. * RETURN:
  6120. * The current dut generation.
  6121. *
  6122. * PARAMETERS:
  6123. * *dev - pointer to device structure
  6124. ******************************************************************************/
  6125. static int _pt_request_dut_generation(struct device *dev)
  6126. {
  6127. struct pt_core_data *cd = dev_get_drvdata(dev);
  6128. return cd->active_dut_generation;
  6129. }
  6130. #define HW_VERSION_LEN_MAX 13
  6131. /*******************************************************************************
  6132. * FUNCTION: _legacy_generate_hw_version
  6133. *
  6134. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6135. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6136. * command) to the hw_version.
  6137. *
  6138. * RETURN:
  6139. * 0 = success
  6140. * !0 = failure
  6141. *
  6142. * PARAMETERS:
  6143. * *cd - pointer to core data
  6144. * *hw_version - pointer to store the hardware version
  6145. ******************************************************************************/
  6146. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6147. char *hw_version)
  6148. {
  6149. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6150. if (cd->sysinfo.ready) {
  6151. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6152. ttdata->jtag_id_h, cd->pid_for_loader);
  6153. return 0;
  6154. } else if (cd->bl_info.ready) {
  6155. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6156. cd->bl_info.chip_id, cd->pid_for_loader);
  6157. return 0;
  6158. } else {
  6159. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6160. pt_debug(cd->dev, DL_ERROR,
  6161. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6162. return -ENODATA;
  6163. }
  6164. }
  6165. /*******************************************************************************
  6166. * FUNCTION: _pip2_generate_hw_version
  6167. *
  6168. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6169. * VERSION command) to the hw_version.
  6170. *
  6171. * RETURN:
  6172. * 0 = success
  6173. * !0 = failure
  6174. *
  6175. * PARAMETERS:
  6176. * *cd - pointer to core data
  6177. * *hw_version - pointer to store the hardware version
  6178. ******************************************************************************/
  6179. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6180. {
  6181. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6182. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6183. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6184. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6185. return 0;
  6186. } else {
  6187. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6188. pt_debug(cd->dev, DL_ERROR,
  6189. "%s: PIP Version are not ready\n", __func__);
  6190. return -ENODATA;
  6191. }
  6192. }
  6193. /*******************************************************************************
  6194. * FUNCTION: pt_generate_hw_version
  6195. *
  6196. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6197. * hw_version from static data.
  6198. *
  6199. * RETURN:
  6200. * 0 = success
  6201. * !0 = failure
  6202. *
  6203. * PARAMETERS:
  6204. * *cd - pointer to core data
  6205. * *hw_version - pointer to store the hardware version
  6206. ******************************************************************************/
  6207. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6208. {
  6209. int rc = 0;
  6210. if (!hw_version)
  6211. return -ENOMEM;
  6212. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6213. rc = _legacy_generate_hw_version(cd, hw_version);
  6214. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6215. rc = _pip2_generate_hw_version(cd, hw_version);
  6216. else {
  6217. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6218. rc = -ENODATA;
  6219. }
  6220. return rc;
  6221. }
  6222. /*******************************************************************************
  6223. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6224. *
  6225. * NOTE: The calling function must ensure to free *hw_version
  6226. *
  6227. * RETURN:
  6228. * 0 = success
  6229. * !0 = Failure
  6230. *
  6231. * PARAMETERS:
  6232. * *dev - pointer to device structure
  6233. * *hw_version - pointer to where the hw_version string will be stored
  6234. ******************************************************************************/
  6235. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6236. {
  6237. int rc = 0;
  6238. u16 actual_read_len;
  6239. u16 pip_ver;
  6240. u8 rd_buf[256];
  6241. u8 status;
  6242. u8 index = PIP2_RESP_STATUS_OFFSET;
  6243. u8 return_data[8];
  6244. u8 panel_id;
  6245. struct pt_core_data *cd = dev_get_drvdata(dev);
  6246. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6247. if (!hw_version)
  6248. return -ENOMEM;
  6249. if (!cd->hw_detected) {
  6250. /* No HW detected */
  6251. rc = -ENODEV;
  6252. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6253. __func__);
  6254. goto exit_error;
  6255. }
  6256. memset(return_data, 0, ARRAY_SIZE(return_data));
  6257. /* For Parade TC or TT parts */
  6258. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6259. rc = _pt_request_pip2_send_cmd(dev,
  6260. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6261. NULL, 0, rd_buf, &actual_read_len);
  6262. if (rc) {
  6263. pt_debug(dev, DL_ERROR,
  6264. "%s: Failed to send PIP2 VERSION cmd\n",
  6265. __func__);
  6266. goto exit_error;
  6267. }
  6268. status = rd_buf[index];
  6269. if (status == 0) {
  6270. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6271. /*
  6272. * BL PIP 2.02 and greater the version fields are
  6273. * swapped
  6274. */
  6275. if (pip_ver >= 0x0202) {
  6276. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6277. "%02X%02X.%02X%02X.FF",
  6278. rd_buf[index + 10], rd_buf[index + 9],
  6279. rd_buf[index + 8], rd_buf[index + 7]);
  6280. } else {
  6281. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6282. "%02X%02X.%02X%02X.FF",
  6283. rd_buf[index + 8], rd_buf[index + 7],
  6284. rd_buf[index + 10], rd_buf[index + 9]);
  6285. }
  6286. return STATUS_SUCCESS;
  6287. } else {
  6288. rc = status;
  6289. pt_debug(dev, DL_WARN,
  6290. "%s: PIP2 VERSION cmd response error\n",
  6291. __func__);
  6292. }
  6293. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6294. /*
  6295. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6296. * hard coded to FFFF
  6297. */
  6298. if (cd->mode == PT_MODE_OPERATIONAL) {
  6299. rc = pt_hid_output_get_sysinfo(cd);
  6300. if (!rc) {
  6301. panel_id =
  6302. cd->sysinfo.sensing_conf_data.panel_id;
  6303. } else {
  6304. panel_id = PANEL_ID_NOT_ENABLED;
  6305. }
  6306. /* In FW - simply retrieve from ttdata struct */
  6307. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6308. "%04X.FFFF.%02X",
  6309. ttdata->jtag_id_h,
  6310. panel_id);
  6311. return STATUS_SUCCESS;
  6312. } else {
  6313. /*
  6314. * Return the stored value if PT_PANEL_ID_BY_BL
  6315. * is not supported while other feature is.
  6316. */
  6317. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6318. rc = pt_hid_output_bl_get_information(
  6319. cd, return_data);
  6320. if (!rc) {
  6321. rc = pt_hid_output_bl_get_panel_id(
  6322. cd, &panel_id);
  6323. }
  6324. } else
  6325. panel_id = cd->pid_for_loader;
  6326. if (!rc) {
  6327. snprintf(hw_version,
  6328. HW_VERSION_LEN_MAX,
  6329. "%02X%02X.FFFF.%02X",
  6330. return_data[3], return_data[2],
  6331. panel_id);
  6332. return STATUS_SUCCESS;
  6333. }
  6334. }
  6335. } else {
  6336. /* Unknown generation */
  6337. rc = -ENODEV;
  6338. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6339. __func__);
  6340. }
  6341. exit_error:
  6342. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6343. return rc;
  6344. }
  6345. /*******************************************************************************
  6346. * FUNCTION: pt_start_wd_timer
  6347. *
  6348. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6349. *
  6350. * RETURN: void
  6351. *
  6352. * PARAMETERS:
  6353. * *cd - pointer to core data
  6354. ******************************************************************************/
  6355. static void pt_start_wd_timer(struct pt_core_data *cd)
  6356. {
  6357. if (cd->watchdog_interval < 100) {
  6358. pt_debug(cd->dev, DL_ERROR,
  6359. "%s: WARNING: Invalid watchdog interval: %d\n",
  6360. __func__, cd->watchdog_interval);
  6361. return;
  6362. }
  6363. if (cd->watchdog_force_stop) {
  6364. pt_debug(cd->dev, DL_INFO,
  6365. "%s: TTDL WD Forced stop\n", __func__);
  6366. return;
  6367. }
  6368. mod_timer(&cd->watchdog_timer, jiffies +
  6369. msecs_to_jiffies(cd->watchdog_interval));
  6370. cd->watchdog_enabled = 1;
  6371. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6372. }
  6373. /*******************************************************************************
  6374. * FUNCTION: pt_stop_wd_timer
  6375. *
  6376. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6377. *
  6378. * RETURN: void
  6379. *
  6380. * PARAMETERS:
  6381. * *cd - pointer to core data
  6382. ******************************************************************************/
  6383. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6384. {
  6385. if (!cd->watchdog_interval)
  6386. return;
  6387. /*
  6388. * Ensure we wait until the watchdog timer
  6389. * running on a different CPU finishes
  6390. */
  6391. del_timer_sync(&cd->watchdog_timer);
  6392. cancel_work_sync(&cd->watchdog_work);
  6393. del_timer_sync(&cd->watchdog_timer);
  6394. cd->watchdog_enabled = 0;
  6395. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6396. }
  6397. /*******************************************************************************
  6398. * FUNCTION: pt_hw_soft_reset
  6399. *
  6400. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6401. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6402. * schedule an enum if it fires when the DUT is being reset.
  6403. * This can cause a double reset.
  6404. *
  6405. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6406. * the WD active could cause this function to fail!
  6407. *
  6408. * RETURN:
  6409. * 0 = success
  6410. * !0 = failure
  6411. *
  6412. * PARAMETERS:
  6413. * *cd - pointer to core data struct
  6414. * protect - flag to call protected or non-protected
  6415. ******************************************************************************/
  6416. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6417. {
  6418. int rc = 0;
  6419. mutex_lock(&cd->system_lock);
  6420. cd->startup_status = STARTUP_STATUS_START;
  6421. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6422. mutex_unlock(&cd->system_lock);
  6423. if (protect)
  6424. rc = pt_hid_cmd_reset(cd);
  6425. else
  6426. rc = pt_hid_cmd_reset_(cd);
  6427. if (rc < 0) {
  6428. pt_debug(cd->dev, DL_ERROR,
  6429. "%s: FAILED to execute SOFT reset\n", __func__);
  6430. return rc;
  6431. }
  6432. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6433. __func__);
  6434. return 0;
  6435. }
  6436. /*******************************************************************************
  6437. * FUNCTION: pt_hw_hard_reset
  6438. *
  6439. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6440. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6441. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6442. * schedule an enum if it fires when the DUT is being reset.
  6443. * This can cause a double reset.
  6444. *
  6445. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6446. * the WD active could cause this function to fail!
  6447. *
  6448. * RETURN:
  6449. * 0 = success
  6450. * !0 = failure
  6451. *
  6452. * PARAMETERS:
  6453. * *cd - pointer to core data struct
  6454. ******************************************************************************/
  6455. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6456. {
  6457. if (cd->cpdata->xres) {
  6458. cd->startup_status = STARTUP_STATUS_START;
  6459. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6460. __func__);
  6461. cd->cpdata->xres(cd->cpdata, cd->dev);
  6462. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6463. __func__);
  6464. return 0;
  6465. }
  6466. pt_debug(cd->dev, DL_ERROR,
  6467. "%s: FAILED to execute HARD reset\n", __func__);
  6468. return -ENODEV;
  6469. }
  6470. /*******************************************************************************
  6471. * FUNCTION: pt_dut_reset
  6472. *
  6473. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6474. * soft reset.
  6475. *
  6476. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6477. * the WD active could cause this function to fail!
  6478. * NOTE: "protect" flag is only used for soft reset.
  6479. *
  6480. * RETURN:
  6481. * 0 = success
  6482. * !0 = failure
  6483. *
  6484. * PARAMETERS:
  6485. * *cd - pointer to core data structure
  6486. * protect - flag to call protected or non-protected
  6487. ******************************************************************************/
  6488. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6489. {
  6490. int rc = 0;
  6491. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6492. mutex_lock(&cd->system_lock);
  6493. cd->hid_reset_cmd_state = 1;
  6494. rc = pt_hw_hard_reset(cd);
  6495. mutex_unlock(&cd->system_lock);
  6496. if (rc == -ENODEV) {
  6497. mutex_lock(&cd->system_lock);
  6498. cd->hid_reset_cmd_state = 0;
  6499. mutex_unlock(&cd->system_lock);
  6500. pt_debug(cd->dev, DL_ERROR,
  6501. "%s: Hard reset failed, try soft reset\n", __func__);
  6502. rc = pt_hw_soft_reset(cd, protect);
  6503. }
  6504. if (rc)
  6505. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6506. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6507. return rc;
  6508. }
  6509. /*******************************************************************************
  6510. * FUNCTION: pt_dut_reset_and_wait
  6511. *
  6512. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6513. * complete
  6514. *
  6515. * RETURN:
  6516. * 0 = success
  6517. * !0 = failure
  6518. *
  6519. * PARAMETERS:
  6520. * *cd - pointer to core data structure
  6521. ******************************************************************************/
  6522. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6523. {
  6524. int rc = 0;
  6525. int t;
  6526. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6527. if (rc < 0)
  6528. goto exit;
  6529. t = wait_event_timeout(cd->wait_q,
  6530. (cd->hid_reset_cmd_state == 0),
  6531. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6532. if (IS_TMO(t)) {
  6533. #ifdef TTDL_DIAGNOSTICS
  6534. cd->bus_transmit_error_count++;
  6535. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6536. #endif /* TTDL_DIAGNOSTICS */
  6537. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6538. __func__);
  6539. rc = -ETIME;
  6540. goto exit;
  6541. }
  6542. exit:
  6543. return rc;
  6544. }
  6545. /*
  6546. * touch default parameters (from report descriptor) to resolve protocol for
  6547. * touch report
  6548. */
  6549. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6550. /* byte offset, size, min, max, bit offset, report */
  6551. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6552. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6553. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6554. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6555. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6556. };
  6557. /*
  6558. * button default parameters (from report descriptor) to resolve protocol for
  6559. * button report
  6560. */
  6561. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6562. /* byte offset, size, min, max, bit offset, report */
  6563. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6564. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6565. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6566. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6567. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6568. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6569. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6570. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6571. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6572. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6573. };
  6574. /*******************************************************************************
  6575. * FUNCTION: pt_init_pip_report_fields
  6576. *
  6577. * SUMMARY: Setup default values for touch/button report parsing.
  6578. *
  6579. * PARAMETERS:
  6580. * *cd - pointer to core data structure
  6581. ******************************************************************************/
  6582. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6583. {
  6584. struct pt_sysinfo *si = &cd->sysinfo;
  6585. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6586. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6587. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6588. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6589. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6590. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6591. cd->features.easywake = 1;
  6592. cd->features.noise_metric = 1;
  6593. cd->features.tracking_heatmap = 1;
  6594. cd->features.sensor_data = 1;
  6595. }
  6596. /*******************************************************************************
  6597. * FUNCTION: pt_get_mode
  6598. *
  6599. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6600. * message
  6601. *
  6602. * RETURN: Enum of the current mode
  6603. *
  6604. * PARAMETERS:
  6605. * *cd - pointer to the Core Data structure
  6606. * protect - run command in protected mode
  6607. * *mode - pointer to store the retrieved mode
  6608. ******************************************************************************/
  6609. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6610. {
  6611. if (desc->packet_id == HID_APP_REPORT_ID)
  6612. return PT_MODE_OPERATIONAL;
  6613. else if (desc->packet_id == HID_BL_REPORT_ID)
  6614. return PT_MODE_BOOTLOADER;
  6615. return PT_MODE_UNKNOWN;
  6616. }
  6617. /*******************************************************************************
  6618. * FUNCTION: _pt_request_get_mode
  6619. *
  6620. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6621. * to determine the current mode of the DUT by use of the Get HID
  6622. * Descriptor command.
  6623. *
  6624. * RETURN:
  6625. * 0 = success
  6626. * !0 = failure
  6627. *
  6628. * PARAMETERS:
  6629. * *dev - pointer to device structure
  6630. * protect - run command in protected mode
  6631. * *mode - pointer to store the retrieved mode
  6632. ******************************************************************************/
  6633. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6634. {
  6635. struct pt_core_data *cd = dev_get_drvdata(dev);
  6636. struct pt_hid_desc hid_desc;
  6637. int rc = 0;
  6638. memset(&hid_desc, 0, sizeof(hid_desc));
  6639. if (protect)
  6640. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6641. else
  6642. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6643. if (rc)
  6644. *mode = PT_MODE_UNKNOWN;
  6645. else
  6646. *mode = pt_get_mode(cd, &hid_desc);
  6647. return rc;
  6648. }
  6649. /*******************************************************************************
  6650. * FUNCTION: pt_queue_enum_
  6651. *
  6652. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6653. * function. It won't try to add/delete sysfs node or modules.
  6654. *
  6655. * RETURN: void
  6656. *
  6657. * PARAMETERS:
  6658. * *cd - pointer to core data
  6659. ******************************************************************************/
  6660. static void pt_queue_enum_(struct pt_core_data *cd)
  6661. {
  6662. if (cd->startup_state == STARTUP_NONE) {
  6663. cd->startup_state = STARTUP_QUEUED;
  6664. #ifdef TTDL_DIAGNOSTICS
  6665. if (!cd->bridge_mode)
  6666. schedule_work(&cd->enum_work);
  6667. else
  6668. cd->startup_state = STARTUP_NONE;
  6669. #else
  6670. schedule_work(&cd->enum_work);
  6671. #endif
  6672. pt_debug(cd->dev, DL_INFO,
  6673. "%s: enum_work queued\n", __func__);
  6674. } else {
  6675. pt_debug(cd->dev, DL_WARN,
  6676. "%s: Enum not queued - startup_state = %d\n",
  6677. __func__, cd->startup_state);
  6678. }
  6679. }
  6680. /*******************************************************************************
  6681. * FUNCTION: pt_queue_enum
  6682. *
  6683. * SUMMARY: Queues a TTDL enum within a mutex lock
  6684. *
  6685. * RETURN: void
  6686. *
  6687. * PARAMETERS:
  6688. * *cd - pointer to core data
  6689. ******************************************************************************/
  6690. static void pt_queue_enum(struct pt_core_data *cd)
  6691. {
  6692. mutex_lock(&cd->system_lock);
  6693. pt_queue_enum_(cd);
  6694. mutex_unlock(&cd->system_lock);
  6695. }
  6696. static void remove_sysfs_and_modules(struct device *dev);
  6697. /*******************************************************************************
  6698. * FUNCTION: pt_queue_restart
  6699. *
  6700. * SUMMARY: Queues a TTDL restart within a mutex lock
  6701. *
  6702. * RETURN: void
  6703. *
  6704. * PARAMETERS:
  6705. * *cd - pointer to core data
  6706. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6707. * False: will not perform remove action
  6708. ******************************************************************************/
  6709. static void pt_queue_restart(struct pt_core_data *cd)
  6710. {
  6711. mutex_lock(&cd->system_lock);
  6712. if (cd->startup_state == STARTUP_NONE) {
  6713. cd->startup_state = STARTUP_QUEUED;
  6714. schedule_work(&cd->ttdl_restart_work);
  6715. pt_debug(cd->dev, DL_INFO,
  6716. "%s: pt_ttdl_restart queued\n", __func__);
  6717. } else {
  6718. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6719. __func__, cd->startup_state);
  6720. }
  6721. mutex_unlock(&cd->system_lock);
  6722. }
  6723. /*******************************************************************************
  6724. * FUNCTION: call_atten_cb
  6725. *
  6726. * SUMMARY: Iterate over attention list call the function that registered.
  6727. *
  6728. * RETURN: void
  6729. *
  6730. * PARAMETERS:
  6731. * *cd - pointer to core data
  6732. * type - type of attention list
  6733. * mode - condition for execution
  6734. ******************************************************************************/
  6735. static void call_atten_cb(struct pt_core_data *cd,
  6736. enum pt_atten_type type, int mode)
  6737. {
  6738. struct atten_node *atten, *atten_n;
  6739. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6740. __func__, type, mode);
  6741. spin_lock(&cd->spinlock);
  6742. list_for_each_entry_safe(atten, atten_n,
  6743. &cd->atten_list[type], node) {
  6744. if (!mode || atten->mode & mode) {
  6745. spin_unlock(&cd->spinlock);
  6746. pt_debug(cd->dev, DL_DEBUG,
  6747. "%s: attention for '%s'",
  6748. __func__, dev_name(atten->dev));
  6749. atten->func(atten->dev);
  6750. spin_lock(&cd->spinlock);
  6751. }
  6752. }
  6753. spin_unlock(&cd->spinlock);
  6754. }
  6755. /*******************************************************************************
  6756. * FUNCTION: start_fw_upgrade
  6757. *
  6758. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6759. * start firmware upgrade.
  6760. *
  6761. * RETURN:
  6762. * 0 = success
  6763. *
  6764. * PARAMETERS:
  6765. * *data - pointer to core data
  6766. ******************************************************************************/
  6767. static int start_fw_upgrade(void *data)
  6768. {
  6769. struct pt_core_data *cd = (struct pt_core_data *)data;
  6770. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6771. return 0;
  6772. }
  6773. /*******************************************************************************
  6774. * FUNCTION: pt_put_device_into_easy_wakeup_
  6775. *
  6776. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6777. * wake up state.
  6778. *
  6779. * RETURN:
  6780. * 0 = success
  6781. * !0 = failure
  6782. *
  6783. * PARAMETERS:
  6784. * *cd - pointer to core data
  6785. ******************************************************************************/
  6786. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6787. {
  6788. int rc = 0;
  6789. u8 status = 0;
  6790. rc = pt_hid_output_enter_easywake_state_(cd,
  6791. cd->easy_wakeup_gesture, &status);
  6792. if (rc || status == 0)
  6793. return -EBUSY;
  6794. return rc;
  6795. }
  6796. /*******************************************************************************
  6797. * FUNCTION: pt_put_device_into_deep_sleep_
  6798. *
  6799. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6800. *
  6801. * RETURN:
  6802. * 0 = success
  6803. * !0 = error
  6804. *
  6805. * PARAMETERS:
  6806. * *cd - pointer to core data
  6807. ******************************************************************************/
  6808. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6809. {
  6810. int rc = 0;
  6811. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6812. if (rc)
  6813. rc = -EBUSY;
  6814. return rc;
  6815. }
  6816. /*******************************************************************************
  6817. * FUNCTION: pt_put_device_into_deep_standby_
  6818. *
  6819. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6820. *
  6821. * RETURN:
  6822. * 0 = success
  6823. * !0 = error
  6824. *
  6825. * PARAMETERS:
  6826. * *cd - pointer to core data
  6827. ******************************************************************************/
  6828. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6829. {
  6830. int rc = 0;
  6831. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6832. if (rc)
  6833. rc = -EBUSY;
  6834. return rc;
  6835. }
  6836. /*******************************************************************************
  6837. * FUNCTION: pt_core_poweroff_device_
  6838. *
  6839. * SUMMARY: Disable IRQ and HW power down the device.
  6840. *
  6841. * RETURN:
  6842. * 0 = success
  6843. * !0 = failure
  6844. *
  6845. * PARAMETERS:
  6846. * *cd - pointer to core data
  6847. ******************************************************************************/
  6848. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6849. {
  6850. int rc;
  6851. if (cd->irq_enabled) {
  6852. cd->irq_enabled = false;
  6853. disable_irq_nosync(cd->irq);
  6854. }
  6855. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6856. if (rc < 0)
  6857. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6858. __func__, rc);
  6859. return rc;
  6860. }
  6861. /*******************************************************************************
  6862. * FUNCTION: pt_core_sleep_
  6863. *
  6864. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6865. * configuration in the core platform data structure.
  6866. *
  6867. * RETURN:
  6868. * 0 = success
  6869. * !0 = failure
  6870. *
  6871. * PARAMETERS:
  6872. * *cd - pointer to core data
  6873. ******************************************************************************/
  6874. static int pt_core_sleep_(struct pt_core_data *cd)
  6875. {
  6876. int rc = 0;
  6877. mutex_lock(&cd->system_lock);
  6878. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6879. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6880. mutex_unlock(&cd->system_lock);
  6881. pt_debug(cd->dev, DL_INFO,
  6882. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6883. return 0;
  6884. } else {
  6885. cd->sleep_state = SS_SLEEPING;
  6886. }
  6887. mutex_unlock(&cd->system_lock);
  6888. /* Ensure watchdog and startup works stopped */
  6889. pt_stop_wd_timer(cd);
  6890. cancel_work_sync(&cd->enum_work);
  6891. pt_stop_wd_timer(cd);
  6892. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6893. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6894. rc = pt_core_poweroff_device_(cd);
  6895. if (rc)
  6896. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6897. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6898. pt_debug(cd->dev, DL_INFO,
  6899. "%s: Entering into deep standby mode:\n", __func__);
  6900. rc = pt_put_device_into_deep_standby_(cd);
  6901. if (rc)
  6902. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6903. } else {
  6904. pt_debug(cd->dev, DL_INFO,
  6905. "%s: Entering into deep sleep mode:\n", __func__);
  6906. rc = pt_put_device_into_deep_sleep_(cd);
  6907. if (rc)
  6908. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6909. }
  6910. mutex_lock(&cd->system_lock);
  6911. cd->sleep_state = SS_SLEEP_ON;
  6912. mutex_unlock(&cd->system_lock);
  6913. return rc;
  6914. }
  6915. /*******************************************************************************
  6916. * FUNCTION: pt_core_easywake_on_
  6917. *
  6918. * SUMMARY: Suspend the device with easy wake on the
  6919. * configuration in the core platform data structure.
  6920. *
  6921. * RETURN:
  6922. * 0 = success
  6923. * !0 = failure
  6924. *
  6925. * PARAMETERS:
  6926. * *cd - pointer to core data
  6927. ******************************************************************************/
  6928. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6929. {
  6930. int rc = 0;
  6931. mutex_lock(&cd->system_lock);
  6932. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6933. mutex_unlock(&cd->system_lock);
  6934. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6935. __func__, cd->sleep_state);
  6936. return 0;
  6937. }
  6938. mutex_unlock(&cd->system_lock);
  6939. /* Ensure watchdog and startup works stopped */
  6940. pt_stop_wd_timer(cd);
  6941. cancel_work_sync(&cd->enum_work);
  6942. pt_stop_wd_timer(cd);
  6943. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6944. rc = pt_put_device_into_easy_wakeup_(cd);
  6945. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6946. if (rc)
  6947. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6948. }
  6949. mutex_lock(&cd->system_lock);
  6950. cd->sleep_state = SS_EASY_WAKING_ON;
  6951. mutex_unlock(&cd->system_lock);
  6952. return rc;
  6953. }
  6954. /*******************************************************************************
  6955. * FUNCTION: pt_core_easywake_on
  6956. *
  6957. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6958. *
  6959. * RETURN:
  6960. * 0 = success
  6961. * !0 = failure
  6962. *
  6963. * PARAMETERS:
  6964. * *cd - pointer to core data
  6965. ******************************************************************************/
  6966. static int pt_core_easywake_on(struct pt_core_data *cd)
  6967. {
  6968. int rc = 0;
  6969. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6970. if (rc < 0) {
  6971. pt_debug(cd->dev, DL_ERROR,
  6972. "%s: fail get exclusive ex=%p own=%p\n",
  6973. __func__, cd->exclusive_dev, cd->dev);
  6974. return rc;
  6975. }
  6976. rc = pt_core_easywake_on_(cd);
  6977. if (release_exclusive(cd, cd->dev) < 0)
  6978. pt_debug(cd->dev, DL_ERROR,
  6979. "%s: fail to release exclusive\n", __func__);
  6980. else
  6981. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6982. __func__);
  6983. return rc;
  6984. }
  6985. /*******************************************************************************
  6986. * FUNCTION: pt_core_sleep
  6987. *
  6988. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6989. *
  6990. * RETURN:
  6991. * 0 = success
  6992. * !0 = failure
  6993. *
  6994. * PARAMETERS:
  6995. * *cd - pointer to core data
  6996. ******************************************************************************/
  6997. static int pt_core_sleep(struct pt_core_data *cd)
  6998. {
  6999. int rc = 0;
  7000. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7001. if (rc < 0) {
  7002. pt_debug(cd->dev, DL_ERROR,
  7003. "%s: fail get exclusive ex=%p own=%p\n",
  7004. __func__, cd->exclusive_dev, cd->dev);
  7005. return rc;
  7006. }
  7007. rc = pt_core_sleep_(cd);
  7008. if (release_exclusive(cd, cd->dev) < 0)
  7009. pt_debug(cd->dev, DL_ERROR,
  7010. "%s: fail to release exclusive\n", __func__);
  7011. else
  7012. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7013. __func__);
  7014. return rc;
  7015. }
  7016. /*******************************************************************************
  7017. * FUNCTION: pt_wakeup_host
  7018. *
  7019. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7020. *
  7021. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7022. *
  7023. * RETURN:
  7024. * 0 = success
  7025. * !0 = failure
  7026. *
  7027. * PARAMETERS:
  7028. * *cd - pointer to core data
  7029. ******************************************************************************/
  7030. static int pt_wakeup_host(struct pt_core_data *cd)
  7031. {
  7032. #ifndef EASYWAKE_TSG6
  7033. /* TSG5 EasyWake */
  7034. int rc = 0;
  7035. int event_id;
  7036. int size = get_unaligned_le16(&cd->input_buf[0]);
  7037. /* Validate report */
  7038. if (size != 4 || cd->input_buf[2] != 4)
  7039. rc = -EINVAL;
  7040. event_id = cd->input_buf[3];
  7041. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7042. __func__, event_id, rc);
  7043. if (rc) {
  7044. pt_core_sleep_(cd);
  7045. goto exit;
  7046. }
  7047. /* attention WAKE */
  7048. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7049. exit:
  7050. return rc;
  7051. #else
  7052. /* TSG6 FW1.3 EasyWake */
  7053. int rc = 0;
  7054. int i = 0;
  7055. int report_length;
  7056. /* Validate report */
  7057. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7058. rc = -EINVAL;
  7059. pt_core_sleep_(cd);
  7060. goto exit;
  7061. }
  7062. /* Get gesture id and gesture data length */
  7063. cd->gesture_id = cd->input_buf[3];
  7064. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7065. cd->gesture_data_length = report_length - 4;
  7066. pt_debug(cd->dev, DL_INFO,
  7067. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7068. __func__, cd->gesture_id, cd->gesture_data_length);
  7069. for (i = 0; i < cd->gesture_data_length; i++)
  7070. cd->gesture_data[i] = cd->input_buf[4 + i];
  7071. /* attention WAKE */
  7072. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7073. exit:
  7074. return rc;
  7075. #endif
  7076. }
  7077. /*******************************************************************************
  7078. * FUNCTION: pt_get_touch_axis
  7079. *
  7080. * SUMMARY: Function to calculate touch axis
  7081. *
  7082. * PARAMETERS:
  7083. * *cd - pointer to core data structure
  7084. * *axis - pointer to axis calculation result
  7085. * size - size in bytes
  7086. * max - max value of result
  7087. * *xy_data - pointer to input data to be parsed
  7088. * bofs - bit offset
  7089. ******************************************************************************/
  7090. static void pt_get_touch_axis(struct pt_core_data *cd,
  7091. int *axis, int size, int max, u8 *data, int bofs)
  7092. {
  7093. int nbyte;
  7094. int next;
  7095. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7096. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7097. next++;
  7098. }
  7099. *axis &= max - 1;
  7100. }
  7101. /*******************************************************************************
  7102. * FUNCTION: move_tracking_heatmap_data
  7103. *
  7104. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7105. * system information structure, xy_mode and xy_data.
  7106. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7107. * the tthe_tuner sysfs node under the label "THM"
  7108. *
  7109. * RETURN:
  7110. * 0 = success
  7111. * !0 = failure
  7112. *
  7113. * PARAMETERS:
  7114. * *cd - pointer to core data
  7115. * *si - pointer to the system information structure
  7116. ******************************************************************************/
  7117. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7118. struct pt_sysinfo *si)
  7119. {
  7120. #ifdef TTHE_TUNER_SUPPORT
  7121. int size = get_unaligned_le16(&cd->input_buf[0]);
  7122. if (size)
  7123. tthe_print(cd, cd->input_buf, size, "THM=");
  7124. #endif
  7125. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7126. return 0;
  7127. }
  7128. /*******************************************************************************
  7129. * FUNCTION: move_sensor_data
  7130. *
  7131. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7132. * information structure, xy_mode and xy_data.
  7133. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7134. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7135. *
  7136. * RETURN:
  7137. * 0 = success
  7138. * !0 = failure
  7139. *
  7140. * PARAMETERS:
  7141. * *cd - pointer to core data
  7142. * *si - pointer to the system information structure
  7143. ******************************************************************************/
  7144. static int move_sensor_data(struct pt_core_data *cd,
  7145. struct pt_sysinfo *si)
  7146. {
  7147. #ifdef TTHE_TUNER_SUPPORT
  7148. int size = get_unaligned_le16(&cd->input_buf[0]);
  7149. if (size)
  7150. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7151. #endif
  7152. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7153. return 0;
  7154. }
  7155. /*******************************************************************************
  7156. * FUNCTION: move_button_data
  7157. *
  7158. * SUMMARY: Move the valid button data from the input buffer into the system
  7159. * information structure, xy_mode and xy_data.
  7160. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7161. * the tthe_tuner sysfs node under the label "OpModeData"
  7162. *
  7163. * RETURN:
  7164. * 0 = success
  7165. * !0 = failure
  7166. *
  7167. * PARAMETERS:
  7168. * *cd - pointer to core data
  7169. * *si - pointer to the system information structure
  7170. ******************************************************************************/
  7171. static int move_button_data(struct pt_core_data *cd,
  7172. struct pt_sysinfo *si)
  7173. {
  7174. #ifdef TTHE_TUNER_SUPPORT
  7175. int size = get_unaligned_le16(&cd->input_buf[0]);
  7176. if (size)
  7177. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7178. #endif
  7179. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7180. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7181. "xy_mode");
  7182. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7183. BTN_REPORT_SIZE);
  7184. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7185. "xy_data");
  7186. return 0;
  7187. }
  7188. /*******************************************************************************
  7189. * FUNCTION: move_touch_data
  7190. *
  7191. * SUMMARY: Move the valid touch data from the input buffer into the system
  7192. * information structure, xy_mode and xy_data.
  7193. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7194. * the tthe_tuner sysfs node under the label "OpModeData"
  7195. *
  7196. * RETURN:
  7197. * 0 = success
  7198. * !0 = failure
  7199. *
  7200. * PARAMETERS:
  7201. * *cd - pointer to core data
  7202. * *si - pointer to the system information structure
  7203. ******************************************************************************/
  7204. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7205. {
  7206. int max_tch = si->sensing_conf_data.max_tch;
  7207. int num_cur_tch;
  7208. int length;
  7209. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7210. #ifdef TTHE_TUNER_SUPPORT
  7211. int size = get_unaligned_le16(&cd->input_buf[0]);
  7212. if (size)
  7213. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7214. #endif
  7215. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7216. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7217. si->desc.tch_header_size, "xy_mode");
  7218. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7219. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7220. if (unlikely(num_cur_tch > max_tch))
  7221. num_cur_tch = max_tch;
  7222. length = num_cur_tch * si->desc.tch_record_size;
  7223. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7224. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7225. return 0;
  7226. }
  7227. /*******************************************************************************
  7228. * FUNCTION: move_hid_pen_data
  7229. *
  7230. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7231. * information structure, xy_mode and xy_data.
  7232. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7233. * the tthe_tuner sysfs node under the label "HID" starting with the
  7234. * report ID.
  7235. *
  7236. * RETURN:
  7237. * 0 = success
  7238. * !0 = failure
  7239. *
  7240. * PARAMETERS:
  7241. * *cd - pointer to core data
  7242. * *si - pointer to the system information structure
  7243. ******************************************************************************/
  7244. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7245. {
  7246. #ifdef TTHE_TUNER_SUPPORT
  7247. int size = get_unaligned_le16(&cd->input_buf[0]);
  7248. if (size) {
  7249. /*
  7250. * HID over USB does not require the two byte length field, so
  7251. * this should print from input_buf[2] but to keep both finger
  7252. * and pen reports the same the length is included
  7253. */
  7254. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7255. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7256. "HID-USB=");
  7257. else
  7258. tthe_print(cd, &(cd->input_buf[0]), size,
  7259. "HID-I2C=");
  7260. }
  7261. #endif
  7262. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7263. return 0;
  7264. }
  7265. /*******************************************************************************
  7266. * FUNCTION: parse_touch_input
  7267. *
  7268. * SUMMARY: Parse the touch report and take action based on the touch
  7269. * report_id.
  7270. *
  7271. * RETURN:
  7272. * 0 = success
  7273. * !0 = failure
  7274. *
  7275. * PARAMETERS:
  7276. * *cd - pointer to core data
  7277. * size - size of touch record
  7278. ******************************************************************************/
  7279. static int parse_touch_input(struct pt_core_data *cd, int size)
  7280. {
  7281. struct pt_sysinfo *si = &cd->sysinfo;
  7282. int report_id = cd->input_buf[2];
  7283. int rc = -EINVAL;
  7284. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7285. __func__);
  7286. if (!si->ready) {
  7287. pt_debug(cd->dev, DL_ERROR,
  7288. "%s: Need system information to parse touches\n",
  7289. __func__);
  7290. return 0;
  7291. }
  7292. if (!si->xy_mode || !si->xy_data)
  7293. return rc;
  7294. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7295. rc = move_touch_data(cd, si);
  7296. else if (report_id == PT_HID_PEN_REPORT_ID)
  7297. rc = move_hid_pen_data(cd, si);
  7298. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7299. rc = move_button_data(cd, si);
  7300. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7301. rc = move_sensor_data(cd, si);
  7302. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7303. rc = move_tracking_heatmap_data(cd, si);
  7304. if (rc)
  7305. return rc;
  7306. /* attention IRQ */
  7307. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7308. return 0;
  7309. }
  7310. /*******************************************************************************
  7311. * FUNCTION: parse_command_input
  7312. *
  7313. * SUMMARY: Move the response data from the input buffer to the response buffer
  7314. *
  7315. * RETURN:
  7316. * 0 = success
  7317. * !0 = failure
  7318. *
  7319. * PARAMETERS:
  7320. * *cd - pointer to core data
  7321. * size - size of response data
  7322. ******************************************************************************/
  7323. static int parse_command_input(struct pt_core_data *cd, int size)
  7324. {
  7325. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7326. __func__);
  7327. memcpy(cd->response_buf, cd->input_buf, size);
  7328. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7329. if (size && cd->show_tt_data) {
  7330. if (cd->pip2_prot_active)
  7331. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7332. else
  7333. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7334. }
  7335. #endif
  7336. mutex_lock(&cd->system_lock);
  7337. cd->hid_cmd_state = 0;
  7338. mutex_unlock(&cd->system_lock);
  7339. wake_up(&cd->wait_q);
  7340. return 0;
  7341. }
  7342. /*******************************************************************************
  7343. * FUNCTION: pt_allow_enumeration
  7344. *
  7345. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7346. * FW sentinel is seen.
  7347. *
  7348. * RETURN:
  7349. * true = allow enumeration or fully re-probe
  7350. * false = skip enumeration and fully re-probe
  7351. *
  7352. * PARAMETERS:
  7353. * *cd - pointer to core data
  7354. ******************************************************************************/
  7355. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7356. {
  7357. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7358. (!cd->hid_reset_cmd_state) &&
  7359. (cd->core_probe_complete) &&
  7360. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7361. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7362. (cd->mode == PT_MODE_OPERATIONAL)) {
  7363. return true;
  7364. }
  7365. if ((!cd->hid_reset_cmd_state) &&
  7366. (cd->core_probe_complete) &&
  7367. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7368. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7369. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7370. return true;
  7371. }
  7372. pt_debug(cd->dev, DL_INFO,
  7373. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7374. __func__,
  7375. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7376. "core_probe_complete(1)", cd->core_probe_complete,
  7377. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7378. "active_dut_gen(0,2)", cd->active_dut_generation);
  7379. return false;
  7380. }
  7381. /*******************************************************************************
  7382. * FUNCTION: pt_is_touch_report
  7383. *
  7384. * SUMMARY: Determine if a report ID should be treated as a touch report
  7385. *
  7386. * RETURN:
  7387. * true = report ID is a touch report
  7388. * false = report ID is not a touch report
  7389. *
  7390. * PARAMETERS:
  7391. * id - Report ID
  7392. ******************************************************************************/
  7393. static bool pt_is_touch_report(int id)
  7394. {
  7395. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7396. id == PT_HID_PEN_REPORT_ID ||
  7397. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7398. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7399. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7400. return true;
  7401. else
  7402. return false;
  7403. }
  7404. /*******************************************************************************
  7405. * FUNCTION: pt_parse_input
  7406. *
  7407. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7408. * on if its a response to a command or asynchronous touch data / reset
  7409. * sentinel. PIP2.x messages have additional error checking that is
  7410. * parsed (SEQ match from cmd to rsp, CRC valid).
  7411. * Look for special packets based on unique lengths:
  7412. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7413. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7414. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7415. * 0xFFXX - Empty buffer (PIP 1.7+)
  7416. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7417. * the initial probe has not yet been done.
  7418. *
  7419. * RETURN:
  7420. * 0 = success
  7421. * !0 = failure
  7422. *
  7423. * PARAMETERS:
  7424. * *cd - pointer to core data
  7425. ******************************************************************************/
  7426. static int pt_parse_input(struct pt_core_data *cd)
  7427. {
  7428. int report_id;
  7429. int cmd_id;
  7430. int is_command = 0;
  7431. int size;
  7432. int print_size;
  7433. bool touch_report = true;
  7434. unsigned short calc_crc;
  7435. unsigned short resp_crc;
  7436. cd->fw_sys_mode_in_standby_state = false;
  7437. size = get_unaligned_le16(&cd->input_buf[0]);
  7438. print_size = size;
  7439. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7440. __func__, print_size);
  7441. if (print_size <= PT_MAX_INPUT)
  7442. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7443. "<<< Read buf");
  7444. if (size == 0 ||
  7445. (size == 11 &&
  7446. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7447. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7448. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7449. PIP2_CMD_COMMAND_ID_MASK) ==
  7450. PIP2_CMD_ID_STATUS)) {
  7451. touch_report = false;
  7452. cd->hw_detected = true;
  7453. cd->bl_pip_ver_ready = false;
  7454. cd->app_pip_ver_ready = false;
  7455. if (size == 0) {
  7456. mutex_lock(&cd->system_lock);
  7457. cd->pip2_prot_active = false;
  7458. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7459. /*
  7460. * For Gen5/6 this sentinel could be from
  7461. * the BL or FW. Attempt to set the correct
  7462. * mode based on the previous PIP command.
  7463. */
  7464. if (cd->hid_cmd_state ==
  7465. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7466. cd->mode = PT_MODE_OPERATIONAL;
  7467. cd->startup_status =
  7468. STARTUP_STATUS_FW_RESET_SENTINEL;
  7469. } else if (cd->hid_cmd_state ==
  7470. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7471. cd->hid_reset_cmd_state) {
  7472. cd->mode = PT_MODE_BOOTLOADER;
  7473. cd->startup_status =
  7474. STARTUP_STATUS_BL_RESET_SENTINEL;
  7475. } else {
  7476. cd->mode = PT_MODE_UNKNOWN;
  7477. cd->startup_status =
  7478. STARTUP_STATUS_START;
  7479. }
  7480. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7481. pt_debug(cd->dev, DL_INFO,
  7482. "%s: ATM Gen5/6 %s sentinel received\n",
  7483. __func__,
  7484. (cd->mode == PT_MODE_OPERATIONAL ?
  7485. "FW" :
  7486. (cd->mode == PT_MODE_BOOTLOADER ?
  7487. "BL" : "Unknown")));
  7488. } else {
  7489. cd->mode = PT_MODE_OPERATIONAL;
  7490. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7491. cd->startup_status =
  7492. STARTUP_STATUS_FW_RESET_SENTINEL;
  7493. pt_debug(cd->dev, DL_INFO,
  7494. "%s: ATM PT/TT FW sentinel received\n",
  7495. __func__);
  7496. }
  7497. mutex_unlock(&cd->system_lock);
  7498. if (pt_allow_enumeration(cd)) {
  7499. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7500. pt_debug(cd->dev, DL_INFO,
  7501. "%s: Queue Restart\n", __func__);
  7502. pt_queue_restart(cd);
  7503. } else {
  7504. pt_debug(cd->dev, DL_INFO,
  7505. "%s: Queue Enum\n", __func__);
  7506. pt_queue_enum(cd);
  7507. }
  7508. } else {
  7509. pt_debug(cd->dev, DL_INFO,
  7510. "%s: Sentinel - No Queued Action\n",
  7511. __func__);
  7512. }
  7513. } else {
  7514. /* Sentinel must be from TT/TC BL */
  7515. mutex_lock(&cd->system_lock);
  7516. cd->pip2_prot_active = true;
  7517. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7518. cd->mode = PT_MODE_BOOTLOADER;
  7519. cd->sysinfo.ready = false;
  7520. mutex_unlock(&cd->system_lock);
  7521. pt_debug(cd->dev, DL_INFO,
  7522. "%s: BL Reset sentinel received\n", __func__);
  7523. if (cd->flashless_dut &&
  7524. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7525. pt_debug(cd->dev, DL_INFO,
  7526. "%s: BL to RAM for flashless DUT\n",
  7527. __func__);
  7528. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7529. }
  7530. }
  7531. mutex_lock(&cd->system_lock);
  7532. memcpy(cd->response_buf, cd->input_buf, 2);
  7533. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7534. mutex_unlock(&cd->system_lock);
  7535. pt_debug(cd->dev, DL_WARN,
  7536. "%s: Device Initiated Reset\n", __func__);
  7537. wake_up(&cd->wait_q);
  7538. return 0;
  7539. }
  7540. cd->hid_reset_cmd_state = 0;
  7541. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7542. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7543. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7544. cd->hid_cmd_state = 0;
  7545. wake_up(&cd->wait_q);
  7546. mutex_unlock(&cd->system_lock);
  7547. return 0;
  7548. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7549. /*
  7550. * This debug message below is used by PBATS to calculate the
  7551. * time from the last lift off IRQ to when FW exits LFT mode.
  7552. */
  7553. touch_report = false;
  7554. pt_debug(cd->dev, DL_WARN,
  7555. "%s: DUT - Empty buffer detected\n", __func__);
  7556. return 0;
  7557. } else if (size > PT_MAX_INPUT || size < 0) {
  7558. pt_debug(cd->dev, DL_ERROR,
  7559. "%s: DUT - Unexpected len field in active bus data!\n",
  7560. __func__);
  7561. return -EINVAL;
  7562. }
  7563. if (cd->pip2_prot_active) {
  7564. pt_debug(cd->dev, DL_DEBUG,
  7565. "%s: Decode PIP2.x Response\n", __func__);
  7566. /* PIP2 does not have a report id, hard code it */
  7567. report_id = 0x00;
  7568. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7569. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7570. resp_crc = cd->input_buf[size - 2] << 8;
  7571. resp_crc |= cd->input_buf[size - 1];
  7572. if ((cd->pip2_cmd_tag_seq !=
  7573. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7574. (resp_crc != calc_crc) &&
  7575. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7576. == PT_PIP_TOUCH_REPORT_ID) ||
  7577. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7578. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7579. pt_debug(cd->dev, DL_WARN,
  7580. "%s: %s %d %s\n",
  7581. __func__,
  7582. "Received PIP1 report id =",
  7583. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7584. "when expecting a PIP2 report - IGNORE report");
  7585. return 0;
  7586. }
  7587. is_command = 1;
  7588. touch_report = false;
  7589. } else {
  7590. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7591. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7592. }
  7593. #ifdef TTDL_DIAGNOSTICS
  7594. pt_debug(cd->dev, DL_DEBUG,
  7595. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7596. __func__, cd->pip2_prot_active, report_id,
  7597. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7598. #endif /* TTDL_DIAGNOSTICS */
  7599. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7600. pt_wakeup_host(cd);
  7601. #ifdef TTHE_TUNER_SUPPORT
  7602. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7603. #endif
  7604. return 0;
  7605. }
  7606. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7607. msecs_to_jiffies(cd->watchdog_interval));
  7608. /*
  7609. * If it is PIP2 response, the report_id has been set to 0,
  7610. * so it will not be parsed as a touch packet.
  7611. */
  7612. if (!pt_is_touch_report(report_id)) {
  7613. is_command = 1;
  7614. touch_report = false;
  7615. }
  7616. if (unlikely(is_command)) {
  7617. parse_command_input(cd, size);
  7618. return 0;
  7619. }
  7620. if (touch_report)
  7621. parse_touch_input(cd, size);
  7622. return 0;
  7623. }
  7624. /*******************************************************************************
  7625. * FUNCTION: pt_read_input
  7626. *
  7627. * SUMMARY: Reads incoming data off of the active bus
  7628. *
  7629. * RETURN:
  7630. * 0 = success
  7631. * !0 = failure
  7632. *
  7633. * PARAMETERS:
  7634. * *cd - pointer to core data
  7635. ******************************************************************************/
  7636. static int pt_read_input(struct pt_core_data *cd)
  7637. {
  7638. struct device *dev = cd->dev;
  7639. int rc = 0;
  7640. int t;
  7641. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7642. /*
  7643. * Workaround for easywake failure
  7644. * Interrupt for easywake, wait for bus controller to wake
  7645. */
  7646. mutex_lock(&cd->system_lock);
  7647. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7648. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7649. mutex_unlock(&cd->system_lock);
  7650. if (!dev->power.is_suspended)
  7651. goto read;
  7652. t = wait_event_timeout(cd->wait_q,
  7653. (cd->wait_until_wake == 1),
  7654. msecs_to_jiffies(2000));
  7655. #ifdef TTDL_DIAGNOSTICS
  7656. if (IS_TMO(t)) {
  7657. cd->bus_transmit_error_count++;
  7658. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7659. pt_debug(dev, DL_ERROR,
  7660. "%s: !!!I2C Transmission Error %d\n",
  7661. __func__,
  7662. cd->bus_transmit_error_count);
  7663. }
  7664. #else
  7665. if (IS_TMO(t))
  7666. pt_queue_enum(cd);
  7667. #endif /* TTDL_DIAGNOSTICS */
  7668. goto read;
  7669. }
  7670. }
  7671. mutex_unlock(&cd->system_lock);
  7672. read:
  7673. /* Try reading up to 'retry' times */
  7674. while (retry-- != 0) {
  7675. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7676. PT_MAX_INPUT);
  7677. if (!rc) {
  7678. pt_debug(dev, DL_DEBUG,
  7679. "%s: Read input successfully\n", __func__);
  7680. goto read_exit;
  7681. }
  7682. usleep_range(1000, 2000);
  7683. }
  7684. pt_debug(dev, DL_ERROR,
  7685. "%s: Error getting report, rc=%d\n", __func__, rc);
  7686. read_exit:
  7687. return rc;
  7688. }
  7689. /*******************************************************************************
  7690. * FUNCTION: pt_irq
  7691. *
  7692. * SUMMARY: Process all detected interrupts
  7693. *
  7694. * RETURN:
  7695. * IRQ_HANDLED - Finished processing the interrupt
  7696. *
  7697. * PARAMETERS:
  7698. * irq - IRQ number
  7699. * *handle - pointer to core data struct
  7700. ******************************************************************************/
  7701. irqreturn_t pt_irq(int irq, void *handle)
  7702. {
  7703. struct pt_core_data *cd = handle;
  7704. int rc = 0;
  7705. if (!pt_check_irq_asserted(cd))
  7706. return IRQ_HANDLED;
  7707. rc = pt_read_input(cd);
  7708. #ifdef TTDL_DIAGNOSTICS
  7709. cd->irq_count++;
  7710. /* Used to calculate T-Refresh */
  7711. if (cd->t_refresh_active) {
  7712. if (cd->t_refresh_count == 0) {
  7713. cd->t_refresh_time = jiffies;
  7714. cd->t_refresh_count++;
  7715. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7716. cd->t_refresh_count++;
  7717. } else {
  7718. cd->t_refresh_active = 0;
  7719. cd->t_refresh_time =
  7720. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7721. }
  7722. }
  7723. #endif /* TTDL_DIAGNOSTICS */
  7724. if (!rc)
  7725. pt_parse_input(cd);
  7726. return IRQ_HANDLED;
  7727. }
  7728. /*******************************************************************************
  7729. * FUNCTION: _pt_subscribe_attention
  7730. *
  7731. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7732. * to subscribe themselves into the TTDL attention list
  7733. *
  7734. * RETURN:
  7735. * 0 = success
  7736. * !0 = failure
  7737. *
  7738. * PARAMETERS:
  7739. * *dev - pointer to device structure
  7740. * type - attention type enum
  7741. * *id - ID of the module calling this function
  7742. * *func - callback function pointer to be called when notified
  7743. * mode - attention mode
  7744. ******************************************************************************/
  7745. int _pt_subscribe_attention(struct device *dev,
  7746. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7747. int mode)
  7748. {
  7749. struct pt_core_data *cd = dev_get_drvdata(dev);
  7750. struct atten_node *atten, *atten_new;
  7751. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7752. if (!atten_new)
  7753. return -ENOMEM;
  7754. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7755. dev_name(cd->dev));
  7756. spin_lock(&cd->spinlock);
  7757. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7758. if (atten->id == id && atten->mode == mode) {
  7759. spin_unlock(&cd->spinlock);
  7760. kfree(atten_new);
  7761. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7762. __func__,
  7763. "already subscribed attention",
  7764. dev, "mode", mode);
  7765. return 0;
  7766. }
  7767. }
  7768. atten_new->id = id;
  7769. atten_new->dev = dev;
  7770. atten_new->mode = mode;
  7771. atten_new->func = func;
  7772. list_add(&atten_new->node, &cd->atten_list[type]);
  7773. spin_unlock(&cd->spinlock);
  7774. return 0;
  7775. }
  7776. /*******************************************************************************
  7777. * FUNCTION: _pt_unsubscribe_attention
  7778. *
  7779. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7780. * to unsubscribe themselves from the TTDL attention list
  7781. *
  7782. * RETURN:
  7783. * 0 = success
  7784. * !0 = failure
  7785. *
  7786. * PARAMETERS:
  7787. * *dev - pointer to device structure
  7788. * type - attention type enum
  7789. * *id - ID of the module calling this function
  7790. * *func - function pointer
  7791. * mode - attention mode
  7792. ******************************************************************************/
  7793. int _pt_unsubscribe_attention(struct device *dev,
  7794. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7795. int mode)
  7796. {
  7797. struct pt_core_data *cd = dev_get_drvdata(dev);
  7798. struct atten_node *atten, *atten_n;
  7799. spin_lock(&cd->spinlock);
  7800. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7801. if (atten->id == id && atten->mode == mode) {
  7802. list_del(&atten->node);
  7803. spin_unlock(&cd->spinlock);
  7804. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7805. __func__,
  7806. "unsub for atten->dev", atten->dev,
  7807. "atten->mode", atten->mode);
  7808. kfree(atten);
  7809. return 0;
  7810. }
  7811. }
  7812. spin_unlock(&cd->spinlock);
  7813. return -ENODEV;
  7814. }
  7815. /*******************************************************************************
  7816. * FUNCTION: _pt_request_exclusive
  7817. *
  7818. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7819. * to request exclusive access
  7820. *
  7821. * RETURN:
  7822. * 0 = success
  7823. * !0 = failure
  7824. *
  7825. * PARAMETERS:
  7826. * *dev - pointer to device structure
  7827. * timeout_ms - timeout to wait for exclusive access
  7828. ******************************************************************************/
  7829. static int _pt_request_exclusive(struct device *dev,
  7830. int timeout_ms)
  7831. {
  7832. struct pt_core_data *cd = dev_get_drvdata(dev);
  7833. return request_exclusive(cd, (void *)dev, timeout_ms);
  7834. }
  7835. /*******************************************************************************
  7836. * FUNCTION: _pt_release_exclusive
  7837. *
  7838. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7839. * to release exclusive access
  7840. *
  7841. * RETURN:
  7842. * 0 = success
  7843. * !0 = failure
  7844. *
  7845. * PARAMETERS:
  7846. * *dev - pointer to device structure
  7847. ******************************************************************************/
  7848. static int _pt_release_exclusive(struct device *dev)
  7849. {
  7850. struct pt_core_data *cd = dev_get_drvdata(dev);
  7851. return release_exclusive(cd, (void *)dev);
  7852. }
  7853. /*******************************************************************************
  7854. * FUNCTION: _pt_request_reset
  7855. *
  7856. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7857. * to request the DUT to be reset. Function returns err if refused or
  7858. * timeout occurs (Note: core uses fixed timeout period).
  7859. *
  7860. * NOTE: Function blocks until ISR occurs.
  7861. *
  7862. * RETURN:
  7863. * 0 = success
  7864. * !0 = failure
  7865. *
  7866. * PARAMETERS:
  7867. * *dev - pointer to device structure
  7868. * protect - flag to call protected or non-protected
  7869. ******************************************************************************/
  7870. static int _pt_request_reset(struct device *dev, int protect)
  7871. {
  7872. struct pt_core_data *cd = dev_get_drvdata(dev);
  7873. int rc;
  7874. rc = pt_dut_reset(cd, protect);
  7875. if (rc < 0) {
  7876. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7877. __func__, rc);
  7878. }
  7879. return rc;
  7880. }
  7881. /*******************************************************************************
  7882. * FUNCTION: _pt_request_enum
  7883. *
  7884. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7885. * to request TTDL to queue an enum. This function will return err
  7886. * if refused; if no error then enum has completed and system is in
  7887. * normal operation mode.
  7888. *
  7889. * RETURN:
  7890. * 0 = success
  7891. * !0 = failure
  7892. *
  7893. * PARAMETERS:
  7894. * *dev - pointer to device structure
  7895. * wait - boolean to determine if to wait for startup event
  7896. ******************************************************************************/
  7897. static int _pt_request_enum(struct device *dev, bool wait)
  7898. {
  7899. struct pt_core_data *cd = dev_get_drvdata(dev);
  7900. pt_queue_enum(cd);
  7901. if (wait)
  7902. wait_event_timeout(cd->wait_q,
  7903. cd->startup_state == STARTUP_NONE,
  7904. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7905. return 0;
  7906. }
  7907. /*******************************************************************************
  7908. * FUNCTION: _pt_request_sysinfo
  7909. *
  7910. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7911. * to request the pointer to the system information structure. This
  7912. * function will return NULL if sysinfo has not been acquired from the
  7913. * DUT yet.
  7914. *
  7915. * RETURN: Pointer to the system information struct
  7916. *
  7917. * PARAMETERS:
  7918. * *dev - pointer to device structure
  7919. ******************************************************************************/
  7920. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7921. {
  7922. struct pt_core_data *cd = dev_get_drvdata(dev);
  7923. int rc = 0;
  7924. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7925. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7926. !cd->sysinfo.ready) {
  7927. rc = pt_hid_output_get_sysinfo_(cd);
  7928. if (rc) {
  7929. pt_debug(cd->dev, DL_ERROR,
  7930. "%s: Error getting sysinfo rc=%d\n",
  7931. __func__, rc);
  7932. }
  7933. }
  7934. if (cd->sysinfo.ready)
  7935. return &cd->sysinfo;
  7936. return NULL;
  7937. }
  7938. /*******************************************************************************
  7939. * FUNCTION: _pt_request_loader_pdata
  7940. *
  7941. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7942. * to request the pointer to the loader platform data
  7943. *
  7944. * RETURN: Pointer to the loader platform data struct
  7945. *
  7946. * PARAMETERS:
  7947. * *dev - pointer to device structure
  7948. ******************************************************************************/
  7949. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7950. struct device *dev)
  7951. {
  7952. struct pt_core_data *cd = dev_get_drvdata(dev);
  7953. return cd->pdata->loader_pdata;
  7954. }
  7955. /*******************************************************************************
  7956. * FUNCTION: _pt_request_start_wd
  7957. *
  7958. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7959. * to request to start the TTDL watchdog
  7960. *
  7961. * RETURN:
  7962. * 0 = success
  7963. * !0 = failure
  7964. *
  7965. * PARAMETERS:
  7966. * *dev - pointer to device structure
  7967. ******************************************************************************/
  7968. static int _pt_request_start_wd(struct device *dev)
  7969. {
  7970. struct pt_core_data *cd = dev_get_drvdata(dev);
  7971. pt_start_wd_timer(cd);
  7972. return 0;
  7973. }
  7974. /*******************************************************************************
  7975. * FUNCTION: _pt_request_stop_wd
  7976. *
  7977. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7978. * to request to stop the TTDL watchdog
  7979. *
  7980. * RETURN:
  7981. * 0 = success
  7982. * !0 = failure
  7983. *
  7984. * PARAMETERS:
  7985. * *dev - pointer to device structure
  7986. ******************************************************************************/
  7987. static int _pt_request_stop_wd(struct device *dev)
  7988. {
  7989. struct pt_core_data *cd = dev_get_drvdata(dev);
  7990. pt_stop_wd_timer(cd);
  7991. return 0;
  7992. }
  7993. /*******************************************************************************
  7994. * FUNCTION: pt_pip2_launch_app
  7995. *
  7996. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7997. * the FW reset sentinel to indicate the function succeeded.
  7998. *
  7999. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8000. * in a timeout delay and with the DUT being reset with an XRES.
  8001. *
  8002. * RETURN:
  8003. * 0 = success
  8004. * !0 = failure
  8005. *
  8006. * PARAMETERS:
  8007. * *cd - pointer to core data structure
  8008. * protect - flag to call protected or non-protected
  8009. ******************************************************************************/
  8010. static int pt_pip2_launch_app(struct device *dev, int protect)
  8011. {
  8012. struct pt_core_data *cd = dev_get_drvdata(dev);
  8013. u16 actual_read_len;
  8014. u16 tmp_startup_status = cd->startup_status;
  8015. u8 read_buf[12];
  8016. u8 status;
  8017. int time = 0;
  8018. int rc = 0;
  8019. mutex_lock(&cd->system_lock);
  8020. cd->startup_status = STARTUP_STATUS_START;
  8021. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8022. mutex_unlock(&cd->system_lock);
  8023. rc = _pt_request_pip2_send_cmd(dev, protect,
  8024. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8025. &actual_read_len);
  8026. if (rc) {
  8027. pt_debug(dev, DL_ERROR,
  8028. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8029. __func__, rc);
  8030. } else {
  8031. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8032. /* Test for no or invalid image in FLASH, no point to reset */
  8033. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8034. rc = status;
  8035. goto exit;
  8036. }
  8037. /* Any other boot failure */
  8038. if (status != 0) {
  8039. pt_debug(dev, DL_ERROR,
  8040. "%s: FW did not EXECUTE, status = %d\n",
  8041. __func__, status);
  8042. rc = status;
  8043. }
  8044. }
  8045. if (rc) {
  8046. pt_debug(dev, DL_ERROR,
  8047. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8048. __func__, rc);
  8049. goto exit;
  8050. }
  8051. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8052. msleep(20);
  8053. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8054. __func__, time, cd->startup_status);
  8055. time += 20;
  8056. }
  8057. if (cd->startup_status == STARTUP_STATUS_START) {
  8058. pt_debug(cd->dev, DL_WARN,
  8059. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8060. rc = -ETIME;
  8061. }
  8062. exit:
  8063. if (cd->startup_status == STARTUP_STATUS_START) {
  8064. /* Reset to original state because we could be stuck in BL */
  8065. mutex_lock(&cd->system_lock);
  8066. cd->startup_status = tmp_startup_status;
  8067. mutex_unlock(&cd->system_lock);
  8068. }
  8069. return rc;
  8070. }
  8071. /*******************************************************************************
  8072. * FUNCTION: _pt_request_pip2_launch_app
  8073. *
  8074. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8075. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8076. * consumed.
  8077. *
  8078. * RETURN:
  8079. * 0 = success
  8080. * !0 = failure
  8081. *
  8082. * PARAMETERS:
  8083. * *cd - pointer to core data structure
  8084. ******************************************************************************/
  8085. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8086. {
  8087. return pt_pip2_launch_app(dev, protect);
  8088. }
  8089. /*******************************************************************************
  8090. * FUNCTION: _pt_request_wait_for_enum_state
  8091. *
  8092. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8093. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8094. *
  8095. * RETURN:
  8096. * 0 = success
  8097. * !0 = failure
  8098. *
  8099. * PARAMETERS:
  8100. * *cd - pointer to core data structure
  8101. * timeout - timeout for how long to wait
  8102. * state - enum state to wait for
  8103. ******************************************************************************/
  8104. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8105. int state)
  8106. {
  8107. int rc = 0;
  8108. int t;
  8109. struct pt_core_data *cd = dev_get_drvdata(dev);
  8110. t = wait_event_timeout(cd->wait_q,
  8111. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8112. msecs_to_jiffies(timeout));
  8113. if (IS_TMO(t)) {
  8114. pt_debug(cd->dev, DL_ERROR,
  8115. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8116. __func__, state, timeout);
  8117. pt_debug(cd->dev, DL_WARN,
  8118. "%s: enum state reached 0x%04X\n",
  8119. __func__, cd->startup_status);
  8120. rc = -ETIME;
  8121. } else if (cd->startup_status & state) {
  8122. pt_debug(cd->dev, DL_INFO,
  8123. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8124. __func__, cd->startup_status,
  8125. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8126. } else {
  8127. if (t == 1) {
  8128. pt_debug(
  8129. cd->dev, DL_ERROR,
  8130. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8131. __func__, state, timeout);
  8132. rc = -ETIME;
  8133. } else {
  8134. pt_debug(
  8135. cd->dev, DL_ERROR,
  8136. "%s: Enum state 0x%04X not reached in %dms\n",
  8137. __func__, state, timeout - jiffies_to_msecs(t));
  8138. rc = -EINVAL;
  8139. }
  8140. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8141. __func__, cd->startup_status);
  8142. }
  8143. return rc;
  8144. }
  8145. /*******************************************************************************
  8146. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8147. *
  8148. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8149. * deep sleep.
  8150. *
  8151. * RETURN:
  8152. * 0 = success
  8153. * !0 = error
  8154. *
  8155. * PARAMETERS:
  8156. * *cd - pointer to core data
  8157. ******************************************************************************/
  8158. static int pt_core_wake_device_from_deep_sleep_(
  8159. struct pt_core_data *cd)
  8160. {
  8161. int rc;
  8162. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8163. if (rc)
  8164. rc = -EAGAIN;
  8165. /* Prevent failure on sequential wake/sleep requests from OS */
  8166. msleep(20);
  8167. return rc;
  8168. }
  8169. /*******************************************************************************
  8170. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8171. *
  8172. * SUMMARY: Wake up device from Easy-Wake state.
  8173. *
  8174. * RETURN:
  8175. * 0 = success
  8176. * !0 = failure
  8177. *
  8178. * PARAMETERS:
  8179. * *cd - pointer to core data
  8180. ******************************************************************************/
  8181. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8182. {
  8183. int rc = 0;
  8184. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8185. return rc;
  8186. }
  8187. /*******************************************************************************
  8188. * FUNCTION: pt_restore_parameters_
  8189. *
  8190. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8191. * back to the DUT
  8192. *
  8193. * RETURN:
  8194. * 0 = success
  8195. * !0 = failure
  8196. *
  8197. * PARAMETERS:
  8198. * *cd - pointer the core data structure
  8199. ******************************************************************************/
  8200. static int pt_restore_parameters_(struct pt_core_data *cd)
  8201. {
  8202. struct param_node *param;
  8203. int rc = 0;
  8204. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8205. goto exit;
  8206. spin_lock(&cd->spinlock);
  8207. list_for_each_entry(param, &cd->param_list, node) {
  8208. spin_unlock(&cd->spinlock);
  8209. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8210. __func__, param->id, param->value);
  8211. rc = pt_pip_set_param_(cd, param->id,
  8212. param->value, param->size);
  8213. if (rc)
  8214. goto exit;
  8215. spin_lock(&cd->spinlock);
  8216. }
  8217. spin_unlock(&cd->spinlock);
  8218. exit:
  8219. return rc;
  8220. }
  8221. /*******************************************************************************
  8222. * FUNCTION: pt_pip2_exit_bl_
  8223. *
  8224. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8225. * a DUT that may not have flash and will need FW to be loaded into RAM
  8226. *
  8227. * RETURN:
  8228. * 0 = success
  8229. * !0 = failure
  8230. *
  8231. * PARAMETERS:
  8232. * *cd - pointer the core data structure
  8233. * *status_str - pointer to optional status string buffer
  8234. * buf_size - size of status_str buffer
  8235. ******************************************************************************/
  8236. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8237. {
  8238. int rc;
  8239. int wait_time = 0;
  8240. u8 mode = PT_MODE_UNKNOWN;
  8241. bool load_status_str = false;
  8242. /*
  8243. * Below function has protective call to ensure no enum is still on
  8244. * going, while this kind of protection should be applied widely in
  8245. * future (TODO).
  8246. */
  8247. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8248. if (status_str && buf_size <= 50)
  8249. load_status_str = true;
  8250. if (mode == PT_MODE_BOOTLOADER) {
  8251. if (cd->flashless_dut == 1) {
  8252. rc = pt_hw_hard_reset(cd);
  8253. } else {
  8254. rc = pt_pip2_launch_app(cd->dev,
  8255. PT_CORE_CMD_UNPROTECTED);
  8256. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8257. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8258. __func__, "Invalid image in FLASH rc", rc);
  8259. } else if (rc) {
  8260. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8261. __func__, "Failed to launch app rc", rc);
  8262. }
  8263. }
  8264. if (!rc) {
  8265. if (cd->flashless_dut == 1) {
  8266. /* Wait for BL to complete before enum */
  8267. rc = _pt_request_wait_for_enum_state(cd->dev,
  8268. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8269. if (rc && load_status_str) {
  8270. strlcpy(status_str, "No FW sentinel after BL",
  8271. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8272. goto exit;
  8273. }
  8274. }
  8275. /*
  8276. * If the host wants to interact with the FW or do a
  8277. * forced calibration, the FW must be out of BOOT mode
  8278. * and the system information must have been retrieved.
  8279. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8280. * If, however, the enumeration does not reach this
  8281. * point, the DUT may still be in APP mode so test
  8282. * for all conditions.
  8283. */
  8284. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8285. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8286. if (!rc || cd->startup_status >=
  8287. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8288. mutex_lock(&cd->system_lock);
  8289. cd->mode = PT_MODE_OPERATIONAL;
  8290. mutex_unlock(&cd->system_lock);
  8291. }
  8292. if (rc) {
  8293. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8294. __func__, "Failed to enum with DUT",
  8295. cd->startup_status);
  8296. if (load_status_str && !(cd->startup_status &
  8297. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8298. strlcpy(status_str, "FW Stuck in Boot mode",
  8299. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8300. goto exit;
  8301. }
  8302. }
  8303. /*
  8304. * The coming FW sentinel could wake up the event
  8305. * queue, which has chance to be taken by next command
  8306. * wrongly. Following delay is a workaround to avoid
  8307. * this issue for most situations.
  8308. */
  8309. msleep(20);
  8310. pt_start_wd_timer(cd);
  8311. }
  8312. if (load_status_str) {
  8313. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8314. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8315. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8316. else if (!rc)
  8317. strlcpy(status_str, "Entered APP from BL mode",
  8318. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8319. else
  8320. strlcpy(status_str, "Failed to enter APP from BL mode",
  8321. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8322. }
  8323. } else if (mode == PT_MODE_OPERATIONAL) {
  8324. mutex_lock(&cd->system_lock);
  8325. cd->mode = mode;
  8326. mutex_unlock(&cd->system_lock);
  8327. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8328. if (load_status_str) {
  8329. if (!rc)
  8330. strlcpy(status_str, "Already in APP mode",
  8331. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8332. else
  8333. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8334. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8335. }
  8336. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8337. mutex_lock(&cd->system_lock);
  8338. cd->mode = mode;
  8339. mutex_unlock(&cd->system_lock);
  8340. if (load_status_str)
  8341. strlcpy(status_str, "Failed to determine active mode",
  8342. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8343. }
  8344. exit:
  8345. if (!rc)
  8346. pt_start_wd_timer(cd);
  8347. return rc;
  8348. }
  8349. /*******************************************************************************
  8350. * FUNCTION: pt_pip2_exit_bl
  8351. *
  8352. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8353. * access.
  8354. *
  8355. * RETURN:
  8356. * 0 = success
  8357. * !0 = failure
  8358. *
  8359. * PARAMETERS:
  8360. * *cd - pointer the core data structure
  8361. * *status_str - pointer to optional status string buffer
  8362. * buf_size - size of status_str buffer
  8363. ******************************************************************************/
  8364. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8365. {
  8366. int rc;
  8367. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8368. if (rc < 0) {
  8369. pt_debug(cd->dev, DL_ERROR,
  8370. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8371. cd->exclusive_dev, cd->dev);
  8372. return rc;
  8373. }
  8374. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8375. if (release_exclusive(cd, cd->dev) < 0)
  8376. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8377. __func__);
  8378. return rc;
  8379. }
  8380. /*******************************************************************************
  8381. * FUNCTION: _fast_startup
  8382. *
  8383. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8384. *
  8385. * RETURN:
  8386. * 0 = success
  8387. * !0 = failure
  8388. *
  8389. * PARAMETERS:
  8390. * *cd - pointer the core data structure
  8391. ******************************************************************************/
  8392. static int _fast_startup(struct pt_core_data *cd)
  8393. {
  8394. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8395. int rc = 0;
  8396. u8 mode = PT_MODE_UNKNOWN;
  8397. struct pt_hid_desc hid_desc;
  8398. int wait_time = 0;
  8399. memset(&hid_desc, 0, sizeof(hid_desc));
  8400. reset:
  8401. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8402. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8403. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8404. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8405. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8406. __func__);
  8407. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8408. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8409. if (rc < 0) {
  8410. pt_debug(cd->dev, DL_ERROR,
  8411. "%s: Error on getting HID descriptor r=%d\n",
  8412. __func__, rc);
  8413. if (retry--)
  8414. goto reset;
  8415. goto exit;
  8416. }
  8417. cd->mode = pt_get_mode(cd, &hid_desc);
  8418. if (cd->mode == PT_MODE_BOOTLOADER) {
  8419. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8420. __func__);
  8421. rc = pt_hid_output_bl_launch_app_(cd);
  8422. if (rc < 0) {
  8423. pt_debug(cd->dev, DL_ERROR,
  8424. "%s: Error on launch app r=%d\n",
  8425. __func__, rc);
  8426. if (retry--)
  8427. goto reset;
  8428. goto exit;
  8429. }
  8430. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8431. if (rc < 0) {
  8432. pt_debug(cd->dev, DL_ERROR,
  8433. "%s: Error on getting HID descriptor r=%d\n",
  8434. __func__, rc);
  8435. if (retry--)
  8436. goto reset;
  8437. goto exit;
  8438. }
  8439. cd->mode = pt_get_mode(cd, &hid_desc);
  8440. if (cd->mode == PT_MODE_BOOTLOADER) {
  8441. if (retry--)
  8442. goto reset;
  8443. goto exit;
  8444. }
  8445. }
  8446. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8447. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8448. } else {
  8449. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8450. __func__);
  8451. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8452. /* Wait for any sentinel before first try */
  8453. rc = _pt_request_wait_for_enum_state(
  8454. cd->dev, 150,
  8455. STARTUP_STATUS_BL_RESET_SENTINEL |
  8456. STARTUP_STATUS_FW_RESET_SENTINEL);
  8457. if (rc)
  8458. pt_debug(cd->dev, DL_ERROR,
  8459. "%s: No Sentinel detected rc = %d\n",
  8460. __func__, rc);
  8461. } else
  8462. pt_flush_bus_if_irq_asserted(cd,
  8463. PT_FLUSH_BUS_BASED_ON_LEN);
  8464. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8465. if (rc) {
  8466. pt_debug(cd->dev, DL_ERROR,
  8467. "%s: Get mode failed, mode unknown\n",
  8468. __func__);
  8469. mode = PT_MODE_UNKNOWN;
  8470. }
  8471. cd->mode = mode;
  8472. if (cd->mode == PT_MODE_BOOTLOADER) {
  8473. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8474. __func__);
  8475. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8476. if (rc) {
  8477. pt_debug(cd->dev, DL_ERROR,
  8478. "%s Failed to exit bootloader\n",
  8479. __func__);
  8480. msleep(50);
  8481. rc = -ENODEV;
  8482. if (retry--)
  8483. goto reset;
  8484. goto exit;
  8485. } else {
  8486. pt_debug(cd->dev, DL_INFO,
  8487. "%s: Exit bootloader successfully\n",
  8488. __func__);
  8489. }
  8490. if (cd->mode != PT_MODE_OPERATIONAL) {
  8491. pt_debug(cd->dev, DL_WARN,
  8492. "%s: restore mode failure mode = %d\n",
  8493. __func__, cd->mode);
  8494. if (retry--)
  8495. goto reset;
  8496. goto exit;
  8497. }
  8498. }
  8499. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8500. }
  8501. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8502. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8503. if (!rc) {
  8504. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8505. pt_debug(cd->dev, DL_WARN,
  8506. "%s: Exit FW BOOT Mode after %dms\n",
  8507. __func__, wait_time);
  8508. } else {
  8509. pt_debug(cd->dev, DL_WARN,
  8510. "%s: FW stuck in BOOT Mode after %dms\n",
  8511. __func__, wait_time);
  8512. goto exit;
  8513. }
  8514. if (!cd->sysinfo.ready) {
  8515. rc = pt_hid_output_get_sysinfo_(cd);
  8516. if (rc) {
  8517. pt_debug(cd->dev, DL_ERROR,
  8518. "%s: Error on getting sysinfo r=%d\n",
  8519. __func__, rc);
  8520. if (retry--)
  8521. goto reset;
  8522. goto exit;
  8523. }
  8524. }
  8525. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8526. rc = pt_restore_parameters_(cd);
  8527. if (rc)
  8528. pt_debug(cd->dev, DL_ERROR,
  8529. "%s: failed to restore parameters rc=%d\n",
  8530. __func__, rc);
  8531. else
  8532. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8533. exit:
  8534. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8535. return rc;
  8536. }
  8537. /*******************************************************************************
  8538. * FUNCTION: pt_core_poweron_device_
  8539. *
  8540. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8541. *
  8542. * RETURN:
  8543. * 0 = success
  8544. * !0 = failure
  8545. *
  8546. * PARAMETERS:
  8547. * *cd - pointer to core data
  8548. ******************************************************************************/
  8549. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8550. {
  8551. struct device *dev = cd->dev;
  8552. int rc = 0;
  8553. /*
  8554. * After power on action, the chip can general FW sentinel. It can
  8555. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8556. * _fast_startup() can perform a quick enumeration too, here doesn't
  8557. * need another enumeration.
  8558. */
  8559. mutex_lock(&cd->system_lock);
  8560. cd->startup_status = STARTUP_STATUS_START;
  8561. cd->hid_reset_cmd_state = 1;
  8562. mutex_unlock(&cd->system_lock);
  8563. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8564. if (rc < 0) {
  8565. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8566. __func__, rc);
  8567. goto exit;
  8568. }
  8569. if (!cd->irq_enabled) {
  8570. cd->irq_enabled = true;
  8571. enable_irq(cd->irq);
  8572. }
  8573. /* TBD: following function doesn't update startup_status */
  8574. rc = _fast_startup(cd);
  8575. exit:
  8576. return rc;
  8577. }
  8578. /*******************************************************************************
  8579. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8580. *
  8581. * SUMMARY: Reset device, and then trigger a full enumeration.
  8582. *
  8583. * RETURN:
  8584. * 0 = success
  8585. * !0 = failure
  8586. *
  8587. * PARAMETERS:
  8588. * *cd - pointer to core data
  8589. ******************************************************************************/
  8590. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8591. {
  8592. int rc;
  8593. rc = pt_dut_reset_and_wait(cd);
  8594. if (rc < 0) {
  8595. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8596. __func__, rc);
  8597. goto exit;
  8598. }
  8599. rc = _fast_startup(cd);
  8600. exit:
  8601. return rc;
  8602. }
  8603. /*******************************************************************************
  8604. * FUNCTION: pt_core_easywake_off_
  8605. *
  8606. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8607. * the configuration in the core platform data structure.
  8608. *
  8609. * RETURN:
  8610. * 0 = success
  8611. * !0 = failure
  8612. *
  8613. * PARAMETERS:
  8614. * *cd - pointer to core data
  8615. ******************************************************************************/
  8616. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8617. {
  8618. int rc = 0;
  8619. mutex_lock(&cd->system_lock);
  8620. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8621. mutex_unlock(&cd->system_lock);
  8622. pt_debug(cd->dev, DL_INFO,
  8623. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8624. return 0;
  8625. }
  8626. mutex_unlock(&cd->system_lock);
  8627. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8628. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8629. rc = pt_core_wake_device_from_easy_wake_(cd);
  8630. if (rc < 0)
  8631. pt_debug(cd->dev, DL_ERROR,
  8632. "%s - %d failed %d\n", __func__, rc);
  8633. }
  8634. mutex_lock(&cd->system_lock);
  8635. cd->sleep_state = SS_EASY_WAKING_OFF;
  8636. mutex_unlock(&cd->system_lock);
  8637. pt_start_wd_timer(cd);
  8638. return rc;
  8639. }
  8640. /*******************************************************************************
  8641. * FUNCTION: pt_core_easywake_off
  8642. *
  8643. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8644. *
  8645. * RETURN:
  8646. * 0 = success
  8647. * !0 = failure
  8648. *
  8649. * PARAMETERS:
  8650. * *cd - pointer to core data
  8651. ******************************************************************************/
  8652. static int pt_core_easywake_off(struct pt_core_data *cd)
  8653. {
  8654. int rc;
  8655. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8656. if (rc < 0) {
  8657. pt_debug(cd->dev, DL_ERROR,
  8658. "%s: fail get exclusive ex=%p own=%p\n",
  8659. __func__, cd->exclusive_dev, cd->dev);
  8660. return rc;
  8661. }
  8662. rc = pt_core_easywake_off_(cd);
  8663. if (release_exclusive(cd, cd->dev) < 0)
  8664. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8665. __func__);
  8666. else
  8667. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8668. __func__);
  8669. return rc;
  8670. }
  8671. /*******************************************************************************
  8672. * FUNCTION: pt_core_wake_
  8673. *
  8674. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8675. * the configuration in the core platform data structure.
  8676. *
  8677. * RETURN:
  8678. * 0 = success
  8679. * !0 = failure
  8680. *
  8681. * PARAMETERS:
  8682. * *cd - pointer to core data
  8683. ******************************************************************************/
  8684. static int pt_core_wake_(struct pt_core_data *cd)
  8685. {
  8686. int rc = 0;
  8687. mutex_lock(&cd->system_lock);
  8688. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8689. mutex_unlock(&cd->system_lock);
  8690. pt_debug(cd->dev, DL_INFO,
  8691. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8692. return 0;
  8693. } else {
  8694. cd->sleep_state = SS_WAKING;
  8695. }
  8696. mutex_unlock(&cd->system_lock);
  8697. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8698. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8699. pt_debug(cd->dev, DL_INFO,
  8700. "%s: Entering into poweron mode:\n", __func__);
  8701. rc = pt_core_poweron_device_(cd);
  8702. if (rc < 0)
  8703. pr_err("%s: Poweron error detected: rc=%d\n",
  8704. __func__, rc);
  8705. }
  8706. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8707. rc = pt_core_wake_device_from_deep_standby_(cd);
  8708. else /* Default action to exit DeepSleep */
  8709. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8710. }
  8711. mutex_lock(&cd->system_lock);
  8712. cd->sleep_state = SS_SLEEP_OFF;
  8713. mutex_unlock(&cd->system_lock);
  8714. pt_start_wd_timer(cd);
  8715. return rc;
  8716. }
  8717. /*******************************************************************************
  8718. * FUNCTION: pt_core_wake_
  8719. *
  8720. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8721. *
  8722. * RETURN:
  8723. * 0 = success
  8724. * !0 = failure
  8725. *
  8726. * PARAMETERS:
  8727. * *cd - pointer to core data
  8728. ******************************************************************************/
  8729. static int pt_core_wake(struct pt_core_data *cd)
  8730. {
  8731. int rc = 0;
  8732. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8733. if (rc < 0) {
  8734. pt_debug(cd->dev, DL_ERROR,
  8735. "%s: fail get exclusive ex=%p own=%p\n",
  8736. __func__, cd->exclusive_dev, cd->dev);
  8737. return rc;
  8738. }
  8739. rc = pt_core_wake_(cd);
  8740. if (release_exclusive(cd, cd->dev) < 0)
  8741. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8742. __func__);
  8743. else
  8744. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8745. __func__);
  8746. return rc;
  8747. }
  8748. /*******************************************************************************
  8749. * FUNCTION: pt_get_ic_crc_
  8750. *
  8751. * SUMMARY: This function retrieves the config block CRC
  8752. *
  8753. * NOTE: The post condition of calling this function will be that the DUT will
  8754. * be in SCANNINING mode if no failures occur
  8755. *
  8756. * RETURN:
  8757. * 0 = success
  8758. * !0 = failure
  8759. *
  8760. * PARAMETERS:
  8761. * *cd - pointer the core data structure
  8762. * ebid - enumerated block ID
  8763. ******************************************************************************/
  8764. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8765. {
  8766. struct pt_sysinfo *si = &cd->sysinfo;
  8767. int rc = 0;
  8768. u8 status;
  8769. u16 calculated_crc = 0;
  8770. u16 stored_crc = 0;
  8771. rc = pt_pip_suspend_scanning_(cd);
  8772. if (rc)
  8773. goto error;
  8774. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8775. &calculated_crc, &stored_crc);
  8776. if (rc)
  8777. goto exit;
  8778. if (status) {
  8779. rc = -EINVAL;
  8780. goto exit;
  8781. }
  8782. si->ttconfig.crc = stored_crc;
  8783. exit:
  8784. pt_pip_resume_scanning_(cd);
  8785. error:
  8786. pt_debug(cd->dev, DL_INFO,
  8787. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8788. __func__, ebid, calculated_crc, stored_crc, rc);
  8789. return rc;
  8790. }
  8791. /*******************************************************************************
  8792. * FUNCTION: pt_pip2_read_gpio
  8793. *
  8794. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8795. * bits into the passed in variable
  8796. *
  8797. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8798. *
  8799. * RETURN:
  8800. * 0 = success
  8801. * !0 = failure
  8802. *
  8803. * PARAMETERS:
  8804. * *dev - pointer to device structure
  8805. * *status - pointer to where the command response status is stored
  8806. * *gpio - pointer to device attributes structure
  8807. ******************************************************************************/
  8808. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8809. {
  8810. int rc = 0;
  8811. u16 actual_read_len;
  8812. u8 read_buf[12];
  8813. u8 tmp_status = 0;
  8814. u8 index = PIP2_RESP_STATUS_OFFSET;
  8815. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8816. rc = _pt_request_pip2_send_cmd(dev,
  8817. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8818. NULL, 0, read_buf, &actual_read_len);
  8819. if (rc) {
  8820. pt_debug(dev, DL_ERROR,
  8821. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8822. rc = -ECOMM;
  8823. } else {
  8824. tmp_status = read_buf[index];
  8825. }
  8826. if (status)
  8827. *status = tmp_status;
  8828. if (!rc && gpio && (tmp_status == 0)) {
  8829. *gpio = ((read_buf[index + 4] << 24) |
  8830. (read_buf[index + 3] << 16) |
  8831. (read_buf[index + 2] << 8) |
  8832. (read_buf[index + 1]));
  8833. }
  8834. return rc;
  8835. }
  8836. /*******************************************************************************
  8837. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8838. *
  8839. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8840. *
  8841. * RETURN:
  8842. * 0 = success
  8843. * !0 = failure
  8844. *
  8845. * PARAMETERS:
  8846. * *cd - pointer the core data structure
  8847. * *pid - pointer to store panel ID
  8848. ******************************************************************************/
  8849. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8850. {
  8851. u32 gpio_value = 0;
  8852. u8 status = 0;
  8853. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8854. int rc = 0;
  8855. if (!pid)
  8856. return -ENOMEM;
  8857. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8858. if (!rc) {
  8859. if (status == 0) {
  8860. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8861. PT_PANEL_ID_SHIFT;
  8862. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8863. __func__,
  8864. "BL mode PID", panel_id, "gpio", gpio_value);
  8865. *pid = panel_id;
  8866. } else {
  8867. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8868. __func__,
  8869. "BL read gpio failed status", status);
  8870. }
  8871. } else {
  8872. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8873. __func__,
  8874. "BL read gpio failed status", status);
  8875. }
  8876. return rc;
  8877. }
  8878. /*******************************************************************************
  8879. * FUNCTION: pt_enum_with_dut_
  8880. *
  8881. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8882. * The core data (cd) startup_status will store, as a bitmask, each
  8883. * state of the enumeration process. The startup will be attempted
  8884. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8885. *
  8886. * RETURN:
  8887. * 0 = success
  8888. * !0 = failure
  8889. *
  8890. * PARAMETERS:
  8891. * *cd - pointer the core data structure
  8892. * reset - Flag to reset the DUT before attempting to enumerate
  8893. * *status - poionter to store the enum status bitmask flags
  8894. ******************************************************************************/
  8895. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8896. u32 *enum_status)
  8897. {
  8898. int try = 1;
  8899. int rc = 0;
  8900. int wait_time = 0;
  8901. bool detected = false;
  8902. u8 return_data[8];
  8903. u8 mode = PT_MODE_UNKNOWN;
  8904. u8 pid = PANEL_ID_NOT_ENABLED;
  8905. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8906. struct pt_hid_desc hid_desc;
  8907. memset(&hid_desc, 0, sizeof(hid_desc));
  8908. #ifdef TTHE_TUNER_SUPPORT
  8909. tthe_print(cd, NULL, 0, "enter startup");
  8910. #endif
  8911. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8912. __func__, cd->startup_status, reset);
  8913. pt_stop_wd_timer(cd);
  8914. reset:
  8915. if (try > 1)
  8916. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8917. __func__, try);
  8918. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8919. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8920. pt_debug(cd->dev, DL_INFO,
  8921. "%s: PIP1 Enumeration start\n", __func__);
  8922. /* Only reset the DUT after the first try */
  8923. if (reset || (try > 1)) {
  8924. /*
  8925. * Reset hardware only for Legacy parts. Skip for TT/TC
  8926. * parts because if the FW image was loaded directly
  8927. * to SRAM issueing a reset ill wipe out what was just
  8928. * loaded.
  8929. */
  8930. rc = pt_dut_reset_and_wait(cd);
  8931. if (rc < 0) {
  8932. pt_debug(cd->dev, DL_ERROR,
  8933. "%s: Error on h/w reset r=%d\n",
  8934. __func__, rc);
  8935. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8936. goto reset;
  8937. goto exit;
  8938. }
  8939. /* sleep to allow FW to be launched if available */
  8940. msleep(120);
  8941. }
  8942. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8943. if (rc < 0) {
  8944. pt_debug(cd->dev, DL_ERROR,
  8945. "%s: Error getting HID Descriptor r=%d\n",
  8946. __func__, rc);
  8947. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8948. goto reset;
  8949. rc = -EIO;
  8950. goto exit;
  8951. }
  8952. detected = true;
  8953. cd->mode = pt_get_mode(cd, &hid_desc);
  8954. /*
  8955. * Most systems do not use an XY pin as the panel_id and so
  8956. * the BL is used to retrieve the panel_id, however for
  8957. * systems that do use an XY pin, the panel_id MUST be
  8958. * retrieved from the system information when running FW
  8959. * (done below) and so this section of code is skipped.
  8960. * Entering the BL here is only needed on XY_PIN systems.
  8961. */
  8962. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8963. if (cd->mode == PT_MODE_OPERATIONAL) {
  8964. rc = pt_pip_start_bootloader_(cd);
  8965. if (rc < 0) {
  8966. pt_debug(cd->dev, DL_ERROR,
  8967. "%s: Error on start bootloader r=%d\n",
  8968. __func__, rc);
  8969. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8970. goto reset;
  8971. goto exit;
  8972. }
  8973. cd->mode = PT_MODE_BOOTLOADER;
  8974. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8975. __func__);
  8976. }
  8977. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8978. if (!rc) {
  8979. cd->bl_info.ready = true;
  8980. cd->bl_info.chip_id =
  8981. get_unaligned_le16(&return_data[2]);
  8982. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8983. __func__, cd->bl_info.chip_id);
  8984. } else {
  8985. pt_debug(cd->dev, DL_ERROR,
  8986. "%s: failed to get chip ID, r=%d\n",
  8987. __func__, rc);
  8988. }
  8989. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8990. mutex_lock(&cd->system_lock);
  8991. if (!rc) {
  8992. cd->pid_for_loader = pid;
  8993. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8994. __func__, cd->pid_for_loader);
  8995. } else {
  8996. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8997. pt_debug(cd->dev, DL_WARN,
  8998. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8999. __func__, cd->pid_for_loader);
  9000. }
  9001. mutex_unlock(&cd->system_lock);
  9002. }
  9003. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9004. if (cd->mode == PT_MODE_BOOTLOADER) {
  9005. rc = pt_hid_output_bl_launch_app_(cd);
  9006. if (rc < 0) {
  9007. pt_debug(cd->dev, DL_ERROR,
  9008. "%s: Error on launch app r=%d\n",
  9009. __func__, rc);
  9010. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9011. goto reset;
  9012. rc = -ENODEV;
  9013. goto exit;
  9014. }
  9015. /* sleep to allow FW to be launched if available */
  9016. msleep(120);
  9017. /* Ensure the DUT is now in Application mode */
  9018. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9019. if (rc < 0) {
  9020. pt_debug(cd->dev, DL_ERROR,
  9021. "%s: Error getting HID Desc r=%d\n",
  9022. __func__, rc);
  9023. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9024. goto reset;
  9025. rc = -EIO;
  9026. goto exit;
  9027. }
  9028. cd->mode = pt_get_mode(cd, &hid_desc);
  9029. if (cd->mode == PT_MODE_BOOTLOADER) {
  9030. pt_debug(cd->dev, DL_WARN,
  9031. "%s: Error confiming exit BL\n",
  9032. __func__);
  9033. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9034. goto reset;
  9035. rc = -ENODEV;
  9036. goto exit;
  9037. }
  9038. }
  9039. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9040. cd->mode = PT_MODE_OPERATIONAL;
  9041. *enum_status |= STARTUP_STATUS_GET_DESC;
  9042. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9043. } else {
  9044. /* Generation is PIP2 Capable */
  9045. pt_debug(cd->dev, DL_INFO,
  9046. "%s: PIP2 Enumeration start\n", __func__);
  9047. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9048. if (rc) {
  9049. pt_debug(cd->dev, DL_ERROR,
  9050. "%s: Get mode failed, mode unknown\n",
  9051. __func__);
  9052. mode = PT_MODE_UNKNOWN;
  9053. } else
  9054. detected = true;
  9055. cd->mode = mode;
  9056. switch (cd->mode) {
  9057. case PT_MODE_OPERATIONAL:
  9058. pt_debug(cd->dev, DL_INFO,
  9059. "%s: Operational mode\n", __func__);
  9060. if (cd->app_pip_ver_ready == false) {
  9061. rc = pt_pip2_get_version_(cd);
  9062. if (!rc)
  9063. cd->app_pip_ver_ready = true;
  9064. else {
  9065. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9066. goto reset;
  9067. goto exit;
  9068. }
  9069. }
  9070. break;
  9071. case PT_MODE_BOOTLOADER:
  9072. pt_debug(cd->dev, DL_INFO,
  9073. "%s: Bootloader mode\n", __func__);
  9074. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9075. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9076. mutex_lock(&cd->system_lock);
  9077. if (!rc) {
  9078. cd->pid_for_loader = pid;
  9079. pt_debug(cd->dev, DL_INFO,
  9080. "%s: Panel ID: 0x%02X\n",
  9081. __func__, cd->pid_for_loader);
  9082. } else {
  9083. cd->pid_for_loader =
  9084. PANEL_ID_NOT_ENABLED;
  9085. pt_debug(cd->dev, DL_WARN,
  9086. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9087. __func__, cd->pid_for_loader);
  9088. }
  9089. mutex_unlock(&cd->system_lock);
  9090. }
  9091. if (cd->bl_pip_ver_ready == false) {
  9092. rc = pt_pip2_get_version_(cd);
  9093. if (!rc)
  9094. cd->bl_pip_ver_ready = true;
  9095. else {
  9096. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9097. goto reset;
  9098. goto exit;
  9099. }
  9100. }
  9101. /*
  9102. * Launch app command will fail in flashless mode.
  9103. * Skip launch app command here to save time for
  9104. * enumeration flow.
  9105. */
  9106. if (cd->flashless_dut)
  9107. goto exit;
  9108. /*
  9109. * pt_pip2_launch_app() is needed here instead of
  9110. * pt_pip2_exit_bl() because exit_bl will cause several
  9111. * hw_resets to occur and the auto BL on a flashless
  9112. * DUT will fail.
  9113. */
  9114. rc = pt_pip2_launch_app(cd->dev,
  9115. PT_CORE_CMD_UNPROTECTED);
  9116. if (rc) {
  9117. pt_debug(cd->dev, DL_ERROR,
  9118. "%s: Error on launch app r=%d\n",
  9119. __func__, rc);
  9120. msleep(50);
  9121. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9122. goto reset;
  9123. goto exit;
  9124. }
  9125. /*
  9126. * IRQ thread can be delayed if the serial log print is
  9127. * enabled. It causes next command to get wrong response
  9128. * Here the delay is to ensure pt_parse_input() to be
  9129. * finished.
  9130. */
  9131. msleep(60);
  9132. /* Check and update the mode */
  9133. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9134. if (rc) {
  9135. pt_debug(cd->dev, DL_ERROR,
  9136. "%s: Get mode failed, mode unknown\n",
  9137. __func__);
  9138. mode = PT_MODE_UNKNOWN;
  9139. }
  9140. cd->mode = mode;
  9141. if (cd->mode == PT_MODE_OPERATIONAL) {
  9142. pt_debug(cd->dev, DL_INFO,
  9143. "%s: Launched to Operational mode\n",
  9144. __func__);
  9145. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9146. pt_debug(cd->dev, DL_ERROR,
  9147. "%s: Launch failed, Bootloader mode\n",
  9148. __func__);
  9149. goto exit;
  9150. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9151. pt_debug(cd->dev, DL_ERROR,
  9152. "%s: Launch failed, Unknown mode\n",
  9153. __func__);
  9154. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9155. goto reset;
  9156. goto exit;
  9157. }
  9158. if (cd->app_pip_ver_ready == false) {
  9159. rc = pt_pip2_get_version_(cd);
  9160. if (!rc)
  9161. cd->app_pip_ver_ready = true;
  9162. else {
  9163. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9164. goto reset;
  9165. goto exit;
  9166. }
  9167. }
  9168. break;
  9169. default:
  9170. pt_debug(cd->dev, DL_ERROR,
  9171. "%s: Unknown mode\n", __func__);
  9172. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9173. goto reset;
  9174. goto exit;
  9175. }
  9176. *enum_status |= STARTUP_STATUS_GET_DESC;
  9177. }
  9178. pt_init_pip_report_fields(cd);
  9179. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9180. if (!cd->features.easywake)
  9181. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9182. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9183. rc = pt_hid_output_get_sysinfo_(cd);
  9184. if (rc) {
  9185. pt_debug(cd->dev, DL_ERROR,
  9186. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9187. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9188. goto reset;
  9189. goto exit;
  9190. }
  9191. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9192. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9193. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9194. if (!rc) {
  9195. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9196. pt_debug(cd->dev, DL_WARN,
  9197. "%s: Exit FW BOOT Mode after %dms\n",
  9198. __func__, wait_time);
  9199. } else {
  9200. pt_debug(cd->dev, DL_WARN,
  9201. "%s: FW stuck in BOOT Mode after %dms\n",
  9202. __func__, wait_time);
  9203. goto exit;
  9204. }
  9205. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9206. __func__,
  9207. cd->sysinfo.ttdata.pip_ver_major,
  9208. cd->sysinfo.ttdata.pip_ver_minor);
  9209. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9210. if (rc) {
  9211. pt_debug(cd->dev, DL_ERROR,
  9212. "%s: DUT Config block CRC failure rc=%d\n",
  9213. __func__, rc);
  9214. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9215. goto reset;
  9216. } else {
  9217. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9218. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9219. pt_debug(cd->dev, DL_ERROR,
  9220. "%s: scan state: %d, retry: %d\n",
  9221. __func__, sys_mode, try);
  9222. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9223. goto reset;
  9224. } else
  9225. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9226. }
  9227. rc = pt_restore_parameters_(cd);
  9228. if (rc) {
  9229. pt_debug(cd->dev, DL_ERROR,
  9230. "%s: Failed to restore parameters rc=%d\n",
  9231. __func__, rc);
  9232. } else
  9233. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9234. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9235. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9236. cd->startup_retry_count = 0;
  9237. exit:
  9238. /* Generate the HW Version of the connected DUT and store in cd */
  9239. pt_generate_hw_version(cd, cd->hw_version);
  9240. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9241. cd->hw_version);
  9242. pt_start_wd_timer(cd);
  9243. if (!detected)
  9244. rc = -ENODEV;
  9245. #ifdef TTHE_TUNER_SUPPORT
  9246. tthe_print(cd, NULL, 0, "exit startup");
  9247. #endif
  9248. pt_debug(cd->dev, DL_WARN,
  9249. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9250. __func__, rc, try);
  9251. return rc;
  9252. }
  9253. /*******************************************************************************
  9254. * FUNCTION: pt_enum_with_dut
  9255. *
  9256. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9257. * requesting exclusive access around the call.
  9258. *
  9259. * RETURN:
  9260. * 0 = success
  9261. * !0 = failure
  9262. *
  9263. * PARAMETERS:
  9264. * *cd - pointer the core data structure
  9265. * reset - Flag to reset the DUT before attempting to enumerate
  9266. * *status - pointer to store the enum status bitmask flags
  9267. ******************************************************************************/
  9268. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9269. {
  9270. int rc = 0;
  9271. mutex_lock(&cd->system_lock);
  9272. cd->startup_state = STARTUP_RUNNING;
  9273. mutex_unlock(&cd->system_lock);
  9274. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9275. if (rc) {
  9276. pt_debug(cd->dev, DL_ERROR,
  9277. "%s: fail get exclusive ex=%p own=%p\n",
  9278. __func__, cd->exclusive_dev, cd->dev);
  9279. goto exit;
  9280. }
  9281. rc = pt_enum_with_dut_(cd, reset, status);
  9282. if (release_exclusive(cd, cd->dev) < 0)
  9283. /* Don't return fail code, mode is already changed. */
  9284. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9285. __func__);
  9286. else
  9287. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9288. __func__);
  9289. exit:
  9290. mutex_lock(&cd->system_lock);
  9291. /* Clear startup state for any tasks waiting for startup completion */
  9292. cd->startup_state = STARTUP_NONE;
  9293. mutex_unlock(&cd->system_lock);
  9294. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9295. *status |= STARTUP_STATUS_COMPLETE;
  9296. /* Wake the waiters for end of startup */
  9297. wake_up(&cd->wait_q);
  9298. return rc;
  9299. }
  9300. static int add_sysfs_interfaces(struct device *dev);
  9301. static void remove_sysfs_interfaces(struct device *dev);
  9302. static void remove_sysfs_and_modules(struct device *dev);
  9303. static void pt_release_modules(struct pt_core_data *cd);
  9304. static void pt_probe_modules(struct pt_core_data *cd);
  9305. /*******************************************************************************
  9306. * FUNCTION: _pt_ttdl_restart
  9307. *
  9308. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9309. *
  9310. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9311. * existing sysfs nodes will produce an error.
  9312. *
  9313. * RETURN:
  9314. * 0 = success
  9315. * !0 = failure
  9316. *
  9317. * PARAMETERS:
  9318. * *dev - pointer to core device
  9319. ******************************************************************************/
  9320. static int _pt_ttdl_restart(struct device *dev)
  9321. {
  9322. int rc = 0;
  9323. struct pt_core_data *cd = dev_get_drvdata(dev);
  9324. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9325. struct i2c_client *client =
  9326. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9327. #endif
  9328. /*
  9329. * Make sure the device is awake, pt_mt_release function will
  9330. * cause pm sleep function and lead to deadlock.
  9331. */
  9332. pm_runtime_get_sync(dev);
  9333. /* Use ttdl_restart_lock to avoid reentry */
  9334. mutex_lock(&cd->ttdl_restart_lock);
  9335. remove_sysfs_and_modules(cd->dev);
  9336. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9337. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9338. pt_debug(dev, DL_ERROR,
  9339. "%s I2C functionality not Supported\n", __func__);
  9340. rc = -EIO;
  9341. goto ttdl_no_error;
  9342. }
  9343. #endif
  9344. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9345. rc = _pt_detect_dut_generation(cd->dev,
  9346. &cd->startup_status, &cd->active_dut_generation,
  9347. &cd->mode);
  9348. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9349. pt_debug(dev, DL_ERROR,
  9350. "%s: Error, Unknown DUT Generation rc=%d\n",
  9351. __func__, rc);
  9352. }
  9353. }
  9354. rc = add_sysfs_interfaces(cd->dev);
  9355. if (rc < 0)
  9356. pt_debug(cd->dev, DL_ERROR,
  9357. "%s: Error, failed sysfs nodes rc=%d\n",
  9358. __func__, rc);
  9359. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9360. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9361. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9362. if (rc < 0)
  9363. pt_debug(dev, DL_ERROR,
  9364. "%s: Error, Failed to Enumerate\n", __func__);
  9365. }
  9366. rc = pt_mt_probe(dev);
  9367. if (rc < 0) {
  9368. pt_debug(dev, DL_ERROR,
  9369. "%s: Error, fail mt probe\n", __func__);
  9370. }
  9371. rc = pt_btn_probe(dev);
  9372. if (rc < 0) {
  9373. pt_debug(dev, DL_ERROR,
  9374. "%s: Error, fail btn probe\n", __func__);
  9375. }
  9376. pt_probe_modules(cd);
  9377. pt_debug(cd->dev, DL_WARN,
  9378. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9379. rc = 0;
  9380. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9381. ttdl_no_error:
  9382. #endif
  9383. mutex_unlock(&cd->ttdl_restart_lock);
  9384. mutex_lock(&cd->system_lock);
  9385. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9386. cd->startup_state = STARTUP_NONE;
  9387. mutex_unlock(&cd->system_lock);
  9388. pm_runtime_put(dev);
  9389. return rc;
  9390. }
  9391. /*******************************************************************************
  9392. * FUNCTION: pt_restart_work_function
  9393. *
  9394. * SUMMARY: This is the wrapper function placed in a work queue to call
  9395. * _pt_ttdl_restart()
  9396. *
  9397. * RETURN: none
  9398. *
  9399. * PARAMETERS:
  9400. * *work - pointer to the work_struct
  9401. ******************************************************************************/
  9402. static void pt_restart_work_function(struct work_struct *work)
  9403. {
  9404. struct pt_core_data *cd = container_of(work,
  9405. struct pt_core_data, ttdl_restart_work);
  9406. int rc = 0;
  9407. rc = _pt_ttdl_restart(cd->dev);
  9408. if (rc < 0)
  9409. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9410. __func__, rc);
  9411. }
  9412. /*******************************************************************************
  9413. * FUNCTION: pt_enum_work_function
  9414. *
  9415. * SUMMARY: This is the wrapper function placed in a work queue to call
  9416. * pt_enum_with_dut()
  9417. *
  9418. * RETURN: none
  9419. *
  9420. * PARAMETERS:
  9421. * *work - pointer to the work_struct
  9422. ******************************************************************************/
  9423. static void pt_enum_work_function(struct work_struct *work)
  9424. {
  9425. struct pt_core_data *cd = container_of(work,
  9426. struct pt_core_data, enum_work);
  9427. int rc;
  9428. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9429. if (rc < 0)
  9430. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9431. __func__, rc);
  9432. }
  9433. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9434. {
  9435. int rc;
  9436. if (!get) {
  9437. rc = 0;
  9438. goto regulator_put;
  9439. }
  9440. cd->vdd = regulator_get(cd->dev, "vdd");
  9441. if (IS_ERR(cd->vdd)) {
  9442. rc = PTR_ERR(cd->vdd);
  9443. dev_err(cd->dev,
  9444. "Regulator get failed vdd rc=%d\n", rc);
  9445. goto regulator_put;
  9446. }
  9447. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9448. if (IS_ERR(cd->vcc_i2c)) {
  9449. rc = PTR_ERR(cd->vcc_i2c);
  9450. dev_err(cd->dev,
  9451. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9452. goto regulator_put;
  9453. }
  9454. return 0;
  9455. regulator_put:
  9456. if (cd->vdd) {
  9457. regulator_put(cd->vdd);
  9458. cd->vdd = NULL;
  9459. }
  9460. if (cd->vcc_i2c) {
  9461. regulator_put(cd->vcc_i2c);
  9462. cd->vcc_i2c = NULL;
  9463. }
  9464. return rc;
  9465. }
  9466. #ifdef ENABLE_I2C_REG_ONLY
  9467. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9468. {
  9469. int rc = 0;
  9470. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9471. if (!en) {
  9472. rc = 0;
  9473. goto disable_vcc_i2c_reg_only;
  9474. }
  9475. if (cd->vcc_i2c) {
  9476. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9477. if (rc < 0)
  9478. pt_debug(cd->dev, DL_INFO,
  9479. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9480. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9481. __func__, rc);
  9482. }
  9483. return 0;
  9484. disable_vcc_i2c_reg_only:
  9485. if (cd->vcc_i2c) {
  9486. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9487. if (rc < 0)
  9488. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9489. __func__, rc);
  9490. }
  9491. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9492. return rc;
  9493. }
  9494. #endif
  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. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9519. }
  9520. if (cd->vcc_i2c) {
  9521. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9522. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9523. FT_I2C_VTG_MAX_UV);
  9524. if (rc) {
  9525. dev_err(cd->dev,
  9526. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9527. goto disable_vdd_reg;
  9528. }
  9529. }
  9530. rc = regulator_enable(cd->vcc_i2c);
  9531. if (rc) {
  9532. dev_err(cd->dev,
  9533. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9534. goto disable_vdd_reg;
  9535. }
  9536. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9537. }
  9538. return 0;
  9539. disable_vcc_i2c_reg:
  9540. if (cd->vcc_i2c) {
  9541. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9542. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9543. FT_I2C_VTG_MAX_UV);
  9544. regulator_disable(cd->vcc_i2c);
  9545. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9546. }
  9547. disable_vdd_reg:
  9548. if (cd->vdd) {
  9549. if (regulator_count_voltages(cd->vdd) > 0)
  9550. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9551. FT_VTG_MAX_UV);
  9552. regulator_disable(cd->vdd);
  9553. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9554. }
  9555. exit:
  9556. return rc;
  9557. }
  9558. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9559. #define KERNEL_VER_GT_3_19
  9560. #endif
  9561. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9562. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9563. #if defined(CONFIG_PM_SLEEP)
  9564. /*******************************************************************************
  9565. * FUNCTION: pt_core_rt_suspend
  9566. *
  9567. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9568. *
  9569. * RETURN:
  9570. * 0 = success
  9571. * !0 = failure
  9572. *
  9573. * PARAMETERS:
  9574. * *dev - pointer to core device
  9575. ******************************************************************************/
  9576. static int pt_core_rt_suspend(struct device *dev)
  9577. {
  9578. struct pt_core_data *cd = dev_get_drvdata(dev);
  9579. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9580. __func__, cd->core_probe_complete);
  9581. return 0;
  9582. }
  9583. /*******************************************************************************
  9584. * FUNCTION: pt_core_rt_resume
  9585. *
  9586. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9587. *
  9588. * RETURN:
  9589. * 0 = success
  9590. * !0 = failure
  9591. *
  9592. * PARAMETERS:
  9593. * *dev - pointer to core device
  9594. ******************************************************************************/
  9595. static int pt_core_rt_resume(struct device *dev)
  9596. {
  9597. struct pt_core_data *cd = dev_get_drvdata(dev);
  9598. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9599. __func__, cd->core_probe_complete);
  9600. return 0;
  9601. }
  9602. #endif /* CONFIG_PM_SLEEP */
  9603. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9604. #if defined(CONFIG_PM_SLEEP)
  9605. /*******************************************************************************
  9606. * FUNCTION: pt_core_suspend_
  9607. *
  9608. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9609. * pt_core_sleep. This function may disable IRQ during sleep state.
  9610. *
  9611. * RETURN:
  9612. * 0 = success
  9613. * !0 = failure
  9614. *
  9615. * PARAMETERS:
  9616. * *dev - pointer to core device
  9617. ******************************************************************************/
  9618. static int pt_core_suspend_(struct device *dev)
  9619. {
  9620. int rc;
  9621. struct pt_core_data *cd = dev_get_drvdata(dev);
  9622. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9623. rc = pt_core_sleep(cd);
  9624. if (rc) {
  9625. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9626. __func__, rc);
  9627. return -EAGAIN;
  9628. }
  9629. rc = pt_enable_regulator(cd, false);
  9630. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9631. return 0;
  9632. /* Required to prevent interrupts before bus awake */
  9633. disable_irq(cd->irq);
  9634. cd->irq_disabled = 1;
  9635. if (device_may_wakeup(dev)) {
  9636. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9637. __func__);
  9638. if (!enable_irq_wake(cd->irq))
  9639. cd->irq_wake = 1;
  9640. } else {
  9641. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9642. __func__);
  9643. }
  9644. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9645. return rc;
  9646. }
  9647. /*******************************************************************************
  9648. * FUNCTION: pt_core_suspend
  9649. *
  9650. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9651. * woke up or put to sleep based on Kernel power state even when the display
  9652. * is off based on the check of TTDL core platform flag.
  9653. *
  9654. * RETURN:
  9655. * 0 = success
  9656. * !0 = failure
  9657. *
  9658. * PARAMETERS:
  9659. * *dev - pointer to core device
  9660. ******************************************************************************/
  9661. static int pt_core_suspend(struct device *dev)
  9662. {
  9663. struct pt_core_data *cd = dev_get_drvdata(dev);
  9664. int rc = 0;
  9665. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9666. return 0;
  9667. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9668. cancel_work_sync(&cd->resume_work);
  9669. cancel_work_sync(&cd->suspend_work);
  9670. mutex_lock(&cd->system_lock);
  9671. cd->wait_until_wake = 0;
  9672. mutex_unlock(&cd->system_lock);
  9673. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9674. rc = pt_core_suspend_(cd->dev);
  9675. cd->quick_boot = true;
  9676. } else {
  9677. rc = pt_enable_i2c_regulator(cd, false);
  9678. if (rc < 0)
  9679. pt_debug(cd->dev, DL_ERROR,
  9680. "%s: Error on disabling i2c regulator\n", __func__);
  9681. }
  9682. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9683. return rc;
  9684. }
  9685. /*******************************************************************************
  9686. * FUNCTION: pt_core_resume_
  9687. *
  9688. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9689. * pt_core_wake. This function may enable IRQ before wake up.
  9690. *
  9691. * RETURN:
  9692. * 0 = success
  9693. * !0 = failure
  9694. *
  9695. * PARAMETERS:
  9696. * *dev - pointer to core device
  9697. ******************************************************************************/
  9698. static int pt_core_resume_(struct device *dev)
  9699. {
  9700. int rc = 0;
  9701. struct pt_core_data *cd = dev_get_drvdata(dev);
  9702. dev_info(dev, "%s: Entering into resume mode:\n",
  9703. __func__);
  9704. rc = pt_enable_regulator(cd, true);
  9705. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9706. __func__, rc);
  9707. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9708. goto exit;
  9709. /*
  9710. * Bus pm does not call suspend if device runtime suspended
  9711. * This flag is covers that case
  9712. */
  9713. if (cd->irq_disabled) {
  9714. enable_irq(cd->irq);
  9715. cd->irq_disabled = 0;
  9716. }
  9717. if (device_may_wakeup(dev)) {
  9718. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9719. __func__);
  9720. if (cd->irq_wake) {
  9721. disable_irq_wake(cd->irq);
  9722. cd->irq_wake = 0;
  9723. }
  9724. } else {
  9725. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9726. __func__);
  9727. }
  9728. exit:
  9729. rc = pt_core_wake(cd);
  9730. if (rc) {
  9731. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9732. __func__, rc);
  9733. pt_enable_regulator(cd, false);
  9734. return -EAGAIN;
  9735. }
  9736. return rc;
  9737. }
  9738. /*******************************************************************************
  9739. * FUNCTION: pt_core_restore
  9740. *
  9741. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9742. * pt_core_wake. This function may enable IRQ before wake up.
  9743. *
  9744. * RETURN:
  9745. * 0 = success
  9746. * !0 = failure
  9747. *
  9748. * PARAMETERS:
  9749. * *dev - pointer to core device
  9750. ******************************************************************************/
  9751. static int pt_core_restore(struct device *dev)
  9752. {
  9753. int rc = 0;
  9754. struct pt_core_data *cd = dev_get_drvdata(dev);
  9755. dev_info(dev, "%s: Entering into resume mode:\n",
  9756. __func__);
  9757. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9758. return rc;
  9759. }
  9760. /*******************************************************************************
  9761. * FUNCTION: suspend_offload_work
  9762. *
  9763. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9764. * along with kernel power state even the display is off based on the check of
  9765. * TTDL core platform flag.
  9766. *
  9767. * RETURN:
  9768. * 0 = success
  9769. * !0 = failure
  9770. *
  9771. * PARAMETERS:
  9772. * *dev - pointer to core device
  9773. ******************************************************************************/
  9774. static void pt_suspend_offload_work(struct work_struct *work)
  9775. {
  9776. int rc = 0;
  9777. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9778. suspend_offload_work);
  9779. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9780. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9781. return;
  9782. rc = pt_core_suspend_(cd->dev);
  9783. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9784. }
  9785. /*******************************************************************************
  9786. * FUNCTION: resume_offload_work
  9787. *
  9788. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9789. * along with kernel power state even the display is off based on the check of
  9790. * TTDL core platform flag.
  9791. *
  9792. * RETURN:
  9793. * 0 = success
  9794. * !0 = failure
  9795. *
  9796. * PARAMETERS:
  9797. * *dev - pointer to core device
  9798. ******************************************************************************/
  9799. static void pt_resume_offload_work(struct work_struct *work)
  9800. {
  9801. int rc = 0;
  9802. int retry_count = 10;
  9803. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9804. resume_offload_work);
  9805. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9806. do {
  9807. retry_count--;
  9808. rc = pt_core_resume_(pt_data->dev);
  9809. if (rc < 0)
  9810. pt_debug(pt_data->dev, DL_ERROR,
  9811. "%s: Error on wake\n", __func__);
  9812. } while (retry_count && rc < 0);
  9813. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9814. rc = pt_core_easywake_on(pt_data);
  9815. if (rc < 0) {
  9816. pt_debug(pt_data->dev, DL_ERROR,
  9817. "%s: Error on enable touch to wake key\n",
  9818. __func__);
  9819. return;
  9820. }
  9821. pt_data->fb_state = FB_OFF;
  9822. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9823. #endif
  9824. pt_data->quick_boot = false;
  9825. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9826. }
  9827. /*******************************************************************************
  9828. * FUNCTION: pt_core_resume
  9829. *
  9830. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9831. * along with kernel power state even the display is off based on the check of
  9832. * TTDL core platform flag.
  9833. *
  9834. * RETURN:
  9835. * 0 = success
  9836. * !0 = failure
  9837. *
  9838. * PARAMETERS:
  9839. * *dev - pointer to core device
  9840. ******************************************************************************/
  9841. static int pt_core_resume(struct device *dev)
  9842. {
  9843. struct pt_core_data *cd = dev_get_drvdata(dev);
  9844. int rc = 0;
  9845. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9846. return 0;
  9847. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9848. rc = pt_core_restore(cd->dev);
  9849. } else {
  9850. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9851. rc = pt_enable_i2c_regulator(cd, true);
  9852. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9853. }
  9854. mutex_lock(&cd->system_lock);
  9855. cd->wait_until_wake = 1;
  9856. mutex_unlock(&cd->system_lock);
  9857. wake_up(&cd->wait_q);
  9858. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9859. return rc;
  9860. }
  9861. #endif
  9862. #ifdef NEED_SUSPEND_NOTIFIER
  9863. /*******************************************************************************
  9864. * FUNCTION: pt_pm_notifier
  9865. *
  9866. * SUMMARY: This function is registered to notifier chain and will perform
  9867. * suspend operation if match event PM_SUSPEND_PREPARE.
  9868. *
  9869. * RETURN:
  9870. * 0 = success
  9871. * !0 = failure
  9872. *
  9873. * PARAMETERS:
  9874. * *nb - pointer to notifier_block structure
  9875. * action - notifier event type
  9876. * *data - void pointer
  9877. ******************************************************************************/
  9878. static int pt_pm_notifier(struct notifier_block *nb,
  9879. unsigned long action, void *data)
  9880. {
  9881. struct pt_core_data *cd = container_of(nb,
  9882. struct pt_core_data, pm_notifier);
  9883. if (action == PM_SUSPEND_PREPARE) {
  9884. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9885. __func__);
  9886. /*
  9887. * If PM runtime is not suspended, either call runtime
  9888. * PM suspend callback or wait until it finishes
  9889. */
  9890. if (!pm_runtime_suspended(cd->dev))
  9891. pm_runtime_suspend(cd->dev);
  9892. (void) pt_core_suspend(cd->dev);
  9893. }
  9894. return NOTIFY_DONE;
  9895. }
  9896. #endif
  9897. const struct dev_pm_ops pt_pm_ops = {
  9898. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9899. .freeze = pt_core_suspend,
  9900. .restore = pt_core_restore,
  9901. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9902. NULL)
  9903. };
  9904. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9905. /*******************************************************************************
  9906. * FUNCTION: _pt_request_pip2_enter_bl
  9907. *
  9908. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9909. * XRES pin or a soft reset.
  9910. *
  9911. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9912. * the WD active could cause this function to fail!
  9913. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9914. * will not try to determine the current mode but will proceed with
  9915. * resetting the DUT and entering the BL.
  9916. *
  9917. * NOTE: The definition of result code:
  9918. * PT_ENTER_BL_PASS (0)
  9919. * PT_ENTER_BL_ERROR (1)
  9920. * PT_ENTER_BL_RESET_FAIL (2)
  9921. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9922. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9923. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9924. *
  9925. * RETURNS:
  9926. * 0 = success
  9927. * !0 = failure
  9928. *
  9929. *
  9930. * PARAMETERS:
  9931. * *dev - pointer to device structure
  9932. * *start_mode - pointer to the mode the DUT was in when this function
  9933. * starts
  9934. * *result - pointer to store the result when to enter BL
  9935. ******************************************************************************/
  9936. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9937. {
  9938. int rc = 0;
  9939. int t;
  9940. int tmp_result = PT_ENTER_BL_ERROR;
  9941. int flash_info_retry = 2;
  9942. u8 mode = PT_MODE_UNKNOWN;
  9943. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9944. u8 read_buf[32];
  9945. u16 actual_read_len;
  9946. struct pt_core_data *cd = dev_get_drvdata(dev);
  9947. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9948. u8 time = 0;
  9949. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9950. if (cd->watchdog_enabled) {
  9951. pt_debug(dev, DL_WARN,
  9952. "%s: Watchdog must be stopped before entering BL\n",
  9953. __func__);
  9954. goto exit;
  9955. }
  9956. cancel_work_sync(&cd->enum_work);
  9957. cancel_work_sync(&cd->watchdog_work);
  9958. /* if undefined assume operational/test to bypass all checks */
  9959. if (*start_mode == PT_MODE_IGNORE) {
  9960. mode = PT_MODE_OPERATIONAL;
  9961. sys_mode = FW_SYS_MODE_TEST;
  9962. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9963. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9964. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9965. if (rc) {
  9966. pt_debug(dev, DL_ERROR,
  9967. "%s: Get mode failed, mode unknown\n",
  9968. __func__);
  9969. }
  9970. *start_mode = mode;
  9971. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9972. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9973. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9974. sys_mode = FW_SYS_MODE_SCANNING;
  9975. }
  9976. _retry:
  9977. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9978. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9979. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9980. switch (mode) {
  9981. case PT_MODE_UNKNOWN:
  9982. /*
  9983. * When the mode could not be determined the DUT could be
  9984. * in App mode running corrupted FW or FW that is not
  9985. * responding to the mode request, assume no communication
  9986. * and do a hard reset
  9987. */
  9988. mutex_lock(&cd->system_lock);
  9989. cd->startup_status = STARTUP_STATUS_START;
  9990. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9991. mutex_unlock(&cd->system_lock);
  9992. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9993. if (rc) {
  9994. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9995. goto exit;
  9996. }
  9997. break;
  9998. case PT_MODE_OPERATIONAL:
  9999. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10000. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10001. __func__);
  10002. rc = pt_pip_suspend_scanning_(cd);
  10003. if (rc) {
  10004. /*
  10005. * Print to log but don't exit, the FW could be
  10006. * running but be hung or fail to respond to
  10007. * this request
  10008. */
  10009. pt_debug(dev, DL_ERROR,
  10010. "%s Suspend Scan Failed\n", __func__);
  10011. }
  10012. /* sleep to allow the suspend scan to be processed */
  10013. usleep_range(1000, 2000);
  10014. }
  10015. mutex_lock(&cd->system_lock);
  10016. cd->startup_status = STARTUP_STATUS_START;
  10017. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10018. mutex_unlock(&cd->system_lock);
  10019. /* Reset device to enter the BL */
  10020. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10021. if (rc) {
  10022. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10023. goto exit;
  10024. }
  10025. break;
  10026. case PT_MODE_BOOTLOADER:
  10027. /* Do nothing as we are already in the BL */
  10028. tmp_result = PT_ENTER_BL_PASS;
  10029. goto exit;
  10030. default:
  10031. /* Should NEVER get here */
  10032. tmp_result = PT_ENTER_BL_ERROR;
  10033. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10034. goto exit;
  10035. }
  10036. if (!cd->flashless_dut &&
  10037. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10038. /*
  10039. * Sending the special "Host Mode" command will instruct the
  10040. * BL to not execute the FW it has loaded into RAM.
  10041. * The command must be sent within a 40ms window from releasing
  10042. * the XRES pin. If the messages is sent too early it will NAK,
  10043. * so keep sending it every 2ms until it is accepted by the BL.
  10044. * A no-flash DUT does not require this command as there is no
  10045. * FW for the BL to load and execute.
  10046. */
  10047. usleep_range(4000, 6000);
  10048. pt_debug(cd->dev, DL_INFO,
  10049. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10050. __func__, (int)sizeof(host_mode_cmd));
  10051. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10052. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10053. rc = 1;
  10054. while (rc && time < 34) {
  10055. rc = pt_adap_write_read_specific(cd, 4,
  10056. host_mode_cmd, NULL);
  10057. usleep_range(1800, 2000);
  10058. time += 2;
  10059. }
  10060. /* Sleep to allow the BL to come up */
  10061. usleep_range(1000, 2000);
  10062. }
  10063. /*
  10064. * To avoid the case that next PIP command can be confused by BL/FW
  10065. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10066. * followed by "wakeup event" function can lower the failure rate.
  10067. */
  10068. t = wait_event_timeout(cd->wait_q,
  10069. ((cd->startup_status != STARTUP_STATUS_START)
  10070. && (cd->hid_reset_cmd_state == 0)),
  10071. msecs_to_jiffies(300));
  10072. if (IS_TMO(t)) {
  10073. pt_debug(cd->dev, DL_ERROR,
  10074. "%s: TMO waiting for BL sentinel\n", __func__);
  10075. }
  10076. /* Check if device is now in BL mode */
  10077. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10078. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10079. cd->startup_status);
  10080. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10081. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10082. mutex_lock(&cd->system_lock);
  10083. cd->pip2_prot_active = true;
  10084. cd->mode = PT_MODE_BOOTLOADER;
  10085. mutex_unlock(&cd->system_lock);
  10086. tmp_result = PT_ENTER_BL_PASS;
  10087. } else {
  10088. /*
  10089. * If the device doesn't enter BL mode as expected and rc is
  10090. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10091. * the function should return an error code to indicate this
  10092. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10093. */
  10094. if (!rc)
  10095. rc = -EINVAL;
  10096. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10097. mutex_lock(&cd->system_lock);
  10098. cd->mode = mode;
  10099. mutex_unlock(&cd->system_lock);
  10100. pt_debug(dev, DL_ERROR,
  10101. "%s ERROR: Not in BL as expected", __func__);
  10102. }
  10103. exit:
  10104. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10105. /* Check to get (buffered) flash information */
  10106. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10107. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10108. read_buf, &actual_read_len);
  10109. if (!rc) {
  10110. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10111. pt_debug(
  10112. dev, DL_WARN,
  10113. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10114. __func__,
  10115. read_buf[PIP2_RESP_BODY_OFFSET]);
  10116. /*
  10117. * If the BL was unable to cache the correct
  10118. * values when entering the first time due to
  10119. * the Flash part not having been powered up
  10120. * long enough, re-enter the BL to trigger the
  10121. * BL to re-attempt to cache the values.
  10122. */
  10123. if (flash_info_retry-- > 0) {
  10124. mode = PT_MODE_UNKNOWN;
  10125. pt_debug(dev, DL_WARN,
  10126. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10127. __func__, flash_info_retry);
  10128. goto _retry;
  10129. } else {
  10130. pt_debug(dev, DL_WARN,
  10131. "%s Manufacturer ID Unknown\n",
  10132. __func__);
  10133. tmp_result = PT_ENTER_BL_PASS;
  10134. }
  10135. }
  10136. } else {
  10137. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10138. pt_debug(
  10139. dev, DL_ERROR,
  10140. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10141. __func__);
  10142. }
  10143. }
  10144. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10145. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10146. "SUPPRESS");
  10147. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10148. if (result)
  10149. *result = tmp_result;
  10150. return rc;
  10151. }
  10152. /*******************************************************************************
  10153. * FUNCTION: _pt_pip2_file_open
  10154. *
  10155. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10156. *
  10157. * NOTE: The DUT must be in BL mode for this command to work
  10158. *
  10159. * RETURNS:
  10160. * <0 = Error
  10161. * >0 = file handle opened
  10162. *
  10163. * PARAMETERS:
  10164. * *dev - pointer to device structure
  10165. * file_no - PIP2 file number to open
  10166. ******************************************************************************/
  10167. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10168. {
  10169. int ret = 0;
  10170. u16 status;
  10171. u16 actual_read_len;
  10172. u8 file_handle;
  10173. u8 data[2];
  10174. u8 read_buf[10];
  10175. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10176. data[0] = file_no;
  10177. ret = _pt_request_pip2_send_cmd(dev,
  10178. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10179. data, 1, read_buf, &actual_read_len);
  10180. if (ret) {
  10181. pt_debug(dev, DL_ERROR,
  10182. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10183. __func__, file_no);
  10184. return -PIP2_RSP_ERR_NOT_OPEN;
  10185. }
  10186. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10187. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10188. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10189. (file_handle != file_no)) {
  10190. pt_debug(dev, DL_ERROR,
  10191. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10192. __func__, status, file_handle);
  10193. return -status;
  10194. }
  10195. return file_handle;
  10196. }
  10197. /*******************************************************************************
  10198. * FUNCTION: _pt_pip2_file_close
  10199. *
  10200. * SUMMARY: Using the BL PIP2 commands close a file
  10201. *
  10202. * NOTE: The DUT must be in BL mode for this command to work
  10203. *
  10204. * RETURNS:
  10205. * <0 = Error
  10206. * >0 = file handle closed
  10207. *
  10208. * PARAMETERS:
  10209. * *dev - pointer to device structure
  10210. * file_handle - handle to the file to be closed
  10211. ******************************************************************************/
  10212. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10213. {
  10214. int ret = 0;
  10215. u16 status;
  10216. u16 actual_read_len;
  10217. u8 data[2];
  10218. u8 read_buf[32];
  10219. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10220. data[0] = file_handle;
  10221. ret = _pt_request_pip2_send_cmd(dev,
  10222. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10223. data, 1, read_buf, &actual_read_len);
  10224. if (ret) {
  10225. pt_debug(dev, DL_ERROR,
  10226. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10227. __func__, file_handle);
  10228. return -ETIME;
  10229. }
  10230. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10231. if (status != 0x00) {
  10232. pt_debug(dev, DL_ERROR,
  10233. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10234. __func__, status, file_handle);
  10235. return -status;
  10236. }
  10237. return file_handle;
  10238. }
  10239. /*******************************************************************************
  10240. * FUNCTION: _pt_pip2_file_erase
  10241. *
  10242. * SUMMARY: Using the BL PIP2 commands erase a file
  10243. *
  10244. * NOTE: The DUT must be in BL mode for this command to work
  10245. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10246. * one retry is attempted for each sector in a file.
  10247. *
  10248. * RETURNS:
  10249. * <0 = Error
  10250. * >0 = file handle closed
  10251. *
  10252. * PARAMETERS:
  10253. * *dev - pointer to device structure
  10254. * file_handle - handle to the file to be erased
  10255. * *status - PIP2 erase status code
  10256. ******************************************************************************/
  10257. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10258. {
  10259. int ret = 0;
  10260. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10261. int retry = 1;
  10262. u16 actual_read_len;
  10263. u8 data[2];
  10264. u8 read_buf[10];
  10265. struct pt_core_data *cd = dev_get_drvdata(dev);
  10266. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10267. data[0] = file_handle;
  10268. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10269. *status = PIP2_RSP_ERR_TIMEOUT;
  10270. /* Increase waiting time for large file erase */
  10271. mutex_lock(&cd->system_lock);
  10272. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10273. mutex_unlock(&cd->system_lock);
  10274. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10275. ret = _pt_request_pip2_send_cmd(dev,
  10276. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10277. data, 2, read_buf, &actual_read_len);
  10278. if (ret)
  10279. break;
  10280. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10281. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10282. #ifdef TTDL_DIAGNOSTICS
  10283. cd->file_erase_timeout_count++;
  10284. #endif
  10285. pt_debug(dev, DL_WARN,
  10286. "%s: ERASE timeout %d for file = %d\n",
  10287. __func__, retry, file_handle);
  10288. }
  10289. retry++;
  10290. }
  10291. mutex_lock(&cd->system_lock);
  10292. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10293. mutex_unlock(&cd->system_lock);
  10294. if (ret) {
  10295. pt_debug(dev, DL_ERROR,
  10296. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10297. __func__, ret, file_handle);
  10298. return -EIO;
  10299. }
  10300. if (*status != 0x00) {
  10301. pt_debug(dev, DL_ERROR,
  10302. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10303. __func__, *status, file_handle);
  10304. return -EIO;
  10305. }
  10306. return file_handle;
  10307. }
  10308. /*******************************************************************************
  10309. * FUNCTION: _pt_pip2_file_read
  10310. *
  10311. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10312. *
  10313. * NOTE: The DUT must be in BL mode for this command to work
  10314. *
  10315. * RETURNS:
  10316. * <0 = Error
  10317. * >0 = number of bytes read
  10318. *
  10319. * PARAMETERS:
  10320. * *dev - pointer to device structure
  10321. * file_handle - File handle to read from
  10322. * num_bytes - number of bytes to read
  10323. ******************************************************************************/
  10324. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10325. u8 *read_buf)
  10326. {
  10327. int ret = 0;
  10328. u16 status;
  10329. u16 actual_read_len;
  10330. u8 data[3];
  10331. data[0] = file_handle;
  10332. data[1] = (num_bytes & 0x00FF);
  10333. data[2] = (num_bytes & 0xFF00) >> 8;
  10334. ret = _pt_request_pip2_send_cmd(dev,
  10335. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10336. data, 3, read_buf, &actual_read_len);
  10337. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10338. if (ret || ((status != 0x00) && (status != 0x03))) {
  10339. pt_debug(dev, DL_ERROR,
  10340. "%s File open failure with error code = %d\n",
  10341. __func__, status);
  10342. return -EPERM;
  10343. }
  10344. ret = num_bytes;
  10345. return ret;
  10346. }
  10347. /*******************************************************************************
  10348. * FUNCTION: _pt_read_us_file
  10349. *
  10350. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10351. * then read the entire file.
  10352. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10353. *
  10354. * RETURN:
  10355. * 0 = success
  10356. * !0 = failure
  10357. *
  10358. * PARAMETERS:
  10359. * *file_path - pointer to the file path
  10360. * *buf - pointer to the buffer to store the file contents
  10361. * *size - pointer to the size of the file
  10362. ******************************************************************************/
  10363. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10364. {
  10365. struct file *filp = NULL;
  10366. struct inode *inode = NULL;
  10367. unsigned int file_len = 0;
  10368. unsigned int read_len = 0;
  10369. mm_segment_t oldfs;
  10370. int rc = 0;
  10371. if (file_path == NULL || buf == NULL) {
  10372. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10373. return -EINVAL;
  10374. }
  10375. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10376. oldfs = get_fs();
  10377. set_fs(KERNEL_DS);
  10378. filp = filp_open(file_path, O_RDONLY, 0400);
  10379. if (IS_ERR(filp)) {
  10380. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10381. file_path);
  10382. rc = -ENOENT;
  10383. goto err;
  10384. }
  10385. if (filp->f_op == NULL) {
  10386. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10387. __func__);
  10388. rc = -EINVAL;
  10389. goto exit;
  10390. }
  10391. inode = filp->f_path.dentry->d_inode;
  10392. if (inode == NULL) {
  10393. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10394. __func__);
  10395. rc = -EINVAL;
  10396. goto exit;
  10397. }
  10398. file_len = i_size_read(inode->i_mapping->host);
  10399. if (file_len == 0) {
  10400. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10401. __func__, file_len);
  10402. rc = -EINVAL;
  10403. goto exit;
  10404. }
  10405. if (*size == 0)
  10406. read_len = file_len;
  10407. else
  10408. read_len = *size;
  10409. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10410. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10411. __func__, read_len);
  10412. rc = -EINVAL;
  10413. goto exit;
  10414. }
  10415. filp->private_data = inode->i_private;
  10416. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10417. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10418. rc = -EINVAL;
  10419. goto exit;
  10420. }
  10421. *size = read_len;
  10422. exit:
  10423. if (filp_close(filp, NULL) != 0)
  10424. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10425. err:
  10426. set_fs(oldfs);
  10427. return rc;
  10428. }
  10429. /*******************************************************************************
  10430. * FUNCTION: _pt_request_pip2_bin_hdr
  10431. *
  10432. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10433. * in the case of a flashless DUT, and parse the contents
  10434. *
  10435. * RETURNS:
  10436. * 0 = Success
  10437. * !0 = Error condition
  10438. *
  10439. * PARAMETERS:
  10440. * *dev - pointer to device structure
  10441. ******************************************************************************/
  10442. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10443. {
  10444. struct pt_core_data *cd = dev_get_drvdata(dev);
  10445. u8 file_handle;
  10446. u8 read_buf[255];
  10447. u8 hdr_len = 0;
  10448. u8 i;
  10449. int bytes_read;
  10450. int read_size;
  10451. int ret = 0;
  10452. int rc = 0;
  10453. bool load_hdr_struct = false;
  10454. if (cd->flashless_dut) {
  10455. read_size = sizeof(read_buf);
  10456. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10457. read_buf, &read_size);
  10458. if (rc) {
  10459. ret = rc;
  10460. pt_debug(dev, DL_ERROR,
  10461. "%s Failed to read fw image from US, rc=%d\n",
  10462. __func__, rc);
  10463. goto exit;
  10464. }
  10465. load_hdr_struct = true;
  10466. hdr_len = read_buf[0];
  10467. i = 0;
  10468. } else {
  10469. if (cd->mode != PT_MODE_BOOTLOADER) {
  10470. ret = -EPERM;
  10471. goto exit;
  10472. }
  10473. /* Open the bin file in Flash */
  10474. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10475. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10476. if (file_handle != PIP2_FW_FILE) {
  10477. ret = -ENOENT;
  10478. pt_debug(dev, DL_ERROR,
  10479. "%s Failed to open bin file\n", __func__);
  10480. goto exit;
  10481. }
  10482. /* Read the header length from the file */
  10483. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10484. read_size = 1;
  10485. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10486. read_buf);
  10487. if (bytes_read != read_size) {
  10488. ret = -EX_ERR_FREAD;
  10489. pt_debug(dev, DL_ERROR,
  10490. "%s Failed to bin file header len\n", __func__);
  10491. goto exit_close_file;
  10492. }
  10493. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10494. if (hdr_len == 0xFF) {
  10495. ret = -EX_ERR_FLEN;
  10496. pt_debug(dev, DL_ERROR,
  10497. "%s Bin header len is invalid\n", __func__);
  10498. goto exit_close_file;
  10499. }
  10500. /* Read the rest of the header from the bin file */
  10501. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10502. memset(read_buf, 0, sizeof(read_buf));
  10503. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10504. read_buf);
  10505. if (bytes_read != hdr_len) {
  10506. ret = -EX_ERR_FREAD;
  10507. pt_debug(dev, DL_ERROR,
  10508. "%s Failed to read bin file\n", __func__);
  10509. goto exit_close_file;
  10510. }
  10511. load_hdr_struct = true;
  10512. exit_close_file:
  10513. /* Close the file */
  10514. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10515. ret = -EX_ERR_FCLOSE;
  10516. pt_debug(dev, DL_ERROR,
  10517. "%s Failed to close bin file\n", __func__);
  10518. }
  10519. /*
  10520. * The length was already read so subtract 1 to make the rest of
  10521. * the offsets match the spec
  10522. */
  10523. i = PIP2_RESP_BODY_OFFSET - 1;
  10524. }
  10525. if (load_hdr_struct) {
  10526. hdr->length = hdr_len;
  10527. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10528. hdr->fw_major = (read_buf[i+3]);
  10529. hdr->fw_minor = (read_buf[i+4]);
  10530. hdr->fw_crc = (read_buf[i+5] << 24) |
  10531. (read_buf[i+6] << 16) |
  10532. (read_buf[i+7] << 8) |
  10533. (read_buf[i+8]);
  10534. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10535. (read_buf[i+10] << 16) |
  10536. (read_buf[i+11] << 8) |
  10537. (read_buf[i+12]);
  10538. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10539. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10540. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10541. if (hdr_len >= 22) {
  10542. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10543. (read_buf[i+20] << 16) |
  10544. (read_buf[i+21] << 8) |
  10545. (read_buf[i+22]);
  10546. } else {
  10547. hdr->hex_file_size = 0;
  10548. }
  10549. }
  10550. exit:
  10551. return ret;
  10552. }
  10553. /*******************************************************************************
  10554. * FUNCTION: _pt_pip2_file_get_stats
  10555. *
  10556. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10557. * opened file
  10558. *
  10559. * NOTE: The DUT must be in BL mode for this command to work
  10560. *
  10561. * RETURNS:
  10562. * !0 = Error
  10563. * 0 = Success
  10564. *
  10565. * PARAMETERS:
  10566. * *dev - pointer to device structure
  10567. * file_handle - File handle to read from
  10568. * *address - pointer to store address of file
  10569. * *file_size _ pointer to store size of file
  10570. ******************************************************************************/
  10571. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10572. u32 *file_size)
  10573. {
  10574. int ret = 1;
  10575. u16 status;
  10576. u16 actual_read_len;
  10577. u8 data[2];
  10578. u8 read_buf[16];
  10579. data[0] = file_handle;
  10580. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10581. ret = _pt_request_pip2_send_cmd(dev,
  10582. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10583. data, 2, read_buf, &actual_read_len);
  10584. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10585. if (ret || (status != 0x00)) {
  10586. pt_debug(dev, DL_ERROR,
  10587. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10588. __func__, status, file_handle, ret);
  10589. ret = -EIO;
  10590. goto exit;
  10591. }
  10592. pt_debug(dev, DL_DEBUG,
  10593. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10594. if (address) {
  10595. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10596. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10597. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10598. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10599. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10600. __func__, *address);
  10601. }
  10602. if (file_size) {
  10603. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10604. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10605. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10606. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10607. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10608. __func__, *file_size);
  10609. }
  10610. exit:
  10611. return ret;
  10612. }
  10613. /*******************************************************************************
  10614. * FUNCTION: _pt_pip2_file_seek_offset
  10615. *
  10616. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10617. * opened file
  10618. *
  10619. * NOTE: The DUT must be in BL mode for this command to work
  10620. * NOTE: File open/erase command can reset the offset
  10621. *
  10622. * RETURNS:
  10623. * !0 = Error
  10624. * 0 = Success
  10625. *
  10626. * PARAMETERS:
  10627. * *dev - pointer to device structure
  10628. * file_handle - File handle to read from
  10629. * read_offset - read offset of file
  10630. * write_offset - write offset of file
  10631. ******************************************************************************/
  10632. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10633. u32 read_offset, u32 write_offset)
  10634. {
  10635. int ret = 1;
  10636. u16 status;
  10637. u16 actual_read_len;
  10638. u8 data[10];
  10639. u8 read_buf[16];
  10640. data[0] = file_handle;
  10641. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10642. data[2] = 0xff & read_offset;
  10643. data[3] = 0xff & (read_offset >> 8);
  10644. data[4] = 0xff & (read_offset >> 16);
  10645. data[5] = 0xff & (read_offset >> 24);
  10646. data[6] = 0xff & write_offset;
  10647. data[7] = 0xff & (write_offset >> 8);
  10648. data[8] = 0xff & (write_offset >> 16);
  10649. data[9] = 0xff & (write_offset >> 24);
  10650. ret = _pt_request_pip2_send_cmd(dev,
  10651. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10652. data, 10, read_buf, &actual_read_len);
  10653. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10654. if (ret || (status != 0x00)) {
  10655. pt_debug(dev, DL_ERROR,
  10656. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10657. __func__, status, ret, file_handle);
  10658. ret = -EIO;
  10659. }
  10660. return ret;
  10661. }
  10662. /*******************************************************************************
  10663. * FUNCTION: _pt_pip2_file_crc
  10664. *
  10665. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10666. * the file.
  10667. *
  10668. * NOTE: The DUT must be in BL mode for this command to work
  10669. * NOTE: This command only can be used for BL version 1.8 or greater.
  10670. * BL version 1.8 added this change according to PGV-173.
  10671. *
  10672. * RETURNS:
  10673. * !0 = Error
  10674. * 0 = Success
  10675. *
  10676. * PARAMETERS:
  10677. * *dev - pointer to device structure
  10678. * file_handle - File handle to read from
  10679. * offset - start offset for CRC calculation
  10680. * length - number of bytes to calculate CRC over
  10681. * read_buf - pointer to the read buffer
  10682. ******************************************************************************/
  10683. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10684. u32 offset, u32 length, u8 *read_buf)
  10685. {
  10686. int rc = 1;
  10687. u16 actual_read_len;
  10688. u8 data[10];
  10689. data[0] = file_handle;
  10690. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10691. data[2] = 0xff & offset;
  10692. data[3] = 0xff & (offset >> 8);
  10693. data[4] = 0xff & (offset >> 16);
  10694. data[5] = 0xff & (offset >> 24);
  10695. data[6] = 0xff & length;
  10696. data[7] = 0xff & (length >> 8);
  10697. data[8] = 0xff & (length >> 16);
  10698. data[9] = 0xff & (length >> 24);
  10699. rc = _pt_request_pip2_send_cmd(dev,
  10700. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10701. data, 10, read_buf, &actual_read_len);
  10702. if (rc)
  10703. pt_debug(dev, DL_ERROR,
  10704. "%s Return FILE_CRC failure, rc = %d\n",
  10705. __func__, rc);
  10706. return rc;
  10707. }
  10708. /*******************************************************************************
  10709. * FUNCTION: pt_pip2_ping_test
  10710. *
  10711. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10712. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10713. * response payload matches what was sent.
  10714. * The max payload size is 247:
  10715. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10716. *
  10717. * RETURN:
  10718. * 0 = success
  10719. * !0 = failure
  10720. *
  10721. * PARAMETERS:
  10722. * *dev - pointer to device structure
  10723. * *attr - pointer to device attributes
  10724. * *buf - pointer to output buffer
  10725. ******************************************************************************/
  10726. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10727. {
  10728. u16 actual_read_len;
  10729. u8 *read_buf = NULL;
  10730. u8 *data = NULL;
  10731. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10732. int i = 1;
  10733. int j = 0;
  10734. int rc = 0;
  10735. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10736. if (!read_buf)
  10737. goto ping_test_exit;
  10738. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10739. if (!data)
  10740. goto ping_test_exit;
  10741. /* Load data payload with an array of walking 1's */
  10742. for (i = 0; i < 255; i++)
  10743. data[i] = 0x01 << (i % 8);
  10744. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10745. for (i = 0; i <= max_bytes; i++) {
  10746. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10747. PIP2_CMD_ID_PING, data, i, read_buf,
  10748. &actual_read_len);
  10749. if (rc) {
  10750. pt_debug(dev, DL_ERROR,
  10751. "%s: PING failed with %d byte payload\n",
  10752. __func__, i);
  10753. break;
  10754. }
  10755. /* Verify data returned matches data sent */
  10756. for (j = 0; j < i; j++) {
  10757. if (read_buf[data_offset + j] != data[j]) {
  10758. pt_debug(dev, DL_DEBUG,
  10759. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10760. __func__, i, j, data[j], j,
  10761. read_buf[data_offset + j]);
  10762. goto ping_test_exit;
  10763. }
  10764. }
  10765. }
  10766. ping_test_exit:
  10767. *last_packet_size = i - 1;
  10768. kfree(read_buf);
  10769. kfree(data);
  10770. return rc;
  10771. }
  10772. /*******************************************************************************
  10773. * FUNCTION: _pt_ic_parse_input_hex
  10774. *
  10775. * SUMMARY: Parse a char data array as space delimited hex values into
  10776. * an int array.
  10777. *
  10778. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10779. * buffer to store data is "u32". This function is still needed by the
  10780. * "command" sysfs node because the buffer type to store data is "u8".
  10781. *
  10782. * RETURN: Length of parsed data
  10783. *
  10784. * PARAMETERS:
  10785. * *dev - pointer to device structure
  10786. * *buf - pointer to buffer that holds the input array to parse
  10787. * buf_size - size of buf
  10788. * *ic_buf - pointer to array to store parsed data
  10789. * ic_buf_size - max size of ic_buf
  10790. ******************************************************************************/
  10791. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10792. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10793. {
  10794. const char *pbuf = buf;
  10795. unsigned long value;
  10796. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10797. u32 i = 0;
  10798. u32 j;
  10799. int last = 0;
  10800. int ret;
  10801. pt_debug(dev, DL_DEBUG,
  10802. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10803. __func__, pbuf, buf, buf_size, "scan buf size",
  10804. PT_MAX_PIP2_MSG_SIZE, buf);
  10805. while (pbuf <= (buf + buf_size)) {
  10806. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10807. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10808. __func__, "Max cmd size exceeded", i,
  10809. PT_MAX_PIP2_MSG_SIZE);
  10810. return -EINVAL;
  10811. }
  10812. if (i >= ic_buf_size) {
  10813. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10814. __func__, "Buffer size exceeded", i,
  10815. ic_buf_size);
  10816. return -EINVAL;
  10817. }
  10818. while (((*pbuf == ' ') || (*pbuf == ','))
  10819. && (pbuf < (buf + buf_size))) {
  10820. last = *pbuf;
  10821. pbuf++;
  10822. }
  10823. if (pbuf >= (buf + buf_size))
  10824. break;
  10825. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10826. if ((last == ',') && (*pbuf == ',')) {
  10827. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10828. __func__, "Invalid data format.");
  10829. return -EINVAL;
  10830. }
  10831. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10832. && (pbuf < (buf + buf_size))
  10833. && (*pbuf != ' ')
  10834. && (*pbuf != ','); j++) {
  10835. last = *pbuf;
  10836. scan_buf[j] = *pbuf++;
  10837. }
  10838. ret = kstrtoul(scan_buf, 16, &value);
  10839. if (ret < 0) {
  10840. pt_debug(dev, DL_ERROR,
  10841. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10842. "Invalid data format. ", scan_buf,
  10843. "Use \"0xHH,...,0xHH\"", " instead.",
  10844. i, ret);
  10845. return ret;
  10846. }
  10847. ic_buf[i] = value;
  10848. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10849. __func__, i, value);
  10850. i++;
  10851. }
  10852. return i;
  10853. }
  10854. /*******************************************************************************
  10855. * FUNCTION: _pt_ic_parse_input
  10856. *
  10857. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10858. * hex values or dec values into an int array with the following rules:
  10859. * 1) Hex values must have a "0x" prefix for each element or the first element
  10860. * only
  10861. * 2) Dec values do not have any prefix
  10862. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10863. * string
  10864. *
  10865. * RETURN: Number of values parsed
  10866. *
  10867. * PARAMETERS:
  10868. * *dev - pointer to device structure
  10869. * *buf - pointer to buffer that holds the input array to parse
  10870. * buf_size - size of buf
  10871. * *out_buf - pointer to array to store parsed data
  10872. * out_buf_size - max size of buffer to be stored
  10873. ******************************************************************************/
  10874. static int _pt_ic_parse_input(struct device *dev,
  10875. const char *buf, size_t buf_size,
  10876. u32 *out_buf, size_t out_buf_size)
  10877. {
  10878. const char *pbuf = buf;
  10879. unsigned long value;
  10880. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10881. u32 i = 0;
  10882. u32 j;
  10883. int last = 0;
  10884. int ret = 0;
  10885. u8 str_base = 0;
  10886. pt_debug(dev, DL_DEBUG,
  10887. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10888. __func__, buf_size, out_buf_size, "scan buf size",
  10889. PT_MAX_PIP2_MSG_SIZE, buf);
  10890. while (pbuf <= (buf + buf_size)) {
  10891. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10892. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10893. __func__, "Max cmd size exceeded", i,
  10894. PT_MAX_PIP2_MSG_SIZE);
  10895. ret = -EINVAL;
  10896. goto error;
  10897. }
  10898. if (i >= out_buf_size) {
  10899. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10900. __func__, "Buffer size exceeded", i,
  10901. out_buf_size);
  10902. ret = -EINVAL;
  10903. goto error;
  10904. }
  10905. while (((*pbuf == ' ') || (*pbuf == ','))
  10906. && (pbuf < (buf + buf_size))) {
  10907. last = *pbuf;
  10908. pbuf++;
  10909. }
  10910. if (pbuf >= (buf + buf_size))
  10911. break;
  10912. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10913. if ((last == ',') && (*pbuf == ',')) {
  10914. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10915. __func__, "Invalid data format.");
  10916. ret = -EINVAL;
  10917. goto error;
  10918. }
  10919. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10920. && (pbuf < (buf + buf_size))
  10921. && (*pbuf != ' ')
  10922. && (*pbuf != ','); j++) {
  10923. last = *pbuf;
  10924. scan_buf[j] = *pbuf++;
  10925. }
  10926. if (i == 0) {
  10927. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10928. (strncmp(scan_buf, "0X", 2) == 0))
  10929. str_base = 16;
  10930. else
  10931. str_base = 10;
  10932. } else {
  10933. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10934. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10935. (str_base == 10)) {
  10936. pt_debug(dev, DL_ERROR,
  10937. "%s: Decimal and Heximal data mixed\n",
  10938. __func__);
  10939. ret = -EINVAL;
  10940. goto error;
  10941. }
  10942. }
  10943. ret = kstrtoul(scan_buf, str_base, &value);
  10944. if (ret < 0) {
  10945. pt_debug(dev, DL_ERROR,
  10946. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10947. "Invalid data format. ", scan_buf,
  10948. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10949. " instead.", i, ret);
  10950. goto error;
  10951. }
  10952. out_buf[i] = value;
  10953. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10954. __func__, i, value, value);
  10955. i++;
  10956. }
  10957. ret = i;
  10958. error:
  10959. return ret;
  10960. }
  10961. #ifdef TTHE_TUNER_SUPPORT
  10962. /*******************************************************************************
  10963. * FUNCTION: tthe_debugfs_open
  10964. *
  10965. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10966. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10967. * printing a large number of fingers and sensor data without overflowing
  10968. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10969. * even multiple of PAGE_SIZE
  10970. *
  10971. * RETURN:
  10972. * 0 = success
  10973. * !0 = failure
  10974. *
  10975. * PARAMETERS:
  10976. * *inode - file inode number
  10977. * *filp - file pointer to debugfs file
  10978. ******************************************************************************/
  10979. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10980. {
  10981. struct pt_core_data *cd = inode->i_private;
  10982. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10983. filp->private_data = inode->i_private;
  10984. if (cd->tthe_buf)
  10985. return -EBUSY;
  10986. while (buf_size < 4096)
  10987. buf_size = buf_size << 1;
  10988. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10989. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10990. cd->tthe_buf_size = buf_size;
  10991. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10992. if (!cd->tthe_buf)
  10993. return -ENOMEM;
  10994. return 0;
  10995. }
  10996. /*******************************************************************************
  10997. * FUNCTION: tthe_debugfs_close
  10998. *
  10999. * SUMMARY: Close method for tthe_tuner debugfs node.
  11000. *
  11001. * RETURN:
  11002. * 0 = success
  11003. * !0 = failure
  11004. *
  11005. * PARAMETERS:
  11006. * *inode - file inode number
  11007. * *filp - file pointer to debugfs file
  11008. ******************************************************************************/
  11009. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11010. {
  11011. struct pt_core_data *cd = filp->private_data;
  11012. filp->private_data = NULL;
  11013. kfree(cd->tthe_buf);
  11014. cd->tthe_buf = NULL;
  11015. return 0;
  11016. }
  11017. /*******************************************************************************
  11018. * FUNCTION: tthe_debugfs_read
  11019. *
  11020. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11021. * tthe_buf to user buffer.
  11022. *
  11023. * RETURN: Size of debugfs data print
  11024. *
  11025. * PARAMETERS:
  11026. * *filp - file pointer to debugfs file
  11027. * *buf - the user space buffer to read to
  11028. * count - the maximum number of bytes to read
  11029. * *ppos - the current position in the buffer
  11030. ******************************************************************************/
  11031. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11032. size_t count, loff_t *ppos)
  11033. {
  11034. struct pt_core_data *cd = filp->private_data;
  11035. int size;
  11036. int ret;
  11037. static int partial_read;
  11038. wait_event_interruptible(cd->wait_q,
  11039. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11040. mutex_lock(&cd->tthe_lock);
  11041. if (cd->tthe_exit) {
  11042. mutex_unlock(&cd->tthe_lock);
  11043. return 0;
  11044. }
  11045. if (count > cd->tthe_buf_len)
  11046. size = cd->tthe_buf_len;
  11047. else
  11048. size = count;
  11049. if (!size) {
  11050. mutex_unlock(&cd->tthe_lock);
  11051. return 0;
  11052. }
  11053. if (partial_read) {
  11054. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11055. partial_read = 0;
  11056. } else {
  11057. ret = copy_to_user(buf, cd->tthe_buf, size);
  11058. }
  11059. if (size == count)
  11060. partial_read = count;
  11061. if (ret == size)
  11062. return -EFAULT;
  11063. size -= ret;
  11064. cd->tthe_buf_len -= size;
  11065. mutex_unlock(&cd->tthe_lock);
  11066. *ppos += size;
  11067. return size;
  11068. }
  11069. static const struct file_operations tthe_debugfs_fops = {
  11070. .open = tthe_debugfs_open,
  11071. .release = tthe_debugfs_close,
  11072. .read = tthe_debugfs_read,
  11073. };
  11074. #endif
  11075. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11076. .start_bl = _pt_request_pip_start_bl,
  11077. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11078. .resume_scanning = _pt_request_pip_resume_scanning,
  11079. .get_param = _pt_request_pip_get_param,
  11080. .set_param = _pt_request_pip_set_param,
  11081. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11082. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11083. .get_data_structure = _pt_request_pip_get_data_structure,
  11084. .run_selftest = _pt_request_pip_run_selftest,
  11085. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11086. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11087. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11088. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11089. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11090. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11091. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11092. .read_data_block = _pt_request_pip_read_data_block,
  11093. .write_data_block = _pt_request_pip_write_data_block,
  11094. .user_cmd = _pt_request_pip_user_cmd,
  11095. .get_bl_info = _pt_request_pip_bl_get_information,
  11096. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11097. .launch_app = _pt_request_pip_launch_app,
  11098. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11099. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11100. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11101. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11102. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11103. .pip2_file_open = _pt_pip2_file_open,
  11104. .pip2_file_close = _pt_pip2_file_close,
  11105. .pip2_file_erase = _pt_pip2_file_erase,
  11106. .read_us_file = _pt_read_us_file,
  11107. .manage_cal_data = _pt_manage_local_cal_data,
  11108. .calc_crc = crc_ccitt_calculate,
  11109. #ifdef TTDL_DIAGNOSTICS
  11110. .pip2_file_read = _pt_pip2_file_read,
  11111. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11112. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11113. .pip2_file_crc = _pt_pip2_file_crc,
  11114. #endif
  11115. };
  11116. static struct pt_core_commands _pt_core_commands = {
  11117. .subscribe_attention = _pt_subscribe_attention,
  11118. .unsubscribe_attention = _pt_unsubscribe_attention,
  11119. .request_exclusive = _pt_request_exclusive,
  11120. .release_exclusive = _pt_release_exclusive,
  11121. .request_reset = _pt_request_reset,
  11122. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11123. .request_enum = _pt_request_enum,
  11124. .request_sysinfo = _pt_request_sysinfo,
  11125. .request_loader_pdata = _pt_request_loader_pdata,
  11126. .request_stop_wd = _pt_request_stop_wd,
  11127. .request_start_wd = _pt_request_start_wd,
  11128. .request_get_mode = _pt_request_get_mode,
  11129. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11130. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11131. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11132. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11133. .request_dut_generation = _pt_request_dut_generation,
  11134. .request_hw_version = _pt_request_hw_version,
  11135. .parse_sysfs_input = _pt_ic_parse_input,
  11136. #ifdef TTHE_TUNER_SUPPORT
  11137. .request_tthe_print = _pt_request_tthe_print,
  11138. #endif
  11139. #ifdef TTDL_DIAGNOSTICS
  11140. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11141. #endif
  11142. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11143. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11144. };
  11145. struct pt_core_commands *pt_get_commands(void)
  11146. {
  11147. return &_pt_core_commands;
  11148. }
  11149. EXPORT_SYMBOL_GPL(pt_get_commands);
  11150. static DEFINE_MUTEX(core_list_lock);
  11151. static LIST_HEAD(core_list);
  11152. static DEFINE_MUTEX(module_list_lock);
  11153. static LIST_HEAD(module_list);
  11154. static int core_number;
  11155. /*******************************************************************************
  11156. * FUNCTION: pt_probe_module
  11157. *
  11158. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11159. *
  11160. * RETURN:
  11161. * 0 = success
  11162. * !0 = failure
  11163. *
  11164. * PARAMETERS:
  11165. * *cd - pointer to core data
  11166. * *module - pointer to module structure
  11167. ******************************************************************************/
  11168. static int pt_probe_module(struct pt_core_data *cd,
  11169. struct pt_module *module)
  11170. {
  11171. struct module_node *module_node;
  11172. int rc = 0;
  11173. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11174. if (!module_node)
  11175. return -ENOMEM;
  11176. module_node->module = module;
  11177. mutex_lock(&cd->module_list_lock);
  11178. list_add(&module_node->node, &cd->module_list);
  11179. mutex_unlock(&cd->module_list_lock);
  11180. rc = module->probe(cd->dev, &module_node->data);
  11181. if (rc) {
  11182. /*
  11183. * Remove from the list when probe fails
  11184. * in order not to call release
  11185. */
  11186. mutex_lock(&cd->module_list_lock);
  11187. list_del(&module_node->node);
  11188. mutex_unlock(&cd->module_list_lock);
  11189. kfree(module_node);
  11190. goto exit;
  11191. }
  11192. exit:
  11193. return rc;
  11194. }
  11195. /*******************************************************************************
  11196. * FUNCTION: pt_release_module
  11197. *
  11198. * SUMMARY: Call the release pointer and remove the module pointer from
  11199. * module_list.
  11200. *
  11201. * PARAMETERS:
  11202. * *cd - pointer to core data
  11203. * *module - pointer to module structure
  11204. ******************************************************************************/
  11205. static void pt_release_module(struct pt_core_data *cd,
  11206. struct pt_module *module)
  11207. {
  11208. struct module_node *m, *m_n;
  11209. mutex_lock(&cd->module_list_lock);
  11210. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11211. if (m->module == module) {
  11212. module->release(cd->dev, m->data);
  11213. list_del(&m->node);
  11214. kfree(m);
  11215. break;
  11216. }
  11217. mutex_unlock(&cd->module_list_lock);
  11218. }
  11219. /*******************************************************************************
  11220. * FUNCTION: pt_probe_modules
  11221. *
  11222. * SUMMARY: Iterate module_list and probe each module.
  11223. *
  11224. * PARAMETERS:
  11225. * *cd - pointer to core data
  11226. ******************************************************************************/
  11227. static void pt_probe_modules(struct pt_core_data *cd)
  11228. {
  11229. struct pt_module *m;
  11230. int rc = 0;
  11231. mutex_lock(&module_list_lock);
  11232. list_for_each_entry(m, &module_list, node) {
  11233. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11234. __func__, m->name);
  11235. rc = pt_probe_module(cd, m);
  11236. if (rc)
  11237. pt_debug(cd->dev, DL_ERROR,
  11238. "%s: Probe fails for module %s\n",
  11239. __func__, m->name);
  11240. }
  11241. mutex_unlock(&module_list_lock);
  11242. }
  11243. /*******************************************************************************
  11244. * FUNCTION: pt_release_modules
  11245. *
  11246. * SUMMARY: Iterate module_list and remove each module.
  11247. *
  11248. * PARAMETERS:
  11249. * *cd - pointer to core data
  11250. ******************************************************************************/
  11251. static void pt_release_modules(struct pt_core_data *cd)
  11252. {
  11253. struct pt_module *m;
  11254. mutex_lock(&module_list_lock);
  11255. list_for_each_entry(m, &module_list, node)
  11256. pt_release_module(cd, m);
  11257. mutex_unlock(&module_list_lock);
  11258. }
  11259. /*******************************************************************************
  11260. * FUNCTION: pt_get_core_data
  11261. *
  11262. * SUMMARY: Iterate core_list and get core data.
  11263. *
  11264. * RETURN:
  11265. * pointer to core data or null pointer if fail
  11266. *
  11267. * PARAMETERS:
  11268. * *id - pointer to core id
  11269. ******************************************************************************/
  11270. struct pt_core_data *pt_get_core_data(char *id)
  11271. {
  11272. struct pt_core_data *d;
  11273. list_for_each_entry(d, &core_list, node)
  11274. if (!strncmp(d->core_id, id, 20))
  11275. return d;
  11276. return NULL;
  11277. }
  11278. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11279. /*******************************************************************************
  11280. * FUNCTION: pt_add_core
  11281. *
  11282. * SUMMARY: Add core data to the core_list.
  11283. *
  11284. * PARAMETERS:
  11285. * *dev - pointer to device structure
  11286. ******************************************************************************/
  11287. static void pt_add_core(struct device *dev)
  11288. {
  11289. struct pt_core_data *d;
  11290. struct pt_core_data *cd = dev_get_drvdata(dev);
  11291. mutex_lock(&core_list_lock);
  11292. list_for_each_entry(d, &core_list, node)
  11293. if (d->dev == dev)
  11294. goto unlock;
  11295. list_add(&cd->node, &core_list);
  11296. unlock:
  11297. mutex_unlock(&core_list_lock);
  11298. }
  11299. /*******************************************************************************
  11300. * FUNCTION: pt_del_core
  11301. *
  11302. * SUMMARY: Remove core data from the core_list.
  11303. *
  11304. * PARAMETERS:
  11305. * *dev - pointer to device structure
  11306. ******************************************************************************/
  11307. static void pt_del_core(struct device *dev)
  11308. {
  11309. struct pt_core_data *d, *d_n;
  11310. mutex_lock(&core_list_lock);
  11311. list_for_each_entry_safe(d, d_n, &core_list, node)
  11312. if (d->dev == dev) {
  11313. list_del(&d->node);
  11314. goto unlock;
  11315. }
  11316. unlock:
  11317. mutex_unlock(&core_list_lock);
  11318. }
  11319. /*******************************************************************************
  11320. * FUNCTION: pt_register_module
  11321. *
  11322. * SUMMARY: Register the module to module_list and probe the module for each
  11323. * core.
  11324. *
  11325. * RETURN:
  11326. * 0 = success
  11327. * !0 = failure
  11328. *
  11329. * PARAMETERS:
  11330. * *module - pointer to module structure
  11331. ******************************************************************************/
  11332. int pt_register_module(struct pt_module *module)
  11333. {
  11334. struct pt_module *m;
  11335. struct pt_core_data *cd;
  11336. int rc = 0;
  11337. if (!module || !module->probe || !module->release)
  11338. return -EINVAL;
  11339. mutex_lock(&module_list_lock);
  11340. list_for_each_entry(m, &module_list, node)
  11341. if (m == module) {
  11342. rc = -EEXIST;
  11343. goto unlock;
  11344. }
  11345. list_add(&module->node, &module_list);
  11346. /* Probe the module for each core */
  11347. mutex_lock(&core_list_lock);
  11348. list_for_each_entry(cd, &core_list, node)
  11349. pt_probe_module(cd, module);
  11350. mutex_unlock(&core_list_lock);
  11351. unlock:
  11352. mutex_unlock(&module_list_lock);
  11353. return rc;
  11354. }
  11355. EXPORT_SYMBOL_GPL(pt_register_module);
  11356. /*******************************************************************************
  11357. * FUNCTION: pt_unregister_module
  11358. *
  11359. * SUMMARY: Release the module for each core and remove the module from
  11360. * module_list.
  11361. *
  11362. * RETURN:
  11363. * 0 = success
  11364. * !0 = failure
  11365. *
  11366. * PARAMETERS:
  11367. * *module - pointer to module structure
  11368. ******************************************************************************/
  11369. void pt_unregister_module(struct pt_module *module)
  11370. {
  11371. struct pt_module *m, *m_n;
  11372. struct pt_core_data *cd;
  11373. if (!module)
  11374. return;
  11375. mutex_lock(&module_list_lock);
  11376. /* Release the module for each core */
  11377. mutex_lock(&core_list_lock);
  11378. list_for_each_entry(cd, &core_list, node)
  11379. pt_release_module(cd, module);
  11380. mutex_unlock(&core_list_lock);
  11381. list_for_each_entry_safe(m, m_n, &module_list, node)
  11382. if (m == module) {
  11383. list_del(&m->node);
  11384. break;
  11385. }
  11386. mutex_unlock(&module_list_lock);
  11387. }
  11388. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11389. /*******************************************************************************
  11390. * FUNCTION: pt_get_module_data
  11391. *
  11392. * SUMMARY: Get module data from module_node by module_list.
  11393. *
  11394. * RETURN:
  11395. * pointer to module data
  11396. *
  11397. * PARAMETERS:
  11398. * *dev - pointer to device structure
  11399. * *module - pointer to module structure
  11400. ******************************************************************************/
  11401. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11402. {
  11403. struct pt_core_data *cd = dev_get_drvdata(dev);
  11404. struct module_node *m;
  11405. void *data = NULL;
  11406. mutex_lock(&cd->module_list_lock);
  11407. list_for_each_entry(m, &cd->module_list, node)
  11408. if (m->module == module) {
  11409. data = m->data;
  11410. break;
  11411. }
  11412. mutex_unlock(&cd->module_list_lock);
  11413. return data;
  11414. }
  11415. EXPORT_SYMBOL(pt_get_module_data);
  11416. #ifdef CONFIG_HAS_EARLYSUSPEND
  11417. /*******************************************************************************
  11418. * FUNCTION: pt_early_suspend
  11419. *
  11420. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11421. * attention list.
  11422. *
  11423. * PARAMETERS:
  11424. * *h - pointer to early_suspend structure
  11425. ******************************************************************************/
  11426. static void pt_early_suspend(struct early_suspend *h)
  11427. {
  11428. struct pt_core_data *cd =
  11429. container_of(h, struct pt_core_data, es);
  11430. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11431. }
  11432. /*******************************************************************************
  11433. * FUNCTION: pt_late_resume
  11434. *
  11435. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11436. * attention list.
  11437. *
  11438. * PARAMETERS:
  11439. * *h - pointer to early_suspend structure
  11440. ******************************************************************************/
  11441. static void pt_late_resume(struct early_suspend *h)
  11442. {
  11443. struct pt_core_data *cd =
  11444. container_of(h, struct pt_core_data, es);
  11445. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11446. }
  11447. /*******************************************************************************
  11448. * FUNCTION: pt_setup_early_suspend
  11449. *
  11450. * SUMMARY: Register early/suspend function to the system.
  11451. *
  11452. * PARAMETERS:
  11453. * *cd - pointer to core data
  11454. ******************************************************************************/
  11455. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11456. {
  11457. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11458. cd->es.suspend = pt_early_suspend;
  11459. cd->es.resume = pt_late_resume;
  11460. register_early_suspend(&cd->es);
  11461. }
  11462. #elif defined(CONFIG_DRM)
  11463. static void pt_resume_work(struct work_struct *work)
  11464. {
  11465. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11466. resume_work);
  11467. int rc = 0;
  11468. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11469. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11470. return;
  11471. rc = pt_core_easywake_off(pt_data);
  11472. if (rc < 0) {
  11473. pt_debug(pt_data->dev, DL_ERROR,
  11474. "%s: Error on wake\n", __func__);
  11475. }
  11476. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11477. return;
  11478. }
  11479. static void pt_suspend_work(struct work_struct *work)
  11480. {
  11481. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11482. suspend_work);
  11483. int rc = 0;
  11484. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11485. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11486. return;
  11487. rc = pt_core_easywake_on(pt_data);
  11488. if (rc < 0) {
  11489. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11490. return;
  11491. }
  11492. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11493. return;
  11494. }
  11495. /*******************************************************************************
  11496. * FUNCTION: drm_notifier_callback
  11497. *
  11498. * SUMMARY: Call back function for DRM notifier to allow to call
  11499. * resume/suspend attention list.
  11500. *
  11501. * RETURN:
  11502. * 0 = success
  11503. *
  11504. * PARAMETERS:
  11505. * *self - pointer to notifier_block structure
  11506. * event - event type of fb notifier
  11507. * *data - pointer to fb_event structure
  11508. ******************************************************************************/
  11509. static int drm_notifier_callback(struct notifier_block *self,
  11510. unsigned long event, void *data)
  11511. {
  11512. struct pt_core_data *cd =
  11513. container_of(self, struct pt_core_data, fb_notifier);
  11514. struct drm_panel_notifier *evdata = data;
  11515. int *blank;
  11516. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11517. if (!evdata)
  11518. goto exit;
  11519. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11520. event == DRM_PANEL_EVENT_BLANK)) {
  11521. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11522. __func__, event);
  11523. goto exit;
  11524. }
  11525. if (cd->quick_boot || cd->drv_debug_suspend)
  11526. goto exit;
  11527. blank = evdata->data;
  11528. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11529. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11530. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11531. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11532. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11533. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11534. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11535. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11536. __func__, event);
  11537. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11538. if (cd->fb_state != FB_ON) {
  11539. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11540. __func__);
  11541. cancel_work_sync(&cd->resume_offload_work);
  11542. cancel_work_sync(&cd->suspend_offload_work);
  11543. cancel_work_sync(&cd->resume_work);
  11544. cancel_work_sync(&cd->suspend_work);
  11545. queue_work(cd->pt_workqueue, &cd->resume_work);
  11546. #if defined(CONFIG_PM_SLEEP)
  11547. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11548. __func__);
  11549. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11550. pt_core_resume_(cd->dev);
  11551. #endif
  11552. cd->fb_state = FB_ON;
  11553. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11554. }
  11555. }
  11556. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11557. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11558. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11559. if (cd->fb_state != FB_OFF) {
  11560. #if defined(CONFIG_PM_SLEEP)
  11561. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11562. __func__);
  11563. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11564. pt_core_suspend_(cd->dev);
  11565. #endif
  11566. cancel_work_sync(&cd->resume_offload_work);
  11567. cancel_work_sync(&cd->suspend_offload_work);
  11568. cancel_work_sync(&cd->resume_work);
  11569. cancel_work_sync(&cd->suspend_work);
  11570. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11571. cd->fb_state = FB_OFF;
  11572. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11573. }
  11574. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11575. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11576. __func__, event);
  11577. }
  11578. } else {
  11579. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11580. __func__, *blank);
  11581. }
  11582. exit:
  11583. return 0;
  11584. }
  11585. /*******************************************************************************
  11586. * FUNCTION: pt_setup_drm_notifier
  11587. *
  11588. * SUMMARY: Set up call back function into drm notifier.
  11589. *
  11590. * PARAMETERS:
  11591. * *cd - pointer to core data
  11592. ******************************************************************************/
  11593. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11594. {
  11595. cd->fb_state = FB_NONE;
  11596. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11597. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11598. if (!active_panel)
  11599. pt_debug(cd->dev, DL_ERROR,
  11600. "%s: Active panel not registered!\n", __func__);
  11601. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11602. if (!cd->pt_workqueue) {
  11603. pt_debug(cd->dev, DL_ERROR,
  11604. "%s: worker thread creation failed !\n", __func__);
  11605. }
  11606. if (cd->pt_workqueue) {
  11607. INIT_WORK(&cd->resume_work, pt_resume_work);
  11608. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11609. }
  11610. if (active_panel &&
  11611. drm_panel_notifier_register(active_panel,
  11612. &cd->fb_notifier) < 0)
  11613. pt_debug(cd->dev, DL_ERROR,
  11614. "%s: Register notifier failed!\n", __func__);
  11615. }
  11616. #elif defined(CONFIG_FB)
  11617. /*******************************************************************************
  11618. * FUNCTION: fb_notifier_callback
  11619. *
  11620. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11621. * resume/suspend attention list.
  11622. *
  11623. * RETURN:
  11624. * 0 = success
  11625. *
  11626. * PARAMETERS:
  11627. * *self - pointer to notifier_block structure
  11628. * event - event type of fb notifier
  11629. * *data - pointer to fb_event structure
  11630. ******************************************************************************/
  11631. static int fb_notifier_callback(struct notifier_block *self,
  11632. unsigned long event, void *data)
  11633. {
  11634. struct pt_core_data *cd =
  11635. container_of(self, struct pt_core_data, fb_notifier);
  11636. struct fb_event *evdata = data;
  11637. int *blank;
  11638. if (event != FB_EVENT_BLANK || !evdata)
  11639. goto exit;
  11640. blank = evdata->data;
  11641. if (*blank == FB_BLANK_UNBLANK) {
  11642. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11643. if (cd->fb_state != FB_ON) {
  11644. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11645. #if defined(CONFIG_PM_SLEEP)
  11646. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11647. pt_core_resume_(cd->dev);
  11648. #endif
  11649. cd->fb_state = FB_ON;
  11650. }
  11651. } else if (*blank == FB_BLANK_POWERDOWN) {
  11652. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11653. if (cd->fb_state != FB_OFF) {
  11654. #if defined(CONFIG_PM_SLEEP)
  11655. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11656. pt_core_suspend_(cd->dev);
  11657. #endif
  11658. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11659. cd->fb_state = FB_OFF;
  11660. }
  11661. }
  11662. exit:
  11663. return 0;
  11664. }
  11665. /*******************************************************************************
  11666. * FUNCTION: pt_setup_fb_notifier
  11667. *
  11668. * SUMMARY: Set up call back function into fb notifier.
  11669. *
  11670. * PARAMETERS:
  11671. * *cd - pointer to core data
  11672. ******************************************************************************/
  11673. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11674. {
  11675. int rc = 0;
  11676. cd->fb_state = FB_ON;
  11677. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11678. rc = fb_register_client(&cd->fb_notifier);
  11679. if (rc)
  11680. pt_debug(cd->dev, DL_ERROR,
  11681. "Unable to register fb_notifier: %d\n", rc);
  11682. }
  11683. #endif
  11684. /*******************************************************************************
  11685. * FUNCTION: pt_watchdog_work
  11686. *
  11687. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11688. * sleeping then this function simply returns. If the DUT is awake the
  11689. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11690. * somehow a response is waiting on the DUT that has not been read. If
  11691. * this occurs the message is simply consumed. If or once the IRQ is
  11692. * cleared, a PIP PING message is sent to the DUT and if the response
  11693. * is received the watchdog succeeds and exits, if no response is seen
  11694. * a startup is queued unless the maximum number of startups have already
  11695. * been attempted, in that case a BL is attempted.
  11696. *
  11697. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11698. * work thread
  11699. *
  11700. * RETURN: void
  11701. *
  11702. * PARAMETERS:
  11703. * *work - pointer to a work structure for the watchdog work queue
  11704. ******************************************************************************/
  11705. static void pt_watchdog_work(struct work_struct *work)
  11706. {
  11707. int rc = 0;
  11708. struct pt_core_data *cd = container_of(work,
  11709. struct pt_core_data, watchdog_work);
  11710. /*
  11711. * if found the current sleep_state is SS_SLEEPING
  11712. * then no need to request_exclusive, directly return
  11713. */
  11714. if (cd->sleep_state == SS_SLEEPING)
  11715. return;
  11716. #ifdef TTDL_DIAGNOSTICS
  11717. cd->watchdog_count++;
  11718. #endif /* TTDL_DIAGNOSTICS */
  11719. /*
  11720. * The first WD interval was extended to allow DDI to come up.
  11721. * If the WD interval is not the default then adjust timer to the
  11722. * current setting. The user can override value with drv_debug sysfs.
  11723. */
  11724. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11725. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11726. msecs_to_jiffies(cd->watchdog_interval));
  11727. }
  11728. if (pt_check_irq_asserted(cd)) {
  11729. #ifdef TTDL_DIAGNOSTICS
  11730. cd->watchdog_irq_stuck_count++;
  11731. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11732. #endif /* TTDL_DIAGNOSTICS */
  11733. pt_debug(cd->dev, DL_ERROR,
  11734. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11735. __func__);
  11736. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11737. }
  11738. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11739. if (rc < 0) {
  11740. pt_debug(cd->dev, DL_ERROR,
  11741. "%s: fail get exclusive ex=%p own=%p\n",
  11742. __func__, cd->exclusive_dev, cd->dev);
  11743. goto queue_startup;
  11744. }
  11745. rc = pt_pip_null_(cd);
  11746. if (release_exclusive(cd, cd->dev) < 0)
  11747. pt_debug(cd->dev, DL_ERROR,
  11748. "%s: fail to release exclusive\n", __func__);
  11749. queue_startup:
  11750. if (rc) {
  11751. #ifdef TTDL_DIAGNOSTICS
  11752. cd->watchdog_failed_access_count++;
  11753. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11754. #endif /* TTDL_DIAGNOSTICS */
  11755. pt_debug(cd->dev, DL_ERROR,
  11756. "%s: failed to access device in WD, retry count=%d\n",
  11757. __func__, cd->startup_retry_count);
  11758. /* Already tried FW upgrade because of watchdog but failed */
  11759. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11760. return;
  11761. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11762. /*
  11763. * Any wrapper function that trys to disable the
  11764. * WD killing this worker cannot be called here.
  11765. */
  11766. rc = request_exclusive(cd, cd->dev,
  11767. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11768. if (rc < 0) {
  11769. pt_debug(cd->dev, DL_ERROR,
  11770. "%s: fail get exclusive ex=%p own=%p\n",
  11771. __func__, cd->exclusive_dev, cd->dev);
  11772. goto exit;
  11773. }
  11774. cd->hw_detected = false;
  11775. cd->startup_status = STARTUP_STATUS_START;
  11776. pt_debug(cd->dev, DL_DEBUG,
  11777. "%s: Startup Status Reset\n", __func__);
  11778. rc = pt_dut_reset_and_wait(cd);
  11779. if (release_exclusive(cd, cd->dev) < 0)
  11780. pt_debug(cd->dev, DL_ERROR,
  11781. "%s: fail to release exclusive\n",
  11782. __func__);
  11783. if (!rc) {
  11784. cd->hw_detected = true;
  11785. if (!cd->flashless_dut)
  11786. pt_queue_enum(cd);
  11787. }
  11788. #ifdef TTDL_DIAGNOSTICS
  11789. cd->wd_xres_count++;
  11790. pt_debug(cd->dev, DL_ERROR,
  11791. "%s: Comm Failed - DUT reset [#%d]\n",
  11792. __func__, cd->wd_xres_count);
  11793. #endif /* TTDL_DIAGNOSTICS */
  11794. } else {
  11795. /*
  11796. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11797. * reset the part to regain communications, try to BL
  11798. */
  11799. pt_debug(cd->dev, DL_ERROR,
  11800. "%s: WD DUT access failure, Start FW Upgrade\n",
  11801. __func__);
  11802. #ifdef TTDL_DIAGNOSTICS
  11803. /*
  11804. * When diagnostics is enabled allow TTDL to keep
  11805. * trying to find the DUT. This allows the DUT to be
  11806. * hot swap-able while the host stays running. In
  11807. * production this may not be wanted as a customer
  11808. * may have several touch drivers and any driver
  11809. * that doesn't match the current DUT should give
  11810. * up trying and give up using the bus.
  11811. */
  11812. pt_debug(cd->dev, DL_INFO,
  11813. "%s: Resetting startup_retry_count\n",
  11814. __func__);
  11815. cd->startup_retry_count = 0;
  11816. #endif /* TTDL_DIAGNOSTICS */
  11817. /*
  11818. * Since fw may be broken,reset sysinfo ready flag
  11819. * to let upgrade function work.
  11820. */
  11821. mutex_lock(&cd->system_lock);
  11822. cd->sysinfo.ready = false;
  11823. mutex_unlock(&cd->system_lock);
  11824. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11825. pt_debug(cd->dev, DL_ERROR,
  11826. "%s: Queue Restart\n", __func__);
  11827. pt_queue_restart(cd);
  11828. } else
  11829. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11830. }
  11831. } else {
  11832. cd->hw_detected = true;
  11833. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11834. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11835. pt_debug(cd->dev, DL_ERROR,
  11836. "%s: HW detected but not enumerated\n",
  11837. __func__);
  11838. pt_queue_enum(cd);
  11839. }
  11840. }
  11841. exit:
  11842. pt_start_wd_timer(cd);
  11843. }
  11844. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11845. /*******************************************************************************
  11846. * FUNCTION: pt_watchdog_timer
  11847. *
  11848. * SUMMARY: The function that is called when the WD timer expires. If the
  11849. * watchdog work is not already busy schedule the watchdog work queue.
  11850. *
  11851. * RETURN: void
  11852. *
  11853. * PARAMETERS:
  11854. * handle - Handle to the watchdog timer
  11855. ******************************************************************************/
  11856. static void pt_watchdog_timer(unsigned long handle)
  11857. {
  11858. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11859. if (!cd)
  11860. return;
  11861. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11862. __func__);
  11863. if (!work_pending(&cd->watchdog_work))
  11864. schedule_work(&cd->watchdog_work);
  11865. }
  11866. #else
  11867. /*******************************************************************************
  11868. * FUNCTION: pt_watchdog_timer
  11869. *
  11870. * SUMMARY: The function that is called when the WD timer expires. If the
  11871. * watchdog work is not already busy schedule the watchdog work queue.
  11872. *
  11873. * RETURN: void
  11874. *
  11875. * PARAMETERS:
  11876. * *t - Pointer to timer list
  11877. ******************************************************************************/
  11878. static void pt_watchdog_timer(struct timer_list *t)
  11879. {
  11880. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11881. if (!cd)
  11882. return;
  11883. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11884. __func__);
  11885. if (!work_pending(&cd->watchdog_work))
  11886. schedule_work(&cd->watchdog_work);
  11887. }
  11888. #endif
  11889. /*******************************************************************************
  11890. * Core sysfs show and store functions
  11891. ******************************************************************************/
  11892. /*******************************************************************************
  11893. * FUNCTION: pt_hw_version_show
  11894. *
  11895. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11896. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11897. *
  11898. * RETURN: size of data written to sysfs node
  11899. *
  11900. * PARAMETERS:
  11901. * *dev - pointer to device structure
  11902. * *attr - pointer to device attributes structure
  11903. * *buf - pointer to print output buffer
  11904. ******************************************************************************/
  11905. static ssize_t pt_hw_version_show(struct device *dev,
  11906. struct device_attribute *attr, char *buf)
  11907. {
  11908. struct pt_core_data *cd = dev_get_drvdata(dev);
  11909. _pt_request_hw_version(dev, cd->hw_version);
  11910. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11911. }
  11912. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11913. /*******************************************************************************
  11914. * FUNCTION: pt_drv_version_show
  11915. *
  11916. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11917. * TTDL version information
  11918. *
  11919. * RETURN: Char buffer with printed TTDL version information
  11920. *
  11921. * PARAMETERS:
  11922. * *dev - pointer to device structure
  11923. * *attr - pointer to device attributes
  11924. * *buf - pointer to output buffer
  11925. ******************************************************************************/
  11926. static ssize_t pt_drv_version_show(struct device *dev,
  11927. struct device_attribute *attr, char *buf)
  11928. {
  11929. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11930. "Driver: %s\nVersion: %s\nDate: %s\n",
  11931. pt_driver_core_name, pt_driver_core_version,
  11932. pt_driver_core_date);
  11933. }
  11934. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11935. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11936. /*******************************************************************************
  11937. * FUNCTION: pt_fw_version_show
  11938. *
  11939. * SUMMARY: Show method for the fw_version sysfs node that will
  11940. * show the firmware, bootloader and PIP version information
  11941. *
  11942. * RETURN: Size of printed buffer
  11943. *
  11944. * PARAMETERS:
  11945. * *dev - pointer to device structure
  11946. * *attr - pointer to device attributes
  11947. * *buf - pointer to output buffer
  11948. ******************************************************************************/
  11949. static ssize_t pt_fw_version_show(struct device *dev,
  11950. struct device_attribute *attr, char *buf)
  11951. {
  11952. struct pt_core_data *cd = dev_get_drvdata(dev);
  11953. struct pt_ttdata *ttdata;
  11954. int rc = 0;
  11955. if (cd->mode == PT_MODE_OPERATIONAL)
  11956. rc = pt_hid_output_get_sysinfo_(cd);
  11957. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11958. __func__, cd->mode, cd->sysinfo.ready);
  11959. if (cd->sysinfo.ready)
  11960. ttdata = &cd->sysinfo.ttdata;
  11961. else
  11962. rc = -ENODATA;
  11963. if (!rc) {
  11964. return scnprintf(buf, strlen(buf),
  11965. "Status: %d\n"
  11966. "FW : %d.%d.%d\n"
  11967. "Config: %d\n"
  11968. "BL : %d.%d\n"
  11969. "PIP : %d.%d\n",
  11970. rc,
  11971. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11972. ttdata->revctrl,
  11973. ttdata->fw_ver_conf,
  11974. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11975. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11976. } else {
  11977. return scnprintf(buf, strlen(buf),
  11978. "Status: %d\n"
  11979. "FW : n/a\n"
  11980. "Config: n/a\n"
  11981. "BL : n/a\n"
  11982. "PIP : n/a\n",
  11983. rc);
  11984. }
  11985. }
  11986. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11987. /*******************************************************************************
  11988. * FUNCTION: pt_sysinfo_show
  11989. *
  11990. * SUMMARY: Show method for the sysinfo sysfs node that will
  11991. * show all the information from get system information command.
  11992. *
  11993. * RETURN: Size of printed buffer
  11994. *
  11995. * PARAMETERS:
  11996. * *dev - pointer to device structure
  11997. * *attr - pointer to device attributes
  11998. * *buf - pointer to output buffer
  11999. ******************************************************************************/
  12000. static ssize_t pt_sysinfo_show(struct device *dev,
  12001. struct device_attribute *attr, char *buf)
  12002. {
  12003. struct pt_core_data *cd = dev_get_drvdata(dev);
  12004. struct pt_sysinfo *si;
  12005. struct pt_ttdata *ttdata = NULL;
  12006. struct pt_sensing_conf_data *scd = NULL;
  12007. int rc = 0;
  12008. if (cd->mode == PT_MODE_OPERATIONAL) {
  12009. rc = pt_hid_output_get_sysinfo_(cd);
  12010. if (cd->sysinfo.ready) {
  12011. si = &cd->sysinfo;
  12012. ttdata = &si->ttdata;
  12013. scd = &si->sensing_conf_data;
  12014. } else
  12015. rc = -ENODATA;
  12016. } else
  12017. rc = -EPERM;
  12018. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12019. __func__, cd->mode, cd->sysinfo.ready);
  12020. if (!rc && ttdata && scd) {
  12021. return scnprintf(buf, strlen(buf),
  12022. "Status: %d\n"
  12023. "pip_ver_major: 0x%02X\n"
  12024. "pip_ver_minor: 0x%02X\n"
  12025. "fw_pid : 0x%04X\n"
  12026. "fw_ver_major : 0x%02X\n"
  12027. "fw_ver_minor : 0x%02X\n"
  12028. "revctrl : 0x%08X\n"
  12029. "fw_ver_conf : 0x%04X\n"
  12030. "bl_ver_major : 0x%02X\n"
  12031. "bl_ver_minor : 0x%02X\n"
  12032. "jtag_id_h : 0x%04X\n"
  12033. "jtag_id_l : 0x%04X\n"
  12034. "mfg_id[0] : 0x%02X\n"
  12035. "mfg_id[1] : 0x%02X\n"
  12036. "mfg_id[2] : 0x%02X\n"
  12037. "mfg_id[3] : 0x%02X\n"
  12038. "mfg_id[4] : 0x%02X\n"
  12039. "mfg_id[5] : 0x%02X\n"
  12040. "mfg_id[6] : 0x%02X\n"
  12041. "mfg_id[7] : 0x%02X\n"
  12042. "post_code : 0x%04X\n"
  12043. "electrodes_x : 0x%02X\n"
  12044. "electrodes_y : 0x%02X\n"
  12045. "len_x : 0x%04X\n"
  12046. "len_y : 0x%04X\n"
  12047. "res_x : 0x%04X\n"
  12048. "res_y : 0x%04X\n"
  12049. "max_z : 0x%04X\n"
  12050. "origin_x : 0x%02X\n"
  12051. "origin_y : 0x%02X\n"
  12052. "panel_id : 0x%02X\n"
  12053. "btn : 0x%02X\n"
  12054. "scan_mode : 0x%02X\n"
  12055. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12056. rc,
  12057. ttdata->pip_ver_major,
  12058. ttdata->pip_ver_minor,
  12059. ttdata->fw_pid,
  12060. ttdata->fw_ver_major,
  12061. ttdata->fw_ver_minor,
  12062. ttdata->revctrl,
  12063. ttdata->fw_ver_conf,
  12064. ttdata->bl_ver_major,
  12065. ttdata->bl_ver_minor,
  12066. ttdata->jtag_id_h,
  12067. ttdata->jtag_id_l,
  12068. ttdata->mfg_id[0],
  12069. ttdata->mfg_id[1],
  12070. ttdata->mfg_id[2],
  12071. ttdata->mfg_id[3],
  12072. ttdata->mfg_id[4],
  12073. ttdata->mfg_id[5],
  12074. ttdata->mfg_id[6],
  12075. ttdata->mfg_id[7],
  12076. ttdata->post_code,
  12077. scd->electrodes_x,
  12078. scd->electrodes_y,
  12079. scd->len_x,
  12080. scd->len_y,
  12081. scd->res_x,
  12082. scd->res_y,
  12083. scd->max_z,
  12084. scd->origin_x,
  12085. scd->origin_y,
  12086. scd->panel_id,
  12087. scd->btn,
  12088. scd->scan_mode,
  12089. scd->max_tch);
  12090. } else {
  12091. return scnprintf(buf, strlen(buf),
  12092. "Status: %d\n",
  12093. rc);
  12094. }
  12095. }
  12096. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12097. /*******************************************************************************
  12098. * FUNCTION: pt_hw_reset_show
  12099. *
  12100. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12101. * by toggling the XRES line and then calls the startup function to
  12102. * allow TTDL to re-enumerate the DUT.
  12103. * The printed value reflects the status of the full reset/enum.
  12104. *
  12105. * PARAMETERS:
  12106. * *dev - pointer to Device structure
  12107. * *attr - pointer to the device attribute structure
  12108. * *buf - pointer to buffer to print
  12109. ******************************************************************************/
  12110. static ssize_t pt_hw_reset_show(struct device *dev,
  12111. struct device_attribute *attr, char *buf)
  12112. {
  12113. struct pt_core_data *cd = dev_get_drvdata(dev);
  12114. int rc = 0;
  12115. int time = 0;
  12116. u8 reset_status = 0;
  12117. int t;
  12118. struct pt_hid_desc hid_desc;
  12119. memset(&hid_desc, 0, sizeof(hid_desc));
  12120. /* Only allow DUT reset if no active BL in progress */
  12121. mutex_lock(&cd->firmware_class_lock);
  12122. mutex_lock(&cd->system_lock);
  12123. cd->startup_state = STARTUP_NONE;
  12124. mutex_unlock(&(cd->system_lock));
  12125. pt_stop_wd_timer(cd);
  12126. /* ensure no left over exclusive access is still locked */
  12127. release_exclusive(cd, cd->dev);
  12128. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12129. if (rc) {
  12130. mutex_unlock(&cd->firmware_class_lock);
  12131. pt_debug(cd->dev, DL_ERROR,
  12132. "%s: HW reset failed rc = %d\n", __func__, rc);
  12133. goto exit_hw_reset;
  12134. }
  12135. reset_status |= 0x01 << 0;
  12136. if (cd->flashless_dut) {
  12137. mutex_unlock(&cd->firmware_class_lock);
  12138. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12139. msecs_to_jiffies(200));
  12140. if (IS_TMO(t)) {
  12141. pt_debug(dev, DL_ERROR,
  12142. "%s: Timeout waiting for FW update",
  12143. __func__);
  12144. rc = -ETIME;
  12145. goto exit_hw_reset;
  12146. } else {
  12147. pt_debug(dev, DL_INFO,
  12148. "%s: ----- Wait FW Loading ----",
  12149. __func__);
  12150. rc = _pt_request_wait_for_enum_state(
  12151. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12152. if (rc) {
  12153. pt_debug(cd->dev, DL_ERROR,
  12154. "%s: No FW Sentinel detected rc = %d\n",
  12155. __func__, rc);
  12156. goto exit_hw_reset;
  12157. }
  12158. reset_status |= 0x01 << 1;
  12159. }
  12160. } else {
  12161. /* Wait for any sentinel */
  12162. rc = _pt_request_wait_for_enum_state(dev, 150,
  12163. STARTUP_STATUS_BL_RESET_SENTINEL |
  12164. STARTUP_STATUS_FW_RESET_SENTINEL);
  12165. if (rc) {
  12166. mutex_unlock(&cd->firmware_class_lock);
  12167. pt_debug(cd->dev, DL_ERROR,
  12168. "%s: No Sentinel detected rc = %d\n",
  12169. __func__, rc);
  12170. goto exit_hw_reset;
  12171. }
  12172. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12173. msleep(30);
  12174. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12175. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12176. if (rc < 0) {
  12177. pt_debug(cd->dev, DL_ERROR,
  12178. "%s: Error on getting HID descriptor r=%d\n",
  12179. __func__, rc);
  12180. goto exit_hw_reset;
  12181. }
  12182. cd->mode = pt_get_mode(cd, &hid_desc);
  12183. if (cd->mode == PT_MODE_BOOTLOADER)
  12184. rc = pt_hid_output_bl_launch_app_(cd);
  12185. } else {
  12186. if (cd->mode == PT_MODE_BOOTLOADER)
  12187. rc = pt_pip2_launch_app(dev,
  12188. PT_CORE_CMD_UNPROTECTED);
  12189. }
  12190. if (rc) {
  12191. mutex_unlock(&cd->firmware_class_lock);
  12192. pt_debug(cd->dev, DL_ERROR,
  12193. "%s: PIP launch app failed rc = %d\n",
  12194. __func__, rc);
  12195. goto exit_hw_reset;
  12196. }
  12197. mutex_unlock(&cd->firmware_class_lock);
  12198. reset_status |= 0x01 << 1;
  12199. msleep(20);
  12200. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12201. (cd->mode != PT_MODE_OPERATIONAL))
  12202. pt_queue_restart(cd);
  12203. else
  12204. pt_queue_enum(cd);
  12205. }
  12206. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12207. msleep(50);
  12208. pt_debug(cd->dev, DL_INFO,
  12209. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12210. __func__, time, STARTUP_STATUS_COMPLETE,
  12211. cd->startup_status);
  12212. time += 50;
  12213. }
  12214. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12215. rc = -ETIME;
  12216. goto exit_hw_reset;
  12217. }
  12218. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12219. __func__, cd->startup_status);
  12220. reset_status |= 0x01 << 2;
  12221. pt_start_wd_timer(cd);
  12222. exit_hw_reset:
  12223. return scnprintf(buf, strlen(buf),
  12224. "Status: %d\n"
  12225. "Reset Status: 0x%02X\n", rc, reset_status);
  12226. }
  12227. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12228. /*******************************************************************************
  12229. * FUNCTION: pt_pip2_cmd_rsp_store
  12230. *
  12231. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12232. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12233. * Command byte order:
  12234. * Byte [0] - PIP2 command ID
  12235. * Byte [1-n] - PIP2 command payload
  12236. *
  12237. * RETURN: Size of passed in buffer
  12238. *
  12239. * PARAMETERS:
  12240. * *dev - pointer to device structure
  12241. * *attr - pointer to device attributes
  12242. * *buf - pointer to buffer that hold the command parameters
  12243. * size - size of buf
  12244. ******************************************************************************/
  12245. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12246. struct device_attribute *attr, const char *buf, size_t size)
  12247. {
  12248. struct pt_core_data *cd = dev_get_drvdata(dev);
  12249. u16 actual_read_len;
  12250. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12251. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12252. u8 pip2_cmd_id = 0x00;
  12253. u8 *pip2_cmd_data = NULL;
  12254. int data_len = 0;
  12255. int length;
  12256. int rc = 0;
  12257. /* clear shared data */
  12258. mutex_lock(&cd->sysfs_lock);
  12259. cd->raw_cmd_status = 0;
  12260. cd->cmd_rsp_buf_len = 0;
  12261. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12262. mutex_unlock(&cd->sysfs_lock);
  12263. length = _pt_ic_parse_input_hex(dev, buf, size,
  12264. input_data, PT_MAX_PIP2_MSG_SIZE);
  12265. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12266. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12267. __func__);
  12268. rc = -EINVAL;
  12269. goto exit;
  12270. }
  12271. /* Send PIP2 command if enough data was provided */
  12272. if (length >= 1) {
  12273. pip2_cmd_id = input_data[0];
  12274. pip2_cmd_data = &input_data[1];
  12275. data_len = length - 1;
  12276. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12277. pip2_cmd_id, pip2_cmd_data, data_len,
  12278. read_buf, &actual_read_len);
  12279. cd->raw_cmd_status = rc;
  12280. if (rc) {
  12281. pt_debug(dev, DL_ERROR,
  12282. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12283. __func__, pip2_cmd_id, rc);
  12284. goto exit;
  12285. } else {
  12286. cd->cmd_rsp_buf_len = actual_read_len;
  12287. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12288. pt_debug(dev, DL_ERROR,
  12289. "%s: PIP2 actual_read_len = %d\n",
  12290. __func__, actual_read_len);
  12291. }
  12292. } else {
  12293. rc = -EINVAL;
  12294. pt_debug(dev, DL_ERROR,
  12295. "%s: Insufficient data provided for PIP2 cmd\n",
  12296. __func__);
  12297. }
  12298. exit:
  12299. if (rc)
  12300. return rc;
  12301. return size;
  12302. }
  12303. /*******************************************************************************
  12304. * FUNCTION: pt_pip2_cmd_rsp_show
  12305. *
  12306. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12307. * response generated after using the store method of the pip2_cmd_rsp
  12308. * sysfs node, are available to be read here.
  12309. *
  12310. * PARAMETERS:
  12311. * *dev - pointer to Device structure
  12312. * *attr - pointer to the device attribute structure
  12313. * *buf - pointer to buffer to print
  12314. ******************************************************************************/
  12315. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12316. struct device_attribute *attr, char *buf)
  12317. {
  12318. struct pt_core_data *cd = dev_get_drvdata(dev);
  12319. int i;
  12320. ssize_t data_len;
  12321. int index;
  12322. mutex_lock(&cd->sysfs_lock);
  12323. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12324. "Status: %d\n", cd->raw_cmd_status);
  12325. if (cd->raw_cmd_status)
  12326. goto error;
  12327. /* Remove the CRC from the length of the response */
  12328. data_len = cd->cmd_rsp_buf_len - 2;
  12329. /* Start printing from the data payload */
  12330. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12331. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12332. "%02X ", cd->cmd_rsp_buf[i]);
  12333. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12334. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12335. "\n(%zd bytes)\n",
  12336. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12337. } else {
  12338. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12339. "\n(%zd bytes)\n", 0);
  12340. }
  12341. error:
  12342. mutex_unlock(&cd->sysfs_lock);
  12343. return index;
  12344. }
  12345. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12346. pt_pip2_cmd_rsp_store);
  12347. /*******************************************************************************
  12348. * FUNCTION: pt_command_store
  12349. *
  12350. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12351. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12352. * TTDL will not parse the command.
  12353. *
  12354. * RETURN: Size of passed in buffer
  12355. *
  12356. * PARAMETERS:
  12357. * *dev - pointer to device structure
  12358. * *attr - pointer to device attributes
  12359. * *buf - pointer to buffer that hold the command parameters
  12360. * size - size of buf
  12361. ******************************************************************************/
  12362. static ssize_t pt_command_store(struct device *dev,
  12363. struct device_attribute *attr, const char *buf, size_t size)
  12364. {
  12365. struct pt_core_data *cd = dev_get_drvdata(dev);
  12366. unsigned short crc;
  12367. u16 actual_read_len;
  12368. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12369. int length;
  12370. int len_field;
  12371. int rc = 0;
  12372. mutex_lock(&cd->sysfs_lock);
  12373. cd->cmd_rsp_buf_len = 0;
  12374. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12375. mutex_unlock(&cd->sysfs_lock);
  12376. length = _pt_ic_parse_input_hex(dev, buf, size,
  12377. input_data, PT_MAX_PIP2_MSG_SIZE);
  12378. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12379. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12380. __func__);
  12381. rc = -EINVAL;
  12382. goto pt_command_store_exit;
  12383. }
  12384. /* PIP2 messages begin with 01 01 */
  12385. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12386. cd->pip2_prot_active = 1;
  12387. /* Override next seq tag with what was sent */
  12388. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12389. /* For PIP2 cmd if length does not include crc, add it */
  12390. len_field = (input_data[3] << 8) | input_data[2];
  12391. if (len_field == length && length <= 254) {
  12392. crc = crc_ccitt_calculate(&input_data[2],
  12393. length - 2);
  12394. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12395. __func__, length, crc);
  12396. input_data[length] = (crc & 0xFF00) >> 8;
  12397. input_data[length + 1] = crc & 0x00FF;
  12398. length = length + 2;
  12399. }
  12400. }
  12401. /* write PIP command to log */
  12402. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12403. pm_runtime_get_sync(dev);
  12404. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12405. length, input_data, &actual_read_len);
  12406. pm_runtime_put(dev);
  12407. mutex_lock(&cd->sysfs_lock);
  12408. cd->raw_cmd_status = rc;
  12409. if (rc) {
  12410. cd->cmd_rsp_buf_len = 0;
  12411. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12412. __func__, buf);
  12413. } else {
  12414. cd->cmd_rsp_buf_len = actual_read_len;
  12415. }
  12416. cd->pip2_prot_active = 0;
  12417. mutex_unlock(&cd->sysfs_lock);
  12418. pt_command_store_exit:
  12419. if (rc)
  12420. return rc;
  12421. return size;
  12422. }
  12423. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12424. /*******************************************************************************
  12425. * FUNCTION: pt_response_show
  12426. *
  12427. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12428. * response generated after using the pt_command_store sysfs node, are
  12429. * available to be read here.
  12430. *
  12431. * PARAMETERS:
  12432. * *dev - pointer to Device structure
  12433. * *attr - pointer to the device attribute structure
  12434. * *buf - pointer to buffer to print
  12435. ******************************************************************************/
  12436. static ssize_t pt_response_show(struct device *dev,
  12437. struct device_attribute *attr, char *buf)
  12438. {
  12439. struct pt_core_data *cd = dev_get_drvdata(dev);
  12440. int i;
  12441. ssize_t num_read;
  12442. int index;
  12443. mutex_lock(&cd->sysfs_lock);
  12444. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12445. "Status: %d\n", cd->raw_cmd_status);
  12446. if (cd->raw_cmd_status)
  12447. goto error;
  12448. num_read = cd->cmd_rsp_buf_len;
  12449. for (i = 0; i < num_read; i++)
  12450. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12451. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12452. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12453. "(%zd bytes)\n", num_read);
  12454. error:
  12455. mutex_unlock(&cd->sysfs_lock);
  12456. return index;
  12457. }
  12458. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12459. /*******************************************************************************
  12460. * FUNCTION: pt_dut_debug_show
  12461. *
  12462. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12463. * are available for the store method.
  12464. *
  12465. * RETURN:
  12466. * 0 = success
  12467. * !0 = failure
  12468. *
  12469. * PARAMETERS:
  12470. * *dev - pointer to device structure
  12471. * *attr - pointer to device attributes
  12472. * *buf - pointer to output buffer
  12473. ******************************************************************************/
  12474. static ssize_t pt_dut_debug_show(struct device *dev,
  12475. struct device_attribute *attr, char *buf)
  12476. {
  12477. ssize_t ret;
  12478. ret = scnprintf(buf, strlen(buf),
  12479. "Status: 0\n"
  12480. "dut_debug sends the following commands to the DUT:\n"
  12481. "%d %s \t- %s\n"
  12482. "%d %s \t- %s\n"
  12483. "%d %s \t- %s\n"
  12484. "%d %s \t- %s\n"
  12485. "%d %s \t- %s\n"
  12486. "%d %s \t- %s\n"
  12487. "%d %s \t- %s\n"
  12488. "%d %s \t- %s\n"
  12489. "%d %s \t- %s\n"
  12490. "%d %s \t- %s\n"
  12491. "%d %s \t- %s\n"
  12492. "%d %s \t- %s\n"
  12493. "%d %s \t- %s\n"
  12494. "%d %s \t- %s\n"
  12495. "%d %s \t- %s\n"
  12496. "%d %s \t- %s\n"
  12497. "%d %s \t- %s\n"
  12498. ,
  12499. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12500. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12501. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12502. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12503. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12504. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12505. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12506. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12507. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12508. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12509. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12510. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12511. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12512. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12513. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12514. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12515. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12516. );
  12517. return ret;
  12518. }
  12519. /*******************************************************************************
  12520. * FUNCTION: pt_drv_debug_show
  12521. *
  12522. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12523. * are available for the store method.
  12524. *
  12525. * RETURN:
  12526. * 0 = success
  12527. * !0 = failure
  12528. *
  12529. * PARAMETERS:
  12530. * *dev - pointer to device structure
  12531. * *attr - pointer to device attributes
  12532. * *buf - pointer to output buffer
  12533. ******************************************************************************/
  12534. static ssize_t pt_drv_debug_show(struct device *dev,
  12535. struct device_attribute *attr, char *buf)
  12536. {
  12537. ssize_t ret;
  12538. ret = scnprintf(buf, strlen(buf),
  12539. "Status: 0\n"
  12540. "drv_debug supports the following values:\n"
  12541. "%d %s \t- %s\n"
  12542. "%d %s \t- %s\n"
  12543. "%d %s \t- %s\n"
  12544. "%d %s \t- %s\n"
  12545. "%d %s \t- %s\n"
  12546. "%d %s \t- %s\n"
  12547. "%d %s \t- %s\n"
  12548. "%d %s \t- %s\n"
  12549. "%d %s \t- %s\n"
  12550. "%d %s - %s\n"
  12551. "%d %s \t- %s\n"
  12552. "%d %s \t- %s\n"
  12553. #ifdef TTDL_DIAGNOSTICS
  12554. "%d %s \t- %s\n"
  12555. "%d %s \t- %s\n"
  12556. "%d %s \t- %s\n"
  12557. #endif /* TTDL_DIAGNOSTICS */
  12558. "%d %s \t- %s\n"
  12559. #ifdef TTDL_DIAGNOSTICS
  12560. "%d %s \t- %s\n"
  12561. "%d %s \t- %s\n"
  12562. "%d %s \t- %s\n"
  12563. "%d %s \t- %s\n"
  12564. "%d %s \t- %s\n"
  12565. "%d %s \t- %s\n"
  12566. "%d %s \t- %s\n"
  12567. "%d %s \t- %s\n"
  12568. "%d %s \t- %s\n"
  12569. #endif /* TTDL_DIAGNOSTICS */
  12570. ,
  12571. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12572. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12573. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12574. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12575. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12576. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12577. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12578. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12579. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12580. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12581. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12582. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12583. #ifdef TTDL_DIAGNOSTICS
  12584. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12585. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12586. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12587. #endif /* TTDL_DIAGNOSTICS */
  12588. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12589. #ifdef TTDL_DIAGNOSTICS
  12590. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12591. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12592. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12593. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12594. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12595. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12596. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12597. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12598. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12599. "TTHE_TUNER HID USB Format"
  12600. #endif /* TTDL_DIAGNOSTICS */
  12601. );
  12602. return ret;
  12603. }
  12604. /*******************************************************************************
  12605. * FUNCTION: pt_drv_debug_store
  12606. *
  12607. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12608. * dut_debug. Drv_debug will contain all functionality that can be run
  12609. * without a DUT preset and is available anytime TTDL is running.
  12610. * Dut_debug requires a DUT to be available and will only be created after
  12611. * a DUT has been detected.
  12612. * This function will eventually be split into two but until the overlap
  12613. * has been depricated this function contains all commands that can be
  12614. * used for TTDL/DUT debugging status and control.
  12615. * All commands require at least one value to be passed in *buf with some
  12616. * requiring two.
  12617. *
  12618. * RETURN: Size of passed in buffer
  12619. *
  12620. * PARAMETERS:
  12621. * *dev - pointer to device structure
  12622. * *attr - pointer to device attributes
  12623. * *buf - pointer to buffer that hold the command parameters
  12624. * size - size of buf
  12625. ******************************************************************************/
  12626. static ssize_t pt_drv_debug_store(struct device *dev,
  12627. struct device_attribute *attr, const char *buf, size_t size)
  12628. {
  12629. struct pt_core_data *cd = dev_get_drvdata(dev);
  12630. unsigned long value;
  12631. int rc = 0;
  12632. u8 return_data[8];
  12633. static u8 wd_disabled;
  12634. u32 input_data[3];
  12635. int length;
  12636. #ifdef TTDL_DIAGNOSTICS
  12637. struct i2c_client *client = to_i2c_client(dev);
  12638. unsigned short crc = 0;
  12639. u16 cal_size;
  12640. #endif
  12641. input_data[0] = 0;
  12642. input_data[1] = 0;
  12643. /* Maximmum input is two elements */
  12644. length = _pt_ic_parse_input(dev, buf, size,
  12645. input_data, ARRAY_SIZE(input_data));
  12646. if (length < 1 || length > 2) {
  12647. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12648. __func__);
  12649. rc = -EINVAL;
  12650. goto pt_drv_debug_store_exit;
  12651. }
  12652. value = input_data[0];
  12653. if (length == 1) {
  12654. pt_debug(dev, DL_DEBUG,
  12655. "%s: Debug Cmd Received (id=%d)\n",
  12656. __func__, input_data[0]);
  12657. } else if (length == 2) {
  12658. pt_debug(dev, DL_DEBUG,
  12659. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12660. __func__, input_data[0], input_data[1]);
  12661. } else {
  12662. pt_debug(dev, DL_DEBUG,
  12663. "%s: Invalid arguments received\n", __func__);
  12664. rc = -EINVAL;
  12665. goto pt_drv_debug_store_exit;
  12666. }
  12667. /* Start watchdog timer command */
  12668. if (value == PT_DRV_DBG_START_WD) {
  12669. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12670. wd_disabled = 0;
  12671. cd->watchdog_force_stop = false;
  12672. pt_start_wd_timer(cd);
  12673. goto pt_drv_debug_store_exit;
  12674. }
  12675. /* Stop watchdog timer temporarily */
  12676. pt_stop_wd_timer(cd);
  12677. if (value == PT_DRV_DBG_STOP_WD) {
  12678. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12679. wd_disabled = 1;
  12680. cd->watchdog_force_stop = true;
  12681. goto pt_drv_debug_store_exit;
  12682. }
  12683. switch (value) {
  12684. case PT_DRV_DBG_SUSPEND: /* 4 */
  12685. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12686. wd_disabled = 1;
  12687. rc = pt_core_suspend_(cd->dev);
  12688. if (rc)
  12689. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12690. __func__, rc);
  12691. else {
  12692. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12693. __func__);
  12694. cd->drv_debug_suspend = true;
  12695. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12696. }
  12697. break;
  12698. case PT_DRV_DBG_RESUME: /* 5 */
  12699. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12700. rc = pt_core_resume_(cd->dev);
  12701. if (rc)
  12702. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12703. __func__, rc);
  12704. else {
  12705. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12706. __func__);
  12707. cd->drv_debug_suspend = false;
  12708. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12709. }
  12710. break;
  12711. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12712. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12713. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12714. break;
  12715. case PT_DUT_DBG_HID_RESET: /* 50 */
  12716. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12717. pt_hid_cmd_reset(cd);
  12718. break;
  12719. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12720. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12721. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12722. wd_disabled = 0;
  12723. break;
  12724. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12725. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12726. __func__);
  12727. wd_disabled = 1;
  12728. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12729. break;
  12730. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12731. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12732. __func__);
  12733. wd_disabled = 1;
  12734. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12735. break;
  12736. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12737. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12738. pt_hid_output_bl_get_information(cd, return_data);
  12739. break;
  12740. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12741. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12742. __func__);
  12743. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12744. break;
  12745. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12746. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12747. pt_hid_output_bl_launch_app(cd);
  12748. break;
  12749. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12750. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12751. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12752. break;
  12753. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12754. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12755. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12756. break;
  12757. case PT_DUT_DBG_RESET: /* 98 */
  12758. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12759. rc = pt_hw_hard_reset(cd);
  12760. break;
  12761. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12762. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12763. pt_pip_null(cd);
  12764. break;
  12765. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12766. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12767. rc = pt_pip_start_bootloader(cd);
  12768. if (!rc) {
  12769. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12770. cd->mode = PT_MODE_BOOTLOADER;
  12771. }
  12772. break;
  12773. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12774. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12775. pt_hid_output_get_sysinfo(cd);
  12776. break;
  12777. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12778. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12779. pt_pip_suspend_scanning(cd);
  12780. break;
  12781. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12782. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12783. pt_pip_resume_scanning(cd);
  12784. break;
  12785. #ifdef TTHE_TUNER_SUPPORT
  12786. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12787. cd->tthe_exit = 1;
  12788. wake_up(&cd->wait_q);
  12789. kfree(cd->tthe_buf);
  12790. cd->tthe_buf = NULL;
  12791. cd->tthe_exit = 0;
  12792. break;
  12793. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12794. if (cd->tthe_buf)
  12795. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12796. else
  12797. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12798. __func__);
  12799. break;
  12800. #endif
  12801. #ifdef TTDL_DIAGNOSTICS
  12802. case PT_DUT_DBG_HID_DESC: /* 109 */
  12803. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12804. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12805. break;
  12806. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12807. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12808. __func__);
  12809. pt_erase_parameter_list(cd);
  12810. break;
  12811. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12812. rc = pt_read_input(cd);
  12813. if (!rc)
  12814. pt_parse_input(cd);
  12815. break;
  12816. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12817. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12818. &cal_size, &crc);
  12819. if (rc)
  12820. pt_debug(dev, DL_ERROR,
  12821. "%s: CAL Data clear failed rc=%d\n",
  12822. __func__, rc);
  12823. else
  12824. pt_debug(dev, DL_INFO,
  12825. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12826. __func__, crc, size);
  12827. break;
  12828. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12829. mutex_lock(&cd->system_lock);
  12830. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12831. cd->debug_level = input_data[1];
  12832. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12833. __func__, cd->debug_level);
  12834. } else {
  12835. rc = -EINVAL;
  12836. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12837. __func__, input_data[1]);
  12838. }
  12839. mutex_unlock(&(cd->system_lock));
  12840. break;
  12841. #endif
  12842. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12843. mutex_lock(&cd->system_lock);
  12844. if (input_data[1] > 100) {
  12845. cd->watchdog_interval = input_data[1];
  12846. pt_debug(dev, DL_INFO,
  12847. "%s: Set watchdog_ interval to: %d\n",
  12848. __func__, cd->watchdog_interval);
  12849. pt_start_wd_timer(cd);
  12850. } else {
  12851. rc = -EINVAL;
  12852. pt_debug(dev, DL_ERROR,
  12853. "%s: Invalid watchdog interval: %d\n",
  12854. __func__, input_data[1]);
  12855. }
  12856. mutex_unlock(&(cd->system_lock));
  12857. break;
  12858. #ifdef TTDL_DIAGNOSTICS
  12859. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12860. mutex_lock(&cd->system_lock);
  12861. if (input_data[1] == 0) {
  12862. cd->show_timestamp = 0;
  12863. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12864. __func__);
  12865. } else if (input_data[1] == 1) {
  12866. cd->show_timestamp = 1;
  12867. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12868. __func__);
  12869. } else {
  12870. rc = -EINVAL;
  12871. pt_debug(dev, DL_ERROR,
  12872. "%s: Invalid parameter: %d\n",
  12873. __func__, input_data[1]);
  12874. }
  12875. mutex_unlock(&(cd->system_lock));
  12876. break;
  12877. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12878. if (input_data[1] == 0) {
  12879. cd->cpdata->setup_power(cd->cpdata,
  12880. PT_MT_POWER_OFF, cd->dev);
  12881. pt_debug(dev, DL_INFO,
  12882. "%s: Initiate Power Off\n", __func__);
  12883. } else if (input_data[1] == 1) {
  12884. cd->cpdata->setup_power(cd->cpdata,
  12885. PT_MT_POWER_ON, cd->dev);
  12886. pt_debug(dev, DL_INFO,
  12887. "%s: Initiate Power On\n", __func__);
  12888. } else {
  12889. rc = -EINVAL;
  12890. pt_debug(dev, DL_ERROR,
  12891. "%s: Invalid parameter: %d\n",
  12892. __func__, input_data[1]);
  12893. }
  12894. break;
  12895. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12896. if (input_data[1] == 0) {
  12897. pm_runtime_put(dev);
  12898. pt_debug(dev, DL_ERROR,
  12899. "%s: Force call pm_runtime_put()\n", __func__);
  12900. } else if (input_data[1] == 1) {
  12901. pm_runtime_get_sync(dev);
  12902. pt_debug(dev, DL_ERROR,
  12903. "%s: Force call pm_runtime_get_sync()\n",
  12904. __func__);
  12905. } else {
  12906. rc = -EINVAL;
  12907. pt_debug(dev, DL_ERROR,
  12908. "%s: WARNING: Invalid parameter: %d\n",
  12909. __func__, input_data[1]);
  12910. }
  12911. break;
  12912. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12913. mutex_lock(&cd->system_lock);
  12914. if (input_data[1] == 0) {
  12915. cd->show_tt_data = false;
  12916. pt_debug(dev, DL_INFO,
  12917. "%s: Disable TT_DATA\n", __func__);
  12918. } else if (input_data[1] == 1) {
  12919. cd->show_tt_data = true;
  12920. pt_debug(dev, DL_INFO,
  12921. "%s: Enable TT_DATA\n", __func__);
  12922. } else {
  12923. rc = -EINVAL;
  12924. pt_debug(dev, DL_ERROR,
  12925. "%s: Invalid parameter: %d\n",
  12926. __func__, input_data[1]);
  12927. }
  12928. mutex_unlock(&(cd->system_lock));
  12929. break;
  12930. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12931. if (input_data[1] == cd->active_dut_generation) {
  12932. mutex_lock(&cd->system_lock);
  12933. cd->set_dut_generation = true;
  12934. mutex_unlock(&(cd->system_lock));
  12935. } else {
  12936. mutex_lock(&cd->system_lock);
  12937. if (input_data[1] == 0) {
  12938. cd->active_dut_generation = DUT_UNKNOWN;
  12939. cd->set_dut_generation = false;
  12940. } else if (input_data[1] == 1) {
  12941. cd->active_dut_generation = DUT_PIP1_ONLY;
  12942. cd->set_dut_generation = true;
  12943. } else if (input_data[1] == 2) {
  12944. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12945. cd->set_dut_generation = true;
  12946. } else {
  12947. rc = -EINVAL;
  12948. pt_debug(dev, DL_ERROR,
  12949. "%s: Invalid parameter: %d\n",
  12950. __func__, input_data[1]);
  12951. break;
  12952. }
  12953. cd->startup_status = STARTUP_STATUS_START;
  12954. pt_debug(cd->dev, DL_DEBUG,
  12955. "%s: Startup Status Reset\n", __func__);
  12956. mutex_unlock(&(cd->system_lock));
  12957. pt_debug(dev, DL_INFO,
  12958. "%s: Active DUT Generation Set to: %d\n",
  12959. __func__, cd->active_dut_generation);
  12960. /* Changing DUT generations full restart needed */
  12961. pt_queue_restart(cd);
  12962. }
  12963. break;
  12964. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12965. mutex_lock(&cd->system_lock);
  12966. if (input_data[1] == 0) {
  12967. cd->bridge_mode = false;
  12968. pt_debug(dev, DL_INFO,
  12969. "%s: Disable Bridge Mode\n", __func__);
  12970. } else if (input_data[1] == 1) {
  12971. cd->bridge_mode = true;
  12972. pt_debug(dev, DL_INFO,
  12973. "%s: Enable Bridge Mode\n", __func__);
  12974. } else {
  12975. rc = -EINVAL;
  12976. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12977. __func__, input_data[1]);
  12978. }
  12979. mutex_unlock(&(cd->system_lock));
  12980. break;
  12981. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12982. mutex_lock(&cd->system_lock);
  12983. /* Only a 7bit address is valid */
  12984. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12985. client->addr = input_data[1];
  12986. pt_debug(dev, DL_INFO,
  12987. "%s: Set I2C Address: 0x%2X\n",
  12988. __func__, client->addr);
  12989. } else {
  12990. rc = -EINVAL;
  12991. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12992. __func__, input_data[1]);
  12993. client->addr = 0x24;
  12994. }
  12995. mutex_unlock(&(cd->system_lock));
  12996. break;
  12997. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12998. mutex_lock(&cd->system_lock);
  12999. if (input_data[1] == 0) {
  13000. cd->flashless_dut = 0;
  13001. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13002. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13003. __func__);
  13004. } else if (input_data[1] == 1) {
  13005. cd->flashless_dut = 1;
  13006. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13007. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13008. __func__);
  13009. }
  13010. mutex_unlock(&(cd->system_lock));
  13011. break;
  13012. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13013. mutex_lock(&cd->system_lock);
  13014. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13015. cd->force_pip2_seq = input_data[1];
  13016. cd->pip2_cmd_tag_seq = input_data[1];
  13017. pt_debug(dev, DL_INFO,
  13018. "%s: Force PIP2 Seq to: 0x%02X\n",
  13019. __func__, input_data[1]);
  13020. } else {
  13021. cd->force_pip2_seq = 0;
  13022. pt_debug(dev, DL_INFO,
  13023. "%s: Clear Forced PIP2 Seq\n", __func__);
  13024. }
  13025. mutex_unlock(&(cd->system_lock));
  13026. break;
  13027. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13028. mutex_lock(&cd->system_lock);
  13029. if (input_data[1] == 0) {
  13030. cd->bl_with_no_int = 0;
  13031. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13032. } else if (input_data[1] == 1) {
  13033. cd->bl_with_no_int = 1;
  13034. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13035. __func__);
  13036. } else {
  13037. rc = -EINVAL;
  13038. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13039. __func__, input_data[1]);
  13040. }
  13041. mutex_unlock(&(cd->system_lock));
  13042. break;
  13043. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13044. mutex_lock(&cd->system_lock);
  13045. if (input_data[1] == 0) {
  13046. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13047. pt_debug(dev, DL_INFO,
  13048. "%s: Disable Calibration cache in host\n",
  13049. __func__);
  13050. } else if (input_data[1] == 1) {
  13051. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13052. pt_debug(dev, DL_INFO,
  13053. "%s: Enable Calibration cache in host\n",
  13054. __func__);
  13055. } else {
  13056. rc = -EINVAL;
  13057. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13058. __func__, input_data[1]);
  13059. }
  13060. mutex_unlock(&(cd->system_lock));
  13061. break;
  13062. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13063. mutex_lock(&cd->system_lock);
  13064. if (input_data[1] == 0) {
  13065. cd->multi_chip = PT_FEATURE_DISABLE;
  13066. cd->ttdl_bist_select = 0x07;
  13067. pt_debug(dev, DL_INFO,
  13068. "%s: Disable Multi-chip support\n", __func__);
  13069. } else if (input_data[1] == 1) {
  13070. cd->multi_chip = PT_FEATURE_ENABLE;
  13071. cd->ttdl_bist_select = 0x3F;
  13072. pt_debug(dev, DL_INFO,
  13073. "%s: Enable Multi-chip support\n", __func__);
  13074. } else {
  13075. rc = -EINVAL;
  13076. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13077. __func__, input_data[1]);
  13078. }
  13079. mutex_unlock(&(cd->system_lock));
  13080. break;
  13081. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13082. mutex_lock(&cd->system_lock);
  13083. if (input_data[1] <= 0x07) {
  13084. cd->panel_id_support = input_data[1];
  13085. pt_debug(dev, DL_INFO,
  13086. "%s: Set panel_id_support to %d\n",
  13087. __func__, cd->panel_id_support);
  13088. } else {
  13089. rc = -EINVAL;
  13090. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13091. __func__, input_data[1]);
  13092. }
  13093. mutex_unlock(&(cd->system_lock));
  13094. break;
  13095. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13096. mutex_lock(&cd->system_lock);
  13097. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13098. /*
  13099. * The timeout is changed for some cases so the
  13100. * pip_cmd_timeout_default is used to retore back to
  13101. * what the user requested as the new timeout.
  13102. */
  13103. cd->pip_cmd_timeout_default = input_data[1];
  13104. cd->pip_cmd_timeout = input_data[1];
  13105. pt_debug(dev, DL_INFO,
  13106. "%s: PIP Timeout = %d\n", __func__,
  13107. cd->pip_cmd_timeout_default);
  13108. } else {
  13109. rc = -EINVAL;
  13110. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13111. __func__, input_data[1]);
  13112. }
  13113. mutex_unlock(&(cd->system_lock));
  13114. break;
  13115. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13116. mutex_lock(&cd->system_lock);
  13117. if (input_data[1] == 0) {
  13118. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13119. pt_debug(dev, DL_INFO,
  13120. "%s: Disable tthe_tuner HID-USB format\n",
  13121. __func__);
  13122. } else if (input_data[1] == 1) {
  13123. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13124. pt_debug(dev, DL_INFO,
  13125. "%s: Enable tthe_tuner HID-USB format\n",
  13126. __func__);
  13127. } else {
  13128. rc = -EINVAL;
  13129. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13130. __func__, input_data[1]);
  13131. }
  13132. mutex_unlock(&(cd->system_lock));
  13133. break;
  13134. #endif /* TTDL_DIAGNOSTICS */
  13135. default:
  13136. rc = -EINVAL;
  13137. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13138. }
  13139. /* Enable watchdog timer */
  13140. if (!wd_disabled)
  13141. pt_start_wd_timer(cd);
  13142. pt_drv_debug_store_exit:
  13143. if (rc)
  13144. return rc;
  13145. return size;
  13146. }
  13147. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13148. pt_drv_debug_store);
  13149. /*******************************************************************************
  13150. * FUNCTION: pt_sleep_status_show
  13151. *
  13152. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13153. * sleep status as either ON or OFF
  13154. *
  13155. * RETURN:
  13156. * 0 = success
  13157. * !0 = failure
  13158. *
  13159. * PARAMETERS:
  13160. * *dev - pointer to device structure
  13161. * *attr - pointer to device attributes
  13162. * *buf - pointer to output buffer
  13163. ******************************************************************************/
  13164. static ssize_t pt_sleep_status_show(struct device *dev,
  13165. struct device_attribute *attr, char *buf)
  13166. {
  13167. struct pt_core_data *cd = dev_get_drvdata(dev);
  13168. ssize_t ret;
  13169. mutex_lock(&cd->system_lock);
  13170. if (cd->sleep_state == SS_SLEEP_ON)
  13171. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13172. else
  13173. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13174. mutex_unlock(&cd->system_lock);
  13175. return ret;
  13176. }
  13177. /*******************************************************************************
  13178. * FUNCTION: pt_panel_id_show
  13179. *
  13180. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13181. * detected panel ID from the DUT
  13182. *
  13183. * RETURN:
  13184. * 0 = success
  13185. * !0 = failure
  13186. *
  13187. * PARAMETERS:
  13188. * *dev - pointer to device structure
  13189. * *attr - pointer to device attributes
  13190. * *buf - pointer to output buffer
  13191. ******************************************************************************/
  13192. static ssize_t pt_panel_id_show(struct device *dev,
  13193. struct device_attribute *attr, char *buf)
  13194. {
  13195. struct pt_core_data *cd = dev_get_drvdata(dev);
  13196. ssize_t ret;
  13197. u8 pid = PANEL_ID_NOT_ENABLED;
  13198. int rc = 0;
  13199. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13200. /*
  13201. * The DUT should report the same panel ID from both the BL and
  13202. * the FW unless the panel_id feature is set to only
  13203. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13204. * to retrieve the panel_id.
  13205. */
  13206. if (cd->mode == PT_MODE_BOOTLOADER) {
  13207. /*
  13208. * Return the stored value if PT_PANEL_ID_BY_BL
  13209. * is not supported while other feature exits.
  13210. */
  13211. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13212. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13213. if (rc) {
  13214. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13215. "Failed to retrieve Panel ID. ",
  13216. "Using cached value\n",
  13217. __func__);
  13218. }
  13219. }
  13220. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13221. if (cd->panel_id_support &
  13222. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13223. /* For all systems sysinfo has the panel_id */
  13224. rc = pt_hid_output_get_sysinfo(cd);
  13225. if (!rc)
  13226. pid =
  13227. cd->sysinfo.sensing_conf_data.panel_id;
  13228. pt_debug(dev, DL_ERROR,
  13229. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13230. __func__, rc, pid);
  13231. }
  13232. } else {
  13233. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13234. __func__);
  13235. rc = -EPERM;
  13236. }
  13237. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13238. if (cd->mode == PT_MODE_BOOTLOADER) {
  13239. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13240. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13241. if (rc) {
  13242. pt_debug(dev, DL_ERROR,
  13243. "%s: BL get panel ID failed rc=%d\n",
  13244. __func__, rc);
  13245. }
  13246. }
  13247. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13248. if (cd->panel_id_support &
  13249. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13250. rc = pt_hid_output_get_sysinfo(cd);
  13251. if (!rc)
  13252. pid =
  13253. cd->sysinfo.sensing_conf_data.panel_id;
  13254. pt_debug(dev, DL_ERROR,
  13255. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13256. __func__, rc, pid);
  13257. }
  13258. } else {
  13259. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13260. __func__);
  13261. rc = -EPERM;
  13262. }
  13263. } else {
  13264. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13265. __func__);
  13266. rc = -EPERM;
  13267. }
  13268. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13269. rc, pid);
  13270. return ret;
  13271. }
  13272. /*******************************************************************************
  13273. * FUNCTION: pt_get_param_store
  13274. *
  13275. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13276. * ID to retrieve with the show method.
  13277. *
  13278. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13279. *
  13280. * RETURN: Size of passed in buffer if successful
  13281. *
  13282. * PARAMETERS:
  13283. * *dev - pointer to device structure
  13284. * *attr - pointer to device attributes
  13285. * *buf - pointer to output buffer
  13286. * size - size of buf
  13287. ******************************************************************************/
  13288. static ssize_t pt_get_param_store(struct device *dev,
  13289. struct device_attribute *attr, const char *buf, size_t size)
  13290. {
  13291. struct pt_core_data *cd = dev_get_drvdata(dev);
  13292. u32 input_data[2];
  13293. int length;
  13294. int rc = 0;
  13295. /* Maximum input of one value */
  13296. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13297. ARRAY_SIZE(input_data));
  13298. if (length != 1) {
  13299. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13300. __func__);
  13301. rc = -EINVAL;
  13302. goto exit;
  13303. }
  13304. mutex_lock(&cd->system_lock);
  13305. cd->get_param_id = input_data[0];
  13306. mutex_unlock(&(cd->system_lock));
  13307. exit:
  13308. if (rc)
  13309. return rc;
  13310. return size;
  13311. }
  13312. /*******************************************************************************
  13313. * FUNCTION: pt_get_param_show
  13314. *
  13315. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13316. * parameter data from the DUT based on the ID stored in the core
  13317. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13318. * communicate or some other error occures, an error status is returned
  13319. * with no value following.
  13320. * Output is in the form:
  13321. * Status: x
  13322. * 0xyyyyyyyy
  13323. * The 32bit data will only follow the status code if the status == 0
  13324. *
  13325. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13326. *
  13327. * RETURN:
  13328. * 0 = success
  13329. * !0 = failure
  13330. *
  13331. * PARAMETERS:
  13332. * *dev - pointer to device structure
  13333. * *attr - pointer to device attributes
  13334. * *buf - pointer to output buffer
  13335. ******************************************************************************/
  13336. static ssize_t pt_get_param_show(struct device *dev,
  13337. struct device_attribute *attr, char *buf)
  13338. {
  13339. struct pt_core_data *cd = dev_get_drvdata(dev);
  13340. ssize_t ret = 0;
  13341. int status;
  13342. u32 value = 0;
  13343. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13344. if (status) {
  13345. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13346. __func__, "pt_get_param", status);
  13347. ret = scnprintf(buf, strlen(buf),
  13348. "%s %d\n",
  13349. "Status:", status);
  13350. } else {
  13351. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13352. __func__, cd->get_param_id, value);
  13353. ret = scnprintf(buf, strlen(buf),
  13354. "Status: %d\n"
  13355. "0x%04X\n",
  13356. status, value);
  13357. }
  13358. return ret;
  13359. }
  13360. /*******************************************************************************
  13361. * FUNCTION: pt_ttdl_restart_show
  13362. *
  13363. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13364. * probed modules, calls startup() and then re-probes modules.
  13365. *
  13366. * RETURN: size of data written to sysfs node
  13367. *
  13368. * PARAMETERS:
  13369. * *dev - pointer to device structure
  13370. * *attr - pointer to device attributes structure
  13371. * *buf - pointer to print output buffer
  13372. ******************************************************************************/
  13373. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13374. struct device_attribute *attr, char *buf)
  13375. {
  13376. struct pt_core_data *cd = dev_get_drvdata(dev);
  13377. int t;
  13378. int rc = 0;
  13379. mutex_lock(&cd->system_lock);
  13380. cd->startup_state = STARTUP_NONE;
  13381. mutex_unlock(&(cd->system_lock));
  13382. /* ensure no left over exclusive access is still locked */
  13383. release_exclusive(cd, cd->dev);
  13384. pt_queue_restart(cd);
  13385. t = wait_event_timeout(cd->wait_q,
  13386. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13387. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13388. if (IS_TMO(t)) {
  13389. pt_debug(cd->dev, DL_ERROR,
  13390. "%s: TMO waiting for FW sentinel\n", __func__);
  13391. rc = -ETIME;
  13392. }
  13393. return scnprintf(buf, strlen(buf),
  13394. "Status: %d\n"
  13395. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13396. }
  13397. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13398. /*******************************************************************************
  13399. * FUNCTION: pt_pip2_gpio_read_show
  13400. *
  13401. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13402. * contents of the response to the passed in output buffer.
  13403. *
  13404. * RETURN: size of data written to sysfs node
  13405. *
  13406. * PARAMETERS:
  13407. * *dev - pointer to device structure
  13408. * *attr - pointer to device attributes structure
  13409. * *buf - pointer to print output buffer
  13410. ******************************************************************************/
  13411. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13412. struct device_attribute *attr, char *buf)
  13413. {
  13414. struct pt_core_data *cd = dev_get_drvdata(dev);
  13415. u8 status = 0;
  13416. u32 gpio_value = 0;
  13417. int rc = 0;
  13418. /* This functionality is only available in the BL */
  13419. if (cd->mode == PT_MODE_BOOTLOADER)
  13420. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13421. else
  13422. rc = -EPERM;
  13423. if (!rc) {
  13424. if (status == 0)
  13425. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13426. "Status: %d\n"
  13427. "DUT GPIO Reg: 0x%08X\n",
  13428. rc, gpio_value);
  13429. else
  13430. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13431. "Status: %d\n"
  13432. "DUT GPIO Reg: n/a\n",
  13433. status);
  13434. } else
  13435. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13436. "Status: %d\n"
  13437. "DUT GPIO Reg: n/a\n",
  13438. rc);
  13439. }
  13440. /*******************************************************************************
  13441. * FUNCTION: pt_pip2_version_show
  13442. *
  13443. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13444. * contents of the response to the passed in output buffer.
  13445. *
  13446. * RETURN: size of data written to sysfs node
  13447. *
  13448. * PARAMETERS:
  13449. * *dev - pointer to device structure
  13450. * *attr - pointer to device attributes structure
  13451. * *buf - pointer to print output buffer
  13452. ******************************************************************************/
  13453. static ssize_t pt_pip2_version_show(struct device *dev,
  13454. struct device_attribute *attr, char *buf)
  13455. {
  13456. int rc = 0;
  13457. struct pt_core_data *cd = dev_get_drvdata(dev);
  13458. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13459. rc = pt_pip2_get_version(cd);
  13460. if (!rc) {
  13461. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13462. "PIP VERSION : %02X.%02X\n"
  13463. "BL VERSION : %02X.%02X\n"
  13464. "FW VERSION : %02X.%02X\n"
  13465. "SILICON ID : %04X.%04X\n"
  13466. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13467. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13468. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13469. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13470. ttdata->chip_id, ttdata->chip_rev,
  13471. ttdata->uid[0], ttdata->uid[1],
  13472. ttdata->uid[2], ttdata->uid[3],
  13473. ttdata->uid[4], ttdata->uid[5],
  13474. ttdata->uid[6], ttdata->uid[7],
  13475. ttdata->uid[8], ttdata->uid[9],
  13476. ttdata->uid[10], ttdata->uid[11]);
  13477. } else {
  13478. pt_debug(dev, DL_ERROR,
  13479. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13480. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13481. "PIP VERSION : -\n"
  13482. "BL VERSION : -\n"
  13483. "FW VERSION : -\n"
  13484. "SILICON ID : -\n"
  13485. "UID : -\n");
  13486. }
  13487. }
  13488. #ifdef TTDL_DIAGNOSTICS
  13489. /*******************************************************************************
  13490. * FUNCTION: pt_ttdl_status_show
  13491. *
  13492. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13493. * variable states and GPIO levels. Additional information printed when
  13494. * TTDL_DIAGNOSTICS is enabled.
  13495. *
  13496. * NOTE: All counters will be reset to 0 when this function is called.
  13497. *
  13498. * RETURN:
  13499. * 0 = success
  13500. * !0 = failure
  13501. *
  13502. * PARAMETERS:
  13503. * *dev - pointer to device structure
  13504. * *attr - pointer to device attributes
  13505. * *buf - pointer to output buffer
  13506. ******************************************************************************/
  13507. static ssize_t pt_ttdl_status_show(struct device *dev,
  13508. struct device_attribute *attr, char *buf)
  13509. {
  13510. struct pt_core_data *cd = dev_get_drvdata(dev);
  13511. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13512. struct i2c_client *client = to_i2c_client(dev);
  13513. ssize_t ret;
  13514. u16 cal_size = 0;
  13515. unsigned short crc = 0;
  13516. if (cd->cal_cache_in_host)
  13517. _pt_manage_local_cal_data(dev,
  13518. PT_CAL_DATA_INFO, &cal_size, &crc);
  13519. ret = scnprintf(buf, strlen(buf),
  13520. "%s: 0x%04X\n"
  13521. "%s: %d\n"
  13522. "%s: %s\n"
  13523. "%s: %s %s\n"
  13524. "%s: %s\n"
  13525. "%s: 0x%02X\n"
  13526. "%s: %s\n"
  13527. "%s: %s\n"
  13528. "%s: %s\n"
  13529. "%s: %s\n"
  13530. "%s: %d\n"
  13531. "%s: %d\n"
  13532. "%s: %s\n"
  13533. "%s: %s\n"
  13534. "%s: %d\n"
  13535. #ifdef TTDL_DIAGNOSTICS
  13536. "%s: %d\n"
  13537. "%s: %d\n"
  13538. "%s: %d\n"
  13539. "%s: %d\n"
  13540. "%s: %d\n"
  13541. "%s: %d\n"
  13542. "%s: %d\n"
  13543. "%s: %d\n"
  13544. "%s: %d\n"
  13545. "%s: %d\n"
  13546. "%s: %s\n"
  13547. "%s: %s\n"
  13548. "%s: %s\n"
  13549. "%s: %d\n"
  13550. "%s: 0x%04X\n"
  13551. "%s: %s\n"
  13552. #endif /* TTDL_DIAGNOSTICS */
  13553. ,
  13554. "Startup Status ", cd->startup_status,
  13555. "TTDL Debug Level ", cd->debug_level,
  13556. "Mode ",
  13557. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13558. "Operational" : "BL") : "Unknown",
  13559. "DUT Generation ",
  13560. cd->active_dut_generation ?
  13561. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13562. "PT TC/TT" : "Gen5/6") : "Unknown",
  13563. cd->active_dut_generation ?
  13564. (cd->set_dut_generation == true ?
  13565. "(Set)" : "(Detected)") : "",
  13566. "HW Detected ",
  13567. cd->hw_detected ? "True" : "False",
  13568. "I2C Address ",
  13569. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13570. "Active Bus Module ",
  13571. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13572. "Flashless Mode ",
  13573. cd->flashless_dut == 1 ? "Yes" : "No",
  13574. "GPIO state - IRQ ",
  13575. cd->cpdata->irq_stat ?
  13576. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13577. "High" : "Low") : "not defined",
  13578. "GPIO state - TP_XRES ",
  13579. pdata->core_pdata->rst_gpio ?
  13580. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13581. "High" : "Low") : "not defined",
  13582. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13583. "Startup Retry Count ", cd->startup_retry_count,
  13584. "WD - Manual Force Stop ",
  13585. cd->watchdog_force_stop ? "True" : "False",
  13586. "WD - Enabled ",
  13587. cd->watchdog_enabled ? "True" : "False",
  13588. "WD - Interval (ms) ", cd->watchdog_interval
  13589. #ifdef TTDL_DIAGNOSTICS
  13590. , "WD - Triggered Count ", cd->watchdog_count,
  13591. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13592. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13593. "WD - XRES Count ", cd->wd_xres_count,
  13594. "IRQ Triggered Count ", cd->irq_count,
  13595. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13596. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13597. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13598. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13599. "Error GPIO trigger type ", cd->err_gpio_type,
  13600. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13601. "Suppress No-Flash Auto BL ",
  13602. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13603. "Yes" : "No",
  13604. "Calibration Cache on host ",
  13605. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13606. "Yes" : "No",
  13607. "Calibration Cache size ", cal_size,
  13608. "Calibration Cache chip ID ", crc,
  13609. "Multi-Chip Support ",
  13610. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13611. #endif /* TTDL_DIAGNOSTICS */
  13612. );
  13613. #ifdef TTDL_DIAGNOSTICS
  13614. /* Reset all diagnostic counters */
  13615. cd->watchdog_count = 0;
  13616. cd->watchdog_irq_stuck_count = 0;
  13617. cd->watchdog_failed_access_count = 0;
  13618. cd->wd_xres_count = 0;
  13619. cd->irq_count = 0;
  13620. cd->bl_retry_packet_count = 0;
  13621. cd->pip2_crc_error_count = 0;
  13622. cd->bus_transmit_error_count = 0;
  13623. #endif
  13624. return ret;
  13625. }
  13626. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13627. /*******************************************************************************
  13628. * FUNCTION: pt_pip2_enter_bl_show
  13629. *
  13630. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13631. * the DUT into the BL and show the success or failure of entering the BL
  13632. *
  13633. * RETURN:
  13634. * 0 = success
  13635. * !0 = failure
  13636. *
  13637. * PARAMETERS:
  13638. * *dev - pointer to device structure
  13639. * *attr - pointer to device attributes
  13640. * *buf - pointer to output buffer
  13641. ******************************************************************************/
  13642. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13643. struct device_attribute *attr, char *buf)
  13644. {
  13645. ssize_t ret = 0;
  13646. int rc = 0;
  13647. int result = 0;
  13648. u8 mode = PT_MODE_UNKNOWN;
  13649. struct pt_core_data *cd = dev_get_drvdata(dev);
  13650. /* Turn off the TTDL WD before enter bootloader */
  13651. pt_stop_wd_timer(cd);
  13652. /* Ensure NO enumeration work is queued or will be queued */
  13653. cancel_work_sync(&cd->enum_work);
  13654. mutex_lock(&cd->system_lock);
  13655. cd->bridge_mode = true;
  13656. mutex_unlock(&cd->system_lock);
  13657. /* set mode to operational to avoid any extra PIP traffic */
  13658. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13659. switch (result) {
  13660. case PT_ENTER_BL_PASS:
  13661. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13662. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13663. break;
  13664. case PT_ENTER_BL_ERROR:
  13665. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13666. rc,
  13667. " Unknown Error");
  13668. break;
  13669. case PT_ENTER_BL_RESET_FAIL:
  13670. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13671. rc,
  13672. " Soft Reset Failed");
  13673. break;
  13674. case PT_ENTER_BL_HID_START_BL_FAIL:
  13675. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13676. rc,
  13677. " PIP Start BL Cmd Failed");
  13678. break;
  13679. case PT_ENTER_BL_CONFIRM_FAIL:
  13680. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13681. rc,
  13682. " Error confirming DUT entered BL");
  13683. break;
  13684. default:
  13685. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13686. rc, " Unknown Error");
  13687. break;
  13688. };
  13689. /* Allow enumeration work to be queued again */
  13690. cd->bridge_mode = false;
  13691. return ret;
  13692. }
  13693. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13694. /*******************************************************************************
  13695. * FUNCTION: pt_pip2_exit_bl_show
  13696. *
  13697. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13698. * launch the APP and put the DUT Application mode
  13699. *
  13700. * RETURN:
  13701. * 0 = success
  13702. * !0 = failure
  13703. *
  13704. * PARAMETERS:
  13705. * *dev - pointer to device structure
  13706. * *attr - pointer to device attributes
  13707. * *buf - pointer to output buffer
  13708. ******************************************************************************/
  13709. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13710. struct device_attribute *attr, char *buf)
  13711. {
  13712. struct pt_core_data *cd = dev_get_drvdata(dev);
  13713. ssize_t ret = 0;
  13714. int rc = 0;
  13715. u8 status_str[PT_STATUS_STR_LEN];
  13716. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13717. /*
  13718. * Perform enum if startup_status doesn't reach to
  13719. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13720. */
  13721. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13722. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13723. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13724. strlcpy(status_str,
  13725. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13726. }
  13727. }
  13728. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13729. rc, status_str);
  13730. return ret;
  13731. }
  13732. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13733. #endif
  13734. #ifdef EASYWAKE_TSG6
  13735. /*******************************************************************************
  13736. * FUNCTION: pt_easy_wakeup_gesture_show
  13737. *
  13738. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13739. * current easy wakeup gesture
  13740. *
  13741. * RETURN:
  13742. * 0 = success
  13743. * !0 = failure
  13744. *
  13745. * PARAMETERS:
  13746. * *dev - pointer to device structure
  13747. * *attr - pointer to device attributes
  13748. * *buf - pointer to output buffer
  13749. ******************************************************************************/
  13750. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13751. struct device_attribute *attr, char *buf)
  13752. {
  13753. struct pt_core_data *cd = dev_get_drvdata(dev);
  13754. ssize_t ret;
  13755. mutex_lock(&cd->system_lock);
  13756. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13757. cd->easy_wakeup_gesture);
  13758. mutex_unlock(&cd->system_lock);
  13759. return ret;
  13760. }
  13761. /*******************************************************************************
  13762. * FUNCTION: pt_easy_wakeup_gesture_store
  13763. *
  13764. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13765. * allows the wake gesture to be set to a custom value.
  13766. *
  13767. * RETURN: Size of passed in buffer is success
  13768. *
  13769. * PARAMETERS:
  13770. * *dev - pointer to device structure
  13771. * *attr - pointer to device attributes
  13772. * *buf - pointer to buffer that hold the command parameters
  13773. * size - size of buf
  13774. ******************************************************************************/
  13775. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13776. struct device_attribute *attr, const char *buf, size_t size)
  13777. {
  13778. struct pt_core_data *cd = dev_get_drvdata(dev);
  13779. u32 input_data[2];
  13780. int length;
  13781. int rc = 0;
  13782. /* Maximum input of one value */
  13783. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13784. ARRAY_SIZE(input_data));
  13785. if (length != 1) {
  13786. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13787. __func__);
  13788. rc = -EINVAL;
  13789. goto exit;
  13790. }
  13791. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13792. __func__, cd->features.easywake);
  13793. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13794. rc = -EINVAL;
  13795. goto exit;
  13796. }
  13797. pm_runtime_get_sync(dev);
  13798. mutex_lock(&cd->system_lock);
  13799. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13800. cd->easy_wakeup_gesture = (u8)input_data[0];
  13801. pt_debug(dev, DL_INFO,
  13802. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13803. __func__, cd->easy_wakeup_gesture);
  13804. } else
  13805. rc = -ENODEV;
  13806. mutex_unlock(&cd->system_lock);
  13807. pm_runtime_put(dev);
  13808. exit:
  13809. if (rc)
  13810. return rc;
  13811. return size;
  13812. }
  13813. /*******************************************************************************
  13814. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13815. *
  13816. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13817. * show the TSG6 easywake gesture ID
  13818. *
  13819. * RETURN:
  13820. * 0 = success
  13821. * !0 = failure
  13822. *
  13823. * PARAMETERS:
  13824. * *dev - pointer to device structure
  13825. * *attr - pointer to device attributes
  13826. * *buf - pointer to output buffer
  13827. ******************************************************************************/
  13828. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13829. struct device_attribute *attr, char *buf)
  13830. {
  13831. struct pt_core_data *cd = dev_get_drvdata(dev);
  13832. ssize_t ret;
  13833. mutex_lock(&cd->system_lock);
  13834. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13835. cd->gesture_id);
  13836. mutex_unlock(&cd->system_lock);
  13837. return ret;
  13838. }
  13839. /*******************************************************************************
  13840. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13841. *
  13842. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13843. * show the TSG6 easywake gesture data in the following format:
  13844. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13845. *
  13846. * RETURN:
  13847. * 0 = success
  13848. * !0 = failure
  13849. *
  13850. * PARAMETERS:
  13851. * *dev - pointer to device structure
  13852. * *attr - pointer to device attributes
  13853. * *buf - pointer to output buffer
  13854. ******************************************************************************/
  13855. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13856. struct device_attribute *attr, char *buf)
  13857. {
  13858. struct pt_core_data *cd = dev_get_drvdata(dev);
  13859. ssize_t ret = 0;
  13860. int i;
  13861. mutex_lock(&cd->system_lock);
  13862. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13863. for (i = 0; i < cd->gesture_data_length; i++)
  13864. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13865. "0x%02X\n", cd->gesture_data[i]);
  13866. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13867. "(%d bytes)\n", cd->gesture_data_length);
  13868. mutex_unlock(&cd->system_lock);
  13869. return ret;
  13870. }
  13871. #endif /* EASYWAKE_TSG6 */
  13872. #ifdef TTDL_DIAGNOSTICS
  13873. /*******************************************************************************
  13874. * FUNCTION: pt_err_gpio_show
  13875. *
  13876. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13877. * setting up the gpio was successful
  13878. *
  13879. * RETURN: Char buffer with printed GPIO creation state
  13880. *
  13881. * PARAMETERS:
  13882. * *dev - pointer to device structure
  13883. * *attr - pointer to device attributes
  13884. * *buf - pointer to output buffer
  13885. ******************************************************************************/
  13886. static ssize_t pt_err_gpio_show(struct device *dev,
  13887. struct device_attribute *attr, char *buf)
  13888. {
  13889. struct pt_core_data *cd = dev_get_drvdata(dev);
  13890. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13891. "Err GPIO (%d) : %s\n"
  13892. "Err GPIO trig type: %d\n",
  13893. cd->err_gpio,
  13894. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13895. "HIGH" : "low") : "not defined"),
  13896. cd->err_gpio_type);
  13897. }
  13898. /*******************************************************************************
  13899. * FUNCTION: pt_err_gpio_store
  13900. *
  13901. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13902. * available host GPIO to be used to trigger when TTDL detects a PIP
  13903. * command/response timeout.
  13904. *
  13905. * RETURN:
  13906. * 0 = success
  13907. * !0 = failure
  13908. *
  13909. * PARAMETERS:
  13910. * *dev - pointer to device structure
  13911. * *attr - pointer to device attributes
  13912. * *buf - pointer to buffer that hold the command parameters
  13913. * size - size of buf
  13914. ******************************************************************************/
  13915. static ssize_t pt_err_gpio_store(struct device *dev,
  13916. struct device_attribute *attr, const char *buf, size_t size)
  13917. {
  13918. struct pt_core_data *cd = dev_get_drvdata(dev);
  13919. unsigned long gpio;
  13920. int rc = 0;
  13921. u32 input_data[3];
  13922. int length;
  13923. u8 err_type;
  13924. input_data[0] = 0;
  13925. input_data[1] = 0;
  13926. /* Maximmum input is two elements */
  13927. length = _pt_ic_parse_input(dev, buf, size,
  13928. input_data, ARRAY_SIZE(input_data));
  13929. if (length < 1 || length > 2) {
  13930. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13931. __func__);
  13932. rc = -EINVAL;
  13933. goto exit;
  13934. }
  13935. gpio = input_data[0];
  13936. err_type = (u8)input_data[1];
  13937. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13938. rc = -EINVAL;
  13939. goto exit;
  13940. }
  13941. mutex_lock(&cd->system_lock);
  13942. gpio_free(gpio);
  13943. rc = gpio_request(gpio, NULL);
  13944. if (rc) {
  13945. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13946. rc = -ENODEV;
  13947. } else {
  13948. cd->err_gpio = gpio;
  13949. cd->err_gpio_type = err_type;
  13950. gpio_direction_output(gpio, 0);
  13951. }
  13952. mutex_unlock(&cd->system_lock);
  13953. exit:
  13954. if (rc)
  13955. return rc;
  13956. return size;
  13957. }
  13958. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13959. pt_err_gpio_store);
  13960. /*******************************************************************************
  13961. * FUNCTION: pt_drv_irq_show
  13962. *
  13963. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13964. * TTDL interrupt is enabled/disabled
  13965. *
  13966. * RETURN:
  13967. * 0 = success
  13968. * !0 = failure
  13969. *
  13970. * PARAMETERS:
  13971. * *dev - pointer to device structure
  13972. * *attr - pointer to device attributes
  13973. * *buf - pointer to output buffer
  13974. ******************************************************************************/
  13975. static ssize_t pt_drv_irq_show(struct device *dev,
  13976. struct device_attribute *attr, char *buf)
  13977. {
  13978. struct pt_core_data *cd = dev_get_drvdata(dev);
  13979. ssize_t ret = 0;
  13980. mutex_lock(&cd->system_lock);
  13981. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13982. if (cd->irq_enabled)
  13983. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13984. "Driver interrupt: ENABLED\n");
  13985. else
  13986. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13987. "Driver interrupt: DISABLED\n");
  13988. mutex_unlock(&cd->system_lock);
  13989. return ret;
  13990. }
  13991. /*******************************************************************************
  13992. * FUNCTION: pt_drv_irq_store
  13993. *
  13994. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13995. * IRQ to be enabled/disabled.
  13996. *
  13997. * RETURN: Size of passed in buffer
  13998. *
  13999. * PARAMETERS:
  14000. * *dev - pointer to device structure
  14001. * *attr - pointer to device attributes
  14002. * *buf - pointer to buffer that hold the command parameters
  14003. * size - size of buf
  14004. ******************************************************************************/
  14005. static ssize_t pt_drv_irq_store(struct device *dev,
  14006. struct device_attribute *attr, const char *buf, size_t size)
  14007. {
  14008. struct pt_core_data *cd = dev_get_drvdata(dev);
  14009. u32 input_data[2];
  14010. int length;
  14011. int rc = 0;
  14012. /* Maximum input of one value */
  14013. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14014. ARRAY_SIZE(input_data));
  14015. if (length != 1) {
  14016. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14017. __func__);
  14018. rc = -EINVAL;
  14019. goto exit;
  14020. }
  14021. mutex_lock(&cd->system_lock);
  14022. switch (input_data[0]) {
  14023. case 0:
  14024. if (cd->irq_enabled) {
  14025. cd->irq_enabled = false;
  14026. /* Disable IRQ has no return value to check */
  14027. disable_irq_nosync(cd->irq);
  14028. pt_debug(dev, DL_INFO,
  14029. "%s: Driver IRQ now disabled\n",
  14030. __func__);
  14031. } else
  14032. pt_debug(dev, DL_INFO,
  14033. "%s: Driver IRQ already disabled\n",
  14034. __func__);
  14035. break;
  14036. case 1:
  14037. if (cd->irq_enabled == false) {
  14038. cd->irq_enabled = true;
  14039. enable_irq(cd->irq);
  14040. pt_debug(dev, DL_INFO,
  14041. "%s: Driver IRQ now enabled\n",
  14042. __func__);
  14043. } else
  14044. pt_debug(dev, DL_INFO,
  14045. "%s: Driver IRQ already enabled\n",
  14046. __func__);
  14047. break;
  14048. default:
  14049. rc = -EINVAL;
  14050. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14051. }
  14052. mutex_unlock(&(cd->system_lock));
  14053. exit:
  14054. if (rc)
  14055. return rc;
  14056. return size;
  14057. }
  14058. /*******************************************************************************
  14059. * FUNCTION: pt_pip2_bin_hdr_show
  14060. *
  14061. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14062. * the bin file header from flash and show each field
  14063. *
  14064. * RETURN:
  14065. * 0 = success
  14066. * !0 = failure
  14067. *
  14068. * PARAMETERS:
  14069. * *dev - pointer to device structure
  14070. * *attr - pointer to device attributes
  14071. * *buf - pointer to output buffer
  14072. ******************************************************************************/
  14073. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14074. struct device_attribute *attr, char *buf)
  14075. {
  14076. ssize_t ret = 0;
  14077. struct pt_bin_file_hdr hdr = {0};
  14078. int rc;
  14079. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14080. ret = scnprintf(buf, strlen(buf),
  14081. "%s: %d\n"
  14082. "%s: %d\n"
  14083. "%s: 0x%04X\n"
  14084. "%s: %d\n"
  14085. "%s: %d\n"
  14086. "%s: %d\n"
  14087. "%s: 0x%08X\n"
  14088. "%s: 0x%04X\n"
  14089. "%s: 0x%04X\n"
  14090. "%s: %d\n"
  14091. "%s: %d\n",
  14092. "Status", rc,
  14093. "Header Length ", hdr.length,
  14094. "TTPID ", hdr.ttpid,
  14095. "FW Major Ver ", hdr.fw_major,
  14096. "FW Minor Ver ", hdr.fw_minor,
  14097. "FW Rev Control ", hdr.fw_rev_ctrl,
  14098. "FW CRC ", hdr.fw_crc,
  14099. "Silicon Rev ", hdr.si_rev,
  14100. "Silicon ID ", hdr.si_id,
  14101. "Config Ver ", hdr.config_ver,
  14102. "HEX File Size ", hdr.hex_file_size
  14103. );
  14104. return ret;
  14105. }
  14106. /*******************************************************************************
  14107. * FUNCTION: pt_platform_data_show
  14108. *
  14109. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14110. * active platform data including: GPIOs, Vendor and Product IDs,
  14111. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14112. * HID registers, and Easy wake gesture
  14113. *
  14114. * RETURN: Size of printed data
  14115. *
  14116. * PARAMETERS:
  14117. * *dev - pointer to device structure
  14118. * *attr - pointer to device attributes
  14119. * *buf - pointer to output buffer
  14120. ******************************************************************************/
  14121. static ssize_t pt_platform_data_show(struct device *dev,
  14122. struct device_attribute *attr, char *buf)
  14123. {
  14124. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14125. struct pt_core_data *cd = dev_get_drvdata(dev);
  14126. ssize_t ret;
  14127. ret = scnprintf(buf, strlen(buf),
  14128. "%s: %d\n"
  14129. "%s: %d\n"
  14130. "%s: %d\n"
  14131. "%s: %d\n"
  14132. "%s: %d\n"
  14133. "%s: %d\n"
  14134. "%s: %d\n"
  14135. "%s: %d\n"
  14136. "%s: %d\n"
  14137. "%s: %d\n"
  14138. "%s: %d\n"
  14139. "%s: %d\n"
  14140. "%s: %d\n"
  14141. "%s: %d\n"
  14142. "%s: %d\n"
  14143. "%s: %d\n"
  14144. "%s: %d\n"
  14145. "%s: %d\n"
  14146. "%s: %d\n"
  14147. "%s: %d\n"
  14148. "%s: %d\n"
  14149. "%s: %d\n"
  14150. "%s: %d\n"
  14151. "%s: %d\n"
  14152. "%s: %d\n"
  14153. "%s: %d\n"
  14154. "%s: %d\n"
  14155. "%s: %s\n"
  14156. "%s: %s\n"
  14157. "%s: %d\n",
  14158. "Status", 0,
  14159. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14160. "Interrupt GPIO Value ",
  14161. pdata->core_pdata->irq_gpio ?
  14162. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14163. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14164. "Reset GPIO Value ",
  14165. pdata->core_pdata->rst_gpio ?
  14166. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14167. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14168. "DDI Reset GPIO Value ",
  14169. pdata->core_pdata->ddi_rst_gpio ?
  14170. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14171. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14172. "VDDI GPIO Value ",
  14173. pdata->core_pdata->vddi_gpio ?
  14174. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14175. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14176. "VCC GPIO Value ",
  14177. pdata->core_pdata->vcc_gpio ?
  14178. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14179. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14180. "AVDD GPIO Value ",
  14181. pdata->core_pdata->avdd_gpio ?
  14182. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14183. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14184. "AVEE GPIO Value ",
  14185. pdata->core_pdata->avee_gpio ?
  14186. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14187. "Vendor ID ", pdata->core_pdata->vendor_id,
  14188. "Product ID ", pdata->core_pdata->product_id,
  14189. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14190. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14191. "Core data flags ", pdata->core_pdata->flags,
  14192. "MT data flags ", pdata->mt_pdata->flags,
  14193. "Loader data flags ", pdata->loader_pdata->flags,
  14194. "Level trigger delay (us) ",
  14195. pdata->core_pdata->level_irq_udelay,
  14196. "HID Descriptor Register ",
  14197. pdata->core_pdata->hid_desc_register,
  14198. "HID Output Register ",
  14199. cd->hid_desc.output_register,
  14200. "HID Command Register ",
  14201. cd->hid_desc.command_register,
  14202. "Easy wakeup gesture ",
  14203. pdata->core_pdata->easy_wakeup_gesture,
  14204. "Config DUT generation ",
  14205. pdata->core_pdata->config_dut_generation ?
  14206. (pdata->core_pdata->config_dut_generation ==
  14207. CONFIG_DUT_PIP2_CAPABLE ?
  14208. "PT TC/TT" : "Gen5/6") : "Auto",
  14209. "Watchdog Force Stop ",
  14210. pdata->core_pdata->watchdog_force_stop ?
  14211. "True" : "False",
  14212. "Panel ID Support ",
  14213. pdata->core_pdata->panel_id_support);
  14214. return ret;
  14215. }
  14216. #define PT_ERR_STR_SIZE 64
  14217. /*******************************************************************************
  14218. * FUNCTION: pt_bist_bus_test
  14219. *
  14220. * SUMMARY: Tests the connectivity of the active bus pins:
  14221. * I2C - SDA and SCL
  14222. * SPI - MOSI, MISO, CLK
  14223. *
  14224. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14225. * bus. If any data is read we know the I2C/SPI pins are connected
  14226. *
  14227. * RETURN:
  14228. * 0 = Success
  14229. * !0 = Failure
  14230. *
  14231. * PARAMETERS:
  14232. * *dev - pointer to device structure
  14233. * *net_toggled - pointer to where to store if bus toggled
  14234. * *err_str - pointer to error string buffer
  14235. ******************************************************************************/
  14236. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14237. {
  14238. struct pt_core_data *cd = dev_get_drvdata(dev);
  14239. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14240. u8 *read_buf = NULL;
  14241. int bytes_read = 0;
  14242. int rc = 0;
  14243. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14244. if (read_buf == NULL) {
  14245. rc = -ENOMEM;
  14246. goto exit;
  14247. }
  14248. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14249. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14250. disable_irq(cd->irq);
  14251. mutex_lock(&cd->system_lock);
  14252. cd->irq_disabled = true;
  14253. mutex_unlock(&cd->system_lock);
  14254. /*
  14255. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14256. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14257. */
  14258. usleep_range(5000, 6000);
  14259. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14260. __func__, (int)sizeof(ver_cmd));
  14261. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14262. ">>> User CMD");
  14263. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14264. if (rc) {
  14265. pt_debug(dev, DL_ERROR,
  14266. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14267. *net_toggled = 0;
  14268. strlcpy(err_str,
  14269. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14270. goto exit_enable_irq;
  14271. }
  14272. usleep_range(4000, 5000);
  14273. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14274. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14275. __func__, bytes_read);
  14276. if (bytes_read == 0) {
  14277. *net_toggled = 0;
  14278. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14279. __func__);
  14280. strlcpy(err_str,
  14281. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14282. rc = -EIO;
  14283. goto exit_enable_irq;
  14284. } else {
  14285. if (cd->bus_ops->bustype == BUS_I2C)
  14286. *net_toggled = 1;
  14287. else {
  14288. if ((bytes_read > 3) &&
  14289. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14290. PIP2_CMD_ID_VERSION)
  14291. *net_toggled = 1;
  14292. else {
  14293. *net_toggled = 0;
  14294. pt_debug(dev, DL_INFO,
  14295. "%s: BUS Read Failed, %d bytes read\n",
  14296. __func__, bytes_read);
  14297. strlcpy(err_str,
  14298. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14299. }
  14300. }
  14301. }
  14302. exit_enable_irq:
  14303. enable_irq(cd->irq);
  14304. usleep_range(5000, 6000);
  14305. mutex_lock(&cd->system_lock);
  14306. cd->irq_disabled = false;
  14307. mutex_unlock(&cd->system_lock);
  14308. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14309. exit:
  14310. kfree(read_buf);
  14311. return rc;
  14312. }
  14313. /*******************************************************************************
  14314. * FUNCTION: pt_bist_irq_test
  14315. *
  14316. * SUMMARY: Tests the connectivity of the IRQ net
  14317. *
  14318. * This test will ensure there is a good connection between the host
  14319. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14320. * asserted and if so keep reading messages off of the bus until
  14321. * it de-asserts. Possible outcomes:
  14322. * - IRQ was already de-asserted: Send a PIP command and if an
  14323. * interrupt is generated the test passes.
  14324. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14325. * test passes.
  14326. * - IRQ stays asserted: After reading the bus multiple times
  14327. * the IRQ stays asserted. Likely open or shorted to GND
  14328. *
  14329. * RETURN:
  14330. * 0 = Success
  14331. * !0 = Failure
  14332. *
  14333. * PARAMETERS:
  14334. * *dev - pointer to device structure
  14335. * *bus_toggled - pointer to where to store if bus toggled
  14336. * *irq_toggled - pointer to where to store if IRQ toggled
  14337. * *xres_toggled - pointer to where to store if XRES toggled
  14338. * *err_str - pointer to error string buffer
  14339. ******************************************************************************/
  14340. static int pt_bist_irq_test(struct device *dev,
  14341. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14342. {
  14343. struct pt_core_data *cd = dev_get_drvdata(dev);
  14344. u8 *read_buf = NULL;
  14345. u8 mode = PT_MODE_UNKNOWN;
  14346. u16 actual_read_len;
  14347. int bytes_read = 0;
  14348. int count = 0;
  14349. int rc = 0;
  14350. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14351. if (read_buf == NULL) {
  14352. rc = -ENOMEM;
  14353. goto exit;
  14354. }
  14355. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14356. cd->irq_count = 0;
  14357. /*
  14358. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14359. * the bus based on the 2 byte initial length read. Try up to 5x.
  14360. */
  14361. while (pt_check_irq_asserted(cd) && count < 5) {
  14362. count++;
  14363. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14364. }
  14365. if (count > 1 && count < 5 && bytes_read > 0) {
  14366. /*
  14367. * IRQ was stuck but data was successfully read from the
  14368. * bus releasing the IRQ line.
  14369. */
  14370. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14371. __func__, count, bytes_read);
  14372. *bus_toggled = 1;
  14373. *irq_toggled = 1;
  14374. goto exit;
  14375. }
  14376. if (count == 5 && bytes_read == 0) {
  14377. /*
  14378. * Looped 5x and read nothing off the bus yet the IRQ is still
  14379. * asserted. Possible conditions:
  14380. * - IRQ open circuit
  14381. * - IRQ shorted to GND
  14382. * - I2C/SPI bus is disconnected
  14383. * - FW holding the pin low
  14384. * Try entering the BL to see if communication works there.
  14385. */
  14386. mode = PT_MODE_IGNORE;
  14387. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14388. if (rc) {
  14389. pt_debug(dev, DL_ERROR,
  14390. "%s Failed to enter BL\n", __func__);
  14391. strlcpy(err_str,
  14392. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14393. *irq_toggled = 0;
  14394. goto exit;
  14395. }
  14396. /*
  14397. * If original mode was operational and we successfully
  14398. * entered the BL, then the XRES net must have toggled
  14399. */
  14400. if (mode == PT_MODE_OPERATIONAL)
  14401. *xres_toggled = 1;
  14402. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14403. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14404. &actual_read_len);
  14405. if (rc) {
  14406. /*
  14407. * Could not communicate to DUT in BL mode. Save the
  14408. * error string, slim chance but the XRES test below may
  14409. * show the IRQ is actually working.
  14410. */
  14411. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14412. pt_debug(dev, DL_ERROR,
  14413. "%s: %s, count=%d bytes_read=%d\n",
  14414. __func__, err_str, count, bytes_read);
  14415. *irq_toggled = 0;
  14416. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14417. goto exit;
  14418. } else {
  14419. *bus_toggled = 1;
  14420. *irq_toggled = 1;
  14421. goto exit;
  14422. }
  14423. }
  14424. if (pt_check_irq_asserted(cd)) {
  14425. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14426. rc = -EIO;
  14427. *irq_toggled = 0;
  14428. goto exit;
  14429. }
  14430. /* Try sending a PIP command to see if we get a response */
  14431. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14432. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14433. if (rc) {
  14434. /*
  14435. * Potential IRQ issue, no communication in App mode, attempt
  14436. * the same command in the BL
  14437. */
  14438. mode = PT_MODE_IGNORE;
  14439. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14440. if (rc) {
  14441. pt_debug(dev, DL_ERROR,
  14442. "%s Failed to enter BL\n", __func__);
  14443. *irq_toggled = 0;
  14444. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14445. goto exit;
  14446. }
  14447. /*
  14448. * If original mode was operational and we successfully
  14449. * entered the BL, this will be useful info for the tp_xres
  14450. * test below.
  14451. */
  14452. if (mode == PT_MODE_OPERATIONAL)
  14453. *xres_toggled = 1;
  14454. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14455. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14456. &actual_read_len);
  14457. if (rc) {
  14458. /*
  14459. * Could not communicate in FW mode or BL mode. Save the
  14460. * error string, slim chance but the XRES test below may
  14461. * show the IRQ is actually working.
  14462. */
  14463. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14464. pt_debug(dev, DL_ERROR,
  14465. "%s: request_active_pip_prot failed\n",
  14466. __func__);
  14467. *irq_toggled = 0;
  14468. goto exit;
  14469. }
  14470. }
  14471. if (cd->irq_count > 0) {
  14472. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14473. cd->irq_count);
  14474. *bus_toggled = 1;
  14475. *irq_toggled = 1;
  14476. goto exit;
  14477. }
  14478. exit:
  14479. kfree(read_buf);
  14480. return rc;
  14481. }
  14482. /*******************************************************************************
  14483. * FUNCTION: pt_bist_xres_test
  14484. *
  14485. * SUMMARY: Tests the connectivity of the TP_XRES net
  14486. *
  14487. * This test will ensure there is a good connection between the host
  14488. * and the DUT on the tp_xres pin. The pin will be toggled to
  14489. * generate a TP reset which will cause the DUT to output a reset
  14490. * sentinel. If the reset sentinel is seen the test passes. If it is
  14491. * not seen the test will attempt to send a soft reset to simply gain
  14492. * some additional information on the failure:
  14493. * - soft reset fails to send: XRES and IRQ likely open
  14494. * - soft reset passes: XRES likely open or stuck de-asserted
  14495. * - soft reset fails: XRES likely stuck asserted
  14496. *
  14497. * RETURN:
  14498. * 0 = Success
  14499. * !0 = Failure
  14500. *
  14501. * PARAMETERS:
  14502. * *dev - pointer to device structure
  14503. * *bus_toggled - pointer to where to store if bus toggled
  14504. * *irq_toggled - pointer to where to store if IRQ toggled
  14505. * *xres_toggled - pointer to where to store if XRES toggled
  14506. * *err_str - pointer to error string buffer
  14507. ******************************************************************************/
  14508. static int pt_bist_xres_test(struct device *dev,
  14509. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14510. {
  14511. struct pt_core_data *cd = dev_get_drvdata(dev);
  14512. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14513. u8 *read_buf = NULL;
  14514. u8 mode = PT_MODE_UNKNOWN;
  14515. int rc = 0;
  14516. int t = 0;
  14517. int timeout = 300;
  14518. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14519. if (read_buf == NULL) {
  14520. rc = -ENOMEM;
  14521. goto exit;
  14522. }
  14523. /* Clear the startup bit mask, reset and enum will re-populate it */
  14524. cd->startup_status = STARTUP_STATUS_START;
  14525. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14526. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14527. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14528. rc = -ENODEV;
  14529. goto exit;
  14530. }
  14531. /* Ensure we have nothing pending on active bus */
  14532. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14533. /* Perform a hard XRES toggle and wait for reset sentinel */
  14534. mutex_lock(&cd->system_lock);
  14535. cd->hid_reset_cmd_state = 1;
  14536. mutex_unlock(&cd->system_lock);
  14537. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14538. rc = pt_hw_hard_reset(cd);
  14539. /* Set timeout to 1s for the flashless case where a BL could be done */
  14540. if (cd->flashless_dut)
  14541. timeout = 1000;
  14542. /*
  14543. * To avoid the case that next PIP command can be confused by BL/FW
  14544. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14545. * followed by "wakeup event" function can lower the failure rate.
  14546. */
  14547. t = wait_event_timeout(cd->wait_q,
  14548. ((cd->startup_status != STARTUP_STATUS_START)
  14549. && (cd->hid_reset_cmd_state == 0)),
  14550. msecs_to_jiffies(timeout));
  14551. if (IS_TMO(t)) {
  14552. pt_debug(cd->dev, DL_ERROR,
  14553. "%s: TMO waiting for sentinel\n", __func__);
  14554. *xres_toggled = 0;
  14555. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14556. /*
  14557. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14558. * To enter the BL we need to generate an XRES so first try to
  14559. * launch the applicaiton
  14560. */
  14561. if (cd->mode == PT_MODE_BOOTLOADER)
  14562. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14563. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14564. __func__);
  14565. mode = PT_MODE_IGNORE;
  14566. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14567. if (rc) {
  14568. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14569. __func__);
  14570. *xres_toggled = 0;
  14571. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14572. goto exit;
  14573. } else {
  14574. /* Wait for the BL sentinel */
  14575. t = wait_event_timeout(cd->wait_q,
  14576. (cd->startup_status != STARTUP_STATUS_START),
  14577. msecs_to_jiffies(500));
  14578. if (IS_TMO(t)) {
  14579. pt_debug(cd->dev, DL_ERROR,
  14580. "%s: TMO waiting for BL sentinel\n",
  14581. __func__);
  14582. *xres_toggled = 0;
  14583. strlcpy(err_str,
  14584. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14585. rc = -ETIME;
  14586. goto exit;
  14587. }
  14588. }
  14589. }
  14590. mutex_lock(&cd->system_lock);
  14591. cd->hid_reset_cmd_state = 0;
  14592. mutex_unlock(&cd->system_lock);
  14593. /* Look for BL or FW reset sentinels */
  14594. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14595. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14596. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14597. /* If a sentinel was seen, all nets are working */
  14598. *xres_toggled = 1;
  14599. *irq_toggled = 1;
  14600. /*
  14601. * For SPI test, bus read result can be confused as FW sentinel
  14602. * if MISO(slave) is connected to MISO(host).
  14603. */
  14604. if (cd->bus_ops->bustype == BUS_I2C)
  14605. *bus_toggled = 1;
  14606. } else {
  14607. /*
  14608. * Hard reset failed, however some additional information
  14609. * could be determined. Try a soft reset to see if DUT resets
  14610. * with the possible outcomes:
  14611. * - if it resets the line is not stuck asserted
  14612. * - if it does not reset the line could be stuck asserted
  14613. */
  14614. *xres_toggled = 0;
  14615. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14616. msleep(30);
  14617. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14618. __func__, rc);
  14619. if (rc) {
  14620. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14621. pt_debug(dev, DL_ERROR,
  14622. "%s: Hard reset failed, soft reset failed %s\n",
  14623. __func__, err_str);
  14624. goto exit;
  14625. }
  14626. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14627. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14628. strlcpy(err_str,
  14629. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14630. pt_debug(dev, DL_ERROR,
  14631. "%s: Hard reset failed, soft reset passed-%s\n",
  14632. __func__, err_str);
  14633. } else if (cd->startup_status == 0) {
  14634. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14635. pt_debug(dev, DL_ERROR,
  14636. "%s: Hard reset failed, soft reset failed-%s\n",
  14637. __func__, err_str);
  14638. } else {
  14639. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14640. pt_debug(dev, DL_ERROR,
  14641. "%s: Hard and Soft reset failed - %s\n",
  14642. __func__, err_str);
  14643. }
  14644. }
  14645. exit:
  14646. kfree(read_buf);
  14647. return rc;
  14648. }
  14649. /*******************************************************************************
  14650. * FUNCTION: pt_bist_slave_irq_test
  14651. *
  14652. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14653. *
  14654. * This test will ensure there is a good IRQ connection between the master
  14655. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14656. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14657. * saw each slave trigger the IRQ with it's reset sentinel.
  14658. *
  14659. * RETURN:
  14660. * 0 = Success
  14661. * !0 = Failure
  14662. *
  14663. * PARAMETERS:
  14664. * *dev - pointer to device structure
  14665. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14666. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14667. * *err_str - pointer to error string buffer
  14668. * *slave_detect - pointer to slave detect buffer
  14669. * *boot_err - pointer to boot_err buffer
  14670. ******************************************************************************/
  14671. static int pt_bist_slave_irq_test(struct device *dev,
  14672. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14673. u8 *slave_detect, u8 *boot_err)
  14674. {
  14675. struct pt_core_data *cd = dev_get_drvdata(dev);
  14676. u8 mode = PT_MODE_UNKNOWN;
  14677. u8 status;
  14678. u8 boot;
  14679. u8 read_buf[12];
  14680. u8 detected = 0;
  14681. u8 last_err = -1;
  14682. u16 actual_read_len;
  14683. int result = 0;
  14684. int rc = 0;
  14685. /*
  14686. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14687. * detect bits. If the DUT was in FW, entering the BL will cause an
  14688. * XRES signal which will inadvertently test the XRES net as well
  14689. */
  14690. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14691. if (rc) {
  14692. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14693. __func__, rc);
  14694. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14695. goto exit;
  14696. }
  14697. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14698. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14699. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14700. &actual_read_len);
  14701. if (!rc) {
  14702. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14703. "PIP2 STATUS");
  14704. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14705. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14706. /* Slave detect is only valid if status ok and in boot exec */
  14707. if (status == PIP2_RSP_ERR_NONE &&
  14708. boot == PIP2_STATUS_BOOT_EXEC) {
  14709. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14710. SLAVE_DETECT_MASK;
  14711. } else {
  14712. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14713. rc = -EPERM;
  14714. }
  14715. } else {
  14716. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14717. __func__);
  14718. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14719. goto exit;
  14720. }
  14721. /*
  14722. * Retrieve boot error regardless of the state of the slave detect
  14723. * bit because the IRQ could have been stuck high or low.
  14724. */
  14725. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14726. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14727. read_buf, &actual_read_len);
  14728. if (!rc) {
  14729. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14730. "PIP2 GET_LAST_ERRNO");
  14731. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14732. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14733. if (last_err) {
  14734. pt_debug(cd->dev, DL_ERROR,
  14735. "%s: Master Boot Last Err = 0x%02X\n",
  14736. __func__, last_err);
  14737. }
  14738. } else {
  14739. pt_debug(cd->dev, DL_ERROR,
  14740. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14741. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14742. }
  14743. exit:
  14744. pt_debug(cd->dev, DL_INFO,
  14745. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14746. __func__, rc, detected, last_err);
  14747. if (err_str && last_err) {
  14748. if (detected)
  14749. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14750. "- Likely stuck low. Boot Error:",
  14751. last_err);
  14752. else
  14753. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14754. "- Likely stuck high. Boot Error:",
  14755. last_err);
  14756. }
  14757. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14758. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14759. last_err = PIP2_RSP_ERR_NONE;
  14760. if (slave_irq_toggled)
  14761. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14762. if (slave_bus_toggled) {
  14763. /* Leave as UNTEST if slave not detected */
  14764. if (detected)
  14765. *slave_bus_toggled = !last_err ? true : false;
  14766. }
  14767. if (slave_detect)
  14768. *slave_detect = detected;
  14769. if (boot_err)
  14770. *boot_err = last_err;
  14771. if (slave_irq_toggled && slave_bus_toggled)
  14772. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14773. __func__,
  14774. "Detected", detected,
  14775. "slave_irq_toggled", *slave_irq_toggled,
  14776. "slave_bus_toggled", *slave_bus_toggled);
  14777. return rc;
  14778. }
  14779. /*******************************************************************************
  14780. * FUNCTION: pt_bist_slave_xres_test
  14781. *
  14782. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14783. *
  14784. * This test will ensure there is a good TP_XRES connection between the
  14785. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14786. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14787. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14788. * it's reset sentinel.
  14789. *
  14790. * RETURN:
  14791. * 0 = Success
  14792. * !0 = Failure
  14793. *
  14794. * PARAMETERS:
  14795. * *dev - pointer to device structure
  14796. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14797. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14798. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14799. * *err_str - pointer to error string buffer
  14800. ******************************************************************************/
  14801. static int pt_bist_slave_xres_test(struct device *dev,
  14802. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14803. u8 *err_str)
  14804. {
  14805. struct pt_core_data *cd = dev_get_drvdata(dev);
  14806. u8 slave_detect = 0;
  14807. u8 boot_err = 0;
  14808. int rc = 0;
  14809. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14810. mutex_lock(&cd->system_lock);
  14811. cd->hid_reset_cmd_state = 1;
  14812. mutex_unlock(&cd->system_lock);
  14813. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14814. pt_hw_hard_reset(cd);
  14815. msleep(100);
  14816. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14817. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14818. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14819. if (!rc && *slave_irq_toggled == false) {
  14820. /*
  14821. * If the slave IRQ did not toggle, either the slave_detect
  14822. * bit was not set or we had a boot error. If the slave
  14823. * detect was not set the slave did not reset causing a boot
  14824. * error.
  14825. */
  14826. if (!slave_detect)
  14827. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14828. else
  14829. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14830. "- likely open or an IRQ issue. Boot Error:",
  14831. boot_err);
  14832. }
  14833. if (slave_xres_toggled) {
  14834. if (!rc)
  14835. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14836. else
  14837. *slave_xres_toggled = false;
  14838. }
  14839. return rc;
  14840. }
  14841. /*******************************************************************************
  14842. * FUNCTION: pt_bist_slave_bus_test
  14843. *
  14844. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14845. *
  14846. * This test will ensure a good SPI bus connection between the
  14847. * master DUT and the slave DUT(s). This bus connection is ensured by
  14848. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14849. * then there is a master/slave communication issue. Opening file 0 on
  14850. * the master will open it on the slave as well if the slave was detected.
  14851. *
  14852. * RETURN:
  14853. * 0 = Success
  14854. * !0 = Failure
  14855. *
  14856. * PARAMETERS:
  14857. * *dev - pointer to device structure
  14858. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14859. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14860. * *err_str - pointer to error string buffer
  14861. ******************************************************************************/
  14862. static int pt_bist_slave_bus_test(struct device *dev,
  14863. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14864. {
  14865. struct pt_core_data *cd = dev_get_drvdata(dev);
  14866. u8 mode = PT_MODE_UNKNOWN;
  14867. u8 bus_toggled = false;
  14868. u8 file_handle;
  14869. int result = 0;
  14870. int rc = 0;
  14871. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14872. if (rc) {
  14873. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14874. __func__, rc);
  14875. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14876. goto exit;
  14877. }
  14878. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14879. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14880. if (file_handle != PIP2_RAM_FILE) {
  14881. rc = -ENOENT;
  14882. bus_toggled = false;
  14883. pt_debug(dev, DL_ERROR,
  14884. "%s Failed to open bin file\n", __func__);
  14885. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14886. goto exit;
  14887. } else {
  14888. bus_toggled = true;
  14889. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14890. pt_debug(dev, DL_ERROR,
  14891. "%s: File Close failed, file_handle=%d\n",
  14892. __func__, file_handle);
  14893. }
  14894. }
  14895. exit:
  14896. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14897. if (slave_irq_toggled)
  14898. *slave_irq_toggled = bus_toggled;
  14899. if (slave_bus_toggled)
  14900. *slave_bus_toggled = bus_toggled;
  14901. return rc;
  14902. }
  14903. /*******************************************************************************
  14904. * FUNCTION: pt_ttdl_bist_show
  14905. *
  14906. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14907. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14908. *
  14909. * NOTE: This function will reset the DUT and the startup_status bit
  14910. * mask. A pt_enum will be queued after completion.
  14911. *
  14912. * NOTE: The order of the net tests is done to optimize the time it takes
  14913. * to run. The first test is capable of verifying all nets, each subsequent
  14914. * test is only run if the previous was not able to see all nets toggle.
  14915. *
  14916. * RETURN:
  14917. * 0 = success
  14918. * !0 = failure
  14919. *
  14920. * PARAMETERS:
  14921. * *dev - pointer to device structure
  14922. * *attr - pointer to device attributes
  14923. * *buf - pointer to output buffer
  14924. ******************************************************************************/
  14925. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14926. struct device_attribute *attr, char *buf)
  14927. {
  14928. struct pt_core_data *cd = dev_get_drvdata(dev);
  14929. ssize_t ret;
  14930. char *bus_err_str = NULL;
  14931. char *irq_err_str = NULL;
  14932. char *xres_err_str = NULL;
  14933. char *slave_bus_err_str = NULL;
  14934. char *slave_irq_err_str = NULL;
  14935. char *slave_xres_err_str = NULL;
  14936. u8 tests;
  14937. int rc = 0;
  14938. int num_tests = 0;
  14939. int status = 1; /* 0 = Pass, !0 = fail */
  14940. u8 bus_toggled = 0x0F; /* default to untested */
  14941. u8 i2c_toggled = 0x0F; /* default to untested */
  14942. u8 spi_toggled = 0x0F; /* default to untested */
  14943. u8 irq_toggled = 0x0F; /* default to untested */
  14944. u8 xres_toggled = 0x0F; /* default to untested */
  14945. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14946. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14947. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14948. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14949. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14950. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14951. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14952. goto print_results;
  14953. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14954. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14955. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14956. if (cd->multi_chip) {
  14957. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14958. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14959. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14960. if (!slave_bus_err_str ||
  14961. !slave_irq_err_str ||
  14962. !slave_xres_err_str)
  14963. goto print_results;
  14964. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14965. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14966. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14967. }
  14968. /* Turn off the TTDL WD during the test */
  14969. pt_stop_wd_timer(cd);
  14970. /* Shorten default PIP cmd timeout while running BIST */
  14971. cd->pip_cmd_timeout = 200;
  14972. /* Count the number of tests to run */
  14973. tests = cd->ttdl_bist_select;
  14974. while (tests) {
  14975. num_tests += tests & 1;
  14976. tests >>= 1;
  14977. }
  14978. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14979. __func__, cd->ttdl_bist_select, num_tests);
  14980. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14981. if (cd->flashless_dut) {
  14982. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14983. __func__);
  14984. mutex_lock(&cd->system_lock);
  14985. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14986. mutex_unlock(&cd->system_lock);
  14987. }
  14988. /* --------------- TP_XRES BIST TEST --------------- */
  14989. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14990. pt_debug(dev, DL_INFO,
  14991. "%s: ----- Start TP_XRES BIST -----", __func__);
  14992. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14993. &xres_toggled, xres_err_str);
  14994. /* Done if the rest of all nets toggled */
  14995. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14996. goto host_nets_complete;
  14997. }
  14998. /* Flush bus in case a PIP response is waiting from previous test */
  14999. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15000. /* --------------- IRQ BIST TEST --------------- */
  15001. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15002. pt_debug(dev, DL_INFO,
  15003. "%s: ----- Start IRQ BIST -----", __func__);
  15004. bus_toggled = 0xFF;
  15005. irq_toggled = 0xFF;
  15006. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15007. &xres_toggled, irq_err_str);
  15008. /* If this net failed clear results from previous net */
  15009. if (irq_toggled != 1) {
  15010. xres_toggled = 0x0F;
  15011. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15012. }
  15013. if (bus_toggled == 1 && irq_toggled == 1)
  15014. goto host_nets_complete;
  15015. }
  15016. /* Flush bus in case a PIP response is waiting from previous test */
  15017. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15018. /* --------------- BUS BIST TEST --------------- */
  15019. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15020. pt_debug(dev, DL_INFO,
  15021. "%s: ----- Start BUS BIST -----", __func__);
  15022. bus_toggled = 0xFF;
  15023. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15024. /* If this net failed clear results from previous net */
  15025. if (bus_toggled == 0) {
  15026. irq_toggled = 0x0F;
  15027. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15028. }
  15029. }
  15030. host_nets_complete:
  15031. /* --------------- SLAVE XRES BIST TEST --------------- */
  15032. if (cd->multi_chip &&
  15033. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15034. pt_debug(dev, DL_INFO,
  15035. "%s: ----- Start Slave XRES BIST -----", __func__);
  15036. slave_xres_toggled = 0xFF;
  15037. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15038. &slave_bus_toggled, &slave_xres_toggled,
  15039. slave_xres_err_str);
  15040. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15041. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15042. goto print_results;
  15043. }
  15044. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15045. if (cd->multi_chip &&
  15046. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15047. pt_debug(dev, DL_INFO,
  15048. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15049. slave_irq_toggled = 0xFF;
  15050. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15051. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15052. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15053. __func__, slave_irq_toggled);
  15054. if (slave_irq_toggled == 1) {
  15055. slave_bus_toggled = 1;
  15056. goto print_results;
  15057. }
  15058. }
  15059. /* --------------- SLAVE BUS BIST TEST --------------- */
  15060. if (cd->multi_chip &&
  15061. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15062. pt_debug(dev, DL_INFO,
  15063. "%s: ----- Start Slave BUS BIST -----", __func__);
  15064. slave_bus_toggled = 0xFF;
  15065. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15066. &slave_bus_toggled, slave_bus_err_str);
  15067. }
  15068. print_results:
  15069. /* Restore PIP command timeout */
  15070. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15071. /*
  15072. * We're done! - Perform a hard XRES toggle, allowing BL
  15073. * to load FW if there is any in Flash
  15074. */
  15075. mutex_lock(&cd->system_lock);
  15076. cd->hid_reset_cmd_state = 0;
  15077. mutex_unlock(&cd->system_lock);
  15078. pt_debug(dev, DL_INFO,
  15079. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15080. if (cd->flashless_dut) {
  15081. /*
  15082. * For Flashless solution, FW update is triggered after BL is
  15083. * seen that several miliseconds delay is needed.
  15084. */
  15085. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15086. __func__);
  15087. mutex_lock(&cd->system_lock);
  15088. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15089. mutex_unlock(&cd->system_lock);
  15090. /* Reset DUT and wait 100ms to see if loader started */
  15091. pt_hw_hard_reset(cd);
  15092. msleep(100);
  15093. if (cd->fw_updating) {
  15094. pt_debug(dev, DL_INFO,
  15095. "%s: ----- BIST Wait FW Loading ----",
  15096. __func__);
  15097. rc = _pt_request_wait_for_enum_state(
  15098. dev, 4000, STARTUP_STATUS_COMPLETE);
  15099. }
  15100. } else {
  15101. if (cd->mode == PT_MODE_BOOTLOADER) {
  15102. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15103. if (rc) {
  15104. pt_debug(dev, DL_ERROR,
  15105. "%s Failed to launch app\n", __func__);
  15106. rc = pt_hw_hard_reset(cd);
  15107. }
  15108. }
  15109. /*
  15110. * If FW exists the BL may have just started or will start soon,
  15111. * so the FW sentinel may be on it's way but with no FW it will
  15112. * not arrive, wait for it before deciding if we need to queue
  15113. * an enum.
  15114. */
  15115. rc = _pt_request_wait_for_enum_state(
  15116. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15117. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15118. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15119. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15120. __func__);
  15121. pt_queue_enum(cd);
  15122. rc = _pt_request_wait_for_enum_state(
  15123. dev, 2000, STARTUP_STATUS_COMPLETE);
  15124. }
  15125. }
  15126. msleep(20);
  15127. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15128. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15129. pt_start_wd_timer(cd);
  15130. /* Canned print if any memory allocation issues */
  15131. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15132. ret = scnprintf(buf, strlen(buf),
  15133. "Status: %d\n"
  15134. "I2C (SDA,SCL): [UNTEST]\n"
  15135. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15136. "IRQ connection: [UNTEST]\n"
  15137. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15138. if (cd->multi_chip) {
  15139. ret += scnprintf(buf + ret, strlen(buf),
  15140. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15141. "I/P IRQ connection: [UNTEST]\n"
  15142. "I/P TP_XRES connection: [UNTEST]\n");
  15143. }
  15144. } else {
  15145. status = 0;
  15146. if (bus_toggled == 1)
  15147. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15148. if (irq_toggled == 1)
  15149. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15150. if (xres_toggled == 1)
  15151. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15152. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15153. status += bus_toggled;
  15154. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15155. status += irq_toggled;
  15156. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15157. status += xres_toggled;
  15158. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15159. __func__, status, bus_toggled, irq_toggled,
  15160. xres_toggled);
  15161. if (cd->multi_chip) {
  15162. if (slave_irq_toggled == 1)
  15163. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15164. if (slave_xres_toggled == 1)
  15165. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15166. if (slave_bus_toggled == 1)
  15167. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15168. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15169. status += slave_bus_toggled;
  15170. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15171. status += slave_irq_toggled;
  15172. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15173. status += slave_xres_toggled;
  15174. pt_debug(dev, DL_ERROR,
  15175. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15176. __func__, status, bus_toggled, irq_toggled,
  15177. xres_toggled, slave_bus_toggled,
  15178. slave_irq_toggled, slave_xres_toggled);
  15179. }
  15180. if (cd->bus_ops->bustype == BUS_I2C)
  15181. i2c_toggled = bus_toggled;
  15182. else
  15183. spi_toggled = bus_toggled;
  15184. ret = scnprintf(buf, strlen(buf),
  15185. "Status: %d\n"
  15186. "I2C (SDA,SCL): %s %s\n"
  15187. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15188. "IRQ connection: %s %s\n"
  15189. "TP_XRES connection: %s %s\n",
  15190. status == num_tests ? 0 : 1,
  15191. i2c_toggled == 0x0F ? "[UNTEST]" :
  15192. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15193. i2c_toggled == 0x0F ? "" : bus_err_str,
  15194. spi_toggled == 0x0F ? "[UNTEST]" :
  15195. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15196. spi_toggled == 0x0F ? "" : bus_err_str,
  15197. irq_toggled == 0x0F ? "[UNTEST]" :
  15198. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15199. irq_err_str,
  15200. xres_toggled == 0x0F ? "[UNTEST]" :
  15201. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15202. xres_err_str);
  15203. if (cd->multi_chip) {
  15204. ret += scnprintf(buf + ret, strlen(buf),
  15205. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15206. "I/P IRQ connection: %s %s\n"
  15207. "I/P TP_XRES connection: %s %s\n",
  15208. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15209. slave_bus_toggled == 1 ? "[ OK ]" :
  15210. "[FAILED]", slave_bus_err_str,
  15211. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15212. slave_irq_toggled == 1 ? "[ OK ]" :
  15213. "[FAILED]", slave_irq_err_str,
  15214. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15215. slave_xres_toggled == 1 ? "[ OK ]" :
  15216. "[FAILED]", slave_xres_err_str);
  15217. }
  15218. }
  15219. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15220. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15221. __func__, cd->startup_status);
  15222. kfree(bus_err_str);
  15223. kfree(irq_err_str);
  15224. kfree(xres_err_str);
  15225. return ret;
  15226. }
  15227. /*******************************************************************************
  15228. * FUNCTION: pt_ttdl_bist_store
  15229. *
  15230. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15231. *
  15232. * RETURN: Size of passed in buffer if successful
  15233. *
  15234. * PARAMETERS:
  15235. * *dev - pointer to device structure
  15236. * *attr - pointer to device attributes
  15237. * *buf - pointer to command buffer
  15238. * size - size of buf
  15239. ******************************************************************************/
  15240. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15241. struct device_attribute *attr, const char *buf, size_t size)
  15242. {
  15243. struct pt_core_data *cd = dev_get_drvdata(dev);
  15244. u32 input_data[2] = {0};
  15245. int length;
  15246. int rc = 0;
  15247. /* Maximum input of one value */
  15248. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15249. ARRAY_SIZE(input_data));
  15250. if (length != 1) {
  15251. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15252. __func__);
  15253. rc = -EINVAL;
  15254. goto exit;
  15255. } else {
  15256. mutex_lock(&cd->system_lock);
  15257. cd->ttdl_bist_select = input_data[0];
  15258. mutex_unlock(&cd->system_lock);
  15259. }
  15260. exit:
  15261. if (rc)
  15262. return rc;
  15263. return size;
  15264. }
  15265. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15266. pt_ttdl_bist_store);
  15267. /*******************************************************************************
  15268. * FUNCTION: pt_flush_bus_store
  15269. *
  15270. * SUMMARY: Store method for the flush_bus sysfs node.
  15271. *
  15272. * RETURN: Size of passed in buffer if successful
  15273. *
  15274. * PARAMETERS:
  15275. * *dev - pointer to device structure
  15276. * *attr - pointer to device attributes
  15277. * *buf - pointer to command buffer
  15278. * size - size of buf
  15279. ******************************************************************************/
  15280. static ssize_t pt_flush_bus_store(struct device *dev,
  15281. struct device_attribute *attr, const char *buf, size_t size)
  15282. {
  15283. struct pt_core_data *cd = dev_get_drvdata(dev);
  15284. u32 input_data[2] = {0};
  15285. int length;
  15286. int rc = 0;
  15287. /* Maximum input of one value */
  15288. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15289. ARRAY_SIZE(input_data));
  15290. if (length != 1) {
  15291. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15292. __func__);
  15293. rc = -EINVAL;
  15294. goto exit;
  15295. }
  15296. mutex_lock(&cd->system_lock);
  15297. if (input_data[0] == 0)
  15298. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15299. else if (input_data[0] == 1)
  15300. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15301. else
  15302. rc = -EINVAL;
  15303. mutex_unlock(&cd->system_lock);
  15304. exit:
  15305. if (rc)
  15306. return rc;
  15307. return size;
  15308. }
  15309. /*******************************************************************************
  15310. * FUNCTION: pt_flush_bus_show
  15311. *
  15312. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15313. * based on either the size of the first two bytes or a blind 256 bytes.
  15314. *
  15315. * RETURN:
  15316. * 0 = success
  15317. * !0 = failure
  15318. *
  15319. * PARAMETERS:
  15320. * *dev - pointer to device structure
  15321. * *attr - pointer to device attributes
  15322. * *buf - pointer to output buffer
  15323. ******************************************************************************/
  15324. static ssize_t pt_flush_bus_show(struct device *dev,
  15325. struct device_attribute *attr, char *buf)
  15326. {
  15327. ssize_t ret = 0;
  15328. ssize_t bytes = 0;
  15329. struct pt_core_data *cd = dev_get_drvdata(dev);
  15330. mutex_lock(&cd->system_lock);
  15331. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15332. ret = scnprintf(buf, strlen(buf),
  15333. "Status: 0\n"
  15334. "%s: %zd\n",
  15335. "Bytes flushed", bytes);
  15336. mutex_unlock(&cd->system_lock);
  15337. return ret;
  15338. }
  15339. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15340. pt_flush_bus_store);
  15341. /*******************************************************************************
  15342. * FUNCTION: pt_pip2_ping_test_store
  15343. *
  15344. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15345. *
  15346. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15347. * someone may want to test sending invalid packet lengths so any values
  15348. * up to 255 are allowed.
  15349. *
  15350. * RETURN: Size of passed in buffer if successful
  15351. *
  15352. * PARAMETERS:
  15353. * *dev - pointer to device structure
  15354. * *attr - pointer to device attributes
  15355. * *buf - pointer to command buffer
  15356. * size - size of buf
  15357. ******************************************************************************/
  15358. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15359. struct device_attribute *attr, const char *buf, size_t size)
  15360. {
  15361. struct pt_core_data *cd = dev_get_drvdata(dev);
  15362. u32 input_data[2];
  15363. int length;
  15364. int rc = 0;
  15365. /* Maximum input of one value */
  15366. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15367. ARRAY_SIZE(input_data));
  15368. if (length != 1) {
  15369. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15370. __func__);
  15371. rc = -EINVAL;
  15372. goto exit;
  15373. }
  15374. mutex_lock(&cd->system_lock);
  15375. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15376. cd->ping_test_size = input_data[0];
  15377. else
  15378. rc = -EINVAL;
  15379. mutex_unlock(&cd->system_lock);
  15380. exit:
  15381. if (rc)
  15382. return rc;
  15383. return size;
  15384. }
  15385. /*******************************************************************************
  15386. * FUNCTION: pt_ping_test_show
  15387. *
  15388. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15389. * PING command and ramps up the optional payload from 0 to
  15390. * ping_test_size.
  15391. * The max payload size is 247:
  15392. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15393. *
  15394. * RETURN:
  15395. * 0 = success
  15396. * !0 = failure
  15397. *
  15398. * PARAMETERS:
  15399. * *dev - pointer to device structure
  15400. * *attr - pointer to device attributes
  15401. * *buf - pointer to output buffer
  15402. ******************************************************************************/
  15403. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15404. struct device_attribute *attr, char *buf)
  15405. {
  15406. struct pt_core_data *cd = dev_get_drvdata(dev);
  15407. ssize_t ret;
  15408. int last_packet_size;
  15409. int rc = 0;
  15410. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15411. if (rc) {
  15412. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15413. return ret;
  15414. }
  15415. ret = scnprintf(buf, strlen(buf),
  15416. "Status: %d\n"
  15417. "PING payload test passed with packet sizes 0 - %d\n",
  15418. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15419. last_packet_size);
  15420. return ret;
  15421. }
  15422. /*******************************************************************************
  15423. * FUNCTION: pt_t_refresh_store
  15424. *
  15425. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15426. * in integer as the number of interrupts to count. A timer is started to
  15427. * calculate the total time it takes to see that number of interrupts.
  15428. *
  15429. * RETURN: Size of passed in buffer if successful
  15430. *
  15431. * PARAMETERS:
  15432. * *dev - pointer to device structure
  15433. * *attr - pointer to device attributes
  15434. * *buf - pointer to buffer that hold the command parameters
  15435. * size - size of buf
  15436. ******************************************************************************/
  15437. static ssize_t pt_t_refresh_store(struct device *dev,
  15438. struct device_attribute *attr, const char *buf, size_t size)
  15439. {
  15440. struct pt_core_data *cd = dev_get_drvdata(dev);
  15441. u32 input_data[2];
  15442. int length;
  15443. int rc = 0;
  15444. /* Maximum input of one value */
  15445. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15446. ARRAY_SIZE(input_data));
  15447. if (length != 1) {
  15448. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15449. __func__);
  15450. rc = -EINVAL;
  15451. goto exit;
  15452. }
  15453. mutex_lock(&cd->system_lock);
  15454. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15455. input_data[0]);
  15456. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15457. cd->t_refresh_total = input_data[0];
  15458. cd->t_refresh_count = 0;
  15459. cd->t_refresh_active = 1;
  15460. } else {
  15461. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15462. rc = -EINVAL;
  15463. }
  15464. mutex_unlock(&cd->system_lock);
  15465. exit:
  15466. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15467. if (rc)
  15468. return rc;
  15469. return size;
  15470. }
  15471. /*******************************************************************************
  15472. * FUNCTION: pt_t_refresh_show
  15473. *
  15474. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15475. * of the T-Refresh timer counting the time it takes to see a user defined
  15476. * number of interrupts.
  15477. *
  15478. * RETURN:
  15479. * 0 = success
  15480. * !0 = failure
  15481. *
  15482. * PARAMETERS:
  15483. * *dev - pointer to device structure
  15484. * *attr - pointer to device attributes
  15485. * *buf - pointer to output buffer
  15486. ******************************************************************************/
  15487. static ssize_t pt_t_refresh_show(struct device *dev,
  15488. struct device_attribute *attr, char *buf)
  15489. {
  15490. ssize_t ret = 0;
  15491. struct pt_core_data *cd = dev_get_drvdata(dev);
  15492. u32 whole;
  15493. u16 fraction;
  15494. mutex_lock(&cd->system_lock);
  15495. /* Check if we have counted the number requested */
  15496. if (cd->t_refresh_count != cd->t_refresh_total) {
  15497. ret = scnprintf(buf, strlen(buf),
  15498. "Status: 0\n"
  15499. "%s: %d\n",
  15500. "Still counting... current IRQ count",
  15501. cd->t_refresh_count);
  15502. } else {
  15503. /* Ensure T-Refresh is de-activated */
  15504. cd->t_refresh_active = 0;
  15505. whole = cd->t_refresh_time / cd->t_refresh_count;
  15506. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15507. fraction = fraction * 1000 / cd->t_refresh_count;
  15508. ret = scnprintf(buf, strlen(buf),
  15509. "Status: 0\n"
  15510. "%s: %d\n"
  15511. "%s: %d\n"
  15512. "%s: %d\n"
  15513. "%s: %d.%02d\n",
  15514. "Requested IRQ Count ", cd->t_refresh_total,
  15515. "IRQ Counted ", cd->t_refresh_count,
  15516. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15517. "Average T-Refresh (ms) ", whole, fraction);
  15518. }
  15519. mutex_unlock(&cd->system_lock);
  15520. return ret;
  15521. }
  15522. /*******************************************************************************
  15523. * FUNCTION: pt_dut_status_show
  15524. *
  15525. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15526. * more items such as operation mode,easywake state are added in the future.
  15527. *
  15528. * RETURN: Char buffer with printed scan status information
  15529. *
  15530. * PARAMETERS:
  15531. * *dev - pointer to device structure
  15532. * *attr - pointer to device attributes
  15533. * *buf - pointer to output buffer
  15534. ******************************************************************************/
  15535. static ssize_t pt_dut_status_show(struct device *dev,
  15536. struct device_attribute *attr, char *buf)
  15537. {
  15538. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15539. u8 mode = PT_MODE_UNKNOWN;
  15540. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15541. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15542. struct pt_core_data *cd = dev_get_drvdata(dev);
  15543. ssize_t ret;
  15544. u16 calculated_crc = 0;
  15545. u16 stored_crc = 0;
  15546. u8 status;
  15547. int rc = 0;
  15548. /* In STANDBY the DUT will not repond to any PIP cmd */
  15549. if (cd->fw_sys_mode_in_standby_state) {
  15550. mode = PT_MODE_OPERATIONAL;
  15551. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15552. goto print_limited_results;
  15553. }
  15554. /* Retrieve mode and FW system mode which can only be 0-4 */
  15555. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15556. if (rc || mode == PT_MODE_UNKNOWN) {
  15557. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15558. "%s: %d\n"
  15559. "%s: n/a\n"
  15560. "%s: n/a\n"
  15561. "%s: n/a\n"
  15562. "%s: n/a\n",
  15563. "Status", rc,
  15564. "Active Exec ",
  15565. "FW System Mode ",
  15566. "Stored CRC ",
  15567. "Calculated CRC ");
  15568. return ret;
  15569. } else {
  15570. if (mode == PT_MODE_OPERATIONAL) {
  15571. if (sys_mode > FW_SYS_MODE_MAX)
  15572. sys_mode = FW_SYS_MODE_UNDEFINED;
  15573. if (sys_mode != FW_SYS_MODE_TEST)
  15574. goto print_limited_results;
  15575. rc = pt_pip_verify_config_block_crc_(cd,
  15576. PT_TCH_PARM_EBID, &status,
  15577. &calculated_crc, &stored_crc);
  15578. if (rc)
  15579. goto print_limited_results;
  15580. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15581. "%s: %d\n"
  15582. "%s: %s\n"
  15583. "%s: %s\n"
  15584. "%s: 0x%04X\n"
  15585. "%s: 0x%04X\n",
  15586. "Status", rc,
  15587. "Active Exec ", "FW",
  15588. "FW System Mode ", outputstring[sys_mode],
  15589. "Stored CRC ", stored_crc,
  15590. "Calculated CRC ", calculated_crc);
  15591. return ret;
  15592. } else {
  15593. /* When in BL or unknon mode Active Exec is "n/a" */
  15594. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15595. }
  15596. }
  15597. print_limited_results:
  15598. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15599. "%s: %d\n"
  15600. "%s: %s\n"
  15601. "%s: %s\n"
  15602. "%s: n/a\n"
  15603. "%s: n/a\n",
  15604. "Status", rc,
  15605. "Active Exec ",
  15606. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15607. "FW System Mode ", outputstring[sys_mode],
  15608. "Stored CRC ",
  15609. "Calculated CRC ");
  15610. return ret;
  15611. }
  15612. #endif /* TTDL_DIAGNOSTICS */
  15613. /*******************************************************************************
  15614. * Structures of sysfs attributes for all DUT dependent sysfs node
  15615. ******************************************************************************/
  15616. static struct attribute *early_attrs[] = {
  15617. &dev_attr_hw_version.attr,
  15618. &dev_attr_drv_version.attr,
  15619. &dev_attr_drv_ver.attr,
  15620. &dev_attr_fw_version.attr,
  15621. &dev_attr_sysinfo.attr,
  15622. &dev_attr_pip2_cmd_rsp.attr,
  15623. &dev_attr_command.attr,
  15624. &dev_attr_drv_debug.attr,
  15625. &dev_attr_hw_reset.attr,
  15626. &dev_attr_response.attr,
  15627. &dev_attr_ttdl_restart.attr,
  15628. #ifdef TTDL_DIAGNOSTICS
  15629. &dev_attr_ttdl_status.attr,
  15630. &dev_attr_pip2_enter_bl.attr,
  15631. &dev_attr_pip2_exit_bl.attr,
  15632. &dev_attr_err_gpio.attr,
  15633. &dev_attr_flush_bus.attr,
  15634. &dev_attr_ttdl_bist.attr,
  15635. #endif /* TTDL_DIAGNOSTICS */
  15636. NULL,
  15637. };
  15638. static struct attribute_group early_attr_group = {
  15639. .attrs = early_attrs,
  15640. };
  15641. static struct device_attribute pip2_attributes[] = {
  15642. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15643. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15644. #ifdef TTDL_DIAGNOSTICS
  15645. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15646. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15647. pt_pip2_ping_test_store),
  15648. #endif
  15649. };
  15650. static struct device_attribute attributes[] = {
  15651. __ATTR(dut_debug, 0644,
  15652. pt_dut_debug_show, pt_drv_debug_store),
  15653. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15654. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15655. __ATTR(get_param, 0644,
  15656. pt_get_param_show, pt_get_param_store),
  15657. #ifdef EASYWAKE_TSG6
  15658. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15659. pt_easy_wakeup_gesture_store),
  15660. __ATTR(easy_wakeup_gesture_id, 0444,
  15661. pt_easy_wakeup_gesture_id_show, NULL),
  15662. __ATTR(easy_wakeup_gesture_data, 0444,
  15663. pt_easy_wakeup_gesture_data_show, NULL),
  15664. #endif
  15665. #ifdef TTDL_DIAGNOSTICS
  15666. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15667. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15668. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15669. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15670. #endif /* TTDL_DIAGNOSTICS */
  15671. };
  15672. /*******************************************************************************
  15673. * FUNCTION: add_sysfs_interfaces
  15674. *
  15675. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15676. *
  15677. * RETURN:
  15678. * 0 = success
  15679. * !0 = failure
  15680. *
  15681. * PARAMETERS:
  15682. * *dev - pointer to device structure
  15683. ******************************************************************************/
  15684. static int add_sysfs_interfaces(struct device *dev)
  15685. {
  15686. int i;
  15687. int j = 0;
  15688. struct pt_core_data *cd = dev_get_drvdata(dev);
  15689. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15690. if (device_create_file(dev, attributes + i))
  15691. goto undo;
  15692. }
  15693. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15694. __func__, cd->active_dut_generation);
  15695. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15696. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15697. if (device_create_file(dev, pip2_attributes + j))
  15698. goto undo;
  15699. }
  15700. }
  15701. return 0;
  15702. undo:
  15703. for (i--; i >= 0; i--)
  15704. device_remove_file(dev, attributes + i);
  15705. for (j--; j >= 0; j--)
  15706. device_remove_file(dev, pip2_attributes + j);
  15707. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15708. __func__);
  15709. return -ENODEV;
  15710. }
  15711. /*******************************************************************************
  15712. * FUNCTION: remove_sysfs_interfaces
  15713. *
  15714. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15715. *
  15716. * RETURN: void
  15717. *
  15718. * PARAMETERS:
  15719. * *dev - pointer to device structure
  15720. ******************************************************************************/
  15721. static void remove_sysfs_interfaces(struct device *dev)
  15722. {
  15723. int i;
  15724. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15725. device_remove_file(dev, attributes + i);
  15726. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15727. device_remove_file(dev, pip2_attributes + i);
  15728. }
  15729. /*******************************************************************************
  15730. * FUNCTION: remove_sysfs_and_modules
  15731. *
  15732. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15733. *
  15734. * RETURN: void
  15735. *
  15736. * PARAMETERS:
  15737. * *dev - pointer to device structure
  15738. ******************************************************************************/
  15739. static void remove_sysfs_and_modules(struct device *dev)
  15740. {
  15741. struct pt_core_data *cd = dev_get_drvdata(dev);
  15742. /* Queued work should be removed before to release loader module */
  15743. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15744. pt_release_modules(cd);
  15745. pt_btn_release(dev);
  15746. pt_mt_release(dev);
  15747. remove_sysfs_interfaces(dev);
  15748. }
  15749. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15750. {
  15751. int retval;
  15752. /* Get pinctrl if target uses pinctrl */
  15753. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15754. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15755. retval = PTR_ERR(cd->ts_pinctrl);
  15756. dev_dbg(cd->dev,
  15757. "Target does not use pinctrl %d\n", retval);
  15758. goto err_pinctrl_get;
  15759. }
  15760. cd->pinctrl_state_active
  15761. = pinctrl_lookup_state(cd->ts_pinctrl,
  15762. PINCTRL_STATE_ACTIVE);
  15763. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15764. retval = PTR_ERR(cd->pinctrl_state_active);
  15765. dev_err(cd->dev,
  15766. "Can not lookup %s pinstate %d\n",
  15767. PINCTRL_STATE_ACTIVE, retval);
  15768. goto err_pinctrl_lookup;
  15769. }
  15770. cd->pinctrl_state_suspend
  15771. = pinctrl_lookup_state(cd->ts_pinctrl,
  15772. PINCTRL_STATE_SUSPEND);
  15773. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15774. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15775. dev_err(cd->dev,
  15776. "Can not lookup %s pinstate %d\n",
  15777. PINCTRL_STATE_SUSPEND, retval);
  15778. goto err_pinctrl_lookup;
  15779. }
  15780. cd->pinctrl_state_release
  15781. = pinctrl_lookup_state(cd->ts_pinctrl,
  15782. PINCTRL_STATE_RELEASE);
  15783. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15784. retval = PTR_ERR(cd->pinctrl_state_release);
  15785. dev_dbg(cd->dev,
  15786. "Can not lookup %s pinstate %d\n",
  15787. PINCTRL_STATE_RELEASE, retval);
  15788. }
  15789. return 0;
  15790. err_pinctrl_lookup:
  15791. devm_pinctrl_put(cd->ts_pinctrl);
  15792. err_pinctrl_get:
  15793. cd->ts_pinctrl = NULL;
  15794. return retval;
  15795. }
  15796. /*******************************************************************************
  15797. *******************************************************************************
  15798. * FUNCTION: pt_probe
  15799. *
  15800. * SUMMARY: Probe of the core module.
  15801. *
  15802. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15803. * probe functionality is split into two functions; pt_probe() and
  15804. * pt_probe_complete(). the initial setup is done in this function which
  15805. * then creates a WORK task which runs after the probe timer expires. This
  15806. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15807. *
  15808. * RETURN:
  15809. * 0 = success
  15810. * !0 = failure
  15811. *
  15812. * PARAMETERS:
  15813. * *ops - pointer to the bus
  15814. * *dev - pointer to the device structure
  15815. * irq - IRQ
  15816. * xfer_buf_size - size of the buffer
  15817. ******************************************************************************/
  15818. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15819. u16 irq, size_t xfer_buf_size)
  15820. {
  15821. struct pt_core_data *cd;
  15822. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15823. enum pt_atten_type type;
  15824. struct i2c_client *client = to_i2c_client(dev);
  15825. int rc = 0;
  15826. u8 pip_ver_major;
  15827. u8 pip_ver_minor;
  15828. u32 status = STARTUP_STATUS_START;
  15829. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15830. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15831. __func__);
  15832. rc = -ENODEV;
  15833. goto error_no_pdata;
  15834. }
  15835. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15836. if (!pdata->core_pdata->power) {
  15837. pt_debug(dev, DL_ERROR,
  15838. "%s: Missing platform data function\n",
  15839. __func__);
  15840. rc = -ENODEV;
  15841. goto error_no_pdata;
  15842. }
  15843. }
  15844. /* get context and debug print buffers */
  15845. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15846. if (!cd) {
  15847. rc = -ENOMEM;
  15848. goto error_no_pdata;
  15849. }
  15850. /* Initialize device info */
  15851. cd->dev = dev;
  15852. cd->pdata = pdata;
  15853. cd->cpdata = pdata->core_pdata;
  15854. cd->bus_ops = ops;
  15855. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15856. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15857. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15858. cd->hw_detected = false;
  15859. cd->pip2_prot_active = false;
  15860. cd->pip2_send_user_cmd = false;
  15861. cd->bl_pip_ver_ready = false;
  15862. cd->app_pip_ver_ready = false;
  15863. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15864. cd->get_param_id = 0;
  15865. cd->watchdog_enabled = 0;
  15866. cd->startup_retry_count = 0;
  15867. cd->core_probe_complete = 0;
  15868. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15869. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15870. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15871. cd->flashless_dut = 0;
  15872. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15873. cd->bl_with_no_int = 0;
  15874. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15875. cd->multi_chip = PT_FEATURE_DISABLE;
  15876. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15877. cd->sleep_state = SS_SLEEP_NONE;
  15878. cd->quick_boot = false;
  15879. cd->drv_debug_suspend = false;
  15880. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15881. cd->set_dut_generation = true;
  15882. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15883. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15884. cd->set_dut_generation = true;
  15885. cd->active_dut_generation = DUT_PIP1_ONLY;
  15886. } else {
  15887. cd->set_dut_generation = false;
  15888. cd->active_dut_generation = DUT_UNKNOWN;
  15889. }
  15890. /* Initialize with platform data */
  15891. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15892. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15893. cd->hid_cmd_state = 1;
  15894. cd->fw_updating = false;
  15895. cd->multi_chip = 0;
  15896. #ifdef TTDL_DIAGNOSTICS
  15897. cd->t_refresh_active = 0;
  15898. cd->t_refresh_count = 0;
  15899. cd->pip2_crc_error_count = 0;
  15900. cd->wd_xres_count = 0;
  15901. cd->bl_retry_packet_count = 0;
  15902. cd->file_erase_timeout_count = 0;
  15903. cd->show_tt_data = false;
  15904. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15905. cd->err_gpio = 0;
  15906. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15907. cd->ttdl_bist_select = 0x07;
  15908. cd->force_pip2_seq = 0;
  15909. #endif /* TTDL_DIAGNOSTICS */
  15910. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15911. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15912. sizeof(PT_PIP2_BIN_FILE_PATH));
  15913. pt_init_hid_descriptor(&cd->hid_desc);
  15914. /* Read and store the descriptor lengths */
  15915. cd->hid_core.hid_report_desc_len =
  15916. le16_to_cpu(cd->hid_desc.report_desc_len);
  15917. cd->hid_core.hid_max_input_len =
  15918. le16_to_cpu(cd->hid_desc.max_input_len);
  15919. cd->hid_core.hid_max_output_len =
  15920. le16_to_cpu(cd->hid_desc.max_output_len);
  15921. /* Initialize mutexes and spinlocks */
  15922. mutex_init(&cd->module_list_lock);
  15923. mutex_init(&cd->system_lock);
  15924. mutex_init(&cd->sysfs_lock);
  15925. mutex_init(&cd->ttdl_restart_lock);
  15926. mutex_init(&cd->firmware_class_lock);
  15927. spin_lock_init(&cd->spinlock);
  15928. /* Initialize module list */
  15929. INIT_LIST_HEAD(&cd->module_list);
  15930. /* Initialize attention lists */
  15931. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15932. INIT_LIST_HEAD(&cd->atten_list[type]);
  15933. /* Initialize parameter list */
  15934. INIT_LIST_HEAD(&cd->param_list);
  15935. /* Initialize wait queue */
  15936. init_waitqueue_head(&cd->wait_q);
  15937. rc = pt_ts_pinctrl_init(cd);
  15938. if (!rc && cd->ts_pinctrl) {
  15939. /*
  15940. * Pinctrl handle is optional. If pinctrl handle is found
  15941. * let pins to be configured in active state. If not
  15942. * found continue further without error.
  15943. */
  15944. rc = pinctrl_select_state(cd->ts_pinctrl,
  15945. cd->pinctrl_state_active);
  15946. if (rc < 0)
  15947. dev_err(&client->dev, "failed to select pin to active state\n");
  15948. }
  15949. rc = pt_get_regulator(cd, true);
  15950. if (rc) {
  15951. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15952. goto error_alloc_data;
  15953. }
  15954. rc = pt_enable_regulator(cd, true);
  15955. if (rc) {
  15956. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15957. goto error_get_regulator;
  15958. }
  15959. /* Initialize works */
  15960. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15961. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15962. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15963. /* Initialize HID specific data */
  15964. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15965. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15966. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15967. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15968. cd->hid_core.hid_desc_register =
  15969. cpu_to_le16(cd->cpdata->hid_desc_register);
  15970. /* Set platform easywake value */
  15971. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15972. #ifdef CONFIG_DRM
  15973. /* Setup active dsi panel */
  15974. active_panel = cd->cpdata->active_panel;
  15975. #endif
  15976. /* Set platform panel_id value */
  15977. cd->panel_id_support = cd->cpdata->panel_id_support;
  15978. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15979. /* Set Panel ID to default to 0 */
  15980. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15981. else
  15982. /* Set Panel ID to Not Enabled */
  15983. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15984. /* Initialize hw_version default to FFFF.FFFF.FF */
  15985. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15986. dev_set_drvdata(dev, cd);
  15987. /* PtSBC builds will call this function in pt_probe_complete() */
  15988. pt_add_core(dev);
  15989. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15990. if (rc) {
  15991. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15992. __func__);
  15993. goto error_enable_regulator;
  15994. }
  15995. /*
  15996. * Save the pointer to a global value, which will be used
  15997. * in ttdl_restart function
  15998. */
  15999. cd->bus_ops = ops;
  16000. /*
  16001. * When the IRQ GPIO is not direclty accessible and no function is
  16002. * defined to get the IRQ status, the IRQ passed in must be assigned
  16003. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16004. */
  16005. if (!cd->cpdata->irq_stat) {
  16006. cd->irq = irq;
  16007. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16008. __func__, cd->irq);
  16009. }
  16010. /* Call platform init function before setting up the GPIO's */
  16011. if (cd->cpdata->init) {
  16012. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16013. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16014. } else {
  16015. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16016. __func__);
  16017. rc = 0;
  16018. }
  16019. if (rc < 0) {
  16020. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16021. __func__, rc);
  16022. }
  16023. /* Power on any needed regulator(s) */
  16024. if (cd->cpdata->setup_power) {
  16025. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16026. rc = cd->cpdata->setup_power(cd->cpdata,
  16027. PT_MT_POWER_ON, cd->dev);
  16028. } else {
  16029. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16030. __func__);
  16031. rc = 0;
  16032. }
  16033. if (rc < 0)
  16034. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16035. __func__, rc);
  16036. #ifdef TTDL_DIAGNOSTICS
  16037. cd->watchdog_irq_stuck_count = 0;
  16038. cd->bus_transmit_error_count = 0;
  16039. #endif /* TTDL_DIAGNOSTICS */
  16040. if (cd->cpdata->detect) {
  16041. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16042. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16043. pt_platform_detect_read);
  16044. if (!rc) {
  16045. cd->hw_detected = true;
  16046. pt_debug(cd->dev, DL_INFO,
  16047. "%s: HW detected\n", __func__);
  16048. } else {
  16049. cd->hw_detected = false;
  16050. pt_debug(cd->dev, DL_INFO,
  16051. "%s: No HW detected\n", __func__);
  16052. rc = -ENODEV;
  16053. goto error_detect;
  16054. }
  16055. } else {
  16056. pt_debug(dev, DL_ERROR,
  16057. "%s: PARADE No HW detect function pointer\n",
  16058. __func__);
  16059. /*
  16060. * "hw_reset" is not needed in the "if" statement,
  16061. * because "hw_reset" is already included in "hw_detect"
  16062. * function.
  16063. */
  16064. rc = pt_hw_hard_reset(cd);
  16065. if (rc)
  16066. pt_debug(cd->dev, DL_ERROR,
  16067. "%s: FAILED to execute HARD reset\n",
  16068. __func__);
  16069. }
  16070. if (cd->cpdata->setup_irq) {
  16071. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16072. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16073. if (rc) {
  16074. pt_debug(dev, DL_ERROR,
  16075. "%s: Error, couldn't setup IRQ\n", __func__);
  16076. goto error_setup_irq;
  16077. }
  16078. } else {
  16079. pt_debug(dev, DL_ERROR,
  16080. "%s: IRQ function pointer not setup\n",
  16081. __func__);
  16082. goto error_setup_irq;
  16083. }
  16084. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16085. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16086. (unsigned long)cd);
  16087. #else
  16088. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16089. #endif
  16090. pt_stop_wd_timer(cd);
  16091. #ifdef TTHE_TUNER_SUPPORT
  16092. mutex_init(&cd->tthe_lock);
  16093. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16094. 0644, NULL, cd, &tthe_debugfs_fops);
  16095. #endif
  16096. rc = device_init_wakeup(dev, 1);
  16097. if (rc < 0)
  16098. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16099. __func__, rc);
  16100. if (!enable_irq_wake(cd->irq)) {
  16101. cd->irq_wake = 1;
  16102. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16103. }
  16104. pm_runtime_get_noresume(dev);
  16105. pm_runtime_set_active(dev);
  16106. pm_runtime_enable(dev);
  16107. /* If IRQ asserted, read out all from buffer to release INT pin */
  16108. if (cd->cpdata->irq_stat) {
  16109. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16110. } else {
  16111. /* Force a read in case the reset sentinel already arrived */
  16112. rc = pt_read_input(cd);
  16113. if (!rc)
  16114. pt_parse_input(cd);
  16115. }
  16116. /* Without sleep DUT is not ready and will NAK the first write */
  16117. msleep(150);
  16118. /* Attempt to set the DUT generation if not yet set */
  16119. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16120. if (cd->bl_pip_ver_ready ||
  16121. (cd->app_pip_ver_ready &&
  16122. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16123. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16124. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16125. __func__, cd->active_dut_generation);
  16126. } else {
  16127. rc = _pt_detect_dut_generation(cd->dev,
  16128. &status, &cd->active_dut_generation,
  16129. &cd->mode);
  16130. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16131. || rc) {
  16132. pt_debug(cd->dev, DL_ERROR,
  16133. " === DUT Gen unknown, Skip Enum ===\n");
  16134. goto skip_enum;
  16135. }
  16136. }
  16137. }
  16138. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16139. &pip_ver_major, &pip_ver_minor);
  16140. if (pip_ver_major == 2) {
  16141. cd->bl_pip_ver_ready = true;
  16142. pt_debug(dev, DL_ERROR,
  16143. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16144. pip_ver_minor);
  16145. cd->hw_detected = true;
  16146. } else if (pip_ver_major == 1) {
  16147. cd->app_pip_ver_ready = true;
  16148. pt_debug(dev, DL_ERROR,
  16149. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16150. cd->hw_detected = true;
  16151. } else {
  16152. cd->sysinfo.ttdata.pip_ver_major = 0;
  16153. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16154. cd->app_pip_ver_ready = false;
  16155. cd->hw_detected = false;
  16156. pt_debug(dev, DL_ERROR,
  16157. " === PIP Version Not Detected, Skip Enum ===\n");
  16158. /* For legacy DUTS proceed, enum will attempt to launch app */
  16159. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16160. goto skip_enum;
  16161. }
  16162. rc = pt_enum_with_dut(cd, false, &status);
  16163. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16164. __func__, cd->startup_status, status);
  16165. if (rc == -ENODEV) {
  16166. pt_debug(cd->dev, DL_ERROR,
  16167. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16168. /* For PtSBC don't error out, allow TTDL to stay up */
  16169. rc = -EPROBE_DEFER;
  16170. goto error_after_startup;
  16171. }
  16172. /* Suspend scanning until probe is complete to avoid asyc touches */
  16173. pt_pip_suspend_scanning_(cd);
  16174. if (cd->hw_detected) {
  16175. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16176. __func__);
  16177. rc = add_sysfs_interfaces(dev);
  16178. if (rc < 0) {
  16179. pt_debug(dev, DL_ERROR,
  16180. "%s: Error, fail sysfs init\n", __func__);
  16181. goto error_after_startup;
  16182. }
  16183. } else {
  16184. pt_debug(dev, DL_ERROR,
  16185. "%s: No HW detected, sysfs interfaces not added\n",
  16186. __func__);
  16187. }
  16188. skip_enum:
  16189. pm_runtime_put_sync(dev);
  16190. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16191. rc = pt_mt_probe(dev);
  16192. if (rc < 0) {
  16193. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16194. __func__);
  16195. goto error_after_sysfs_create;
  16196. }
  16197. rc = pt_btn_probe(dev);
  16198. if (rc < 0) {
  16199. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16200. __func__);
  16201. goto error_after_startup_mt;
  16202. }
  16203. pt_probe_modules(cd);
  16204. #ifdef CONFIG_HAS_EARLYSUSPEND
  16205. pt_setup_early_suspend(cd);
  16206. #elif defined(CONFIG_DRM)
  16207. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16208. pt_setup_drm_notifier(cd);
  16209. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16210. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16211. #elif defined(CONFIG_FB)
  16212. pt_setup_fb_notifier(cd);
  16213. #endif
  16214. #ifdef NEED_SUSPEND_NOTIFIER
  16215. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16216. register_pm_notifier(&cd->pm_notifier);
  16217. #endif
  16218. pt_pip_resume_scanning_(cd);
  16219. mutex_lock(&cd->system_lock);
  16220. cd->startup_status |= status;
  16221. cd->core_probe_complete = 1;
  16222. mutex_unlock(&cd->system_lock);
  16223. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16224. __func__);
  16225. return 0;
  16226. error_after_startup_mt:
  16227. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16228. pt_mt_release(dev);
  16229. error_after_sysfs_create:
  16230. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16231. pm_runtime_disable(dev);
  16232. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16233. device_wakeup_disable(dev);
  16234. #endif
  16235. device_init_wakeup(dev, 0);
  16236. cancel_work_sync(&cd->enum_work);
  16237. pt_stop_wd_timer(cd);
  16238. pt_free_si_ptrs(cd);
  16239. remove_sysfs_interfaces(dev);
  16240. error_after_startup:
  16241. pr_err("%s PARADE error_after_startup\n", __func__);
  16242. del_timer(&cd->watchdog_timer);
  16243. if (cd->cpdata->setup_irq)
  16244. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16245. error_setup_irq:
  16246. error_detect:
  16247. if (cd->cpdata->init)
  16248. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16249. if (cd->cpdata->setup_power)
  16250. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16251. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16252. error_enable_regulator:
  16253. pt_del_core(dev);
  16254. dev_set_drvdata(dev, NULL);
  16255. pt_enable_regulator(cd, false);
  16256. error_get_regulator:
  16257. pt_get_regulator(cd, false);
  16258. error_alloc_data:
  16259. kfree(cd);
  16260. error_no_pdata:
  16261. pr_err("%s failed.\n", __func__);
  16262. return rc;
  16263. }
  16264. EXPORT_SYMBOL_GPL(pt_probe);
  16265. /*******************************************************************************
  16266. * FUNCTION: pt_release
  16267. *
  16268. * SUMMARY: This function does the following cleanup:
  16269. * - Releases all probed modules
  16270. * - Stops the watchdog
  16271. * - Cancels all pending work tasks
  16272. * - Removes all created sysfs nodes
  16273. * - Removes all created debugfs nodes
  16274. * - Frees the IRQ
  16275. * - Deletes the core
  16276. * - Frees all pointers and HID reports
  16277. *
  16278. * RETURN:
  16279. * 0 = success
  16280. * !0 = failure
  16281. *
  16282. * PARAMETERS:
  16283. * *cd - pointer to the core data structure
  16284. ******************************************************************************/
  16285. int pt_release(struct pt_core_data *cd)
  16286. {
  16287. struct device *dev = cd->dev;
  16288. /*
  16289. * Suspend the device before freeing the startup_work and stopping
  16290. * the watchdog since sleep function restarts watchdog on failure
  16291. */
  16292. pm_runtime_suspend(dev);
  16293. pm_runtime_disable(dev);
  16294. /*
  16295. * Any 'work' that can trigger a new thread should be canceled first.
  16296. * The watchdog is also stopped again because another thread could have
  16297. * restarted it. The irq_work is cancelled last because it is used for
  16298. * all I2C/SPI communication.
  16299. */
  16300. pt_stop_wd_timer(cd);
  16301. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16302. cancel_work_sync(&cd->ttdl_restart_work);
  16303. cancel_work_sync(&cd->enum_work);
  16304. cancel_work_sync(&cd->resume_offload_work);
  16305. cancel_work_sync(&cd->suspend_offload_work);
  16306. cancel_work_sync(&cd->resume_work);
  16307. cancel_work_sync(&cd->suspend_work);
  16308. destroy_workqueue(cd->pt_workqueue);
  16309. pt_stop_wd_timer(cd);
  16310. pt_release_modules(cd);
  16311. pt_proximity_release(dev);
  16312. pt_btn_release(dev);
  16313. pt_mt_release(dev);
  16314. #ifdef CONFIG_HAS_EARLYSUSPEND
  16315. unregister_early_suspend(&cd->es);
  16316. #elif defined(CONFIG_DRM)
  16317. if (active_panel)
  16318. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16319. #elif defined(CONFIG_FB)
  16320. fb_unregister_client(&cd->fb_notifier);
  16321. #endif
  16322. #ifdef NEED_SUSPEND_NOTIFIER
  16323. unregister_pm_notifier(&cd->pm_notifier);
  16324. #endif
  16325. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16326. device_wakeup_disable(dev);
  16327. #endif
  16328. device_init_wakeup(dev, 0);
  16329. #ifdef TTHE_TUNER_SUPPORT
  16330. mutex_lock(&cd->tthe_lock);
  16331. cd->tthe_exit = 1;
  16332. wake_up(&cd->wait_q);
  16333. mutex_unlock(&cd->tthe_lock);
  16334. debugfs_remove(cd->tthe_debugfs);
  16335. #endif
  16336. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16337. remove_sysfs_interfaces(dev);
  16338. disable_irq_nosync(cd->irq);
  16339. if (cd->cpdata->setup_irq)
  16340. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16341. if (cd->cpdata->init)
  16342. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16343. if (cd->cpdata->setup_power)
  16344. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16345. dev_set_drvdata(dev, NULL);
  16346. pt_del_core(dev);
  16347. if (cd->vcc_i2c)
  16348. regulator_set_load(cd->vcc_i2c, 0);
  16349. if (cd->vdd)
  16350. regulator_set_load(cd->vdd, 0);
  16351. pt_enable_regulator(cd, false);
  16352. pt_get_regulator(cd, false);
  16353. pt_free_si_ptrs(cd);
  16354. kfree(cd);
  16355. return 0;
  16356. }
  16357. EXPORT_SYMBOL_GPL(pt_release);
  16358. MODULE_LICENSE("GPL");
  16359. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16360. MODULE_AUTHOR("Parade Technologies <[email protected]>");