pt_core.c 517 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002
  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. cmd = kzalloc(cmd_length, GFP_KERNEL);
  507. if (!cmd)
  508. return -ENOMEM;
  509. /* Set Command register */
  510. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  511. sizeof(cd->hid_desc.command_register));
  512. cmd_offset += sizeof(cd->hid_desc.command_register);
  513. /* Set Command */
  514. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  515. if (hid_cmd->report_id >= 0XF)
  516. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  517. else
  518. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  519. cmd_offset++;
  520. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  521. cmd_offset++;
  522. if (hid_cmd->report_id >= 0XF) {
  523. cmd[cmd_offset] = hid_cmd->report_id;
  524. cmd_offset++;
  525. }
  526. /* Set Data register */
  527. if (hid_cmd->has_data_register) {
  528. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  529. sizeof(cd->hid_desc.data_register));
  530. cmd_offset += sizeof(cd->hid_desc.data_register);
  531. }
  532. /* Set Data */
  533. if (hid_cmd->write_length && hid_cmd->write_buf) {
  534. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  535. hid_cmd->write_length);
  536. cmd_offset += hid_cmd->write_length;
  537. }
  538. pt_debug(cd->dev, DL_INFO,
  539. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  540. __func__, cmd_length, hid_cmd->report_id);
  541. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  542. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  543. hid_cmd->read_buf);
  544. if (rc)
  545. pt_debug(cd->dev, DL_ERROR,
  546. "%s: Fail pt_adap_transfer\n", __func__);
  547. kfree(cmd);
  548. return rc;
  549. }
  550. #ifdef TTDL_DIAGNOSTICS
  551. /*******************************************************************************
  552. * FUNCTION: pt_toggle_err_gpio
  553. *
  554. * SUMMARY: Toggles the pre-defined error GPIO
  555. *
  556. * RETURN: n/a
  557. *
  558. * PARAMETERS:
  559. * *cd - pointer to core data
  560. * type - type of err that occurred
  561. ******************************************************************************/
  562. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  563. {
  564. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  565. __func__, type);
  566. if (cd->err_gpio && type == cd->err_gpio_type) {
  567. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  568. gpio_direction_output(cd->err_gpio,
  569. !gpio_get_value(cd->err_gpio));
  570. }
  571. }
  572. /*******************************************************************************
  573. * FUNCTION: _pt_request_toggle_err_gpio
  574. *
  575. * SUMMARY: Function pointer included in core_cmds to allow other modules
  576. * to request to toggle the err_gpio
  577. *
  578. * RETURN: n/a
  579. *
  580. * PARAMETERS:
  581. * *cd - pointer to core data
  582. * type - type of err that occurred
  583. ******************************************************************************/
  584. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  585. {
  586. struct pt_core_data *cd = dev_get_drvdata(dev);
  587. pt_toggle_err_gpio(cd, type);
  588. }
  589. #endif /* TTDL_DIAGNOSTICS */
  590. /*******************************************************************************
  591. * FUNCTION: pt_hid_exec_cmd_and_wait_
  592. *
  593. * SUMMARY: Send the HID command to the DUT and wait for the response
  594. *
  595. * RETURN:
  596. * 0 = success
  597. * !0 = failure
  598. *
  599. * PARAMETERS:
  600. * *cd - pointer to core data
  601. * *hid_cmd - pointer to the HID command to send
  602. ******************************************************************************/
  603. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  604. struct pt_hid_cmd *hid_cmd)
  605. {
  606. int rc = 0;
  607. int t;
  608. u16 timeout_ms;
  609. int *cmd_state;
  610. if (hid_cmd->reset_cmd)
  611. cmd_state = &cd->hid_reset_cmd_state;
  612. else
  613. cmd_state = &cd->hid_cmd_state;
  614. if (hid_cmd->wait_interrupt) {
  615. mutex_lock(&cd->system_lock);
  616. *cmd_state = 1;
  617. mutex_unlock(&cd->system_lock);
  618. }
  619. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  620. if (rc) {
  621. if (hid_cmd->wait_interrupt)
  622. goto error;
  623. goto exit;
  624. }
  625. if (!hid_cmd->wait_interrupt)
  626. goto exit;
  627. if (hid_cmd->timeout_ms)
  628. timeout_ms = hid_cmd->timeout_ms;
  629. else
  630. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  631. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  632. msecs_to_jiffies(timeout_ms));
  633. if (IS_TMO(t)) {
  634. #ifdef TTDL_DIAGNOSTICS
  635. cd->bus_transmit_error_count++;
  636. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  637. #endif /* TTDL_DIAGNOSTICS */
  638. pt_debug(cd->dev, DL_ERROR,
  639. "%s: HID output cmd execution timed out\n",
  640. __func__);
  641. rc = -ETIME;
  642. goto error;
  643. }
  644. goto exit;
  645. error:
  646. mutex_lock(&cd->system_lock);
  647. *cmd_state = 0;
  648. mutex_unlock(&cd->system_lock);
  649. exit:
  650. return rc;
  651. }
  652. /*******************************************************************************
  653. * FUNCTION: pt_hid_cmd_reset_
  654. *
  655. * SUMMARY: Send the HID RESET command to the DUT
  656. *
  657. * RETURN:
  658. * 0 = success
  659. * !0 = failure
  660. *
  661. * PARAMETERS:
  662. * *cd - pointer to core data
  663. ******************************************************************************/
  664. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  665. {
  666. struct pt_hid_cmd hid_cmd = {
  667. .opcode = HID_CMD_RESET,
  668. .wait_interrupt = 1,
  669. .reset_cmd = 1,
  670. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  671. };
  672. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  673. }
  674. /*******************************************************************************
  675. * FUNCTION: pt_hid_cmd_reset
  676. *
  677. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  678. * access.
  679. *
  680. * RETURN:
  681. * 0 = success
  682. * !0 = failure
  683. *
  684. * PARAMETERS:
  685. * *cd - pointer to core data
  686. ******************************************************************************/
  687. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  688. {
  689. int rc = 0;
  690. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  691. if (rc < 0) {
  692. pt_debug(cd->dev, DL_ERROR,
  693. "%s: fail get exclusive ex=%p own=%p\n",
  694. __func__, cd->exclusive_dev, cd->dev);
  695. return rc;
  696. }
  697. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  698. rc = pt_hid_cmd_reset_(cd);
  699. if (release_exclusive(cd, cd->dev) < 0)
  700. pt_debug(cd->dev, DL_ERROR,
  701. "%s: fail to release exclusive\n", __func__);
  702. return rc;
  703. }
  704. /*******************************************************************************
  705. * FUNCTION: pt_hid_cmd_set_power_
  706. *
  707. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  708. *
  709. * RETURN:
  710. * 0 = success
  711. * !0 = failure
  712. *
  713. * PARAMETERS:
  714. * *cd - pointer to core data
  715. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  716. ******************************************************************************/
  717. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  718. u8 power_state)
  719. {
  720. int rc = 0;
  721. struct pt_hid_cmd hid_cmd = {
  722. .opcode = HID_CMD_SET_POWER,
  723. .wait_interrupt = 1,
  724. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  725. };
  726. hid_cmd.power_state = power_state;
  727. /* The chip won't give response if goes to Deep Standby */
  728. if (power_state == HID_POWER_STANDBY) {
  729. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  730. if (rc)
  731. pt_debug(cd->dev, DL_ERROR,
  732. "%s: Failed to set power to state:%d\n",
  733. __func__, power_state);
  734. else
  735. cd->fw_sys_mode_in_standby_state = true;
  736. return rc;
  737. }
  738. cd->fw_sys_mode_in_standby_state = false;
  739. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  740. if (rc) {
  741. pt_debug(cd->dev, DL_ERROR,
  742. "%s: Failed to set power to state:%d\n",
  743. __func__, power_state);
  744. return rc;
  745. }
  746. /* validate */
  747. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  748. || ((cd->response_buf[3] & 0x3) != power_state)
  749. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  750. rc = -EINVAL;
  751. return rc;
  752. }
  753. /*******************************************************************************
  754. * FUNCTION: pt_hid_cmd_set_power
  755. *
  756. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  757. * exclusive access.
  758. *
  759. * RETURN:
  760. * 0 = success
  761. * !0 = failure
  762. *
  763. * PARAMETERS:
  764. * *cd - pointer to core data
  765. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  766. ******************************************************************************/
  767. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  768. u8 power_state)
  769. {
  770. int rc;
  771. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  772. if (rc < 0) {
  773. pt_debug(cd->dev, DL_ERROR,
  774. "%s: fail get exclusive ex=%p own=%p\n",
  775. __func__, cd->exclusive_dev, cd->dev);
  776. return rc;
  777. }
  778. rc = pt_hid_cmd_set_power_(cd, power_state);
  779. if (release_exclusive(cd, cd->dev) < 0)
  780. pt_debug(cd->dev, DL_ERROR,
  781. "%s: fail to release exclusive\n", __func__);
  782. return rc;
  783. }
  784. static const u16 crc_table[16] = {
  785. 0x0000, 0x1021, 0x2042, 0x3063,
  786. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  787. 0x8108, 0x9129, 0xa14a, 0xb16b,
  788. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  789. };
  790. /*******************************************************************************
  791. * FUNCTION: _pt_compute_crc
  792. *
  793. * SUMMARY: Calculate CRC by CRC table.
  794. *
  795. * RETURN:
  796. * CRC calculation result
  797. *
  798. * PARAMETERS:
  799. * *buf - pointer to the data array to be calculated
  800. * size - size of data array
  801. ******************************************************************************/
  802. static u16 _pt_compute_crc(u8 *buf, u32 size)
  803. {
  804. u16 remainder = 0xFFFF;
  805. u16 xor_mask = 0x0000;
  806. u32 index;
  807. u32 byte_value;
  808. u32 table_index;
  809. u32 crc_bit_width = sizeof(u16) * 8;
  810. /* Divide the message by polynomial, via the table. */
  811. for (index = 0; index < size; index++) {
  812. byte_value = buf[index];
  813. table_index = ((byte_value >> 4) & 0x0F)
  814. ^ (remainder >> (crc_bit_width - 4));
  815. remainder = crc_table[table_index] ^ (remainder << 4);
  816. table_index = (byte_value & 0x0F)
  817. ^ (remainder >> (crc_bit_width - 4));
  818. remainder = crc_table[table_index] ^ (remainder << 4);
  819. }
  820. /* Perform the final remainder CRC. */
  821. return remainder ^ xor_mask;
  822. }
  823. u16 ccitt_Table[] = {
  824. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  825. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  826. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  827. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  828. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  829. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  830. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  831. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  832. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  833. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  834. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  835. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  836. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  837. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  838. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  839. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  840. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  841. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  842. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  843. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  844. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  845. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  846. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  847. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  848. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  849. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  850. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  851. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  852. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  853. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  854. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  855. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  856. };
  857. /*******************************************************************************
  858. * FUNCTION: crc_ccitt_calculate
  859. *
  860. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  861. *
  862. * RETURN:
  863. * CRC calculation result
  864. *
  865. * PARAMETERS:
  866. * *q - pointer to the data array to be calculated
  867. * len - size of data array
  868. ******************************************************************************/
  869. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  870. {
  871. unsigned short crc = 0xffff;
  872. while (len-- > 0)
  873. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  874. return crc;
  875. }
  876. /*******************************************************************************
  877. * FUNCTION: pt_pip2_cmd_calculate_crc
  878. *
  879. * SUMMARY: Calculate the CRC of a command packet
  880. *
  881. * RETURN: void
  882. *
  883. * PARAMETERS:
  884. * *cmd - pointer to command data
  885. * extra_bytes - Extra bytes included in command length
  886. ******************************************************************************/
  887. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  888. u8 extra_bytes)
  889. {
  890. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  891. unsigned short crc;
  892. buf[0] = cmd->len & 0xff;
  893. buf[1] = (cmd->len & 0xff00) >> 8;
  894. buf[2] = cmd->seq;
  895. buf[3] = cmd->id;
  896. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  897. /* Calculate the CRC for the first 4 bytes above and the data payload */
  898. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  899. cmd->crc[0] = (crc & 0xff00) >> 8;
  900. cmd->crc[1] = (crc & 0xff);
  901. }
  902. /*******************************************************************************
  903. * FUNCTION: pt_pip2_get_next_cmd_seq
  904. *
  905. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  906. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  907. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  908. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  909. * increment, just use the forced value.
  910. *
  911. * RETURN: Next command sequence number [0x08-0x0F]
  912. *
  913. * PARAMETERS:
  914. * *cd - pointer to core data
  915. ******************************************************************************/
  916. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  917. {
  918. #ifdef TTDL_DIAGNOSTICS
  919. if (cd->force_pip2_seq <= 0x07) {
  920. cd->pip2_cmd_tag_seq++;
  921. if (cd->pip2_cmd_tag_seq > 0x0F)
  922. cd->pip2_cmd_tag_seq = 0x08;
  923. } else {
  924. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  925. }
  926. #else
  927. cd->pip2_cmd_tag_seq++;
  928. if (cd->pip2_cmd_tag_seq > 0x0F)
  929. cd->pip2_cmd_tag_seq = 0x08;
  930. #endif
  931. return cd->pip2_cmd_tag_seq;
  932. }
  933. /*
  934. * Following macros are to define the response time (the interval between PIP2
  935. * command finishes sending and INT pin falls). The unit is in microsecond.
  936. * It has different time settings between the solution GPIO polling and Bus
  937. * polling due to the considration for system load.
  938. */
  939. #ifdef PT_POLL_RESP_BY_BUS
  940. #define POLL_RETRY_DEFAULT_INTERVAL 50
  941. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  942. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  943. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  944. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  945. #else
  946. #define POLL_RETRY_DEFAULT_INTERVAL 20
  947. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  948. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  949. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  950. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  951. #endif
  952. /*
  953. * id: the command id defined in PIP2
  954. * response_len: the (maximum) length of response.
  955. * response_time_min: minimum response time in microsecond
  956. * response_time_max: maximum response time in microsecond
  957. */
  958. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  959. {.id = PIP2_CMD_ID_PING,
  960. .response_len = 255,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_STATUS,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  967. {.id = PIP2_CMD_ID_CTRL,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  971. {.id = PIP2_CMD_ID_CONFIG,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_CLEAR,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_RESET,
  980. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  981. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  982. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  983. {.id = PIP2_CMD_ID_VERSION,
  984. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_OPEN,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  989. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_CLOSE,
  992. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FILE_READ,
  996. .response_len = 255,
  997. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_FILE_WRITE,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1001. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_FLASH_INFO,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_EXECUTE,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1020. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1021. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1022. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1023. {.id = PIP2_CMD_ID_READ_GPIO,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_EXECUTE_SCAN,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_SET_PARAMETER,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_GET_PARAMETER,
  1036. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1039. {.id = PIP2_CMD_SET_DDI_REG,
  1040. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1041. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1042. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1043. {.id = PIP2_CMD_GET_DDI_REG,
  1044. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1045. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1046. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1047. {.id = PIP2_CMD_ID_END,
  1048. .response_len = 255,
  1049. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1050. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1051. };
  1052. /*******************************************************************************
  1053. * FUNCTION: pt_pip2_get_cmd_response_len
  1054. *
  1055. * SUMMARY: Gets the expected response length based on the command ID
  1056. *
  1057. * RETURN: Expected response length
  1058. *
  1059. * PARAMETERS:
  1060. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1061. ******************************************************************************/
  1062. static int pt_pip2_get_cmd_response_len(u8 id)
  1063. {
  1064. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1065. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1066. p++;
  1067. if (p->id != PIP2_CMD_ID_END)
  1068. return p->response_len;
  1069. else
  1070. return -EPERM;
  1071. }
  1072. /*******************************************************************************
  1073. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1074. *
  1075. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1076. * finishes sending and INT pin falls) based on the command ID
  1077. *
  1078. * RETURN: Estimated minimum response time in microsecond
  1079. *
  1080. * PARAMETERS:
  1081. * id - Command ID
  1082. ******************************************************************************/
  1083. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1084. {
  1085. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1086. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1087. p++;
  1088. if (p->id != PIP2_CMD_ID_END)
  1089. return p->response_time_min;
  1090. else
  1091. return PIP2_RESP_DEFAULT_TIME_MIN;
  1092. }
  1093. /*******************************************************************************
  1094. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1095. *
  1096. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1097. * finishes sending and INT pin falls) based on the command ID
  1098. *
  1099. * RETURN: Estimated maximum response time in microsecond
  1100. *
  1101. * PARAMETERS:
  1102. * id - Command ID
  1103. ******************************************************************************/
  1104. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1105. {
  1106. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1107. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1108. p++;
  1109. if (p->id != PIP2_CMD_ID_END)
  1110. return p->response_time_max;
  1111. else
  1112. return PIP2_RESP_DEFAULT_TIME_MAX;
  1113. }
  1114. /*******************************************************************************
  1115. * FUNCTION: pt_pip2_validate_response
  1116. *
  1117. * SUMMARY: Validate the response of PIP2 command.
  1118. *
  1119. * RETURN:
  1120. * 0 = success
  1121. * !0 = failure
  1122. *
  1123. * PARAMETERS:
  1124. * *cd - pointer to core data
  1125. * *pip2_cmd - pointer to PIP2 command to send
  1126. * *read_buf - pointer to response buffer
  1127. * actual_read_len - actual read length of the response
  1128. ******************************************************************************/
  1129. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1130. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1131. u16 actual_read_len)
  1132. {
  1133. int rc = 0;
  1134. u8 response_seq = 0;
  1135. u8 reserved_bits = 0;
  1136. u8 cmd_id = 0;
  1137. u8 response_bit = 0;
  1138. unsigned short calc_crc = 0;
  1139. unsigned short resp_crc = 0;
  1140. /* Verify the length of response buffer */
  1141. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1142. pt_debug(cd->dev, DL_ERROR,
  1143. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1144. __func__, pip2_cmd->id, actual_read_len);
  1145. rc = -EINVAL;
  1146. goto exit;
  1147. }
  1148. /* Verify the CRC */
  1149. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1150. resp_crc = read_buf[actual_read_len - 2] << 8;
  1151. resp_crc |= read_buf[actual_read_len - 1];
  1152. if (resp_crc != calc_crc) {
  1153. pt_debug(cd->dev, DL_ERROR,
  1154. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1155. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1156. #ifdef TTDL_DIAGNOSTICS
  1157. cd->pip2_crc_error_count++;
  1158. #endif /* TTDL_DIAGNOSTICS */
  1159. rc = -EINVAL;
  1160. goto exit;
  1161. }
  1162. /* Verify the response bit is set */
  1163. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1164. if (!response_bit) {
  1165. pt_debug(cd->dev, DL_ERROR,
  1166. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1167. __func__, pip2_cmd->id, response_bit);
  1168. rc = -EINVAL;
  1169. goto exit;
  1170. }
  1171. /* Verify the command ID matches from command to response */
  1172. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1173. if (cmd_id != pip2_cmd->id) {
  1174. pt_debug(cd->dev, DL_ERROR,
  1175. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1176. __func__, pip2_cmd->id, cmd_id);
  1177. rc = -EINVAL;
  1178. goto exit;
  1179. }
  1180. /* Verify the SEQ number matches from command to response */
  1181. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1182. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1183. pt_debug(cd->dev, DL_ERROR,
  1184. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1185. __func__, pip2_cmd->id,
  1186. pip2_cmd->seq, response_seq);
  1187. rc = -EINVAL;
  1188. goto exit;
  1189. }
  1190. /* Verify the reserved bits are 0 */
  1191. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1192. if (reserved_bits)
  1193. pt_debug(cd->dev, DL_WARN,
  1194. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1195. __func__, pip2_cmd->id, reserved_bits);
  1196. exit:
  1197. if (rc)
  1198. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1199. "PIP RSP:");
  1200. return rc;
  1201. }
  1202. /*******************************************************************************
  1203. * FUNCTION: pt_hid_output_validate_bl_response
  1204. *
  1205. * SUMMARY: Validate the response of bootloader command.
  1206. *
  1207. * RETURN:
  1208. * 0 = success
  1209. * !0 = failure
  1210. *
  1211. * PARAMETERS:
  1212. * *cd - pointer to core data
  1213. * *hid_output - pointer to hid output data structure
  1214. ******************************************************************************/
  1215. static int pt_hid_output_validate_bl_response(
  1216. struct pt_core_data *cd,
  1217. struct pt_hid_output *hid_output)
  1218. {
  1219. u16 size;
  1220. u16 crc;
  1221. u8 status;
  1222. size = get_unaligned_le16(&cd->response_buf[0]);
  1223. if (hid_output->reset_expected && !size)
  1224. return 0;
  1225. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1226. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1227. pt_debug(cd->dev, DL_ERROR,
  1228. "%s: BL output response, wrong report_id\n", __func__);
  1229. return -EPROTO;
  1230. }
  1231. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong SOP\n", __func__);
  1234. return -EPROTO;
  1235. }
  1236. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1237. pt_debug(cd->dev, DL_ERROR,
  1238. "%s: BL output response, wrong EOP\n", __func__);
  1239. return -EPROTO;
  1240. }
  1241. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1242. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1243. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1244. pt_debug(cd->dev, DL_ERROR,
  1245. "%s: BL output response, wrong CRC 0x%X\n",
  1246. __func__, crc);
  1247. return -EPROTO;
  1248. }
  1249. status = cd->response_buf[5];
  1250. if (status) {
  1251. pt_debug(cd->dev, DL_ERROR,
  1252. "%s: BL output response, ERROR:%d\n",
  1253. __func__, status);
  1254. return -EPROTO;
  1255. }
  1256. return 0;
  1257. }
  1258. /*******************************************************************************
  1259. * FUNCTION: pt_hid_output_validate_app_response
  1260. *
  1261. * SUMMARY: Validate the response of application command.
  1262. *
  1263. * RETURN:
  1264. * 0 = success
  1265. * !0 = failure
  1266. *
  1267. * PARAMETERS:
  1268. * *cd - pointer to core data
  1269. * *hid_output - pointer to hid output data structure
  1270. ******************************************************************************/
  1271. static int pt_hid_output_validate_app_response(
  1272. struct pt_core_data *cd,
  1273. struct pt_hid_output *hid_output)
  1274. {
  1275. int command_code;
  1276. u16 size;
  1277. size = get_unaligned_le16(&cd->response_buf[0]);
  1278. if (hid_output->reset_expected && !size)
  1279. return 0;
  1280. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1281. != PT_PIP_NON_HID_RESPONSE_ID) {
  1282. pt_debug(cd->dev, DL_ERROR,
  1283. "%s: APP output response, wrong report_id\n", __func__);
  1284. return -EPROTO;
  1285. }
  1286. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1287. & PIP1_RESP_COMMAND_ID_MASK;
  1288. if (command_code != hid_output->command_code) {
  1289. pt_debug(cd->dev, DL_ERROR,
  1290. "%s: APP output response, wrong command_code:%X\n",
  1291. __func__, command_code);
  1292. return -EPROTO;
  1293. }
  1294. return 0;
  1295. }
  1296. /*******************************************************************************
  1297. * FUNCTION: pt_check_set_parameter
  1298. *
  1299. * SUMMARY: Check command input and response for Set Parameter command.And
  1300. * store the parameter to the list for resume work if pass the check.
  1301. *
  1302. * PARAMETERS:
  1303. * *cd - pointer to core data
  1304. * *hid_output - pointer to hid output data structure
  1305. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1306. ******************************************************************************/
  1307. static void pt_check_set_parameter(struct pt_core_data *cd,
  1308. struct pt_hid_output *hid_output, bool raw)
  1309. {
  1310. u8 *param_buf;
  1311. u32 param_value = 0;
  1312. u8 param_size;
  1313. u8 param_id;
  1314. int i = 0;
  1315. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1316. return;
  1317. /* Check command input for Set Parameter command */
  1318. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1319. && !memcmp(&hid_output->write_buf[0],
  1320. &cd->hid_desc.output_register,
  1321. sizeof(cd->hid_desc.output_register))
  1322. && hid_output->write_buf[4] ==
  1323. PT_PIP_NON_HID_COMMAND_ID
  1324. && hid_output->write_buf[6] ==
  1325. PIP1_CMD_ID_SET_PARAM)
  1326. param_buf = &hid_output->write_buf[7];
  1327. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1328. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1329. && hid_output->write_length >= 3
  1330. && hid_output->write_length <= 6)
  1331. param_buf = &hid_output->write_buf[0];
  1332. else
  1333. return;
  1334. /* Get parameter ID, size and value */
  1335. param_id = param_buf[0];
  1336. param_size = param_buf[1];
  1337. if (param_size > 4) {
  1338. pt_debug(cd->dev, DL_ERROR,
  1339. "%s: Invalid parameter size\n", __func__);
  1340. return;
  1341. }
  1342. param_buf = &param_buf[2];
  1343. while (i < param_size)
  1344. param_value += *(param_buf++) << (8 * i++);
  1345. /* Check command response for Set Parameter command */
  1346. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1347. || (cd->response_buf[4] &
  1348. PIP1_RESP_COMMAND_ID_MASK) !=
  1349. PIP1_CMD_ID_SET_PARAM
  1350. || cd->response_buf[5] != param_id
  1351. || cd->response_buf[6] != param_size) {
  1352. pt_debug(cd->dev, DL_ERROR,
  1353. "%s: Set Parameter command not successful\n",
  1354. __func__);
  1355. return;
  1356. }
  1357. pt_add_parameter(cd, param_id, param_value, param_size);
  1358. }
  1359. /*******************************************************************************
  1360. * FUNCTION: pt_check_command
  1361. *
  1362. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1363. * called here to check output command and store parameter to the list.
  1364. *
  1365. * PARAMETERS:
  1366. * *cd - pointer to core data
  1367. * *hid_output - pointer to hid output data structure
  1368. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1369. ******************************************************************************/
  1370. static void pt_check_command(struct pt_core_data *cd,
  1371. struct pt_hid_output *hid_output, bool raw)
  1372. {
  1373. pt_check_set_parameter(cd, hid_output, raw);
  1374. }
  1375. /*******************************************************************************
  1376. * FUNCTION: pt_hid_output_validate_response
  1377. *
  1378. * SUMMARY: Validate the response of application or bootloader command.
  1379. *
  1380. * RETURN:
  1381. * 0 = success
  1382. * !0 = failure
  1383. *
  1384. * PARAMETERS:
  1385. * *cd - pointer to core data
  1386. * *hid_output - pointer to hid output data structure
  1387. ******************************************************************************/
  1388. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1389. struct pt_hid_output *hid_output)
  1390. {
  1391. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1392. return pt_hid_output_validate_bl_response(cd, hid_output);
  1393. return pt_hid_output_validate_app_response(cd, hid_output);
  1394. }
  1395. /*******************************************************************************
  1396. * FUNCTION: pt_hid_send_output_user_
  1397. *
  1398. * SUMMARY: Blindly send user data to the DUT.
  1399. *
  1400. * RETURN:
  1401. * 0 = success
  1402. * !0 = failure
  1403. *
  1404. * PARAMETERS:
  1405. * *cd - pointer to core data
  1406. * *hid_output - pointer to the command to send
  1407. ******************************************************************************/
  1408. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1409. struct pt_hid_output *hid_output)
  1410. {
  1411. int rc = 0;
  1412. int cmd;
  1413. if (!hid_output->length || !hid_output->write_buf)
  1414. return -EINVAL;
  1415. if (cd->pip2_prot_active) {
  1416. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1417. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1418. } else
  1419. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1420. pt_debug(cd->dev, DL_INFO,
  1421. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1422. __func__, hid_output->length, cmd);
  1423. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1424. hid_output->length, ">>> User CMD");
  1425. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1426. hid_output->write_buf, NULL);
  1427. if (rc)
  1428. pt_debug(cd->dev, DL_ERROR,
  1429. "%s: Fail pt_adap_transfer\n", __func__);
  1430. return rc;
  1431. }
  1432. /*******************************************************************************
  1433. * FUNCTION: pt_hid_send_output_user_and_wait_
  1434. *
  1435. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1436. *
  1437. * RETURN:
  1438. * 0 = success
  1439. * !0 = failure
  1440. *
  1441. * PARAMETERS:
  1442. * *cd - pointer to core data
  1443. * *hid_output - pointer to the command to send
  1444. ******************************************************************************/
  1445. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1446. struct pt_hid_output *hid_output)
  1447. {
  1448. int rc = 0;
  1449. int t;
  1450. mutex_lock(&cd->system_lock);
  1451. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1452. mutex_unlock(&cd->system_lock);
  1453. rc = pt_hid_send_output_user_(cd, hid_output);
  1454. if (rc)
  1455. goto error;
  1456. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1457. msecs_to_jiffies(cd->pip_cmd_timeout));
  1458. if (IS_TMO(t)) {
  1459. #ifdef TTDL_DIAGNOSTICS
  1460. cd->bus_transmit_error_count++;
  1461. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1462. #endif /* TTDL_DIAGNOSTICS */
  1463. pt_debug(cd->dev, DL_ERROR,
  1464. "%s: HID output cmd execution timed out\n",
  1465. __func__);
  1466. rc = -ETIME;
  1467. goto error;
  1468. }
  1469. pt_check_command(cd, hid_output, true);
  1470. goto exit;
  1471. error:
  1472. mutex_lock(&cd->system_lock);
  1473. cd->hid_cmd_state = 0;
  1474. mutex_unlock(&cd->system_lock);
  1475. exit:
  1476. return rc;
  1477. }
  1478. /*******************************************************************************
  1479. * FUNCTION: pt_check_irq_asserted
  1480. *
  1481. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1482. * the FW can hold the INT line low ~150us after the read is complete.
  1483. * NOTE: if irq_stat is not defined this function will return false
  1484. *
  1485. * RETURN:
  1486. * true = IRQ asserted
  1487. * false = IRQ not asserted
  1488. *
  1489. * PARAMETERS:
  1490. * *cd - pointer to core data
  1491. ******************************************************************************/
  1492. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1493. {
  1494. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1495. /*
  1496. * Workaround for FW defect, CDT165308
  1497. * bl_launch app creates a glitch in IRQ line
  1498. */
  1499. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1500. && cd->cpdata->irq_stat) {
  1501. /*
  1502. * in X1S panel and GC1546 panel, the width for the INT
  1503. * glitch is about 4us,the normal INT width of response
  1504. * will last more than 200us, so use 10us delay
  1505. * for distinguish the glitch the normal INT is enough.
  1506. */
  1507. udelay(10);
  1508. }
  1509. #endif
  1510. if (cd->cpdata->irq_stat) {
  1511. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1512. == PT_IRQ_ASSERTED_VALUE) {
  1513. /* Debounce to allow FW to release INT */
  1514. usleep_range(100, 200);
  1515. }
  1516. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1517. == PT_IRQ_ASSERTED_VALUE)
  1518. return true;
  1519. else
  1520. return false;
  1521. }
  1522. return true;
  1523. }
  1524. /*******************************************************************************
  1525. * FUNCTION: pt_flush_bus
  1526. *
  1527. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1528. * Used if IRQ is found to be stuck low
  1529. *
  1530. * RETURN: Length of bytes read from bus
  1531. *
  1532. * PARAMETERS:
  1533. * *cd - pointer to core data
  1534. * flush_type - type of flush
  1535. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1536. * - PT_FLUSH_BUS_FULL_256_READ
  1537. * *read_buf - pointer to store read data
  1538. ******************************************************************************/
  1539. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1540. u8 flush_type, u8 *read_buf)
  1541. {
  1542. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1543. u16 pip_len;
  1544. int bytes_read;
  1545. int rc = 0;
  1546. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1547. rc = pt_adap_read_default(cd, buf, 2);
  1548. if (rc) {
  1549. bytes_read = 0;
  1550. goto exit;
  1551. }
  1552. pip_len = get_unaligned_le16(&buf[0]);
  1553. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1554. #ifdef TTDL_DIAGNOSTICS
  1555. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1556. #endif
  1557. bytes_read = 2;
  1558. pt_debug(cd->dev, DL_INFO,
  1559. "%s: Empty buf detected - len=0x%04X\n",
  1560. __func__, pip_len);
  1561. } else if (pip_len == 0) {
  1562. bytes_read = 0;
  1563. pt_debug(cd->dev, DL_INFO,
  1564. "%s: Sentinel detected\n", __func__);
  1565. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1566. pt_debug(cd->dev, DL_ERROR,
  1567. "%s: Illegal len=0x%04x, force %d byte read\n",
  1568. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1569. rc = pt_adap_read_default(cd, buf,
  1570. PT_MAX_PIP2_MSG_SIZE);
  1571. if (!rc)
  1572. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1573. else
  1574. bytes_read = 0;
  1575. } else {
  1576. pt_debug(cd->dev, DL_INFO,
  1577. "%s: Flush read of %d bytes...\n",
  1578. __func__, pip_len);
  1579. rc = pt_adap_read_default(cd, buf, pip_len);
  1580. if (!rc)
  1581. bytes_read = pip_len;
  1582. else
  1583. bytes_read = 0;
  1584. }
  1585. } else {
  1586. pt_debug(cd->dev, DL_INFO,
  1587. "%s: Forced flush of max %d bytes...\n",
  1588. __func__, PT_MAX_PIP2_MSG_SIZE);
  1589. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1590. if (!rc)
  1591. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1592. else
  1593. bytes_read = 0;
  1594. }
  1595. if (read_buf && (bytes_read > 3))
  1596. memcpy(read_buf, buf, bytes_read);
  1597. exit:
  1598. return bytes_read;
  1599. }
  1600. /*******************************************************************************
  1601. * FUNCTION: pt_flush_bus_if_irq_asserted
  1602. *
  1603. * SUMMARY: This function will flush the active bus if the INT is found to be
  1604. * asserted.
  1605. *
  1606. * RETURN: bytes cleared from bus
  1607. *
  1608. * PARAMETERS:
  1609. * *cd - pointer the core data structure
  1610. * flush_type - type of flush
  1611. * - PT_FLUSH_BUS_BASED_ON_LEN
  1612. * - PT_FLUSH_BUS_FULL_256_READ
  1613. ******************************************************************************/
  1614. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1615. {
  1616. int count = 0;
  1617. int bytes_read = 0;
  1618. while (pt_check_irq_asserted(cd) && count < 5) {
  1619. count++;
  1620. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1621. if (bytes_read) {
  1622. pt_debug(cd->dev, DL_WARN,
  1623. "%s: Cleared %d bytes off bus\n",
  1624. __func__, bytes_read);
  1625. }
  1626. }
  1627. if (pt_check_irq_asserted(cd)) {
  1628. pt_debug(cd->dev, DL_ERROR,
  1629. "%s: IRQ still asserted, %d bytes read\n",
  1630. __func__, bytes_read);
  1631. } else {
  1632. pt_debug(cd->dev, DL_INFO,
  1633. "%s: IRQ cleared, %d bytes read\n",
  1634. __func__, bytes_read);
  1635. }
  1636. return bytes_read;
  1637. }
  1638. /*******************************************************************************
  1639. * FUNCTION: pt_hid_send_output_
  1640. *
  1641. * SUMMARY: Send a touch application command to the DUT
  1642. *
  1643. * RETURN:
  1644. * 0 = success
  1645. * !0 = failure
  1646. *
  1647. * PARAMETERS:
  1648. * *cd - pointer to core data
  1649. * *hid_output - pointer to the command to send
  1650. ******************************************************************************/
  1651. static int pt_hid_send_output_(struct pt_core_data *cd,
  1652. struct pt_hid_output *hid_output)
  1653. {
  1654. int rc = 0;
  1655. u8 *cmd;
  1656. u16 length;
  1657. u16 crc;
  1658. u8 report_id;
  1659. u8 cmd_offset = 0;
  1660. u8 cmd_allocated = 0;
  1661. switch (hid_output->cmd_type) {
  1662. case PIP1_CMD_TYPE_FW:
  1663. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1664. length = 5;
  1665. break;
  1666. case PIP1_CMD_TYPE_BL:
  1667. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1668. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1669. break;
  1670. default:
  1671. return -EINVAL;
  1672. }
  1673. length += hid_output->write_length;
  1674. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1675. cmd = kzalloc(length + 2, GFP_KERNEL);
  1676. if (!cmd)
  1677. return -ENOMEM;
  1678. cmd_allocated = 1;
  1679. } else {
  1680. cmd = cd->cmd_buf;
  1681. }
  1682. /* Set Output register */
  1683. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1684. sizeof(cd->hid_desc.output_register));
  1685. cmd_offset += sizeof(cd->hid_desc.output_register);
  1686. cmd[cmd_offset++] = LOW_BYTE(length);
  1687. cmd[cmd_offset++] = HI_BYTE(length);
  1688. cmd[cmd_offset++] = report_id;
  1689. cmd[cmd_offset++] = 0x0; /* reserved */
  1690. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1691. cmd[cmd_offset++] = PIP1_BL_SOP;
  1692. cmd[cmd_offset++] = hid_output->command_code;
  1693. /* Set Data Length for bootloader */
  1694. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1695. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1696. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1697. }
  1698. /* Set Data */
  1699. if (hid_output->write_length && hid_output->write_buf) {
  1700. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1701. hid_output->write_length);
  1702. cmd_offset += hid_output->write_length;
  1703. }
  1704. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1705. crc = _pt_compute_crc(&cmd[6],
  1706. hid_output->write_length + 4);
  1707. cmd[cmd_offset++] = LOW_BYTE(crc);
  1708. cmd[cmd_offset++] = HI_BYTE(crc);
  1709. cmd[cmd_offset++] = PIP1_BL_EOP;
  1710. }
  1711. pt_debug(cd->dev, DL_INFO,
  1712. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1713. __func__, length + 2, hid_output->command_code);
  1714. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1715. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1716. if (rc)
  1717. pt_debug(cd->dev, DL_ERROR,
  1718. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1719. if (cmd_allocated)
  1720. kfree(cmd);
  1721. return rc;
  1722. }
  1723. /*******************************************************************************
  1724. * FUNCTION: pt_pip1_send_output_and_wait_
  1725. *
  1726. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1727. *
  1728. * RETURN:
  1729. * 0 = success
  1730. * !0 = failure
  1731. *
  1732. * PARAMETERS:
  1733. * *cd - pointer to core data
  1734. * *hid_output - pointer to the command to send
  1735. ******************************************************************************/
  1736. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1737. struct pt_hid_output *hid_output)
  1738. {
  1739. int rc = 0;
  1740. int t;
  1741. u16 timeout_ms;
  1742. mutex_lock(&cd->system_lock);
  1743. cd->hid_cmd_state = hid_output->command_code + 1;
  1744. mutex_unlock(&cd->system_lock);
  1745. if (hid_output->timeout_ms)
  1746. timeout_ms = hid_output->timeout_ms;
  1747. else
  1748. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1749. rc = pt_hid_send_output_(cd, hid_output);
  1750. if (rc)
  1751. goto error;
  1752. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1753. msecs_to_jiffies(timeout_ms));
  1754. if (IS_TMO(t)) {
  1755. #ifdef TTDL_DIAGNOSTICS
  1756. cd->bus_transmit_error_count++;
  1757. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1758. #endif /* TTDL_DIAGNOSTICS */
  1759. pt_debug(cd->dev, DL_ERROR,
  1760. "%s: HID output cmd execution timed out (%dms)\n",
  1761. __func__, timeout_ms);
  1762. rc = -ETIME;
  1763. goto error;
  1764. }
  1765. if (!hid_output->novalidate)
  1766. rc = pt_hid_output_validate_response(cd, hid_output);
  1767. pt_check_command(cd, hid_output, false);
  1768. goto exit;
  1769. error:
  1770. mutex_lock(&cd->system_lock);
  1771. cd->hid_cmd_state = 0;
  1772. mutex_unlock(&cd->system_lock);
  1773. exit:
  1774. return rc;
  1775. }
  1776. /*******************************************************************************
  1777. * FUNCTION: pt_hid_output_user_cmd_
  1778. *
  1779. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1780. * to the DUT waiting for the response and loading it into the read buffer
  1781. *
  1782. * RETURN:
  1783. * 0 = success
  1784. * !0 = failure
  1785. *
  1786. * PARAMETERS:
  1787. * *cd - pointer to core data
  1788. * read_len - expected read length of the response
  1789. * *read_buf - pointer to where the response will be loaded
  1790. * write_len - length of the write buffer
  1791. * *write_buf - pointer to the write buffer
  1792. * *actual_read_len - pointer to the actual amount of data read back
  1793. ******************************************************************************/
  1794. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1795. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1796. u16 *actual_read_len)
  1797. {
  1798. int rc = 0;
  1799. u16 size;
  1800. struct pt_hid_output hid_output = {
  1801. .length = write_len,
  1802. .write_buf = write_buf,
  1803. };
  1804. #ifdef TTHE_TUNER_SUPPORT
  1805. if (!cd->pip2_send_user_cmd) {
  1806. int command_code = 0;
  1807. int len;
  1808. /* Print up to cmd ID */
  1809. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1810. if (write_len < len)
  1811. len = write_len;
  1812. else
  1813. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1814. & PIP1_CMD_COMMAND_ID_MASK;
  1815. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1816. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1817. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1818. tthe_print(cd, write_buf, len, "CMD=");
  1819. }
  1820. #endif
  1821. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1822. if (rc)
  1823. return rc;
  1824. /* Get the response size from the first 2 bytes in the response */
  1825. size = get_unaligned_le16(&cd->response_buf[0]);
  1826. /* Ensure size is not greater than max buffer size */
  1827. if (size > PT_MAX_PIP2_MSG_SIZE)
  1828. size = PT_MAX_PIP2_MSG_SIZE;
  1829. /* Minimum size to read is the 2 byte len field */
  1830. if (size == 0)
  1831. size = 2;
  1832. if (size > read_len) {
  1833. pt_debug(cd->dev, DL_ERROR,
  1834. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1835. __func__, size, read_len);
  1836. *actual_read_len = 0;
  1837. return -EIO;
  1838. }
  1839. memcpy(read_buf, cd->response_buf, size);
  1840. *actual_read_len = size;
  1841. return 0;
  1842. }
  1843. /*******************************************************************************
  1844. * FUNCTION: pt_hid_output_user_cmd
  1845. *
  1846. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1847. * the DUT.
  1848. *
  1849. * RETURN:
  1850. * 0 = success
  1851. * !0 = failure
  1852. *
  1853. * PARAMETERS:
  1854. * *cd - pointer to core data
  1855. * read_len - length of data to read
  1856. * *read_buf - pointer to store read data
  1857. * write_len - length of data to write
  1858. * *write_buf - pointer to buffer to write
  1859. * *actual_read_len - pointer to store data length actually read
  1860. ******************************************************************************/
  1861. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1862. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1863. u16 *actual_read_len)
  1864. {
  1865. int rc;
  1866. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1867. if (rc < 0) {
  1868. pt_debug(cd->dev, DL_ERROR,
  1869. "%s: fail get exclusive ex=%p own=%p\n",
  1870. __func__, cd->exclusive_dev, cd->dev);
  1871. return rc;
  1872. }
  1873. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1874. write_len, write_buf, actual_read_len);
  1875. if (release_exclusive(cd, cd->dev) < 0)
  1876. pt_debug(cd->dev, DL_ERROR,
  1877. "%s: fail to release exclusive\n", __func__);
  1878. return rc;
  1879. }
  1880. /*******************************************************************************
  1881. * FUNCTION: _pt_request_pip2_send_cmd
  1882. *
  1883. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1884. * interrupt and reads response data to read_buf
  1885. *
  1886. * RETURN:
  1887. * 0 = success
  1888. * !0 = failure
  1889. *
  1890. * PARAMETERS:
  1891. * *dev - pointer to device structure
  1892. * protect - flag to run in protected mode
  1893. * id - ID of PIP command
  1894. * *data - pointer to PIP data payload
  1895. * report_body_len - report length
  1896. * *read_buf - pointer to response buffer
  1897. * *actual_read_len - pointer to response buffer length
  1898. ******************************************************************************/
  1899. static int _pt_request_pip2_send_cmd(struct device *dev,
  1900. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1901. u16 *actual_read_len)
  1902. {
  1903. struct pt_core_data *cd = dev_get_drvdata(dev);
  1904. struct pip2_cmd_structure pip2_cmd;
  1905. int rc = 0;
  1906. int i = 0;
  1907. int j = 0;
  1908. u16 write_len;
  1909. u8 *write_buf = NULL;
  1910. u16 read_len;
  1911. u8 extra_bytes;
  1912. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1913. /* Hard coded register for PIP2.x */
  1914. pip2_cmd.reg[0] = 0x01;
  1915. pip2_cmd.reg[1] = 0x01;
  1916. /*
  1917. * For PIP2.1+ the length field value includes itself:
  1918. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1919. *
  1920. * The overall write length must include only the register:
  1921. * ADD 2: 2 (Register)
  1922. */
  1923. extra_bytes = 6;
  1924. write_len = 2;
  1925. /* PIP2 the CMD ID is a 7bit field */
  1926. if (id > PIP2_CMD_ID_END) {
  1927. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1928. __func__, id);
  1929. rc = -EINVAL;
  1930. goto exit;
  1931. }
  1932. pip2_cmd.len = report_body_len + extra_bytes;
  1933. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1934. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1935. pip2_cmd.data = data;
  1936. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1937. /* Add the command length to the extra bytes based on PIP version */
  1938. write_len += pip2_cmd.len;
  1939. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1940. __func__, pip2_cmd.len, write_len);
  1941. write_buf = kzalloc(write_len, GFP_KERNEL);
  1942. if (write_buf == NULL) {
  1943. rc = -ENOMEM;
  1944. goto exit;
  1945. }
  1946. write_buf[i++] = pip2_cmd.reg[0];
  1947. write_buf[i++] = pip2_cmd.reg[1];
  1948. write_buf[i++] = pip2_cmd.len & 0xff;
  1949. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1950. write_buf[i++] = pip2_cmd.seq;
  1951. write_buf[i++] = pip2_cmd.id;
  1952. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1953. write_buf[j] = pip2_cmd.data[j-i];
  1954. write_buf[j++] = pip2_cmd.crc[0];
  1955. write_buf[j++] = pip2_cmd.crc[1];
  1956. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1957. if (read_len < 0)
  1958. read_len = 255;
  1959. pt_debug(dev, DL_INFO,
  1960. "%s cmd_id[0x%02X] expected response length:%d ",
  1961. __func__, pip2_cmd.id, read_len);
  1962. /*
  1963. * All PIP2 commands come through this function.
  1964. * Set flag for PIP2.x interface to allow response parsing to know
  1965. * how to decode the protocol header.
  1966. */
  1967. mutex_lock(&cd->system_lock);
  1968. cd->pip2_prot_active = true;
  1969. cd->pip2_send_user_cmd = true;
  1970. mutex_unlock(&cd->system_lock);
  1971. if (protect == PT_CORE_CMD_PROTECTED)
  1972. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1973. write_len, write_buf, actual_read_len);
  1974. else {
  1975. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1976. write_len, write_buf, actual_read_len);
  1977. }
  1978. if (rc) {
  1979. pt_debug(dev, DL_ERROR,
  1980. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1981. __func__, rc);
  1982. goto exit;
  1983. }
  1984. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1985. *actual_read_len);
  1986. exit:
  1987. mutex_lock(&cd->system_lock);
  1988. cd->pip2_prot_active = false;
  1989. cd->pip2_send_user_cmd = false;
  1990. mutex_unlock(&cd->system_lock);
  1991. kfree(write_buf);
  1992. return rc;
  1993. }
  1994. /*******************************************************************************
  1995. * FUNCTION: _pt_pip2_send_cmd_no_int
  1996. *
  1997. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1998. * reads response data to read_buf if response is available.
  1999. *
  2000. * NOTE:
  2001. * Interrupt MUST be disabled before to call this function.
  2002. *
  2003. * RETURN:
  2004. * 0 = success
  2005. * !0 = failure
  2006. *
  2007. * PARAMETERS:
  2008. * *dev - pointer to device structure
  2009. * protect - flag to run in protected mode
  2010. * id - ID of PIP command
  2011. * *data - pointer to PIP data payload
  2012. * report_body_len - report length
  2013. * *read_buf - pointer to response buffer
  2014. * *actual_read_len - pointer to response buffer length
  2015. ******************************************************************************/
  2016. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2017. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2018. u16 *actual_read_len)
  2019. {
  2020. int max_retry = 0;
  2021. int retry = 0;
  2022. int rc = 0;
  2023. int i = 0;
  2024. int j = 0;
  2025. u16 write_len;
  2026. u8 *write_buf = NULL;
  2027. u16 read_len;
  2028. u16 size = 0;
  2029. u8 response_seq = 0;
  2030. u8 extra_bytes;
  2031. u32 retry_interval = 0;
  2032. u32 retry_total_time = 0;
  2033. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2034. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2035. struct pt_core_data *cd = dev_get_drvdata(dev);
  2036. struct pip2_cmd_structure pip2_cmd;
  2037. if (protect == PT_CORE_CMD_PROTECTED) {
  2038. rc = request_exclusive(cd,
  2039. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2040. if (rc < 0) {
  2041. pt_debug(cd->dev, DL_ERROR,
  2042. "%s: fail get exclusive ex=%p own=%p\n",
  2043. __func__, cd->exclusive_dev, cd->dev);
  2044. return rc;
  2045. }
  2046. }
  2047. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2048. /* Hard coded register for PIP2.x */
  2049. pip2_cmd.reg[0] = 0x01;
  2050. pip2_cmd.reg[1] = 0x01;
  2051. /*
  2052. * For PIP2.1+ the length field value includes itself:
  2053. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2054. *
  2055. * The overall write length must include only the register:
  2056. * ADD 2: 2 (Register)
  2057. */
  2058. extra_bytes = 6;
  2059. write_len = 2;
  2060. pip2_cmd.len = report_body_len + extra_bytes;
  2061. pip2_cmd.id = id;
  2062. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2063. pip2_cmd.data = data;
  2064. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2065. /* Add the command length to the extra bytes based on PIP version */
  2066. write_len += pip2_cmd.len;
  2067. write_buf = kzalloc(write_len, GFP_KERNEL);
  2068. if (write_buf == NULL) {
  2069. rc = -ENOMEM;
  2070. goto exit;
  2071. }
  2072. write_buf[i++] = pip2_cmd.reg[0];
  2073. write_buf[i++] = pip2_cmd.reg[1];
  2074. write_buf[i++] = pip2_cmd.len & 0xff;
  2075. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2076. write_buf[i++] = pip2_cmd.seq;
  2077. write_buf[i++] = pip2_cmd.id;
  2078. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2079. write_buf[j] = pip2_cmd.data[j-i];
  2080. write_buf[j++] = pip2_cmd.crc[0];
  2081. write_buf[j++] = pip2_cmd.crc[1];
  2082. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2083. if (read_len < 0)
  2084. read_len = 255;
  2085. pt_debug(dev, DL_INFO,
  2086. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2087. __func__, pip2_cmd.id, read_len);
  2088. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2089. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2090. if (rc) {
  2091. pt_debug(dev, DL_ERROR,
  2092. "%s: SPI write Error = %d\n",
  2093. __func__, rc);
  2094. goto exit;
  2095. }
  2096. #ifdef PT_POLL_RESP_BY_BUS
  2097. /*
  2098. * Frequent bus read can increase system load obviously. The expected
  2099. * first bus read should be valid and timely. The tollerance for
  2100. * usleep_range should be limited. The minimum response delay (between
  2101. * command finishes sending and INT pin falls) is less than 50
  2102. * microseconds. So the 10 microseconds should be maximum tollerance
  2103. * with the consideration that the unit to calculate the response delay
  2104. * is 10 microseconds and more precise is not necessary. Every
  2105. * additional 10 microseconds only contribute less than 3 milliseconds
  2106. * for whole BL.
  2107. */
  2108. usleep_range(resp_time_min, resp_time_min+10);
  2109. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2110. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2111. rc = pt_adap_read_default(cd, read_buf, read_len);
  2112. if (rc) {
  2113. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2114. __func__, rc);
  2115. break;
  2116. }
  2117. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2118. size = get_unaligned_le16(&read_buf[0]);
  2119. if ((size <= read_len) &&
  2120. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2121. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2122. break;
  2123. }
  2124. /*
  2125. * To reduce the bus and system load, increase the sleep
  2126. * step gradually:
  2127. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2128. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2129. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2130. */
  2131. retry++;
  2132. if (retry < 20) {
  2133. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2134. usleep_range(retry_interval,
  2135. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2136. } else if (retry < 40) {
  2137. retry_interval += 1000;
  2138. usleep_range(retry_interval, retry_interval + 1000);
  2139. } else {
  2140. retry_interval += 50000;
  2141. msleep(retry_interval/1000);
  2142. }
  2143. retry_total_time += retry_interval;
  2144. }
  2145. #else
  2146. /*
  2147. * Frequent GPIO read will not increase CPU/system load heavily if the
  2148. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2149. * fixed interval: 20 us.
  2150. */
  2151. usleep_range(resp_time_min, resp_time_min+10);
  2152. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2153. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2154. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2155. rc = pt_adap_read_default(cd, read_buf, read_len);
  2156. size = get_unaligned_le16(&read_buf[0]);
  2157. if (rc)
  2158. pt_debug(dev, DL_ERROR,
  2159. "%s: SPI read Error = %d\n",
  2160. __func__, rc);
  2161. else if (size > read_len) {
  2162. pt_debug(cd->dev, DL_ERROR,
  2163. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2164. __func__, size, read_len);
  2165. rc = -EIO;
  2166. }
  2167. break;
  2168. }
  2169. /*
  2170. * Poll GPIO with fixed interval 20 us, and tollerance is
  2171. * limited to 10 us to speed up the process.
  2172. */
  2173. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2174. usleep_range(retry_interval, retry_interval+10);
  2175. retry_total_time += retry_interval;
  2176. }
  2177. #endif
  2178. *actual_read_len = size;
  2179. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2180. pt_debug(dev, DL_ERROR,
  2181. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2182. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2183. *actual_read_len = 0;
  2184. rc = -EINVAL;
  2185. }
  2186. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2187. "<<< NO_INT Read");
  2188. exit:
  2189. kfree(write_buf);
  2190. if (protect == PT_CORE_CMD_PROTECTED) {
  2191. if (release_exclusive(cd, cd->dev) < 0)
  2192. pt_debug(cd->dev, DL_ERROR,
  2193. "%s: fail to release exclusive\n", __func__);
  2194. }
  2195. return rc;
  2196. }
  2197. /*******************************************************************************
  2198. * FUNCTION: pt_pip_null_
  2199. *
  2200. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2201. * This function is used by watchdog to check if the fw corrupts.
  2202. *
  2203. * RETURN:
  2204. * 0 = success
  2205. * !0 = failure
  2206. *
  2207. * PARAMETERS:
  2208. * *cd - pointer to core data
  2209. ******************************************************************************/
  2210. static int pt_pip_null_(struct pt_core_data *cd)
  2211. {
  2212. struct pt_hid_output hid_output = {
  2213. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2214. };
  2215. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2216. }
  2217. /*******************************************************************************
  2218. * FUNCTION: pt_pip_null
  2219. *
  2220. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2221. *
  2222. * RETURN:
  2223. * 0 = success
  2224. * !0 = failure
  2225. *
  2226. * PARAMETERS:
  2227. * *cd - pointer to core data
  2228. ******************************************************************************/
  2229. static int pt_pip_null(struct pt_core_data *cd)
  2230. {
  2231. int rc;
  2232. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2233. if (rc < 0) {
  2234. pt_debug(cd->dev, DL_ERROR,
  2235. "%s: fail get exclusive ex=%p own=%p\n",
  2236. __func__, cd->exclusive_dev, cd->dev);
  2237. return rc;
  2238. }
  2239. rc = pt_pip_null_(cd);
  2240. if (release_exclusive(cd, cd->dev) < 0)
  2241. pt_debug(cd->dev, DL_ERROR,
  2242. "%s: fail to release exclusive\n", __func__);
  2243. return rc;
  2244. }
  2245. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2246. /*******************************************************************************
  2247. * FUNCTION: pt_pip_start_bootloader_
  2248. *
  2249. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2250. *
  2251. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2252. * the WD active could cause this function to fail!
  2253. *
  2254. * RETURN:
  2255. * 0 = success
  2256. * !0 = failure
  2257. *
  2258. * PARAMETERS:
  2259. * *cd - pointer to core data
  2260. ******************************************************************************/
  2261. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2262. {
  2263. int rc = 0;
  2264. struct pt_hid_output hid_output = {
  2265. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2266. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2267. .reset_expected = 1,
  2268. };
  2269. if (cd->watchdog_enabled) {
  2270. pt_debug(cd->dev, DL_WARN,
  2271. "%s: watchdog isn't stopped before enter bl\n",
  2272. __func__);
  2273. goto exit;
  2274. }
  2275. /* Reset startup status after entering BL, new DUT enum required */
  2276. cd->startup_status = STARTUP_STATUS_START;
  2277. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2278. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2279. if (rc) {
  2280. pt_debug(cd->dev, DL_ERROR,
  2281. "%s: Start BL PIP cmd failed. rc = %d\n",
  2282. __func__, rc);
  2283. }
  2284. exit:
  2285. return rc;
  2286. }
  2287. /*******************************************************************************
  2288. * FUNCTION: pt_pip_start_bootloader
  2289. *
  2290. * SUMMARY: Protected function to force DUT to enter the BL
  2291. *
  2292. * RETURN:
  2293. * 0 = success
  2294. * !0 = failure
  2295. *
  2296. * PARAMETERS:
  2297. * *cd - pointer to core data structure
  2298. ******************************************************************************/
  2299. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2300. {
  2301. int rc;
  2302. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2303. if (rc < 0) {
  2304. pt_debug(cd->dev, DL_ERROR,
  2305. "%s: fail get exclusive ex=%p own=%p\n",
  2306. __func__, cd->exclusive_dev, cd->dev);
  2307. return rc;
  2308. }
  2309. rc = pt_pip_start_bootloader_(cd);
  2310. if (release_exclusive(cd, cd->dev) < 0)
  2311. pt_debug(cd->dev, DL_ERROR,
  2312. "%s: fail to release exclusive\n", __func__);
  2313. return rc;
  2314. }
  2315. /*******************************************************************************
  2316. * FUNCTION: _pt_request_pip_start_bl
  2317. *
  2318. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2319. * modules to request the DUT to enter the BL
  2320. *
  2321. * RETURN:
  2322. * 0 = success
  2323. * !0 = failure
  2324. *
  2325. * PARAMETERS:
  2326. * *dev - pointer to device structure
  2327. * protect - flag to run in protected mode
  2328. ******************************************************************************/
  2329. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2330. {
  2331. struct pt_core_data *cd = dev_get_drvdata(dev);
  2332. if (protect)
  2333. return pt_pip_start_bootloader(cd);
  2334. return pt_pip_start_bootloader_(cd);
  2335. }
  2336. /*******************************************************************************
  2337. * FUNCTION: pt_pip2_ver_load_ttdata
  2338. *
  2339. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2340. * command into the core data struct.
  2341. *
  2342. * RETURN: n/a
  2343. *
  2344. * PARAMETERS:
  2345. * *cd - pointer to core data structure
  2346. * len - Length of data in response_buf
  2347. ******************************************************************************/
  2348. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2349. {
  2350. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2351. struct pt_pip2_version_full *full_ver;
  2352. struct pt_pip2_version *ver;
  2353. /*
  2354. * The PIP2 VERSION command can return different lengths of data.
  2355. * The additional LOT fields are included when the packet
  2356. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2357. * NOTE:
  2358. * - The FW would swap the BL and FW versions when reporting
  2359. * the small packet.
  2360. * - Sub Lot bytes 16 and 17 are reserved.
  2361. */
  2362. if (len >= 0x1D) {
  2363. full_ver = (struct pt_pip2_version_full *)
  2364. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2365. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2366. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2367. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2368. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2369. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2370. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2371. /*
  2372. * BL PIP 2.02 and greater the version fields are
  2373. * swapped
  2374. */
  2375. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2376. ttdata->chip_rev =
  2377. get_unaligned_le16(&full_ver->chip_rev);
  2378. ttdata->chip_id =
  2379. get_unaligned_le16(&full_ver->chip_id);
  2380. } else {
  2381. ttdata->chip_rev =
  2382. get_unaligned_le16(&full_ver->chip_id);
  2383. ttdata->chip_id =
  2384. get_unaligned_le16(&full_ver->chip_rev);
  2385. }
  2386. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2387. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2388. sizeof(struct pt_pip2_version_full),
  2389. "PIP2 VERSION FULL");
  2390. } else {
  2391. ver = (struct pt_pip2_version *)
  2392. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2393. ttdata->pip_ver_major = ver->pip2_version_msb;
  2394. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2395. ttdata->bl_ver_major = ver->bl_version_msb;
  2396. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2397. ttdata->fw_ver_major = ver->fw_version_msb;
  2398. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2399. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2400. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2401. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2402. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2403. }
  2404. }
  2405. /*******************************************************************************
  2406. * FUNCTION: pt_si_get_ttdata
  2407. *
  2408. * SUMMARY: Function to load the version information from the system information
  2409. * PIP command into the core data struct.
  2410. *
  2411. * RETURN: n/a
  2412. *
  2413. * PARAMETERS:
  2414. * *cd - pointer to core data structure
  2415. ******************************************************************************/
  2416. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2417. {
  2418. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2419. struct pt_ttdata_dev *ttdata_dev =
  2420. (struct pt_ttdata_dev *)
  2421. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2422. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2423. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2424. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2425. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2426. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2427. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2428. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2429. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2430. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2431. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2432. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2433. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2434. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2435. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2436. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2437. }
  2438. /*******************************************************************************
  2439. * FUNCTION: pt_si_get_sensing_conf_data
  2440. *
  2441. * SUMMARY: Function to load the sensing information from the system information
  2442. * PIP command into the core data struct.
  2443. *
  2444. * RETURN: n/a
  2445. *
  2446. * PARAMETERS:
  2447. * *cd - pointer to core data structure
  2448. ******************************************************************************/
  2449. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2450. {
  2451. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2452. struct pt_sensing_conf_data_dev *scd_dev =
  2453. (struct pt_sensing_conf_data_dev *)
  2454. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2455. scd->electrodes_x = scd_dev->electrodes_x;
  2456. scd->electrodes_y = scd_dev->electrodes_y;
  2457. scd->origin_x = scd_dev->origin_x;
  2458. scd->origin_y = scd_dev->origin_y;
  2459. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2460. if (scd->origin_x & 0x02) {
  2461. scd->tx_num = scd->electrodes_x;
  2462. scd->rx_num = scd->electrodes_y;
  2463. } else {
  2464. scd->tx_num = scd->electrodes_y;
  2465. scd->rx_num = scd->electrodes_x;
  2466. }
  2467. /*
  2468. * When the Panel ID is coming from an XY pin and not a dedicated
  2469. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2470. * other DUTs as the loader will use cd->pid_for_loader to generate
  2471. * the bin file name but will ignore it if pid_for_loader is still
  2472. * set to PANEL_ID_NOT_ENABLED
  2473. */
  2474. if (cd->panel_id_support &
  2475. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2476. mutex_lock(&cd->system_lock);
  2477. cd->pid_for_loader = scd_dev->panel_id;
  2478. mutex_unlock(&cd->system_lock);
  2479. }
  2480. scd->panel_id = scd_dev->panel_id;
  2481. scd->btn = scd_dev->btn;
  2482. scd->scan_mode = scd_dev->scan_mode;
  2483. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2484. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2485. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2486. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2487. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2488. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2489. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2490. sizeof(struct pt_sensing_conf_data_dev),
  2491. "sensing_conf_data");
  2492. }
  2493. /*******************************************************************************
  2494. * FUNCTION: pt_si_setup
  2495. *
  2496. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2497. *
  2498. * RETURN:
  2499. * 0 = success
  2500. * !0 = failure
  2501. *
  2502. * PARAMETERS:
  2503. * *cd - pointer to core data structure
  2504. ******************************************************************************/
  2505. static int pt_si_setup(struct pt_core_data *cd)
  2506. {
  2507. struct pt_sysinfo *si = &cd->sysinfo;
  2508. int max_tch = si->sensing_conf_data.max_tch;
  2509. if (!si->xy_data)
  2510. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2511. GFP_KERNEL);
  2512. if (!si->xy_data)
  2513. return -ENOMEM;
  2514. if (!si->xy_mode)
  2515. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2516. if (!si->xy_mode) {
  2517. kfree(si->xy_data);
  2518. return -ENOMEM;
  2519. }
  2520. return 0;
  2521. }
  2522. /*******************************************************************************
  2523. * FUNCTION: pt_si_get_btn_data
  2524. *
  2525. * SUMMARY: Setup the core data button information based on the response of the
  2526. * System Information PIP command.
  2527. *
  2528. * RETURN:
  2529. * 0 = success
  2530. * !0 = failure
  2531. *
  2532. * PARAMETERS:
  2533. * *cd - pointer to core data structure
  2534. ******************************************************************************/
  2535. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2536. {
  2537. struct pt_sysinfo *si = &cd->sysinfo;
  2538. int num_btns = 0;
  2539. int num_defined_keys;
  2540. u16 *key_table;
  2541. int btn;
  2542. int i;
  2543. int rc = 0;
  2544. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2545. & PIP1_SYSINFO_BTN_MASK;
  2546. size_t btn_keys_size;
  2547. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2548. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2549. if (btns & (1 << i))
  2550. num_btns++;
  2551. }
  2552. si->num_btns = num_btns;
  2553. if (num_btns) {
  2554. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2555. if (!si->btn)
  2556. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2557. if (!si->btn)
  2558. return -ENOMEM;
  2559. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2560. num_defined_keys = 0;
  2561. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2562. num_defined_keys = 0;
  2563. else
  2564. num_defined_keys = cd->cpdata->sett
  2565. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2566. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2567. key_table = (u16 *)cd->cpdata->sett
  2568. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2569. si->btn[btn].key_code = key_table[btn];
  2570. si->btn[btn].enabled = true;
  2571. }
  2572. for (; btn < num_btns; btn++) {
  2573. si->btn[btn].key_code = KEY_RESERVED;
  2574. si->btn[btn].enabled = true;
  2575. }
  2576. return rc;
  2577. }
  2578. kfree(si->btn);
  2579. si->btn = NULL;
  2580. return rc;
  2581. }
  2582. /*******************************************************************************
  2583. * FUNCTION: pt_si_put_log_data
  2584. *
  2585. * SUMMARY: Prints all sys info data to kmsg log
  2586. *
  2587. * RETURN: n/a
  2588. *
  2589. * PARAMETERS:
  2590. * *cd - pointer to core data structure
  2591. ******************************************************************************/
  2592. static void pt_si_put_log_data(struct pt_core_data *cd)
  2593. {
  2594. struct pt_sysinfo *si = &cd->sysinfo;
  2595. struct pt_ttdata *ttdata = &si->ttdata;
  2596. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2597. int i;
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2599. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2601. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2603. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2605. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2607. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2608. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2609. __func__, ttdata->revctrl, ttdata->revctrl);
  2610. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2611. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2612. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2613. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2614. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2615. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2616. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2617. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2618. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2619. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2620. for (i = 0; i < PT_NUM_MFGID; i++)
  2621. pt_debug(cd->dev, DL_DEBUG,
  2622. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2623. __func__, i, ttdata->mfg_id[i],
  2624. ttdata->mfg_id[i]);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2626. __func__, ttdata->post_code, ttdata->post_code);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2628. __func__, scd->electrodes_x, scd->electrodes_x);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2630. __func__, scd->electrodes_y, scd->electrodes_y);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2632. __func__, scd->len_x, scd->len_x);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2634. __func__, scd->len_y, scd->len_y);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2636. __func__, scd->res_x, scd->res_x);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2638. __func__, scd->res_y, scd->res_y);
  2639. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2640. __func__, scd->max_z, scd->max_z);
  2641. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2642. __func__, scd->origin_x, scd->origin_x);
  2643. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2644. __func__, scd->origin_y, scd->origin_y);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2646. __func__, scd->panel_id, scd->panel_id);
  2647. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2648. __func__, scd->btn, scd->btn);
  2649. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2650. __func__, scd->scan_mode, scd->scan_mode);
  2651. pt_debug(cd->dev, DL_DEBUG,
  2652. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2653. __func__, scd->max_tch, scd->max_tch);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2655. __func__, si->xy_mode);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2657. __func__, si->xy_data);
  2658. }
  2659. /*******************************************************************************
  2660. * FUNCTION: pt_get_sysinfo_regs
  2661. *
  2662. * SUMMARY: Setup all the core data System information based on the response
  2663. * of the System Information PIP command.
  2664. *
  2665. * RETURN:
  2666. * 0 = success
  2667. * !0 = failure
  2668. *
  2669. * PARAMETERS:
  2670. * *cd - pointer to core data structure
  2671. ******************************************************************************/
  2672. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2673. {
  2674. struct pt_sysinfo *si = &cd->sysinfo;
  2675. int rc;
  2676. rc = pt_si_get_btn_data(cd);
  2677. if (rc < 0)
  2678. return rc;
  2679. pt_si_get_ttdata(cd);
  2680. pt_si_get_sensing_conf_data(cd);
  2681. pt_si_setup(cd);
  2682. pt_si_put_log_data(cd);
  2683. si->ready = true;
  2684. return rc;
  2685. }
  2686. /*******************************************************************************
  2687. * FUNCTION: pt_free_si_ptrs
  2688. *
  2689. * SUMMARY: Frees all memory associated with the System Information within
  2690. * core data
  2691. *
  2692. * RETURN: n/a
  2693. *
  2694. * PARAMETERS:
  2695. * *cd - pointer to core data structure
  2696. ******************************************************************************/
  2697. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2698. {
  2699. struct pt_sysinfo *si = &cd->sysinfo;
  2700. kfree(si->btn);
  2701. kfree(si->xy_mode);
  2702. kfree(si->xy_data);
  2703. }
  2704. /*******************************************************************************
  2705. * FUNCTION: pt_hid_output_get_sysinfo_
  2706. *
  2707. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2708. * response.
  2709. *
  2710. * RETURN::
  2711. * 0 = success
  2712. * !0 = failure
  2713. *
  2714. * PARAMETERS:
  2715. * *cd - pointer to core data structure
  2716. ******************************************************************************/
  2717. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2718. {
  2719. int rc = 0;
  2720. struct pt_hid_output hid_output = {
  2721. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2722. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2723. };
  2724. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2725. if (rc)
  2726. return rc;
  2727. /* Parse the sysinfo data */
  2728. rc = pt_get_sysinfo_regs(cd);
  2729. if (rc)
  2730. pt_free_si_ptrs(cd);
  2731. return rc;
  2732. }
  2733. /*******************************************************************************
  2734. * FUNCTION: pt_hid_output_get_sysinfo
  2735. *
  2736. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2737. *
  2738. * RETURN::
  2739. * 0 = success
  2740. * !0 = failure
  2741. *
  2742. * PARAMETERS:
  2743. * *cd - pointer to core data structure
  2744. ******************************************************************************/
  2745. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2746. {
  2747. int rc;
  2748. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2749. if (rc < 0) {
  2750. pt_debug(cd->dev, DL_ERROR,
  2751. "%s: fail get exclusive ex=%p own=%p\n",
  2752. __func__, cd->exclusive_dev, cd->dev);
  2753. return rc;
  2754. }
  2755. rc = pt_hid_output_get_sysinfo_(cd);
  2756. if (release_exclusive(cd, cd->dev) < 0)
  2757. pt_debug(cd->dev, DL_ERROR,
  2758. "%s: fail to release exclusive\n", __func__);
  2759. return rc;
  2760. }
  2761. /*******************************************************************************
  2762. * FUNCTION: pt_pip_suspend_scanning_
  2763. *
  2764. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2765. *
  2766. * RETURN::
  2767. * 0 = success
  2768. * !0 = failure
  2769. *
  2770. * PARAMETERS:
  2771. * *cd - pointer to core data structure
  2772. ******************************************************************************/
  2773. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2774. {
  2775. int rc = 0;
  2776. struct pt_hid_output hid_output = {
  2777. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2778. };
  2779. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2780. if (rc) {
  2781. pt_debug(cd->dev, DL_ERROR,
  2782. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2783. __func__, rc);
  2784. }
  2785. return rc;
  2786. }
  2787. /*******************************************************************************
  2788. * FUNCTION: pt_pip_suspend_scanning
  2789. *
  2790. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2791. *
  2792. * RETURN::
  2793. * 0 = success
  2794. * !0 = failure
  2795. *
  2796. * PARAMETERS:
  2797. * *cd - pointer to core data structure
  2798. ******************************************************************************/
  2799. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2800. {
  2801. int rc;
  2802. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2803. if (rc < 0) {
  2804. pt_debug(cd->dev, DL_ERROR,
  2805. "%s: fail get exclusive ex=%p own=%p\n",
  2806. __func__, cd->exclusive_dev, cd->dev);
  2807. return rc;
  2808. }
  2809. rc = pt_pip_suspend_scanning_(cd);
  2810. if (release_exclusive(cd, cd->dev) < 0)
  2811. pt_debug(cd->dev, DL_ERROR,
  2812. "%s: fail to release exclusive\n", __func__);
  2813. return rc;
  2814. }
  2815. /*******************************************************************************
  2816. * FUNCTION: _pt_request_pip_suspend_scanning
  2817. *
  2818. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2819. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2820. *
  2821. * RETURN:
  2822. * 0 = success
  2823. * !0 = failure
  2824. *
  2825. * PARAMETERS:
  2826. * *dev - pointer to device structure
  2827. * protect - 0 = call non-protected function
  2828. * 1 = call protected function
  2829. ******************************************************************************/
  2830. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2831. int protect)
  2832. {
  2833. struct pt_core_data *cd = dev_get_drvdata(dev);
  2834. if (protect)
  2835. return pt_pip_suspend_scanning(cd);
  2836. return pt_pip_suspend_scanning_(cd);
  2837. }
  2838. /*******************************************************************************
  2839. * FUNCTION: pt_pip_resume_scanning_
  2840. *
  2841. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2842. *
  2843. * RETURN::
  2844. * 0 = success
  2845. * !0 = failure
  2846. *
  2847. * PARAMETERS:
  2848. * *cd - pointer to core data structure
  2849. ******************************************************************************/
  2850. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2851. {
  2852. int rc = 0;
  2853. struct pt_hid_output hid_output = {
  2854. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2855. };
  2856. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2857. if (rc) {
  2858. pt_debug(cd->dev, DL_ERROR,
  2859. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2860. __func__, rc);
  2861. }
  2862. return rc;
  2863. }
  2864. /*******************************************************************************
  2865. * FUNCTION: pt_pip_resume_scanning
  2866. *
  2867. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2868. *
  2869. * RETURN::
  2870. * 0 = success
  2871. * !0 = failure
  2872. *
  2873. * PARAMETERS:
  2874. * *cd - pointer to core data structure
  2875. ******************************************************************************/
  2876. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2877. {
  2878. int rc;
  2879. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2880. if (rc < 0) {
  2881. pt_debug(cd->dev, DL_ERROR,
  2882. "%s: fail get exclusive ex=%p own=%p\n",
  2883. __func__, cd->exclusive_dev, cd->dev);
  2884. return rc;
  2885. }
  2886. rc = pt_pip_resume_scanning_(cd);
  2887. if (release_exclusive(cd, cd->dev) < 0)
  2888. pt_debug(cd->dev, DL_ERROR,
  2889. "%s: fail to release exclusive\n", __func__);
  2890. return rc;
  2891. }
  2892. /*******************************************************************************
  2893. * FUNCTION: _pt_request_pip_resume_scanning
  2894. *
  2895. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2896. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2897. *
  2898. * RETURN::
  2899. * 0 = success
  2900. * !0 = failure
  2901. *
  2902. * PARAMETERS:
  2903. * *dev - pointer to device structure
  2904. * protect - 0 = call non-protected function
  2905. * 1 = call protected function
  2906. ******************************************************************************/
  2907. static int _pt_request_pip_resume_scanning(struct device *dev,
  2908. int protect)
  2909. {
  2910. struct pt_core_data *cd = dev_get_drvdata(dev);
  2911. if (protect)
  2912. return pt_pip_resume_scanning(cd);
  2913. return pt_pip_resume_scanning_(cd);
  2914. }
  2915. /*******************************************************************************
  2916. * FUNCTION: pt_pip_get_param_
  2917. *
  2918. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2919. * the 32bit parameter value
  2920. *
  2921. * RETURN:
  2922. * 0 = success
  2923. * !0 = failure
  2924. *
  2925. * PARAMETERS:
  2926. * *cd - pointer to core data
  2927. * param_id - parameter ID to retrieve
  2928. * *value - value of DUT parameter
  2929. ******************************************************************************/
  2930. static int pt_pip_get_param_(struct pt_core_data *cd,
  2931. u8 param_id, u32 *value)
  2932. {
  2933. int write_length = 1;
  2934. u8 param[1] = { param_id };
  2935. u8 read_param_id;
  2936. int param_size;
  2937. u8 *ptr;
  2938. int rc = 0;
  2939. int i;
  2940. struct pt_hid_output hid_output = {
  2941. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2942. .write_length = write_length,
  2943. .write_buf = param,
  2944. };
  2945. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2946. if (rc)
  2947. return rc;
  2948. read_param_id = cd->response_buf[5];
  2949. if (read_param_id != param_id)
  2950. return -EPROTO;
  2951. param_size = cd->response_buf[6];
  2952. ptr = &cd->response_buf[7];
  2953. *value = 0;
  2954. for (i = 0; i < param_size; i++)
  2955. *value += ptr[i] << (i * 8);
  2956. return 0;
  2957. }
  2958. /*******************************************************************************
  2959. * FUNCTION: pt_pip_get_param
  2960. *
  2961. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2962. *
  2963. * RETURN:
  2964. * 0 = success
  2965. * !0 = failure
  2966. *
  2967. * PARAMETERS:
  2968. * *cd - pointer to core data
  2969. * param_id - parameter ID to retrieve
  2970. * *value - value of DUT parameter
  2971. ******************************************************************************/
  2972. static int pt_pip_get_param(struct pt_core_data *cd,
  2973. u8 param_id, u32 *value)
  2974. {
  2975. int rc;
  2976. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2977. if (rc < 0) {
  2978. pt_debug(cd->dev, DL_ERROR,
  2979. "%s: fail get exclusive ex=%p own=%p\n",
  2980. __func__, cd->exclusive_dev, cd->dev);
  2981. return rc;
  2982. }
  2983. rc = pt_pip_get_param_(cd, param_id, value);
  2984. if (release_exclusive(cd, cd->dev) < 0)
  2985. pt_debug(cd->dev, DL_ERROR,
  2986. "%s: fail to release exclusive\n", __func__);
  2987. return rc;
  2988. }
  2989. /*******************************************************************************
  2990. * FUNCTION: _pt_request_pip_get_param
  2991. *
  2992. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2993. * calls to the protected or unprotected call to pt_pip_get_param
  2994. *
  2995. * RETURN:
  2996. * 0 = success
  2997. * !0 = failure
  2998. *
  2999. * PARAMETERS:
  3000. * *dev - pointer to device structure
  3001. * protect - flag to call protected or non protected function
  3002. * param_id - parameter ID to retrieve
  3003. * *value - value of DUT parameter
  3004. ******************************************************************************/
  3005. int _pt_request_pip_get_param(struct device *dev,
  3006. int protect, u8 param_id, u32 *value)
  3007. {
  3008. struct pt_core_data *cd = dev_get_drvdata(dev);
  3009. if (protect)
  3010. return pt_pip_get_param(cd, param_id, value);
  3011. return pt_pip_get_param_(cd, param_id, value);
  3012. }
  3013. /*******************************************************************************
  3014. * FUNCTION: pt_pip_set_param_
  3015. *
  3016. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3017. * passed in value to flash
  3018. *
  3019. * RETURN:
  3020. * 0 = success
  3021. * !0 = failure
  3022. *
  3023. * PARAMETERS:
  3024. * *cd - pointer to core data
  3025. * param_id - parameter ID to set
  3026. * value - value to write
  3027. * size - size to write
  3028. ******************************************************************************/
  3029. static int pt_pip_set_param_(struct pt_core_data *cd,
  3030. u8 param_id, u32 value, u8 size)
  3031. {
  3032. u8 write_buf[6];
  3033. u8 *ptr = &write_buf[2];
  3034. int rc = 0;
  3035. int i;
  3036. struct pt_hid_output hid_output = {
  3037. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3038. .write_buf = write_buf,
  3039. };
  3040. write_buf[0] = param_id;
  3041. write_buf[1] = size;
  3042. for (i = 0; i < size; i++) {
  3043. ptr[i] = value & 0xFF;
  3044. value = value >> 8;
  3045. }
  3046. hid_output.write_length = 2 + size;
  3047. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3048. if (rc)
  3049. return rc;
  3050. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3051. return -EPROTO;
  3052. return 0;
  3053. }
  3054. /*******************************************************************************
  3055. * FUNCTION: pt_pip_set_param
  3056. *
  3057. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3058. *
  3059. * RETURN:
  3060. * 0 = success
  3061. * !0 = failure
  3062. *
  3063. * PARAMETERS:
  3064. * *cd - pointer to core data
  3065. * param_id - parameter ID to set
  3066. * value - value to write
  3067. * size - size to write
  3068. ******************************************************************************/
  3069. static int pt_pip_set_param(struct pt_core_data *cd,
  3070. u8 param_id, u32 value, u8 size)
  3071. {
  3072. int rc;
  3073. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3074. if (rc < 0) {
  3075. pt_debug(cd->dev, DL_ERROR,
  3076. "%s: fail get exclusive ex=%p own=%p\n",
  3077. __func__, cd->exclusive_dev, cd->dev);
  3078. return rc;
  3079. }
  3080. rc = pt_pip_set_param_(cd, param_id, value, size);
  3081. if (release_exclusive(cd, cd->dev) < 0)
  3082. pt_debug(cd->dev, DL_ERROR,
  3083. "%s: fail to release exclusive\n", __func__);
  3084. return rc;
  3085. }
  3086. /*******************************************************************************
  3087. * FUNCTION: _pt_request_pip_set_param
  3088. *
  3089. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3090. * calls to the protected or unprotected call to pt_pip_set_param
  3091. *
  3092. * RETURN:
  3093. * 0 = success
  3094. * !0 = failure
  3095. *
  3096. * PARAMETERS:
  3097. * *dev - pointer to device structure
  3098. * protect - flag to call protected or non-protected
  3099. * param_id - parameter ID to set
  3100. * value - value to write
  3101. * size - size to write
  3102. ******************************************************************************/
  3103. int _pt_request_pip_set_param(struct device *dev, int protect,
  3104. u8 param_id, u32 value, u8 size)
  3105. {
  3106. struct pt_core_data *cd = dev_get_drvdata(dev);
  3107. if (protect)
  3108. return pt_pip_set_param(cd, param_id, value, size);
  3109. return pt_pip_set_param_(cd, param_id, value, size);
  3110. }
  3111. /*******************************************************************************
  3112. * FUNCTION: _pt_pip_enter_easywake_state_
  3113. *
  3114. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3115. *
  3116. * RETURN:
  3117. * 0 = success
  3118. * !0 = failure
  3119. *
  3120. * PARAMETERS:
  3121. * *dev - pointer to device structure
  3122. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3123. * *return_data - return status if easywake was entered
  3124. ******************************************************************************/
  3125. static int pt_hid_output_enter_easywake_state_(
  3126. struct pt_core_data *cd, u8 data, u8 *return_data)
  3127. {
  3128. int write_length = 1;
  3129. u8 param[1] = { data };
  3130. int rc = 0;
  3131. struct pt_hid_output hid_output = {
  3132. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3133. .write_length = write_length,
  3134. .write_buf = param,
  3135. };
  3136. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3137. if (rc)
  3138. return rc;
  3139. *return_data = cd->response_buf[5];
  3140. return rc;
  3141. }
  3142. /*******************************************************************************
  3143. * FUNCTION: pt_pip_verify_config_block_crc_
  3144. *
  3145. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3146. *
  3147. * RETURN:
  3148. * 0 = success
  3149. * !0 = failure
  3150. *
  3151. * PARAMETERS:
  3152. * *cd - pointer the core data structure
  3153. * ebid - enumerated block ID
  3154. * *status - PIP command status
  3155. * calculated_crc - calculated CRC
  3156. * stored_crc - stored CRC in config area
  3157. ******************************************************************************/
  3158. static int pt_pip_verify_config_block_crc_(
  3159. struct pt_core_data *cd, u8 ebid, u8 *status,
  3160. u16 *calculated_crc, u16 *stored_crc)
  3161. {
  3162. int write_length = 1;
  3163. u8 param[1] = { ebid };
  3164. u8 *ptr;
  3165. int rc = 0;
  3166. struct pt_hid_output hid_output = {
  3167. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3168. .write_length = write_length,
  3169. .write_buf = param,
  3170. };
  3171. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3172. if (rc)
  3173. return rc;
  3174. ptr = &cd->response_buf[5];
  3175. *status = ptr[0];
  3176. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3177. *stored_crc = get_unaligned_le16(&ptr[3]);
  3178. return 0;
  3179. }
  3180. /*******************************************************************************
  3181. * FUNCTION: pt_pip_verify_config_block_crc
  3182. *
  3183. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3184. * an exclusive access to the DUT.
  3185. *
  3186. * RETURN:
  3187. * 0 = success
  3188. * !0 = failure
  3189. *
  3190. * PARAMETERS:
  3191. * *cd - pointer the core data structure
  3192. * ebid - enumerated block ID
  3193. * *status - PIP command status
  3194. * calculated_crc - calculated CRC
  3195. * stored_crc - stored CRC in config area
  3196. ******************************************************************************/
  3197. static int pt_pip_verify_config_block_crc(
  3198. struct pt_core_data *cd, u8 ebid, u8 *status,
  3199. u16 *calculated_crc, u16 *stored_crc)
  3200. {
  3201. int rc;
  3202. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3203. if (rc < 0) {
  3204. pt_debug(cd->dev, DL_ERROR,
  3205. "%s: fail get exclusive ex=%p own=%p\n",
  3206. __func__, cd->exclusive_dev, cd->dev);
  3207. return rc;
  3208. }
  3209. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3210. calculated_crc, stored_crc);
  3211. if (release_exclusive(cd, cd->dev) < 0)
  3212. pt_debug(cd->dev, DL_ERROR,
  3213. "%s: fail to release exclusive\n", __func__);
  3214. return rc;
  3215. }
  3216. /*******************************************************************************
  3217. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3218. *
  3219. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3220. * calls to the protected or unprotected call to
  3221. * pt_pip_verify_config_block_crc_
  3222. *
  3223. * RETURN:
  3224. * 0 = success
  3225. * !0 = failure
  3226. *
  3227. * PARAMETERS:
  3228. * *dev - pointer to device structure
  3229. * protect - flag to call protected or non-protected
  3230. * ebid - enumerated block ID
  3231. * *status - PIP command status
  3232. * calculated_crc - calculated CRC
  3233. * stored_crc - stored CRC in config area
  3234. ******************************************************************************/
  3235. static int _pt_request_pip_verify_config_block_crc(
  3236. struct device *dev, int protect, u8 ebid, u8 *status,
  3237. u16 *calculated_crc, u16 *stored_crc)
  3238. {
  3239. struct pt_core_data *cd = dev_get_drvdata(dev);
  3240. if (protect)
  3241. return pt_pip_verify_config_block_crc(cd, ebid,
  3242. status, calculated_crc, stored_crc);
  3243. return pt_pip_verify_config_block_crc_(cd, ebid,
  3244. status, calculated_crc, stored_crc);
  3245. }
  3246. /*******************************************************************************
  3247. * FUNCTION: pt_pip_get_config_row_size_
  3248. *
  3249. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3250. *
  3251. * RETURN:
  3252. * 0 = success
  3253. * !0 = failure
  3254. *
  3255. * PARAMETERS:
  3256. * *cd - pointer to core data
  3257. * protect - flag to call protected or non-protected
  3258. * *row_size - pointer to store the retrieved row size
  3259. ******************************************************************************/
  3260. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3261. u16 *row_size)
  3262. {
  3263. int rc = 0;
  3264. struct pt_hid_output hid_output = {
  3265. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3266. };
  3267. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3268. if (rc)
  3269. return rc;
  3270. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3271. return 0;
  3272. }
  3273. /*******************************************************************************
  3274. * FUNCTION: pt_pip_get_config_row_size
  3275. *
  3276. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3277. * an exclusive access to the DUT.
  3278. *
  3279. * RETURN:
  3280. * 0 = success
  3281. * !0 = failure
  3282. *
  3283. * PARAMETERS:
  3284. * *cd - pointer to core data
  3285. * protect - flag to call protected or non-protected
  3286. * *row_size - pointer to store the retrieved row size
  3287. ******************************************************************************/
  3288. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3289. u16 *row_size)
  3290. {
  3291. int rc;
  3292. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3293. if (rc < 0) {
  3294. pt_debug(cd->dev, DL_ERROR,
  3295. "%s: fail get exclusive ex=%p own=%p\n",
  3296. __func__, cd->exclusive_dev, cd->dev);
  3297. return rc;
  3298. }
  3299. rc = pt_pip_get_config_row_size_(cd, row_size);
  3300. if (release_exclusive(cd, cd->dev) < 0)
  3301. pt_debug(cd->dev, DL_ERROR,
  3302. "%s: fail to release exclusive\n", __func__);
  3303. return rc;
  3304. }
  3305. /*******************************************************************************
  3306. * FUNCTION: _pt_request_pip_get_config_row_size
  3307. *
  3308. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3309. * calls to the protected or unprotected call to
  3310. * pt_pip_get_config_row_size_
  3311. *
  3312. * RETURN:
  3313. * 0 = success
  3314. * !0 = failure
  3315. *
  3316. * PARAMETERS:
  3317. * *dev - pointer to device structure
  3318. * protect - flag to call protected or non-protected
  3319. * *row_size - pointer to store the retrieved row size
  3320. ******************************************************************************/
  3321. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3322. int protect, u16 *row_size)
  3323. {
  3324. struct pt_core_data *cd = dev_get_drvdata(dev);
  3325. if (protect)
  3326. return pt_pip_get_config_row_size(cd, row_size);
  3327. return pt_pip_get_config_row_size_(cd, row_size);
  3328. }
  3329. /*******************************************************************************
  3330. * FUNCTION: pt_pip1_read_data_block_
  3331. *
  3332. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3333. * output data to the "read_buf" and update "crc".
  3334. *
  3335. * RETURN:
  3336. * 0 = success
  3337. * !0 = failure
  3338. *
  3339. * PARAMETERS:
  3340. * *cd - pointer to core data
  3341. * row_number - row number
  3342. * length - length of data to read
  3343. * ebid - block id
  3344. * *actual_read_len - Actual data length read
  3345. * *read_buf - pointer to the buffer to store read data
  3346. * read_buf_size - size of read_buf
  3347. * *crc - pointer to store CRC of row data
  3348. ******************************************************************************/
  3349. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3350. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3351. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3352. {
  3353. int read_ebid;
  3354. int status;
  3355. int rc = 0;
  3356. int write_length = 5;
  3357. u8 write_buf[5];
  3358. u8 cmd_offset = 0;
  3359. u16 calc_crc;
  3360. struct pt_hid_output hid_output = {
  3361. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3362. .write_length = write_length,
  3363. .write_buf = write_buf,
  3364. };
  3365. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3366. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3367. write_buf[cmd_offset++] = LOW_BYTE(length);
  3368. write_buf[cmd_offset++] = HI_BYTE(length);
  3369. write_buf[cmd_offset++] = ebid;
  3370. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3371. if (rc)
  3372. return rc;
  3373. status = cd->response_buf[5];
  3374. if (status)
  3375. return status;
  3376. read_ebid = cd->response_buf[6];
  3377. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3378. return -EPROTO;
  3379. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3380. if (length == 0 || *actual_read_len == 0)
  3381. return 0;
  3382. if (read_buf_size >= *actual_read_len)
  3383. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3384. else
  3385. return -EPROTO;
  3386. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3387. /* Validate Row Data CRC */
  3388. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3389. if (*crc == calc_crc) {
  3390. return 0;
  3391. } else {
  3392. pt_debug(cd->dev, DL_ERROR,
  3393. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3394. __func__, *crc, calc_crc);
  3395. return -EPROTO;
  3396. }
  3397. }
  3398. /*******************************************************************************
  3399. * FUNCTION: _pt_request_pip_read_data_block
  3400. *
  3401. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3402. * calls to pt_pip1_read_data_block_
  3403. *
  3404. * RETURN:
  3405. * 0 = success
  3406. * !0 = failure
  3407. *
  3408. * PARAMETERS:
  3409. * *dev - pointer to device structure
  3410. * row_number - row number
  3411. * length - length of data to read
  3412. * ebid - block id
  3413. * *actual_read_len - Actual data length read
  3414. * *read_buf - pointer to the buffer to store read data
  3415. * *crc - pointer to store CRC of row data
  3416. ******************************************************************************/
  3417. static int _pt_request_pip_read_data_block(struct device *dev,
  3418. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3419. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3420. {
  3421. struct pt_core_data *cd = dev_get_drvdata(dev);
  3422. return pt_pip1_read_data_block_(cd, row_number, length,
  3423. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3424. }
  3425. /*******************************************************************************
  3426. * FUNCTION: pt_pip1_write_data_block_
  3427. *
  3428. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3429. * write data to the data block.
  3430. *
  3431. * RETURN:
  3432. * 0 = success
  3433. * !0 = failure
  3434. *
  3435. * PARAMETERS:
  3436. * *cd - pointer to core data
  3437. * row_number - row in config block to write to
  3438. * write_length - length of data to write
  3439. * ebid - enumerated block ID
  3440. * *write_buf - pointer to buffer to write
  3441. * *security_key - pointer to security key to allow write
  3442. * *actual_write_len - pointer to store data length actually written
  3443. ******************************************************************************/
  3444. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3445. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3446. u8 *security_key, u16 *actual_write_len)
  3447. {
  3448. /* row_number + write_len + ebid + security_key + crc */
  3449. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3450. u8 *full_write_buf;
  3451. u8 cmd_offset = 0;
  3452. u16 crc;
  3453. int status;
  3454. int rc = 0;
  3455. int read_ebid;
  3456. u8 *data;
  3457. struct pt_hid_output hid_output = {
  3458. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3459. .write_length = full_write_length,
  3460. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3461. };
  3462. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3463. if (!full_write_buf)
  3464. return -ENOMEM;
  3465. hid_output.write_buf = full_write_buf;
  3466. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3467. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3468. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3469. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3470. full_write_buf[cmd_offset++] = ebid;
  3471. data = &full_write_buf[cmd_offset];
  3472. memcpy(data, write_buf, write_length);
  3473. cmd_offset += write_length;
  3474. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3475. cmd_offset += 8;
  3476. crc = _pt_compute_crc(data, write_length);
  3477. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3478. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3479. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3480. if (rc)
  3481. goto exit;
  3482. status = cd->response_buf[5];
  3483. if (status) {
  3484. rc = -EINVAL;
  3485. goto exit;
  3486. }
  3487. read_ebid = cd->response_buf[6];
  3488. if (read_ebid != ebid) {
  3489. rc = -EPROTO;
  3490. goto exit;
  3491. }
  3492. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3493. exit:
  3494. kfree(full_write_buf);
  3495. return rc;
  3496. }
  3497. /*******************************************************************************
  3498. * FUNCTION: _pt_request_pip_write_data_block
  3499. *
  3500. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3501. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3502. *
  3503. * RETURN:
  3504. * 0 = success
  3505. * !0 = failure
  3506. *
  3507. * PARAMETERS:
  3508. * *dev - pointer to device structure
  3509. * protect - flag to call protected or non-protected
  3510. * row_number - row in config block to write to
  3511. * write_length - length of data to write
  3512. * ebid - enumerated block ID
  3513. * *write_buf - pointer to buffer to write
  3514. * *security_key - pointer to security key to allow write
  3515. * *actual_write_len - pointer to store data length actually written
  3516. ******************************************************************************/
  3517. static int _pt_request_pip_write_data_block(struct device *dev,
  3518. u16 row_number, u16 write_length, u8 ebid,
  3519. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3520. {
  3521. struct pt_core_data *cd = dev_get_drvdata(dev);
  3522. return pt_pip1_write_data_block_(cd, row_number,
  3523. write_length, ebid, write_buf, security_key,
  3524. actual_write_len);
  3525. }
  3526. /*******************************************************************************
  3527. * FUNCTION: pt_pip_get_data_structure_
  3528. *
  3529. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3530. * returning a structure of data defined by data_id
  3531. *
  3532. * RETURN:
  3533. * 0 = success
  3534. * !0 = failure
  3535. *
  3536. * PARAMETERS:
  3537. * *cd - pointer to core data
  3538. * read_offset - read pointer offset
  3539. * read_length - length of data to read
  3540. * data_id - data ID to read
  3541. * *status - pointer to store the read response status
  3542. * *data_format - pointer to store format of data read
  3543. * *actual_read_len - pointer to store data length actually read
  3544. * *data - pointer to store data read
  3545. ******************************************************************************/
  3546. static int pt_pip_get_data_structure_(
  3547. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3548. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3549. u8 *data)
  3550. {
  3551. int rc = 0;
  3552. u16 total_read_len = 0;
  3553. u16 read_len;
  3554. u16 off_buf = 0;
  3555. u8 write_buf[5];
  3556. u8 read_data_id;
  3557. struct pt_hid_output hid_output = {
  3558. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3559. .write_length = 5,
  3560. .write_buf = write_buf,
  3561. };
  3562. again:
  3563. write_buf[0] = LOW_BYTE(read_offset);
  3564. write_buf[1] = HI_BYTE(read_offset);
  3565. write_buf[2] = LOW_BYTE(read_length);
  3566. write_buf[3] = HI_BYTE(read_length);
  3567. write_buf[4] = data_id;
  3568. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3569. if (rc)
  3570. return rc;
  3571. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3572. goto set_status;
  3573. read_data_id = cd->response_buf[6];
  3574. if (read_data_id != data_id)
  3575. return -EPROTO;
  3576. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3577. if (read_len && data) {
  3578. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3579. total_read_len += read_len;
  3580. if (read_len < read_length) {
  3581. read_offset += read_len;
  3582. off_buf += read_len;
  3583. read_length -= read_len;
  3584. goto again;
  3585. }
  3586. }
  3587. if (data_format)
  3588. *data_format = cd->response_buf[9];
  3589. if (actual_read_len)
  3590. *actual_read_len = total_read_len;
  3591. set_status:
  3592. if (status)
  3593. *status = cd->response_buf[5];
  3594. return rc;
  3595. }
  3596. /*******************************************************************************
  3597. * FUNCTION: pt_pip_get_data_structure
  3598. *
  3599. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3600. * an exclusive access to the DUT.
  3601. *
  3602. * RETURN:
  3603. * 0 = success
  3604. * !0 = failure
  3605. *
  3606. * PARAMETERS:
  3607. * *cd - pointer to core data
  3608. * read_offset - read pointer offset
  3609. * read_length - length of data to read
  3610. * data_id - data ID to read
  3611. * *status - pointer to store the read response status
  3612. * *data_format - pointer to store format of data read
  3613. * *actual_read_len - pointer to store data length actually read
  3614. * *data - pointer to store data read
  3615. ******************************************************************************/
  3616. static int pt_pip_get_data_structure(
  3617. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3618. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3619. u8 *data)
  3620. {
  3621. int rc;
  3622. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3623. if (rc < 0) {
  3624. pt_debug(cd->dev, DL_ERROR,
  3625. "%s: fail get exclusive ex=%p own=%p\n",
  3626. __func__, cd->exclusive_dev, cd->dev);
  3627. return rc;
  3628. }
  3629. rc = pt_pip_get_data_structure_(cd, read_offset,
  3630. read_length, data_id, status, data_format,
  3631. actual_read_len, data);
  3632. if (release_exclusive(cd, cd->dev) < 0)
  3633. pt_debug(cd->dev, DL_ERROR,
  3634. "%s: fail to release exclusive\n", __func__);
  3635. return rc;
  3636. }
  3637. /*******************************************************************************
  3638. * FUNCTION: _pt_request_pip_get_data_structure
  3639. *
  3640. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3641. * calls to the protected or unprotected call to
  3642. * pt_pip_get_data_structure
  3643. *
  3644. * RETURN:
  3645. * 0 = success
  3646. * !0 = failure
  3647. *
  3648. * PARAMETERS:
  3649. * *dev - pointer to device structure
  3650. * protect - flag to call protected or non-protected
  3651. * read_offset - read pointer offset
  3652. * read_length - length of data to read
  3653. * data_id - data ID to read
  3654. * *status - pointer to store the read response status
  3655. * *data_format - pointer to store format of data read
  3656. * *actual_read_len - pointer to store data length actually read
  3657. * *data - pointer to store data read
  3658. ******************************************************************************/
  3659. static int _pt_request_pip_get_data_structure(struct device *dev,
  3660. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3661. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3662. {
  3663. struct pt_core_data *cd = dev_get_drvdata(dev);
  3664. if (protect)
  3665. return pt_pip_get_data_structure(cd,
  3666. read_offset, read_length, data_id, status,
  3667. data_format, actual_read_len, data);
  3668. return pt_pip_get_data_structure_(cd,
  3669. read_offset, read_length, data_id, status,
  3670. data_format, actual_read_len, data);
  3671. }
  3672. /*******************************************************************************
  3673. * FUNCTION: _pt_manage_local_cal_data
  3674. *
  3675. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3676. * CALIBRATION data. It stores it in a local static array and can be
  3677. * cleared, loaded or used to restore the CAL data back to the running FW.
  3678. * The CAL data is read or restored by use of the PIP1 commands:
  3679. * - READ_DATA_BLOCK (0x22)
  3680. * - WRITE_DATA_BLOCK (0x23)
  3681. *
  3682. * RETURN:
  3683. * 0 = success
  3684. * !0 = failure
  3685. *
  3686. * PARAMETERS:
  3687. * *dev - pointer to device structure
  3688. * action - One of the following actions:
  3689. * - PT_CAL_DATA_SAVE
  3690. * - PT_CAL_DATA_RESTORE
  3691. * - PT_CAL_DATA_CLEAR
  3692. * - PT_CAL_DATA_SIZE
  3693. * *size - pointer to the number of bytes transferred
  3694. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3695. ******************************************************************************/
  3696. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3697. unsigned short *crc)
  3698. {
  3699. struct pt_core_data *cd = dev_get_drvdata(dev);
  3700. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3701. unsigned short calc_id_crc = 0;
  3702. static u8 *cal_cache_data;
  3703. static u16 cal_cache_len;
  3704. static unsigned short cal_cache_chip_id;
  3705. int rc = 0;
  3706. u8 *tmp_data = NULL;
  3707. u8 row_number = 0;
  3708. u8 prefix[20];
  3709. u16 cal_size = 0;
  3710. u16 transfer_size;
  3711. u16 act_trans_len = 0;
  3712. u16 byte_offset = 0;
  3713. u16 cal_blk_size;
  3714. u16 total_rows;
  3715. u16 remain_bytes;
  3716. u16 data_block_crc;
  3717. u16 buf_size = 12;
  3718. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3719. __func__, action);
  3720. switch (action) {
  3721. case PT_CAL_DATA_SAVE:
  3722. /* Read the size of the CAL block and calculate # rows */
  3723. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3724. if (!tmp_data) {
  3725. rc = -ENOMEM;
  3726. goto exit;
  3727. }
  3728. /*
  3729. * Don't check rc as doing a read size will give a false
  3730. * error on the CRC check.
  3731. */
  3732. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3733. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3734. cal_blk_size = act_trans_len;
  3735. kfree(tmp_data);
  3736. pt_debug(dev, DL_INFO,
  3737. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3738. __func__, cal_cache_len, cal_blk_size);
  3739. /* Safety net to ensure we didn't read incorrect size */
  3740. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3741. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3742. __func__);
  3743. rc = 1;
  3744. goto exit;
  3745. }
  3746. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3747. if (cal_blk_size != cal_cache_len) {
  3748. kfree(cal_cache_data);
  3749. cal_cache_data = kzalloc(cal_blk_size + 2,
  3750. GFP_KERNEL);
  3751. if (!cal_cache_data) {
  3752. rc = -ENOMEM;
  3753. goto exit;
  3754. }
  3755. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3756. __func__);
  3757. }
  3758. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3759. /* Calculate how many rows [0-n] (PIP Transactions) */
  3760. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3761. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3762. /* Add row if we have a last partial row */
  3763. if (remain_bytes > 0)
  3764. total_rows++;
  3765. pt_debug(dev, DL_INFO,
  3766. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3767. __func__, cal_blk_size, total_rows, remain_bytes);
  3768. /* Read all rows unless an error occurs */
  3769. rc = 0;
  3770. while (rc == 0 && row_number <= total_rows) {
  3771. act_trans_len = 0;
  3772. if (remain_bytes > 0 && row_number == total_rows)
  3773. transfer_size = remain_bytes;
  3774. else
  3775. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3776. rc = pt_pip1_read_data_block_(cd, row_number,
  3777. transfer_size, PT_CAL_EBID,
  3778. &act_trans_len,
  3779. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3780. &data_block_crc);
  3781. if (rc) {
  3782. /* Error occurred, exit loop */
  3783. cal_size = 0;
  3784. break;
  3785. }
  3786. pt_debug(dev, DL_INFO,
  3787. "%s: CAL read rc=%d actual read len=%d\n",
  3788. __func__, rc, act_trans_len);
  3789. byte_offset += act_trans_len;
  3790. cal_size = byte_offset;
  3791. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3792. pt_pr_buf(dev, DL_INFO,
  3793. &cal_cache_data[byte_offset - act_trans_len],
  3794. act_trans_len, prefix);
  3795. row_number++;
  3796. }
  3797. if (cal_size > 0) {
  3798. /* Save a CRC of the chip info the CAL was saved from */
  3799. calc_id_crc = crc_ccitt_calculate(
  3800. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3801. cal_cache_chip_id = calc_id_crc;
  3802. cal_cache_len = cal_size;
  3803. pt_debug(dev, DL_INFO,
  3804. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3805. __func__, calc_id_crc, cal_size);
  3806. }
  3807. *size = cal_size;
  3808. *crc = calc_id_crc;
  3809. break;
  3810. case PT_CAL_DATA_RESTORE:
  3811. cal_size = cal_cache_len;
  3812. while ((rc == 0) && (byte_offset < cal_size)) {
  3813. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3814. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3815. else
  3816. transfer_size = cal_size - byte_offset;
  3817. rc = pt_pip1_write_data_block_(cd, row_number,
  3818. transfer_size, PT_CAL_EBID,
  3819. &cal_cache_data[byte_offset],
  3820. (u8 *)pt_data_block_security_key,
  3821. &act_trans_len);
  3822. byte_offset += act_trans_len;
  3823. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3824. __func__, byte_offset);
  3825. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3826. pt_pr_buf(dev, DL_INFO,
  3827. &cal_cache_data[byte_offset - act_trans_len],
  3828. act_trans_len, prefix);
  3829. if ((byte_offset > cal_size) ||
  3830. (act_trans_len != transfer_size))
  3831. rc = -EIO;
  3832. row_number++;
  3833. }
  3834. *size = byte_offset;
  3835. *crc = cal_cache_chip_id;
  3836. break;
  3837. case PT_CAL_DATA_CLEAR:
  3838. if (cal_cache_data)
  3839. memset(&cal_cache_data[0], 0, cal_cache_len);
  3840. cal_cache_len = 0;
  3841. cal_cache_chip_id = 0;
  3842. *size = 0;
  3843. *crc = 0;
  3844. break;
  3845. case PT_CAL_DATA_INFO:
  3846. default:
  3847. *size = cal_cache_len;
  3848. *crc = cal_cache_chip_id;
  3849. pt_debug(dev, DL_INFO,
  3850. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3851. __func__, cal_cache_chip_id,
  3852. cal_cache_len);
  3853. break;
  3854. }
  3855. exit:
  3856. pt_debug(dev, DL_INFO,
  3857. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3858. __func__, rc, *crc, *size);
  3859. return rc;
  3860. }
  3861. /*******************************************************************************
  3862. * FUNCTION: pt_pip_run_selftest_
  3863. *
  3864. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3865. * to execute a FW built in self test
  3866. *
  3867. * RETURN:
  3868. * 0 = success
  3869. * !0 = failure
  3870. *
  3871. * PARAMETERS:
  3872. * *cd - pointer to core data
  3873. * test_id - enumerated test ID to run
  3874. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3875. * *status - pointer to store the read response status
  3876. * *summary_results - pointer to store the results summary
  3877. * *results_available - pointer to store if results are available
  3878. *****************************************************************************/
  3879. static int pt_pip_run_selftest_(
  3880. struct pt_core_data *cd, u8 test_id,
  3881. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3882. u8 *results_available)
  3883. {
  3884. int rc = 0;
  3885. u8 write_buf[2];
  3886. struct pt_hid_output hid_output = {
  3887. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3888. .write_length = 2,
  3889. .write_buf = write_buf,
  3890. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3891. };
  3892. write_buf[0] = test_id;
  3893. write_buf[1] = write_idacs_to_flash;
  3894. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3895. hid_output.write_length = 1;
  3896. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3897. if (rc)
  3898. return rc;
  3899. if (status)
  3900. *status = cd->response_buf[5];
  3901. if (summary_result)
  3902. *summary_result = cd->response_buf[6];
  3903. /* results_available only available before PIP 1.03 */
  3904. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3905. if (results_available)
  3906. *results_available = cd->response_buf[7];
  3907. }
  3908. return rc;
  3909. }
  3910. /*******************************************************************************
  3911. * FUNCTION: pt_pip_run_selftest
  3912. *
  3913. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3914. * an exclusive access to the DUT.
  3915. *
  3916. * RETURN:
  3917. * 0 = success
  3918. * !0 = failure
  3919. *
  3920. * PARAMETERS:
  3921. * *cd - pointer to core data
  3922. * test_id - enumerated test ID to run
  3923. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3924. * *status - pointer to store the read response status
  3925. * *summary_results - pointer to store the results summary
  3926. * *results_available - pointer to store if results are available
  3927. ******************************************************************************/
  3928. static int pt_pip_run_selftest(
  3929. struct pt_core_data *cd, u8 test_id,
  3930. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3931. u8 *results_available)
  3932. {
  3933. int rc;
  3934. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3935. if (rc < 0) {
  3936. pt_debug(cd->dev, DL_ERROR,
  3937. "%s: fail get exclusive ex=%p own=%p\n",
  3938. __func__, cd->exclusive_dev, cd->dev);
  3939. return rc;
  3940. }
  3941. rc = pt_pip_run_selftest_(cd, test_id,
  3942. write_idacs_to_flash, status, summary_result,
  3943. results_available);
  3944. if (release_exclusive(cd, cd->dev) < 0)
  3945. pt_debug(cd->dev, DL_ERROR,
  3946. "%s: fail to release exclusive\n", __func__);
  3947. return rc;
  3948. }
  3949. /*******************************************************************************
  3950. * FUNCTION: _pt_request_pip_run_selftest
  3951. *
  3952. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3953. * calls to the protected or unprotected call to pt_pip_run_selftest
  3954. *
  3955. * RETURN:
  3956. * 0 = success
  3957. * !0 = failure
  3958. *
  3959. * PARAMETERS:
  3960. * *dev - pointer to device structure
  3961. * protect - flag to call protected or non-protected
  3962. * test_id - enumerated test ID to run
  3963. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3964. * *status - pointer to store the read response status
  3965. * *summary_results - pointer to store the results summary
  3966. * *results_available - pointer to store if results are available
  3967. ******************************************************************************/
  3968. static int _pt_request_pip_run_selftest(struct device *dev,
  3969. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3970. u8 *summary_result, u8 *results_available)
  3971. {
  3972. struct pt_core_data *cd = dev_get_drvdata(dev);
  3973. if (protect)
  3974. return pt_pip_run_selftest(cd, test_id,
  3975. write_idacs_to_flash, status, summary_result,
  3976. results_available);
  3977. return pt_pip_run_selftest_(cd, test_id,
  3978. write_idacs_to_flash, status, summary_result,
  3979. results_available);
  3980. }
  3981. /*******************************************************************************
  3982. * FUNCTION: _pt_pip_get_selftest_result_
  3983. *
  3984. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3985. * to retrieve the self test results from the self test already executed
  3986. *
  3987. * RETURN:
  3988. * 0 = success
  3989. * !0 = failure
  3990. *
  3991. * PARAMETERS:
  3992. * *cd - pointer to core data
  3993. * read_offset - read pointer offset
  3994. * read_length - length of data to read
  3995. * test_id - enumerated test ID to read selftest results from
  3996. * *status - pointer to store the read response status
  3997. * *actual_read_len - pointer to store data length actually read
  3998. * *status - pointer to where the cmd response statas is stored
  3999. ******************************************************************************/
  4000. static int pt_pip_get_selftest_result_(
  4001. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4002. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4003. {
  4004. int rc = 0;
  4005. u16 total_read_len = 0;
  4006. u16 read_len;
  4007. u16 off_buf = 0;
  4008. u8 write_buf[5];
  4009. u8 read_test_id;
  4010. bool repeat;
  4011. struct pt_hid_output hid_output = {
  4012. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4013. .write_length = 5,
  4014. .write_buf = write_buf,
  4015. };
  4016. /*
  4017. * Do not repeat reading for Auto Shorts test
  4018. * when PIP version < 1.3
  4019. */
  4020. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4021. || test_id != PT_ST_ID_AUTOSHORTS;
  4022. again:
  4023. write_buf[0] = LOW_BYTE(read_offset);
  4024. write_buf[1] = HI_BYTE(read_offset);
  4025. write_buf[2] = LOW_BYTE(read_length);
  4026. write_buf[3] = HI_BYTE(read_length);
  4027. write_buf[4] = test_id;
  4028. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4029. if (rc)
  4030. return rc;
  4031. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4032. goto set_status;
  4033. read_test_id = cd->response_buf[6];
  4034. if (read_test_id != test_id)
  4035. return -EPROTO;
  4036. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4037. if (read_len && data) {
  4038. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4039. total_read_len += read_len;
  4040. if (repeat && read_len < read_length) {
  4041. read_offset += read_len;
  4042. off_buf += read_len;
  4043. read_length -= read_len;
  4044. goto again;
  4045. }
  4046. }
  4047. if (actual_read_len)
  4048. *actual_read_len = total_read_len;
  4049. set_status:
  4050. if (status)
  4051. *status = cd->response_buf[5];
  4052. return rc;
  4053. }
  4054. /*******************************************************************************
  4055. * FUNCTION: _pt_pip_get_selftest_result
  4056. *
  4057. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4058. * access to the DUT
  4059. *
  4060. * RETURN:
  4061. * 0 = success
  4062. * !0 = failure
  4063. *
  4064. * PARAMETERS:
  4065. * *cd - pointer to core data
  4066. * read_offset - read pointer offset
  4067. * read_length - length of data to read
  4068. * test_id - enumerated test ID to read selftest results from
  4069. * *status - pointer to store the read response status
  4070. * *actual_read_len - pointer to store data length actually read
  4071. * *status - pointer to where the cmd response statas is stored
  4072. ******************************************************************************/
  4073. static int pt_pip_get_selftest_result(
  4074. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4075. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4076. {
  4077. int rc;
  4078. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4079. if (rc < 0) {
  4080. pt_debug(cd->dev, DL_ERROR,
  4081. "%s: fail get exclusive ex=%p own=%p\n",
  4082. __func__, cd->exclusive_dev, cd->dev);
  4083. return rc;
  4084. }
  4085. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4086. read_length, test_id, status, actual_read_len, data);
  4087. if (release_exclusive(cd, cd->dev) < 0)
  4088. pt_debug(cd->dev, DL_ERROR,
  4089. "%s: fail to release exclusive\n", __func__);
  4090. return rc;
  4091. }
  4092. /*******************************************************************************
  4093. * FUNCTION: _pt_request_pip_get_selftest_result
  4094. *
  4095. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4096. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4097. *
  4098. * RETURN:
  4099. * 0 = success
  4100. * !0 = failure
  4101. *
  4102. * PARAMETERS:
  4103. * *dev - pointer to device structure
  4104. * protect - flag to call protected or non-protected
  4105. * read_offset - read pointer offset
  4106. * read_length - length of data to read
  4107. * test_id - enumerated test ID to read selftest results from
  4108. * *status - pointer to store the read response status
  4109. * *actual_read_len - pointer to store data length actually read
  4110. * *data - pointer to where the data read is stored
  4111. ******************************************************************************/
  4112. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4113. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4114. u8 *status, u16 *actual_read_len, u8 *data)
  4115. {
  4116. struct pt_core_data *cd = dev_get_drvdata(dev);
  4117. if (protect)
  4118. return pt_pip_get_selftest_result(cd, read_offset,
  4119. read_length, test_id, status, actual_read_len,
  4120. data);
  4121. return pt_pip_get_selftest_result_(cd, read_offset,
  4122. read_length, test_id, status, actual_read_len,
  4123. data);
  4124. }
  4125. /*******************************************************************************
  4126. * FUNCTION: _pt_pip_load_self_test_param
  4127. *
  4128. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4129. * to load parameters needed by a self test
  4130. *
  4131. * RETURN:
  4132. * 0 = success
  4133. * !0 = failure
  4134. *
  4135. * PARAMETERS:
  4136. * *cd - pointer to core data
  4137. * self_test_id - enumerated test ID for which the parmeters belong
  4138. * load_offset - mem offset to where to load parameters
  4139. * load_length - length of parameter data to load
  4140. * *parameters - pointer to list of parameter data
  4141. * *status - pointer to store the response status
  4142. * *ret_test_id - pointer to returned test id the parameters were stored
  4143. * *act_load_len - pointer to store the actual load length that was writen
  4144. ******************************************************************************/
  4145. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4146. u8 self_test_id, u16 load_offset, u16 load_length,
  4147. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4148. {
  4149. int rc = 0;
  4150. int i;
  4151. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4152. struct pt_hid_output hid_output = {
  4153. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4154. .write_length = 5 + load_length,
  4155. .write_buf = write_buf,
  4156. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4157. };
  4158. write_buf[0] = LOW_BYTE(load_offset);
  4159. write_buf[1] = HI_BYTE(load_offset);
  4160. write_buf[2] = LOW_BYTE(load_length);
  4161. write_buf[3] = HI_BYTE(load_length);
  4162. write_buf[4] = self_test_id;
  4163. for (i = 0; i < load_length; i++)
  4164. write_buf[i + 5] = parameters[i];
  4165. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4166. if (rc)
  4167. return rc;
  4168. if (status)
  4169. *status = cd->response_buf[5];
  4170. if (ret_test_id)
  4171. *ret_test_id = cd->response_buf[6];
  4172. if (act_load_len)
  4173. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4174. return rc;
  4175. }
  4176. /*******************************************************************************
  4177. * FUNCTION: pt_pip_load_self_test_param
  4178. *
  4179. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4180. * access to the DUT.
  4181. *
  4182. * RETURN:
  4183. * 0 = success
  4184. * !0 = failure
  4185. *
  4186. * PARAMETERS:
  4187. * *cd - pointer to core data
  4188. * self_test_id - enumerated test ID for which the parmeters belong
  4189. * load_offset - mem offset to where to load parameters
  4190. * load_length - length of parameter data to load
  4191. * *parameters - pointer to list of parameter data
  4192. * *status - pointer to store the response status
  4193. * *ret_test_id - pointer to returned test id the parameters were stored
  4194. * *act_load_len - pointer to store the actual load length that was writen
  4195. ******************************************************************************/
  4196. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4197. u8 self_test_id, u16 load_offset, u16 load_length,
  4198. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4199. {
  4200. int rc;
  4201. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4202. if (rc < 0) {
  4203. pt_debug(cd->dev, DL_ERROR,
  4204. "%s: fail get exclusive ex=%p own=%p\n",
  4205. __func__, cd->exclusive_dev, cd->dev);
  4206. return rc;
  4207. }
  4208. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4209. load_length, parameters, status, ret_test_id, act_load_len);
  4210. if (release_exclusive(cd, cd->dev) < 0)
  4211. pt_debug(cd->dev, DL_ERROR,
  4212. "%s: fail to release exclusive\n", __func__);
  4213. return rc;
  4214. }
  4215. /*******************************************************************************
  4216. * FUNCTION: _pt_request_pip_load_self_test_param
  4217. *
  4218. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4219. * calls to the protected or unprotected call to
  4220. * pt_pip_load_self_test_param
  4221. *
  4222. * RETURN:
  4223. * 0 = success
  4224. * !0 = failure
  4225. *
  4226. * PARAMETERS:
  4227. * *dev - pointer to device structure
  4228. * protect - flag to call protected or non-protected
  4229. * self_test_id - enumerated test ID for which the parmeters belong
  4230. * load_offset - mem offset to where to load parameters
  4231. * load_length - length of parameter data to load
  4232. * *parameters - pointer to list of parameter data
  4233. * *status - pointer to store the response status
  4234. * *ret_test_id - pointer to returned test id the parameters were stored
  4235. * *act_load_len - pointer to store the actual load length that was writen
  4236. ******************************************************************************/
  4237. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4238. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4239. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4240. {
  4241. struct pt_core_data *cd = dev_get_drvdata(dev);
  4242. if (protect)
  4243. return pt_pip_load_self_test_param(cd, self_test_id,
  4244. load_offset, load_length, parameters, status, ret_test_id,
  4245. act_load_len);
  4246. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4247. load_length, parameters, status, ret_test_id, act_load_len);
  4248. }
  4249. /*******************************************************************************
  4250. * FUNCTION: pt_pip_calibrate_ext_
  4251. *
  4252. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4253. * for the response
  4254. *
  4255. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4256. *
  4257. * RETURN:
  4258. * 0 = success
  4259. * !0 = failure
  4260. *
  4261. * PARAMETERS:
  4262. * *cd - pointer to core data
  4263. * *cal_data - pointer to extended calibration data structure
  4264. * *status - pointer to where the command response status is stored
  4265. ******************************************************************************/
  4266. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4267. struct pt_cal_ext_data *cal_data, u8 *status)
  4268. {
  4269. int rc = 0;
  4270. int write_length = 4;
  4271. u8 write_buf[4];
  4272. u16 size = 0;
  4273. unsigned short crc = 0;
  4274. struct pt_hid_output hid_output = {
  4275. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4276. .write_length = write_length,
  4277. .write_buf = write_buf,
  4278. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4279. };
  4280. if (cal_data == NULL)
  4281. return -EINVAL;
  4282. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4283. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4284. if (rc)
  4285. return rc;
  4286. if (status)
  4287. *status = cd->response_buf[5];
  4288. /*
  4289. * When doing a calibration on a flashless DUT, save CAL data in
  4290. * the TTDL cache on any successful calibration
  4291. */
  4292. if (*status == 0 && cd->cal_cache_in_host) {
  4293. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4294. __func__);
  4295. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4296. &size, &crc);
  4297. if (rc)
  4298. pt_debug(cd->dev, DL_ERROR,
  4299. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4300. else
  4301. pt_debug(cd->dev, DL_INFO,
  4302. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4303. __func__, crc, size);
  4304. }
  4305. return 0;
  4306. }
  4307. /*******************************************************************************
  4308. * FUNCTION: pt_pip_calibrate_ext
  4309. *
  4310. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4311. * DUT
  4312. *
  4313. * RETURN:
  4314. * 0 = success
  4315. * !0 = failure
  4316. *
  4317. * PARAMETERS:
  4318. * *cd - pointer to core data
  4319. * *cal_data - pointer to extended calibration data structure
  4320. * *status - pointer to where the command response status is stored
  4321. ******************************************************************************/
  4322. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4323. struct pt_cal_ext_data *cal_data, u8 *status)
  4324. {
  4325. int rc;
  4326. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4327. if (rc < 0) {
  4328. pt_debug(cd->dev, DL_ERROR,
  4329. "%s: fail get exclusive ex=%p own=%p\n",
  4330. __func__, cd->exclusive_dev, cd->dev);
  4331. return rc;
  4332. }
  4333. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4334. if (release_exclusive(cd, cd->dev) < 0)
  4335. pt_debug(cd->dev, DL_ERROR,
  4336. "%s: fail to release exclusive\n", __func__);
  4337. return rc;
  4338. }
  4339. /*******************************************************************************
  4340. * FUNCTION: _pt_request_pip_calibrate_ext
  4341. *
  4342. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4343. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4344. *
  4345. * RETURN:
  4346. * 0 = success
  4347. * !0 = failure
  4348. *
  4349. * PARAMETERS:
  4350. * *dev - pointer to device structure
  4351. * protect - flag to call protected or non-protected
  4352. * *cal_data - pointer to extended calibration data structure
  4353. * *status - pointer to where the command response status is stored
  4354. ******************************************************************************/
  4355. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4356. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4357. {
  4358. struct pt_core_data *cd = dev_get_drvdata(dev);
  4359. if (protect)
  4360. return pt_pip_calibrate_ext(cd, cal_data, status);
  4361. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4362. }
  4363. /*******************************************************************************
  4364. * FUNCTION: pt_pip_calibrate_idacs_
  4365. *
  4366. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4367. * for the response
  4368. *
  4369. * RETURN:
  4370. * 0 = success
  4371. * !0 = failure
  4372. *
  4373. * PARAMETERS:
  4374. * *cd - pointer to core data
  4375. * mode - sense mode to calibrate (0-5)
  4376. * *status - pointer to where the command response status is stored
  4377. ******************************************************************************/
  4378. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4379. u8 mode, u8 *status)
  4380. {
  4381. int rc = 0;
  4382. int write_length = 1;
  4383. u8 write_buf[1];
  4384. u8 cmd_offset = 0;
  4385. struct pt_hid_output hid_output = {
  4386. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4387. .write_length = write_length,
  4388. .write_buf = write_buf,
  4389. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4390. };
  4391. write_buf[cmd_offset++] = mode;
  4392. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4393. if (rc)
  4394. return rc;
  4395. *status = cd->response_buf[5];
  4396. if (*status)
  4397. return -EINVAL;
  4398. return 0;
  4399. }
  4400. /*******************************************************************************
  4401. * FUNCTION: pt_pip_calibrate_idacs
  4402. *
  4403. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4404. * access to the DUT
  4405. *
  4406. * RETURN:
  4407. * 0 = success
  4408. * !0 = failure
  4409. *
  4410. * PARAMETERS:
  4411. * *cd - pointer to core data
  4412. * mode - sense mode to calibrate (0-5)
  4413. * *status - pointer to where the command response status is stored
  4414. ******************************************************************************/
  4415. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4416. u8 mode, u8 *status)
  4417. {
  4418. int rc;
  4419. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4420. if (rc < 0) {
  4421. pt_debug(cd->dev, DL_ERROR,
  4422. "%s: fail get exclusive ex=%p own=%p\n",
  4423. __func__, cd->exclusive_dev, cd->dev);
  4424. return rc;
  4425. }
  4426. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4427. if (release_exclusive(cd, cd->dev) < 0)
  4428. pt_debug(cd->dev, DL_ERROR,
  4429. "%s: fail to release exclusive\n", __func__);
  4430. return rc;
  4431. }
  4432. /*******************************************************************************
  4433. * FUNCTION: _pt_request_pip_calibrate_idacs
  4434. *
  4435. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4436. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4437. *
  4438. * RETURN:
  4439. * 0 = success
  4440. * !0 = failure
  4441. *
  4442. * PARAMETERS:
  4443. * *dev - pointer to device structure
  4444. * protect - flag to call protected or non-protected
  4445. * mode - sense mode to calibrate (0-5)
  4446. * *status - pointer to where the command response status is stored
  4447. ******************************************************************************/
  4448. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4449. int protect, u8 mode, u8 *status)
  4450. {
  4451. struct pt_core_data *cd = dev_get_drvdata(dev);
  4452. if (protect)
  4453. return pt_pip_calibrate_idacs(cd, mode, status);
  4454. return pt_pip_calibrate_idacs_(cd, mode, status);
  4455. }
  4456. /*******************************************************************************
  4457. * FUNCTION: pt_hid_output_initialize_baselines_
  4458. *
  4459. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4460. * waiting for the response.
  4461. *
  4462. * RETURN:
  4463. * 0 = success
  4464. * !0 = failure
  4465. *
  4466. * PARAMETERS:
  4467. * *cd - pointer to core data
  4468. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4469. * each or together with a single command.
  4470. * *status - pointer to where the command response status is stored
  4471. ******************************************************************************/
  4472. static int pt_hid_output_initialize_baselines_(
  4473. struct pt_core_data *cd, u8 test_id, u8 *status)
  4474. {
  4475. int rc = 0;
  4476. int write_length = 1;
  4477. u8 write_buf[1];
  4478. u8 cmd_offset = 0;
  4479. struct pt_hid_output hid_output = {
  4480. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4481. .write_length = write_length,
  4482. .write_buf = write_buf,
  4483. };
  4484. write_buf[cmd_offset++] = test_id;
  4485. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4486. if (rc)
  4487. return rc;
  4488. *status = cd->response_buf[5];
  4489. if (*status)
  4490. return -EINVAL;
  4491. return rc;
  4492. }
  4493. /*******************************************************************************
  4494. * FUNCTION: pt_hid_output_initialize_baselines
  4495. *
  4496. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4497. * access to the DUT
  4498. *
  4499. * RETURN:
  4500. * 0 = success
  4501. * !0 = failure
  4502. *
  4503. * PARAMETERS:
  4504. * *cd - pointer to core data
  4505. * test_id - enumerated ID against which to initialize the baseline
  4506. * *status - pointer to where the command response status is stored
  4507. ******************************************************************************/
  4508. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4509. u8 test_id, u8 *status)
  4510. {
  4511. int rc;
  4512. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4513. if (rc < 0) {
  4514. pt_debug(cd->dev, DL_ERROR,
  4515. "%s: fail get exclusive ex=%p own=%p\n",
  4516. __func__, cd->exclusive_dev, cd->dev);
  4517. return rc;
  4518. }
  4519. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4520. if (release_exclusive(cd, cd->dev) < 0)
  4521. pt_debug(cd->dev, DL_ERROR,
  4522. "%s: fail to release exclusive\n", __func__);
  4523. return rc;
  4524. }
  4525. /*******************************************************************************
  4526. * FUNCTION: _pt_request_pip_initialize_baselines
  4527. *
  4528. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4529. * calls to the protected or unprotected call to
  4530. * pt_pip_initialize_baselines
  4531. *
  4532. * RETURN:
  4533. * 0 = success
  4534. * !0 = failure
  4535. *
  4536. * PARAMETERS:
  4537. * *dev - pointer to device structure
  4538. * protect - flag to call protected or non-protected
  4539. * test_id - enumerated ID against which to initialize the baseline
  4540. * *status - pointer to where the command response status is stored
  4541. ******************************************************************************/
  4542. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4543. int protect, u8 test_id, u8 *status)
  4544. {
  4545. struct pt_core_data *cd = dev_get_drvdata(dev);
  4546. if (protect)
  4547. return pt_hid_output_initialize_baselines(cd, test_id,
  4548. status);
  4549. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4550. }
  4551. /*******************************************************************************
  4552. * FUNCTION: pt_hid_output_exec_panel_scan_
  4553. *
  4554. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4555. * the response
  4556. *
  4557. * RETURN:
  4558. * 0 = success
  4559. * !0 = failure
  4560. *
  4561. * PARAMETERS:
  4562. * *cd - pointer to core data
  4563. ******************************************************************************/
  4564. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4565. {
  4566. struct pt_hid_output hid_output = {
  4567. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4568. };
  4569. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4570. }
  4571. /*******************************************************************************
  4572. * FUNCTION: pt_hid_output_exec_panel_scan
  4573. *
  4574. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4575. * access to the DUT.
  4576. *
  4577. * RETURN:
  4578. * 0 = success
  4579. * !0 = failure
  4580. *
  4581. * PARAMETERS:
  4582. * *cd - pointer to core data
  4583. ******************************************************************************/
  4584. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4585. {
  4586. int rc;
  4587. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4588. if (rc < 0) {
  4589. pt_debug(cd->dev, DL_ERROR,
  4590. "%s: fail get exclusive ex=%p own=%p\n",
  4591. __func__, cd->exclusive_dev, cd->dev);
  4592. return rc;
  4593. }
  4594. rc = pt_hid_output_exec_panel_scan_(cd);
  4595. if (release_exclusive(cd, cd->dev) < 0)
  4596. pt_debug(cd->dev, DL_ERROR,
  4597. "%s: fail to release exclusive\n", __func__);
  4598. return rc;
  4599. }
  4600. /*******************************************************************************
  4601. * FUNCTION: pt_pip2_exec_panel_scan_
  4602. *
  4603. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4604. * the response
  4605. *
  4606. * RETURN:
  4607. * 0 = success
  4608. * !0 = failure
  4609. *
  4610. * PARAMETERS:
  4611. * *cd - pointer to core data
  4612. * scan_type - type of panel scan to perform (PIP2 only)
  4613. ******************************************************************************/
  4614. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4615. {
  4616. int rc = 0;
  4617. u8 data[2];
  4618. u8 read_buf[10];
  4619. u16 actual_read_len;
  4620. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4621. __func__, scan_type);
  4622. data[0] = scan_type;
  4623. rc = _pt_request_pip2_send_cmd(cd->dev,
  4624. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4625. data, 1, read_buf, &actual_read_len);
  4626. if (rc) {
  4627. pt_debug(cd->dev, DL_ERROR,
  4628. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4629. __func__, scan_type, rc);
  4630. }
  4631. return rc;
  4632. }
  4633. /*******************************************************************************
  4634. * FUNCTION: pt_pip2_exec_panel_scan
  4635. *
  4636. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4637. * access to the DUT.
  4638. *
  4639. * RETURN:
  4640. * 0 = success
  4641. * !0 = failure
  4642. *
  4643. * PARAMETERS:
  4644. * *cd - pointer to core data
  4645. * scan_type - type of panel scan to perform (PIP2 only)
  4646. ******************************************************************************/
  4647. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4648. {
  4649. int rc;
  4650. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4651. if (rc < 0) {
  4652. pt_debug(cd->dev, DL_ERROR,
  4653. "%s: fail get exclusive ex=%p own=%p\n",
  4654. __func__, cd->exclusive_dev, cd->dev);
  4655. return rc;
  4656. }
  4657. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4658. if (release_exclusive(cd, cd->dev) < 0)
  4659. pt_debug(cd->dev, DL_ERROR,
  4660. "%s: fail to release exclusive\n", __func__);
  4661. return rc;
  4662. }
  4663. /*******************************************************************************
  4664. * FUNCTION: _pt_request_pip_exec_panel_scan
  4665. *
  4666. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4667. * calls to the protected or unprotected call to
  4668. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4669. *
  4670. * RETURN:
  4671. * 0 = success
  4672. * !0 = failure
  4673. *
  4674. * PARAMETERS:
  4675. * *dev - pointer to device structure
  4676. * protect - flag to call protected or non-protected
  4677. * scan_type - type of panel scan to perform (PIP2 only)
  4678. ******************************************************************************/
  4679. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4680. int protect, u8 scan_type)
  4681. {
  4682. struct pt_core_data *cd = dev_get_drvdata(dev);
  4683. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4684. if (protect)
  4685. return pt_pip2_exec_panel_scan(cd, scan_type);
  4686. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4687. }
  4688. if (protect)
  4689. return pt_hid_output_exec_panel_scan(cd);
  4690. return pt_hid_output_exec_panel_scan_(cd);
  4691. }
  4692. /*******************************************************************************
  4693. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4694. *
  4695. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4696. * to retrieve the specified data type for a the last successful Execute
  4697. * Panel Scan command.
  4698. *
  4699. * RETURN:
  4700. * 0 = success
  4701. * !0 = failure
  4702. *
  4703. * PARAMETERS:
  4704. * *dev - pointer to device structure
  4705. * protect - flag to call protected or non-protected
  4706. * read_offset - read pointer offset
  4707. * read_count - length of data to read
  4708. * data_id - enumerated test ID to read selftest results from
  4709. * *response - pointer to store the read response status
  4710. * *config - pointer to store config data
  4711. * *actual_read_len - pointer to store data length actually read
  4712. * *read_buf - pointer to the read buffer
  4713. ******************************************************************************/
  4714. static int pt_hid_output_retrieve_panel_scan_(
  4715. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4716. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4717. u8 *read_buf)
  4718. {
  4719. int status;
  4720. u8 read_data_id;
  4721. int rc = 0;
  4722. int write_length = 5;
  4723. u8 write_buf[5];
  4724. u8 cmd_offset = 0;
  4725. u8 data_elem_size;
  4726. int size;
  4727. int data_size;
  4728. struct pt_hid_output hid_output = {
  4729. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4730. .write_length = write_length,
  4731. .write_buf = write_buf,
  4732. };
  4733. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4734. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4735. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4736. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4737. write_buf[cmd_offset++] = data_id;
  4738. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4739. if (rc)
  4740. return rc;
  4741. status = cd->response_buf[5];
  4742. if (status)
  4743. return -EINVAL;
  4744. read_data_id = cd->response_buf[6];
  4745. if (read_data_id != data_id)
  4746. return -EPROTO;
  4747. size = get_unaligned_le16(&cd->response_buf[0]);
  4748. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4749. *config = cd->response_buf[9];
  4750. data_elem_size = *config & 0x07;
  4751. data_size = *actual_read_len * data_elem_size;
  4752. if (read_buf)
  4753. memcpy(read_buf, &cd->response_buf[10], data_size);
  4754. if (response)
  4755. memcpy(response, cd->response_buf, size);
  4756. return rc;
  4757. }
  4758. /*******************************************************************************
  4759. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4760. *
  4761. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4762. * access to the DUT.
  4763. *
  4764. * RETURN:
  4765. * 0 = success
  4766. * !0 = failure
  4767. *
  4768. * PARAMETERS:
  4769. * *dev - pointer to device structure
  4770. * protect - flag to call protected or non-protected
  4771. * read_offset - read pointer offset
  4772. * read_count - length of data to read
  4773. * data_id - enumerated test ID to read selftest results from
  4774. * *response - pointer to store the read response status
  4775. * *config - pointer to store config data
  4776. * *actual_read_len - pointer to store data length actually read
  4777. * *read_buf - pointer to the read buffer
  4778. ******************************************************************************/
  4779. static int pt_hid_output_retrieve_panel_scan(
  4780. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4781. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4782. u8 *read_buf)
  4783. {
  4784. int rc;
  4785. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4786. if (rc < 0) {
  4787. pt_debug(cd->dev, DL_ERROR,
  4788. "%s: fail get exclusive ex=%p own=%p\n",
  4789. __func__, cd->exclusive_dev, cd->dev);
  4790. return rc;
  4791. }
  4792. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4793. read_count, data_id, response, config,
  4794. actual_read_len, read_buf);
  4795. if (release_exclusive(cd, cd->dev) < 0)
  4796. pt_debug(cd->dev, DL_ERROR,
  4797. "%s: fail to release exclusive\n", __func__);
  4798. return rc;
  4799. }
  4800. /*******************************************************************************
  4801. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4802. *
  4803. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4804. * calls to the protected or unprotected call to
  4805. * pt_hid_output_retrieve_panel_scan
  4806. *
  4807. * RETURN:
  4808. * 0 = success
  4809. * !0 = failure
  4810. *
  4811. * PARAMETERS:
  4812. * *dev - pointer to device structure
  4813. * protect - flag to call protected or non-protected
  4814. * read_offset - read pointer offset
  4815. * read_count - length of data to read
  4816. * data_id - enumerated test ID to read selftest results from
  4817. * *response - pointer to store the read response status
  4818. * *config - pointer to store config data
  4819. * *actual_read_len - pointer to store data length actually read
  4820. * *read_buf - pointer to the read buffer
  4821. ******************************************************************************/
  4822. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4823. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4824. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4825. {
  4826. struct pt_core_data *cd = dev_get_drvdata(dev);
  4827. if (protect)
  4828. return pt_hid_output_retrieve_panel_scan(cd,
  4829. read_offset, read_count, data_id, response,
  4830. config, actual_read_len, read_buf);
  4831. return pt_hid_output_retrieve_panel_scan_(cd,
  4832. read_offset, read_count, data_id, response,
  4833. config, actual_read_len, read_buf);
  4834. }
  4835. /*******************************************************************************
  4836. * FUNCTION: _pt_request_pip_user_cmd
  4837. *
  4838. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4839. * calls to the protected or unprotected call to
  4840. * pt_hid_output_user_cmd
  4841. *
  4842. * RETURN:
  4843. * 0 = success
  4844. * !0 = failure
  4845. *
  4846. * PARAMETERS:
  4847. * *dev - pointer to device structure
  4848. * protect - flag to call protected or non-protected
  4849. * read_len - length of data to read
  4850. * *read_buf - pointer to store read data
  4851. * write_len - length of data to write
  4852. * *write_buf - pointer to buffer to write
  4853. * *actual_read_len - pointer to store data length actually read
  4854. ******************************************************************************/
  4855. static int _pt_request_pip_user_cmd(struct device *dev,
  4856. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4857. u8 *write_buf, u16 *actual_read_len)
  4858. {
  4859. struct pt_core_data *cd = dev_get_drvdata(dev);
  4860. if (protect)
  4861. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4862. write_len, write_buf, actual_read_len);
  4863. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4864. write_len, write_buf, actual_read_len);
  4865. }
  4866. /*******************************************************************************
  4867. * FUNCTION: pt_hid_output_bl_get_information_
  4868. *
  4869. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4870. * DUT to retrieve bootloader version and chip identification information.
  4871. *
  4872. * RETURN:
  4873. * 0 = success
  4874. * !0 = failure
  4875. *
  4876. * PARAMETERS:
  4877. * *cd - pointer to core data
  4878. * *return_data - pointer to store the return data
  4879. *****************************************************************************/
  4880. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4881. u8 *return_data)
  4882. {
  4883. int rc;
  4884. int data_len;
  4885. struct pt_hid_output hid_output = {
  4886. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4887. };
  4888. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4889. if (rc)
  4890. return rc;
  4891. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4892. if (!data_len)
  4893. return -EPROTO;
  4894. memcpy(return_data, &cd->response_buf[8], data_len);
  4895. return 0;
  4896. }
  4897. /*******************************************************************************
  4898. * FUNCTION: pt_hid_output_bl_get_information
  4899. *
  4900. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4901. * access to the DUT.
  4902. *
  4903. * RETURN:
  4904. * 0 = success
  4905. * !0 = failure
  4906. *
  4907. * PARAMETERS:
  4908. * *cd - pointer to core data
  4909. * *return_data - pointer to store the return data
  4910. ******************************************************************************/
  4911. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4912. u8 *return_data)
  4913. {
  4914. int rc;
  4915. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4916. if (rc < 0) {
  4917. pt_debug(cd->dev, DL_ERROR,
  4918. "%s: fail get exclusive ex=%p own=%p\n",
  4919. __func__, cd->exclusive_dev, cd->dev);
  4920. return rc;
  4921. }
  4922. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4923. if (release_exclusive(cd, cd->dev) < 0)
  4924. pt_debug(cd->dev, DL_ERROR,
  4925. "%s: fail to release exclusive\n", __func__);
  4926. return rc;
  4927. }
  4928. /*******************************************************************************
  4929. * FUNCTION: _pt_request_pip_bl_get_information
  4930. *
  4931. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4932. * calls to the protected or unprotected call to
  4933. * pt_hid_output_bl_get_information
  4934. *
  4935. * RETURN:
  4936. * 0 = success
  4937. * !0 = failure
  4938. *
  4939. * PARAMETERS:
  4940. * *dev - pointer to device structure
  4941. * protect - flag to call protected or non-protected
  4942. * *return_data - pointer to store bl data
  4943. ******************************************************************************/
  4944. static int _pt_request_pip_bl_get_information(struct device *dev,
  4945. int protect, u8 *return_data)
  4946. {
  4947. struct pt_core_data *cd = dev_get_drvdata(dev);
  4948. if (protect)
  4949. return pt_hid_output_bl_get_information(cd, return_data);
  4950. return pt_hid_output_bl_get_information_(cd, return_data);
  4951. }
  4952. /*******************************************************************************
  4953. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4954. *
  4955. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4956. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4957. * and Design Data block in flash and enables the host to execute the Program
  4958. * and Verify Row command to bootload the application image and data.
  4959. *
  4960. * RETURN:
  4961. * 0 = success
  4962. * !0 = failure
  4963. *
  4964. * PARAMETERS:
  4965. * *cd - pointer to core data
  4966. * protect - flag to call protected or non-protected
  4967. * key_size - size of key
  4968. * *key_buf - pointer to key data to allow operation
  4969. * row_size - size of the meta data row
  4970. * *metadata_row_buf - pointer to meta data to write
  4971. ******************************************************************************/
  4972. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4973. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4974. {
  4975. u16 write_length = key_size + row_size;
  4976. u8 *write_buf;
  4977. int rc = 0;
  4978. struct pt_hid_output hid_output = {
  4979. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4980. .write_length = write_length,
  4981. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4982. };
  4983. write_buf = kzalloc(write_length, GFP_KERNEL);
  4984. if (!write_buf)
  4985. return -ENOMEM;
  4986. hid_output.write_buf = write_buf;
  4987. if (key_size)
  4988. memcpy(write_buf, key_buf, key_size);
  4989. if (row_size)
  4990. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4991. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4992. kfree(write_buf);
  4993. return rc;
  4994. }
  4995. /*******************************************************************************
  4996. * FUNCTION: pt_hid_output_bl_initiate_bl
  4997. *
  4998. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4999. * access to the DUT.
  5000. *
  5001. * RETURN:
  5002. * 0 = success
  5003. * !0 = failure
  5004. *
  5005. * PARAMETERS:
  5006. * *cd - pointer to core data
  5007. * key_size - size of key
  5008. * *key_buf - pointer to key data to allow operation
  5009. * row_size - size of the meta data row
  5010. * *metadata_row_buf - pointer to meta data to write
  5011. ******************************************************************************/
  5012. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5013. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5014. {
  5015. int rc;
  5016. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5017. if (rc < 0) {
  5018. pt_debug(cd->dev, DL_ERROR,
  5019. "%s: fail get exclusive ex=%p own=%p\n",
  5020. __func__, cd->exclusive_dev, cd->dev);
  5021. return rc;
  5022. }
  5023. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5024. row_size, metadata_row_buf);
  5025. if (release_exclusive(cd, cd->dev) < 0)
  5026. pt_debug(cd->dev, DL_ERROR,
  5027. "%s: fail to release exclusive\n", __func__);
  5028. return rc;
  5029. }
  5030. /*******************************************************************************
  5031. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5032. *
  5033. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5034. * calls to the protected or unprotected call to
  5035. * pt_hid_output_bl_initiate_bl
  5036. *
  5037. * RETURN:
  5038. * 0 = success
  5039. * !0 = failure
  5040. *
  5041. * PARAMETERS:
  5042. * *dev - pointer to device structure
  5043. * protect - flag to call protected or non-protected
  5044. * key_size - size of key
  5045. * *key_buf - pointer to key data to allow operation
  5046. * row_size - size of the meta data row
  5047. * *metadata_row_buf - pointer to meta data to write
  5048. ******************************************************************************/
  5049. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5050. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5051. u8 *metadata_row_buf)
  5052. {
  5053. struct pt_core_data *cd = dev_get_drvdata(dev);
  5054. if (protect)
  5055. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5056. row_size, metadata_row_buf);
  5057. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5058. row_size, metadata_row_buf);
  5059. }
  5060. /*******************************************************************************
  5061. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5062. *
  5063. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5064. * and program a 128-byte row into the flash, and then verifies written data.
  5065. *
  5066. * RETURN:
  5067. * 0 = success
  5068. * !0 = failure
  5069. *
  5070. * PARAMETERS:
  5071. * *cd - pointer to core data
  5072. * data_len - length of data_buf
  5073. * *data_buf - firmware image to program
  5074. ******************************************************************************/
  5075. static int pt_hid_output_bl_program_and_verify_(
  5076. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5077. {
  5078. struct pt_hid_output hid_output = {
  5079. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5080. .write_length = data_len,
  5081. .write_buf = data_buf,
  5082. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5083. };
  5084. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5085. }
  5086. /*******************************************************************************
  5087. * FUNCTION: pt_hid_output_bl_program_and_verify
  5088. *
  5089. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5090. * access to the DUT.
  5091. *
  5092. * RETURN:
  5093. * 0 = success
  5094. * !0 = failure
  5095. *
  5096. * PARAMETERS:
  5097. * *cd - pointer to core data
  5098. * data_len - length of data_buf
  5099. * *data_buf - firmware image to program
  5100. ******************************************************************************/
  5101. static int pt_hid_output_bl_program_and_verify(
  5102. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5103. {
  5104. int rc;
  5105. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5106. if (rc < 0) {
  5107. pt_debug(cd->dev, DL_ERROR,
  5108. "%s: fail get exclusive ex=%p own=%p\n",
  5109. __func__, cd->exclusive_dev, cd->dev);
  5110. return rc;
  5111. }
  5112. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5113. if (release_exclusive(cd, cd->dev) < 0)
  5114. pt_debug(cd->dev, DL_ERROR,
  5115. "%s: fail to release exclusive\n", __func__);
  5116. return rc;
  5117. }
  5118. /*******************************************************************************
  5119. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5120. *
  5121. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5122. * to request to have the BL program and verify a FW image
  5123. *
  5124. * RETURN:
  5125. * 0 = success
  5126. * !0 = failure
  5127. *
  5128. * PARAMETERS:
  5129. * *dev - pointer to device structure
  5130. * protect - boolean to determine to call the protected function
  5131. * data_len - length of data_buf
  5132. * *data_buf - firmware image to program
  5133. ******************************************************************************/
  5134. static int _pt_request_pip_bl_program_and_verify(
  5135. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5136. {
  5137. struct pt_core_data *cd = dev_get_drvdata(dev);
  5138. if (protect)
  5139. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5140. data_buf);
  5141. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5142. data_buf);
  5143. }
  5144. /*******************************************************************************
  5145. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5146. *
  5147. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5148. * perform a full verification of the application integrity by calculating the
  5149. * CRC of the image in flash and compare it to the expected CRC stored in the
  5150. * Metadata row.
  5151. *
  5152. * RETURN:
  5153. * 0 = success
  5154. * !0 = failure
  5155. *
  5156. * PARAMETERS:
  5157. * *cd - pointer to core data
  5158. * *result - pointer to store result
  5159. ******************************************************************************/
  5160. static int pt_hid_output_bl_verify_app_integrity_(
  5161. struct pt_core_data *cd, u8 *result)
  5162. {
  5163. int rc;
  5164. struct pt_hid_output hid_output = {
  5165. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5166. };
  5167. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5168. if (rc) {
  5169. *result = 0;
  5170. return rc;
  5171. }
  5172. *result = cd->response_buf[8];
  5173. return 0;
  5174. }
  5175. /*******************************************************************************
  5176. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5177. *
  5178. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5179. * exclusive access to the DUT.
  5180. *
  5181. * RETURN:
  5182. * 0 = success
  5183. * !0 = failure
  5184. *
  5185. * PARAMETERS:
  5186. * *cd - pointer to core data
  5187. * *result - pointer to store result
  5188. ******************************************************************************/
  5189. static int pt_hid_output_bl_verify_app_integrity(
  5190. struct pt_core_data *cd, u8 *result)
  5191. {
  5192. int rc;
  5193. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5194. if (rc < 0) {
  5195. pt_debug(cd->dev, DL_ERROR,
  5196. "%s: fail get exclusive ex=%p own=%p\n",
  5197. __func__, cd->exclusive_dev, cd->dev);
  5198. return rc;
  5199. }
  5200. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5201. if (release_exclusive(cd, cd->dev) < 0)
  5202. pt_debug(cd->dev, DL_ERROR,
  5203. "%s: fail to release exclusive\n", __func__);
  5204. return rc;
  5205. }
  5206. /*******************************************************************************
  5207. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5208. *
  5209. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5210. * to request to have the BL verify the application integrity (PIP1.x only)
  5211. *
  5212. * RETURN:
  5213. * 0 = success
  5214. * !0 = failure
  5215. *
  5216. * PARAMETERS:
  5217. * *dev - pointer to device structure
  5218. * protect - boolean to determine to call the protected function
  5219. * *result - pointer to store result
  5220. ******************************************************************************/
  5221. static int _pt_request_pip_bl_verify_app_integrity(
  5222. struct device *dev, int protect, u8 *result)
  5223. {
  5224. struct pt_core_data *cd = dev_get_drvdata(dev);
  5225. if (protect)
  5226. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5227. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5228. }
  5229. /*******************************************************************************
  5230. * FUNCTION: pt_hid_output_bl_launch_app_
  5231. *
  5232. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5233. * application from bootloader (PIP1.x only).
  5234. *
  5235. * RETURN:
  5236. * 0 = success
  5237. * !0 = failure
  5238. *
  5239. * PARAMETERS:
  5240. * *cd - pointer to core data
  5241. ******************************************************************************/
  5242. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5243. {
  5244. struct pt_hid_output hid_output = {
  5245. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5246. .reset_expected = 1,
  5247. };
  5248. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5249. }
  5250. /*******************************************************************************
  5251. * FUNCTION: pt_hid_output_bl_launch_app
  5252. *
  5253. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5254. * to the DUT.
  5255. *
  5256. * RETURN:
  5257. * 0 = success
  5258. * !0 = failure
  5259. *
  5260. * PARAMETERS:
  5261. * *cd - pointer to core data
  5262. ******************************************************************************/
  5263. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5264. {
  5265. int rc;
  5266. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5267. if (rc < 0) {
  5268. pt_debug(cd->dev, DL_ERROR,
  5269. "%s: fail get exclusive ex=%p own=%p\n",
  5270. __func__, cd->exclusive_dev, cd->dev);
  5271. return rc;
  5272. }
  5273. rc = pt_hid_output_bl_launch_app_(cd);
  5274. if (release_exclusive(cd, cd->dev) < 0)
  5275. pt_debug(cd->dev, DL_ERROR,
  5276. "%s: fail to release exclusive\n", __func__);
  5277. return rc;
  5278. }
  5279. /*******************************************************************************
  5280. * FUNCTION: _pt_request_pip_launch_app
  5281. *
  5282. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5283. * to request to have the BL launch the application. (PIP1.x only)
  5284. *
  5285. * RETURN:
  5286. * 0 = success
  5287. * !0 = failure
  5288. *
  5289. * PARAMETERS:
  5290. * *dev - pointer to device structure
  5291. * protect - boolean to determine to call the protected function
  5292. ******************************************************************************/
  5293. static int _pt_request_pip_launch_app(struct device *dev,
  5294. int protect)
  5295. {
  5296. struct pt_core_data *cd = dev_get_drvdata(dev);
  5297. if (protect)
  5298. return pt_hid_output_bl_launch_app(cd);
  5299. return pt_hid_output_bl_launch_app_(cd);
  5300. }
  5301. /*******************************************************************************
  5302. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5303. *
  5304. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5305. * value store in the System Information.
  5306. *
  5307. * RETURN:
  5308. * 0 = success
  5309. * !0 = failure
  5310. *
  5311. * PARAMETERS:
  5312. * *cd - pointer to core data
  5313. * *panel_id - pointer to where the panel ID will be stored
  5314. ******************************************************************************/
  5315. static int pt_hid_output_bl_get_panel_id_(
  5316. struct pt_core_data *cd, u8 *panel_id)
  5317. {
  5318. int rc;
  5319. struct pt_hid_output hid_output = {
  5320. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5321. };
  5322. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5323. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5324. pt_debug(cd->dev, DL_ERROR,
  5325. "%s: Get Panel ID command not supported\n",
  5326. __func__);
  5327. *panel_id = PANEL_ID_NOT_ENABLED;
  5328. return 0;
  5329. } else if (rc < 0) {
  5330. pt_debug(cd->dev, DL_ERROR,
  5331. "%s: Error on Get Panel ID command\n", __func__);
  5332. return rc;
  5333. }
  5334. *panel_id = cd->response_buf[8];
  5335. return 0;
  5336. }
  5337. /*******************************************************************************
  5338. * FUNCTION: pt_hid_output_bl_get_panel_id
  5339. *
  5340. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5341. * to the DUT.
  5342. *
  5343. * RETURN:
  5344. * 0 = success
  5345. * !0 = failure
  5346. *
  5347. * PARAMETERS:
  5348. * *cd - pointer to core data
  5349. * *panel_id - pointer to where the panel ID will be stored
  5350. ******************************************************************************/
  5351. static int pt_hid_output_bl_get_panel_id(
  5352. struct pt_core_data *cd, u8 *panel_id)
  5353. {
  5354. int rc;
  5355. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5356. if (rc < 0) {
  5357. pt_debug(cd->dev, DL_ERROR,
  5358. "%s: fail get exclusive ex=%p own=%p\n",
  5359. __func__, cd->exclusive_dev, cd->dev);
  5360. return rc;
  5361. }
  5362. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5363. if (release_exclusive(cd, cd->dev) < 0)
  5364. pt_debug(cd->dev, DL_ERROR,
  5365. "%s: fail to release exclusive\n", __func__);
  5366. return rc;
  5367. }
  5368. /*******************************************************************************
  5369. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5370. *
  5371. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5372. * to have the BL retrieve the panel ID
  5373. *
  5374. * RETURN:
  5375. * 0 = success
  5376. * !0 = failure
  5377. *
  5378. * PARAMETERS:
  5379. * *dev - pointer to device structure
  5380. * protect - flag to run in protected mode
  5381. * *panel_id - pointer to where the panel ID will be stored
  5382. ******************************************************************************/
  5383. static int _pt_request_pip_bl_get_panel_id(
  5384. struct device *dev, int protect, u8 *panel_id)
  5385. {
  5386. struct pt_core_data *cd = dev_get_drvdata(dev);
  5387. if (protect)
  5388. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5389. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5390. }
  5391. /*******************************************************************************
  5392. * FUNCTION: pt_pip2_get_mode_sysmode_
  5393. *
  5394. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5395. * PIP2 STATUS command.
  5396. *
  5397. * RETURN:
  5398. * 0 = success
  5399. * !0 = failure
  5400. *
  5401. * PARAMETERS:
  5402. * *cd - pointer to core data structure
  5403. * *mode - pointer to store the retrieved mode
  5404. * *sys_mode - pointer to store the FW system mode
  5405. ******************************************************************************/
  5406. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5407. u8 *mode, u8 *sys_mode)
  5408. {
  5409. u16 actual_read_len;
  5410. u8 read_buf[12];
  5411. u8 status, boot;
  5412. int rc = 0;
  5413. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5414. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5415. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5416. __func__, rc);
  5417. if (!rc) {
  5418. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5419. "PIP2 STATUS");
  5420. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5421. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5422. if (sys_mode) {
  5423. if (status == PIP2_RSP_ERR_NONE &&
  5424. boot == PIP2_STATUS_APP_EXEC)
  5425. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5426. else
  5427. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5428. }
  5429. if (mode) {
  5430. if (status == PIP2_RSP_ERR_NONE &&
  5431. boot == PIP2_STATUS_BOOT_EXEC)
  5432. *mode = PT_MODE_BOOTLOADER;
  5433. else if (status == PIP2_RSP_ERR_NONE &&
  5434. boot == PIP2_STATUS_APP_EXEC)
  5435. *mode = PT_MODE_OPERATIONAL;
  5436. else
  5437. *mode = PT_MODE_UNKNOWN;
  5438. }
  5439. } else {
  5440. if (mode)
  5441. *mode = PT_MODE_UNKNOWN;
  5442. if (sys_mode)
  5443. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5444. pt_debug(cd->dev, DL_WARN,
  5445. "%s: Mode and sys_mode could not be determined\n",
  5446. __func__);
  5447. }
  5448. return rc;
  5449. }
  5450. /*******************************************************************************
  5451. * FUNCTION: pt_pip2_get_mode_sysmode
  5452. *
  5453. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5454. * access to the DUT.
  5455. *
  5456. * RETURN:
  5457. * 0 = success
  5458. * !0 = failure
  5459. *
  5460. * PARAMETERS:
  5461. * *cd - pointer to core data structure
  5462. * *mode - pointer to store the retrieved mode
  5463. * *sys_mode - pointer to store the FW system mode
  5464. ******************************************************************************/
  5465. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5466. u8 *mode, u8 *sys_mode)
  5467. {
  5468. int rc;
  5469. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5470. if (rc < 0) {
  5471. pt_debug(cd->dev, DL_ERROR,
  5472. "%s: fail get exclusive ex=%p own=%p\n",
  5473. __func__, cd->exclusive_dev, cd->dev);
  5474. return rc;
  5475. }
  5476. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5477. if (release_exclusive(cd, cd->dev) < 0)
  5478. pt_debug(cd->dev, DL_ERROR,
  5479. "%s: fail to release exclusive\n", __func__);
  5480. return rc;
  5481. }
  5482. /*******************************************************************************
  5483. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5484. *
  5485. * SUMMARY: Function pointer included in core_commands struct for external
  5486. * calls to the protected or unprotected call to
  5487. * pt_pip2_get_mode_sysmode
  5488. *
  5489. * RETURN:
  5490. * 0 = success
  5491. * !0 = failure
  5492. *
  5493. * PARAMETERS:
  5494. * *dev - pointer to device structure
  5495. * *mode - pointer to store the retrieved mode
  5496. * *sys_mode - pointer to store the FW system mode
  5497. ******************************************************************************/
  5498. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5499. int protect, u8 *mode, u8 *sys_mode)
  5500. {
  5501. struct pt_core_data *cd = dev_get_drvdata(dev);
  5502. if (protect)
  5503. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5504. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5505. }
  5506. /*******************************************************************************
  5507. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5508. *
  5509. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5510. * mode only the following PIP commands will be serviced, any other PIP
  5511. * command the FW will respond with an "Invalid PIP Command" response.
  5512. * - Get HID Descriptor (Register 0x0001, no command ID)
  5513. * - Reset (Register 0x0005, RESET HID request)
  5514. * - Ping (Register 0x0004, Command ID 0x00
  5515. * - Get System Information (Register 0x0004, Command ID 0x02)
  5516. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5517. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5518. * This function will loop on the results of the STATUS command until
  5519. * the FW reports it is out of BOOT mode.
  5520. *
  5521. * NOTE:
  5522. * - This function will update cd->fw_system_mode
  5523. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5524. *
  5525. * RETURN:
  5526. * 0 = success
  5527. * !0 = failure
  5528. *
  5529. * PARAMETERS:
  5530. * *cd - pointer to core data
  5531. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5532. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5533. ******************************************************************************/
  5534. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5535. int *actual_wait)
  5536. {
  5537. int loop = 0;
  5538. u8 sys_mode = cd->fw_system_mode;
  5539. u8 pause = 10; /* in ms */
  5540. int rc = 0;
  5541. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5542. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5543. /*
  5544. * For PIP <1.11, no support for polling wait so do a hard
  5545. * coded wait and assume the FW is out of BOOT. Added 1 to
  5546. * timeout to make it clear in kmsg if non polling was done.
  5547. */
  5548. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5549. pt_debug(cd->dev, DL_ERROR,
  5550. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5551. __func__,
  5552. cd->sysinfo.ttdata.pip_ver_major,
  5553. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5554. msleep(*actual_wait);
  5555. sys_mode = FW_SYS_MODE_SCANNING;
  5556. }
  5557. if (sys_mode == FW_SYS_MODE_BOOT) {
  5558. while (!rc && loop <= max_loop &&
  5559. (sys_mode == FW_SYS_MODE_BOOT)) {
  5560. loop++;
  5561. usleep_range(9000, pause * 1000);
  5562. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5563. pt_debug(cd->dev, DL_DEBUG,
  5564. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5565. __func__, loop * pause, sys_mode);
  5566. }
  5567. *actual_wait = (int)(loop * pause);
  5568. pt_debug(cd->dev, DL_WARN,
  5569. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5570. __func__, *actual_wait, sys_mode);
  5571. if (rc)
  5572. sys_mode = FW_SYS_MODE_UNDEFINED;
  5573. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5574. sys_mode == FW_SYS_MODE_UNDEFINED)
  5575. rc = -EBUSY;
  5576. }
  5577. mutex_lock(&cd->system_lock);
  5578. cd->fw_system_mode = sys_mode;
  5579. mutex_unlock(&cd->system_lock);
  5580. return rc;
  5581. }
  5582. /*******************************************************************************
  5583. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5584. *
  5585. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5586. * access to the DUT.
  5587. *
  5588. * RETURN:
  5589. * 0 = success
  5590. * !0 = failure
  5591. *
  5592. * PARAMETERS:
  5593. * *cd - pointer to core data
  5594. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5595. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5596. ******************************************************************************/
  5597. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5598. int *actual_wait)
  5599. {
  5600. int rc;
  5601. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5602. if (rc < 0) {
  5603. pt_debug(cd->dev, DL_ERROR,
  5604. "%s: fail get exclusive ex=%p own=%p\n",
  5605. __func__, cd->exclusive_dev, cd->dev);
  5606. return rc;
  5607. }
  5608. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5609. if (release_exclusive(cd, cd->dev) < 0)
  5610. pt_debug(cd->dev, DL_ERROR,
  5611. "%s: fail to release exclusive\n", __func__);
  5612. return rc;
  5613. }
  5614. /*******************************************************************************
  5615. * FUNCTION: _pt_get_fw_sys_mode
  5616. *
  5617. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5618. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5619. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5620. * information but any PIP command above 0x1F requires scanning to be disabled
  5621. * before it will be operational. If scanning was not disabled before sending
  5622. * these PIP commands the FW will respond with a 6 byte error response. So to
  5623. * safely determine the scanning state, a PIP message that will not affect the
  5624. * operation of the FW was chosen:
  5625. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5626. * received scanning is active.
  5627. *
  5628. * RETURN:
  5629. * 0 = success
  5630. * !0 = failure
  5631. *
  5632. * PARAMETERS:
  5633. * *cd - pointer to core data
  5634. * *sys_mode - pointer to FW System mode
  5635. * *mode - pointer to mode (BL/FW)
  5636. ******************************************************************************/
  5637. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5638. {
  5639. int write_length = 1;
  5640. int report_length;
  5641. int rc = 0;
  5642. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5643. u8 tmp_mode = PT_MODE_UNKNOWN;
  5644. u8 param[1] = { PT_TCH_PARM_EBID };
  5645. struct pt_hid_output hid_output = {
  5646. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5647. .write_length = write_length,
  5648. .write_buf = param,
  5649. .novalidate = true,
  5650. };
  5651. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5652. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5653. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5654. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5655. __func__, tmp_sys_mode, tmp_mode);
  5656. if (!rc) {
  5657. if (tmp_mode != PT_MODE_OPERATIONAL)
  5658. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5659. }
  5660. goto exit;
  5661. }
  5662. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5663. if (cd->mode != PT_MODE_OPERATIONAL) {
  5664. tmp_mode = cd->mode;
  5665. goto exit;
  5666. }
  5667. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5668. if (rc)
  5669. goto exit;
  5670. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5671. if ((report_length == 0x06) &&
  5672. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5673. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5674. tmp_mode = PIP2_STATUS_APP_EXEC;
  5675. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5676. } else if ((report_length == 0x0A) &&
  5677. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5678. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5679. tmp_mode = PIP2_STATUS_APP_EXEC;
  5680. tmp_sys_mode = FW_SYS_MODE_TEST;
  5681. }
  5682. exit:
  5683. if (mode)
  5684. *mode = tmp_mode;
  5685. if (sys_mode)
  5686. *sys_mode = tmp_sys_mode;
  5687. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5688. __func__, tmp_mode, tmp_sys_mode);
  5689. return rc;
  5690. }
  5691. /*******************************************************************************
  5692. * FUNCTION: pt_get_fw_sys_mode
  5693. *
  5694. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5695. * is active or not.
  5696. *
  5697. * RETURN:
  5698. * 0 = success
  5699. * !0 = failure
  5700. *
  5701. * PARAMETERS:
  5702. * *cd - pointer to core data
  5703. * *sys_mode - pointer to fw system mode
  5704. * *mode - pointer to mode
  5705. ******************************************************************************/
  5706. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5707. {
  5708. int rc;
  5709. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5710. if (rc < 0) {
  5711. pt_debug(cd->dev, DL_ERROR,
  5712. "%s: fail get exclusive ex=%p own=%p\n",
  5713. __func__, cd->exclusive_dev, cd->dev);
  5714. return rc;
  5715. }
  5716. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5717. if (release_exclusive(cd, cd->dev) < 0)
  5718. pt_debug(cd->dev, DL_ERROR,
  5719. "%s: fail to release exclusive\n", __func__);
  5720. return rc;
  5721. }
  5722. /*******************************************************************************
  5723. * FUNCTION: _pt_request_get_fw_sys_mode
  5724. *
  5725. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5726. * to request to get scan state
  5727. *
  5728. * RETURN:
  5729. * 0 = success
  5730. * !0 = failure
  5731. *
  5732. * PARAMETERS:
  5733. * *dev - pointer to device structure
  5734. * protect - flag to call protected or non-protected
  5735. * *sys_mode - pointer to FW system mode
  5736. * *mode - pointer to mode
  5737. ******************************************************************************/
  5738. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5739. u8 *sys_mode, u8 *mode)
  5740. {
  5741. struct pt_core_data *cd = dev_get_drvdata(dev);
  5742. if (protect)
  5743. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5744. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5745. }
  5746. /* Default hid descriptor to provide basic register map */
  5747. const struct pt_hid_desc hid_desc_default = {
  5748. 230, /* hid_desc_len */
  5749. HID_APP_REPORT_ID, /* packet_id */
  5750. 0x00, /* reserved_byte */
  5751. 0x0100, /* bcd_version */
  5752. 0x00EC, /* report_desc_len */
  5753. 0x0002, /* report_desc_register */
  5754. 0x0003, /* input_register */
  5755. 0x00FE, /* max_input_len */
  5756. 0x0004, /* output_register */
  5757. 0x00FE, /* max_output_len */
  5758. 0x0005, /* command_register */
  5759. 0x0006, /* data_register */
  5760. 0x04B4, /* vendor_id */
  5761. 0xC101, /* product_id */
  5762. 0x0100, /* version_id */
  5763. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5764. };
  5765. /*******************************************************************************
  5766. * FUNCTION: pt_init_hid_descriptor
  5767. *
  5768. * SUMMARY: Setup default values for HID descriptor structure
  5769. *
  5770. *
  5771. * PARAMETERS:
  5772. * *desc - pointer to the HID descriptor data read back from DUT
  5773. ******************************************************************************/
  5774. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5775. {
  5776. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5777. }
  5778. /*******************************************************************************
  5779. * FUNCTION: pt_get_hid_descriptor_
  5780. *
  5781. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5782. * into the HID descriptor structure
  5783. *
  5784. * RETURN:
  5785. * 0 = success
  5786. * !0 = failure
  5787. *
  5788. * PARAMETERS:
  5789. * *cd - pointer to core data
  5790. * *desc - pointer to the HID descriptor data read back from DUT
  5791. ******************************************************************************/
  5792. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5793. struct pt_hid_desc *desc)
  5794. {
  5795. struct device *dev = cd->dev;
  5796. int rc = 0;
  5797. int t;
  5798. u8 cmd[2];
  5799. /*
  5800. * During startup the HID descriptor is required for all future
  5801. * processing. If IRQ is already asserted due to an early touch report
  5802. * the report must be cleared before sending command.
  5803. */
  5804. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5805. /* Read HID descriptor length and version */
  5806. mutex_lock(&cd->system_lock);
  5807. cd->hid_cmd_state = 1;
  5808. mutex_unlock(&cd->system_lock);
  5809. /* Set HID descriptor register */
  5810. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5811. sizeof(cd->hid_core.hid_desc_register));
  5812. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5813. __func__, sizeof(cmd));
  5814. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5815. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5816. if (rc) {
  5817. pt_debug(dev, DL_ERROR,
  5818. "%s: failed to get HID descriptor, rc=%d\n",
  5819. __func__, rc);
  5820. goto error;
  5821. }
  5822. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5823. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5824. if (IS_TMO(t)) {
  5825. #ifdef TTDL_DIAGNOSTICS
  5826. cd->bus_transmit_error_count++;
  5827. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5828. #endif /* TTDL_DIAGNOSTICS */
  5829. pt_debug(cd->dev, DL_ERROR,
  5830. "%s: HID get descriptor timed out\n", __func__);
  5831. rc = -ETIME;
  5832. goto error;
  5833. } else {
  5834. cd->hw_detected = true;
  5835. }
  5836. /* Load the HID descriptor including all registers */
  5837. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5838. /* Check HID descriptor length and version */
  5839. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5840. le16_to_cpu(desc->hid_desc_len),
  5841. le16_to_cpu(desc->bcd_version));
  5842. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5843. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5844. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5845. __func__);
  5846. return -ENODEV;
  5847. }
  5848. goto exit;
  5849. error:
  5850. mutex_lock(&cd->system_lock);
  5851. cd->hid_cmd_state = 0;
  5852. mutex_unlock(&cd->system_lock);
  5853. exit:
  5854. return rc;
  5855. }
  5856. /*******************************************************************************
  5857. * FUNCTION: pt_get_hid_descriptor
  5858. *
  5859. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5860. *
  5861. * RETURN:
  5862. * 0 = success
  5863. * !0 = failure
  5864. *
  5865. * PARAMETERS:
  5866. * *cd - pointer to core data
  5867. * *desc - pointer to the HID descriptor data read back from DUT
  5868. ******************************************************************************/
  5869. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5870. struct pt_hid_desc *desc)
  5871. {
  5872. int rc;
  5873. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5874. if (rc < 0) {
  5875. pt_debug(cd->dev, DL_ERROR,
  5876. "%s: fail get exclusive ex=%p own=%p\n",
  5877. __func__, cd->exclusive_dev, cd->dev);
  5878. return rc;
  5879. }
  5880. rc = pt_get_hid_descriptor_(cd, desc);
  5881. if (release_exclusive(cd, cd->dev) < 0)
  5882. pt_debug(cd->dev, DL_ERROR,
  5883. "%s: fail to release exclusive\n", __func__);
  5884. return rc;
  5885. }
  5886. /*******************************************************************************
  5887. * FUNCTION: pt_pip2_get_version_
  5888. *
  5889. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5890. * cd-ttdata
  5891. *
  5892. * RETURN:
  5893. * 0 = success
  5894. * !0 = failure
  5895. *
  5896. * PARAMETERS:
  5897. * *cd - pointer to core data
  5898. ******************************************************************************/
  5899. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5900. {
  5901. int rc = 0;
  5902. int status;
  5903. u8 read_buf[64];
  5904. u16 actual_read_len;
  5905. rc = _pt_request_pip2_send_cmd(cd->dev,
  5906. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5907. NULL, 0, read_buf, &actual_read_len);
  5908. if (rc) {
  5909. pt_debug(cd->dev, DL_ERROR,
  5910. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5911. __func__, rc);
  5912. return rc;
  5913. }
  5914. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5915. if (status == 0) {
  5916. /* Parse the PIP2 VERSION response into ttdata */
  5917. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5918. } else {
  5919. pt_debug(cd->dev, DL_ERROR,
  5920. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5921. __func__, status);
  5922. return status;
  5923. }
  5924. return rc;
  5925. }
  5926. /*******************************************************************************
  5927. * FUNCTION: pt_pip2_get_version
  5928. *
  5929. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5930. * access to the DUT.
  5931. *
  5932. * RETURN:
  5933. * 0 = success
  5934. * !0 = failure
  5935. *
  5936. * PARAMETERS:
  5937. * *cd - pointer to core data structure
  5938. ******************************************************************************/
  5939. static int pt_pip2_get_version(struct pt_core_data *cd)
  5940. {
  5941. int rc;
  5942. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5943. if (rc < 0) {
  5944. pt_debug(cd->dev, DL_ERROR,
  5945. "%s: fail get exclusive ex=%p own=%p\n",
  5946. __func__, cd->exclusive_dev, cd->dev);
  5947. return rc;
  5948. }
  5949. rc = pt_pip2_get_version_(cd);
  5950. if (release_exclusive(cd, cd->dev) < 0)
  5951. pt_debug(cd->dev, DL_ERROR,
  5952. "%s: fail to release exclusive\n", __func__);
  5953. return rc;
  5954. }
  5955. /*******************************************************************************
  5956. * FUNCTION: _pt_request_active_pip_protocol
  5957. *
  5958. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5959. * Function will return PIP version of BL or application based on
  5960. * when it's called.
  5961. *
  5962. * RETURN:
  5963. * 0 = success
  5964. * !0 = failure
  5965. *
  5966. * PARAMETERS:
  5967. * *dev - pointer to device structure
  5968. * protect - flag to run in protected mode
  5969. * *pip_version_major - pointer to store PIP major version
  5970. * *pip_version_minor - pointer to store PIP minor version
  5971. ******************************************************************************/
  5972. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5973. u8 *pip_version_major, u8 *pip_version_minor)
  5974. {
  5975. struct pt_core_data *cd = dev_get_drvdata(dev);
  5976. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5977. int rc = 0;
  5978. struct pt_hid_output sys_info = {
  5979. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5980. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5981. };
  5982. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5983. /* Skip PIP2 command if DUT generation is confirmed */
  5984. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5985. goto skip_pip2_command;
  5986. rc = pt_pip2_get_version_(cd);
  5987. if (!rc) {
  5988. *pip_version_major = ttdata->pip_ver_major;
  5989. *pip_version_minor = ttdata->pip_ver_minor;
  5990. pt_debug(dev, DL_INFO,
  5991. "%s: pip_version = %d.%d\n", __func__,
  5992. *pip_version_major, *pip_version_minor);
  5993. } else {
  5994. /*
  5995. * Legacy products do not support the pip2 protocol to get
  5996. * pip version. However, they do support the "get sysinfo"
  5997. * command to get pip version from FW, but the bootloader
  5998. * does not support it. This function will try "get sysinfo"
  5999. * command if the pip2 command failed but this cmd could also
  6000. * fail if DUT is stuck in bootloader mode.
  6001. */
  6002. pt_debug(dev, DL_INFO,
  6003. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6004. __func__, rc);
  6005. skip_pip2_command:
  6006. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6007. if (!rc) {
  6008. *pip_version_minor =
  6009. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6010. *pip_version_major =
  6011. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6012. pt_debug(dev, DL_INFO,
  6013. "%s: pip_version = %d.%d\n", __func__,
  6014. *pip_version_major, *pip_version_minor);
  6015. } else {
  6016. *pip_version_major = 0;
  6017. *pip_version_minor = 0;
  6018. pt_debug(dev, DL_ERROR,
  6019. "%s: pip_version Not Detected\n", __func__);
  6020. }
  6021. }
  6022. return rc;
  6023. }
  6024. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6025. /*******************************************************************************
  6026. * FUNCTION: _pt_detect_dut_generation
  6027. *
  6028. * SUMMARY: Determine the generation of device that we are communicating with:
  6029. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6030. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6031. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6032. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6033. * coming form the BL or the FW:
  6034. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6035. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6036. * This function will return a modified status if it detects the DUT
  6037. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6038. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6039. *
  6040. * RETURN:
  6041. * 0 = success
  6042. * !0 = failure
  6043. *
  6044. * PARAMETERS:
  6045. * *dev - pointer to device structure
  6046. * *status - pointer to status bitmask
  6047. * *dut_gen - pointer to store the dut_generation
  6048. * *mode - pointer to store the PT_MODE
  6049. ******************************************************************************/
  6050. static int _pt_detect_dut_generation(struct device *dev,
  6051. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6052. {
  6053. int rc = 0;
  6054. u8 dut_gen_tmp = DUT_UNKNOWN;
  6055. u8 mode_tmp = PT_MODE_UNKNOWN;
  6056. u8 attempt = 1;
  6057. u32 status_tmp = STARTUP_STATUS_START;
  6058. struct pt_core_data *cd = dev_get_drvdata(dev);
  6059. struct pt_hid_desc hid_desc;
  6060. memset(&hid_desc, 0, sizeof(hid_desc));
  6061. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6062. while (rc && attempt < 3) {
  6063. attempt++;
  6064. usleep_range(2000, 5000);
  6065. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6066. }
  6067. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6068. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6069. mode_tmp = PT_MODE_BOOTLOADER;
  6070. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6071. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6072. rc = pt_pip2_get_version_(cd);
  6073. if (!rc)
  6074. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6075. else
  6076. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6077. mode_tmp = PT_MODE_OPERATIONAL;
  6078. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6079. rc = 0; /* To return success instead of error code */
  6080. } else if (rc) {
  6081. rc = pt_pip2_get_version_(cd);
  6082. if (!rc) {
  6083. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6084. mode_tmp = PT_MODE_BOOTLOADER;
  6085. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6086. }
  6087. }
  6088. mutex_lock(&cd->system_lock);
  6089. if (dut_gen)
  6090. *dut_gen = dut_gen_tmp;
  6091. if (mode)
  6092. *mode = mode_tmp;
  6093. if (status)
  6094. *status = status_tmp;
  6095. mutex_unlock(&cd->system_lock);
  6096. #ifdef TTDL_DIAGNOSTICS
  6097. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6098. __func__, dut_gen_tmp, mode_tmp);
  6099. #endif /* TTDL_DIAGNOSTICS */
  6100. return rc;
  6101. }
  6102. /*******************************************************************************
  6103. * FUNCTION: _pt_request_dut_generation
  6104. *
  6105. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6106. * to get current dut generation.
  6107. *
  6108. * NOTE: This function WILL NOT try to determine dut generation.
  6109. *
  6110. * RETURN:
  6111. * The current dut generation.
  6112. *
  6113. * PARAMETERS:
  6114. * *dev - pointer to device structure
  6115. ******************************************************************************/
  6116. static int _pt_request_dut_generation(struct device *dev)
  6117. {
  6118. struct pt_core_data *cd = dev_get_drvdata(dev);
  6119. return cd->active_dut_generation;
  6120. }
  6121. #define HW_VERSION_LEN_MAX 13
  6122. /*******************************************************************************
  6123. * FUNCTION: _legacy_generate_hw_version
  6124. *
  6125. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6126. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6127. * command) to the hw_version.
  6128. *
  6129. * RETURN:
  6130. * 0 = success
  6131. * !0 = failure
  6132. *
  6133. * PARAMETERS:
  6134. * *cd - pointer to core data
  6135. * *hw_version - pointer to store the hardware version
  6136. ******************************************************************************/
  6137. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6138. char *hw_version)
  6139. {
  6140. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6141. if (cd->sysinfo.ready) {
  6142. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6143. ttdata->jtag_id_h, cd->pid_for_loader);
  6144. return 0;
  6145. } else if (cd->bl_info.ready) {
  6146. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6147. cd->bl_info.chip_id, cd->pid_for_loader);
  6148. return 0;
  6149. } else {
  6150. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6151. pt_debug(cd->dev, DL_ERROR,
  6152. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6153. return -ENODATA;
  6154. }
  6155. }
  6156. /*******************************************************************************
  6157. * FUNCTION: _pip2_generate_hw_version
  6158. *
  6159. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6160. * VERSION command) to the hw_version.
  6161. *
  6162. * RETURN:
  6163. * 0 = success
  6164. * !0 = failure
  6165. *
  6166. * PARAMETERS:
  6167. * *cd - pointer to core data
  6168. * *hw_version - pointer to store the hardware version
  6169. ******************************************************************************/
  6170. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6171. {
  6172. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6173. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6174. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6175. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6176. return 0;
  6177. } else {
  6178. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6179. pt_debug(cd->dev, DL_ERROR,
  6180. "%s: PIP Version are not ready\n", __func__);
  6181. return -ENODATA;
  6182. }
  6183. }
  6184. /*******************************************************************************
  6185. * FUNCTION: pt_generate_hw_version
  6186. *
  6187. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6188. * hw_version from static data.
  6189. *
  6190. * RETURN:
  6191. * 0 = success
  6192. * !0 = failure
  6193. *
  6194. * PARAMETERS:
  6195. * *cd - pointer to core data
  6196. * *hw_version - pointer to store the hardware version
  6197. ******************************************************************************/
  6198. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6199. {
  6200. int rc = 0;
  6201. if (!hw_version)
  6202. return -ENOMEM;
  6203. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6204. rc = _legacy_generate_hw_version(cd, hw_version);
  6205. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6206. rc = _pip2_generate_hw_version(cd, hw_version);
  6207. else {
  6208. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6209. rc = -ENODATA;
  6210. }
  6211. return rc;
  6212. }
  6213. /*******************************************************************************
  6214. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6215. *
  6216. * NOTE: The calling function must ensure to free *hw_version
  6217. *
  6218. * RETURN:
  6219. * 0 = success
  6220. * !0 = Failure
  6221. *
  6222. * PARAMETERS:
  6223. * *dev - pointer to device structure
  6224. * *hw_version - pointer to where the hw_version string will be stored
  6225. ******************************************************************************/
  6226. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6227. {
  6228. int rc = 0;
  6229. u16 actual_read_len;
  6230. u16 pip_ver;
  6231. u8 rd_buf[256];
  6232. u8 status;
  6233. u8 index = PIP2_RESP_STATUS_OFFSET;
  6234. u8 return_data[8];
  6235. u8 panel_id;
  6236. struct pt_core_data *cd = dev_get_drvdata(dev);
  6237. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6238. if (!hw_version)
  6239. return -ENOMEM;
  6240. if (!cd->hw_detected) {
  6241. /* No HW detected */
  6242. rc = -ENODEV;
  6243. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6244. __func__);
  6245. goto exit_error;
  6246. }
  6247. /* For Parade TC or TT parts */
  6248. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6249. rc = _pt_request_pip2_send_cmd(dev,
  6250. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6251. NULL, 0, rd_buf, &actual_read_len);
  6252. if (rc) {
  6253. pt_debug(dev, DL_ERROR,
  6254. "%s: Failed to send PIP2 VERSION cmd\n",
  6255. __func__);
  6256. goto exit_error;
  6257. }
  6258. status = rd_buf[index];
  6259. if (status == 0) {
  6260. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6261. /*
  6262. * BL PIP 2.02 and greater the version fields are
  6263. * swapped
  6264. */
  6265. if (pip_ver >= 0x0202) {
  6266. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6267. "%02X%02X.%02X%02X.FF",
  6268. rd_buf[index + 10], rd_buf[index + 9],
  6269. rd_buf[index + 8], rd_buf[index + 7]);
  6270. } else {
  6271. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6272. "%02X%02X.%02X%02X.FF",
  6273. rd_buf[index + 8], rd_buf[index + 7],
  6274. rd_buf[index + 10], rd_buf[index + 9]);
  6275. }
  6276. return STATUS_SUCCESS;
  6277. } else {
  6278. rc = status;
  6279. pt_debug(dev, DL_WARN,
  6280. "%s: PIP2 VERSION cmd response error\n",
  6281. __func__);
  6282. }
  6283. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6284. /*
  6285. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6286. * hard coded to FFFF
  6287. */
  6288. if (cd->mode == PT_MODE_OPERATIONAL) {
  6289. rc = pt_hid_output_get_sysinfo(cd);
  6290. if (!rc) {
  6291. panel_id =
  6292. cd->sysinfo.sensing_conf_data.panel_id;
  6293. } else {
  6294. panel_id = PANEL_ID_NOT_ENABLED;
  6295. }
  6296. /* In FW - simply retrieve from ttdata struct */
  6297. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6298. "%04X.FFFF.%02X",
  6299. ttdata->jtag_id_h,
  6300. panel_id);
  6301. return STATUS_SUCCESS;
  6302. } else {
  6303. /*
  6304. * Return the stored value if PT_PANEL_ID_BY_BL
  6305. * is not supported while other feature is.
  6306. */
  6307. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6308. rc = pt_hid_output_bl_get_information(
  6309. cd, return_data);
  6310. if (!rc) {
  6311. rc = pt_hid_output_bl_get_panel_id(
  6312. cd, &panel_id);
  6313. }
  6314. } else
  6315. panel_id = cd->pid_for_loader;
  6316. if (!rc) {
  6317. snprintf(hw_version,
  6318. HW_VERSION_LEN_MAX,
  6319. "%02X%02X.FFFF.%02X",
  6320. return_data[3], return_data[2],
  6321. panel_id);
  6322. return STATUS_SUCCESS;
  6323. }
  6324. }
  6325. } else {
  6326. /* Unknown generation */
  6327. rc = -ENODEV;
  6328. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6329. __func__);
  6330. }
  6331. exit_error:
  6332. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6333. return rc;
  6334. }
  6335. /*******************************************************************************
  6336. * FUNCTION: pt_start_wd_timer
  6337. *
  6338. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6339. *
  6340. * RETURN: void
  6341. *
  6342. * PARAMETERS:
  6343. * *cd - pointer to core data
  6344. ******************************************************************************/
  6345. static void pt_start_wd_timer(struct pt_core_data *cd)
  6346. {
  6347. if (cd->watchdog_interval < 100) {
  6348. pt_debug(cd->dev, DL_ERROR,
  6349. "%s: WARNING: Invalid watchdog interval: %d\n",
  6350. __func__, cd->watchdog_interval);
  6351. return;
  6352. }
  6353. if (cd->watchdog_force_stop) {
  6354. pt_debug(cd->dev, DL_INFO,
  6355. "%s: TTDL WD Forced stop\n", __func__);
  6356. return;
  6357. }
  6358. mod_timer(&cd->watchdog_timer, jiffies +
  6359. msecs_to_jiffies(cd->watchdog_interval));
  6360. cd->watchdog_enabled = 1;
  6361. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6362. }
  6363. /*******************************************************************************
  6364. * FUNCTION: pt_stop_wd_timer
  6365. *
  6366. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6367. *
  6368. * RETURN: void
  6369. *
  6370. * PARAMETERS:
  6371. * *cd - pointer to core data
  6372. ******************************************************************************/
  6373. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6374. {
  6375. if (!cd->watchdog_interval)
  6376. return;
  6377. /*
  6378. * Ensure we wait until the watchdog timer
  6379. * running on a different CPU finishes
  6380. */
  6381. del_timer_sync(&cd->watchdog_timer);
  6382. cancel_work_sync(&cd->watchdog_work);
  6383. del_timer_sync(&cd->watchdog_timer);
  6384. cd->watchdog_enabled = 0;
  6385. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6386. }
  6387. /*******************************************************************************
  6388. * FUNCTION: pt_hw_soft_reset
  6389. *
  6390. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6391. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6392. * schedule an enum if it fires when the DUT is being reset.
  6393. * This can cause a double reset.
  6394. *
  6395. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6396. * the WD active could cause this function to fail!
  6397. *
  6398. * RETURN:
  6399. * 0 = success
  6400. * !0 = failure
  6401. *
  6402. * PARAMETERS:
  6403. * *cd - pointer to core data struct
  6404. * protect - flag to call protected or non-protected
  6405. ******************************************************************************/
  6406. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6407. {
  6408. int rc = 0;
  6409. mutex_lock(&cd->system_lock);
  6410. cd->startup_status = STARTUP_STATUS_START;
  6411. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6412. mutex_unlock(&cd->system_lock);
  6413. if (protect)
  6414. rc = pt_hid_cmd_reset(cd);
  6415. else
  6416. rc = pt_hid_cmd_reset_(cd);
  6417. if (rc < 0) {
  6418. pt_debug(cd->dev, DL_ERROR,
  6419. "%s: FAILED to execute SOFT reset\n", __func__);
  6420. return rc;
  6421. }
  6422. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6423. __func__);
  6424. return 0;
  6425. }
  6426. /*******************************************************************************
  6427. * FUNCTION: pt_hw_hard_reset
  6428. *
  6429. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6430. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6431. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6432. * schedule an enum if it fires when the DUT is being reset.
  6433. * This can cause a double reset.
  6434. *
  6435. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6436. * the WD active could cause this function to fail!
  6437. *
  6438. * RETURN:
  6439. * 0 = success
  6440. * !0 = failure
  6441. *
  6442. * PARAMETERS:
  6443. * *cd - pointer to core data struct
  6444. ******************************************************************************/
  6445. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6446. {
  6447. if (cd->cpdata->xres) {
  6448. cd->startup_status = STARTUP_STATUS_START;
  6449. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6450. __func__);
  6451. cd->cpdata->xres(cd->cpdata, cd->dev);
  6452. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6453. __func__);
  6454. return 0;
  6455. }
  6456. pt_debug(cd->dev, DL_ERROR,
  6457. "%s: FAILED to execute HARD reset\n", __func__);
  6458. return -ENODEV;
  6459. }
  6460. /*******************************************************************************
  6461. * FUNCTION: pt_dut_reset
  6462. *
  6463. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6464. * soft reset.
  6465. *
  6466. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6467. * the WD active could cause this function to fail!
  6468. * NOTE: "protect" flag is only used for soft reset.
  6469. *
  6470. * RETURN:
  6471. * 0 = success
  6472. * !0 = failure
  6473. *
  6474. * PARAMETERS:
  6475. * *cd - pointer to core data structure
  6476. * protect - flag to call protected or non-protected
  6477. ******************************************************************************/
  6478. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6479. {
  6480. int rc = 0;
  6481. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6482. mutex_lock(&cd->system_lock);
  6483. cd->hid_reset_cmd_state = 1;
  6484. rc = pt_hw_hard_reset(cd);
  6485. mutex_unlock(&cd->system_lock);
  6486. if (rc == -ENODEV) {
  6487. mutex_lock(&cd->system_lock);
  6488. cd->hid_reset_cmd_state = 0;
  6489. mutex_unlock(&cd->system_lock);
  6490. pt_debug(cd->dev, DL_ERROR,
  6491. "%s: Hard reset failed, try soft reset\n", __func__);
  6492. rc = pt_hw_soft_reset(cd, protect);
  6493. }
  6494. if (rc)
  6495. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6496. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6497. return rc;
  6498. }
  6499. /*******************************************************************************
  6500. * FUNCTION: pt_dut_reset_and_wait
  6501. *
  6502. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6503. * complete
  6504. *
  6505. * RETURN:
  6506. * 0 = success
  6507. * !0 = failure
  6508. *
  6509. * PARAMETERS:
  6510. * *cd - pointer to core data structure
  6511. ******************************************************************************/
  6512. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6513. {
  6514. int rc = 0;
  6515. int t;
  6516. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6517. if (rc < 0)
  6518. goto exit;
  6519. t = wait_event_timeout(cd->wait_q,
  6520. (cd->hid_reset_cmd_state == 0),
  6521. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6522. if (IS_TMO(t)) {
  6523. #ifdef TTDL_DIAGNOSTICS
  6524. cd->bus_transmit_error_count++;
  6525. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6526. #endif /* TTDL_DIAGNOSTICS */
  6527. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6528. __func__);
  6529. rc = -ETIME;
  6530. goto exit;
  6531. }
  6532. exit:
  6533. return rc;
  6534. }
  6535. /*
  6536. * touch default parameters (from report descriptor) to resolve protocol for
  6537. * touch report
  6538. */
  6539. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6540. /* byte offset, size, min, max, bit offset, report */
  6541. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6542. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6543. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6544. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6545. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6546. };
  6547. /*
  6548. * button default parameters (from report descriptor) to resolve protocol for
  6549. * button report
  6550. */
  6551. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6552. /* byte offset, size, min, max, bit offset, report */
  6553. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6554. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6555. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6556. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6557. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6558. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6559. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6560. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6561. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6562. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6563. };
  6564. /*******************************************************************************
  6565. * FUNCTION: pt_init_pip_report_fields
  6566. *
  6567. * SUMMARY: Setup default values for touch/button report parsing.
  6568. *
  6569. * PARAMETERS:
  6570. * *cd - pointer to core data structure
  6571. ******************************************************************************/
  6572. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6573. {
  6574. struct pt_sysinfo *si = &cd->sysinfo;
  6575. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6576. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6577. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6578. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6579. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6580. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6581. cd->features.easywake = 1;
  6582. cd->features.noise_metric = 1;
  6583. cd->features.tracking_heatmap = 1;
  6584. cd->features.sensor_data = 1;
  6585. }
  6586. /*******************************************************************************
  6587. * FUNCTION: pt_get_mode
  6588. *
  6589. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6590. * message
  6591. *
  6592. * RETURN: Enum of the current mode
  6593. *
  6594. * PARAMETERS:
  6595. * *cd - pointer to the Core Data structure
  6596. * protect - run command in protected mode
  6597. * *mode - pointer to store the retrieved mode
  6598. ******************************************************************************/
  6599. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6600. {
  6601. if (desc->packet_id == HID_APP_REPORT_ID)
  6602. return PT_MODE_OPERATIONAL;
  6603. else if (desc->packet_id == HID_BL_REPORT_ID)
  6604. return PT_MODE_BOOTLOADER;
  6605. return PT_MODE_UNKNOWN;
  6606. }
  6607. /*******************************************************************************
  6608. * FUNCTION: _pt_request_get_mode
  6609. *
  6610. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6611. * to determine the current mode of the DUT by use of the Get HID
  6612. * Descriptor command.
  6613. *
  6614. * RETURN:
  6615. * 0 = success
  6616. * !0 = failure
  6617. *
  6618. * PARAMETERS:
  6619. * *dev - pointer to device structure
  6620. * protect - run command in protected mode
  6621. * *mode - pointer to store the retrieved mode
  6622. ******************************************************************************/
  6623. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6624. {
  6625. struct pt_core_data *cd = dev_get_drvdata(dev);
  6626. struct pt_hid_desc hid_desc;
  6627. int rc = 0;
  6628. memset(&hid_desc, 0, sizeof(hid_desc));
  6629. if (protect)
  6630. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6631. else
  6632. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6633. if (rc)
  6634. *mode = PT_MODE_UNKNOWN;
  6635. else
  6636. *mode = pt_get_mode(cd, &hid_desc);
  6637. return rc;
  6638. }
  6639. /*******************************************************************************
  6640. * FUNCTION: pt_queue_enum_
  6641. *
  6642. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6643. * function. It won't try to add/delete sysfs node or modules.
  6644. *
  6645. * RETURN: void
  6646. *
  6647. * PARAMETERS:
  6648. * *cd - pointer to core data
  6649. ******************************************************************************/
  6650. static void pt_queue_enum_(struct pt_core_data *cd)
  6651. {
  6652. if (cd->startup_state == STARTUP_NONE) {
  6653. cd->startup_state = STARTUP_QUEUED;
  6654. #ifdef TTDL_DIAGNOSTICS
  6655. if (!cd->bridge_mode)
  6656. schedule_work(&cd->enum_work);
  6657. else
  6658. cd->startup_state = STARTUP_NONE;
  6659. #else
  6660. schedule_work(&cd->enum_work);
  6661. #endif
  6662. pt_debug(cd->dev, DL_INFO,
  6663. "%s: enum_work queued\n", __func__);
  6664. } else {
  6665. pt_debug(cd->dev, DL_WARN,
  6666. "%s: Enum not queued - startup_state = %d\n",
  6667. __func__, cd->startup_state);
  6668. }
  6669. }
  6670. /*******************************************************************************
  6671. * FUNCTION: pt_queue_enum
  6672. *
  6673. * SUMMARY: Queues a TTDL enum within a mutex lock
  6674. *
  6675. * RETURN: void
  6676. *
  6677. * PARAMETERS:
  6678. * *cd - pointer to core data
  6679. ******************************************************************************/
  6680. static void pt_queue_enum(struct pt_core_data *cd)
  6681. {
  6682. mutex_lock(&cd->system_lock);
  6683. pt_queue_enum_(cd);
  6684. mutex_unlock(&cd->system_lock);
  6685. }
  6686. static void remove_sysfs_and_modules(struct device *dev);
  6687. /*******************************************************************************
  6688. * FUNCTION: pt_queue_restart
  6689. *
  6690. * SUMMARY: Queues a TTDL restart within a mutex lock
  6691. *
  6692. * RETURN: void
  6693. *
  6694. * PARAMETERS:
  6695. * *cd - pointer to core data
  6696. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6697. * False: will not perform remove action
  6698. ******************************************************************************/
  6699. static void pt_queue_restart(struct pt_core_data *cd)
  6700. {
  6701. mutex_lock(&cd->system_lock);
  6702. if (cd->startup_state == STARTUP_NONE) {
  6703. cd->startup_state = STARTUP_QUEUED;
  6704. schedule_work(&cd->ttdl_restart_work);
  6705. pt_debug(cd->dev, DL_INFO,
  6706. "%s: pt_ttdl_restart queued\n", __func__);
  6707. } else {
  6708. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6709. __func__, cd->startup_state);
  6710. }
  6711. mutex_unlock(&cd->system_lock);
  6712. }
  6713. /*******************************************************************************
  6714. * FUNCTION: call_atten_cb
  6715. *
  6716. * SUMMARY: Iterate over attention list call the function that registered.
  6717. *
  6718. * RETURN: void
  6719. *
  6720. * PARAMETERS:
  6721. * *cd - pointer to core data
  6722. * type - type of attention list
  6723. * mode - condition for execution
  6724. ******************************************************************************/
  6725. static void call_atten_cb(struct pt_core_data *cd,
  6726. enum pt_atten_type type, int mode)
  6727. {
  6728. struct atten_node *atten, *atten_n;
  6729. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6730. __func__, type, mode);
  6731. spin_lock(&cd->spinlock);
  6732. list_for_each_entry_safe(atten, atten_n,
  6733. &cd->atten_list[type], node) {
  6734. if (!mode || atten->mode & mode) {
  6735. spin_unlock(&cd->spinlock);
  6736. pt_debug(cd->dev, DL_DEBUG,
  6737. "%s: attention for '%s'",
  6738. __func__, dev_name(atten->dev));
  6739. atten->func(atten->dev);
  6740. spin_lock(&cd->spinlock);
  6741. }
  6742. }
  6743. spin_unlock(&cd->spinlock);
  6744. }
  6745. /*******************************************************************************
  6746. * FUNCTION: start_fw_upgrade
  6747. *
  6748. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6749. * start firmware upgrade.
  6750. *
  6751. * RETURN:
  6752. * 0 = success
  6753. *
  6754. * PARAMETERS:
  6755. * *data - pointer to core data
  6756. ******************************************************************************/
  6757. static int start_fw_upgrade(void *data)
  6758. {
  6759. struct pt_core_data *cd = (struct pt_core_data *)data;
  6760. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6761. return 0;
  6762. }
  6763. /*******************************************************************************
  6764. * FUNCTION: pt_put_device_into_easy_wakeup_
  6765. *
  6766. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6767. * wake up state.
  6768. *
  6769. * RETURN:
  6770. * 0 = success
  6771. * !0 = failure
  6772. *
  6773. * PARAMETERS:
  6774. * *cd - pointer to core data
  6775. ******************************************************************************/
  6776. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6777. {
  6778. int rc = 0;
  6779. u8 status = 0;
  6780. mutex_lock(&cd->system_lock);
  6781. cd->wait_until_wake = 0;
  6782. mutex_unlock(&cd->system_lock);
  6783. rc = pt_hid_output_enter_easywake_state_(cd,
  6784. cd->easy_wakeup_gesture, &status);
  6785. if (rc || status == 0)
  6786. return -EBUSY;
  6787. return rc;
  6788. }
  6789. /*******************************************************************************
  6790. * FUNCTION: pt_put_device_into_deep_sleep_
  6791. *
  6792. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6793. *
  6794. * RETURN:
  6795. * 0 = success
  6796. * !0 = error
  6797. *
  6798. * PARAMETERS:
  6799. * *cd - pointer to core data
  6800. ******************************************************************************/
  6801. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6802. {
  6803. int rc = 0;
  6804. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6805. if (rc)
  6806. rc = -EBUSY;
  6807. return rc;
  6808. }
  6809. /*******************************************************************************
  6810. * FUNCTION: pt_put_device_into_deep_standby_
  6811. *
  6812. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6813. *
  6814. * RETURN:
  6815. * 0 = success
  6816. * !0 = error
  6817. *
  6818. * PARAMETERS:
  6819. * *cd - pointer to core data
  6820. ******************************************************************************/
  6821. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6822. {
  6823. int rc = 0;
  6824. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6825. if (rc)
  6826. rc = -EBUSY;
  6827. return rc;
  6828. }
  6829. /*******************************************************************************
  6830. * FUNCTION: pt_core_poweroff_device_
  6831. *
  6832. * SUMMARY: Disable IRQ and HW power down the device.
  6833. *
  6834. * RETURN:
  6835. * 0 = success
  6836. * !0 = failure
  6837. *
  6838. * PARAMETERS:
  6839. * *cd - pointer to core data
  6840. ******************************************************************************/
  6841. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6842. {
  6843. int rc;
  6844. if (cd->irq_enabled) {
  6845. cd->irq_enabled = false;
  6846. disable_irq_nosync(cd->irq);
  6847. }
  6848. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6849. if (rc < 0)
  6850. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6851. __func__, rc);
  6852. return rc;
  6853. }
  6854. /*******************************************************************************
  6855. * FUNCTION: pt_core_sleep_
  6856. *
  6857. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6858. * configuration in the core platform data structure.
  6859. *
  6860. * RETURN:
  6861. * 0 = success
  6862. * !0 = failure
  6863. *
  6864. * PARAMETERS:
  6865. * *cd - pointer to core data
  6866. ******************************************************************************/
  6867. static int pt_core_sleep_(struct pt_core_data *cd)
  6868. {
  6869. int rc = 0;
  6870. mutex_lock(&cd->system_lock);
  6871. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6872. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6873. mutex_unlock(&cd->system_lock);
  6874. pt_debug(cd->dev, DL_INFO,
  6875. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6876. return 0;
  6877. } else {
  6878. cd->sleep_state = SS_SLEEPING;
  6879. }
  6880. mutex_unlock(&cd->system_lock);
  6881. /* Ensure watchdog and startup works stopped */
  6882. pt_stop_wd_timer(cd);
  6883. cancel_work_sync(&cd->enum_work);
  6884. pt_stop_wd_timer(cd);
  6885. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6886. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6887. rc = pt_core_poweroff_device_(cd);
  6888. if (rc)
  6889. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6890. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6891. pt_debug(cd->dev, DL_INFO,
  6892. "%s: Entering into deep standby mode:\n", __func__);
  6893. rc = pt_put_device_into_deep_standby_(cd);
  6894. if (rc)
  6895. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6896. } else {
  6897. pt_debug(cd->dev, DL_INFO,
  6898. "%s: Entering into deep sleep mode:\n", __func__);
  6899. rc = pt_put_device_into_deep_sleep_(cd);
  6900. if (rc)
  6901. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6902. }
  6903. mutex_lock(&cd->system_lock);
  6904. cd->sleep_state = SS_SLEEP_ON;
  6905. mutex_unlock(&cd->system_lock);
  6906. return rc;
  6907. }
  6908. /*******************************************************************************
  6909. * FUNCTION: pt_core_easywake_on_
  6910. *
  6911. * SUMMARY: Suspend the device with easy wake on the
  6912. * configuration in the core platform data structure.
  6913. *
  6914. * RETURN:
  6915. * 0 = success
  6916. * !0 = failure
  6917. *
  6918. * PARAMETERS:
  6919. * *cd - pointer to core data
  6920. ******************************************************************************/
  6921. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6922. {
  6923. int rc = 0;
  6924. mutex_lock(&cd->system_lock);
  6925. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6926. mutex_unlock(&cd->system_lock);
  6927. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6928. __func__, cd->sleep_state);
  6929. return 0;
  6930. }
  6931. mutex_unlock(&cd->system_lock);
  6932. /* Ensure watchdog and startup works stopped */
  6933. pt_stop_wd_timer(cd);
  6934. cancel_work_sync(&cd->enum_work);
  6935. pt_stop_wd_timer(cd);
  6936. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6937. rc = pt_put_device_into_easy_wakeup_(cd);
  6938. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6939. if (rc)
  6940. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6941. }
  6942. mutex_lock(&cd->system_lock);
  6943. cd->sleep_state = SS_EASY_WAKING_ON;
  6944. mutex_unlock(&cd->system_lock);
  6945. return rc;
  6946. }
  6947. /*******************************************************************************
  6948. * FUNCTION: pt_core_easywake_on
  6949. *
  6950. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6951. *
  6952. * RETURN:
  6953. * 0 = success
  6954. * !0 = failure
  6955. *
  6956. * PARAMETERS:
  6957. * *cd - pointer to core data
  6958. ******************************************************************************/
  6959. static int pt_core_easywake_on(struct pt_core_data *cd)
  6960. {
  6961. int rc = 0;
  6962. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6963. if (rc < 0) {
  6964. pt_debug(cd->dev, DL_ERROR,
  6965. "%s: fail get exclusive ex=%p own=%p\n",
  6966. __func__, cd->exclusive_dev, cd->dev);
  6967. return rc;
  6968. }
  6969. rc = pt_core_easywake_on_(cd);
  6970. if (release_exclusive(cd, cd->dev) < 0)
  6971. pt_debug(cd->dev, DL_ERROR,
  6972. "%s: fail to release exclusive\n", __func__);
  6973. else
  6974. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6975. __func__);
  6976. return rc;
  6977. }
  6978. /*******************************************************************************
  6979. * FUNCTION: pt_core_sleep
  6980. *
  6981. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6982. *
  6983. * RETURN:
  6984. * 0 = success
  6985. * !0 = failure
  6986. *
  6987. * PARAMETERS:
  6988. * *cd - pointer to core data
  6989. ******************************************************************************/
  6990. static int pt_core_sleep(struct pt_core_data *cd)
  6991. {
  6992. int rc = 0;
  6993. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6994. if (rc < 0) {
  6995. pt_debug(cd->dev, DL_ERROR,
  6996. "%s: fail get exclusive ex=%p own=%p\n",
  6997. __func__, cd->exclusive_dev, cd->dev);
  6998. return rc;
  6999. }
  7000. rc = pt_core_sleep_(cd);
  7001. if (release_exclusive(cd, cd->dev) < 0)
  7002. pt_debug(cd->dev, DL_ERROR,
  7003. "%s: fail to release exclusive\n", __func__);
  7004. else
  7005. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7006. __func__);
  7007. return rc;
  7008. }
  7009. /*******************************************************************************
  7010. * FUNCTION: pt_wakeup_host
  7011. *
  7012. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7013. *
  7014. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7015. *
  7016. * RETURN:
  7017. * 0 = success
  7018. * !0 = failure
  7019. *
  7020. * PARAMETERS:
  7021. * *cd - pointer to core data
  7022. ******************************************************************************/
  7023. static int pt_wakeup_host(struct pt_core_data *cd)
  7024. {
  7025. #ifndef EASYWAKE_TSG6
  7026. /* TSG5 EasyWake */
  7027. int rc = 0;
  7028. int event_id;
  7029. int size = get_unaligned_le16(&cd->input_buf[0]);
  7030. /* Validate report */
  7031. if (size != 4 || cd->input_buf[2] != 4)
  7032. rc = -EINVAL;
  7033. event_id = cd->input_buf[3];
  7034. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7035. __func__, event_id, rc);
  7036. if (rc) {
  7037. pt_core_sleep_(cd);
  7038. goto exit;
  7039. }
  7040. /* attention WAKE */
  7041. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7042. exit:
  7043. return rc;
  7044. #else
  7045. /* TSG6 FW1.3 EasyWake */
  7046. int rc = 0;
  7047. int i = 0;
  7048. int report_length;
  7049. /* Validate report */
  7050. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7051. rc = -EINVAL;
  7052. pt_core_sleep_(cd);
  7053. goto exit;
  7054. }
  7055. /* Get gesture id and gesture data length */
  7056. cd->gesture_id = cd->input_buf[3];
  7057. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7058. cd->gesture_data_length = report_length - 4;
  7059. pt_debug(cd->dev, DL_INFO,
  7060. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7061. __func__, cd->gesture_id, cd->gesture_data_length);
  7062. for (i = 0; i < cd->gesture_data_length; i++)
  7063. cd->gesture_data[i] = cd->input_buf[4 + i];
  7064. /* attention WAKE */
  7065. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7066. exit:
  7067. return rc;
  7068. #endif
  7069. }
  7070. /*******************************************************************************
  7071. * FUNCTION: pt_get_touch_axis
  7072. *
  7073. * SUMMARY: Function to calculate touch axis
  7074. *
  7075. * PARAMETERS:
  7076. * *cd - pointer to core data structure
  7077. * *axis - pointer to axis calculation result
  7078. * size - size in bytes
  7079. * max - max value of result
  7080. * *xy_data - pointer to input data to be parsed
  7081. * bofs - bit offset
  7082. ******************************************************************************/
  7083. static void pt_get_touch_axis(struct pt_core_data *cd,
  7084. int *axis, int size, int max, u8 *data, int bofs)
  7085. {
  7086. int nbyte;
  7087. int next;
  7088. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7089. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7090. next++;
  7091. }
  7092. *axis &= max - 1;
  7093. }
  7094. /*******************************************************************************
  7095. * FUNCTION: move_tracking_heatmap_data
  7096. *
  7097. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7098. * system information structure, xy_mode and xy_data.
  7099. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7100. * the tthe_tuner sysfs node under the label "THM"
  7101. *
  7102. * RETURN:
  7103. * 0 = success
  7104. * !0 = failure
  7105. *
  7106. * PARAMETERS:
  7107. * *cd - pointer to core data
  7108. * *si - pointer to the system information structure
  7109. ******************************************************************************/
  7110. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7111. struct pt_sysinfo *si)
  7112. {
  7113. #ifdef TTHE_TUNER_SUPPORT
  7114. int size = get_unaligned_le16(&cd->input_buf[0]);
  7115. if (size)
  7116. tthe_print(cd, cd->input_buf, size, "THM=");
  7117. #endif
  7118. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7119. return 0;
  7120. }
  7121. /*******************************************************************************
  7122. * FUNCTION: move_sensor_data
  7123. *
  7124. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7125. * information structure, xy_mode and xy_data.
  7126. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7127. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7128. *
  7129. * RETURN:
  7130. * 0 = success
  7131. * !0 = failure
  7132. *
  7133. * PARAMETERS:
  7134. * *cd - pointer to core data
  7135. * *si - pointer to the system information structure
  7136. ******************************************************************************/
  7137. static int move_sensor_data(struct pt_core_data *cd,
  7138. struct pt_sysinfo *si)
  7139. {
  7140. #ifdef TTHE_TUNER_SUPPORT
  7141. int size = get_unaligned_le16(&cd->input_buf[0]);
  7142. if (size)
  7143. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7144. #endif
  7145. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7146. return 0;
  7147. }
  7148. /*******************************************************************************
  7149. * FUNCTION: move_button_data
  7150. *
  7151. * SUMMARY: Move the valid button data from the input buffer into the system
  7152. * information structure, xy_mode and xy_data.
  7153. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7154. * the tthe_tuner sysfs node under the label "OpModeData"
  7155. *
  7156. * RETURN:
  7157. * 0 = success
  7158. * !0 = failure
  7159. *
  7160. * PARAMETERS:
  7161. * *cd - pointer to core data
  7162. * *si - pointer to the system information structure
  7163. ******************************************************************************/
  7164. static int move_button_data(struct pt_core_data *cd,
  7165. struct pt_sysinfo *si)
  7166. {
  7167. #ifdef TTHE_TUNER_SUPPORT
  7168. int size = get_unaligned_le16(&cd->input_buf[0]);
  7169. if (size)
  7170. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7171. #endif
  7172. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7173. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7174. "xy_mode");
  7175. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7176. BTN_REPORT_SIZE);
  7177. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7178. "xy_data");
  7179. return 0;
  7180. }
  7181. /*******************************************************************************
  7182. * FUNCTION: move_touch_data
  7183. *
  7184. * SUMMARY: Move the valid touch data from the input buffer into the system
  7185. * information structure, xy_mode and xy_data.
  7186. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7187. * the tthe_tuner sysfs node under the label "OpModeData"
  7188. *
  7189. * RETURN:
  7190. * 0 = success
  7191. * !0 = failure
  7192. *
  7193. * PARAMETERS:
  7194. * *cd - pointer to core data
  7195. * *si - pointer to the system information structure
  7196. ******************************************************************************/
  7197. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7198. {
  7199. int max_tch = si->sensing_conf_data.max_tch;
  7200. int num_cur_tch;
  7201. int length;
  7202. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7203. #ifdef TTHE_TUNER_SUPPORT
  7204. int size = get_unaligned_le16(&cd->input_buf[0]);
  7205. if (size)
  7206. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7207. #endif
  7208. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7209. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7210. si->desc.tch_header_size, "xy_mode");
  7211. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7212. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7213. if (unlikely(num_cur_tch > max_tch))
  7214. num_cur_tch = max_tch;
  7215. length = num_cur_tch * si->desc.tch_record_size;
  7216. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7217. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7218. return 0;
  7219. }
  7220. /*******************************************************************************
  7221. * FUNCTION: move_hid_pen_data
  7222. *
  7223. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7224. * information structure, xy_mode and xy_data.
  7225. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7226. * the tthe_tuner sysfs node under the label "HID" starting with the
  7227. * report ID.
  7228. *
  7229. * RETURN:
  7230. * 0 = success
  7231. * !0 = failure
  7232. *
  7233. * PARAMETERS:
  7234. * *cd - pointer to core data
  7235. * *si - pointer to the system information structure
  7236. ******************************************************************************/
  7237. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7238. {
  7239. #ifdef TTHE_TUNER_SUPPORT
  7240. int size = get_unaligned_le16(&cd->input_buf[0]);
  7241. if (size) {
  7242. /*
  7243. * HID over USB does not require the two byte length field, so
  7244. * this should print from input_buf[2] but to keep both finger
  7245. * and pen reports the same the length is included
  7246. */
  7247. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7248. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7249. "HID-USB=");
  7250. else
  7251. tthe_print(cd, &(cd->input_buf[0]), size,
  7252. "HID-I2C=");
  7253. }
  7254. #endif
  7255. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7256. return 0;
  7257. }
  7258. /*******************************************************************************
  7259. * FUNCTION: parse_touch_input
  7260. *
  7261. * SUMMARY: Parse the touch report and take action based on the touch
  7262. * report_id.
  7263. *
  7264. * RETURN:
  7265. * 0 = success
  7266. * !0 = failure
  7267. *
  7268. * PARAMETERS:
  7269. * *cd - pointer to core data
  7270. * size - size of touch record
  7271. ******************************************************************************/
  7272. static int parse_touch_input(struct pt_core_data *cd, int size)
  7273. {
  7274. struct pt_sysinfo *si = &cd->sysinfo;
  7275. int report_id = cd->input_buf[2];
  7276. int rc = -EINVAL;
  7277. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7278. __func__);
  7279. if (!si->ready) {
  7280. pt_debug(cd->dev, DL_ERROR,
  7281. "%s: Need system information to parse touches\n",
  7282. __func__);
  7283. return 0;
  7284. }
  7285. if (!si->xy_mode || !si->xy_data)
  7286. return rc;
  7287. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7288. rc = move_touch_data(cd, si);
  7289. else if (report_id == PT_HID_PEN_REPORT_ID)
  7290. rc = move_hid_pen_data(cd, si);
  7291. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7292. rc = move_button_data(cd, si);
  7293. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7294. rc = move_sensor_data(cd, si);
  7295. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7296. rc = move_tracking_heatmap_data(cd, si);
  7297. if (rc)
  7298. return rc;
  7299. /* attention IRQ */
  7300. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7301. return 0;
  7302. }
  7303. /*******************************************************************************
  7304. * FUNCTION: parse_command_input
  7305. *
  7306. * SUMMARY: Move the response data from the input buffer to the response buffer
  7307. *
  7308. * RETURN:
  7309. * 0 = success
  7310. * !0 = failure
  7311. *
  7312. * PARAMETERS:
  7313. * *cd - pointer to core data
  7314. * size - size of response data
  7315. ******************************************************************************/
  7316. static int parse_command_input(struct pt_core_data *cd, int size)
  7317. {
  7318. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7319. __func__);
  7320. memcpy(cd->response_buf, cd->input_buf, size);
  7321. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7322. if (size && cd->show_tt_data) {
  7323. if (cd->pip2_prot_active)
  7324. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7325. else
  7326. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7327. }
  7328. #endif
  7329. mutex_lock(&cd->system_lock);
  7330. cd->hid_cmd_state = 0;
  7331. mutex_unlock(&cd->system_lock);
  7332. wake_up(&cd->wait_q);
  7333. return 0;
  7334. }
  7335. /*******************************************************************************
  7336. * FUNCTION: pt_allow_enumeration
  7337. *
  7338. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7339. * FW sentinel is seen.
  7340. *
  7341. * RETURN:
  7342. * true = allow enumeration or fully re-probe
  7343. * false = skip enumeration and fully re-probe
  7344. *
  7345. * PARAMETERS:
  7346. * *cd - pointer to core data
  7347. ******************************************************************************/
  7348. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7349. {
  7350. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7351. (!cd->hid_reset_cmd_state) &&
  7352. (cd->core_probe_complete) &&
  7353. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7354. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7355. (cd->mode == PT_MODE_OPERATIONAL)) {
  7356. return true;
  7357. }
  7358. if ((!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->active_dut_generation != DUT_PIP1_ONLY)) {
  7363. return true;
  7364. }
  7365. pt_debug(cd->dev, DL_INFO,
  7366. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7367. __func__,
  7368. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7369. "core_probe_complete(1)", cd->core_probe_complete,
  7370. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7371. "active_dut_gen(0,2)", cd->active_dut_generation);
  7372. return false;
  7373. }
  7374. /*******************************************************************************
  7375. * FUNCTION: pt_is_touch_report
  7376. *
  7377. * SUMMARY: Determine if a report ID should be treated as a touch report
  7378. *
  7379. * RETURN:
  7380. * true = report ID is a touch report
  7381. * false = report ID is not a touch report
  7382. *
  7383. * PARAMETERS:
  7384. * id - Report ID
  7385. ******************************************************************************/
  7386. static bool pt_is_touch_report(int id)
  7387. {
  7388. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7389. id == PT_HID_PEN_REPORT_ID ||
  7390. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7391. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7392. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7393. return true;
  7394. else
  7395. return false;
  7396. }
  7397. /*******************************************************************************
  7398. * FUNCTION: pt_parse_input
  7399. *
  7400. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7401. * on if its a response to a command or asynchronous touch data / reset
  7402. * sentinel. PIP2.x messages have additional error checking that is
  7403. * parsed (SEQ match from cmd to rsp, CRC valid).
  7404. * Look for special packets based on unique lengths:
  7405. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7406. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7407. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7408. * 0xFFXX - Empty buffer (PIP 1.7+)
  7409. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7410. * the initial probe has not yet been done.
  7411. *
  7412. * RETURN:
  7413. * 0 = success
  7414. * !0 = failure
  7415. *
  7416. * PARAMETERS:
  7417. * *cd - pointer to core data
  7418. ******************************************************************************/
  7419. static int pt_parse_input(struct pt_core_data *cd)
  7420. {
  7421. int report_id;
  7422. int cmd_id;
  7423. int is_command = 0;
  7424. int size;
  7425. int print_size;
  7426. bool touch_report = true;
  7427. unsigned short calc_crc;
  7428. unsigned short resp_crc;
  7429. cd->fw_sys_mode_in_standby_state = false;
  7430. size = get_unaligned_le16(&cd->input_buf[0]);
  7431. print_size = size;
  7432. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7433. __func__, print_size);
  7434. if (print_size <= PT_MAX_INPUT)
  7435. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7436. "<<< Read buf");
  7437. if (size == 0 ||
  7438. (size == 11 &&
  7439. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7440. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7441. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7442. PIP2_CMD_COMMAND_ID_MASK) ==
  7443. PIP2_CMD_ID_STATUS)) {
  7444. touch_report = false;
  7445. cd->hw_detected = true;
  7446. cd->bl_pip_ver_ready = false;
  7447. cd->app_pip_ver_ready = false;
  7448. if (size == 0) {
  7449. mutex_lock(&cd->system_lock);
  7450. cd->pip2_prot_active = false;
  7451. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7452. /*
  7453. * For Gen5/6 this sentinel could be from
  7454. * the BL or FW. Attempt to set the correct
  7455. * mode based on the previous PIP command.
  7456. */
  7457. if (cd->hid_cmd_state ==
  7458. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7459. cd->mode = PT_MODE_OPERATIONAL;
  7460. cd->startup_status =
  7461. STARTUP_STATUS_FW_RESET_SENTINEL;
  7462. } else if (cd->hid_cmd_state ==
  7463. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7464. cd->hid_reset_cmd_state) {
  7465. cd->mode = PT_MODE_BOOTLOADER;
  7466. cd->startup_status =
  7467. STARTUP_STATUS_BL_RESET_SENTINEL;
  7468. } else {
  7469. cd->mode = PT_MODE_UNKNOWN;
  7470. cd->startup_status =
  7471. STARTUP_STATUS_START;
  7472. }
  7473. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7474. pt_debug(cd->dev, DL_INFO,
  7475. "%s: ATM Gen5/6 %s sentinel received\n",
  7476. __func__,
  7477. (cd->mode == PT_MODE_OPERATIONAL ?
  7478. "FW" :
  7479. (cd->mode == PT_MODE_BOOTLOADER ?
  7480. "BL" : "Unknown")));
  7481. } else {
  7482. cd->mode = PT_MODE_OPERATIONAL;
  7483. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7484. cd->startup_status =
  7485. STARTUP_STATUS_FW_RESET_SENTINEL;
  7486. pt_debug(cd->dev, DL_INFO,
  7487. "%s: ATM PT/TT FW sentinel received\n",
  7488. __func__);
  7489. }
  7490. mutex_unlock(&cd->system_lock);
  7491. if (pt_allow_enumeration(cd)) {
  7492. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7493. pt_debug(cd->dev, DL_INFO,
  7494. "%s: Queue Restart\n", __func__);
  7495. pt_queue_restart(cd);
  7496. } else {
  7497. pt_debug(cd->dev, DL_INFO,
  7498. "%s: Queue Enum\n", __func__);
  7499. pt_queue_enum(cd);
  7500. }
  7501. } else {
  7502. pt_debug(cd->dev, DL_INFO,
  7503. "%s: Sentinel - No Queued Action\n",
  7504. __func__);
  7505. }
  7506. } else {
  7507. /* Sentinel must be from TT/TC BL */
  7508. mutex_lock(&cd->system_lock);
  7509. cd->pip2_prot_active = true;
  7510. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7511. cd->mode = PT_MODE_BOOTLOADER;
  7512. cd->sysinfo.ready = false;
  7513. mutex_unlock(&cd->system_lock);
  7514. pt_debug(cd->dev, DL_INFO,
  7515. "%s: BL Reset sentinel received\n", __func__);
  7516. if (cd->flashless_dut &&
  7517. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7518. pt_debug(cd->dev, DL_INFO,
  7519. "%s: BL to RAM for flashless DUT\n",
  7520. __func__);
  7521. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7522. }
  7523. }
  7524. mutex_lock(&cd->system_lock);
  7525. memcpy(cd->response_buf, cd->input_buf, 2);
  7526. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7527. mutex_unlock(&cd->system_lock);
  7528. pt_debug(cd->dev, DL_WARN,
  7529. "%s: Device Initiated Reset\n", __func__);
  7530. wake_up(&cd->wait_q);
  7531. return 0;
  7532. }
  7533. cd->hid_reset_cmd_state = 0;
  7534. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7535. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7536. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7537. cd->hid_cmd_state = 0;
  7538. wake_up(&cd->wait_q);
  7539. mutex_unlock(&cd->system_lock);
  7540. return 0;
  7541. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7542. /*
  7543. * This debug message below is used by PBATS to calculate the
  7544. * time from the last lift off IRQ to when FW exits LFT mode.
  7545. */
  7546. touch_report = false;
  7547. pt_debug(cd->dev, DL_WARN,
  7548. "%s: DUT - Empty buffer detected\n", __func__);
  7549. return 0;
  7550. } else if (size > PT_MAX_INPUT) {
  7551. pt_debug(cd->dev, DL_ERROR,
  7552. "%s: DUT - Unexpected len field in active bus data!\n",
  7553. __func__);
  7554. return -EINVAL;
  7555. }
  7556. if (cd->pip2_prot_active) {
  7557. pt_debug(cd->dev, DL_DEBUG,
  7558. "%s: Decode PIP2.x Response\n", __func__);
  7559. /* PIP2 does not have a report id, hard code it */
  7560. report_id = 0x00;
  7561. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7562. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7563. resp_crc = cd->input_buf[size - 2] << 8;
  7564. resp_crc |= cd->input_buf[size - 1];
  7565. if ((cd->pip2_cmd_tag_seq !=
  7566. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7567. (resp_crc != calc_crc) &&
  7568. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7569. == PT_PIP_TOUCH_REPORT_ID) ||
  7570. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7571. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7572. pt_debug(cd->dev, DL_WARN,
  7573. "%s: %s %d %s\n",
  7574. __func__,
  7575. "Received PIP1 report id =",
  7576. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7577. "when expecting a PIP2 report - IGNORE report");
  7578. return 0;
  7579. }
  7580. is_command = 1;
  7581. touch_report = false;
  7582. } else {
  7583. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7584. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7585. }
  7586. #ifdef TTDL_DIAGNOSTICS
  7587. pt_debug(cd->dev, DL_DEBUG,
  7588. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7589. __func__, cd->pip2_prot_active, report_id,
  7590. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7591. #endif /* TTDL_DIAGNOSTICS */
  7592. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7593. pt_wakeup_host(cd);
  7594. #ifdef TTHE_TUNER_SUPPORT
  7595. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7596. #endif
  7597. return 0;
  7598. }
  7599. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7600. msecs_to_jiffies(cd->watchdog_interval));
  7601. /*
  7602. * If it is PIP2 response, the report_id has been set to 0,
  7603. * so it will not be parsed as a touch packet.
  7604. */
  7605. if (!pt_is_touch_report(report_id)) {
  7606. is_command = 1;
  7607. touch_report = false;
  7608. }
  7609. if (unlikely(is_command)) {
  7610. parse_command_input(cd, size);
  7611. return 0;
  7612. }
  7613. if (touch_report)
  7614. parse_touch_input(cd, size);
  7615. return 0;
  7616. }
  7617. /*******************************************************************************
  7618. * FUNCTION: pt_read_input
  7619. *
  7620. * SUMMARY: Reads incoming data off of the active bus
  7621. *
  7622. * RETURN:
  7623. * 0 = success
  7624. * !0 = failure
  7625. *
  7626. * PARAMETERS:
  7627. * *cd - pointer to core data
  7628. ******************************************************************************/
  7629. static int pt_read_input(struct pt_core_data *cd)
  7630. {
  7631. struct device *dev = cd->dev;
  7632. int rc = 0;
  7633. int t;
  7634. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7635. /*
  7636. * Workaround for easywake failure
  7637. * Interrupt for easywake, wait for bus controller to wake
  7638. */
  7639. mutex_lock(&cd->system_lock);
  7640. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7641. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7642. mutex_unlock(&cd->system_lock);
  7643. if (!dev->power.is_suspended)
  7644. goto read;
  7645. t = wait_event_timeout(cd->wait_q,
  7646. (cd->wait_until_wake == 1),
  7647. msecs_to_jiffies(2000));
  7648. #ifdef TTDL_DIAGNOSTICS
  7649. if (IS_TMO(t)) {
  7650. cd->bus_transmit_error_count++;
  7651. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7652. pt_debug(dev, DL_ERROR,
  7653. "%s: !!!I2C Transmission Error %d\n",
  7654. __func__,
  7655. cd->bus_transmit_error_count);
  7656. }
  7657. #else
  7658. if (IS_TMO(t))
  7659. pt_queue_enum(cd);
  7660. #endif /* TTDL_DIAGNOSTICS */
  7661. goto read;
  7662. }
  7663. }
  7664. mutex_unlock(&cd->system_lock);
  7665. read:
  7666. /* Try reading up to 'retry' times */
  7667. while (retry-- != 0) {
  7668. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7669. PT_MAX_INPUT);
  7670. if (!rc) {
  7671. pt_debug(dev, DL_DEBUG,
  7672. "%s: Read input successfully\n", __func__);
  7673. goto read_exit;
  7674. }
  7675. usleep_range(1000, 2000);
  7676. }
  7677. pt_debug(dev, DL_ERROR,
  7678. "%s: Error getting report, rc=%d\n", __func__, rc);
  7679. read_exit:
  7680. return rc;
  7681. }
  7682. /*******************************************************************************
  7683. * FUNCTION: pt_irq
  7684. *
  7685. * SUMMARY: Process all detected interrupts
  7686. *
  7687. * RETURN:
  7688. * IRQ_HANDLED - Finished processing the interrupt
  7689. *
  7690. * PARAMETERS:
  7691. * irq - IRQ number
  7692. * *handle - pointer to core data struct
  7693. ******************************************************************************/
  7694. irqreturn_t pt_irq(int irq, void *handle)
  7695. {
  7696. struct pt_core_data *cd = handle;
  7697. int rc = 0;
  7698. if (!pt_check_irq_asserted(cd))
  7699. return IRQ_HANDLED;
  7700. rc = pt_read_input(cd);
  7701. #ifdef TTDL_DIAGNOSTICS
  7702. cd->irq_count++;
  7703. /* Used to calculate T-Refresh */
  7704. if (cd->t_refresh_active) {
  7705. if (cd->t_refresh_count == 0) {
  7706. cd->t_refresh_time = jiffies;
  7707. cd->t_refresh_count++;
  7708. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7709. cd->t_refresh_count++;
  7710. } else {
  7711. cd->t_refresh_active = 0;
  7712. cd->t_refresh_time =
  7713. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7714. }
  7715. }
  7716. #endif /* TTDL_DIAGNOSTICS */
  7717. if (!rc)
  7718. pt_parse_input(cd);
  7719. return IRQ_HANDLED;
  7720. }
  7721. /*******************************************************************************
  7722. * FUNCTION: _pt_subscribe_attention
  7723. *
  7724. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7725. * to subscribe themselves into the TTDL attention list
  7726. *
  7727. * RETURN:
  7728. * 0 = success
  7729. * !0 = failure
  7730. *
  7731. * PARAMETERS:
  7732. * *dev - pointer to device structure
  7733. * type - attention type enum
  7734. * *id - ID of the module calling this function
  7735. * *func - callback function pointer to be called when notified
  7736. * mode - attention mode
  7737. ******************************************************************************/
  7738. int _pt_subscribe_attention(struct device *dev,
  7739. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7740. int mode)
  7741. {
  7742. struct pt_core_data *cd = dev_get_drvdata(dev);
  7743. struct atten_node *atten, *atten_new;
  7744. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7745. if (!atten_new)
  7746. return -ENOMEM;
  7747. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7748. dev_name(cd->dev));
  7749. spin_lock(&cd->spinlock);
  7750. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7751. if (atten->id == id && atten->mode == mode) {
  7752. spin_unlock(&cd->spinlock);
  7753. kfree(atten_new);
  7754. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7755. __func__,
  7756. "already subscribed attention",
  7757. dev, "mode", mode);
  7758. return 0;
  7759. }
  7760. }
  7761. atten_new->id = id;
  7762. atten_new->dev = dev;
  7763. atten_new->mode = mode;
  7764. atten_new->func = func;
  7765. list_add(&atten_new->node, &cd->atten_list[type]);
  7766. spin_unlock(&cd->spinlock);
  7767. return 0;
  7768. }
  7769. /*******************************************************************************
  7770. * FUNCTION: _pt_unsubscribe_attention
  7771. *
  7772. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7773. * to unsubscribe themselves from the TTDL attention list
  7774. *
  7775. * RETURN:
  7776. * 0 = success
  7777. * !0 = failure
  7778. *
  7779. * PARAMETERS:
  7780. * *dev - pointer to device structure
  7781. * type - attention type enum
  7782. * *id - ID of the module calling this function
  7783. * *func - function pointer
  7784. * mode - attention mode
  7785. ******************************************************************************/
  7786. int _pt_unsubscribe_attention(struct device *dev,
  7787. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7788. int mode)
  7789. {
  7790. struct pt_core_data *cd = dev_get_drvdata(dev);
  7791. struct atten_node *atten, *atten_n;
  7792. spin_lock(&cd->spinlock);
  7793. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7794. if (atten->id == id && atten->mode == mode) {
  7795. list_del(&atten->node);
  7796. spin_unlock(&cd->spinlock);
  7797. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7798. __func__,
  7799. "unsub for atten->dev", atten->dev,
  7800. "atten->mode", atten->mode);
  7801. kfree(atten);
  7802. return 0;
  7803. }
  7804. }
  7805. spin_unlock(&cd->spinlock);
  7806. return -ENODEV;
  7807. }
  7808. /*******************************************************************************
  7809. * FUNCTION: _pt_request_exclusive
  7810. *
  7811. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7812. * to request exclusive access
  7813. *
  7814. * RETURN:
  7815. * 0 = success
  7816. * !0 = failure
  7817. *
  7818. * PARAMETERS:
  7819. * *dev - pointer to device structure
  7820. * timeout_ms - timeout to wait for exclusive access
  7821. ******************************************************************************/
  7822. static int _pt_request_exclusive(struct device *dev,
  7823. int timeout_ms)
  7824. {
  7825. struct pt_core_data *cd = dev_get_drvdata(dev);
  7826. return request_exclusive(cd, (void *)dev, timeout_ms);
  7827. }
  7828. /*******************************************************************************
  7829. * FUNCTION: _pt_release_exclusive
  7830. *
  7831. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7832. * to release exclusive access
  7833. *
  7834. * RETURN:
  7835. * 0 = success
  7836. * !0 = failure
  7837. *
  7838. * PARAMETERS:
  7839. * *dev - pointer to device structure
  7840. ******************************************************************************/
  7841. static int _pt_release_exclusive(struct device *dev)
  7842. {
  7843. struct pt_core_data *cd = dev_get_drvdata(dev);
  7844. return release_exclusive(cd, (void *)dev);
  7845. }
  7846. /*******************************************************************************
  7847. * FUNCTION: _pt_request_reset
  7848. *
  7849. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7850. * to request the DUT to be reset. Function returns err if refused or
  7851. * timeout occurs (Note: core uses fixed timeout period).
  7852. *
  7853. * NOTE: Function blocks until ISR occurs.
  7854. *
  7855. * RETURN:
  7856. * 0 = success
  7857. * !0 = failure
  7858. *
  7859. * PARAMETERS:
  7860. * *dev - pointer to device structure
  7861. * protect - flag to call protected or non-protected
  7862. ******************************************************************************/
  7863. static int _pt_request_reset(struct device *dev, int protect)
  7864. {
  7865. struct pt_core_data *cd = dev_get_drvdata(dev);
  7866. int rc;
  7867. rc = pt_dut_reset(cd, protect);
  7868. if (rc < 0) {
  7869. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7870. __func__, rc);
  7871. }
  7872. return rc;
  7873. }
  7874. /*******************************************************************************
  7875. * FUNCTION: _pt_request_enum
  7876. *
  7877. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7878. * to request TTDL to queue an enum. This function will return err
  7879. * if refused; if no error then enum has completed and system is in
  7880. * normal operation mode.
  7881. *
  7882. * RETURN:
  7883. * 0 = success
  7884. * !0 = failure
  7885. *
  7886. * PARAMETERS:
  7887. * *dev - pointer to device structure
  7888. * wait - boolean to determine if to wait for startup event
  7889. ******************************************************************************/
  7890. static int _pt_request_enum(struct device *dev, bool wait)
  7891. {
  7892. struct pt_core_data *cd = dev_get_drvdata(dev);
  7893. pt_queue_enum(cd);
  7894. if (wait)
  7895. wait_event_timeout(cd->wait_q,
  7896. cd->startup_state == STARTUP_NONE,
  7897. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7898. return 0;
  7899. }
  7900. /*******************************************************************************
  7901. * FUNCTION: _pt_request_sysinfo
  7902. *
  7903. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7904. * to request the pointer to the system information structure. This
  7905. * function will return NULL if sysinfo has not been acquired from the
  7906. * DUT yet.
  7907. *
  7908. * RETURN: Pointer to the system information struct
  7909. *
  7910. * PARAMETERS:
  7911. * *dev - pointer to device structure
  7912. ******************************************************************************/
  7913. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7914. {
  7915. struct pt_core_data *cd = dev_get_drvdata(dev);
  7916. int rc = 0;
  7917. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7918. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7919. !cd->sysinfo.ready) {
  7920. rc = pt_hid_output_get_sysinfo_(cd);
  7921. if (rc) {
  7922. pt_debug(cd->dev, DL_ERROR,
  7923. "%s: Error getting sysinfo rc=%d\n",
  7924. __func__, rc);
  7925. }
  7926. }
  7927. if (cd->sysinfo.ready)
  7928. return &cd->sysinfo;
  7929. return NULL;
  7930. }
  7931. /*******************************************************************************
  7932. * FUNCTION: _pt_request_loader_pdata
  7933. *
  7934. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7935. * to request the pointer to the loader platform data
  7936. *
  7937. * RETURN: Pointer to the loader platform data struct
  7938. *
  7939. * PARAMETERS:
  7940. * *dev - pointer to device structure
  7941. ******************************************************************************/
  7942. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7943. struct device *dev)
  7944. {
  7945. struct pt_core_data *cd = dev_get_drvdata(dev);
  7946. return cd->pdata->loader_pdata;
  7947. }
  7948. /*******************************************************************************
  7949. * FUNCTION: _pt_request_start_wd
  7950. *
  7951. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7952. * to request to start the TTDL watchdog
  7953. *
  7954. * RETURN:
  7955. * 0 = success
  7956. * !0 = failure
  7957. *
  7958. * PARAMETERS:
  7959. * *dev - pointer to device structure
  7960. ******************************************************************************/
  7961. static int _pt_request_start_wd(struct device *dev)
  7962. {
  7963. struct pt_core_data *cd = dev_get_drvdata(dev);
  7964. pt_start_wd_timer(cd);
  7965. return 0;
  7966. }
  7967. /*******************************************************************************
  7968. * FUNCTION: _pt_request_stop_wd
  7969. *
  7970. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7971. * to request to stop the TTDL watchdog
  7972. *
  7973. * RETURN:
  7974. * 0 = success
  7975. * !0 = failure
  7976. *
  7977. * PARAMETERS:
  7978. * *dev - pointer to device structure
  7979. ******************************************************************************/
  7980. static int _pt_request_stop_wd(struct device *dev)
  7981. {
  7982. struct pt_core_data *cd = dev_get_drvdata(dev);
  7983. pt_stop_wd_timer(cd);
  7984. return 0;
  7985. }
  7986. /*******************************************************************************
  7987. * FUNCTION: pt_pip2_launch_app
  7988. *
  7989. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7990. * the FW reset sentinel to indicate the function succeeded.
  7991. *
  7992. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7993. * in a timeout delay and with the DUT being reset with an XRES.
  7994. *
  7995. * RETURN:
  7996. * 0 = success
  7997. * !0 = failure
  7998. *
  7999. * PARAMETERS:
  8000. * *cd - pointer to core data structure
  8001. * protect - flag to call protected or non-protected
  8002. ******************************************************************************/
  8003. static int pt_pip2_launch_app(struct device *dev, int protect)
  8004. {
  8005. struct pt_core_data *cd = dev_get_drvdata(dev);
  8006. u16 actual_read_len;
  8007. u16 tmp_startup_status = cd->startup_status;
  8008. u8 read_buf[12];
  8009. u8 status;
  8010. int time = 0;
  8011. int rc = 0;
  8012. mutex_lock(&cd->system_lock);
  8013. cd->startup_status = STARTUP_STATUS_START;
  8014. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8015. mutex_unlock(&cd->system_lock);
  8016. rc = _pt_request_pip2_send_cmd(dev, protect,
  8017. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8018. &actual_read_len);
  8019. if (rc) {
  8020. pt_debug(dev, DL_ERROR,
  8021. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8022. __func__, rc);
  8023. } else {
  8024. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8025. /* Test for no or invalid image in FLASH, no point to reset */
  8026. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8027. rc = status;
  8028. goto exit;
  8029. }
  8030. /* Any other boot failure */
  8031. if (status != 0) {
  8032. pt_debug(dev, DL_ERROR,
  8033. "%s: FW did not EXECUTE, status = %d\n",
  8034. __func__, status);
  8035. rc = status;
  8036. }
  8037. }
  8038. if (rc) {
  8039. pt_debug(dev, DL_ERROR,
  8040. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8041. __func__, rc);
  8042. goto exit;
  8043. }
  8044. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8045. msleep(20);
  8046. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8047. __func__, time, cd->startup_status);
  8048. time += 20;
  8049. }
  8050. if (cd->startup_status == STARTUP_STATUS_START) {
  8051. pt_debug(cd->dev, DL_WARN,
  8052. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8053. rc = -ETIME;
  8054. }
  8055. exit:
  8056. if (cd->startup_status == STARTUP_STATUS_START) {
  8057. /* Reset to original state because we could be stuck in BL */
  8058. mutex_lock(&cd->system_lock);
  8059. cd->startup_status = tmp_startup_status;
  8060. mutex_unlock(&cd->system_lock);
  8061. }
  8062. return rc;
  8063. }
  8064. /*******************************************************************************
  8065. * FUNCTION: _pt_request_pip2_launch_app
  8066. *
  8067. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8068. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8069. * consumed.
  8070. *
  8071. * RETURN:
  8072. * 0 = success
  8073. * !0 = failure
  8074. *
  8075. * PARAMETERS:
  8076. * *cd - pointer to core data structure
  8077. ******************************************************************************/
  8078. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8079. {
  8080. return pt_pip2_launch_app(dev, protect);
  8081. }
  8082. /*******************************************************************************
  8083. * FUNCTION: _pt_request_wait_for_enum_state
  8084. *
  8085. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8086. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8087. *
  8088. * RETURN:
  8089. * 0 = success
  8090. * !0 = failure
  8091. *
  8092. * PARAMETERS:
  8093. * *cd - pointer to core data structure
  8094. * timeout - timeout for how long to wait
  8095. * state - enum state to wait for
  8096. ******************************************************************************/
  8097. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8098. int state)
  8099. {
  8100. int rc = 0;
  8101. int t;
  8102. struct pt_core_data *cd = dev_get_drvdata(dev);
  8103. t = wait_event_timeout(cd->wait_q,
  8104. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8105. msecs_to_jiffies(timeout));
  8106. if (IS_TMO(t)) {
  8107. pt_debug(cd->dev, DL_ERROR,
  8108. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8109. __func__, state, timeout);
  8110. pt_debug(cd->dev, DL_WARN,
  8111. "%s: enum state reached 0x%04X\n",
  8112. __func__, cd->startup_status);
  8113. rc = -ETIME;
  8114. } else if (cd->startup_status & state) {
  8115. pt_debug(cd->dev, DL_INFO,
  8116. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8117. __func__, cd->startup_status,
  8118. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8119. } else {
  8120. if (t == 1) {
  8121. pt_debug(
  8122. cd->dev, DL_ERROR,
  8123. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8124. __func__, state, timeout);
  8125. rc = -ETIME;
  8126. } else {
  8127. pt_debug(
  8128. cd->dev, DL_ERROR,
  8129. "%s: Enum state 0x%04X not reached in %dms\n",
  8130. __func__, state, timeout - jiffies_to_msecs(t));
  8131. rc = -EINVAL;
  8132. }
  8133. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8134. __func__, cd->startup_status);
  8135. }
  8136. return rc;
  8137. }
  8138. /*******************************************************************************
  8139. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8140. *
  8141. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8142. * deep sleep.
  8143. *
  8144. * RETURN:
  8145. * 0 = success
  8146. * !0 = error
  8147. *
  8148. * PARAMETERS:
  8149. * *cd - pointer to core data
  8150. ******************************************************************************/
  8151. static int pt_core_wake_device_from_deep_sleep_(
  8152. struct pt_core_data *cd)
  8153. {
  8154. int rc;
  8155. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8156. if (rc)
  8157. rc = -EAGAIN;
  8158. /* Prevent failure on sequential wake/sleep requests from OS */
  8159. msleep(20);
  8160. return rc;
  8161. }
  8162. /*******************************************************************************
  8163. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8164. *
  8165. * SUMMARY: Wake up device from Easy-Wake state.
  8166. *
  8167. * RETURN:
  8168. * 0 = success
  8169. * !0 = failure
  8170. *
  8171. * PARAMETERS:
  8172. * *cd - pointer to core data
  8173. ******************************************************************************/
  8174. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8175. {
  8176. int rc = 0;
  8177. mutex_lock(&cd->system_lock);
  8178. cd->wait_until_wake = 1;
  8179. mutex_unlock(&cd->system_lock);
  8180. wake_up(&cd->wait_q);
  8181. msleep(20);
  8182. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8183. return rc;
  8184. }
  8185. /*******************************************************************************
  8186. * FUNCTION: pt_restore_parameters_
  8187. *
  8188. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8189. * back to the DUT
  8190. *
  8191. * RETURN:
  8192. * 0 = success
  8193. * !0 = failure
  8194. *
  8195. * PARAMETERS:
  8196. * *cd - pointer the core data structure
  8197. ******************************************************************************/
  8198. static int pt_restore_parameters_(struct pt_core_data *cd)
  8199. {
  8200. struct param_node *param;
  8201. int rc = 0;
  8202. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8203. goto exit;
  8204. spin_lock(&cd->spinlock);
  8205. list_for_each_entry(param, &cd->param_list, node) {
  8206. spin_unlock(&cd->spinlock);
  8207. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8208. __func__, param->id, param->value);
  8209. rc = pt_pip_set_param_(cd, param->id,
  8210. param->value, param->size);
  8211. if (rc)
  8212. goto exit;
  8213. spin_lock(&cd->spinlock);
  8214. }
  8215. spin_unlock(&cd->spinlock);
  8216. exit:
  8217. return rc;
  8218. }
  8219. /*******************************************************************************
  8220. * FUNCTION: pt_pip2_exit_bl_
  8221. *
  8222. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8223. * a DUT that may not have flash and will need FW to be loaded into RAM
  8224. *
  8225. * RETURN:
  8226. * 0 = success
  8227. * !0 = failure
  8228. *
  8229. * PARAMETERS:
  8230. * *cd - pointer the core data structure
  8231. * *status_str - pointer to optional status string buffer
  8232. * buf_size - size of status_str buffer
  8233. ******************************************************************************/
  8234. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8235. {
  8236. int rc;
  8237. int wait_time = 0;
  8238. u8 mode = PT_MODE_UNKNOWN;
  8239. bool load_status_str = false;
  8240. /*
  8241. * Below function has protective call to ensure no enum is still on
  8242. * going, while this kind of protection should be applied widely in
  8243. * future (TODO).
  8244. */
  8245. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8246. if (status_str && buf_size <= 50)
  8247. load_status_str = true;
  8248. if (mode == PT_MODE_BOOTLOADER) {
  8249. if (cd->flashless_dut == 1) {
  8250. rc = pt_hw_hard_reset(cd);
  8251. } else {
  8252. rc = pt_pip2_launch_app(cd->dev,
  8253. PT_CORE_CMD_UNPROTECTED);
  8254. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8255. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8256. __func__, "Invalid image in FLASH rc", rc);
  8257. } else if (rc) {
  8258. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8259. __func__, "Failed to launch app rc", rc);
  8260. }
  8261. }
  8262. if (!rc) {
  8263. if (cd->flashless_dut == 1) {
  8264. /* Wait for BL to complete before enum */
  8265. rc = _pt_request_wait_for_enum_state(cd->dev,
  8266. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8267. if (rc && load_status_str) {
  8268. strlcpy(status_str, "No FW sentinel after BL",
  8269. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8270. goto exit;
  8271. }
  8272. }
  8273. /*
  8274. * If the host wants to interact with the FW or do a
  8275. * forced calibration, the FW must be out of BOOT mode
  8276. * and the system information must have been retrieved.
  8277. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8278. * If, however, the enumeration does not reach this
  8279. * point, the DUT may still be in APP mode so test
  8280. * for all conditions.
  8281. */
  8282. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8283. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8284. if (!rc || cd->startup_status >=
  8285. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8286. mutex_lock(&cd->system_lock);
  8287. cd->mode = PT_MODE_OPERATIONAL;
  8288. mutex_unlock(&cd->system_lock);
  8289. }
  8290. if (rc) {
  8291. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8292. __func__, "Failed to enum with DUT",
  8293. cd->startup_status);
  8294. if (load_status_str && !(cd->startup_status &
  8295. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8296. strlcpy(status_str, "FW Stuck in Boot mode",
  8297. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8298. goto exit;
  8299. }
  8300. }
  8301. /*
  8302. * The coming FW sentinel could wake up the event
  8303. * queue, which has chance to be taken by next command
  8304. * wrongly. Following delay is a workaround to avoid
  8305. * this issue for most situations.
  8306. */
  8307. msleep(20);
  8308. pt_start_wd_timer(cd);
  8309. }
  8310. if (load_status_str) {
  8311. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8312. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8313. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8314. else if (!rc)
  8315. strlcpy(status_str, "Entered APP from BL mode",
  8316. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8317. else
  8318. strlcpy(status_str, "Failed to enter APP from BL mode",
  8319. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8320. }
  8321. } else if (mode == PT_MODE_OPERATIONAL) {
  8322. mutex_lock(&cd->system_lock);
  8323. cd->mode = mode;
  8324. mutex_unlock(&cd->system_lock);
  8325. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8326. if (load_status_str) {
  8327. if (!rc)
  8328. strlcpy(status_str, "Already in APP mode",
  8329. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8330. else
  8331. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8332. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8333. }
  8334. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8335. mutex_lock(&cd->system_lock);
  8336. cd->mode = mode;
  8337. mutex_unlock(&cd->system_lock);
  8338. if (load_status_str)
  8339. strlcpy(status_str, "Failed to determine active mode",
  8340. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8341. }
  8342. exit:
  8343. if (!rc)
  8344. pt_start_wd_timer(cd);
  8345. return rc;
  8346. }
  8347. /*******************************************************************************
  8348. * FUNCTION: pt_pip2_exit_bl
  8349. *
  8350. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8351. * access.
  8352. *
  8353. * RETURN:
  8354. * 0 = success
  8355. * !0 = failure
  8356. *
  8357. * PARAMETERS:
  8358. * *cd - pointer the core data structure
  8359. * *status_str - pointer to optional status string buffer
  8360. * buf_size - size of status_str buffer
  8361. ******************************************************************************/
  8362. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8363. {
  8364. int rc;
  8365. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8366. if (rc < 0) {
  8367. pt_debug(cd->dev, DL_ERROR,
  8368. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8369. cd->exclusive_dev, cd->dev);
  8370. return rc;
  8371. }
  8372. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8373. if (release_exclusive(cd, cd->dev) < 0)
  8374. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8375. __func__);
  8376. return rc;
  8377. }
  8378. /*******************************************************************************
  8379. * FUNCTION: _fast_startup
  8380. *
  8381. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8382. *
  8383. * RETURN:
  8384. * 0 = success
  8385. * !0 = failure
  8386. *
  8387. * PARAMETERS:
  8388. * *cd - pointer the core data structure
  8389. ******************************************************************************/
  8390. static int _fast_startup(struct pt_core_data *cd)
  8391. {
  8392. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8393. int rc = 0;
  8394. u8 mode = PT_MODE_UNKNOWN;
  8395. struct pt_hid_desc hid_desc;
  8396. int wait_time = 0;
  8397. memset(&hid_desc, 0, sizeof(hid_desc));
  8398. reset:
  8399. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8400. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8401. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8402. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8403. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8404. __func__);
  8405. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8406. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8407. if (rc < 0) {
  8408. pt_debug(cd->dev, DL_ERROR,
  8409. "%s: Error on getting HID descriptor r=%d\n",
  8410. __func__, rc);
  8411. if (retry--)
  8412. goto reset;
  8413. goto exit;
  8414. }
  8415. cd->mode = pt_get_mode(cd, &hid_desc);
  8416. if (cd->mode == PT_MODE_BOOTLOADER) {
  8417. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8418. __func__);
  8419. rc = pt_hid_output_bl_launch_app_(cd);
  8420. if (rc < 0) {
  8421. pt_debug(cd->dev, DL_ERROR,
  8422. "%s: Error on launch app r=%d\n",
  8423. __func__, rc);
  8424. if (retry--)
  8425. goto reset;
  8426. goto exit;
  8427. }
  8428. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8429. if (rc < 0) {
  8430. pt_debug(cd->dev, DL_ERROR,
  8431. "%s: Error on getting HID descriptor r=%d\n",
  8432. __func__, rc);
  8433. if (retry--)
  8434. goto reset;
  8435. goto exit;
  8436. }
  8437. cd->mode = pt_get_mode(cd, &hid_desc);
  8438. if (cd->mode == PT_MODE_BOOTLOADER) {
  8439. if (retry--)
  8440. goto reset;
  8441. goto exit;
  8442. }
  8443. }
  8444. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8445. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8446. } else {
  8447. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8448. __func__);
  8449. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8450. /* Wait for any sentinel before first try */
  8451. rc = _pt_request_wait_for_enum_state(
  8452. cd->dev, 150,
  8453. STARTUP_STATUS_BL_RESET_SENTINEL |
  8454. STARTUP_STATUS_FW_RESET_SENTINEL);
  8455. if (rc)
  8456. pt_debug(cd->dev, DL_ERROR,
  8457. "%s: No Sentinel detected rc = %d\n",
  8458. __func__, rc);
  8459. } else
  8460. pt_flush_bus_if_irq_asserted(cd,
  8461. PT_FLUSH_BUS_BASED_ON_LEN);
  8462. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8463. if (rc) {
  8464. pt_debug(cd->dev, DL_ERROR,
  8465. "%s: Get mode failed, mode unknown\n",
  8466. __func__);
  8467. mode = PT_MODE_UNKNOWN;
  8468. }
  8469. cd->mode = mode;
  8470. if (cd->mode == PT_MODE_BOOTLOADER) {
  8471. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8472. __func__);
  8473. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8474. if (rc) {
  8475. pt_debug(cd->dev, DL_ERROR,
  8476. "%s Failed to exit bootloader\n",
  8477. __func__);
  8478. msleep(50);
  8479. rc = -ENODEV;
  8480. if (retry--)
  8481. goto reset;
  8482. goto exit;
  8483. } else {
  8484. pt_debug(cd->dev, DL_INFO,
  8485. "%s: Exit bootloader successfully\n",
  8486. __func__);
  8487. }
  8488. if (cd->mode != PT_MODE_OPERATIONAL) {
  8489. pt_debug(cd->dev, DL_WARN,
  8490. "%s: restore mode failure mode = %d\n",
  8491. __func__, cd->mode);
  8492. if (retry--)
  8493. goto reset;
  8494. goto exit;
  8495. }
  8496. }
  8497. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8498. }
  8499. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8500. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8501. if (!rc) {
  8502. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8503. pt_debug(cd->dev, DL_WARN,
  8504. "%s: Exit FW BOOT Mode after %dms\n",
  8505. __func__, wait_time);
  8506. } else {
  8507. pt_debug(cd->dev, DL_WARN,
  8508. "%s: FW stuck in BOOT Mode after %dms\n",
  8509. __func__, wait_time);
  8510. goto exit;
  8511. }
  8512. if (!cd->sysinfo.ready) {
  8513. rc = pt_hid_output_get_sysinfo_(cd);
  8514. if (rc) {
  8515. pt_debug(cd->dev, DL_ERROR,
  8516. "%s: Error on getting sysinfo r=%d\n",
  8517. __func__, rc);
  8518. if (retry--)
  8519. goto reset;
  8520. goto exit;
  8521. }
  8522. }
  8523. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8524. rc = pt_restore_parameters_(cd);
  8525. if (rc)
  8526. pt_debug(cd->dev, DL_ERROR,
  8527. "%s: failed to restore parameters rc=%d\n",
  8528. __func__, rc);
  8529. else
  8530. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8531. exit:
  8532. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8533. return rc;
  8534. }
  8535. /*******************************************************************************
  8536. * FUNCTION: pt_core_poweron_device_
  8537. *
  8538. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8539. *
  8540. * RETURN:
  8541. * 0 = success
  8542. * !0 = failure
  8543. *
  8544. * PARAMETERS:
  8545. * *cd - pointer to core data
  8546. ******************************************************************************/
  8547. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8548. {
  8549. struct device *dev = cd->dev;
  8550. int rc = 0;
  8551. /*
  8552. * After power on action, the chip can general FW sentinel. It can
  8553. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8554. * _fast_startup() can perform a quick enumeration too, here doesn't
  8555. * need another enumeration.
  8556. */
  8557. mutex_lock(&cd->system_lock);
  8558. cd->startup_status = STARTUP_STATUS_START;
  8559. cd->hid_reset_cmd_state = 1;
  8560. mutex_unlock(&cd->system_lock);
  8561. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8562. if (rc < 0) {
  8563. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8564. __func__, rc);
  8565. goto exit;
  8566. }
  8567. if (!cd->irq_enabled) {
  8568. cd->irq_enabled = true;
  8569. enable_irq(cd->irq);
  8570. }
  8571. /* TBD: following function doesn't update startup_status */
  8572. rc = _fast_startup(cd);
  8573. exit:
  8574. return rc;
  8575. }
  8576. /*******************************************************************************
  8577. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8578. *
  8579. * SUMMARY: Reset device, and then trigger a full enumeration.
  8580. *
  8581. * RETURN:
  8582. * 0 = success
  8583. * !0 = failure
  8584. *
  8585. * PARAMETERS:
  8586. * *cd - pointer to core data
  8587. ******************************************************************************/
  8588. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8589. {
  8590. int rc;
  8591. rc = pt_dut_reset_and_wait(cd);
  8592. if (rc < 0) {
  8593. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8594. __func__, rc);
  8595. goto exit;
  8596. }
  8597. rc = _fast_startup(cd);
  8598. exit:
  8599. return rc;
  8600. }
  8601. /*******************************************************************************
  8602. * FUNCTION: pt_core_easywake_off_
  8603. *
  8604. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8605. * the configuration in the core platform data structure.
  8606. *
  8607. * RETURN:
  8608. * 0 = success
  8609. * !0 = failure
  8610. *
  8611. * PARAMETERS:
  8612. * *cd - pointer to core data
  8613. ******************************************************************************/
  8614. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8615. {
  8616. int rc = 0;
  8617. mutex_lock(&cd->system_lock);
  8618. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8619. mutex_unlock(&cd->system_lock);
  8620. pt_debug(cd->dev, DL_INFO,
  8621. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8622. return 0;
  8623. }
  8624. mutex_unlock(&cd->system_lock);
  8625. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8626. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8627. rc = pt_core_wake_device_from_easy_wake_(cd);
  8628. if (rc < 0)
  8629. pt_debug(cd->dev, DL_ERROR,
  8630. "%s - %d failed %d\n", __func__, rc);
  8631. }
  8632. mutex_lock(&cd->system_lock);
  8633. cd->sleep_state = SS_EASY_WAKING_OFF;
  8634. mutex_unlock(&cd->system_lock);
  8635. pt_start_wd_timer(cd);
  8636. return rc;
  8637. }
  8638. /*******************************************************************************
  8639. * FUNCTION: pt_core_easywake_off
  8640. *
  8641. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8642. *
  8643. * RETURN:
  8644. * 0 = success
  8645. * !0 = failure
  8646. *
  8647. * PARAMETERS:
  8648. * *cd - pointer to core data
  8649. ******************************************************************************/
  8650. static int pt_core_easywake_off(struct pt_core_data *cd)
  8651. {
  8652. int rc;
  8653. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8654. if (rc < 0) {
  8655. pt_debug(cd->dev, DL_ERROR,
  8656. "%s: fail get exclusive ex=%p own=%p\n",
  8657. __func__, cd->exclusive_dev, cd->dev);
  8658. return rc;
  8659. }
  8660. rc = pt_core_easywake_off_(cd);
  8661. if (release_exclusive(cd, cd->dev) < 0)
  8662. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8663. __func__);
  8664. else
  8665. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8666. __func__);
  8667. return rc;
  8668. }
  8669. /*******************************************************************************
  8670. * FUNCTION: pt_core_wake_
  8671. *
  8672. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8673. * the configuration in the core platform data structure.
  8674. *
  8675. * RETURN:
  8676. * 0 = success
  8677. * !0 = failure
  8678. *
  8679. * PARAMETERS:
  8680. * *cd - pointer to core data
  8681. ******************************************************************************/
  8682. static int pt_core_wake_(struct pt_core_data *cd)
  8683. {
  8684. int rc = 0;
  8685. mutex_lock(&cd->system_lock);
  8686. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8687. mutex_unlock(&cd->system_lock);
  8688. pt_debug(cd->dev, DL_INFO,
  8689. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8690. return 0;
  8691. } else {
  8692. cd->sleep_state = SS_WAKING;
  8693. }
  8694. mutex_unlock(&cd->system_lock);
  8695. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8696. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8697. pt_debug(cd->dev, DL_INFO,
  8698. "%s: Entering into poweron mode:\n", __func__);
  8699. rc = pt_core_poweron_device_(cd);
  8700. if (rc < 0)
  8701. pr_err("%s: Poweron error detected: rc=%d\n",
  8702. __func__, rc);
  8703. }
  8704. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8705. rc = pt_core_wake_device_from_deep_standby_(cd);
  8706. else /* Default action to exit DeepSleep */
  8707. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8708. }
  8709. mutex_lock(&cd->system_lock);
  8710. cd->sleep_state = SS_SLEEP_OFF;
  8711. mutex_unlock(&cd->system_lock);
  8712. pt_start_wd_timer(cd);
  8713. return rc;
  8714. }
  8715. /*******************************************************************************
  8716. * FUNCTION: pt_core_wake_
  8717. *
  8718. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8719. *
  8720. * RETURN:
  8721. * 0 = success
  8722. * !0 = failure
  8723. *
  8724. * PARAMETERS:
  8725. * *cd - pointer to core data
  8726. ******************************************************************************/
  8727. static int pt_core_wake(struct pt_core_data *cd)
  8728. {
  8729. int rc = 0;
  8730. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8731. if (rc < 0) {
  8732. pt_debug(cd->dev, DL_ERROR,
  8733. "%s: fail get exclusive ex=%p own=%p\n",
  8734. __func__, cd->exclusive_dev, cd->dev);
  8735. return rc;
  8736. }
  8737. rc = pt_core_wake_(cd);
  8738. if (release_exclusive(cd, cd->dev) < 0)
  8739. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8740. __func__);
  8741. else
  8742. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8743. __func__);
  8744. return rc;
  8745. }
  8746. /*******************************************************************************
  8747. * FUNCTION: pt_get_ic_crc_
  8748. *
  8749. * SUMMARY: This function retrieves the config block CRC
  8750. *
  8751. * NOTE: The post condition of calling this function will be that the DUT will
  8752. * be in SCANNINING mode if no failures occur
  8753. *
  8754. * RETURN:
  8755. * 0 = success
  8756. * !0 = failure
  8757. *
  8758. * PARAMETERS:
  8759. * *cd - pointer the core data structure
  8760. * ebid - enumerated block ID
  8761. ******************************************************************************/
  8762. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8763. {
  8764. struct pt_sysinfo *si = &cd->sysinfo;
  8765. int rc = 0;
  8766. u8 status;
  8767. u16 calculated_crc = 0;
  8768. u16 stored_crc = 0;
  8769. rc = pt_pip_suspend_scanning_(cd);
  8770. if (rc)
  8771. goto error;
  8772. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8773. &calculated_crc, &stored_crc);
  8774. if (rc)
  8775. goto exit;
  8776. if (status) {
  8777. rc = -EINVAL;
  8778. goto exit;
  8779. }
  8780. si->ttconfig.crc = stored_crc;
  8781. exit:
  8782. pt_pip_resume_scanning_(cd);
  8783. error:
  8784. pt_debug(cd->dev, DL_INFO,
  8785. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8786. __func__, ebid, calculated_crc, stored_crc, rc);
  8787. return rc;
  8788. }
  8789. /*******************************************************************************
  8790. * FUNCTION: pt_pip2_read_gpio
  8791. *
  8792. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8793. * bits into the passed in variable
  8794. *
  8795. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8796. *
  8797. * RETURN:
  8798. * 0 = success
  8799. * !0 = failure
  8800. *
  8801. * PARAMETERS:
  8802. * *dev - pointer to device structure
  8803. * *status - pointer to where the command response status is stored
  8804. * *gpio - pointer to device attributes structure
  8805. ******************************************************************************/
  8806. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8807. {
  8808. int rc = 0;
  8809. u16 actual_read_len;
  8810. u8 read_buf[12];
  8811. u8 tmp_status = 0;
  8812. u8 index = PIP2_RESP_STATUS_OFFSET;
  8813. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8814. rc = _pt_request_pip2_send_cmd(dev,
  8815. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8816. NULL, 0, read_buf, &actual_read_len);
  8817. if (rc) {
  8818. pt_debug(dev, DL_ERROR,
  8819. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8820. rc = -ECOMM;
  8821. } else {
  8822. tmp_status = read_buf[index];
  8823. }
  8824. if (status)
  8825. *status = tmp_status;
  8826. if (!rc && gpio && (tmp_status == 0)) {
  8827. *gpio = ((read_buf[index + 4] << 24) |
  8828. (read_buf[index + 3] << 16) |
  8829. (read_buf[index + 2] << 8) |
  8830. (read_buf[index + 1]));
  8831. }
  8832. return rc;
  8833. }
  8834. /*******************************************************************************
  8835. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8836. *
  8837. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8838. *
  8839. * RETURN:
  8840. * 0 = success
  8841. * !0 = failure
  8842. *
  8843. * PARAMETERS:
  8844. * *cd - pointer the core data structure
  8845. * *pid - pointer to store panel ID
  8846. ******************************************************************************/
  8847. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8848. {
  8849. u32 gpio_value = 0;
  8850. u8 status = 0;
  8851. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8852. int rc = 0;
  8853. if (!pid)
  8854. return -ENOMEM;
  8855. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8856. if (!rc) {
  8857. if (status == 0) {
  8858. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8859. PT_PANEL_ID_SHIFT;
  8860. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8861. __func__,
  8862. "BL mode PID", panel_id, "gpio", gpio_value);
  8863. *pid = panel_id;
  8864. } else {
  8865. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8866. __func__,
  8867. "BL read gpio failed status", status);
  8868. }
  8869. } else {
  8870. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8871. __func__,
  8872. "BL read gpio failed status", status);
  8873. }
  8874. return rc;
  8875. }
  8876. /*******************************************************************************
  8877. * FUNCTION: pt_enum_with_dut_
  8878. *
  8879. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8880. * The core data (cd) startup_status will store, as a bitmask, each
  8881. * state of the enumeration process. The startup will be attempted
  8882. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8883. *
  8884. * RETURN:
  8885. * 0 = success
  8886. * !0 = failure
  8887. *
  8888. * PARAMETERS:
  8889. * *cd - pointer the core data structure
  8890. * reset - Flag to reset the DUT before attempting to enumerate
  8891. * *status - poionter to store the enum status bitmask flags
  8892. ******************************************************************************/
  8893. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8894. u32 *enum_status)
  8895. {
  8896. int try = 1;
  8897. int rc = 0;
  8898. int wait_time = 0;
  8899. bool detected = false;
  8900. u8 return_data[8];
  8901. u8 mode = PT_MODE_UNKNOWN;
  8902. u8 pid = PANEL_ID_NOT_ENABLED;
  8903. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8904. struct pt_hid_desc hid_desc;
  8905. memset(&hid_desc, 0, sizeof(hid_desc));
  8906. #ifdef TTHE_TUNER_SUPPORT
  8907. tthe_print(cd, NULL, 0, "enter startup");
  8908. #endif
  8909. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8910. __func__, cd->startup_status, reset);
  8911. pt_stop_wd_timer(cd);
  8912. reset:
  8913. if (try > 1)
  8914. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8915. __func__, try);
  8916. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8917. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8918. pt_debug(cd->dev, DL_INFO,
  8919. "%s: PIP1 Enumeration start\n", __func__);
  8920. /* Only reset the DUT after the first try */
  8921. if (reset || (try > 1)) {
  8922. /*
  8923. * Reset hardware only for Legacy parts. Skip for TT/TC
  8924. * parts because if the FW image was loaded directly
  8925. * to SRAM issueing a reset ill wipe out what was just
  8926. * loaded.
  8927. */
  8928. rc = pt_dut_reset_and_wait(cd);
  8929. if (rc < 0) {
  8930. pt_debug(cd->dev, DL_ERROR,
  8931. "%s: Error on h/w reset r=%d\n",
  8932. __func__, rc);
  8933. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8934. goto reset;
  8935. goto exit;
  8936. }
  8937. /* sleep to allow FW to be launched if available */
  8938. msleep(120);
  8939. }
  8940. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8941. if (rc < 0) {
  8942. pt_debug(cd->dev, DL_ERROR,
  8943. "%s: Error getting HID Descriptor r=%d\n",
  8944. __func__, rc);
  8945. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8946. goto reset;
  8947. rc = -EIO;
  8948. goto exit;
  8949. }
  8950. detected = true;
  8951. cd->mode = pt_get_mode(cd, &hid_desc);
  8952. /*
  8953. * Most systems do not use an XY pin as the panel_id and so
  8954. * the BL is used to retrieve the panel_id, however for
  8955. * systems that do use an XY pin, the panel_id MUST be
  8956. * retrieved from the system information when running FW
  8957. * (done below) and so this section of code is skipped.
  8958. * Entering the BL here is only needed on XY_PIN systems.
  8959. */
  8960. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8961. if (cd->mode == PT_MODE_OPERATIONAL) {
  8962. rc = pt_pip_start_bootloader_(cd);
  8963. if (rc < 0) {
  8964. pt_debug(cd->dev, DL_ERROR,
  8965. "%s: Error on start bootloader r=%d\n",
  8966. __func__, rc);
  8967. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8968. goto reset;
  8969. goto exit;
  8970. }
  8971. cd->mode = PT_MODE_BOOTLOADER;
  8972. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8973. __func__);
  8974. }
  8975. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8976. if (!rc) {
  8977. cd->bl_info.ready = true;
  8978. cd->bl_info.chip_id =
  8979. get_unaligned_le16(&return_data[2]);
  8980. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8981. __func__, cd->bl_info.chip_id);
  8982. } else {
  8983. pt_debug(cd->dev, DL_ERROR,
  8984. "%s: failed to get chip ID, r=%d\n",
  8985. __func__, rc);
  8986. }
  8987. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8988. mutex_lock(&cd->system_lock);
  8989. if (!rc) {
  8990. cd->pid_for_loader = pid;
  8991. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8992. __func__, cd->pid_for_loader);
  8993. } else {
  8994. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8995. pt_debug(cd->dev, DL_WARN,
  8996. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8997. __func__, cd->pid_for_loader);
  8998. }
  8999. mutex_unlock(&cd->system_lock);
  9000. }
  9001. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9002. if (cd->mode == PT_MODE_BOOTLOADER) {
  9003. rc = pt_hid_output_bl_launch_app_(cd);
  9004. if (rc < 0) {
  9005. pt_debug(cd->dev, DL_ERROR,
  9006. "%s: Error on launch app r=%d\n",
  9007. __func__, rc);
  9008. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9009. goto reset;
  9010. rc = -ENODEV;
  9011. goto exit;
  9012. }
  9013. /* sleep to allow FW to be launched if available */
  9014. msleep(120);
  9015. /* Ensure the DUT is now in Application mode */
  9016. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9017. if (rc < 0) {
  9018. pt_debug(cd->dev, DL_ERROR,
  9019. "%s: Error getting HID Desc r=%d\n",
  9020. __func__, rc);
  9021. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9022. goto reset;
  9023. rc = -EIO;
  9024. goto exit;
  9025. }
  9026. cd->mode = pt_get_mode(cd, &hid_desc);
  9027. if (cd->mode == PT_MODE_BOOTLOADER) {
  9028. pt_debug(cd->dev, DL_WARN,
  9029. "%s: Error confiming exit BL\n",
  9030. __func__);
  9031. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9032. goto reset;
  9033. rc = -ENODEV;
  9034. goto exit;
  9035. }
  9036. }
  9037. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9038. cd->mode = PT_MODE_OPERATIONAL;
  9039. *enum_status |= STARTUP_STATUS_GET_DESC;
  9040. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9041. } else {
  9042. /* Generation is PIP2 Capable */
  9043. pt_debug(cd->dev, DL_INFO,
  9044. "%s: PIP2 Enumeration start\n", __func__);
  9045. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9046. if (rc) {
  9047. pt_debug(cd->dev, DL_ERROR,
  9048. "%s: Get mode failed, mode unknown\n",
  9049. __func__);
  9050. mode = PT_MODE_UNKNOWN;
  9051. } else
  9052. detected = true;
  9053. cd->mode = mode;
  9054. switch (cd->mode) {
  9055. case PT_MODE_OPERATIONAL:
  9056. pt_debug(cd->dev, DL_INFO,
  9057. "%s: Operational mode\n", __func__);
  9058. if (cd->app_pip_ver_ready == false) {
  9059. rc = pt_pip2_get_version_(cd);
  9060. if (!rc)
  9061. cd->app_pip_ver_ready = true;
  9062. else {
  9063. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9064. goto reset;
  9065. goto exit;
  9066. }
  9067. }
  9068. break;
  9069. case PT_MODE_BOOTLOADER:
  9070. pt_debug(cd->dev, DL_INFO,
  9071. "%s: Bootloader mode\n", __func__);
  9072. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9073. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9074. mutex_lock(&cd->system_lock);
  9075. if (!rc) {
  9076. cd->pid_for_loader = pid;
  9077. pt_debug(cd->dev, DL_INFO,
  9078. "%s: Panel ID: 0x%02X\n",
  9079. __func__, cd->pid_for_loader);
  9080. } else {
  9081. cd->pid_for_loader =
  9082. PANEL_ID_NOT_ENABLED;
  9083. pt_debug(cd->dev, DL_WARN,
  9084. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9085. __func__, cd->pid_for_loader);
  9086. }
  9087. mutex_unlock(&cd->system_lock);
  9088. }
  9089. if (cd->bl_pip_ver_ready == false) {
  9090. rc = pt_pip2_get_version_(cd);
  9091. if (!rc)
  9092. cd->bl_pip_ver_ready = true;
  9093. else {
  9094. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9095. goto reset;
  9096. goto exit;
  9097. }
  9098. }
  9099. /*
  9100. * Launch app command will fail in flashless mode.
  9101. * Skip launch app command here to save time for
  9102. * enumeration flow.
  9103. */
  9104. if (cd->flashless_dut)
  9105. goto exit;
  9106. /*
  9107. * pt_pip2_launch_app() is needed here instead of
  9108. * pt_pip2_exit_bl() because exit_bl will cause several
  9109. * hw_resets to occur and the auto BL on a flashless
  9110. * DUT will fail.
  9111. */
  9112. rc = pt_pip2_launch_app(cd->dev,
  9113. PT_CORE_CMD_UNPROTECTED);
  9114. if (rc) {
  9115. pt_debug(cd->dev, DL_ERROR,
  9116. "%s: Error on launch app r=%d\n",
  9117. __func__, rc);
  9118. msleep(50);
  9119. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9120. goto reset;
  9121. goto exit;
  9122. }
  9123. /*
  9124. * IRQ thread can be delayed if the serial log print is
  9125. * enabled. It causes next command to get wrong response
  9126. * Here the delay is to ensure pt_parse_input() to be
  9127. * finished.
  9128. */
  9129. msleep(60);
  9130. /* Check and update the mode */
  9131. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9132. if (rc) {
  9133. pt_debug(cd->dev, DL_ERROR,
  9134. "%s: Get mode failed, mode unknown\n",
  9135. __func__);
  9136. mode = PT_MODE_UNKNOWN;
  9137. }
  9138. cd->mode = mode;
  9139. if (cd->mode == PT_MODE_OPERATIONAL) {
  9140. pt_debug(cd->dev, DL_INFO,
  9141. "%s: Launched to Operational mode\n",
  9142. __func__);
  9143. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9144. pt_debug(cd->dev, DL_ERROR,
  9145. "%s: Launch failed, Bootloader mode\n",
  9146. __func__);
  9147. goto exit;
  9148. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9149. pt_debug(cd->dev, DL_ERROR,
  9150. "%s: Launch failed, Unknown mode\n",
  9151. __func__);
  9152. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9153. goto reset;
  9154. goto exit;
  9155. }
  9156. if (cd->app_pip_ver_ready == false) {
  9157. rc = pt_pip2_get_version_(cd);
  9158. if (!rc)
  9159. cd->app_pip_ver_ready = true;
  9160. else {
  9161. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9162. goto reset;
  9163. goto exit;
  9164. }
  9165. }
  9166. break;
  9167. default:
  9168. pt_debug(cd->dev, DL_ERROR,
  9169. "%s: Unknown mode\n", __func__);
  9170. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9171. goto reset;
  9172. goto exit;
  9173. }
  9174. *enum_status |= STARTUP_STATUS_GET_DESC;
  9175. }
  9176. pt_init_pip_report_fields(cd);
  9177. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9178. if (!cd->features.easywake)
  9179. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9180. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9181. rc = pt_hid_output_get_sysinfo_(cd);
  9182. if (rc) {
  9183. pt_debug(cd->dev, DL_ERROR,
  9184. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9185. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9186. goto reset;
  9187. goto exit;
  9188. }
  9189. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9190. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9191. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9192. if (!rc) {
  9193. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9194. pt_debug(cd->dev, DL_WARN,
  9195. "%s: Exit FW BOOT Mode after %dms\n",
  9196. __func__, wait_time);
  9197. } else {
  9198. pt_debug(cd->dev, DL_WARN,
  9199. "%s: FW stuck in BOOT Mode after %dms\n",
  9200. __func__, wait_time);
  9201. goto exit;
  9202. }
  9203. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9204. __func__,
  9205. cd->sysinfo.ttdata.pip_ver_major,
  9206. cd->sysinfo.ttdata.pip_ver_minor);
  9207. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9208. if (rc) {
  9209. pt_debug(cd->dev, DL_ERROR,
  9210. "%s: DUT Config block CRC failure rc=%d\n",
  9211. __func__, rc);
  9212. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9213. goto reset;
  9214. } else {
  9215. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9216. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9217. pt_debug(cd->dev, DL_ERROR,
  9218. "%s: scan state: %d, retry: %d\n",
  9219. __func__, sys_mode, try);
  9220. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9221. goto reset;
  9222. } else
  9223. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9224. }
  9225. rc = pt_restore_parameters_(cd);
  9226. if (rc) {
  9227. pt_debug(cd->dev, DL_ERROR,
  9228. "%s: Failed to restore parameters rc=%d\n",
  9229. __func__, rc);
  9230. } else
  9231. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9232. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9233. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9234. cd->startup_retry_count = 0;
  9235. exit:
  9236. /* Generate the HW Version of the connected DUT and store in cd */
  9237. pt_generate_hw_version(cd, cd->hw_version);
  9238. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9239. cd->hw_version);
  9240. pt_start_wd_timer(cd);
  9241. if (!detected)
  9242. rc = -ENODEV;
  9243. #ifdef TTHE_TUNER_SUPPORT
  9244. tthe_print(cd, NULL, 0, "exit startup");
  9245. #endif
  9246. pt_debug(cd->dev, DL_WARN,
  9247. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9248. __func__, rc, try);
  9249. return rc;
  9250. }
  9251. /*******************************************************************************
  9252. * FUNCTION: pt_enum_with_dut
  9253. *
  9254. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9255. * requesting exclusive access around the call.
  9256. *
  9257. * RETURN:
  9258. * 0 = success
  9259. * !0 = failure
  9260. *
  9261. * PARAMETERS:
  9262. * *cd - pointer the core data structure
  9263. * reset - Flag to reset the DUT before attempting to enumerate
  9264. * *status - pointer to store the enum status bitmask flags
  9265. ******************************************************************************/
  9266. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9267. {
  9268. int rc = 0;
  9269. mutex_lock(&cd->system_lock);
  9270. cd->startup_state = STARTUP_RUNNING;
  9271. mutex_unlock(&cd->system_lock);
  9272. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9273. if (rc) {
  9274. pt_debug(cd->dev, DL_ERROR,
  9275. "%s: fail get exclusive ex=%p own=%p\n",
  9276. __func__, cd->exclusive_dev, cd->dev);
  9277. goto exit;
  9278. }
  9279. rc = pt_enum_with_dut_(cd, reset, status);
  9280. if (release_exclusive(cd, cd->dev) < 0)
  9281. /* Don't return fail code, mode is already changed. */
  9282. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9283. __func__);
  9284. else
  9285. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9286. __func__);
  9287. exit:
  9288. mutex_lock(&cd->system_lock);
  9289. /* Clear startup state for any tasks waiting for startup completion */
  9290. cd->startup_state = STARTUP_NONE;
  9291. mutex_unlock(&cd->system_lock);
  9292. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9293. *status |= STARTUP_STATUS_COMPLETE;
  9294. /* Wake the waiters for end of startup */
  9295. wake_up(&cd->wait_q);
  9296. return rc;
  9297. }
  9298. static int add_sysfs_interfaces(struct device *dev);
  9299. static void remove_sysfs_interfaces(struct device *dev);
  9300. static void remove_sysfs_and_modules(struct device *dev);
  9301. static void pt_release_modules(struct pt_core_data *cd);
  9302. static void pt_probe_modules(struct pt_core_data *cd);
  9303. /*******************************************************************************
  9304. * FUNCTION: _pt_ttdl_restart
  9305. *
  9306. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9307. *
  9308. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9309. * existing sysfs nodes will produce an error.
  9310. *
  9311. * RETURN:
  9312. * 0 = success
  9313. * !0 = failure
  9314. *
  9315. * PARAMETERS:
  9316. * *dev - pointer to core device
  9317. ******************************************************************************/
  9318. static int _pt_ttdl_restart(struct device *dev)
  9319. {
  9320. int rc = 0;
  9321. struct pt_core_data *cd = dev_get_drvdata(dev);
  9322. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9323. struct i2c_client *client =
  9324. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9325. #endif
  9326. /*
  9327. * Make sure the device is awake, pt_mt_release function will
  9328. * cause pm sleep function and lead to deadlock.
  9329. */
  9330. pm_runtime_get_sync(dev);
  9331. /* Use ttdl_restart_lock to avoid reentry */
  9332. mutex_lock(&cd->ttdl_restart_lock);
  9333. remove_sysfs_and_modules(cd->dev);
  9334. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9335. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9336. pt_debug(dev, DL_ERROR,
  9337. "%s I2C functionality not Supported\n", __func__);
  9338. rc = -EIO;
  9339. goto ttdl_no_error;
  9340. }
  9341. #endif
  9342. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9343. rc = _pt_detect_dut_generation(cd->dev,
  9344. &cd->startup_status, &cd->active_dut_generation,
  9345. &cd->mode);
  9346. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9347. pt_debug(dev, DL_ERROR,
  9348. "%s: Error, Unknown DUT Generation rc=%d\n",
  9349. __func__, rc);
  9350. }
  9351. }
  9352. rc = add_sysfs_interfaces(cd->dev);
  9353. if (rc < 0)
  9354. pt_debug(cd->dev, DL_ERROR,
  9355. "%s: Error, failed sysfs nodes rc=%d\n",
  9356. __func__, rc);
  9357. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9358. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9359. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9360. if (rc < 0)
  9361. pt_debug(dev, DL_ERROR,
  9362. "%s: Error, Failed to Enumerate\n", __func__);
  9363. }
  9364. rc = pt_mt_probe(dev);
  9365. if (rc < 0) {
  9366. pt_debug(dev, DL_ERROR,
  9367. "%s: Error, fail mt probe\n", __func__);
  9368. }
  9369. rc = pt_btn_probe(dev);
  9370. if (rc < 0) {
  9371. pt_debug(dev, DL_ERROR,
  9372. "%s: Error, fail btn probe\n", __func__);
  9373. }
  9374. pt_probe_modules(cd);
  9375. pt_debug(cd->dev, DL_WARN,
  9376. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9377. rc = 0;
  9378. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9379. ttdl_no_error:
  9380. #endif
  9381. mutex_unlock(&cd->ttdl_restart_lock);
  9382. mutex_lock(&cd->system_lock);
  9383. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9384. cd->startup_state = STARTUP_NONE;
  9385. mutex_unlock(&cd->system_lock);
  9386. pm_runtime_put(dev);
  9387. return rc;
  9388. }
  9389. /*******************************************************************************
  9390. * FUNCTION: pt_restart_work_function
  9391. *
  9392. * SUMMARY: This is the wrapper function placed in a work queue to call
  9393. * _pt_ttdl_restart()
  9394. *
  9395. * RETURN: none
  9396. *
  9397. * PARAMETERS:
  9398. * *work - pointer to the work_struct
  9399. ******************************************************************************/
  9400. static void pt_restart_work_function(struct work_struct *work)
  9401. {
  9402. struct pt_core_data *cd = container_of(work,
  9403. struct pt_core_data, ttdl_restart_work);
  9404. int rc = 0;
  9405. rc = _pt_ttdl_restart(cd->dev);
  9406. if (rc < 0)
  9407. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9408. __func__, rc);
  9409. }
  9410. /*******************************************************************************
  9411. * FUNCTION: pt_enum_work_function
  9412. *
  9413. * SUMMARY: This is the wrapper function placed in a work queue to call
  9414. * pt_enum_with_dut()
  9415. *
  9416. * RETURN: none
  9417. *
  9418. * PARAMETERS:
  9419. * *work - pointer to the work_struct
  9420. ******************************************************************************/
  9421. static void pt_enum_work_function(struct work_struct *work)
  9422. {
  9423. struct pt_core_data *cd = container_of(work,
  9424. struct pt_core_data, enum_work);
  9425. int rc;
  9426. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9427. if (rc < 0)
  9428. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9429. __func__, rc);
  9430. }
  9431. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9432. {
  9433. int rc;
  9434. if (!get) {
  9435. rc = 0;
  9436. goto regulator_put;
  9437. }
  9438. cd->vdd = regulator_get(cd->dev, "vdd");
  9439. if (IS_ERR(cd->vdd)) {
  9440. rc = PTR_ERR(cd->vdd);
  9441. dev_err(cd->dev,
  9442. "Regulator get failed vdd rc=%d\n", rc);
  9443. goto regulator_put;
  9444. }
  9445. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9446. if (IS_ERR(cd->vcc_i2c)) {
  9447. rc = PTR_ERR(cd->vcc_i2c);
  9448. dev_err(cd->dev,
  9449. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9450. goto regulator_put;
  9451. }
  9452. return 0;
  9453. regulator_put:
  9454. if (cd->vdd) {
  9455. regulator_put(cd->vdd);
  9456. cd->vdd = NULL;
  9457. }
  9458. if (cd->vcc_i2c) {
  9459. regulator_put(cd->vcc_i2c);
  9460. cd->vcc_i2c = NULL;
  9461. }
  9462. return rc;
  9463. }
  9464. #ifdef ENABLE_I2C_REG_ONLY
  9465. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9466. {
  9467. int rc = 0;
  9468. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9469. if (!en) {
  9470. rc = 0;
  9471. goto disable_vcc_i2c_reg_only;
  9472. }
  9473. if (cd->vcc_i2c) {
  9474. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9475. if (rc < 0)
  9476. pt_debug(cd->dev, DL_INFO,
  9477. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9478. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9479. __func__, rc);
  9480. }
  9481. return 0;
  9482. disable_vcc_i2c_reg_only:
  9483. if (cd->vcc_i2c) {
  9484. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9485. if (rc < 0)
  9486. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9487. __func__, rc);
  9488. }
  9489. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9490. return rc;
  9491. }
  9492. #endif
  9493. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9494. {
  9495. int rc;
  9496. if (!en) {
  9497. rc = 0;
  9498. goto disable_vcc_i2c_reg;
  9499. }
  9500. if (cd->vdd) {
  9501. if (regulator_count_voltages(cd->vdd) > 0) {
  9502. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9503. FT_VTG_MAX_UV);
  9504. if (rc) {
  9505. dev_err(cd->dev,
  9506. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9507. goto exit;
  9508. }
  9509. }
  9510. rc = regulator_enable(cd->vdd);
  9511. if (rc) {
  9512. dev_err(cd->dev,
  9513. "Regulator vdd enable failed rc=%d\n", rc);
  9514. goto exit;
  9515. }
  9516. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9517. }
  9518. if (cd->vcc_i2c) {
  9519. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9520. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9521. FT_I2C_VTG_MAX_UV);
  9522. if (rc) {
  9523. dev_err(cd->dev,
  9524. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9525. goto disable_vdd_reg;
  9526. }
  9527. }
  9528. rc = regulator_enable(cd->vcc_i2c);
  9529. if (rc) {
  9530. dev_err(cd->dev,
  9531. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9532. goto disable_vdd_reg;
  9533. }
  9534. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9535. }
  9536. return 0;
  9537. disable_vcc_i2c_reg:
  9538. if (cd->vcc_i2c) {
  9539. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9540. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9541. FT_I2C_VTG_MAX_UV);
  9542. regulator_disable(cd->vcc_i2c);
  9543. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9544. }
  9545. disable_vdd_reg:
  9546. if (cd->vdd) {
  9547. if (regulator_count_voltages(cd->vdd) > 0)
  9548. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9549. FT_VTG_MAX_UV);
  9550. regulator_disable(cd->vdd);
  9551. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9552. }
  9553. exit:
  9554. return rc;
  9555. }
  9556. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9557. #define KERNEL_VER_GT_3_19
  9558. #endif
  9559. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9560. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9561. #if defined(CONFIG_PM_SLEEP)
  9562. /*******************************************************************************
  9563. * FUNCTION: pt_core_rt_suspend
  9564. *
  9565. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9566. *
  9567. * RETURN:
  9568. * 0 = success
  9569. * !0 = failure
  9570. *
  9571. * PARAMETERS:
  9572. * *dev - pointer to core device
  9573. ******************************************************************************/
  9574. static int pt_core_rt_suspend(struct device *dev)
  9575. {
  9576. struct pt_core_data *cd = dev_get_drvdata(dev);
  9577. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9578. __func__, cd->core_probe_complete);
  9579. return 0;
  9580. }
  9581. /*******************************************************************************
  9582. * FUNCTION: pt_core_rt_resume
  9583. *
  9584. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9585. *
  9586. * RETURN:
  9587. * 0 = success
  9588. * !0 = failure
  9589. *
  9590. * PARAMETERS:
  9591. * *dev - pointer to core device
  9592. ******************************************************************************/
  9593. static int pt_core_rt_resume(struct device *dev)
  9594. {
  9595. struct pt_core_data *cd = dev_get_drvdata(dev);
  9596. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9597. __func__, cd->core_probe_complete);
  9598. return 0;
  9599. }
  9600. #endif /* CONFIG_PM_SLEEP */
  9601. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9602. #if defined(CONFIG_PM_SLEEP)
  9603. /*******************************************************************************
  9604. * FUNCTION: pt_core_suspend_
  9605. *
  9606. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9607. * pt_core_sleep. This function may disable IRQ during sleep state.
  9608. *
  9609. * RETURN:
  9610. * 0 = success
  9611. * !0 = failure
  9612. *
  9613. * PARAMETERS:
  9614. * *dev - pointer to core device
  9615. ******************************************************************************/
  9616. static int pt_core_suspend_(struct device *dev)
  9617. {
  9618. int rc;
  9619. struct pt_core_data *cd = dev_get_drvdata(dev);
  9620. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9621. rc = pt_core_sleep(cd);
  9622. if (rc) {
  9623. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9624. __func__, rc);
  9625. return -EAGAIN;
  9626. }
  9627. rc = pt_enable_regulator(cd, false);
  9628. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9629. return 0;
  9630. /* Required to prevent interrupts before bus awake */
  9631. disable_irq(cd->irq);
  9632. cd->irq_disabled = 1;
  9633. if (device_may_wakeup(dev)) {
  9634. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9635. __func__);
  9636. if (!enable_irq_wake(cd->irq))
  9637. cd->irq_wake = 1;
  9638. } else {
  9639. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9640. __func__);
  9641. }
  9642. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9643. return rc;
  9644. }
  9645. /*******************************************************************************
  9646. * FUNCTION: pt_core_suspend
  9647. *
  9648. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9649. * woke up or put to sleep based on Kernel power state even when the display
  9650. * is off based on the check of TTDL core platform flag.
  9651. *
  9652. * RETURN:
  9653. * 0 = success
  9654. * !0 = failure
  9655. *
  9656. * PARAMETERS:
  9657. * *dev - pointer to core device
  9658. ******************************************************************************/
  9659. static int pt_core_suspend(struct device *dev)
  9660. {
  9661. struct pt_core_data *cd = dev_get_drvdata(dev);
  9662. int rc = 0, status = 0;
  9663. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9664. return 0;
  9665. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9666. cancel_work_sync(&cd->resume_work);
  9667. cancel_work_sync(&cd->suspend_work);
  9668. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9669. rc = pt_core_suspend_(cd->dev);
  9670. cd->quick_boot = true;
  9671. } else {
  9672. rc = pt_core_easywake_on(cd);
  9673. if (rc < 0)
  9674. pt_debug(cd->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9675. cd->fb_state = FB_OFF;
  9676. status = pt_enable_i2c_regulator(cd, false);
  9677. pt_debug(cd->dev, DL_INFO, "%s Exit - rc = %d\n", __func__, status);
  9678. }
  9679. return rc;
  9680. }
  9681. /*******************************************************************************
  9682. * FUNCTION: pt_core_resume_
  9683. *
  9684. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9685. * pt_core_wake. This function may enable IRQ before wake up.
  9686. *
  9687. * RETURN:
  9688. * 0 = success
  9689. * !0 = failure
  9690. *
  9691. * PARAMETERS:
  9692. * *dev - pointer to core device
  9693. ******************************************************************************/
  9694. static int pt_core_resume_(struct device *dev)
  9695. {
  9696. int rc = 0;
  9697. struct pt_core_data *cd = dev_get_drvdata(dev);
  9698. dev_info(dev, "%s: Entering into resume mode:\n",
  9699. __func__);
  9700. rc = pt_enable_regulator(cd, true);
  9701. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9702. __func__, rc);
  9703. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9704. goto exit;
  9705. /*
  9706. * Bus pm does not call suspend if device runtime suspended
  9707. * This flag is covers that case
  9708. */
  9709. if (cd->irq_disabled) {
  9710. enable_irq(cd->irq);
  9711. cd->irq_disabled = 0;
  9712. }
  9713. if (device_may_wakeup(dev)) {
  9714. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9715. __func__);
  9716. if (cd->irq_wake) {
  9717. disable_irq_wake(cd->irq);
  9718. cd->irq_wake = 0;
  9719. }
  9720. } else {
  9721. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9722. __func__);
  9723. }
  9724. exit:
  9725. rc = pt_core_wake(cd);
  9726. if (rc) {
  9727. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9728. __func__, rc);
  9729. return -EAGAIN;
  9730. }
  9731. return rc;
  9732. }
  9733. /*******************************************************************************
  9734. * FUNCTION: pt_core_restore
  9735. *
  9736. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9737. * pt_core_wake. This function may enable IRQ before wake up.
  9738. *
  9739. * RETURN:
  9740. * 0 = success
  9741. * !0 = failure
  9742. *
  9743. * PARAMETERS:
  9744. * *dev - pointer to core device
  9745. ******************************************************************************/
  9746. static int pt_core_restore(struct device *dev)
  9747. {
  9748. int rc = 0;
  9749. struct pt_core_data *cd = dev_get_drvdata(dev);
  9750. dev_info(dev, "%s: Entering into resume mode:\n",
  9751. __func__);
  9752. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9753. return rc;
  9754. }
  9755. /*******************************************************************************
  9756. * FUNCTION: suspend_offload_work
  9757. *
  9758. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9759. * along with kernel power state even the display is off based on the check of
  9760. * TTDL core platform flag.
  9761. *
  9762. * RETURN:
  9763. * 0 = success
  9764. * !0 = failure
  9765. *
  9766. * PARAMETERS:
  9767. * *dev - pointer to core device
  9768. ******************************************************************************/
  9769. static void pt_suspend_offload_work(struct work_struct *work)
  9770. {
  9771. int rc = 0;
  9772. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9773. suspend_offload_work);
  9774. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9775. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9776. return;
  9777. rc = pt_core_suspend_(cd->dev);
  9778. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9779. }
  9780. /*******************************************************************************
  9781. * FUNCTION: resume_offload_work
  9782. *
  9783. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9784. * along with kernel power state even the display is off based on the check of
  9785. * TTDL core platform flag.
  9786. *
  9787. * RETURN:
  9788. * 0 = success
  9789. * !0 = failure
  9790. *
  9791. * PARAMETERS:
  9792. * *dev - pointer to core device
  9793. ******************************************************************************/
  9794. static void pt_resume_offload_work(struct work_struct *work)
  9795. {
  9796. int rc = 0;
  9797. int retry_count = 10;
  9798. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9799. resume_offload_work);
  9800. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9801. do {
  9802. retry_count--;
  9803. rc = pt_core_resume_(pt_data->dev);
  9804. if (rc < 0)
  9805. pt_debug(pt_data->dev, DL_ERROR,
  9806. "%s: Error on wake\n", __func__);
  9807. } while (retry_count && rc < 0);
  9808. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9809. rc = pt_core_easywake_on(pt_data);
  9810. if (rc < 0) {
  9811. pt_debug(pt_data->dev, DL_ERROR,
  9812. "%s: Error on enable touch to wake key\n",
  9813. __func__);
  9814. return;
  9815. }
  9816. pt_data->fb_state = FB_OFF;
  9817. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9818. #endif
  9819. pt_data->quick_boot = false;
  9820. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9821. }
  9822. /*******************************************************************************
  9823. * FUNCTION: pt_core_resume
  9824. *
  9825. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9826. * along with kernel power state even the display is off based on the check of
  9827. * TTDL core platform flag.
  9828. *
  9829. * RETURN:
  9830. * 0 = success
  9831. * !0 = failure
  9832. *
  9833. * PARAMETERS:
  9834. * *dev - pointer to core device
  9835. ******************************************************************************/
  9836. static int pt_core_resume(struct device *dev)
  9837. {
  9838. struct pt_core_data *cd = dev_get_drvdata(dev);
  9839. int rc = 0;
  9840. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9841. return 0;
  9842. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9843. rc = pt_core_restore(cd->dev);
  9844. } else {
  9845. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9846. rc = pt_enable_i2c_regulator(cd, true);
  9847. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9848. pt_debug(cd->dev, DL_INFO, "%s End\n", __func__);
  9849. }
  9850. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9851. return rc;
  9852. }
  9853. #endif
  9854. #ifdef NEED_SUSPEND_NOTIFIER
  9855. /*******************************************************************************
  9856. * FUNCTION: pt_pm_notifier
  9857. *
  9858. * SUMMARY: This function is registered to notifier chain and will perform
  9859. * suspend operation if match event PM_SUSPEND_PREPARE.
  9860. *
  9861. * RETURN:
  9862. * 0 = success
  9863. * !0 = failure
  9864. *
  9865. * PARAMETERS:
  9866. * *nb - pointer to notifier_block structure
  9867. * action - notifier event type
  9868. * *data - void pointer
  9869. ******************************************************************************/
  9870. static int pt_pm_notifier(struct notifier_block *nb,
  9871. unsigned long action, void *data)
  9872. {
  9873. struct pt_core_data *cd = container_of(nb,
  9874. struct pt_core_data, pm_notifier);
  9875. if (action == PM_SUSPEND_PREPARE) {
  9876. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9877. __func__);
  9878. /*
  9879. * If PM runtime is not suspended, either call runtime
  9880. * PM suspend callback or wait until it finishes
  9881. */
  9882. if (!pm_runtime_suspended(cd->dev))
  9883. pm_runtime_suspend(cd->dev);
  9884. (void) pt_core_suspend(cd->dev);
  9885. }
  9886. return NOTIFY_DONE;
  9887. }
  9888. #endif
  9889. const struct dev_pm_ops pt_pm_ops = {
  9890. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9891. .freeze = pt_core_suspend,
  9892. .restore = pt_core_restore,
  9893. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9894. NULL)
  9895. };
  9896. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9897. /*******************************************************************************
  9898. * FUNCTION: _pt_request_pip2_enter_bl
  9899. *
  9900. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9901. * XRES pin or a soft reset.
  9902. *
  9903. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9904. * the WD active could cause this function to fail!
  9905. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9906. * will not try to determine the current mode but will proceed with
  9907. * resetting the DUT and entering the BL.
  9908. *
  9909. * NOTE: The definition of result code:
  9910. * PT_ENTER_BL_PASS (0)
  9911. * PT_ENTER_BL_ERROR (1)
  9912. * PT_ENTER_BL_RESET_FAIL (2)
  9913. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9914. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9915. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9916. *
  9917. * RETURNS:
  9918. * 0 = success
  9919. * !0 = failure
  9920. *
  9921. *
  9922. * PARAMETERS:
  9923. * *dev - pointer to device structure
  9924. * *start_mode - pointer to the mode the DUT was in when this function
  9925. * starts
  9926. * *result - pointer to store the result when to enter BL
  9927. ******************************************************************************/
  9928. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9929. {
  9930. int rc = 0;
  9931. int t;
  9932. int tmp_result = PT_ENTER_BL_ERROR;
  9933. int flash_info_retry = 2;
  9934. u8 mode = PT_MODE_UNKNOWN;
  9935. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9936. u8 read_buf[32];
  9937. u16 actual_read_len;
  9938. struct pt_core_data *cd = dev_get_drvdata(dev);
  9939. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9940. u8 time = 0;
  9941. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9942. if (cd->watchdog_enabled) {
  9943. pt_debug(dev, DL_WARN,
  9944. "%s: Watchdog must be stopped before entering BL\n",
  9945. __func__);
  9946. goto exit;
  9947. }
  9948. cancel_work_sync(&cd->enum_work);
  9949. cancel_work_sync(&cd->watchdog_work);
  9950. /* if undefined assume operational/test to bypass all checks */
  9951. if (*start_mode == PT_MODE_IGNORE) {
  9952. mode = PT_MODE_OPERATIONAL;
  9953. sys_mode = FW_SYS_MODE_TEST;
  9954. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9955. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9956. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9957. if (rc) {
  9958. pt_debug(dev, DL_ERROR,
  9959. "%s: Get mode failed, mode unknown\n",
  9960. __func__);
  9961. }
  9962. *start_mode = mode;
  9963. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9964. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9965. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9966. sys_mode = FW_SYS_MODE_SCANNING;
  9967. }
  9968. _retry:
  9969. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9970. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9971. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9972. switch (mode) {
  9973. case PT_MODE_UNKNOWN:
  9974. /*
  9975. * When the mode could not be determined the DUT could be
  9976. * in App mode running corrupted FW or FW that is not
  9977. * responding to the mode request, assume no communication
  9978. * and do a hard reset
  9979. */
  9980. mutex_lock(&cd->system_lock);
  9981. cd->startup_status = STARTUP_STATUS_START;
  9982. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9983. mutex_unlock(&cd->system_lock);
  9984. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9985. if (rc) {
  9986. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9987. goto exit;
  9988. }
  9989. break;
  9990. case PT_MODE_OPERATIONAL:
  9991. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9992. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9993. __func__);
  9994. rc = pt_pip_suspend_scanning_(cd);
  9995. if (rc) {
  9996. /*
  9997. * Print to log but don't exit, the FW could be
  9998. * running but be hung or fail to respond to
  9999. * this request
  10000. */
  10001. pt_debug(dev, DL_ERROR,
  10002. "%s Suspend Scan Failed\n", __func__);
  10003. }
  10004. /* sleep to allow the suspend scan to be processed */
  10005. usleep_range(1000, 2000);
  10006. }
  10007. mutex_lock(&cd->system_lock);
  10008. cd->startup_status = STARTUP_STATUS_START;
  10009. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10010. mutex_unlock(&cd->system_lock);
  10011. /* Reset device to enter the BL */
  10012. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10013. if (rc) {
  10014. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10015. goto exit;
  10016. }
  10017. break;
  10018. case PT_MODE_BOOTLOADER:
  10019. /* Do nothing as we are already in the BL */
  10020. tmp_result = PT_ENTER_BL_PASS;
  10021. goto exit;
  10022. default:
  10023. /* Should NEVER get here */
  10024. tmp_result = PT_ENTER_BL_ERROR;
  10025. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10026. goto exit;
  10027. }
  10028. if (!cd->flashless_dut &&
  10029. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10030. /*
  10031. * Sending the special "Host Mode" command will instruct the
  10032. * BL to not execute the FW it has loaded into RAM.
  10033. * The command must be sent within a 40ms window from releasing
  10034. * the XRES pin. If the messages is sent too early it will NAK,
  10035. * so keep sending it every 2ms until it is accepted by the BL.
  10036. * A no-flash DUT does not require this command as there is no
  10037. * FW for the BL to load and execute.
  10038. */
  10039. usleep_range(4000, 6000);
  10040. pt_debug(cd->dev, DL_INFO,
  10041. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10042. __func__, (int)sizeof(host_mode_cmd));
  10043. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10044. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10045. rc = 1;
  10046. while (rc && time < 34) {
  10047. rc = pt_adap_write_read_specific(cd, 4,
  10048. host_mode_cmd, NULL);
  10049. usleep_range(1800, 2000);
  10050. time += 2;
  10051. }
  10052. /* Sleep to allow the BL to come up */
  10053. usleep_range(1000, 2000);
  10054. }
  10055. /*
  10056. * To avoid the case that next PIP command can be confused by BL/FW
  10057. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10058. * followed by "wakeup event" function can lower the failure rate.
  10059. */
  10060. t = wait_event_timeout(cd->wait_q,
  10061. ((cd->startup_status != STARTUP_STATUS_START)
  10062. && (cd->hid_reset_cmd_state == 0)),
  10063. msecs_to_jiffies(300));
  10064. if (IS_TMO(t)) {
  10065. pt_debug(cd->dev, DL_ERROR,
  10066. "%s: TMO waiting for BL sentinel\n", __func__);
  10067. }
  10068. /* Check if device is now in BL mode */
  10069. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10070. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10071. cd->startup_status);
  10072. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10073. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10074. mutex_lock(&cd->system_lock);
  10075. cd->pip2_prot_active = true;
  10076. cd->mode = PT_MODE_BOOTLOADER;
  10077. mutex_unlock(&cd->system_lock);
  10078. tmp_result = PT_ENTER_BL_PASS;
  10079. } else {
  10080. /*
  10081. * If the device doesn't enter BL mode as expected and rc is
  10082. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10083. * the function should return an error code to indicate this
  10084. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10085. */
  10086. if (!rc)
  10087. rc = -EINVAL;
  10088. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10089. mutex_lock(&cd->system_lock);
  10090. cd->mode = mode;
  10091. mutex_unlock(&cd->system_lock);
  10092. pt_debug(dev, DL_ERROR,
  10093. "%s ERROR: Not in BL as expected", __func__);
  10094. }
  10095. exit:
  10096. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10097. /* Check to get (buffered) flash information */
  10098. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10099. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10100. read_buf, &actual_read_len);
  10101. if (!rc) {
  10102. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10103. pt_debug(
  10104. dev, DL_WARN,
  10105. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10106. __func__,
  10107. read_buf[PIP2_RESP_BODY_OFFSET]);
  10108. /*
  10109. * If the BL was unable to cache the correct
  10110. * values when entering the first time due to
  10111. * the Flash part not having been powered up
  10112. * long enough, re-enter the BL to trigger the
  10113. * BL to re-attempt to cache the values.
  10114. */
  10115. if (flash_info_retry-- > 0) {
  10116. mode = PT_MODE_UNKNOWN;
  10117. pt_debug(dev, DL_WARN,
  10118. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10119. __func__, flash_info_retry);
  10120. goto _retry;
  10121. } else {
  10122. pt_debug(dev, DL_WARN,
  10123. "%s Manufacturer ID Unknown\n",
  10124. __func__);
  10125. tmp_result = PT_ENTER_BL_PASS;
  10126. }
  10127. }
  10128. } else {
  10129. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10130. pt_debug(
  10131. dev, DL_ERROR,
  10132. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10133. __func__);
  10134. }
  10135. }
  10136. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10137. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10138. "SUPPRESS");
  10139. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10140. if (result)
  10141. *result = tmp_result;
  10142. return rc;
  10143. }
  10144. /*******************************************************************************
  10145. * FUNCTION: _pt_pip2_file_open
  10146. *
  10147. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10148. *
  10149. * NOTE: The DUT must be in BL mode for this command to work
  10150. *
  10151. * RETURNS:
  10152. * <0 = Error
  10153. * >0 = file handle opened
  10154. *
  10155. * PARAMETERS:
  10156. * *dev - pointer to device structure
  10157. * file_no - PIP2 file number to open
  10158. ******************************************************************************/
  10159. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10160. {
  10161. int ret = 0;
  10162. u16 status;
  10163. u16 actual_read_len;
  10164. u8 file_handle;
  10165. u8 data[2];
  10166. u8 read_buf[10];
  10167. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10168. data[0] = file_no;
  10169. ret = _pt_request_pip2_send_cmd(dev,
  10170. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10171. data, 1, read_buf, &actual_read_len);
  10172. if (ret) {
  10173. pt_debug(dev, DL_ERROR,
  10174. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10175. __func__, file_no);
  10176. return -PIP2_RSP_ERR_NOT_OPEN;
  10177. }
  10178. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10179. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10180. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10181. (file_handle != file_no)) {
  10182. pt_debug(dev, DL_ERROR,
  10183. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10184. __func__, status, file_handle);
  10185. return -status;
  10186. }
  10187. return file_handle;
  10188. }
  10189. /*******************************************************************************
  10190. * FUNCTION: _pt_pip2_file_close
  10191. *
  10192. * SUMMARY: Using the BL PIP2 commands close a file
  10193. *
  10194. * NOTE: The DUT must be in BL mode for this command to work
  10195. *
  10196. * RETURNS:
  10197. * <0 = Error
  10198. * >0 = file handle closed
  10199. *
  10200. * PARAMETERS:
  10201. * *dev - pointer to device structure
  10202. * file_handle - handle to the file to be closed
  10203. ******************************************************************************/
  10204. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10205. {
  10206. int ret = 0;
  10207. u16 status;
  10208. u16 actual_read_len;
  10209. u8 data[2];
  10210. u8 read_buf[32];
  10211. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10212. data[0] = file_handle;
  10213. ret = _pt_request_pip2_send_cmd(dev,
  10214. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10215. data, 1, read_buf, &actual_read_len);
  10216. if (ret) {
  10217. pt_debug(dev, DL_ERROR,
  10218. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10219. __func__, file_handle);
  10220. return -ETIME;
  10221. }
  10222. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10223. if (status != 0x00) {
  10224. pt_debug(dev, DL_ERROR,
  10225. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10226. __func__, status, file_handle);
  10227. return -status;
  10228. }
  10229. return file_handle;
  10230. }
  10231. /*******************************************************************************
  10232. * FUNCTION: _pt_pip2_file_erase
  10233. *
  10234. * SUMMARY: Using the BL PIP2 commands erase a file
  10235. *
  10236. * NOTE: The DUT must be in BL mode for this command to work
  10237. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10238. * one retry is attempted for each sector in a file.
  10239. *
  10240. * RETURNS:
  10241. * <0 = Error
  10242. * >0 = file handle closed
  10243. *
  10244. * PARAMETERS:
  10245. * *dev - pointer to device structure
  10246. * file_handle - handle to the file to be erased
  10247. * *status - PIP2 erase status code
  10248. ******************************************************************************/
  10249. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10250. {
  10251. int ret = 0;
  10252. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10253. int retry = 1;
  10254. u16 actual_read_len;
  10255. u8 data[2];
  10256. u8 read_buf[10];
  10257. struct pt_core_data *cd = dev_get_drvdata(dev);
  10258. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10259. data[0] = file_handle;
  10260. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10261. *status = PIP2_RSP_ERR_TIMEOUT;
  10262. /* Increase waiting time for large file erase */
  10263. mutex_lock(&cd->system_lock);
  10264. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10265. mutex_unlock(&cd->system_lock);
  10266. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10267. ret = _pt_request_pip2_send_cmd(dev,
  10268. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10269. data, 2, read_buf, &actual_read_len);
  10270. if (ret)
  10271. break;
  10272. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10273. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10274. #ifdef TTDL_DIAGNOSTICS
  10275. cd->file_erase_timeout_count++;
  10276. #endif
  10277. pt_debug(dev, DL_WARN,
  10278. "%s: ERASE timeout %d for file = %d\n",
  10279. __func__, retry, file_handle);
  10280. }
  10281. retry++;
  10282. }
  10283. mutex_lock(&cd->system_lock);
  10284. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10285. mutex_unlock(&cd->system_lock);
  10286. if (ret) {
  10287. pt_debug(dev, DL_ERROR,
  10288. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10289. __func__, ret, file_handle);
  10290. return -EIO;
  10291. }
  10292. if (*status != 0x00) {
  10293. pt_debug(dev, DL_ERROR,
  10294. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10295. __func__, *status, file_handle);
  10296. return -EIO;
  10297. }
  10298. return file_handle;
  10299. }
  10300. /*******************************************************************************
  10301. * FUNCTION: _pt_pip2_file_read
  10302. *
  10303. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10304. *
  10305. * NOTE: The DUT must be in BL mode for this command to work
  10306. *
  10307. * RETURNS:
  10308. * <0 = Error
  10309. * >0 = number of bytes read
  10310. *
  10311. * PARAMETERS:
  10312. * *dev - pointer to device structure
  10313. * file_handle - File handle to read from
  10314. * num_bytes - number of bytes to read
  10315. ******************************************************************************/
  10316. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10317. u8 *read_buf)
  10318. {
  10319. int ret = 0;
  10320. u16 status;
  10321. u16 actual_read_len;
  10322. u8 data[3];
  10323. data[0] = file_handle;
  10324. data[1] = (num_bytes & 0x00FF);
  10325. data[2] = (num_bytes & 0xFF00) >> 8;
  10326. ret = _pt_request_pip2_send_cmd(dev,
  10327. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10328. data, 3, read_buf, &actual_read_len);
  10329. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10330. if (ret || ((status != 0x00) && (status != 0x03))) {
  10331. pt_debug(dev, DL_ERROR,
  10332. "%s File open failure with error code = %d\n",
  10333. __func__, status);
  10334. return -EPERM;
  10335. }
  10336. ret = num_bytes;
  10337. return ret;
  10338. }
  10339. /*******************************************************************************
  10340. * FUNCTION: _pt_read_us_file
  10341. *
  10342. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10343. * then read the entire file.
  10344. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10345. *
  10346. * RETURN:
  10347. * 0 = success
  10348. * !0 = failure
  10349. *
  10350. * PARAMETERS:
  10351. * *file_path - pointer to the file path
  10352. * *buf - pointer to the buffer to store the file contents
  10353. * *size - pointer to the size of the file
  10354. ******************************************************************************/
  10355. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10356. {
  10357. struct file *filp = NULL;
  10358. struct inode *inode = NULL;
  10359. unsigned int file_len = 0;
  10360. unsigned int read_len = 0;
  10361. mm_segment_t oldfs;
  10362. int rc = 0;
  10363. if (file_path == NULL || buf == NULL) {
  10364. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10365. return -EINVAL;
  10366. }
  10367. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10368. oldfs = get_fs();
  10369. set_fs(KERNEL_DS);
  10370. filp = filp_open(file_path, O_RDONLY, 0400);
  10371. if (IS_ERR(filp)) {
  10372. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10373. file_path);
  10374. rc = -ENOENT;
  10375. goto err;
  10376. }
  10377. if (filp->f_op == NULL) {
  10378. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10379. __func__);
  10380. rc = -EINVAL;
  10381. goto exit;
  10382. }
  10383. inode = filp->f_path.dentry->d_inode;
  10384. if (inode == NULL) {
  10385. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10386. __func__);
  10387. rc = -EINVAL;
  10388. goto exit;
  10389. }
  10390. file_len = i_size_read(inode->i_mapping->host);
  10391. if (file_len == 0) {
  10392. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10393. __func__, file_len);
  10394. rc = -EINVAL;
  10395. goto exit;
  10396. }
  10397. if (*size == 0)
  10398. read_len = file_len;
  10399. else
  10400. read_len = *size;
  10401. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10402. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10403. __func__, read_len);
  10404. rc = -EINVAL;
  10405. goto exit;
  10406. }
  10407. filp->private_data = inode->i_private;
  10408. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10409. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10410. rc = -EINVAL;
  10411. goto exit;
  10412. }
  10413. *size = read_len;
  10414. exit:
  10415. if (filp_close(filp, NULL) != 0)
  10416. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10417. err:
  10418. set_fs(oldfs);
  10419. return rc;
  10420. }
  10421. /*******************************************************************************
  10422. * FUNCTION: _pt_request_pip2_bin_hdr
  10423. *
  10424. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10425. * in the case of a flashless DUT, and parse the contents
  10426. *
  10427. * RETURNS:
  10428. * 0 = Success
  10429. * !0 = Error condition
  10430. *
  10431. * PARAMETERS:
  10432. * *dev - pointer to device structure
  10433. ******************************************************************************/
  10434. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10435. {
  10436. struct pt_core_data *cd = dev_get_drvdata(dev);
  10437. u8 file_handle;
  10438. u8 read_buf[255];
  10439. u8 hdr_len = 0;
  10440. u8 i;
  10441. int bytes_read;
  10442. int read_size;
  10443. int ret = 0;
  10444. int rc = 0;
  10445. bool load_hdr_struct = false;
  10446. if (cd->flashless_dut) {
  10447. read_size = sizeof(read_buf);
  10448. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10449. read_buf, &read_size);
  10450. if (rc) {
  10451. ret = rc;
  10452. pt_debug(dev, DL_ERROR,
  10453. "%s Failed to read fw image from US, rc=%d\n",
  10454. __func__, rc);
  10455. goto exit;
  10456. }
  10457. load_hdr_struct = true;
  10458. hdr_len = read_buf[0];
  10459. i = 0;
  10460. } else {
  10461. if (cd->mode != PT_MODE_BOOTLOADER) {
  10462. ret = -EPERM;
  10463. goto exit;
  10464. }
  10465. /* Open the bin file in Flash */
  10466. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10467. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10468. if (file_handle != PIP2_FW_FILE) {
  10469. ret = -ENOENT;
  10470. pt_debug(dev, DL_ERROR,
  10471. "%s Failed to open bin file\n", __func__);
  10472. goto exit;
  10473. }
  10474. /* Read the header length from the file */
  10475. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10476. read_size = 1;
  10477. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10478. read_buf);
  10479. if (bytes_read != read_size) {
  10480. ret = -EX_ERR_FREAD;
  10481. pt_debug(dev, DL_ERROR,
  10482. "%s Failed to bin file header len\n", __func__);
  10483. goto exit_close_file;
  10484. }
  10485. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10486. if (hdr_len == 0xFF) {
  10487. ret = -EX_ERR_FLEN;
  10488. pt_debug(dev, DL_ERROR,
  10489. "%s Bin header len is invalid\n", __func__);
  10490. goto exit_close_file;
  10491. }
  10492. /* Read the rest of the header from the bin file */
  10493. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10494. memset(read_buf, 0, sizeof(read_buf));
  10495. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10496. read_buf);
  10497. if (bytes_read != hdr_len) {
  10498. ret = -EX_ERR_FREAD;
  10499. pt_debug(dev, DL_ERROR,
  10500. "%s Failed to read bin file\n", __func__);
  10501. goto exit_close_file;
  10502. }
  10503. load_hdr_struct = true;
  10504. exit_close_file:
  10505. /* Close the file */
  10506. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10507. ret = -EX_ERR_FCLOSE;
  10508. pt_debug(dev, DL_ERROR,
  10509. "%s Failed to close bin file\n", __func__);
  10510. }
  10511. /*
  10512. * The length was already read so subtract 1 to make the rest of
  10513. * the offsets match the spec
  10514. */
  10515. i = PIP2_RESP_BODY_OFFSET - 1;
  10516. }
  10517. if (load_hdr_struct) {
  10518. hdr->length = hdr_len;
  10519. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10520. hdr->fw_major = (read_buf[i+3]);
  10521. hdr->fw_minor = (read_buf[i+4]);
  10522. hdr->fw_crc = (read_buf[i+5] << 24) |
  10523. (read_buf[i+6] << 16) |
  10524. (read_buf[i+7] << 8) |
  10525. (read_buf[i+8]);
  10526. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10527. (read_buf[i+10] << 16) |
  10528. (read_buf[i+11] << 8) |
  10529. (read_buf[i+12]);
  10530. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10531. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10532. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10533. if (hdr_len >= 22) {
  10534. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10535. (read_buf[i+20] << 16) |
  10536. (read_buf[i+21] << 8) |
  10537. (read_buf[i+22]);
  10538. } else {
  10539. hdr->hex_file_size = 0;
  10540. }
  10541. }
  10542. exit:
  10543. return ret;
  10544. }
  10545. /*******************************************************************************
  10546. * FUNCTION: _pt_pip2_file_get_stats
  10547. *
  10548. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10549. * opened file
  10550. *
  10551. * NOTE: The DUT must be in BL mode for this command to work
  10552. *
  10553. * RETURNS:
  10554. * !0 = Error
  10555. * 0 = Success
  10556. *
  10557. * PARAMETERS:
  10558. * *dev - pointer to device structure
  10559. * file_handle - File handle to read from
  10560. * *address - pointer to store address of file
  10561. * *file_size _ pointer to store size of file
  10562. ******************************************************************************/
  10563. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10564. u32 *file_size)
  10565. {
  10566. int ret = 1;
  10567. u16 status;
  10568. u16 actual_read_len;
  10569. u8 data[2];
  10570. u8 read_buf[16];
  10571. data[0] = file_handle;
  10572. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10573. ret = _pt_request_pip2_send_cmd(dev,
  10574. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10575. data, 2, read_buf, &actual_read_len);
  10576. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10577. if (ret || (status != 0x00)) {
  10578. pt_debug(dev, DL_ERROR,
  10579. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10580. __func__, status, file_handle, ret);
  10581. ret = -EIO;
  10582. goto exit;
  10583. }
  10584. pt_debug(dev, DL_DEBUG,
  10585. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10586. if (address) {
  10587. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10588. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10589. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10590. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10591. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10592. __func__, *address);
  10593. }
  10594. if (file_size) {
  10595. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10596. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10597. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10598. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10599. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10600. __func__, *file_size);
  10601. }
  10602. exit:
  10603. return ret;
  10604. }
  10605. /*******************************************************************************
  10606. * FUNCTION: _pt_pip2_file_seek_offset
  10607. *
  10608. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10609. * opened file
  10610. *
  10611. * NOTE: The DUT must be in BL mode for this command to work
  10612. * NOTE: File open/erase command can reset the offset
  10613. *
  10614. * RETURNS:
  10615. * !0 = Error
  10616. * 0 = Success
  10617. *
  10618. * PARAMETERS:
  10619. * *dev - pointer to device structure
  10620. * file_handle - File handle to read from
  10621. * read_offset - read offset of file
  10622. * write_offset - write offset of file
  10623. ******************************************************************************/
  10624. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10625. u32 read_offset, u32 write_offset)
  10626. {
  10627. int ret = 1;
  10628. u16 status;
  10629. u16 actual_read_len;
  10630. u8 data[10];
  10631. u8 read_buf[16];
  10632. data[0] = file_handle;
  10633. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10634. data[2] = 0xff & read_offset;
  10635. data[3] = 0xff & (read_offset >> 8);
  10636. data[4] = 0xff & (read_offset >> 16);
  10637. data[5] = 0xff & (read_offset >> 24);
  10638. data[6] = 0xff & write_offset;
  10639. data[7] = 0xff & (write_offset >> 8);
  10640. data[8] = 0xff & (write_offset >> 16);
  10641. data[9] = 0xff & (write_offset >> 24);
  10642. ret = _pt_request_pip2_send_cmd(dev,
  10643. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10644. data, 10, read_buf, &actual_read_len);
  10645. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10646. if (ret || (status != 0x00)) {
  10647. pt_debug(dev, DL_ERROR,
  10648. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10649. __func__, status, ret, file_handle);
  10650. ret = -EIO;
  10651. }
  10652. return ret;
  10653. }
  10654. /*******************************************************************************
  10655. * FUNCTION: _pt_pip2_file_crc
  10656. *
  10657. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10658. * the file.
  10659. *
  10660. * NOTE: The DUT must be in BL mode for this command to work
  10661. * NOTE: This command only can be used for BL version 1.8 or greater.
  10662. * BL version 1.8 added this change according to PGV-173.
  10663. *
  10664. * RETURNS:
  10665. * !0 = Error
  10666. * 0 = Success
  10667. *
  10668. * PARAMETERS:
  10669. * *dev - pointer to device structure
  10670. * file_handle - File handle to read from
  10671. * offset - start offset for CRC calculation
  10672. * length - number of bytes to calculate CRC over
  10673. * read_buf - pointer to the read buffer
  10674. ******************************************************************************/
  10675. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10676. u32 offset, u32 length, u8 *read_buf)
  10677. {
  10678. int rc = 1;
  10679. u16 actual_read_len;
  10680. u8 data[10];
  10681. data[0] = file_handle;
  10682. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10683. data[2] = 0xff & offset;
  10684. data[3] = 0xff & (offset >> 8);
  10685. data[4] = 0xff & (offset >> 16);
  10686. data[5] = 0xff & (offset >> 24);
  10687. data[6] = 0xff & length;
  10688. data[7] = 0xff & (length >> 8);
  10689. data[8] = 0xff & (length >> 16);
  10690. data[9] = 0xff & (length >> 24);
  10691. rc = _pt_request_pip2_send_cmd(dev,
  10692. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10693. data, 10, read_buf, &actual_read_len);
  10694. if (rc)
  10695. pt_debug(dev, DL_ERROR,
  10696. "%s Return FILE_CRC failure, rc = %d\n",
  10697. __func__, rc);
  10698. return rc;
  10699. }
  10700. /*******************************************************************************
  10701. * FUNCTION: pt_pip2_ping_test
  10702. *
  10703. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10704. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10705. * response payload matches what was sent.
  10706. * The max payload size is 247:
  10707. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10708. *
  10709. * RETURN:
  10710. * 0 = success
  10711. * !0 = failure
  10712. *
  10713. * PARAMETERS:
  10714. * *dev - pointer to device structure
  10715. * *attr - pointer to device attributes
  10716. * *buf - pointer to output buffer
  10717. ******************************************************************************/
  10718. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10719. {
  10720. u16 actual_read_len;
  10721. u8 *read_buf = NULL;
  10722. u8 *data = NULL;
  10723. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10724. int i = 1;
  10725. int j = 0;
  10726. int rc = 0;
  10727. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10728. if (!read_buf)
  10729. goto ping_test_exit;
  10730. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10731. if (!data)
  10732. goto ping_test_exit;
  10733. /* Load data payload with an array of walking 1's */
  10734. for (i = 0; i < 255; i++)
  10735. data[i] = 0x01 << (i % 8);
  10736. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10737. for (i = 0; i <= max_bytes; i++) {
  10738. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10739. PIP2_CMD_ID_PING, data, i, read_buf,
  10740. &actual_read_len);
  10741. if (rc) {
  10742. pt_debug(dev, DL_ERROR,
  10743. "%s: PING failed with %d byte payload\n",
  10744. __func__, i);
  10745. break;
  10746. }
  10747. /* Verify data returned matches data sent */
  10748. for (j = 0; j < i; j++) {
  10749. if (read_buf[data_offset + j] != data[j]) {
  10750. pt_debug(dev, DL_DEBUG,
  10751. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10752. __func__, i, j, data[j], j,
  10753. read_buf[data_offset + j]);
  10754. goto ping_test_exit;
  10755. }
  10756. }
  10757. }
  10758. ping_test_exit:
  10759. *last_packet_size = i - 1;
  10760. kfree(read_buf);
  10761. kfree(data);
  10762. return rc;
  10763. }
  10764. /*******************************************************************************
  10765. * FUNCTION: _pt_ic_parse_input_hex
  10766. *
  10767. * SUMMARY: Parse a char data array as space delimited hex values into
  10768. * an int array.
  10769. *
  10770. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10771. * buffer to store data is "u32". This function is still needed by the
  10772. * "command" sysfs node because the buffer type to store data is "u8".
  10773. *
  10774. * RETURN: Length of parsed data
  10775. *
  10776. * PARAMETERS:
  10777. * *dev - pointer to device structure
  10778. * *buf - pointer to buffer that holds the input array to parse
  10779. * buf_size - size of buf
  10780. * *ic_buf - pointer to array to store parsed data
  10781. * ic_buf_size - max size of ic_buf
  10782. ******************************************************************************/
  10783. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10784. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10785. {
  10786. const char *pbuf = buf;
  10787. unsigned long value;
  10788. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10789. u32 i = 0;
  10790. u32 j;
  10791. int last = 0;
  10792. int ret;
  10793. pt_debug(dev, DL_DEBUG,
  10794. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10795. __func__, pbuf, buf, buf_size, "scan buf size",
  10796. PT_MAX_PIP2_MSG_SIZE, buf);
  10797. while (pbuf <= (buf + buf_size)) {
  10798. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10799. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10800. __func__, "Max cmd size exceeded", i,
  10801. PT_MAX_PIP2_MSG_SIZE);
  10802. return -EINVAL;
  10803. }
  10804. if (i >= ic_buf_size) {
  10805. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10806. __func__, "Buffer size exceeded", i,
  10807. ic_buf_size);
  10808. return -EINVAL;
  10809. }
  10810. while (((*pbuf == ' ') || (*pbuf == ','))
  10811. && (pbuf < (buf + buf_size))) {
  10812. last = *pbuf;
  10813. pbuf++;
  10814. }
  10815. if (pbuf >= (buf + buf_size))
  10816. break;
  10817. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10818. if ((last == ',') && (*pbuf == ',')) {
  10819. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10820. __func__, "Invalid data format.");
  10821. return -EINVAL;
  10822. }
  10823. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10824. && (pbuf < (buf + buf_size))
  10825. && (*pbuf != ' ')
  10826. && (*pbuf != ','); j++) {
  10827. last = *pbuf;
  10828. scan_buf[j] = *pbuf++;
  10829. }
  10830. ret = kstrtoul(scan_buf, 16, &value);
  10831. if (ret < 0) {
  10832. pt_debug(dev, DL_ERROR,
  10833. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10834. "Invalid data format. ", scan_buf,
  10835. "Use \"0xHH,...,0xHH\"", " instead.",
  10836. i, ret);
  10837. return ret;
  10838. }
  10839. ic_buf[i] = value;
  10840. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10841. __func__, i, value);
  10842. i++;
  10843. }
  10844. return i;
  10845. }
  10846. /*******************************************************************************
  10847. * FUNCTION: _pt_ic_parse_input
  10848. *
  10849. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10850. * hex values or dec values into an int array with the following rules:
  10851. * 1) Hex values must have a "0x" prefix for each element or the first element
  10852. * only
  10853. * 2) Dec values do not have any prefix
  10854. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10855. * string
  10856. *
  10857. * RETURN: Number of values parsed
  10858. *
  10859. * PARAMETERS:
  10860. * *dev - pointer to device structure
  10861. * *buf - pointer to buffer that holds the input array to parse
  10862. * buf_size - size of buf
  10863. * *out_buf - pointer to array to store parsed data
  10864. * out_buf_size - max size of buffer to be stored
  10865. ******************************************************************************/
  10866. static int _pt_ic_parse_input(struct device *dev,
  10867. const char *buf, size_t buf_size,
  10868. u32 *out_buf, size_t out_buf_size)
  10869. {
  10870. const char *pbuf = buf;
  10871. unsigned long value;
  10872. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10873. u32 i = 0;
  10874. u32 j;
  10875. int last = 0;
  10876. int ret = 0;
  10877. u8 str_base = 0;
  10878. pt_debug(dev, DL_DEBUG,
  10879. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10880. __func__, buf_size, out_buf_size, "scan buf size",
  10881. PT_MAX_PIP2_MSG_SIZE, buf);
  10882. while (pbuf <= (buf + buf_size)) {
  10883. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10884. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10885. __func__, "Max cmd size exceeded", i,
  10886. PT_MAX_PIP2_MSG_SIZE);
  10887. ret = -EINVAL;
  10888. goto error;
  10889. }
  10890. if (i >= out_buf_size) {
  10891. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10892. __func__, "Buffer size exceeded", i,
  10893. out_buf_size);
  10894. ret = -EINVAL;
  10895. goto error;
  10896. }
  10897. while (((*pbuf == ' ') || (*pbuf == ','))
  10898. && (pbuf < (buf + buf_size))) {
  10899. last = *pbuf;
  10900. pbuf++;
  10901. }
  10902. if (pbuf >= (buf + buf_size))
  10903. break;
  10904. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10905. if ((last == ',') && (*pbuf == ',')) {
  10906. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10907. __func__, "Invalid data format.");
  10908. ret = -EINVAL;
  10909. goto error;
  10910. }
  10911. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10912. && (pbuf < (buf + buf_size))
  10913. && (*pbuf != ' ')
  10914. && (*pbuf != ','); j++) {
  10915. last = *pbuf;
  10916. scan_buf[j] = *pbuf++;
  10917. }
  10918. if (i == 0) {
  10919. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10920. (strncmp(scan_buf, "0X", 2) == 0))
  10921. str_base = 16;
  10922. else
  10923. str_base = 10;
  10924. } else {
  10925. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10926. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10927. (str_base == 10)) {
  10928. pt_debug(dev, DL_ERROR,
  10929. "%s: Decimal and Heximal data mixed\n",
  10930. __func__);
  10931. ret = -EINVAL;
  10932. goto error;
  10933. }
  10934. }
  10935. ret = kstrtoul(scan_buf, str_base, &value);
  10936. if (ret < 0) {
  10937. pt_debug(dev, DL_ERROR,
  10938. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10939. "Invalid data format. ", scan_buf,
  10940. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10941. " instead.", i, ret);
  10942. goto error;
  10943. }
  10944. out_buf[i] = value;
  10945. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10946. __func__, i, value, value);
  10947. i++;
  10948. }
  10949. ret = i;
  10950. error:
  10951. return ret;
  10952. }
  10953. #ifdef TTHE_TUNER_SUPPORT
  10954. /*******************************************************************************
  10955. * FUNCTION: tthe_debugfs_open
  10956. *
  10957. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10958. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10959. * printing a large number of fingers and sensor data without overflowing
  10960. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10961. * even multiple of PAGE_SIZE
  10962. *
  10963. * RETURN:
  10964. * 0 = success
  10965. * !0 = failure
  10966. *
  10967. * PARAMETERS:
  10968. * *inode - file inode number
  10969. * *filp - file pointer to debugfs file
  10970. ******************************************************************************/
  10971. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10972. {
  10973. struct pt_core_data *cd = inode->i_private;
  10974. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10975. filp->private_data = inode->i_private;
  10976. if (cd->tthe_buf)
  10977. return -EBUSY;
  10978. while (buf_size < 4096)
  10979. buf_size = buf_size << 1;
  10980. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10981. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10982. cd->tthe_buf_size = buf_size;
  10983. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10984. if (!cd->tthe_buf)
  10985. return -ENOMEM;
  10986. return 0;
  10987. }
  10988. /*******************************************************************************
  10989. * FUNCTION: tthe_debugfs_close
  10990. *
  10991. * SUMMARY: Close method for tthe_tuner debugfs node.
  10992. *
  10993. * RETURN:
  10994. * 0 = success
  10995. * !0 = failure
  10996. *
  10997. * PARAMETERS:
  10998. * *inode - file inode number
  10999. * *filp - file pointer to debugfs file
  11000. ******************************************************************************/
  11001. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11002. {
  11003. struct pt_core_data *cd = filp->private_data;
  11004. filp->private_data = NULL;
  11005. kfree(cd->tthe_buf);
  11006. cd->tthe_buf = NULL;
  11007. return 0;
  11008. }
  11009. /*******************************************************************************
  11010. * FUNCTION: tthe_debugfs_read
  11011. *
  11012. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11013. * tthe_buf to user buffer.
  11014. *
  11015. * RETURN: Size of debugfs data print
  11016. *
  11017. * PARAMETERS:
  11018. * *filp - file pointer to debugfs file
  11019. * *buf - the user space buffer to read to
  11020. * count - the maximum number of bytes to read
  11021. * *ppos - the current position in the buffer
  11022. ******************************************************************************/
  11023. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11024. size_t count, loff_t *ppos)
  11025. {
  11026. struct pt_core_data *cd = filp->private_data;
  11027. int size;
  11028. int ret;
  11029. static int partial_read;
  11030. wait_event_interruptible(cd->wait_q,
  11031. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11032. mutex_lock(&cd->tthe_lock);
  11033. if (cd->tthe_exit) {
  11034. mutex_unlock(&cd->tthe_lock);
  11035. return 0;
  11036. }
  11037. if (count > cd->tthe_buf_len)
  11038. size = cd->tthe_buf_len;
  11039. else
  11040. size = count;
  11041. if (!size) {
  11042. mutex_unlock(&cd->tthe_lock);
  11043. return 0;
  11044. }
  11045. if (partial_read) {
  11046. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11047. partial_read = 0;
  11048. } else {
  11049. ret = copy_to_user(buf, cd->tthe_buf, size);
  11050. }
  11051. if (size == count)
  11052. partial_read = count;
  11053. if (ret == size)
  11054. return -EFAULT;
  11055. size -= ret;
  11056. cd->tthe_buf_len -= size;
  11057. mutex_unlock(&cd->tthe_lock);
  11058. *ppos += size;
  11059. return size;
  11060. }
  11061. static const struct file_operations tthe_debugfs_fops = {
  11062. .open = tthe_debugfs_open,
  11063. .release = tthe_debugfs_close,
  11064. .read = tthe_debugfs_read,
  11065. };
  11066. #endif
  11067. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11068. .start_bl = _pt_request_pip_start_bl,
  11069. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11070. .resume_scanning = _pt_request_pip_resume_scanning,
  11071. .get_param = _pt_request_pip_get_param,
  11072. .set_param = _pt_request_pip_set_param,
  11073. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11074. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11075. .get_data_structure = _pt_request_pip_get_data_structure,
  11076. .run_selftest = _pt_request_pip_run_selftest,
  11077. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11078. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11079. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11080. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11081. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11082. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11083. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11084. .read_data_block = _pt_request_pip_read_data_block,
  11085. .write_data_block = _pt_request_pip_write_data_block,
  11086. .user_cmd = _pt_request_pip_user_cmd,
  11087. .get_bl_info = _pt_request_pip_bl_get_information,
  11088. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11089. .launch_app = _pt_request_pip_launch_app,
  11090. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11091. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11092. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11093. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11094. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11095. .pip2_file_open = _pt_pip2_file_open,
  11096. .pip2_file_close = _pt_pip2_file_close,
  11097. .pip2_file_erase = _pt_pip2_file_erase,
  11098. .read_us_file = _pt_read_us_file,
  11099. .manage_cal_data = _pt_manage_local_cal_data,
  11100. .calc_crc = crc_ccitt_calculate,
  11101. #ifdef TTDL_DIAGNOSTICS
  11102. .pip2_file_read = _pt_pip2_file_read,
  11103. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11104. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11105. .pip2_file_crc = _pt_pip2_file_crc,
  11106. #endif
  11107. };
  11108. static struct pt_core_commands _pt_core_commands = {
  11109. .subscribe_attention = _pt_subscribe_attention,
  11110. .unsubscribe_attention = _pt_unsubscribe_attention,
  11111. .request_exclusive = _pt_request_exclusive,
  11112. .release_exclusive = _pt_release_exclusive,
  11113. .request_reset = _pt_request_reset,
  11114. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11115. .request_enum = _pt_request_enum,
  11116. .request_sysinfo = _pt_request_sysinfo,
  11117. .request_loader_pdata = _pt_request_loader_pdata,
  11118. .request_stop_wd = _pt_request_stop_wd,
  11119. .request_start_wd = _pt_request_start_wd,
  11120. .request_get_mode = _pt_request_get_mode,
  11121. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11122. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11123. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11124. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11125. .request_dut_generation = _pt_request_dut_generation,
  11126. .request_hw_version = _pt_request_hw_version,
  11127. .parse_sysfs_input = _pt_ic_parse_input,
  11128. #ifdef TTHE_TUNER_SUPPORT
  11129. .request_tthe_print = _pt_request_tthe_print,
  11130. #endif
  11131. #ifdef TTDL_DIAGNOSTICS
  11132. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11133. #endif
  11134. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11135. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11136. };
  11137. struct pt_core_commands *pt_get_commands(void)
  11138. {
  11139. return &_pt_core_commands;
  11140. }
  11141. EXPORT_SYMBOL_GPL(pt_get_commands);
  11142. static DEFINE_MUTEX(core_list_lock);
  11143. static LIST_HEAD(core_list);
  11144. static DEFINE_MUTEX(module_list_lock);
  11145. static LIST_HEAD(module_list);
  11146. static int core_number;
  11147. /*******************************************************************************
  11148. * FUNCTION: pt_probe_module
  11149. *
  11150. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11151. *
  11152. * RETURN:
  11153. * 0 = success
  11154. * !0 = failure
  11155. *
  11156. * PARAMETERS:
  11157. * *cd - pointer to core data
  11158. * *module - pointer to module structure
  11159. ******************************************************************************/
  11160. static int pt_probe_module(struct pt_core_data *cd,
  11161. struct pt_module *module)
  11162. {
  11163. struct module_node *module_node;
  11164. int rc = 0;
  11165. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11166. if (!module_node)
  11167. return -ENOMEM;
  11168. module_node->module = module;
  11169. mutex_lock(&cd->module_list_lock);
  11170. list_add(&module_node->node, &cd->module_list);
  11171. mutex_unlock(&cd->module_list_lock);
  11172. rc = module->probe(cd->dev, &module_node->data);
  11173. if (rc) {
  11174. /*
  11175. * Remove from the list when probe fails
  11176. * in order not to call release
  11177. */
  11178. mutex_lock(&cd->module_list_lock);
  11179. list_del(&module_node->node);
  11180. mutex_unlock(&cd->module_list_lock);
  11181. kfree(module_node);
  11182. goto exit;
  11183. }
  11184. exit:
  11185. return rc;
  11186. }
  11187. /*******************************************************************************
  11188. * FUNCTION: pt_release_module
  11189. *
  11190. * SUMMARY: Call the release pointer and remove the module pointer from
  11191. * module_list.
  11192. *
  11193. * PARAMETERS:
  11194. * *cd - pointer to core data
  11195. * *module - pointer to module structure
  11196. ******************************************************************************/
  11197. static void pt_release_module(struct pt_core_data *cd,
  11198. struct pt_module *module)
  11199. {
  11200. struct module_node *m, *m_n;
  11201. mutex_lock(&cd->module_list_lock);
  11202. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11203. if (m->module == module) {
  11204. module->release(cd->dev, m->data);
  11205. list_del(&m->node);
  11206. kfree(m);
  11207. break;
  11208. }
  11209. mutex_unlock(&cd->module_list_lock);
  11210. }
  11211. /*******************************************************************************
  11212. * FUNCTION: pt_probe_modules
  11213. *
  11214. * SUMMARY: Iterate module_list and probe each module.
  11215. *
  11216. * PARAMETERS:
  11217. * *cd - pointer to core data
  11218. ******************************************************************************/
  11219. static void pt_probe_modules(struct pt_core_data *cd)
  11220. {
  11221. struct pt_module *m;
  11222. int rc = 0;
  11223. mutex_lock(&module_list_lock);
  11224. list_for_each_entry(m, &module_list, node) {
  11225. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11226. __func__, m->name);
  11227. rc = pt_probe_module(cd, m);
  11228. if (rc)
  11229. pt_debug(cd->dev, DL_ERROR,
  11230. "%s: Probe fails for module %s\n",
  11231. __func__, m->name);
  11232. }
  11233. mutex_unlock(&module_list_lock);
  11234. }
  11235. /*******************************************************************************
  11236. * FUNCTION: pt_release_modules
  11237. *
  11238. * SUMMARY: Iterate module_list and remove each module.
  11239. *
  11240. * PARAMETERS:
  11241. * *cd - pointer to core data
  11242. ******************************************************************************/
  11243. static void pt_release_modules(struct pt_core_data *cd)
  11244. {
  11245. struct pt_module *m;
  11246. mutex_lock(&module_list_lock);
  11247. list_for_each_entry(m, &module_list, node)
  11248. pt_release_module(cd, m);
  11249. mutex_unlock(&module_list_lock);
  11250. }
  11251. /*******************************************************************************
  11252. * FUNCTION: pt_get_core_data
  11253. *
  11254. * SUMMARY: Iterate core_list and get core data.
  11255. *
  11256. * RETURN:
  11257. * pointer to core data or null pointer if fail
  11258. *
  11259. * PARAMETERS:
  11260. * *id - pointer to core id
  11261. ******************************************************************************/
  11262. struct pt_core_data *pt_get_core_data(char *id)
  11263. {
  11264. struct pt_core_data *d;
  11265. list_for_each_entry(d, &core_list, node)
  11266. if (!strncmp(d->core_id, id, 20))
  11267. return d;
  11268. return NULL;
  11269. }
  11270. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11271. /*******************************************************************************
  11272. * FUNCTION: pt_add_core
  11273. *
  11274. * SUMMARY: Add core data to the core_list.
  11275. *
  11276. * PARAMETERS:
  11277. * *dev - pointer to device structure
  11278. ******************************************************************************/
  11279. static void pt_add_core(struct device *dev)
  11280. {
  11281. struct pt_core_data *d;
  11282. struct pt_core_data *cd = dev_get_drvdata(dev);
  11283. mutex_lock(&core_list_lock);
  11284. list_for_each_entry(d, &core_list, node)
  11285. if (d->dev == dev)
  11286. goto unlock;
  11287. list_add(&cd->node, &core_list);
  11288. unlock:
  11289. mutex_unlock(&core_list_lock);
  11290. }
  11291. /*******************************************************************************
  11292. * FUNCTION: pt_del_core
  11293. *
  11294. * SUMMARY: Remove core data from the core_list.
  11295. *
  11296. * PARAMETERS:
  11297. * *dev - pointer to device structure
  11298. ******************************************************************************/
  11299. static void pt_del_core(struct device *dev)
  11300. {
  11301. struct pt_core_data *d, *d_n;
  11302. mutex_lock(&core_list_lock);
  11303. list_for_each_entry_safe(d, d_n, &core_list, node)
  11304. if (d->dev == dev) {
  11305. list_del(&d->node);
  11306. goto unlock;
  11307. }
  11308. unlock:
  11309. mutex_unlock(&core_list_lock);
  11310. }
  11311. /*******************************************************************************
  11312. * FUNCTION: pt_register_module
  11313. *
  11314. * SUMMARY: Register the module to module_list and probe the module for each
  11315. * core.
  11316. *
  11317. * RETURN:
  11318. * 0 = success
  11319. * !0 = failure
  11320. *
  11321. * PARAMETERS:
  11322. * *module - pointer to module structure
  11323. ******************************************************************************/
  11324. int pt_register_module(struct pt_module *module)
  11325. {
  11326. struct pt_module *m;
  11327. struct pt_core_data *cd;
  11328. int rc = 0;
  11329. if (!module || !module->probe || !module->release)
  11330. return -EINVAL;
  11331. mutex_lock(&module_list_lock);
  11332. list_for_each_entry(m, &module_list, node)
  11333. if (m == module) {
  11334. rc = -EEXIST;
  11335. goto unlock;
  11336. }
  11337. list_add(&module->node, &module_list);
  11338. /* Probe the module for each core */
  11339. mutex_lock(&core_list_lock);
  11340. list_for_each_entry(cd, &core_list, node)
  11341. pt_probe_module(cd, module);
  11342. mutex_unlock(&core_list_lock);
  11343. unlock:
  11344. mutex_unlock(&module_list_lock);
  11345. return rc;
  11346. }
  11347. EXPORT_SYMBOL_GPL(pt_register_module);
  11348. /*******************************************************************************
  11349. * FUNCTION: pt_unregister_module
  11350. *
  11351. * SUMMARY: Release the module for each core and remove the module from
  11352. * module_list.
  11353. *
  11354. * RETURN:
  11355. * 0 = success
  11356. * !0 = failure
  11357. *
  11358. * PARAMETERS:
  11359. * *module - pointer to module structure
  11360. ******************************************************************************/
  11361. void pt_unregister_module(struct pt_module *module)
  11362. {
  11363. struct pt_module *m, *m_n;
  11364. struct pt_core_data *cd;
  11365. if (!module)
  11366. return;
  11367. mutex_lock(&module_list_lock);
  11368. /* Release the module for each core */
  11369. mutex_lock(&core_list_lock);
  11370. list_for_each_entry(cd, &core_list, node)
  11371. pt_release_module(cd, module);
  11372. mutex_unlock(&core_list_lock);
  11373. list_for_each_entry_safe(m, m_n, &module_list, node)
  11374. if (m == module) {
  11375. list_del(&m->node);
  11376. break;
  11377. }
  11378. mutex_unlock(&module_list_lock);
  11379. }
  11380. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11381. /*******************************************************************************
  11382. * FUNCTION: pt_get_module_data
  11383. *
  11384. * SUMMARY: Get module data from module_node by module_list.
  11385. *
  11386. * RETURN:
  11387. * pointer to module data
  11388. *
  11389. * PARAMETERS:
  11390. * *dev - pointer to device structure
  11391. * *module - pointer to module structure
  11392. ******************************************************************************/
  11393. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11394. {
  11395. struct pt_core_data *cd = dev_get_drvdata(dev);
  11396. struct module_node *m;
  11397. void *data = NULL;
  11398. mutex_lock(&cd->module_list_lock);
  11399. list_for_each_entry(m, &cd->module_list, node)
  11400. if (m->module == module) {
  11401. data = m->data;
  11402. break;
  11403. }
  11404. mutex_unlock(&cd->module_list_lock);
  11405. return data;
  11406. }
  11407. EXPORT_SYMBOL(pt_get_module_data);
  11408. #ifdef CONFIG_HAS_EARLYSUSPEND
  11409. /*******************************************************************************
  11410. * FUNCTION: pt_early_suspend
  11411. *
  11412. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11413. * attention list.
  11414. *
  11415. * PARAMETERS:
  11416. * *h - pointer to early_suspend structure
  11417. ******************************************************************************/
  11418. static void pt_early_suspend(struct early_suspend *h)
  11419. {
  11420. struct pt_core_data *cd =
  11421. container_of(h, struct pt_core_data, es);
  11422. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11423. }
  11424. /*******************************************************************************
  11425. * FUNCTION: pt_late_resume
  11426. *
  11427. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11428. * attention list.
  11429. *
  11430. * PARAMETERS:
  11431. * *h - pointer to early_suspend structure
  11432. ******************************************************************************/
  11433. static void pt_late_resume(struct early_suspend *h)
  11434. {
  11435. struct pt_core_data *cd =
  11436. container_of(h, struct pt_core_data, es);
  11437. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11438. }
  11439. /*******************************************************************************
  11440. * FUNCTION: pt_setup_early_suspend
  11441. *
  11442. * SUMMARY: Register early/suspend function to the system.
  11443. *
  11444. * PARAMETERS:
  11445. * *cd - pointer to core data
  11446. ******************************************************************************/
  11447. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11448. {
  11449. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11450. cd->es.suspend = pt_early_suspend;
  11451. cd->es.resume = pt_late_resume;
  11452. register_early_suspend(&cd->es);
  11453. }
  11454. #elif defined(CONFIG_DRM)
  11455. static void pt_resume_work(struct work_struct *work)
  11456. {
  11457. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11458. resume_work);
  11459. int rc = 0;
  11460. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11461. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11462. return;
  11463. rc = pt_core_easywake_off(pt_data);
  11464. if (rc < 0) {
  11465. pt_debug(pt_data->dev, DL_ERROR,
  11466. "%s: Error on wake\n", __func__);
  11467. }
  11468. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11469. return;
  11470. }
  11471. static void pt_suspend_work(struct work_struct *work)
  11472. {
  11473. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11474. suspend_work);
  11475. int rc = 0;
  11476. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11477. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11478. return;
  11479. rc = pt_core_easywake_on(pt_data);
  11480. if (rc < 0) {
  11481. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11482. return;
  11483. }
  11484. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11485. return;
  11486. }
  11487. /*******************************************************************************
  11488. * FUNCTION: drm_notifier_callback
  11489. *
  11490. * SUMMARY: Call back function for DRM notifier to allow to call
  11491. * resume/suspend attention list.
  11492. *
  11493. * RETURN:
  11494. * 0 = success
  11495. *
  11496. * PARAMETERS:
  11497. * *self - pointer to notifier_block structure
  11498. * event - event type of fb notifier
  11499. * *data - pointer to fb_event structure
  11500. ******************************************************************************/
  11501. static int drm_notifier_callback(struct notifier_block *self,
  11502. unsigned long event, void *data)
  11503. {
  11504. struct pt_core_data *cd =
  11505. container_of(self, struct pt_core_data, fb_notifier);
  11506. struct drm_panel_notifier *evdata = data;
  11507. int *blank;
  11508. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11509. if (!evdata)
  11510. goto exit;
  11511. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11512. event == DRM_PANEL_EVENT_BLANK)) {
  11513. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11514. __func__, event);
  11515. goto exit;
  11516. }
  11517. if (cd->quick_boot || cd->drv_debug_suspend)
  11518. goto exit;
  11519. blank = evdata->data;
  11520. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11521. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11522. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11523. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11524. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11525. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11526. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11527. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11528. __func__, event);
  11529. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11530. if (cd->fb_state != FB_ON) {
  11531. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11532. __func__);
  11533. cancel_work_sync(&cd->resume_offload_work);
  11534. cancel_work_sync(&cd->suspend_offload_work);
  11535. cancel_work_sync(&cd->resume_work);
  11536. cancel_work_sync(&cd->suspend_work);
  11537. queue_work(cd->pt_workqueue, &cd->resume_work);
  11538. #if defined(CONFIG_PM_SLEEP)
  11539. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11540. __func__);
  11541. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11542. pt_core_resume_(cd->dev);
  11543. #endif
  11544. cd->fb_state = FB_ON;
  11545. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11546. }
  11547. }
  11548. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11549. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11550. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11551. if (cd->fb_state != FB_OFF) {
  11552. #if defined(CONFIG_PM_SLEEP)
  11553. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11554. __func__);
  11555. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11556. pt_core_suspend_(cd->dev);
  11557. #endif
  11558. cancel_work_sync(&cd->resume_offload_work);
  11559. cancel_work_sync(&cd->suspend_offload_work);
  11560. cancel_work_sync(&cd->resume_work);
  11561. cancel_work_sync(&cd->suspend_work);
  11562. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11563. cd->fb_state = FB_OFF;
  11564. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11565. }
  11566. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11567. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11568. __func__, event);
  11569. }
  11570. } else {
  11571. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11572. __func__, *blank);
  11573. }
  11574. exit:
  11575. return 0;
  11576. }
  11577. /*******************************************************************************
  11578. * FUNCTION: pt_setup_drm_notifier
  11579. *
  11580. * SUMMARY: Set up call back function into drm notifier.
  11581. *
  11582. * PARAMETERS:
  11583. * *cd - pointer to core data
  11584. ******************************************************************************/
  11585. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11586. {
  11587. cd->fb_state = FB_NONE;
  11588. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11589. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11590. if (!active_panel)
  11591. pt_debug(cd->dev, DL_ERROR,
  11592. "%s: Active panel not registered!\n", __func__);
  11593. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11594. if (!cd->pt_workqueue) {
  11595. pt_debug(cd->dev, DL_ERROR,
  11596. "%s: worker thread creation failed !\n", __func__);
  11597. }
  11598. if (cd->pt_workqueue) {
  11599. INIT_WORK(&cd->resume_work, pt_resume_work);
  11600. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11601. }
  11602. if (active_panel &&
  11603. drm_panel_notifier_register(active_panel,
  11604. &cd->fb_notifier) < 0)
  11605. pt_debug(cd->dev, DL_ERROR,
  11606. "%s: Register notifier failed!\n", __func__);
  11607. }
  11608. #elif defined(CONFIG_FB)
  11609. /*******************************************************************************
  11610. * FUNCTION: fb_notifier_callback
  11611. *
  11612. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11613. * resume/suspend attention list.
  11614. *
  11615. * RETURN:
  11616. * 0 = success
  11617. *
  11618. * PARAMETERS:
  11619. * *self - pointer to notifier_block structure
  11620. * event - event type of fb notifier
  11621. * *data - pointer to fb_event structure
  11622. ******************************************************************************/
  11623. static int fb_notifier_callback(struct notifier_block *self,
  11624. unsigned long event, void *data)
  11625. {
  11626. struct pt_core_data *cd =
  11627. container_of(self, struct pt_core_data, fb_notifier);
  11628. struct fb_event *evdata = data;
  11629. int *blank;
  11630. if (event != FB_EVENT_BLANK || !evdata)
  11631. goto exit;
  11632. blank = evdata->data;
  11633. if (*blank == FB_BLANK_UNBLANK) {
  11634. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11635. if (cd->fb_state != FB_ON) {
  11636. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11637. #if defined(CONFIG_PM_SLEEP)
  11638. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11639. pt_core_resume_(cd->dev);
  11640. #endif
  11641. cd->fb_state = FB_ON;
  11642. }
  11643. } else if (*blank == FB_BLANK_POWERDOWN) {
  11644. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11645. if (cd->fb_state != FB_OFF) {
  11646. #if defined(CONFIG_PM_SLEEP)
  11647. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11648. pt_core_suspend_(cd->dev);
  11649. #endif
  11650. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11651. cd->fb_state = FB_OFF;
  11652. }
  11653. }
  11654. exit:
  11655. return 0;
  11656. }
  11657. /*******************************************************************************
  11658. * FUNCTION: pt_setup_fb_notifier
  11659. *
  11660. * SUMMARY: Set up call back function into fb notifier.
  11661. *
  11662. * PARAMETERS:
  11663. * *cd - pointer to core data
  11664. ******************************************************************************/
  11665. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11666. {
  11667. int rc = 0;
  11668. cd->fb_state = FB_ON;
  11669. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11670. rc = fb_register_client(&cd->fb_notifier);
  11671. if (rc)
  11672. pt_debug(cd->dev, DL_ERROR,
  11673. "Unable to register fb_notifier: %d\n", rc);
  11674. }
  11675. #endif
  11676. /*******************************************************************************
  11677. * FUNCTION: pt_watchdog_work
  11678. *
  11679. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11680. * sleeping then this function simply returns. If the DUT is awake the
  11681. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11682. * somehow a response is waiting on the DUT that has not been read. If
  11683. * this occurs the message is simply consumed. If or once the IRQ is
  11684. * cleared, a PIP PING message is sent to the DUT and if the response
  11685. * is received the watchdog succeeds and exits, if no response is seen
  11686. * a startup is queued unless the maximum number of startups have already
  11687. * been attempted, in that case a BL is attempted.
  11688. *
  11689. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11690. * work thread
  11691. *
  11692. * RETURN: void
  11693. *
  11694. * PARAMETERS:
  11695. * *work - pointer to a work structure for the watchdog work queue
  11696. ******************************************************************************/
  11697. static void pt_watchdog_work(struct work_struct *work)
  11698. {
  11699. int rc = 0;
  11700. struct pt_core_data *cd = container_of(work,
  11701. struct pt_core_data, watchdog_work);
  11702. /*
  11703. * if found the current sleep_state is SS_SLEEPING
  11704. * then no need to request_exclusive, directly return
  11705. */
  11706. if (cd->sleep_state == SS_SLEEPING)
  11707. return;
  11708. #ifdef TTDL_DIAGNOSTICS
  11709. cd->watchdog_count++;
  11710. #endif /* TTDL_DIAGNOSTICS */
  11711. /*
  11712. * The first WD interval was extended to allow DDI to come up.
  11713. * If the WD interval is not the default then adjust timer to the
  11714. * current setting. The user can override value with drv_debug sysfs.
  11715. */
  11716. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11717. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11718. msecs_to_jiffies(cd->watchdog_interval));
  11719. }
  11720. if (pt_check_irq_asserted(cd)) {
  11721. #ifdef TTDL_DIAGNOSTICS
  11722. cd->watchdog_irq_stuck_count++;
  11723. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11724. #endif /* TTDL_DIAGNOSTICS */
  11725. pt_debug(cd->dev, DL_ERROR,
  11726. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11727. __func__);
  11728. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11729. }
  11730. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11731. if (rc < 0) {
  11732. pt_debug(cd->dev, DL_ERROR,
  11733. "%s: fail get exclusive ex=%p own=%p\n",
  11734. __func__, cd->exclusive_dev, cd->dev);
  11735. goto queue_startup;
  11736. }
  11737. rc = pt_pip_null_(cd);
  11738. if (release_exclusive(cd, cd->dev) < 0)
  11739. pt_debug(cd->dev, DL_ERROR,
  11740. "%s: fail to release exclusive\n", __func__);
  11741. queue_startup:
  11742. if (rc) {
  11743. #ifdef TTDL_DIAGNOSTICS
  11744. cd->watchdog_failed_access_count++;
  11745. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11746. #endif /* TTDL_DIAGNOSTICS */
  11747. pt_debug(cd->dev, DL_ERROR,
  11748. "%s: failed to access device in WD, retry count=%d\n",
  11749. __func__, cd->startup_retry_count);
  11750. /* Already tried FW upgrade because of watchdog but failed */
  11751. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11752. return;
  11753. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11754. /*
  11755. * Any wrapper function that trys to disable the
  11756. * WD killing this worker cannot be called here.
  11757. */
  11758. rc = request_exclusive(cd, cd->dev,
  11759. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11760. if (rc < 0) {
  11761. pt_debug(cd->dev, DL_ERROR,
  11762. "%s: fail get exclusive ex=%p own=%p\n",
  11763. __func__, cd->exclusive_dev, cd->dev);
  11764. goto exit;
  11765. }
  11766. cd->hw_detected = false;
  11767. cd->startup_status = STARTUP_STATUS_START;
  11768. pt_debug(cd->dev, DL_DEBUG,
  11769. "%s: Startup Status Reset\n", __func__);
  11770. rc = pt_dut_reset_and_wait(cd);
  11771. if (release_exclusive(cd, cd->dev) < 0)
  11772. pt_debug(cd->dev, DL_ERROR,
  11773. "%s: fail to release exclusive\n",
  11774. __func__);
  11775. if (!rc) {
  11776. cd->hw_detected = true;
  11777. if (!cd->flashless_dut)
  11778. pt_queue_enum(cd);
  11779. }
  11780. #ifdef TTDL_DIAGNOSTICS
  11781. cd->wd_xres_count++;
  11782. pt_debug(cd->dev, DL_ERROR,
  11783. "%s: Comm Failed - DUT reset [#%d]\n",
  11784. __func__, cd->wd_xres_count);
  11785. #endif /* TTDL_DIAGNOSTICS */
  11786. } else {
  11787. /*
  11788. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11789. * reset the part to regain communications, try to BL
  11790. */
  11791. pt_debug(cd->dev, DL_ERROR,
  11792. "%s: WD DUT access failure, Start FW Upgrade\n",
  11793. __func__);
  11794. #ifdef TTDL_DIAGNOSTICS
  11795. /*
  11796. * When diagnostics is enabled allow TTDL to keep
  11797. * trying to find the DUT. This allows the DUT to be
  11798. * hot swap-able while the host stays running. In
  11799. * production this may not be wanted as a customer
  11800. * may have several touch drivers and any driver
  11801. * that doesn't match the current DUT should give
  11802. * up trying and give up using the bus.
  11803. */
  11804. pt_debug(cd->dev, DL_INFO,
  11805. "%s: Resetting startup_retry_count\n",
  11806. __func__);
  11807. cd->startup_retry_count = 0;
  11808. #endif /* TTDL_DIAGNOSTICS */
  11809. /*
  11810. * Since fw may be broken,reset sysinfo ready flag
  11811. * to let upgrade function work.
  11812. */
  11813. mutex_lock(&cd->system_lock);
  11814. cd->sysinfo.ready = false;
  11815. mutex_unlock(&cd->system_lock);
  11816. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11817. pt_debug(cd->dev, DL_ERROR,
  11818. "%s: Queue Restart\n", __func__);
  11819. pt_queue_restart(cd);
  11820. } else
  11821. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11822. }
  11823. } else {
  11824. cd->hw_detected = true;
  11825. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11826. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11827. pt_debug(cd->dev, DL_ERROR,
  11828. "%s: HW detected but not enumerated\n",
  11829. __func__);
  11830. pt_queue_enum(cd);
  11831. }
  11832. }
  11833. exit:
  11834. pt_start_wd_timer(cd);
  11835. }
  11836. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11837. /*******************************************************************************
  11838. * FUNCTION: pt_watchdog_timer
  11839. *
  11840. * SUMMARY: The function that is called when the WD timer expires. If the
  11841. * watchdog work is not already busy schedule the watchdog work queue.
  11842. *
  11843. * RETURN: void
  11844. *
  11845. * PARAMETERS:
  11846. * handle - Handle to the watchdog timer
  11847. ******************************************************************************/
  11848. static void pt_watchdog_timer(unsigned long handle)
  11849. {
  11850. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11851. if (!cd)
  11852. return;
  11853. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11854. __func__);
  11855. if (!work_pending(&cd->watchdog_work))
  11856. schedule_work(&cd->watchdog_work);
  11857. }
  11858. #else
  11859. /*******************************************************************************
  11860. * FUNCTION: pt_watchdog_timer
  11861. *
  11862. * SUMMARY: The function that is called when the WD timer expires. If the
  11863. * watchdog work is not already busy schedule the watchdog work queue.
  11864. *
  11865. * RETURN: void
  11866. *
  11867. * PARAMETERS:
  11868. * *t - Pointer to timer list
  11869. ******************************************************************************/
  11870. static void pt_watchdog_timer(struct timer_list *t)
  11871. {
  11872. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11873. if (!cd)
  11874. return;
  11875. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11876. __func__);
  11877. if (!work_pending(&cd->watchdog_work))
  11878. schedule_work(&cd->watchdog_work);
  11879. }
  11880. #endif
  11881. /*******************************************************************************
  11882. * Core sysfs show and store functions
  11883. ******************************************************************************/
  11884. /*******************************************************************************
  11885. * FUNCTION: pt_hw_version_show
  11886. *
  11887. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11888. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11889. *
  11890. * RETURN: size of data written to sysfs node
  11891. *
  11892. * PARAMETERS:
  11893. * *dev - pointer to device structure
  11894. * *attr - pointer to device attributes structure
  11895. * *buf - pointer to print output buffer
  11896. ******************************************************************************/
  11897. static ssize_t pt_hw_version_show(struct device *dev,
  11898. struct device_attribute *attr, char *buf)
  11899. {
  11900. struct pt_core_data *cd = dev_get_drvdata(dev);
  11901. _pt_request_hw_version(dev, cd->hw_version);
  11902. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11903. }
  11904. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11905. /*******************************************************************************
  11906. * FUNCTION: pt_drv_version_show
  11907. *
  11908. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11909. * TTDL version information
  11910. *
  11911. * RETURN: Char buffer with printed TTDL version information
  11912. *
  11913. * PARAMETERS:
  11914. * *dev - pointer to device structure
  11915. * *attr - pointer to device attributes
  11916. * *buf - pointer to output buffer
  11917. ******************************************************************************/
  11918. static ssize_t pt_drv_version_show(struct device *dev,
  11919. struct device_attribute *attr, char *buf)
  11920. {
  11921. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11922. "Driver: %s\nVersion: %s\nDate: %s\n",
  11923. pt_driver_core_name, pt_driver_core_version,
  11924. pt_driver_core_date);
  11925. }
  11926. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11927. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11928. /*******************************************************************************
  11929. * FUNCTION: pt_fw_version_show
  11930. *
  11931. * SUMMARY: Show method for the fw_version sysfs node that will
  11932. * show the firmware, bootloader and PIP version information
  11933. *
  11934. * RETURN: Size of printed buffer
  11935. *
  11936. * PARAMETERS:
  11937. * *dev - pointer to device structure
  11938. * *attr - pointer to device attributes
  11939. * *buf - pointer to output buffer
  11940. ******************************************************************************/
  11941. static ssize_t pt_fw_version_show(struct device *dev,
  11942. struct device_attribute *attr, char *buf)
  11943. {
  11944. struct pt_core_data *cd = dev_get_drvdata(dev);
  11945. struct pt_ttdata *ttdata;
  11946. int rc = 0;
  11947. if (cd->mode == PT_MODE_OPERATIONAL)
  11948. rc = pt_hid_output_get_sysinfo_(cd);
  11949. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11950. __func__, cd->mode, cd->sysinfo.ready);
  11951. if (cd->sysinfo.ready)
  11952. ttdata = &cd->sysinfo.ttdata;
  11953. else
  11954. rc = -ENODATA;
  11955. if (!rc) {
  11956. return scnprintf(buf, strlen(buf),
  11957. "Status: %d\n"
  11958. "FW : %d.%d.%d\n"
  11959. "Config: %d\n"
  11960. "BL : %d.%d\n"
  11961. "PIP : %d.%d\n",
  11962. rc,
  11963. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11964. ttdata->revctrl,
  11965. ttdata->fw_ver_conf,
  11966. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11967. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11968. } else {
  11969. return scnprintf(buf, strlen(buf),
  11970. "Status: %d\n"
  11971. "FW : n/a\n"
  11972. "Config: n/a\n"
  11973. "BL : n/a\n"
  11974. "PIP : n/a\n",
  11975. rc);
  11976. }
  11977. }
  11978. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11979. /*******************************************************************************
  11980. * FUNCTION: pt_sysinfo_show
  11981. *
  11982. * SUMMARY: Show method for the sysinfo sysfs node that will
  11983. * show all the information from get system information command.
  11984. *
  11985. * RETURN: Size of printed buffer
  11986. *
  11987. * PARAMETERS:
  11988. * *dev - pointer to device structure
  11989. * *attr - pointer to device attributes
  11990. * *buf - pointer to output buffer
  11991. ******************************************************************************/
  11992. static ssize_t pt_sysinfo_show(struct device *dev,
  11993. struct device_attribute *attr, char *buf)
  11994. {
  11995. struct pt_core_data *cd = dev_get_drvdata(dev);
  11996. struct pt_sysinfo *si;
  11997. struct pt_ttdata *ttdata = NULL;
  11998. struct pt_sensing_conf_data *scd = NULL;
  11999. int rc = 0;
  12000. if (cd->mode == PT_MODE_OPERATIONAL) {
  12001. rc = pt_hid_output_get_sysinfo_(cd);
  12002. if (cd->sysinfo.ready) {
  12003. si = &cd->sysinfo;
  12004. ttdata = &si->ttdata;
  12005. scd = &si->sensing_conf_data;
  12006. } else
  12007. rc = -ENODATA;
  12008. } else
  12009. rc = -EPERM;
  12010. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12011. __func__, cd->mode, cd->sysinfo.ready);
  12012. if (!rc && ttdata && scd) {
  12013. return scnprintf(buf, strlen(buf),
  12014. "Status: %d\n"
  12015. "pip_ver_major: 0x%02X\n"
  12016. "pip_ver_minor: 0x%02X\n"
  12017. "fw_pid : 0x%04X\n"
  12018. "fw_ver_major : 0x%02X\n"
  12019. "fw_ver_minor : 0x%02X\n"
  12020. "revctrl : 0x%08X\n"
  12021. "fw_ver_conf : 0x%04X\n"
  12022. "bl_ver_major : 0x%02X\n"
  12023. "bl_ver_minor : 0x%02X\n"
  12024. "jtag_id_h : 0x%04X\n"
  12025. "jtag_id_l : 0x%04X\n"
  12026. "mfg_id[0] : 0x%02X\n"
  12027. "mfg_id[1] : 0x%02X\n"
  12028. "mfg_id[2] : 0x%02X\n"
  12029. "mfg_id[3] : 0x%02X\n"
  12030. "mfg_id[4] : 0x%02X\n"
  12031. "mfg_id[5] : 0x%02X\n"
  12032. "mfg_id[6] : 0x%02X\n"
  12033. "mfg_id[7] : 0x%02X\n"
  12034. "post_code : 0x%04X\n"
  12035. "electrodes_x : 0x%02X\n"
  12036. "electrodes_y : 0x%02X\n"
  12037. "len_x : 0x%04X\n"
  12038. "len_y : 0x%04X\n"
  12039. "res_x : 0x%04X\n"
  12040. "res_y : 0x%04X\n"
  12041. "max_z : 0x%04X\n"
  12042. "origin_x : 0x%02X\n"
  12043. "origin_y : 0x%02X\n"
  12044. "panel_id : 0x%02X\n"
  12045. "btn : 0x%02X\n"
  12046. "scan_mode : 0x%02X\n"
  12047. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12048. rc,
  12049. ttdata->pip_ver_major,
  12050. ttdata->pip_ver_minor,
  12051. ttdata->fw_pid,
  12052. ttdata->fw_ver_major,
  12053. ttdata->fw_ver_minor,
  12054. ttdata->revctrl,
  12055. ttdata->fw_ver_conf,
  12056. ttdata->bl_ver_major,
  12057. ttdata->bl_ver_minor,
  12058. ttdata->jtag_id_h,
  12059. ttdata->jtag_id_l,
  12060. ttdata->mfg_id[0],
  12061. ttdata->mfg_id[1],
  12062. ttdata->mfg_id[2],
  12063. ttdata->mfg_id[3],
  12064. ttdata->mfg_id[4],
  12065. ttdata->mfg_id[5],
  12066. ttdata->mfg_id[6],
  12067. ttdata->mfg_id[7],
  12068. ttdata->post_code,
  12069. scd->electrodes_x,
  12070. scd->electrodes_y,
  12071. scd->len_x,
  12072. scd->len_y,
  12073. scd->res_x,
  12074. scd->res_y,
  12075. scd->max_z,
  12076. scd->origin_x,
  12077. scd->origin_y,
  12078. scd->panel_id,
  12079. scd->btn,
  12080. scd->scan_mode,
  12081. scd->max_tch);
  12082. } else {
  12083. return scnprintf(buf, strlen(buf),
  12084. "Status: %d\n",
  12085. rc);
  12086. }
  12087. }
  12088. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12089. /*******************************************************************************
  12090. * FUNCTION: pt_hw_reset_show
  12091. *
  12092. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12093. * by toggling the XRES line and then calls the startup function to
  12094. * allow TTDL to re-enumerate the DUT.
  12095. * The printed value reflects the status of the full reset/enum.
  12096. *
  12097. * PARAMETERS:
  12098. * *dev - pointer to Device structure
  12099. * *attr - pointer to the device attribute structure
  12100. * *buf - pointer to buffer to print
  12101. ******************************************************************************/
  12102. static ssize_t pt_hw_reset_show(struct device *dev,
  12103. struct device_attribute *attr, char *buf)
  12104. {
  12105. struct pt_core_data *cd = dev_get_drvdata(dev);
  12106. int rc = 0;
  12107. int time = 0;
  12108. u8 reset_status = 0;
  12109. int t;
  12110. struct pt_hid_desc hid_desc;
  12111. memset(&hid_desc, 0, sizeof(hid_desc));
  12112. /* Only allow DUT reset if no active BL in progress */
  12113. mutex_lock(&cd->firmware_class_lock);
  12114. mutex_lock(&cd->system_lock);
  12115. cd->startup_state = STARTUP_NONE;
  12116. mutex_unlock(&(cd->system_lock));
  12117. pt_stop_wd_timer(cd);
  12118. /* ensure no left over exclusive access is still locked */
  12119. release_exclusive(cd, cd->dev);
  12120. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12121. if (rc) {
  12122. mutex_unlock(&cd->firmware_class_lock);
  12123. pt_debug(cd->dev, DL_ERROR,
  12124. "%s: HW reset failed rc = %d\n", __func__, rc);
  12125. goto exit_hw_reset;
  12126. }
  12127. reset_status |= 0x01 << 0;
  12128. if (cd->flashless_dut) {
  12129. mutex_unlock(&cd->firmware_class_lock);
  12130. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12131. msecs_to_jiffies(200));
  12132. if (IS_TMO(t)) {
  12133. pt_debug(dev, DL_ERROR,
  12134. "%s: Timeout waiting for FW update",
  12135. __func__);
  12136. rc = -ETIME;
  12137. goto exit_hw_reset;
  12138. } else {
  12139. pt_debug(dev, DL_INFO,
  12140. "%s: ----- Wait FW Loading ----",
  12141. __func__);
  12142. rc = _pt_request_wait_for_enum_state(
  12143. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12144. if (rc) {
  12145. pt_debug(cd->dev, DL_ERROR,
  12146. "%s: No FW Sentinel detected rc = %d\n",
  12147. __func__, rc);
  12148. goto exit_hw_reset;
  12149. }
  12150. reset_status |= 0x01 << 1;
  12151. }
  12152. } else {
  12153. /* Wait for any sentinel */
  12154. rc = _pt_request_wait_for_enum_state(dev, 150,
  12155. STARTUP_STATUS_BL_RESET_SENTINEL |
  12156. STARTUP_STATUS_FW_RESET_SENTINEL);
  12157. if (rc) {
  12158. mutex_unlock(&cd->firmware_class_lock);
  12159. pt_debug(cd->dev, DL_ERROR,
  12160. "%s: No Sentinel detected rc = %d\n",
  12161. __func__, rc);
  12162. goto exit_hw_reset;
  12163. }
  12164. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12165. msleep(30);
  12166. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12167. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12168. if (rc < 0) {
  12169. pt_debug(cd->dev, DL_ERROR,
  12170. "%s: Error on getting HID descriptor r=%d\n",
  12171. __func__, rc);
  12172. goto exit_hw_reset;
  12173. }
  12174. cd->mode = pt_get_mode(cd, &hid_desc);
  12175. if (cd->mode == PT_MODE_BOOTLOADER)
  12176. rc = pt_hid_output_bl_launch_app_(cd);
  12177. } else {
  12178. if (cd->mode == PT_MODE_BOOTLOADER)
  12179. rc = pt_pip2_launch_app(dev,
  12180. PT_CORE_CMD_UNPROTECTED);
  12181. }
  12182. if (rc) {
  12183. mutex_unlock(&cd->firmware_class_lock);
  12184. pt_debug(cd->dev, DL_ERROR,
  12185. "%s: PIP launch app failed rc = %d\n",
  12186. __func__, rc);
  12187. goto exit_hw_reset;
  12188. }
  12189. mutex_unlock(&cd->firmware_class_lock);
  12190. reset_status |= 0x01 << 1;
  12191. msleep(20);
  12192. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12193. (cd->mode != PT_MODE_OPERATIONAL))
  12194. pt_queue_restart(cd);
  12195. else
  12196. pt_queue_enum(cd);
  12197. }
  12198. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12199. msleep(50);
  12200. pt_debug(cd->dev, DL_INFO,
  12201. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12202. __func__, time, STARTUP_STATUS_COMPLETE,
  12203. cd->startup_status);
  12204. time += 50;
  12205. }
  12206. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12207. rc = -ETIME;
  12208. goto exit_hw_reset;
  12209. }
  12210. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12211. __func__, cd->startup_status);
  12212. reset_status |= 0x01 << 2;
  12213. pt_start_wd_timer(cd);
  12214. exit_hw_reset:
  12215. return scnprintf(buf, strlen(buf),
  12216. "Status: %d\n"
  12217. "Reset Status: 0x%02X\n", rc, reset_status);
  12218. }
  12219. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12220. /*******************************************************************************
  12221. * FUNCTION: pt_pip2_cmd_rsp_store
  12222. *
  12223. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12224. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12225. * Command byte order:
  12226. * Byte [0] - PIP2 command ID
  12227. * Byte [1-n] - PIP2 command payload
  12228. *
  12229. * RETURN: Size of passed in buffer
  12230. *
  12231. * PARAMETERS:
  12232. * *dev - pointer to device structure
  12233. * *attr - pointer to device attributes
  12234. * *buf - pointer to buffer that hold the command parameters
  12235. * size - size of buf
  12236. ******************************************************************************/
  12237. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12238. struct device_attribute *attr, const char *buf, size_t size)
  12239. {
  12240. struct pt_core_data *cd = dev_get_drvdata(dev);
  12241. u16 actual_read_len;
  12242. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12243. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12244. u8 pip2_cmd_id = 0x00;
  12245. u8 *pip2_cmd_data = NULL;
  12246. int data_len = 0;
  12247. int length;
  12248. int rc = 0;
  12249. /* clear shared data */
  12250. mutex_lock(&cd->sysfs_lock);
  12251. cd->raw_cmd_status = 0;
  12252. cd->cmd_rsp_buf_len = 0;
  12253. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12254. mutex_unlock(&cd->sysfs_lock);
  12255. length = _pt_ic_parse_input_hex(dev, buf, size,
  12256. input_data, PT_MAX_PIP2_MSG_SIZE);
  12257. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12258. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12259. __func__);
  12260. rc = -EINVAL;
  12261. goto exit;
  12262. }
  12263. /* Send PIP2 command if enough data was provided */
  12264. if (length >= 1) {
  12265. pip2_cmd_id = input_data[0];
  12266. pip2_cmd_data = &input_data[1];
  12267. data_len = length - 1;
  12268. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12269. pip2_cmd_id, pip2_cmd_data, data_len,
  12270. read_buf, &actual_read_len);
  12271. cd->raw_cmd_status = rc;
  12272. if (rc) {
  12273. pt_debug(dev, DL_ERROR,
  12274. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12275. __func__, pip2_cmd_id, rc);
  12276. goto exit;
  12277. } else {
  12278. cd->cmd_rsp_buf_len = actual_read_len;
  12279. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12280. pt_debug(dev, DL_ERROR,
  12281. "%s: PIP2 actual_read_len = %d\n",
  12282. __func__, actual_read_len);
  12283. }
  12284. } else {
  12285. rc = -EINVAL;
  12286. pt_debug(dev, DL_ERROR,
  12287. "%s: Insufficient data provided for PIP2 cmd\n",
  12288. __func__);
  12289. }
  12290. exit:
  12291. if (rc)
  12292. return rc;
  12293. return size;
  12294. }
  12295. /*******************************************************************************
  12296. * FUNCTION: pt_pip2_cmd_rsp_show
  12297. *
  12298. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12299. * response generated after using the store method of the pip2_cmd_rsp
  12300. * sysfs node, are available to be read here.
  12301. *
  12302. * PARAMETERS:
  12303. * *dev - pointer to Device structure
  12304. * *attr - pointer to the device attribute structure
  12305. * *buf - pointer to buffer to print
  12306. ******************************************************************************/
  12307. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12308. struct device_attribute *attr, char *buf)
  12309. {
  12310. struct pt_core_data *cd = dev_get_drvdata(dev);
  12311. int i;
  12312. ssize_t data_len;
  12313. int index;
  12314. mutex_lock(&cd->sysfs_lock);
  12315. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12316. "Status: %d\n", cd->raw_cmd_status);
  12317. if (cd->raw_cmd_status)
  12318. goto error;
  12319. /* Remove the CRC from the length of the response */
  12320. data_len = cd->cmd_rsp_buf_len - 2;
  12321. /* Start printing from the data payload */
  12322. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12323. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12324. "%02X ", cd->cmd_rsp_buf[i]);
  12325. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12326. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12327. "\n(%zd bytes)\n",
  12328. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12329. } else {
  12330. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12331. "\n(%zd bytes)\n", 0);
  12332. }
  12333. error:
  12334. mutex_unlock(&cd->sysfs_lock);
  12335. return index;
  12336. }
  12337. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12338. pt_pip2_cmd_rsp_store);
  12339. /*******************************************************************************
  12340. * FUNCTION: pt_command_store
  12341. *
  12342. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12343. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12344. * TTDL will not parse the command.
  12345. *
  12346. * RETURN: Size of passed in buffer
  12347. *
  12348. * PARAMETERS:
  12349. * *dev - pointer to device structure
  12350. * *attr - pointer to device attributes
  12351. * *buf - pointer to buffer that hold the command parameters
  12352. * size - size of buf
  12353. ******************************************************************************/
  12354. static ssize_t pt_command_store(struct device *dev,
  12355. struct device_attribute *attr, const char *buf, size_t size)
  12356. {
  12357. struct pt_core_data *cd = dev_get_drvdata(dev);
  12358. unsigned short crc;
  12359. u16 actual_read_len;
  12360. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12361. int length;
  12362. int len_field;
  12363. int rc = 0;
  12364. mutex_lock(&cd->sysfs_lock);
  12365. cd->cmd_rsp_buf_len = 0;
  12366. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12367. mutex_unlock(&cd->sysfs_lock);
  12368. length = _pt_ic_parse_input_hex(dev, buf, size,
  12369. input_data, PT_MAX_PIP2_MSG_SIZE);
  12370. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12371. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12372. __func__);
  12373. rc = -EINVAL;
  12374. goto pt_command_store_exit;
  12375. }
  12376. /* PIP2 messages begin with 01 01 */
  12377. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12378. cd->pip2_prot_active = 1;
  12379. /* Override next seq tag with what was sent */
  12380. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12381. /* For PIP2 cmd if length does not include crc, add it */
  12382. len_field = (input_data[3] << 8) | input_data[2];
  12383. if (len_field == length && length <= 254) {
  12384. crc = crc_ccitt_calculate(&input_data[2],
  12385. length - 2);
  12386. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12387. __func__, length, crc);
  12388. input_data[length] = (crc & 0xFF00) >> 8;
  12389. input_data[length + 1] = crc & 0x00FF;
  12390. length = length + 2;
  12391. }
  12392. }
  12393. /* write PIP command to log */
  12394. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12395. pm_runtime_get_sync(dev);
  12396. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12397. length, input_data, &actual_read_len);
  12398. pm_runtime_put(dev);
  12399. mutex_lock(&cd->sysfs_lock);
  12400. cd->raw_cmd_status = rc;
  12401. if (rc) {
  12402. cd->cmd_rsp_buf_len = 0;
  12403. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12404. __func__, buf);
  12405. } else {
  12406. cd->cmd_rsp_buf_len = actual_read_len;
  12407. }
  12408. cd->pip2_prot_active = 0;
  12409. mutex_unlock(&cd->sysfs_lock);
  12410. pt_command_store_exit:
  12411. if (rc)
  12412. return rc;
  12413. return size;
  12414. }
  12415. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12416. /*******************************************************************************
  12417. * FUNCTION: pt_response_show
  12418. *
  12419. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12420. * response generated after using the pt_command_store sysfs node, are
  12421. * available to be read here.
  12422. *
  12423. * PARAMETERS:
  12424. * *dev - pointer to Device structure
  12425. * *attr - pointer to the device attribute structure
  12426. * *buf - pointer to buffer to print
  12427. ******************************************************************************/
  12428. static ssize_t pt_response_show(struct device *dev,
  12429. struct device_attribute *attr, char *buf)
  12430. {
  12431. struct pt_core_data *cd = dev_get_drvdata(dev);
  12432. int i;
  12433. ssize_t num_read;
  12434. int index;
  12435. mutex_lock(&cd->sysfs_lock);
  12436. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12437. "Status: %d\n", cd->raw_cmd_status);
  12438. if (cd->raw_cmd_status)
  12439. goto error;
  12440. num_read = cd->cmd_rsp_buf_len;
  12441. for (i = 0; i < num_read; i++)
  12442. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12443. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12444. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12445. "(%zd bytes)\n", num_read);
  12446. error:
  12447. mutex_unlock(&cd->sysfs_lock);
  12448. return index;
  12449. }
  12450. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12451. /*******************************************************************************
  12452. * FUNCTION: pt_dut_debug_show
  12453. *
  12454. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12455. * are available for the store method.
  12456. *
  12457. * RETURN:
  12458. * 0 = success
  12459. * !0 = failure
  12460. *
  12461. * PARAMETERS:
  12462. * *dev - pointer to device structure
  12463. * *attr - pointer to device attributes
  12464. * *buf - pointer to output buffer
  12465. ******************************************************************************/
  12466. static ssize_t pt_dut_debug_show(struct device *dev,
  12467. struct device_attribute *attr, char *buf)
  12468. {
  12469. ssize_t ret;
  12470. ret = scnprintf(buf, strlen(buf),
  12471. "Status: 0\n"
  12472. "dut_debug sends the following commands to the DUT:\n"
  12473. "%d %s \t- %s\n"
  12474. "%d %s \t- %s\n"
  12475. "%d %s \t- %s\n"
  12476. "%d %s \t- %s\n"
  12477. "%d %s \t- %s\n"
  12478. "%d %s \t- %s\n"
  12479. "%d %s \t- %s\n"
  12480. "%d %s \t- %s\n"
  12481. "%d %s \t- %s\n"
  12482. "%d %s \t- %s\n"
  12483. "%d %s \t- %s\n"
  12484. "%d %s \t- %s\n"
  12485. "%d %s \t- %s\n"
  12486. "%d %s \t- %s\n"
  12487. "%d %s \t- %s\n"
  12488. "%d %s \t- %s\n"
  12489. "%d %s \t- %s\n"
  12490. ,
  12491. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12492. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12493. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12494. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12495. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12496. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12497. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12498. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12499. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12500. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12501. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12502. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12503. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12504. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12505. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12506. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12507. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12508. );
  12509. return ret;
  12510. }
  12511. /*******************************************************************************
  12512. * FUNCTION: pt_drv_debug_show
  12513. *
  12514. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12515. * are available for the store method.
  12516. *
  12517. * RETURN:
  12518. * 0 = success
  12519. * !0 = failure
  12520. *
  12521. * PARAMETERS:
  12522. * *dev - pointer to device structure
  12523. * *attr - pointer to device attributes
  12524. * *buf - pointer to output buffer
  12525. ******************************************************************************/
  12526. static ssize_t pt_drv_debug_show(struct device *dev,
  12527. struct device_attribute *attr, char *buf)
  12528. {
  12529. ssize_t ret;
  12530. ret = scnprintf(buf, strlen(buf),
  12531. "Status: 0\n"
  12532. "drv_debug supports the following values:\n"
  12533. "%d %s \t- %s\n"
  12534. "%d %s \t- %s\n"
  12535. "%d %s \t- %s\n"
  12536. "%d %s \t- %s\n"
  12537. "%d %s \t- %s\n"
  12538. "%d %s \t- %s\n"
  12539. "%d %s \t- %s\n"
  12540. "%d %s \t- %s\n"
  12541. "%d %s \t- %s\n"
  12542. "%d %s - %s\n"
  12543. "%d %s \t- %s\n"
  12544. "%d %s \t- %s\n"
  12545. #ifdef TTDL_DIAGNOSTICS
  12546. "%d %s \t- %s\n"
  12547. "%d %s \t- %s\n"
  12548. "%d %s \t- %s\n"
  12549. #endif /* TTDL_DIAGNOSTICS */
  12550. "%d %s \t- %s\n"
  12551. #ifdef TTDL_DIAGNOSTICS
  12552. "%d %s \t- %s\n"
  12553. "%d %s \t- %s\n"
  12554. "%d %s \t- %s\n"
  12555. "%d %s \t- %s\n"
  12556. "%d %s \t- %s\n"
  12557. "%d %s \t- %s\n"
  12558. "%d %s \t- %s\n"
  12559. "%d %s \t- %s\n"
  12560. "%d %s \t- %s\n"
  12561. #endif /* TTDL_DIAGNOSTICS */
  12562. ,
  12563. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12564. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12565. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12566. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12567. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12568. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12569. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12570. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12571. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12572. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12573. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12574. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12575. #ifdef TTDL_DIAGNOSTICS
  12576. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12577. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12578. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12579. #endif /* TTDL_DIAGNOSTICS */
  12580. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12581. #ifdef TTDL_DIAGNOSTICS
  12582. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12583. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12584. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12585. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12586. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12587. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12588. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12589. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12590. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12591. "TTHE_TUNER HID USB Format"
  12592. #endif /* TTDL_DIAGNOSTICS */
  12593. );
  12594. return ret;
  12595. }
  12596. /*******************************************************************************
  12597. * FUNCTION: pt_drv_debug_store
  12598. *
  12599. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12600. * dut_debug. Drv_debug will contain all functionality that can be run
  12601. * without a DUT preset and is available anytime TTDL is running.
  12602. * Dut_debug requires a DUT to be available and will only be created after
  12603. * a DUT has been detected.
  12604. * This function will eventually be split into two but until the overlap
  12605. * has been depricated this function contains all commands that can be
  12606. * used for TTDL/DUT debugging status and control.
  12607. * All commands require at least one value to be passed in *buf with some
  12608. * requiring two.
  12609. *
  12610. * RETURN: Size of passed in buffer
  12611. *
  12612. * PARAMETERS:
  12613. * *dev - pointer to device structure
  12614. * *attr - pointer to device attributes
  12615. * *buf - pointer to buffer that hold the command parameters
  12616. * size - size of buf
  12617. ******************************************************************************/
  12618. static ssize_t pt_drv_debug_store(struct device *dev,
  12619. struct device_attribute *attr, const char *buf, size_t size)
  12620. {
  12621. struct pt_core_data *cd = dev_get_drvdata(dev);
  12622. unsigned long value;
  12623. int rc = 0;
  12624. u8 return_data[8];
  12625. static u8 wd_disabled;
  12626. u32 input_data[3];
  12627. int length;
  12628. #ifdef TTDL_DIAGNOSTICS
  12629. struct i2c_client *client = to_i2c_client(dev);
  12630. unsigned short crc = 0;
  12631. u16 cal_size;
  12632. #endif
  12633. input_data[0] = 0;
  12634. input_data[1] = 0;
  12635. /* Maximmum input is two elements */
  12636. length = _pt_ic_parse_input(dev, buf, size,
  12637. input_data, ARRAY_SIZE(input_data));
  12638. if (length < 1 || length > 2) {
  12639. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12640. __func__);
  12641. rc = -EINVAL;
  12642. goto pt_drv_debug_store_exit;
  12643. }
  12644. value = input_data[0];
  12645. if (length == 1) {
  12646. pt_debug(dev, DL_DEBUG,
  12647. "%s: Debug Cmd Received (id=%d)\n",
  12648. __func__, input_data[0]);
  12649. } else if (length == 2) {
  12650. pt_debug(dev, DL_DEBUG,
  12651. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12652. __func__, input_data[0], input_data[1]);
  12653. } else {
  12654. pt_debug(dev, DL_DEBUG,
  12655. "%s: Invalid arguments received\n", __func__);
  12656. rc = -EINVAL;
  12657. goto pt_drv_debug_store_exit;
  12658. }
  12659. /* Start watchdog timer command */
  12660. if (value == PT_DRV_DBG_START_WD) {
  12661. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12662. wd_disabled = 0;
  12663. cd->watchdog_force_stop = false;
  12664. pt_start_wd_timer(cd);
  12665. goto pt_drv_debug_store_exit;
  12666. }
  12667. /* Stop watchdog timer temporarily */
  12668. pt_stop_wd_timer(cd);
  12669. if (value == PT_DRV_DBG_STOP_WD) {
  12670. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12671. wd_disabled = 1;
  12672. cd->watchdog_force_stop = true;
  12673. goto pt_drv_debug_store_exit;
  12674. }
  12675. switch (value) {
  12676. case PT_DRV_DBG_SUSPEND: /* 4 */
  12677. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12678. wd_disabled = 1;
  12679. rc = pt_core_suspend_(cd->dev);
  12680. if (rc)
  12681. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12682. __func__, rc);
  12683. else {
  12684. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12685. __func__);
  12686. cd->drv_debug_suspend = true;
  12687. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12688. }
  12689. break;
  12690. case PT_DRV_DBG_RESUME: /* 5 */
  12691. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12692. rc = pt_core_resume_(cd->dev);
  12693. if (rc)
  12694. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12695. __func__, rc);
  12696. else {
  12697. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12698. __func__);
  12699. cd->drv_debug_suspend = false;
  12700. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12701. }
  12702. break;
  12703. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12704. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12705. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12706. break;
  12707. case PT_DUT_DBG_HID_RESET: /* 50 */
  12708. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12709. pt_hid_cmd_reset(cd);
  12710. break;
  12711. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12712. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12713. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12714. wd_disabled = 0;
  12715. break;
  12716. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12717. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12718. __func__);
  12719. wd_disabled = 1;
  12720. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12721. break;
  12722. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12723. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12724. __func__);
  12725. wd_disabled = 1;
  12726. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12727. break;
  12728. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12729. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12730. pt_hid_output_bl_get_information(cd, return_data);
  12731. break;
  12732. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12733. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12734. __func__);
  12735. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12736. break;
  12737. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12738. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12739. pt_hid_output_bl_launch_app(cd);
  12740. break;
  12741. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12742. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12743. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12744. break;
  12745. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12746. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12747. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12748. break;
  12749. case PT_DUT_DBG_RESET: /* 98 */
  12750. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12751. rc = pt_hw_hard_reset(cd);
  12752. break;
  12753. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12754. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12755. pt_pip_null(cd);
  12756. break;
  12757. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12758. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12759. rc = pt_pip_start_bootloader(cd);
  12760. if (!rc) {
  12761. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12762. cd->mode = PT_MODE_BOOTLOADER;
  12763. }
  12764. break;
  12765. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12766. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12767. pt_hid_output_get_sysinfo(cd);
  12768. break;
  12769. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12770. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12771. pt_pip_suspend_scanning(cd);
  12772. break;
  12773. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12774. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12775. pt_pip_resume_scanning(cd);
  12776. break;
  12777. #ifdef TTHE_TUNER_SUPPORT
  12778. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12779. cd->tthe_exit = 1;
  12780. wake_up(&cd->wait_q);
  12781. kfree(cd->tthe_buf);
  12782. cd->tthe_buf = NULL;
  12783. cd->tthe_exit = 0;
  12784. break;
  12785. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12786. if (cd->tthe_buf)
  12787. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12788. else
  12789. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12790. __func__);
  12791. break;
  12792. #endif
  12793. #ifdef TTDL_DIAGNOSTICS
  12794. case PT_DUT_DBG_HID_DESC: /* 109 */
  12795. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12796. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12797. break;
  12798. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12799. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12800. __func__);
  12801. pt_erase_parameter_list(cd);
  12802. break;
  12803. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12804. rc = pt_read_input(cd);
  12805. if (!rc)
  12806. pt_parse_input(cd);
  12807. break;
  12808. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12809. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12810. &cal_size, &crc);
  12811. if (rc)
  12812. pt_debug(dev, DL_ERROR,
  12813. "%s: CAL Data clear failed rc=%d\n",
  12814. __func__, rc);
  12815. else
  12816. pt_debug(dev, DL_INFO,
  12817. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12818. __func__, crc, size);
  12819. break;
  12820. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12821. mutex_lock(&cd->system_lock);
  12822. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12823. cd->debug_level = input_data[1];
  12824. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12825. __func__, cd->debug_level);
  12826. } else {
  12827. rc = -EINVAL;
  12828. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12829. __func__, input_data[1]);
  12830. }
  12831. mutex_unlock(&(cd->system_lock));
  12832. break;
  12833. #endif
  12834. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12835. mutex_lock(&cd->system_lock);
  12836. if (input_data[1] > 100) {
  12837. cd->watchdog_interval = input_data[1];
  12838. pt_debug(dev, DL_INFO,
  12839. "%s: Set watchdog_ interval to: %d\n",
  12840. __func__, cd->watchdog_interval);
  12841. pt_start_wd_timer(cd);
  12842. } else {
  12843. rc = -EINVAL;
  12844. pt_debug(dev, DL_ERROR,
  12845. "%s: Invalid watchdog interval: %d\n",
  12846. __func__, input_data[1]);
  12847. }
  12848. mutex_unlock(&(cd->system_lock));
  12849. break;
  12850. #ifdef TTDL_DIAGNOSTICS
  12851. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12852. mutex_lock(&cd->system_lock);
  12853. if (input_data[1] == 0) {
  12854. cd->show_timestamp = 0;
  12855. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12856. __func__);
  12857. } else if (input_data[1] == 1) {
  12858. cd->show_timestamp = 1;
  12859. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12860. __func__);
  12861. } else {
  12862. rc = -EINVAL;
  12863. pt_debug(dev, DL_ERROR,
  12864. "%s: Invalid parameter: %d\n",
  12865. __func__, input_data[1]);
  12866. }
  12867. mutex_unlock(&(cd->system_lock));
  12868. break;
  12869. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12870. if (input_data[1] == 0) {
  12871. cd->cpdata->setup_power(cd->cpdata,
  12872. PT_MT_POWER_OFF, cd->dev);
  12873. pt_debug(dev, DL_INFO,
  12874. "%s: Initiate Power Off\n", __func__);
  12875. } else if (input_data[1] == 1) {
  12876. cd->cpdata->setup_power(cd->cpdata,
  12877. PT_MT_POWER_ON, cd->dev);
  12878. pt_debug(dev, DL_INFO,
  12879. "%s: Initiate Power On\n", __func__);
  12880. } else {
  12881. rc = -EINVAL;
  12882. pt_debug(dev, DL_ERROR,
  12883. "%s: Invalid parameter: %d\n",
  12884. __func__, input_data[1]);
  12885. }
  12886. break;
  12887. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12888. if (input_data[1] == 0) {
  12889. pm_runtime_put(dev);
  12890. pt_debug(dev, DL_ERROR,
  12891. "%s: Force call pm_runtime_put()\n", __func__);
  12892. } else if (input_data[1] == 1) {
  12893. pm_runtime_get_sync(dev);
  12894. pt_debug(dev, DL_ERROR,
  12895. "%s: Force call pm_runtime_get_sync()\n",
  12896. __func__);
  12897. } else {
  12898. rc = -EINVAL;
  12899. pt_debug(dev, DL_ERROR,
  12900. "%s: WARNING: Invalid parameter: %d\n",
  12901. __func__, input_data[1]);
  12902. }
  12903. break;
  12904. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12905. mutex_lock(&cd->system_lock);
  12906. if (input_data[1] == 0) {
  12907. cd->show_tt_data = false;
  12908. pt_debug(dev, DL_INFO,
  12909. "%s: Disable TT_DATA\n", __func__);
  12910. } else if (input_data[1] == 1) {
  12911. cd->show_tt_data = true;
  12912. pt_debug(dev, DL_INFO,
  12913. "%s: Enable TT_DATA\n", __func__);
  12914. } else {
  12915. rc = -EINVAL;
  12916. pt_debug(dev, DL_ERROR,
  12917. "%s: Invalid parameter: %d\n",
  12918. __func__, input_data[1]);
  12919. }
  12920. mutex_unlock(&(cd->system_lock));
  12921. break;
  12922. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12923. if (input_data[1] == cd->active_dut_generation) {
  12924. mutex_lock(&cd->system_lock);
  12925. cd->set_dut_generation = true;
  12926. mutex_unlock(&(cd->system_lock));
  12927. } else {
  12928. mutex_lock(&cd->system_lock);
  12929. if (input_data[1] == 0) {
  12930. cd->active_dut_generation = DUT_UNKNOWN;
  12931. cd->set_dut_generation = false;
  12932. } else if (input_data[1] == 1) {
  12933. cd->active_dut_generation = DUT_PIP1_ONLY;
  12934. cd->set_dut_generation = true;
  12935. } else if (input_data[1] == 2) {
  12936. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12937. cd->set_dut_generation = true;
  12938. } else {
  12939. rc = -EINVAL;
  12940. pt_debug(dev, DL_ERROR,
  12941. "%s: Invalid parameter: %d\n",
  12942. __func__, input_data[1]);
  12943. break;
  12944. }
  12945. cd->startup_status = STARTUP_STATUS_START;
  12946. pt_debug(cd->dev, DL_DEBUG,
  12947. "%s: Startup Status Reset\n", __func__);
  12948. mutex_unlock(&(cd->system_lock));
  12949. pt_debug(dev, DL_INFO,
  12950. "%s: Active DUT Generation Set to: %d\n",
  12951. __func__, cd->active_dut_generation);
  12952. /* Changing DUT generations full restart needed */
  12953. pt_queue_restart(cd);
  12954. }
  12955. break;
  12956. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12957. mutex_lock(&cd->system_lock);
  12958. if (input_data[1] == 0) {
  12959. cd->bridge_mode = false;
  12960. pt_debug(dev, DL_INFO,
  12961. "%s: Disable Bridge Mode\n", __func__);
  12962. } else if (input_data[1] == 1) {
  12963. cd->bridge_mode = true;
  12964. pt_debug(dev, DL_INFO,
  12965. "%s: Enable Bridge Mode\n", __func__);
  12966. } else {
  12967. rc = -EINVAL;
  12968. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12969. __func__, input_data[1]);
  12970. }
  12971. mutex_unlock(&(cd->system_lock));
  12972. break;
  12973. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12974. mutex_lock(&cd->system_lock);
  12975. /* Only a 7bit address is valid */
  12976. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12977. client->addr = input_data[1];
  12978. pt_debug(dev, DL_INFO,
  12979. "%s: Set I2C Address: 0x%2X\n",
  12980. __func__, client->addr);
  12981. } else {
  12982. rc = -EINVAL;
  12983. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12984. __func__, input_data[1]);
  12985. client->addr = 0x24;
  12986. }
  12987. mutex_unlock(&(cd->system_lock));
  12988. break;
  12989. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12990. mutex_lock(&cd->system_lock);
  12991. if (input_data[1] == 0) {
  12992. cd->flashless_dut = 0;
  12993. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12994. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12995. __func__);
  12996. } else if (input_data[1] == 1) {
  12997. cd->flashless_dut = 1;
  12998. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12999. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13000. __func__);
  13001. }
  13002. mutex_unlock(&(cd->system_lock));
  13003. break;
  13004. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13005. mutex_lock(&cd->system_lock);
  13006. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13007. cd->force_pip2_seq = input_data[1];
  13008. cd->pip2_cmd_tag_seq = input_data[1];
  13009. pt_debug(dev, DL_INFO,
  13010. "%s: Force PIP2 Seq to: 0x%02X\n",
  13011. __func__, input_data[1]);
  13012. } else {
  13013. cd->force_pip2_seq = 0;
  13014. pt_debug(dev, DL_INFO,
  13015. "%s: Clear Forced PIP2 Seq\n", __func__);
  13016. }
  13017. mutex_unlock(&(cd->system_lock));
  13018. break;
  13019. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13020. mutex_lock(&cd->system_lock);
  13021. if (input_data[1] == 0) {
  13022. cd->bl_with_no_int = 0;
  13023. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13024. } else if (input_data[1] == 1) {
  13025. cd->bl_with_no_int = 1;
  13026. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13027. __func__);
  13028. } else {
  13029. rc = -EINVAL;
  13030. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13031. __func__, input_data[1]);
  13032. }
  13033. mutex_unlock(&(cd->system_lock));
  13034. break;
  13035. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13036. mutex_lock(&cd->system_lock);
  13037. if (input_data[1] == 0) {
  13038. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13039. pt_debug(dev, DL_INFO,
  13040. "%s: Disable Calibration cache in host\n",
  13041. __func__);
  13042. } else if (input_data[1] == 1) {
  13043. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13044. pt_debug(dev, DL_INFO,
  13045. "%s: Enable Calibration cache in host\n",
  13046. __func__);
  13047. } else {
  13048. rc = -EINVAL;
  13049. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13050. __func__, input_data[1]);
  13051. }
  13052. mutex_unlock(&(cd->system_lock));
  13053. break;
  13054. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13055. mutex_lock(&cd->system_lock);
  13056. if (input_data[1] == 0) {
  13057. cd->multi_chip = PT_FEATURE_DISABLE;
  13058. cd->ttdl_bist_select = 0x07;
  13059. pt_debug(dev, DL_INFO,
  13060. "%s: Disable Multi-chip support\n", __func__);
  13061. } else if (input_data[1] == 1) {
  13062. cd->multi_chip = PT_FEATURE_ENABLE;
  13063. cd->ttdl_bist_select = 0x3F;
  13064. pt_debug(dev, DL_INFO,
  13065. "%s: Enable Multi-chip support\n", __func__);
  13066. } else {
  13067. rc = -EINVAL;
  13068. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13069. __func__, input_data[1]);
  13070. }
  13071. mutex_unlock(&(cd->system_lock));
  13072. break;
  13073. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13074. mutex_lock(&cd->system_lock);
  13075. if (input_data[1] <= 0x07) {
  13076. cd->panel_id_support = input_data[1];
  13077. pt_debug(dev, DL_INFO,
  13078. "%s: Set panel_id_support to %d\n",
  13079. __func__, cd->panel_id_support);
  13080. } else {
  13081. rc = -EINVAL;
  13082. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13083. __func__, input_data[1]);
  13084. }
  13085. mutex_unlock(&(cd->system_lock));
  13086. break;
  13087. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13088. mutex_lock(&cd->system_lock);
  13089. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13090. /*
  13091. * The timeout is changed for some cases so the
  13092. * pip_cmd_timeout_default is used to retore back to
  13093. * what the user requested as the new timeout.
  13094. */
  13095. cd->pip_cmd_timeout_default = input_data[1];
  13096. cd->pip_cmd_timeout = input_data[1];
  13097. pt_debug(dev, DL_INFO,
  13098. "%s: PIP Timeout = %d\n", __func__,
  13099. cd->pip_cmd_timeout_default);
  13100. } else {
  13101. rc = -EINVAL;
  13102. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13103. __func__, input_data[1]);
  13104. }
  13105. mutex_unlock(&(cd->system_lock));
  13106. break;
  13107. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13108. mutex_lock(&cd->system_lock);
  13109. if (input_data[1] == 0) {
  13110. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13111. pt_debug(dev, DL_INFO,
  13112. "%s: Disable tthe_tuner HID-USB format\n",
  13113. __func__);
  13114. } else if (input_data[1] == 1) {
  13115. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13116. pt_debug(dev, DL_INFO,
  13117. "%s: Enable tthe_tuner HID-USB format\n",
  13118. __func__);
  13119. } else {
  13120. rc = -EINVAL;
  13121. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13122. __func__, input_data[1]);
  13123. }
  13124. mutex_unlock(&(cd->system_lock));
  13125. break;
  13126. #endif /* TTDL_DIAGNOSTICS */
  13127. default:
  13128. rc = -EINVAL;
  13129. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13130. }
  13131. /* Enable watchdog timer */
  13132. if (!wd_disabled)
  13133. pt_start_wd_timer(cd);
  13134. pt_drv_debug_store_exit:
  13135. if (rc)
  13136. return rc;
  13137. return size;
  13138. }
  13139. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13140. pt_drv_debug_store);
  13141. /*******************************************************************************
  13142. * FUNCTION: pt_sleep_status_show
  13143. *
  13144. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13145. * sleep status as either ON or OFF
  13146. *
  13147. * RETURN:
  13148. * 0 = success
  13149. * !0 = failure
  13150. *
  13151. * PARAMETERS:
  13152. * *dev - pointer to device structure
  13153. * *attr - pointer to device attributes
  13154. * *buf - pointer to output buffer
  13155. ******************************************************************************/
  13156. static ssize_t pt_sleep_status_show(struct device *dev,
  13157. struct device_attribute *attr, char *buf)
  13158. {
  13159. struct pt_core_data *cd = dev_get_drvdata(dev);
  13160. ssize_t ret;
  13161. mutex_lock(&cd->system_lock);
  13162. if (cd->sleep_state == SS_SLEEP_ON)
  13163. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13164. else
  13165. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13166. mutex_unlock(&cd->system_lock);
  13167. return ret;
  13168. }
  13169. /*******************************************************************************
  13170. * FUNCTION: pt_panel_id_show
  13171. *
  13172. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13173. * detected panel ID from the DUT
  13174. *
  13175. * RETURN:
  13176. * 0 = success
  13177. * !0 = failure
  13178. *
  13179. * PARAMETERS:
  13180. * *dev - pointer to device structure
  13181. * *attr - pointer to device attributes
  13182. * *buf - pointer to output buffer
  13183. ******************************************************************************/
  13184. static ssize_t pt_panel_id_show(struct device *dev,
  13185. struct device_attribute *attr, char *buf)
  13186. {
  13187. struct pt_core_data *cd = dev_get_drvdata(dev);
  13188. ssize_t ret;
  13189. u8 pid = PANEL_ID_NOT_ENABLED;
  13190. int rc = 0;
  13191. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13192. /*
  13193. * The DUT should report the same panel ID from both the BL and
  13194. * the FW unless the panel_id feature is set to only
  13195. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13196. * to retrieve the panel_id.
  13197. */
  13198. if (cd->mode == PT_MODE_BOOTLOADER) {
  13199. /*
  13200. * Return the stored value if PT_PANEL_ID_BY_BL
  13201. * is not supported while other feature exits.
  13202. */
  13203. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13204. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13205. if (rc) {
  13206. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13207. "Failed to retrieve Panel ID. ",
  13208. "Using cached value\n",
  13209. __func__);
  13210. }
  13211. }
  13212. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13213. if (cd->panel_id_support &
  13214. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13215. /* For all systems sysinfo has the panel_id */
  13216. rc = pt_hid_output_get_sysinfo(cd);
  13217. if (!rc)
  13218. pid =
  13219. cd->sysinfo.sensing_conf_data.panel_id;
  13220. pt_debug(dev, DL_ERROR,
  13221. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13222. __func__, rc, pid);
  13223. }
  13224. } else {
  13225. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13226. __func__);
  13227. rc = -EPERM;
  13228. }
  13229. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13230. if (cd->mode == PT_MODE_BOOTLOADER) {
  13231. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13232. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13233. if (rc) {
  13234. pt_debug(dev, DL_ERROR,
  13235. "%s: BL get panel ID failed rc=%d\n",
  13236. __func__, rc);
  13237. }
  13238. }
  13239. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13240. if (cd->panel_id_support &
  13241. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13242. rc = pt_hid_output_get_sysinfo(cd);
  13243. if (!rc)
  13244. pid =
  13245. cd->sysinfo.sensing_conf_data.panel_id;
  13246. pt_debug(dev, DL_ERROR,
  13247. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13248. __func__, rc, pid);
  13249. }
  13250. } else {
  13251. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13252. __func__);
  13253. rc = -EPERM;
  13254. }
  13255. } else {
  13256. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13257. __func__);
  13258. rc = -EPERM;
  13259. }
  13260. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13261. rc, pid);
  13262. return ret;
  13263. }
  13264. /*******************************************************************************
  13265. * FUNCTION: pt_get_param_store
  13266. *
  13267. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13268. * ID to retrieve with the show method.
  13269. *
  13270. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13271. *
  13272. * RETURN: Size of passed in buffer if successful
  13273. *
  13274. * PARAMETERS:
  13275. * *dev - pointer to device structure
  13276. * *attr - pointer to device attributes
  13277. * *buf - pointer to output buffer
  13278. * size - size of buf
  13279. ******************************************************************************/
  13280. static ssize_t pt_get_param_store(struct device *dev,
  13281. struct device_attribute *attr, const char *buf, size_t size)
  13282. {
  13283. struct pt_core_data *cd = dev_get_drvdata(dev);
  13284. u32 input_data[2];
  13285. int length;
  13286. int rc = 0;
  13287. /* Maximum input of one value */
  13288. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13289. ARRAY_SIZE(input_data));
  13290. if (length != 1) {
  13291. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13292. __func__);
  13293. rc = -EINVAL;
  13294. goto exit;
  13295. }
  13296. mutex_lock(&cd->system_lock);
  13297. cd->get_param_id = input_data[0];
  13298. mutex_unlock(&(cd->system_lock));
  13299. exit:
  13300. if (rc)
  13301. return rc;
  13302. return size;
  13303. }
  13304. /*******************************************************************************
  13305. * FUNCTION: pt_get_param_show
  13306. *
  13307. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13308. * parameter data from the DUT based on the ID stored in the core
  13309. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13310. * communicate or some other error occures, an error status is returned
  13311. * with no value following.
  13312. * Output is in the form:
  13313. * Status: x
  13314. * 0xyyyyyyyy
  13315. * The 32bit data will only follow the status code if the status == 0
  13316. *
  13317. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13318. *
  13319. * RETURN:
  13320. * 0 = success
  13321. * !0 = failure
  13322. *
  13323. * PARAMETERS:
  13324. * *dev - pointer to device structure
  13325. * *attr - pointer to device attributes
  13326. * *buf - pointer to output buffer
  13327. ******************************************************************************/
  13328. static ssize_t pt_get_param_show(struct device *dev,
  13329. struct device_attribute *attr, char *buf)
  13330. {
  13331. struct pt_core_data *cd = dev_get_drvdata(dev);
  13332. ssize_t ret = 0;
  13333. int status;
  13334. u32 value = 0;
  13335. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13336. if (status) {
  13337. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13338. __func__, "pt_get_param", status);
  13339. ret = scnprintf(buf, strlen(buf),
  13340. "%s %d\n",
  13341. "Status:", status);
  13342. } else {
  13343. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13344. __func__, cd->get_param_id, value);
  13345. ret = scnprintf(buf, strlen(buf),
  13346. "Status: %d\n"
  13347. "0x%04X\n",
  13348. status, value);
  13349. }
  13350. return ret;
  13351. }
  13352. /*******************************************************************************
  13353. * FUNCTION: pt_ttdl_restart_show
  13354. *
  13355. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13356. * probed modules, calls startup() and then re-probes modules.
  13357. *
  13358. * RETURN: size of data written to sysfs node
  13359. *
  13360. * PARAMETERS:
  13361. * *dev - pointer to device structure
  13362. * *attr - pointer to device attributes structure
  13363. * *buf - pointer to print output buffer
  13364. ******************************************************************************/
  13365. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13366. struct device_attribute *attr, char *buf)
  13367. {
  13368. struct pt_core_data *cd = dev_get_drvdata(dev);
  13369. int t;
  13370. int rc = 0;
  13371. mutex_lock(&cd->system_lock);
  13372. cd->startup_state = STARTUP_NONE;
  13373. mutex_unlock(&(cd->system_lock));
  13374. /* ensure no left over exclusive access is still locked */
  13375. release_exclusive(cd, cd->dev);
  13376. pt_queue_restart(cd);
  13377. t = wait_event_timeout(cd->wait_q,
  13378. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13379. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13380. if (IS_TMO(t)) {
  13381. pt_debug(cd->dev, DL_ERROR,
  13382. "%s: TMO waiting for FW sentinel\n", __func__);
  13383. rc = -ETIME;
  13384. }
  13385. return scnprintf(buf, strlen(buf),
  13386. "Status: %d\n"
  13387. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13388. }
  13389. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13390. /*******************************************************************************
  13391. * FUNCTION: pt_pip2_gpio_read_show
  13392. *
  13393. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13394. * contents of the response to the passed in output buffer.
  13395. *
  13396. * RETURN: size of data written to sysfs node
  13397. *
  13398. * PARAMETERS:
  13399. * *dev - pointer to device structure
  13400. * *attr - pointer to device attributes structure
  13401. * *buf - pointer to print output buffer
  13402. ******************************************************************************/
  13403. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13404. struct device_attribute *attr, char *buf)
  13405. {
  13406. struct pt_core_data *cd = dev_get_drvdata(dev);
  13407. u8 status = 0;
  13408. u32 gpio_value = 0;
  13409. int rc = 0;
  13410. /* This functionality is only available in the BL */
  13411. if (cd->mode == PT_MODE_BOOTLOADER)
  13412. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13413. else
  13414. rc = -EPERM;
  13415. if (!rc) {
  13416. if (status == 0)
  13417. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13418. "Status: %d\n"
  13419. "DUT GPIO Reg: 0x%08X\n",
  13420. rc, gpio_value);
  13421. else
  13422. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13423. "Status: %d\n"
  13424. "DUT GPIO Reg: n/a\n",
  13425. status);
  13426. } else
  13427. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13428. "Status: %d\n"
  13429. "DUT GPIO Reg: n/a\n",
  13430. rc);
  13431. }
  13432. /*******************************************************************************
  13433. * FUNCTION: pt_pip2_version_show
  13434. *
  13435. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13436. * contents of the response to the passed in output buffer.
  13437. *
  13438. * RETURN: size of data written to sysfs node
  13439. *
  13440. * PARAMETERS:
  13441. * *dev - pointer to device structure
  13442. * *attr - pointer to device attributes structure
  13443. * *buf - pointer to print output buffer
  13444. ******************************************************************************/
  13445. static ssize_t pt_pip2_version_show(struct device *dev,
  13446. struct device_attribute *attr, char *buf)
  13447. {
  13448. int rc = 0;
  13449. struct pt_core_data *cd = dev_get_drvdata(dev);
  13450. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13451. rc = pt_pip2_get_version(cd);
  13452. if (!rc) {
  13453. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13454. "PIP VERSION : %02X.%02X\n"
  13455. "BL VERSION : %02X.%02X\n"
  13456. "FW VERSION : %02X.%02X\n"
  13457. "SILICON ID : %04X.%04X\n"
  13458. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13459. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13460. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13461. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13462. ttdata->chip_id, ttdata->chip_rev,
  13463. ttdata->uid[0], ttdata->uid[1],
  13464. ttdata->uid[2], ttdata->uid[3],
  13465. ttdata->uid[4], ttdata->uid[5],
  13466. ttdata->uid[6], ttdata->uid[7],
  13467. ttdata->uid[8], ttdata->uid[9],
  13468. ttdata->uid[10], ttdata->uid[11]);
  13469. } else {
  13470. pt_debug(dev, DL_ERROR,
  13471. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13472. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13473. "PIP VERSION : -\n"
  13474. "BL VERSION : -\n"
  13475. "FW VERSION : -\n"
  13476. "SILICON ID : -\n"
  13477. "UID : -\n");
  13478. }
  13479. }
  13480. #ifdef TTDL_DIAGNOSTICS
  13481. /*******************************************************************************
  13482. * FUNCTION: pt_ttdl_status_show
  13483. *
  13484. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13485. * variable states and GPIO levels. Additional information printed when
  13486. * TTDL_DIAGNOSTICS is enabled.
  13487. *
  13488. * NOTE: All counters will be reset to 0 when this function is called.
  13489. *
  13490. * RETURN:
  13491. * 0 = success
  13492. * !0 = failure
  13493. *
  13494. * PARAMETERS:
  13495. * *dev - pointer to device structure
  13496. * *attr - pointer to device attributes
  13497. * *buf - pointer to output buffer
  13498. ******************************************************************************/
  13499. static ssize_t pt_ttdl_status_show(struct device *dev,
  13500. struct device_attribute *attr, char *buf)
  13501. {
  13502. struct pt_core_data *cd = dev_get_drvdata(dev);
  13503. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13504. struct i2c_client *client = to_i2c_client(dev);
  13505. ssize_t ret;
  13506. u16 cal_size = 0;
  13507. unsigned short crc = 0;
  13508. if (cd->cal_cache_in_host)
  13509. _pt_manage_local_cal_data(dev,
  13510. PT_CAL_DATA_INFO, &cal_size, &crc);
  13511. ret = scnprintf(buf, strlen(buf),
  13512. "%s: 0x%04X\n"
  13513. "%s: %d\n"
  13514. "%s: %s\n"
  13515. "%s: %s %s\n"
  13516. "%s: %s\n"
  13517. "%s: 0x%02X\n"
  13518. "%s: %s\n"
  13519. "%s: %s\n"
  13520. "%s: %s\n"
  13521. "%s: %s\n"
  13522. "%s: %d\n"
  13523. "%s: %d\n"
  13524. "%s: %s\n"
  13525. "%s: %s\n"
  13526. "%s: %d\n"
  13527. #ifdef TTDL_DIAGNOSTICS
  13528. "%s: %d\n"
  13529. "%s: %d\n"
  13530. "%s: %d\n"
  13531. "%s: %d\n"
  13532. "%s: %d\n"
  13533. "%s: %d\n"
  13534. "%s: %d\n"
  13535. "%s: %d\n"
  13536. "%s: %d\n"
  13537. "%s: %d\n"
  13538. "%s: %s\n"
  13539. "%s: %s\n"
  13540. "%s: %s\n"
  13541. "%s: %d\n"
  13542. "%s: 0x%04X\n"
  13543. "%s: %s\n"
  13544. #endif /* TTDL_DIAGNOSTICS */
  13545. ,
  13546. "Startup Status ", cd->startup_status,
  13547. "TTDL Debug Level ", cd->debug_level,
  13548. "Mode ",
  13549. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13550. "Operational" : "BL") : "Unknown",
  13551. "DUT Generation ",
  13552. cd->active_dut_generation ?
  13553. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13554. "PT TC/TT" : "Gen5/6") : "Unknown",
  13555. cd->active_dut_generation ?
  13556. (cd->set_dut_generation == true ?
  13557. "(Set)" : "(Detected)") : "",
  13558. "HW Detected ",
  13559. cd->hw_detected ? "True" : "False",
  13560. "I2C Address ",
  13561. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13562. "Active Bus Module ",
  13563. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13564. "Flashless Mode ",
  13565. cd->flashless_dut == 1 ? "Yes" : "No",
  13566. "GPIO state - IRQ ",
  13567. cd->cpdata->irq_stat ?
  13568. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13569. "High" : "Low") : "not defined",
  13570. "GPIO state - TP_XRES ",
  13571. pdata->core_pdata->rst_gpio ?
  13572. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13573. "High" : "Low") : "not defined",
  13574. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13575. "Startup Retry Count ", cd->startup_retry_count,
  13576. "WD - Manual Force Stop ",
  13577. cd->watchdog_force_stop ? "True" : "False",
  13578. "WD - Enabled ",
  13579. cd->watchdog_enabled ? "True" : "False",
  13580. "WD - Interval (ms) ", cd->watchdog_interval
  13581. #ifdef TTDL_DIAGNOSTICS
  13582. , "WD - Triggered Count ", cd->watchdog_count,
  13583. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13584. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13585. "WD - XRES Count ", cd->wd_xres_count,
  13586. "IRQ Triggered Count ", cd->irq_count,
  13587. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13588. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13589. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13590. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13591. "Error GPIO trigger type ", cd->err_gpio_type,
  13592. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13593. "Suppress No-Flash Auto BL ",
  13594. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13595. "Yes" : "No",
  13596. "Calibration Cache on host ",
  13597. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13598. "Yes" : "No",
  13599. "Calibration Cache size ", cal_size,
  13600. "Calibration Cache chip ID ", crc,
  13601. "Multi-Chip Support ",
  13602. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13603. #endif /* TTDL_DIAGNOSTICS */
  13604. );
  13605. #ifdef TTDL_DIAGNOSTICS
  13606. /* Reset all diagnostic counters */
  13607. cd->watchdog_count = 0;
  13608. cd->watchdog_irq_stuck_count = 0;
  13609. cd->watchdog_failed_access_count = 0;
  13610. cd->wd_xres_count = 0;
  13611. cd->irq_count = 0;
  13612. cd->bl_retry_packet_count = 0;
  13613. cd->pip2_crc_error_count = 0;
  13614. cd->bus_transmit_error_count = 0;
  13615. #endif
  13616. return ret;
  13617. }
  13618. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13619. /*******************************************************************************
  13620. * FUNCTION: pt_pip2_enter_bl_show
  13621. *
  13622. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13623. * the DUT into the BL and show the success or failure of entering the BL
  13624. *
  13625. * RETURN:
  13626. * 0 = success
  13627. * !0 = failure
  13628. *
  13629. * PARAMETERS:
  13630. * *dev - pointer to device structure
  13631. * *attr - pointer to device attributes
  13632. * *buf - pointer to output buffer
  13633. ******************************************************************************/
  13634. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13635. struct device_attribute *attr, char *buf)
  13636. {
  13637. ssize_t ret = 0;
  13638. int rc = 0;
  13639. int result = 0;
  13640. u8 mode = PT_MODE_UNKNOWN;
  13641. struct pt_core_data *cd = dev_get_drvdata(dev);
  13642. /* Turn off the TTDL WD before enter bootloader */
  13643. pt_stop_wd_timer(cd);
  13644. /* Ensure NO enumeration work is queued or will be queued */
  13645. cancel_work_sync(&cd->enum_work);
  13646. mutex_lock(&cd->system_lock);
  13647. cd->bridge_mode = true;
  13648. mutex_unlock(&cd->system_lock);
  13649. /* set mode to operational to avoid any extra PIP traffic */
  13650. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13651. switch (result) {
  13652. case PT_ENTER_BL_PASS:
  13653. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13654. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13655. break;
  13656. case PT_ENTER_BL_ERROR:
  13657. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13658. rc,
  13659. " Unknown Error");
  13660. break;
  13661. case PT_ENTER_BL_RESET_FAIL:
  13662. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13663. rc,
  13664. " Soft Reset Failed");
  13665. break;
  13666. case PT_ENTER_BL_HID_START_BL_FAIL:
  13667. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13668. rc,
  13669. " PIP Start BL Cmd Failed");
  13670. break;
  13671. case PT_ENTER_BL_CONFIRM_FAIL:
  13672. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13673. rc,
  13674. " Error confirming DUT entered BL");
  13675. break;
  13676. default:
  13677. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13678. rc, " Unknown Error");
  13679. break;
  13680. };
  13681. /* Allow enumeration work to be queued again */
  13682. cd->bridge_mode = false;
  13683. return ret;
  13684. }
  13685. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13686. /*******************************************************************************
  13687. * FUNCTION: pt_pip2_exit_bl_show
  13688. *
  13689. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13690. * launch the APP and put the DUT Application mode
  13691. *
  13692. * RETURN:
  13693. * 0 = success
  13694. * !0 = failure
  13695. *
  13696. * PARAMETERS:
  13697. * *dev - pointer to device structure
  13698. * *attr - pointer to device attributes
  13699. * *buf - pointer to output buffer
  13700. ******************************************************************************/
  13701. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13702. struct device_attribute *attr, char *buf)
  13703. {
  13704. struct pt_core_data *cd = dev_get_drvdata(dev);
  13705. ssize_t ret = 0;
  13706. int rc = 0;
  13707. u8 status_str[PT_STATUS_STR_LEN];
  13708. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13709. /*
  13710. * Perform enum if startup_status doesn't reach to
  13711. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13712. */
  13713. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13714. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13715. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13716. strlcpy(status_str,
  13717. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13718. }
  13719. }
  13720. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13721. rc, status_str);
  13722. return ret;
  13723. }
  13724. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13725. #endif
  13726. #ifdef EASYWAKE_TSG6
  13727. /*******************************************************************************
  13728. * FUNCTION: pt_easy_wakeup_gesture_show
  13729. *
  13730. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13731. * current easy wakeup gesture
  13732. *
  13733. * RETURN:
  13734. * 0 = success
  13735. * !0 = failure
  13736. *
  13737. * PARAMETERS:
  13738. * *dev - pointer to device structure
  13739. * *attr - pointer to device attributes
  13740. * *buf - pointer to output buffer
  13741. ******************************************************************************/
  13742. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13743. struct device_attribute *attr, char *buf)
  13744. {
  13745. struct pt_core_data *cd = dev_get_drvdata(dev);
  13746. ssize_t ret;
  13747. mutex_lock(&cd->system_lock);
  13748. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13749. cd->easy_wakeup_gesture);
  13750. mutex_unlock(&cd->system_lock);
  13751. return ret;
  13752. }
  13753. /*******************************************************************************
  13754. * FUNCTION: pt_easy_wakeup_gesture_store
  13755. *
  13756. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13757. * allows the wake gesture to be set to a custom value.
  13758. *
  13759. * RETURN: Size of passed in buffer is success
  13760. *
  13761. * PARAMETERS:
  13762. * *dev - pointer to device structure
  13763. * *attr - pointer to device attributes
  13764. * *buf - pointer to buffer that hold the command parameters
  13765. * size - size of buf
  13766. ******************************************************************************/
  13767. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13768. struct device_attribute *attr, const char *buf, size_t size)
  13769. {
  13770. struct pt_core_data *cd = dev_get_drvdata(dev);
  13771. u32 input_data[2];
  13772. int length;
  13773. int rc = 0;
  13774. /* Maximum input of one value */
  13775. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13776. ARRAY_SIZE(input_data));
  13777. if (length != 1) {
  13778. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13779. __func__);
  13780. rc = -EINVAL;
  13781. goto exit;
  13782. }
  13783. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13784. __func__, cd->features.easywake);
  13785. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13786. rc = -EINVAL;
  13787. goto exit;
  13788. }
  13789. pm_runtime_get_sync(dev);
  13790. mutex_lock(&cd->system_lock);
  13791. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13792. cd->easy_wakeup_gesture = (u8)input_data[0];
  13793. pt_debug(dev, DL_INFO,
  13794. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13795. __func__, cd->easy_wakeup_gesture);
  13796. } else
  13797. rc = -ENODEV;
  13798. mutex_unlock(&cd->system_lock);
  13799. pm_runtime_put(dev);
  13800. exit:
  13801. if (rc)
  13802. return rc;
  13803. return size;
  13804. }
  13805. /*******************************************************************************
  13806. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13807. *
  13808. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13809. * show the TSG6 easywake gesture ID
  13810. *
  13811. * RETURN:
  13812. * 0 = success
  13813. * !0 = failure
  13814. *
  13815. * PARAMETERS:
  13816. * *dev - pointer to device structure
  13817. * *attr - pointer to device attributes
  13818. * *buf - pointer to output buffer
  13819. ******************************************************************************/
  13820. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13821. struct device_attribute *attr, char *buf)
  13822. {
  13823. struct pt_core_data *cd = dev_get_drvdata(dev);
  13824. ssize_t ret;
  13825. mutex_lock(&cd->system_lock);
  13826. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13827. cd->gesture_id);
  13828. mutex_unlock(&cd->system_lock);
  13829. return ret;
  13830. }
  13831. /*******************************************************************************
  13832. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13833. *
  13834. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13835. * show the TSG6 easywake gesture data in the following format:
  13836. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13837. *
  13838. * RETURN:
  13839. * 0 = success
  13840. * !0 = failure
  13841. *
  13842. * PARAMETERS:
  13843. * *dev - pointer to device structure
  13844. * *attr - pointer to device attributes
  13845. * *buf - pointer to output buffer
  13846. ******************************************************************************/
  13847. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13848. struct device_attribute *attr, char *buf)
  13849. {
  13850. struct pt_core_data *cd = dev_get_drvdata(dev);
  13851. ssize_t ret = 0;
  13852. int i;
  13853. mutex_lock(&cd->system_lock);
  13854. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13855. for (i = 0; i < cd->gesture_data_length; i++)
  13856. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13857. "0x%02X\n", cd->gesture_data[i]);
  13858. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13859. "(%d bytes)\n", cd->gesture_data_length);
  13860. mutex_unlock(&cd->system_lock);
  13861. return ret;
  13862. }
  13863. #endif /* EASYWAKE_TSG6 */
  13864. #ifdef TTDL_DIAGNOSTICS
  13865. /*******************************************************************************
  13866. * FUNCTION: pt_err_gpio_show
  13867. *
  13868. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13869. * setting up the gpio was successful
  13870. *
  13871. * RETURN: Char buffer with printed GPIO creation state
  13872. *
  13873. * PARAMETERS:
  13874. * *dev - pointer to device structure
  13875. * *attr - pointer to device attributes
  13876. * *buf - pointer to output buffer
  13877. ******************************************************************************/
  13878. static ssize_t pt_err_gpio_show(struct device *dev,
  13879. struct device_attribute *attr, char *buf)
  13880. {
  13881. struct pt_core_data *cd = dev_get_drvdata(dev);
  13882. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13883. "Err GPIO (%d) : %s\n"
  13884. "Err GPIO trig type: %d\n",
  13885. cd->err_gpio,
  13886. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13887. "HIGH" : "low") : "not defined"),
  13888. cd->err_gpio_type);
  13889. }
  13890. /*******************************************************************************
  13891. * FUNCTION: pt_err_gpio_store
  13892. *
  13893. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13894. * available host GPIO to be used to trigger when TTDL detects a PIP
  13895. * command/response timeout.
  13896. *
  13897. * RETURN:
  13898. * 0 = success
  13899. * !0 = failure
  13900. *
  13901. * PARAMETERS:
  13902. * *dev - pointer to device structure
  13903. * *attr - pointer to device attributes
  13904. * *buf - pointer to buffer that hold the command parameters
  13905. * size - size of buf
  13906. ******************************************************************************/
  13907. static ssize_t pt_err_gpio_store(struct device *dev,
  13908. struct device_attribute *attr, const char *buf, size_t size)
  13909. {
  13910. struct pt_core_data *cd = dev_get_drvdata(dev);
  13911. unsigned long gpio;
  13912. int rc = 0;
  13913. u32 input_data[3];
  13914. int length;
  13915. u8 err_type;
  13916. input_data[0] = 0;
  13917. input_data[1] = 0;
  13918. /* Maximmum input is two elements */
  13919. length = _pt_ic_parse_input(dev, buf, size,
  13920. input_data, ARRAY_SIZE(input_data));
  13921. if (length < 1 || length > 2) {
  13922. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13923. __func__);
  13924. rc = -EINVAL;
  13925. goto exit;
  13926. }
  13927. gpio = input_data[0];
  13928. err_type = (u8)input_data[1];
  13929. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13930. rc = -EINVAL;
  13931. goto exit;
  13932. }
  13933. mutex_lock(&cd->system_lock);
  13934. gpio_free(gpio);
  13935. rc = gpio_request(gpio, NULL);
  13936. if (rc) {
  13937. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13938. rc = -ENODEV;
  13939. } else {
  13940. cd->err_gpio = gpio;
  13941. cd->err_gpio_type = err_type;
  13942. gpio_direction_output(gpio, 0);
  13943. }
  13944. mutex_unlock(&cd->system_lock);
  13945. exit:
  13946. if (rc)
  13947. return rc;
  13948. return size;
  13949. }
  13950. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13951. pt_err_gpio_store);
  13952. /*******************************************************************************
  13953. * FUNCTION: pt_drv_irq_show
  13954. *
  13955. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13956. * TTDL interrupt is enabled/disabled
  13957. *
  13958. * RETURN:
  13959. * 0 = success
  13960. * !0 = failure
  13961. *
  13962. * PARAMETERS:
  13963. * *dev - pointer to device structure
  13964. * *attr - pointer to device attributes
  13965. * *buf - pointer to output buffer
  13966. ******************************************************************************/
  13967. static ssize_t pt_drv_irq_show(struct device *dev,
  13968. struct device_attribute *attr, char *buf)
  13969. {
  13970. struct pt_core_data *cd = dev_get_drvdata(dev);
  13971. ssize_t ret = 0;
  13972. mutex_lock(&cd->system_lock);
  13973. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13974. if (cd->irq_enabled)
  13975. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13976. "Driver interrupt: ENABLED\n");
  13977. else
  13978. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13979. "Driver interrupt: DISABLED\n");
  13980. mutex_unlock(&cd->system_lock);
  13981. return ret;
  13982. }
  13983. /*******************************************************************************
  13984. * FUNCTION: pt_drv_irq_store
  13985. *
  13986. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13987. * IRQ to be enabled/disabled.
  13988. *
  13989. * RETURN: Size of passed in buffer
  13990. *
  13991. * PARAMETERS:
  13992. * *dev - pointer to device structure
  13993. * *attr - pointer to device attributes
  13994. * *buf - pointer to buffer that hold the command parameters
  13995. * size - size of buf
  13996. ******************************************************************************/
  13997. static ssize_t pt_drv_irq_store(struct device *dev,
  13998. struct device_attribute *attr, const char *buf, size_t size)
  13999. {
  14000. struct pt_core_data *cd = dev_get_drvdata(dev);
  14001. u32 input_data[2];
  14002. int length;
  14003. int rc = 0;
  14004. /* Maximum input of one value */
  14005. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14006. ARRAY_SIZE(input_data));
  14007. if (length != 1) {
  14008. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14009. __func__);
  14010. rc = -EINVAL;
  14011. goto exit;
  14012. }
  14013. mutex_lock(&cd->system_lock);
  14014. switch (input_data[0]) {
  14015. case 0:
  14016. if (cd->irq_enabled) {
  14017. cd->irq_enabled = false;
  14018. /* Disable IRQ has no return value to check */
  14019. disable_irq_nosync(cd->irq);
  14020. pt_debug(dev, DL_INFO,
  14021. "%s: Driver IRQ now disabled\n",
  14022. __func__);
  14023. } else
  14024. pt_debug(dev, DL_INFO,
  14025. "%s: Driver IRQ already disabled\n",
  14026. __func__);
  14027. break;
  14028. case 1:
  14029. if (cd->irq_enabled == false) {
  14030. cd->irq_enabled = true;
  14031. enable_irq(cd->irq);
  14032. pt_debug(dev, DL_INFO,
  14033. "%s: Driver IRQ now enabled\n",
  14034. __func__);
  14035. } else
  14036. pt_debug(dev, DL_INFO,
  14037. "%s: Driver IRQ already enabled\n",
  14038. __func__);
  14039. break;
  14040. default:
  14041. rc = -EINVAL;
  14042. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14043. }
  14044. mutex_unlock(&(cd->system_lock));
  14045. exit:
  14046. if (rc)
  14047. return rc;
  14048. return size;
  14049. }
  14050. /*******************************************************************************
  14051. * FUNCTION: pt_pip2_bin_hdr_show
  14052. *
  14053. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14054. * the bin file header from flash and show each field
  14055. *
  14056. * RETURN:
  14057. * 0 = success
  14058. * !0 = failure
  14059. *
  14060. * PARAMETERS:
  14061. * *dev - pointer to device structure
  14062. * *attr - pointer to device attributes
  14063. * *buf - pointer to output buffer
  14064. ******************************************************************************/
  14065. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14066. struct device_attribute *attr, char *buf)
  14067. {
  14068. ssize_t ret = 0;
  14069. struct pt_bin_file_hdr hdr = {0};
  14070. int rc;
  14071. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14072. ret = scnprintf(buf, strlen(buf),
  14073. "%s: %d\n"
  14074. "%s: %d\n"
  14075. "%s: 0x%04X\n"
  14076. "%s: %d\n"
  14077. "%s: %d\n"
  14078. "%s: %d\n"
  14079. "%s: 0x%08X\n"
  14080. "%s: 0x%04X\n"
  14081. "%s: 0x%04X\n"
  14082. "%s: %d\n"
  14083. "%s: %d\n",
  14084. "Status", rc,
  14085. "Header Length ", hdr.length,
  14086. "TTPID ", hdr.ttpid,
  14087. "FW Major Ver ", hdr.fw_major,
  14088. "FW Minor Ver ", hdr.fw_minor,
  14089. "FW Rev Control ", hdr.fw_rev_ctrl,
  14090. "FW CRC ", hdr.fw_crc,
  14091. "Silicon Rev ", hdr.si_rev,
  14092. "Silicon ID ", hdr.si_id,
  14093. "Config Ver ", hdr.config_ver,
  14094. "HEX File Size ", hdr.hex_file_size
  14095. );
  14096. return ret;
  14097. }
  14098. /*******************************************************************************
  14099. * FUNCTION: pt_platform_data_show
  14100. *
  14101. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14102. * active platform data including: GPIOs, Vendor and Product IDs,
  14103. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14104. * HID registers, and Easy wake gesture
  14105. *
  14106. * RETURN: Size of printed data
  14107. *
  14108. * PARAMETERS:
  14109. * *dev - pointer to device structure
  14110. * *attr - pointer to device attributes
  14111. * *buf - pointer to output buffer
  14112. ******************************************************************************/
  14113. static ssize_t pt_platform_data_show(struct device *dev,
  14114. struct device_attribute *attr, char *buf)
  14115. {
  14116. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14117. struct pt_core_data *cd = dev_get_drvdata(dev);
  14118. ssize_t ret;
  14119. ret = scnprintf(buf, strlen(buf),
  14120. "%s: %d\n"
  14121. "%s: %d\n"
  14122. "%s: %d\n"
  14123. "%s: %d\n"
  14124. "%s: %d\n"
  14125. "%s: %d\n"
  14126. "%s: %d\n"
  14127. "%s: %d\n"
  14128. "%s: %d\n"
  14129. "%s: %d\n"
  14130. "%s: %d\n"
  14131. "%s: %d\n"
  14132. "%s: %d\n"
  14133. "%s: %d\n"
  14134. "%s: %d\n"
  14135. "%s: %d\n"
  14136. "%s: %d\n"
  14137. "%s: %d\n"
  14138. "%s: %d\n"
  14139. "%s: %d\n"
  14140. "%s: %d\n"
  14141. "%s: %d\n"
  14142. "%s: %d\n"
  14143. "%s: %d\n"
  14144. "%s: %d\n"
  14145. "%s: %d\n"
  14146. "%s: %d\n"
  14147. "%s: %s\n"
  14148. "%s: %s\n"
  14149. "%s: %d\n",
  14150. "Status", 0,
  14151. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14152. "Interrupt GPIO Value ",
  14153. pdata->core_pdata->irq_gpio ?
  14154. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14155. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14156. "Reset GPIO Value ",
  14157. pdata->core_pdata->rst_gpio ?
  14158. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14159. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14160. "DDI Reset GPIO Value ",
  14161. pdata->core_pdata->ddi_rst_gpio ?
  14162. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14163. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14164. "VDDI GPIO Value ",
  14165. pdata->core_pdata->vddi_gpio ?
  14166. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14167. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14168. "VCC GPIO Value ",
  14169. pdata->core_pdata->vcc_gpio ?
  14170. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14171. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14172. "AVDD GPIO Value ",
  14173. pdata->core_pdata->avdd_gpio ?
  14174. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14175. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14176. "AVEE GPIO Value ",
  14177. pdata->core_pdata->avee_gpio ?
  14178. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14179. "Vendor ID ", pdata->core_pdata->vendor_id,
  14180. "Product ID ", pdata->core_pdata->product_id,
  14181. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14182. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14183. "Core data flags ", pdata->core_pdata->flags,
  14184. "MT data flags ", pdata->mt_pdata->flags,
  14185. "Loader data flags ", pdata->loader_pdata->flags,
  14186. "Level trigger delay (us) ",
  14187. pdata->core_pdata->level_irq_udelay,
  14188. "HID Descriptor Register ",
  14189. pdata->core_pdata->hid_desc_register,
  14190. "HID Output Register ",
  14191. cd->hid_desc.output_register,
  14192. "HID Command Register ",
  14193. cd->hid_desc.command_register,
  14194. "Easy wakeup gesture ",
  14195. pdata->core_pdata->easy_wakeup_gesture,
  14196. "Config DUT generation ",
  14197. pdata->core_pdata->config_dut_generation ?
  14198. (pdata->core_pdata->config_dut_generation ==
  14199. CONFIG_DUT_PIP2_CAPABLE ?
  14200. "PT TC/TT" : "Gen5/6") : "Auto",
  14201. "Watchdog Force Stop ",
  14202. pdata->core_pdata->watchdog_force_stop ?
  14203. "True" : "False",
  14204. "Panel ID Support ",
  14205. pdata->core_pdata->panel_id_support);
  14206. return ret;
  14207. }
  14208. #define PT_ERR_STR_SIZE 64
  14209. /*******************************************************************************
  14210. * FUNCTION: pt_bist_bus_test
  14211. *
  14212. * SUMMARY: Tests the connectivity of the active bus pins:
  14213. * I2C - SDA and SCL
  14214. * SPI - MOSI, MISO, CLK
  14215. *
  14216. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14217. * bus. If any data is read we know the I2C/SPI pins are connected
  14218. *
  14219. * RETURN:
  14220. * 0 = Success
  14221. * !0 = Failure
  14222. *
  14223. * PARAMETERS:
  14224. * *dev - pointer to device structure
  14225. * *net_toggled - pointer to where to store if bus toggled
  14226. * *err_str - pointer to error string buffer
  14227. ******************************************************************************/
  14228. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14229. {
  14230. struct pt_core_data *cd = dev_get_drvdata(dev);
  14231. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14232. u8 *read_buf = NULL;
  14233. int bytes_read = 0;
  14234. int rc = 0;
  14235. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14236. if (read_buf == NULL) {
  14237. rc = -ENOMEM;
  14238. goto exit;
  14239. }
  14240. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14241. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14242. disable_irq(cd->irq);
  14243. mutex_lock(&cd->system_lock);
  14244. cd->irq_disabled = true;
  14245. mutex_unlock(&cd->system_lock);
  14246. /*
  14247. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14248. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14249. */
  14250. usleep_range(5000, 6000);
  14251. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14252. __func__, (int)sizeof(ver_cmd));
  14253. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14254. ">>> User CMD");
  14255. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14256. if (rc) {
  14257. pt_debug(dev, DL_ERROR,
  14258. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14259. *net_toggled = 0;
  14260. strlcpy(err_str,
  14261. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14262. goto exit_enable_irq;
  14263. }
  14264. usleep_range(4000, 5000);
  14265. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14266. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14267. __func__, bytes_read);
  14268. if (bytes_read == 0) {
  14269. *net_toggled = 0;
  14270. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14271. __func__);
  14272. strlcpy(err_str,
  14273. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14274. rc = -EIO;
  14275. goto exit_enable_irq;
  14276. } else {
  14277. if (cd->bus_ops->bustype == BUS_I2C)
  14278. *net_toggled = 1;
  14279. else {
  14280. if ((bytes_read > 3) &&
  14281. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14282. PIP2_CMD_ID_VERSION)
  14283. *net_toggled = 1;
  14284. else {
  14285. *net_toggled = 0;
  14286. pt_debug(dev, DL_INFO,
  14287. "%s: BUS Read Failed, %d bytes read\n",
  14288. __func__, bytes_read);
  14289. strlcpy(err_str,
  14290. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14291. }
  14292. }
  14293. }
  14294. exit_enable_irq:
  14295. enable_irq(cd->irq);
  14296. usleep_range(5000, 6000);
  14297. mutex_lock(&cd->system_lock);
  14298. cd->irq_disabled = false;
  14299. mutex_unlock(&cd->system_lock);
  14300. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14301. exit:
  14302. kfree(read_buf);
  14303. return rc;
  14304. }
  14305. /*******************************************************************************
  14306. * FUNCTION: pt_bist_irq_test
  14307. *
  14308. * SUMMARY: Tests the connectivity of the IRQ net
  14309. *
  14310. * This test will ensure there is a good connection between the host
  14311. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14312. * asserted and if so keep reading messages off of the bus until
  14313. * it de-asserts. Possible outcomes:
  14314. * - IRQ was already de-asserted: Send a PIP command and if an
  14315. * interrupt is generated the test passes.
  14316. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14317. * test passes.
  14318. * - IRQ stays asserted: After reading the bus multiple times
  14319. * the IRQ stays asserted. Likely open or shorted to GND
  14320. *
  14321. * RETURN:
  14322. * 0 = Success
  14323. * !0 = Failure
  14324. *
  14325. * PARAMETERS:
  14326. * *dev - pointer to device structure
  14327. * *bus_toggled - pointer to where to store if bus toggled
  14328. * *irq_toggled - pointer to where to store if IRQ toggled
  14329. * *xres_toggled - pointer to where to store if XRES toggled
  14330. * *err_str - pointer to error string buffer
  14331. ******************************************************************************/
  14332. static int pt_bist_irq_test(struct device *dev,
  14333. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14334. {
  14335. struct pt_core_data *cd = dev_get_drvdata(dev);
  14336. u8 *read_buf = NULL;
  14337. u8 mode = PT_MODE_UNKNOWN;
  14338. u16 actual_read_len;
  14339. int bytes_read = 0;
  14340. int count = 0;
  14341. int rc = 0;
  14342. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14343. if (read_buf == NULL) {
  14344. rc = -ENOMEM;
  14345. goto exit;
  14346. }
  14347. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14348. cd->irq_count = 0;
  14349. /*
  14350. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14351. * the bus based on the 2 byte initial length read. Try up to 5x.
  14352. */
  14353. while (pt_check_irq_asserted(cd) && count < 5) {
  14354. count++;
  14355. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14356. }
  14357. if (count > 1 && count < 5 && bytes_read > 0) {
  14358. /*
  14359. * IRQ was stuck but data was successfully read from the
  14360. * bus releasing the IRQ line.
  14361. */
  14362. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14363. __func__, count, bytes_read);
  14364. *bus_toggled = 1;
  14365. *irq_toggled = 1;
  14366. goto exit;
  14367. }
  14368. if (count == 5 && bytes_read == 0) {
  14369. /*
  14370. * Looped 5x and read nothing off the bus yet the IRQ is still
  14371. * asserted. Possible conditions:
  14372. * - IRQ open circuit
  14373. * - IRQ shorted to GND
  14374. * - I2C/SPI bus is disconnected
  14375. * - FW holding the pin low
  14376. * Try entering the BL to see if communication works there.
  14377. */
  14378. mode = PT_MODE_IGNORE;
  14379. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14380. if (rc) {
  14381. pt_debug(dev, DL_ERROR,
  14382. "%s Failed to enter BL\n", __func__);
  14383. strlcpy(err_str,
  14384. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14385. *irq_toggled = 0;
  14386. goto exit;
  14387. }
  14388. /*
  14389. * If original mode was operational and we successfully
  14390. * entered the BL, then the XRES net must have toggled
  14391. */
  14392. if (mode == PT_MODE_OPERATIONAL)
  14393. *xres_toggled = 1;
  14394. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14395. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14396. &actual_read_len);
  14397. if (rc) {
  14398. /*
  14399. * Could not communicate to DUT in BL mode. Save the
  14400. * error string, slim chance but the XRES test below may
  14401. * show the IRQ is actually working.
  14402. */
  14403. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14404. pt_debug(dev, DL_ERROR,
  14405. "%s: %s, count=%d bytes_read=%d\n",
  14406. __func__, err_str, count, bytes_read);
  14407. *irq_toggled = 0;
  14408. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14409. goto exit;
  14410. } else {
  14411. *bus_toggled = 1;
  14412. *irq_toggled = 1;
  14413. goto exit;
  14414. }
  14415. }
  14416. if (pt_check_irq_asserted(cd)) {
  14417. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14418. rc = -EIO;
  14419. *irq_toggled = 0;
  14420. goto exit;
  14421. }
  14422. /* Try sending a PIP command to see if we get a response */
  14423. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14424. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14425. if (rc) {
  14426. /*
  14427. * Potential IRQ issue, no communication in App mode, attempt
  14428. * the same command in the BL
  14429. */
  14430. mode = PT_MODE_IGNORE;
  14431. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14432. if (rc) {
  14433. pt_debug(dev, DL_ERROR,
  14434. "%s Failed to enter BL\n", __func__);
  14435. *irq_toggled = 0;
  14436. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14437. goto exit;
  14438. }
  14439. /*
  14440. * If original mode was operational and we successfully
  14441. * entered the BL, this will be useful info for the tp_xres
  14442. * test below.
  14443. */
  14444. if (mode == PT_MODE_OPERATIONAL)
  14445. *xres_toggled = 1;
  14446. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14447. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14448. &actual_read_len);
  14449. if (rc) {
  14450. /*
  14451. * Could not communicate in FW mode or BL mode. Save the
  14452. * error string, slim chance but the XRES test below may
  14453. * show the IRQ is actually working.
  14454. */
  14455. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14456. pt_debug(dev, DL_ERROR,
  14457. "%s: request_active_pip_prot failed\n",
  14458. __func__);
  14459. *irq_toggled = 0;
  14460. goto exit;
  14461. }
  14462. }
  14463. if (cd->irq_count > 0) {
  14464. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14465. cd->irq_count);
  14466. *bus_toggled = 1;
  14467. *irq_toggled = 1;
  14468. goto exit;
  14469. }
  14470. exit:
  14471. kfree(read_buf);
  14472. return rc;
  14473. }
  14474. /*******************************************************************************
  14475. * FUNCTION: pt_bist_xres_test
  14476. *
  14477. * SUMMARY: Tests the connectivity of the TP_XRES net
  14478. *
  14479. * This test will ensure there is a good connection between the host
  14480. * and the DUT on the tp_xres pin. The pin will be toggled to
  14481. * generate a TP reset which will cause the DUT to output a reset
  14482. * sentinel. If the reset sentinel is seen the test passes. If it is
  14483. * not seen the test will attempt to send a soft reset to simply gain
  14484. * some additional information on the failure:
  14485. * - soft reset fails to send: XRES and IRQ likely open
  14486. * - soft reset passes: XRES likely open or stuck de-asserted
  14487. * - soft reset fails: XRES likely stuck asserted
  14488. *
  14489. * RETURN:
  14490. * 0 = Success
  14491. * !0 = Failure
  14492. *
  14493. * PARAMETERS:
  14494. * *dev - pointer to device structure
  14495. * *bus_toggled - pointer to where to store if bus toggled
  14496. * *irq_toggled - pointer to where to store if IRQ toggled
  14497. * *xres_toggled - pointer to where to store if XRES toggled
  14498. * *err_str - pointer to error string buffer
  14499. ******************************************************************************/
  14500. static int pt_bist_xres_test(struct device *dev,
  14501. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14502. {
  14503. struct pt_core_data *cd = dev_get_drvdata(dev);
  14504. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14505. u8 *read_buf = NULL;
  14506. u8 mode = PT_MODE_UNKNOWN;
  14507. int rc = 0;
  14508. int t = 0;
  14509. int timeout = 300;
  14510. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14511. if (read_buf == NULL) {
  14512. rc = -ENOMEM;
  14513. goto exit;
  14514. }
  14515. /* Clear the startup bit mask, reset and enum will re-populate it */
  14516. cd->startup_status = STARTUP_STATUS_START;
  14517. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14518. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14519. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14520. rc = -ENODEV;
  14521. goto exit;
  14522. }
  14523. /* Ensure we have nothing pending on active bus */
  14524. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14525. /* Perform a hard XRES toggle and wait for reset sentinel */
  14526. mutex_lock(&cd->system_lock);
  14527. cd->hid_reset_cmd_state = 1;
  14528. mutex_unlock(&cd->system_lock);
  14529. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14530. rc = pt_hw_hard_reset(cd);
  14531. /* Set timeout to 1s for the flashless case where a BL could be done */
  14532. if (cd->flashless_dut)
  14533. timeout = 1000;
  14534. /*
  14535. * To avoid the case that next PIP command can be confused by BL/FW
  14536. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14537. * followed by "wakeup event" function can lower the failure rate.
  14538. */
  14539. t = wait_event_timeout(cd->wait_q,
  14540. ((cd->startup_status != STARTUP_STATUS_START)
  14541. && (cd->hid_reset_cmd_state == 0)),
  14542. msecs_to_jiffies(timeout));
  14543. if (IS_TMO(t)) {
  14544. pt_debug(cd->dev, DL_ERROR,
  14545. "%s: TMO waiting for sentinel\n", __func__);
  14546. *xres_toggled = 0;
  14547. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14548. /*
  14549. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14550. * To enter the BL we need to generate an XRES so first try to
  14551. * launch the applicaiton
  14552. */
  14553. if (cd->mode == PT_MODE_BOOTLOADER)
  14554. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14555. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14556. __func__);
  14557. mode = PT_MODE_IGNORE;
  14558. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14559. if (rc) {
  14560. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14561. __func__);
  14562. *xres_toggled = 0;
  14563. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14564. goto exit;
  14565. } else {
  14566. /* Wait for the BL sentinel */
  14567. t = wait_event_timeout(cd->wait_q,
  14568. (cd->startup_status != STARTUP_STATUS_START),
  14569. msecs_to_jiffies(500));
  14570. if (IS_TMO(t)) {
  14571. pt_debug(cd->dev, DL_ERROR,
  14572. "%s: TMO waiting for BL sentinel\n",
  14573. __func__);
  14574. *xres_toggled = 0;
  14575. strlcpy(err_str,
  14576. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14577. rc = -ETIME;
  14578. goto exit;
  14579. }
  14580. }
  14581. }
  14582. mutex_lock(&cd->system_lock);
  14583. cd->hid_reset_cmd_state = 0;
  14584. mutex_unlock(&cd->system_lock);
  14585. /* Look for BL or FW reset sentinels */
  14586. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14587. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14588. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14589. /* If a sentinel was seen, all nets are working */
  14590. *xres_toggled = 1;
  14591. *irq_toggled = 1;
  14592. /*
  14593. * For SPI test, bus read result can be confused as FW sentinel
  14594. * if MISO(slave) is connected to MISO(host).
  14595. */
  14596. if (cd->bus_ops->bustype == BUS_I2C)
  14597. *bus_toggled = 1;
  14598. } else {
  14599. /*
  14600. * Hard reset failed, however some additional information
  14601. * could be determined. Try a soft reset to see if DUT resets
  14602. * with the possible outcomes:
  14603. * - if it resets the line is not stuck asserted
  14604. * - if it does not reset the line could be stuck asserted
  14605. */
  14606. *xres_toggled = 0;
  14607. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14608. msleep(30);
  14609. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14610. __func__, rc);
  14611. if (rc) {
  14612. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14613. pt_debug(dev, DL_ERROR,
  14614. "%s: Hard reset failed, soft reset failed %s\n",
  14615. __func__, err_str);
  14616. goto exit;
  14617. }
  14618. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14619. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14620. strlcpy(err_str,
  14621. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14622. pt_debug(dev, DL_ERROR,
  14623. "%s: Hard reset failed, soft reset passed-%s\n",
  14624. __func__, err_str);
  14625. } else if (cd->startup_status == 0) {
  14626. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14627. pt_debug(dev, DL_ERROR,
  14628. "%s: Hard reset failed, soft reset failed-%s\n",
  14629. __func__, err_str);
  14630. } else {
  14631. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14632. pt_debug(dev, DL_ERROR,
  14633. "%s: Hard and Soft reset failed - %s\n",
  14634. __func__, err_str);
  14635. }
  14636. }
  14637. exit:
  14638. kfree(read_buf);
  14639. return rc;
  14640. }
  14641. /*******************************************************************************
  14642. * FUNCTION: pt_bist_slave_irq_test
  14643. *
  14644. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14645. *
  14646. * This test will ensure there is a good IRQ connection between the master
  14647. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14648. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14649. * saw each slave trigger the IRQ with it's reset sentinel.
  14650. *
  14651. * RETURN:
  14652. * 0 = Success
  14653. * !0 = Failure
  14654. *
  14655. * PARAMETERS:
  14656. * *dev - pointer to device structure
  14657. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14658. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14659. * *err_str - pointer to error string buffer
  14660. * *slave_detect - pointer to slave detect buffer
  14661. * *boot_err - pointer to boot_err buffer
  14662. ******************************************************************************/
  14663. static int pt_bist_slave_irq_test(struct device *dev,
  14664. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14665. u8 *slave_detect, u8 *boot_err)
  14666. {
  14667. struct pt_core_data *cd = dev_get_drvdata(dev);
  14668. u8 mode = PT_MODE_UNKNOWN;
  14669. u8 status;
  14670. u8 boot;
  14671. u8 read_buf[12];
  14672. u8 detected = 0;
  14673. u8 last_err = -1;
  14674. u16 actual_read_len;
  14675. int result = 0;
  14676. int rc = 0;
  14677. /*
  14678. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14679. * detect bits. If the DUT was in FW, entering the BL will cause an
  14680. * XRES signal which will inadvertently test the XRES net as well
  14681. */
  14682. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14683. if (rc) {
  14684. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14685. __func__, rc);
  14686. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14687. goto exit;
  14688. }
  14689. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14690. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14691. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14692. &actual_read_len);
  14693. if (!rc) {
  14694. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14695. "PIP2 STATUS");
  14696. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14697. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14698. /* Slave detect is only valid if status ok and in boot exec */
  14699. if (status == PIP2_RSP_ERR_NONE &&
  14700. boot == PIP2_STATUS_BOOT_EXEC) {
  14701. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14702. SLAVE_DETECT_MASK;
  14703. } else {
  14704. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14705. rc = -EPERM;
  14706. }
  14707. } else {
  14708. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14709. __func__);
  14710. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14711. goto exit;
  14712. }
  14713. /*
  14714. * Retrieve boot error regardless of the state of the slave detect
  14715. * bit because the IRQ could have been stuck high or low.
  14716. */
  14717. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14718. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14719. read_buf, &actual_read_len);
  14720. if (!rc) {
  14721. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14722. "PIP2 GET_LAST_ERRNO");
  14723. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14724. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14725. if (last_err) {
  14726. pt_debug(cd->dev, DL_ERROR,
  14727. "%s: Master Boot Last Err = 0x%02X\n",
  14728. __func__, last_err);
  14729. }
  14730. } else {
  14731. pt_debug(cd->dev, DL_ERROR,
  14732. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14733. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14734. }
  14735. exit:
  14736. pt_debug(cd->dev, DL_INFO,
  14737. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14738. __func__, rc, detected, last_err);
  14739. if (err_str && last_err) {
  14740. if (detected)
  14741. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14742. "- Likely stuck low. Boot Error:",
  14743. last_err);
  14744. else
  14745. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14746. "- Likely stuck high. Boot Error:",
  14747. last_err);
  14748. }
  14749. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14750. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14751. last_err = PIP2_RSP_ERR_NONE;
  14752. if (slave_irq_toggled)
  14753. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14754. if (slave_bus_toggled) {
  14755. /* Leave as UNTEST if slave not detected */
  14756. if (detected)
  14757. *slave_bus_toggled = !last_err ? true : false;
  14758. }
  14759. if (slave_detect)
  14760. *slave_detect = detected;
  14761. if (boot_err)
  14762. *boot_err = last_err;
  14763. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14764. __func__,
  14765. "Detected", detected,
  14766. "slave_irq_toggled", *slave_irq_toggled,
  14767. "slave_bus_toggled", *slave_bus_toggled);
  14768. return rc;
  14769. }
  14770. /*******************************************************************************
  14771. * FUNCTION: pt_bist_slave_xres_test
  14772. *
  14773. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14774. *
  14775. * This test will ensure there is a good TP_XRES connection between the
  14776. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14777. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14778. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14779. * it's reset sentinel.
  14780. *
  14781. * RETURN:
  14782. * 0 = Success
  14783. * !0 = Failure
  14784. *
  14785. * PARAMETERS:
  14786. * *dev - pointer to device structure
  14787. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14788. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14789. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14790. * *err_str - pointer to error string buffer
  14791. ******************************************************************************/
  14792. static int pt_bist_slave_xres_test(struct device *dev,
  14793. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14794. u8 *err_str)
  14795. {
  14796. struct pt_core_data *cd = dev_get_drvdata(dev);
  14797. u8 slave_detect = 0;
  14798. u8 boot_err = 0;
  14799. int rc = 0;
  14800. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14801. mutex_lock(&cd->system_lock);
  14802. cd->hid_reset_cmd_state = 1;
  14803. mutex_unlock(&cd->system_lock);
  14804. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14805. pt_hw_hard_reset(cd);
  14806. msleep(100);
  14807. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14808. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14809. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14810. if (!rc && *slave_irq_toggled == false) {
  14811. /*
  14812. * If the slave IRQ did not toggle, either the slave_detect
  14813. * bit was not set or we had a boot error. If the slave
  14814. * detect was not set the slave did not reset causing a boot
  14815. * error.
  14816. */
  14817. if (!slave_detect)
  14818. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14819. else
  14820. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14821. "- likely open or an IRQ issue. Boot Error:",
  14822. boot_err);
  14823. }
  14824. if (slave_xres_toggled) {
  14825. if (!rc)
  14826. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14827. else
  14828. *slave_xres_toggled = false;
  14829. }
  14830. return rc;
  14831. }
  14832. /*******************************************************************************
  14833. * FUNCTION: pt_bist_slave_bus_test
  14834. *
  14835. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14836. *
  14837. * This test will ensure a good SPI bus connection between the
  14838. * master DUT and the slave DUT(s). This bus connection is ensured by
  14839. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14840. * then there is a master/slave communication issue. Opening file 0 on
  14841. * the master will open it on the slave as well if the slave was detected.
  14842. *
  14843. * RETURN:
  14844. * 0 = Success
  14845. * !0 = Failure
  14846. *
  14847. * PARAMETERS:
  14848. * *dev - pointer to device structure
  14849. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14850. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14851. * *err_str - pointer to error string buffer
  14852. ******************************************************************************/
  14853. static int pt_bist_slave_bus_test(struct device *dev,
  14854. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14855. {
  14856. struct pt_core_data *cd = dev_get_drvdata(dev);
  14857. u8 mode = PT_MODE_UNKNOWN;
  14858. u8 bus_toggled = false;
  14859. u8 file_handle;
  14860. int result = 0;
  14861. int rc = 0;
  14862. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14863. if (rc) {
  14864. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14865. __func__, rc);
  14866. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14867. goto exit;
  14868. }
  14869. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14870. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14871. if (file_handle != PIP2_RAM_FILE) {
  14872. rc = -ENOENT;
  14873. bus_toggled = false;
  14874. pt_debug(dev, DL_ERROR,
  14875. "%s Failed to open bin file\n", __func__);
  14876. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14877. goto exit;
  14878. } else {
  14879. bus_toggled = true;
  14880. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14881. pt_debug(dev, DL_ERROR,
  14882. "%s: File Close failed, file_handle=%d\n",
  14883. __func__, file_handle);
  14884. }
  14885. }
  14886. exit:
  14887. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14888. if (slave_irq_toggled)
  14889. *slave_irq_toggled = bus_toggled;
  14890. if (slave_bus_toggled)
  14891. *slave_bus_toggled = bus_toggled;
  14892. return rc;
  14893. }
  14894. /*******************************************************************************
  14895. * FUNCTION: pt_ttdl_bist_show
  14896. *
  14897. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14898. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14899. *
  14900. * NOTE: This function will reset the DUT and the startup_status bit
  14901. * mask. A pt_enum will be queued after completion.
  14902. *
  14903. * NOTE: The order of the net tests is done to optimize the time it takes
  14904. * to run. The first test is capable of verifying all nets, each subsequent
  14905. * test is only run if the previous was not able to see all nets toggle.
  14906. *
  14907. * RETURN:
  14908. * 0 = success
  14909. * !0 = failure
  14910. *
  14911. * PARAMETERS:
  14912. * *dev - pointer to device structure
  14913. * *attr - pointer to device attributes
  14914. * *buf - pointer to output buffer
  14915. ******************************************************************************/
  14916. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14917. struct device_attribute *attr, char *buf)
  14918. {
  14919. struct pt_core_data *cd = dev_get_drvdata(dev);
  14920. ssize_t ret;
  14921. char *bus_err_str = NULL;
  14922. char *irq_err_str = NULL;
  14923. char *xres_err_str = NULL;
  14924. char *slave_bus_err_str = NULL;
  14925. char *slave_irq_err_str = NULL;
  14926. char *slave_xres_err_str = NULL;
  14927. u8 tests;
  14928. int rc = 0;
  14929. int num_tests = 0;
  14930. int status = 1; /* 0 = Pass, !0 = fail */
  14931. u8 bus_toggled = 0x0F; /* default to untested */
  14932. u8 i2c_toggled = 0x0F; /* default to untested */
  14933. u8 spi_toggled = 0x0F; /* default to untested */
  14934. u8 irq_toggled = 0x0F; /* default to untested */
  14935. u8 xres_toggled = 0x0F; /* default to untested */
  14936. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14937. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14938. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14939. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14940. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14941. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14942. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14943. goto print_results;
  14944. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14945. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14946. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14947. if (cd->multi_chip) {
  14948. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14949. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14950. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14951. if (!slave_bus_err_str ||
  14952. !slave_irq_err_str ||
  14953. !slave_xres_err_str)
  14954. goto print_results;
  14955. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14956. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14957. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14958. }
  14959. /* Turn off the TTDL WD during the test */
  14960. pt_stop_wd_timer(cd);
  14961. /* Shorten default PIP cmd timeout while running BIST */
  14962. cd->pip_cmd_timeout = 200;
  14963. /* Count the number of tests to run */
  14964. tests = cd->ttdl_bist_select;
  14965. while (tests) {
  14966. num_tests += tests & 1;
  14967. tests >>= 1;
  14968. }
  14969. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14970. __func__, cd->ttdl_bist_select, num_tests);
  14971. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14972. if (cd->flashless_dut) {
  14973. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14974. __func__);
  14975. mutex_lock(&cd->system_lock);
  14976. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14977. mutex_unlock(&cd->system_lock);
  14978. }
  14979. /* --------------- TP_XRES BIST TEST --------------- */
  14980. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14981. pt_debug(dev, DL_INFO,
  14982. "%s: ----- Start TP_XRES BIST -----", __func__);
  14983. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14984. &xres_toggled, xres_err_str);
  14985. /* Done if the rest of all nets toggled */
  14986. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14987. goto host_nets_complete;
  14988. }
  14989. /* Flush bus in case a PIP response is waiting from previous test */
  14990. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14991. /* --------------- IRQ BIST TEST --------------- */
  14992. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14993. pt_debug(dev, DL_INFO,
  14994. "%s: ----- Start IRQ BIST -----", __func__);
  14995. bus_toggled = 0xFF;
  14996. irq_toggled = 0xFF;
  14997. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14998. &xres_toggled, irq_err_str);
  14999. /* If this net failed clear results from previous net */
  15000. if (irq_toggled != 1) {
  15001. xres_toggled = 0x0F;
  15002. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15003. }
  15004. if (bus_toggled == 1 && irq_toggled == 1)
  15005. goto host_nets_complete;
  15006. }
  15007. /* Flush bus in case a PIP response is waiting from previous test */
  15008. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15009. /* --------------- BUS BIST TEST --------------- */
  15010. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15011. pt_debug(dev, DL_INFO,
  15012. "%s: ----- Start BUS BIST -----", __func__);
  15013. bus_toggled = 0xFF;
  15014. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15015. /* If this net failed clear results from previous net */
  15016. if (bus_toggled == 0) {
  15017. irq_toggled = 0x0F;
  15018. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15019. }
  15020. }
  15021. host_nets_complete:
  15022. /* --------------- SLAVE XRES BIST TEST --------------- */
  15023. if (cd->multi_chip &&
  15024. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15025. pt_debug(dev, DL_INFO,
  15026. "%s: ----- Start Slave XRES BIST -----", __func__);
  15027. slave_xres_toggled = 0xFF;
  15028. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15029. &slave_bus_toggled, &slave_xres_toggled,
  15030. slave_xres_err_str);
  15031. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15032. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15033. goto print_results;
  15034. }
  15035. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15036. if (cd->multi_chip &&
  15037. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15038. pt_debug(dev, DL_INFO,
  15039. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15040. slave_irq_toggled = 0xFF;
  15041. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15042. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15043. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15044. __func__, slave_irq_toggled);
  15045. if (slave_irq_toggled == 1) {
  15046. slave_bus_toggled = 1;
  15047. goto print_results;
  15048. }
  15049. }
  15050. /* --------------- SLAVE BUS BIST TEST --------------- */
  15051. if (cd->multi_chip &&
  15052. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15053. pt_debug(dev, DL_INFO,
  15054. "%s: ----- Start Slave BUS BIST -----", __func__);
  15055. slave_bus_toggled = 0xFF;
  15056. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15057. &slave_bus_toggled, slave_bus_err_str);
  15058. }
  15059. print_results:
  15060. /* Restore PIP command timeout */
  15061. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15062. /*
  15063. * We're done! - Perform a hard XRES toggle, allowing BL
  15064. * to load FW if there is any in Flash
  15065. */
  15066. mutex_lock(&cd->system_lock);
  15067. cd->hid_reset_cmd_state = 0;
  15068. mutex_unlock(&cd->system_lock);
  15069. pt_debug(dev, DL_INFO,
  15070. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15071. if (cd->flashless_dut) {
  15072. /*
  15073. * For Flashless solution, FW update is triggered after BL is
  15074. * seen that several miliseconds delay is needed.
  15075. */
  15076. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15077. __func__);
  15078. mutex_lock(&cd->system_lock);
  15079. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15080. mutex_unlock(&cd->system_lock);
  15081. /* Reset DUT and wait 100ms to see if loader started */
  15082. pt_hw_hard_reset(cd);
  15083. msleep(100);
  15084. if (cd->fw_updating) {
  15085. pt_debug(dev, DL_INFO,
  15086. "%s: ----- BIST Wait FW Loading ----",
  15087. __func__);
  15088. rc = _pt_request_wait_for_enum_state(
  15089. dev, 4000, STARTUP_STATUS_COMPLETE);
  15090. }
  15091. } else {
  15092. if (cd->mode == PT_MODE_BOOTLOADER) {
  15093. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15094. if (rc) {
  15095. pt_debug(dev, DL_ERROR,
  15096. "%s Failed to launch app\n", __func__);
  15097. rc = pt_hw_hard_reset(cd);
  15098. }
  15099. }
  15100. /*
  15101. * If FW exists the BL may have just started or will start soon,
  15102. * so the FW sentinel may be on it's way but with no FW it will
  15103. * not arrive, wait for it before deciding if we need to queue
  15104. * an enum.
  15105. */
  15106. rc = _pt_request_wait_for_enum_state(
  15107. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15108. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15109. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15110. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15111. __func__);
  15112. pt_queue_enum(cd);
  15113. rc = _pt_request_wait_for_enum_state(
  15114. dev, 2000, STARTUP_STATUS_COMPLETE);
  15115. }
  15116. }
  15117. msleep(20);
  15118. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15119. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15120. pt_start_wd_timer(cd);
  15121. /* Canned print if any memory allocation issues */
  15122. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15123. ret = scnprintf(buf, strlen(buf),
  15124. "Status: %d\n"
  15125. "I2C (SDA,SCL): [UNTEST]\n"
  15126. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15127. "IRQ connection: [UNTEST]\n"
  15128. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15129. if (cd->multi_chip) {
  15130. ret += scnprintf(buf + ret, strlen(buf),
  15131. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15132. "I/P IRQ connection: [UNTEST]\n"
  15133. "I/P TP_XRES connection: [UNTEST]\n");
  15134. }
  15135. } else {
  15136. status = 0;
  15137. if (bus_toggled == 1)
  15138. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15139. if (irq_toggled == 1)
  15140. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15141. if (xres_toggled == 1)
  15142. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15143. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15144. status += bus_toggled;
  15145. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15146. status += irq_toggled;
  15147. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15148. status += xres_toggled;
  15149. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15150. __func__, status, bus_toggled, irq_toggled,
  15151. xres_toggled);
  15152. if (cd->multi_chip) {
  15153. if (slave_irq_toggled == 1)
  15154. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15155. if (slave_xres_toggled == 1)
  15156. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15157. if (slave_bus_toggled == 1)
  15158. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15159. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15160. status += slave_bus_toggled;
  15161. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15162. status += slave_irq_toggled;
  15163. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15164. status += slave_xres_toggled;
  15165. pt_debug(dev, DL_ERROR,
  15166. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15167. __func__, status, bus_toggled, irq_toggled,
  15168. xres_toggled, slave_bus_toggled,
  15169. slave_irq_toggled, slave_xres_toggled);
  15170. }
  15171. if (cd->bus_ops->bustype == BUS_I2C)
  15172. i2c_toggled = bus_toggled;
  15173. else
  15174. spi_toggled = bus_toggled;
  15175. ret = scnprintf(buf, strlen(buf),
  15176. "Status: %d\n"
  15177. "I2C (SDA,SCL): %s %s\n"
  15178. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15179. "IRQ connection: %s %s\n"
  15180. "TP_XRES connection: %s %s\n",
  15181. status == num_tests ? 0 : 1,
  15182. i2c_toggled == 0x0F ? "[UNTEST]" :
  15183. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15184. i2c_toggled == 0x0F ? "" : bus_err_str,
  15185. spi_toggled == 0x0F ? "[UNTEST]" :
  15186. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15187. spi_toggled == 0x0F ? "" : bus_err_str,
  15188. irq_toggled == 0x0F ? "[UNTEST]" :
  15189. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15190. irq_err_str,
  15191. xres_toggled == 0x0F ? "[UNTEST]" :
  15192. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15193. xres_err_str);
  15194. if (cd->multi_chip) {
  15195. ret += scnprintf(buf + ret, strlen(buf),
  15196. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15197. "I/P IRQ connection: %s %s\n"
  15198. "I/P TP_XRES connection: %s %s\n",
  15199. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15200. slave_bus_toggled == 1 ? "[ OK ]" :
  15201. "[FAILED]", slave_bus_err_str,
  15202. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15203. slave_irq_toggled == 1 ? "[ OK ]" :
  15204. "[FAILED]", slave_irq_err_str,
  15205. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15206. slave_xres_toggled == 1 ? "[ OK ]" :
  15207. "[FAILED]", slave_xres_err_str);
  15208. }
  15209. }
  15210. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15211. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15212. __func__, cd->startup_status);
  15213. kfree(bus_err_str);
  15214. kfree(irq_err_str);
  15215. kfree(xres_err_str);
  15216. return ret;
  15217. }
  15218. /*******************************************************************************
  15219. * FUNCTION: pt_ttdl_bist_store
  15220. *
  15221. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15222. *
  15223. * RETURN: Size of passed in buffer if successful
  15224. *
  15225. * PARAMETERS:
  15226. * *dev - pointer to device structure
  15227. * *attr - pointer to device attributes
  15228. * *buf - pointer to command buffer
  15229. * size - size of buf
  15230. ******************************************************************************/
  15231. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15232. struct device_attribute *attr, const char *buf, size_t size)
  15233. {
  15234. struct pt_core_data *cd = dev_get_drvdata(dev);
  15235. u32 input_data[2] = {0};
  15236. int length;
  15237. int rc = 0;
  15238. /* Maximum input of one value */
  15239. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15240. ARRAY_SIZE(input_data));
  15241. if (length != 1) {
  15242. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15243. __func__);
  15244. rc = -EINVAL;
  15245. goto exit;
  15246. } else {
  15247. mutex_lock(&cd->system_lock);
  15248. cd->ttdl_bist_select = input_data[0];
  15249. mutex_unlock(&cd->system_lock);
  15250. }
  15251. exit:
  15252. if (rc)
  15253. return rc;
  15254. return size;
  15255. }
  15256. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15257. pt_ttdl_bist_store);
  15258. /*******************************************************************************
  15259. * FUNCTION: pt_flush_bus_store
  15260. *
  15261. * SUMMARY: Store method for the flush_bus sysfs node.
  15262. *
  15263. * RETURN: Size of passed in buffer if successful
  15264. *
  15265. * PARAMETERS:
  15266. * *dev - pointer to device structure
  15267. * *attr - pointer to device attributes
  15268. * *buf - pointer to command buffer
  15269. * size - size of buf
  15270. ******************************************************************************/
  15271. static ssize_t pt_flush_bus_store(struct device *dev,
  15272. struct device_attribute *attr, const char *buf, size_t size)
  15273. {
  15274. struct pt_core_data *cd = dev_get_drvdata(dev);
  15275. u32 input_data[2] = {0};
  15276. int length;
  15277. int rc = 0;
  15278. /* Maximum input of one value */
  15279. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15280. ARRAY_SIZE(input_data));
  15281. if (length != 1) {
  15282. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15283. __func__);
  15284. rc = -EINVAL;
  15285. goto exit;
  15286. }
  15287. mutex_lock(&cd->system_lock);
  15288. if (input_data[0] == 0)
  15289. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15290. else if (input_data[0] == 1)
  15291. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15292. else
  15293. rc = -EINVAL;
  15294. mutex_unlock(&cd->system_lock);
  15295. exit:
  15296. if (rc)
  15297. return rc;
  15298. return size;
  15299. }
  15300. /*******************************************************************************
  15301. * FUNCTION: pt_flush_bus_show
  15302. *
  15303. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15304. * based on either the size of the first two bytes or a blind 256 bytes.
  15305. *
  15306. * RETURN:
  15307. * 0 = success
  15308. * !0 = failure
  15309. *
  15310. * PARAMETERS:
  15311. * *dev - pointer to device structure
  15312. * *attr - pointer to device attributes
  15313. * *buf - pointer to output buffer
  15314. ******************************************************************************/
  15315. static ssize_t pt_flush_bus_show(struct device *dev,
  15316. struct device_attribute *attr, char *buf)
  15317. {
  15318. ssize_t ret = 0;
  15319. ssize_t bytes = 0;
  15320. struct pt_core_data *cd = dev_get_drvdata(dev);
  15321. mutex_lock(&cd->system_lock);
  15322. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15323. ret = scnprintf(buf, strlen(buf),
  15324. "Status: 0\n"
  15325. "%s: %zd\n",
  15326. "Bytes flushed", bytes);
  15327. mutex_unlock(&cd->system_lock);
  15328. return ret;
  15329. }
  15330. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15331. pt_flush_bus_store);
  15332. /*******************************************************************************
  15333. * FUNCTION: pt_pip2_ping_test_store
  15334. *
  15335. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15336. *
  15337. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15338. * someone may want to test sending invalid packet lengths so any values
  15339. * up to 255 are allowed.
  15340. *
  15341. * RETURN: Size of passed in buffer if successful
  15342. *
  15343. * PARAMETERS:
  15344. * *dev - pointer to device structure
  15345. * *attr - pointer to device attributes
  15346. * *buf - pointer to command buffer
  15347. * size - size of buf
  15348. ******************************************************************************/
  15349. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15350. struct device_attribute *attr, const char *buf, size_t size)
  15351. {
  15352. struct pt_core_data *cd = dev_get_drvdata(dev);
  15353. u32 input_data[2];
  15354. int length;
  15355. int rc = 0;
  15356. /* Maximum input of one value */
  15357. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15358. ARRAY_SIZE(input_data));
  15359. if (length != 1) {
  15360. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15361. __func__);
  15362. rc = -EINVAL;
  15363. goto exit;
  15364. }
  15365. mutex_lock(&cd->system_lock);
  15366. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15367. cd->ping_test_size = input_data[0];
  15368. else
  15369. rc = -EINVAL;
  15370. mutex_unlock(&cd->system_lock);
  15371. exit:
  15372. if (rc)
  15373. return rc;
  15374. return size;
  15375. }
  15376. /*******************************************************************************
  15377. * FUNCTION: pt_ping_test_show
  15378. *
  15379. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15380. * PING command and ramps up the optional payload from 0 to
  15381. * ping_test_size.
  15382. * The max payload size is 247:
  15383. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15384. *
  15385. * RETURN:
  15386. * 0 = success
  15387. * !0 = failure
  15388. *
  15389. * PARAMETERS:
  15390. * *dev - pointer to device structure
  15391. * *attr - pointer to device attributes
  15392. * *buf - pointer to output buffer
  15393. ******************************************************************************/
  15394. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15395. struct device_attribute *attr, char *buf)
  15396. {
  15397. struct pt_core_data *cd = dev_get_drvdata(dev);
  15398. ssize_t ret;
  15399. int last_packet_size;
  15400. int rc = 0;
  15401. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15402. if (rc) {
  15403. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15404. return ret;
  15405. }
  15406. ret = scnprintf(buf, strlen(buf),
  15407. "Status: %d\n"
  15408. "PING payload test passed with packet sizes 0 - %d\n",
  15409. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15410. last_packet_size);
  15411. return ret;
  15412. }
  15413. /*******************************************************************************
  15414. * FUNCTION: pt_t_refresh_store
  15415. *
  15416. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15417. * in integer as the number of interrupts to count. A timer is started to
  15418. * calculate the total time it takes to see that number of interrupts.
  15419. *
  15420. * RETURN: Size of passed in buffer if successful
  15421. *
  15422. * PARAMETERS:
  15423. * *dev - pointer to device structure
  15424. * *attr - pointer to device attributes
  15425. * *buf - pointer to buffer that hold the command parameters
  15426. * size - size of buf
  15427. ******************************************************************************/
  15428. static ssize_t pt_t_refresh_store(struct device *dev,
  15429. struct device_attribute *attr, const char *buf, size_t size)
  15430. {
  15431. struct pt_core_data *cd = dev_get_drvdata(dev);
  15432. u32 input_data[2];
  15433. int length;
  15434. int rc = 0;
  15435. /* Maximum input of one value */
  15436. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15437. ARRAY_SIZE(input_data));
  15438. if (length != 1) {
  15439. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15440. __func__);
  15441. rc = -EINVAL;
  15442. goto exit;
  15443. }
  15444. mutex_lock(&cd->system_lock);
  15445. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15446. input_data[0]);
  15447. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15448. cd->t_refresh_total = input_data[0];
  15449. cd->t_refresh_count = 0;
  15450. cd->t_refresh_active = 1;
  15451. } else {
  15452. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15453. rc = -EINVAL;
  15454. }
  15455. mutex_unlock(&cd->system_lock);
  15456. exit:
  15457. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15458. if (rc)
  15459. return rc;
  15460. return size;
  15461. }
  15462. /*******************************************************************************
  15463. * FUNCTION: pt_t_refresh_show
  15464. *
  15465. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15466. * of the T-Refresh timer counting the time it takes to see a user defined
  15467. * number of interrupts.
  15468. *
  15469. * RETURN:
  15470. * 0 = success
  15471. * !0 = failure
  15472. *
  15473. * PARAMETERS:
  15474. * *dev - pointer to device structure
  15475. * *attr - pointer to device attributes
  15476. * *buf - pointer to output buffer
  15477. ******************************************************************************/
  15478. static ssize_t pt_t_refresh_show(struct device *dev,
  15479. struct device_attribute *attr, char *buf)
  15480. {
  15481. ssize_t ret = 0;
  15482. struct pt_core_data *cd = dev_get_drvdata(dev);
  15483. u32 whole;
  15484. u16 fraction;
  15485. mutex_lock(&cd->system_lock);
  15486. /* Check if we have counted the number requested */
  15487. if (cd->t_refresh_count != cd->t_refresh_total) {
  15488. ret = scnprintf(buf, strlen(buf),
  15489. "Status: 0\n"
  15490. "%s: %d\n",
  15491. "Still counting... current IRQ count",
  15492. cd->t_refresh_count);
  15493. } else {
  15494. /* Ensure T-Refresh is de-activated */
  15495. cd->t_refresh_active = 0;
  15496. whole = cd->t_refresh_time / cd->t_refresh_count;
  15497. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15498. fraction = fraction * 1000 / cd->t_refresh_count;
  15499. ret = scnprintf(buf, strlen(buf),
  15500. "Status: 0\n"
  15501. "%s: %d\n"
  15502. "%s: %d\n"
  15503. "%s: %d\n"
  15504. "%s: %d.%02d\n",
  15505. "Requested IRQ Count ", cd->t_refresh_total,
  15506. "IRQ Counted ", cd->t_refresh_count,
  15507. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15508. "Average T-Refresh (ms) ", whole, fraction);
  15509. }
  15510. mutex_unlock(&cd->system_lock);
  15511. return ret;
  15512. }
  15513. /*******************************************************************************
  15514. * FUNCTION: pt_dut_status_show
  15515. *
  15516. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15517. * more items such as operation mode,easywake state are added in the future.
  15518. *
  15519. * RETURN: Char buffer with printed scan status information
  15520. *
  15521. * PARAMETERS:
  15522. * *dev - pointer to device structure
  15523. * *attr - pointer to device attributes
  15524. * *buf - pointer to output buffer
  15525. ******************************************************************************/
  15526. static ssize_t pt_dut_status_show(struct device *dev,
  15527. struct device_attribute *attr, char *buf)
  15528. {
  15529. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15530. u8 mode = PT_MODE_UNKNOWN;
  15531. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15532. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15533. struct pt_core_data *cd = dev_get_drvdata(dev);
  15534. ssize_t ret;
  15535. u16 calculated_crc = 0;
  15536. u16 stored_crc = 0;
  15537. u8 status;
  15538. int rc = 0;
  15539. /* In STANDBY the DUT will not repond to any PIP cmd */
  15540. if (cd->fw_sys_mode_in_standby_state) {
  15541. mode = PT_MODE_OPERATIONAL;
  15542. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15543. goto print_limited_results;
  15544. }
  15545. /* Retrieve mode and FW system mode which can only be 0-4 */
  15546. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15547. if (rc || mode == PT_MODE_UNKNOWN) {
  15548. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15549. "%s: %d\n"
  15550. "%s: n/a\n"
  15551. "%s: n/a\n"
  15552. "%s: n/a\n"
  15553. "%s: n/a\n",
  15554. "Status", rc,
  15555. "Active Exec ",
  15556. "FW System Mode ",
  15557. "Stored CRC ",
  15558. "Calculated CRC ");
  15559. return ret;
  15560. } else {
  15561. if (mode == PT_MODE_OPERATIONAL) {
  15562. if (sys_mode > FW_SYS_MODE_MAX)
  15563. sys_mode = FW_SYS_MODE_UNDEFINED;
  15564. if (sys_mode != FW_SYS_MODE_TEST)
  15565. goto print_limited_results;
  15566. rc = pt_pip_verify_config_block_crc_(cd,
  15567. PT_TCH_PARM_EBID, &status,
  15568. &calculated_crc, &stored_crc);
  15569. if (rc)
  15570. goto print_limited_results;
  15571. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15572. "%s: %d\n"
  15573. "%s: %s\n"
  15574. "%s: %s\n"
  15575. "%s: 0x%04X\n"
  15576. "%s: 0x%04X\n",
  15577. "Status", rc,
  15578. "Active Exec ", "FW",
  15579. "FW System Mode ", outputstring[sys_mode],
  15580. "Stored CRC ", stored_crc,
  15581. "Calculated CRC ", calculated_crc);
  15582. return ret;
  15583. } else {
  15584. /* When in BL or unknon mode Active Exec is "n/a" */
  15585. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15586. }
  15587. }
  15588. print_limited_results:
  15589. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15590. "%s: %d\n"
  15591. "%s: %s\n"
  15592. "%s: %s\n"
  15593. "%s: n/a\n"
  15594. "%s: n/a\n",
  15595. "Status", rc,
  15596. "Active Exec ",
  15597. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15598. "FW System Mode ", outputstring[sys_mode],
  15599. "Stored CRC ",
  15600. "Calculated CRC ");
  15601. return ret;
  15602. }
  15603. #endif /* TTDL_DIAGNOSTICS */
  15604. /*******************************************************************************
  15605. * Structures of sysfs attributes for all DUT dependent sysfs node
  15606. ******************************************************************************/
  15607. static struct attribute *early_attrs[] = {
  15608. &dev_attr_hw_version.attr,
  15609. &dev_attr_drv_version.attr,
  15610. &dev_attr_drv_ver.attr,
  15611. &dev_attr_fw_version.attr,
  15612. &dev_attr_sysinfo.attr,
  15613. &dev_attr_pip2_cmd_rsp.attr,
  15614. &dev_attr_command.attr,
  15615. &dev_attr_drv_debug.attr,
  15616. &dev_attr_hw_reset.attr,
  15617. &dev_attr_response.attr,
  15618. &dev_attr_ttdl_restart.attr,
  15619. #ifdef TTDL_DIAGNOSTICS
  15620. &dev_attr_ttdl_status.attr,
  15621. &dev_attr_pip2_enter_bl.attr,
  15622. &dev_attr_pip2_exit_bl.attr,
  15623. &dev_attr_err_gpio.attr,
  15624. &dev_attr_flush_bus.attr,
  15625. &dev_attr_ttdl_bist.attr,
  15626. #endif /* TTDL_DIAGNOSTICS */
  15627. NULL,
  15628. };
  15629. static struct attribute_group early_attr_group = {
  15630. .attrs = early_attrs,
  15631. };
  15632. static struct device_attribute pip2_attributes[] = {
  15633. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15634. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15635. #ifdef TTDL_DIAGNOSTICS
  15636. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15637. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15638. pt_pip2_ping_test_store),
  15639. #endif
  15640. };
  15641. static struct device_attribute attributes[] = {
  15642. __ATTR(dut_debug, 0644,
  15643. pt_dut_debug_show, pt_drv_debug_store),
  15644. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15645. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15646. __ATTR(get_param, 0644,
  15647. pt_get_param_show, pt_get_param_store),
  15648. #ifdef EASYWAKE_TSG6
  15649. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15650. pt_easy_wakeup_gesture_store),
  15651. __ATTR(easy_wakeup_gesture_id, 0444,
  15652. pt_easy_wakeup_gesture_id_show, NULL),
  15653. __ATTR(easy_wakeup_gesture_data, 0444,
  15654. pt_easy_wakeup_gesture_data_show, NULL),
  15655. #endif
  15656. #ifdef TTDL_DIAGNOSTICS
  15657. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15658. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15659. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15660. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15661. #endif /* TTDL_DIAGNOSTICS */
  15662. };
  15663. /*******************************************************************************
  15664. * FUNCTION: add_sysfs_interfaces
  15665. *
  15666. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15667. *
  15668. * RETURN:
  15669. * 0 = success
  15670. * !0 = failure
  15671. *
  15672. * PARAMETERS:
  15673. * *dev - pointer to device structure
  15674. ******************************************************************************/
  15675. static int add_sysfs_interfaces(struct device *dev)
  15676. {
  15677. int i;
  15678. int j = 0;
  15679. struct pt_core_data *cd = dev_get_drvdata(dev);
  15680. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15681. if (device_create_file(dev, attributes + i))
  15682. goto undo;
  15683. }
  15684. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15685. __func__, cd->active_dut_generation);
  15686. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15687. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15688. if (device_create_file(dev, pip2_attributes + j))
  15689. goto undo;
  15690. }
  15691. }
  15692. return 0;
  15693. undo:
  15694. for (i--; i >= 0; i--)
  15695. device_remove_file(dev, attributes + i);
  15696. for (j--; j >= 0; j--)
  15697. device_remove_file(dev, pip2_attributes + j);
  15698. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15699. __func__);
  15700. return -ENODEV;
  15701. }
  15702. /*******************************************************************************
  15703. * FUNCTION: remove_sysfs_interfaces
  15704. *
  15705. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15706. *
  15707. * RETURN: void
  15708. *
  15709. * PARAMETERS:
  15710. * *dev - pointer to device structure
  15711. ******************************************************************************/
  15712. static void remove_sysfs_interfaces(struct device *dev)
  15713. {
  15714. int i;
  15715. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15716. device_remove_file(dev, attributes + i);
  15717. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15718. device_remove_file(dev, pip2_attributes + i);
  15719. }
  15720. /*******************************************************************************
  15721. * FUNCTION: remove_sysfs_and_modules
  15722. *
  15723. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15724. *
  15725. * RETURN: void
  15726. *
  15727. * PARAMETERS:
  15728. * *dev - pointer to device structure
  15729. ******************************************************************************/
  15730. static void remove_sysfs_and_modules(struct device *dev)
  15731. {
  15732. struct pt_core_data *cd = dev_get_drvdata(dev);
  15733. /* Queued work should be removed before to release loader module */
  15734. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15735. pt_release_modules(cd);
  15736. pt_btn_release(dev);
  15737. pt_mt_release(dev);
  15738. remove_sysfs_interfaces(dev);
  15739. }
  15740. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15741. {
  15742. int retval;
  15743. /* Get pinctrl if target uses pinctrl */
  15744. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15745. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15746. retval = PTR_ERR(cd->ts_pinctrl);
  15747. dev_dbg(cd->dev,
  15748. "Target does not use pinctrl %d\n", retval);
  15749. goto err_pinctrl_get;
  15750. }
  15751. cd->pinctrl_state_active
  15752. = pinctrl_lookup_state(cd->ts_pinctrl,
  15753. PINCTRL_STATE_ACTIVE);
  15754. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15755. retval = PTR_ERR(cd->pinctrl_state_active);
  15756. dev_err(cd->dev,
  15757. "Can not lookup %s pinstate %d\n",
  15758. PINCTRL_STATE_ACTIVE, retval);
  15759. goto err_pinctrl_lookup;
  15760. }
  15761. cd->pinctrl_state_suspend
  15762. = pinctrl_lookup_state(cd->ts_pinctrl,
  15763. PINCTRL_STATE_SUSPEND);
  15764. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15765. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15766. dev_err(cd->dev,
  15767. "Can not lookup %s pinstate %d\n",
  15768. PINCTRL_STATE_SUSPEND, retval);
  15769. goto err_pinctrl_lookup;
  15770. }
  15771. cd->pinctrl_state_release
  15772. = pinctrl_lookup_state(cd->ts_pinctrl,
  15773. PINCTRL_STATE_RELEASE);
  15774. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15775. retval = PTR_ERR(cd->pinctrl_state_release);
  15776. dev_dbg(cd->dev,
  15777. "Can not lookup %s pinstate %d\n",
  15778. PINCTRL_STATE_RELEASE, retval);
  15779. }
  15780. return 0;
  15781. err_pinctrl_lookup:
  15782. devm_pinctrl_put(cd->ts_pinctrl);
  15783. err_pinctrl_get:
  15784. cd->ts_pinctrl = NULL;
  15785. return retval;
  15786. }
  15787. /*******************************************************************************
  15788. *******************************************************************************
  15789. * FUNCTION: pt_probe
  15790. *
  15791. * SUMMARY: Probe of the core module.
  15792. *
  15793. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15794. * probe functionality is split into two functions; pt_probe() and
  15795. * pt_probe_complete(). the initial setup is done in this function which
  15796. * then creates a WORK task which runs after the probe timer expires. This
  15797. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15798. *
  15799. * RETURN:
  15800. * 0 = success
  15801. * !0 = failure
  15802. *
  15803. * PARAMETERS:
  15804. * *ops - pointer to the bus
  15805. * *dev - pointer to the device structure
  15806. * irq - IRQ
  15807. * xfer_buf_size - size of the buffer
  15808. ******************************************************************************/
  15809. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15810. u16 irq, size_t xfer_buf_size)
  15811. {
  15812. struct pt_core_data *cd;
  15813. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15814. enum pt_atten_type type;
  15815. struct i2c_client *client = to_i2c_client(dev);
  15816. int rc = 0;
  15817. u8 pip_ver_major;
  15818. u8 pip_ver_minor;
  15819. u32 status = STARTUP_STATUS_START;
  15820. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15821. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15822. __func__);
  15823. rc = -ENODEV;
  15824. goto error_no_pdata;
  15825. }
  15826. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15827. if (!pdata->core_pdata->power) {
  15828. pt_debug(dev, DL_ERROR,
  15829. "%s: Missing platform data function\n",
  15830. __func__);
  15831. rc = -ENODEV;
  15832. goto error_no_pdata;
  15833. }
  15834. }
  15835. /* get context and debug print buffers */
  15836. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15837. if (!cd) {
  15838. rc = -ENOMEM;
  15839. goto error_no_pdata;
  15840. }
  15841. /* Initialize device info */
  15842. cd->dev = dev;
  15843. cd->pdata = pdata;
  15844. cd->cpdata = pdata->core_pdata;
  15845. cd->bus_ops = ops;
  15846. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15847. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15848. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15849. cd->hw_detected = false;
  15850. cd->pip2_prot_active = false;
  15851. cd->pip2_send_user_cmd = false;
  15852. cd->bl_pip_ver_ready = false;
  15853. cd->app_pip_ver_ready = false;
  15854. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15855. cd->get_param_id = 0;
  15856. cd->watchdog_enabled = 0;
  15857. cd->startup_retry_count = 0;
  15858. cd->core_probe_complete = 0;
  15859. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15860. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15861. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15862. cd->flashless_dut = 0;
  15863. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15864. cd->bl_with_no_int = 0;
  15865. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15866. cd->multi_chip = PT_FEATURE_DISABLE;
  15867. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15868. cd->sleep_state = SS_SLEEP_NONE;
  15869. cd->quick_boot = false;
  15870. cd->drv_debug_suspend = false;
  15871. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15872. cd->set_dut_generation = true;
  15873. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15874. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15875. cd->set_dut_generation = true;
  15876. cd->active_dut_generation = DUT_PIP1_ONLY;
  15877. } else {
  15878. cd->set_dut_generation = false;
  15879. cd->active_dut_generation = DUT_UNKNOWN;
  15880. }
  15881. /* Initialize with platform data */
  15882. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15883. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15884. cd->hid_cmd_state = 1;
  15885. cd->fw_updating = false;
  15886. cd->multi_chip = 0;
  15887. #ifdef TTDL_DIAGNOSTICS
  15888. cd->t_refresh_active = 0;
  15889. cd->t_refresh_count = 0;
  15890. cd->pip2_crc_error_count = 0;
  15891. cd->wd_xres_count = 0;
  15892. cd->bl_retry_packet_count = 0;
  15893. cd->file_erase_timeout_count = 0;
  15894. cd->show_tt_data = false;
  15895. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15896. cd->err_gpio = 0;
  15897. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15898. cd->ttdl_bist_select = 0x07;
  15899. cd->force_pip2_seq = 0;
  15900. #endif /* TTDL_DIAGNOSTICS */
  15901. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15902. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15903. sizeof(PT_PIP2_BIN_FILE_PATH));
  15904. pt_init_hid_descriptor(&cd->hid_desc);
  15905. /* Read and store the descriptor lengths */
  15906. cd->hid_core.hid_report_desc_len =
  15907. le16_to_cpu(cd->hid_desc.report_desc_len);
  15908. cd->hid_core.hid_max_input_len =
  15909. le16_to_cpu(cd->hid_desc.max_input_len);
  15910. cd->hid_core.hid_max_output_len =
  15911. le16_to_cpu(cd->hid_desc.max_output_len);
  15912. /* Initialize mutexes and spinlocks */
  15913. mutex_init(&cd->module_list_lock);
  15914. mutex_init(&cd->system_lock);
  15915. mutex_init(&cd->sysfs_lock);
  15916. mutex_init(&cd->ttdl_restart_lock);
  15917. mutex_init(&cd->firmware_class_lock);
  15918. spin_lock_init(&cd->spinlock);
  15919. /* Initialize module list */
  15920. INIT_LIST_HEAD(&cd->module_list);
  15921. /* Initialize attention lists */
  15922. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15923. INIT_LIST_HEAD(&cd->atten_list[type]);
  15924. /* Initialize parameter list */
  15925. INIT_LIST_HEAD(&cd->param_list);
  15926. /* Initialize wait queue */
  15927. init_waitqueue_head(&cd->wait_q);
  15928. rc = pt_ts_pinctrl_init(cd);
  15929. if (!rc && cd->ts_pinctrl) {
  15930. /*
  15931. * Pinctrl handle is optional. If pinctrl handle is found
  15932. * let pins to be configured in active state. If not
  15933. * found continue further without error.
  15934. */
  15935. rc = pinctrl_select_state(cd->ts_pinctrl,
  15936. cd->pinctrl_state_active);
  15937. if (rc < 0)
  15938. dev_err(&client->dev, "failed to select pin to active state\n");
  15939. }
  15940. rc = pt_get_regulator(cd, true);
  15941. if (rc) {
  15942. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15943. goto error_alloc_data;
  15944. }
  15945. rc = pt_enable_regulator(cd, true);
  15946. if (rc) {
  15947. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15948. goto error_get_regulator;
  15949. }
  15950. /* Initialize works */
  15951. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15952. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15953. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15954. /* Initialize HID specific data */
  15955. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15956. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15957. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15958. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15959. cd->hid_core.hid_desc_register =
  15960. cpu_to_le16(cd->cpdata->hid_desc_register);
  15961. /* Set platform easywake value */
  15962. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15963. #ifdef CONFIG_DRM
  15964. /* Setup active dsi panel */
  15965. active_panel = cd->cpdata->active_panel;
  15966. #endif
  15967. /* Set platform panel_id value */
  15968. cd->panel_id_support = cd->cpdata->panel_id_support;
  15969. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15970. /* Set Panel ID to default to 0 */
  15971. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15972. else
  15973. /* Set Panel ID to Not Enabled */
  15974. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15975. /* Initialize hw_version default to FFFF.FFFF.FF */
  15976. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15977. dev_set_drvdata(dev, cd);
  15978. /* PtSBC builds will call this function in pt_probe_complete() */
  15979. pt_add_core(dev);
  15980. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15981. if (rc) {
  15982. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15983. __func__);
  15984. goto error_enable_regulator;
  15985. }
  15986. /*
  15987. * Save the pointer to a global value, which will be used
  15988. * in ttdl_restart function
  15989. */
  15990. cd->bus_ops = ops;
  15991. /*
  15992. * When the IRQ GPIO is not direclty accessible and no function is
  15993. * defined to get the IRQ status, the IRQ passed in must be assigned
  15994. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15995. */
  15996. if (!cd->cpdata->irq_stat) {
  15997. cd->irq = irq;
  15998. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15999. __func__, cd->irq);
  16000. }
  16001. /* Call platform init function before setting up the GPIO's */
  16002. if (cd->cpdata->init) {
  16003. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16004. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16005. } else {
  16006. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16007. __func__);
  16008. rc = 0;
  16009. }
  16010. if (rc < 0) {
  16011. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16012. __func__, rc);
  16013. }
  16014. /* Power on any needed regulator(s) */
  16015. if (cd->cpdata->setup_power) {
  16016. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16017. rc = cd->cpdata->setup_power(cd->cpdata,
  16018. PT_MT_POWER_ON, cd->dev);
  16019. } else {
  16020. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16021. __func__);
  16022. rc = 0;
  16023. }
  16024. if (rc < 0)
  16025. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16026. __func__, rc);
  16027. #ifdef TTDL_DIAGNOSTICS
  16028. cd->watchdog_irq_stuck_count = 0;
  16029. cd->bus_transmit_error_count = 0;
  16030. #endif /* TTDL_DIAGNOSTICS */
  16031. if (cd->cpdata->detect) {
  16032. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16033. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16034. pt_platform_detect_read);
  16035. if (!rc) {
  16036. cd->hw_detected = true;
  16037. pt_debug(cd->dev, DL_INFO,
  16038. "%s: HW detected\n", __func__);
  16039. } else {
  16040. cd->hw_detected = false;
  16041. pt_debug(cd->dev, DL_INFO,
  16042. "%s: No HW detected\n", __func__);
  16043. rc = -ENODEV;
  16044. goto error_detect;
  16045. }
  16046. } else {
  16047. pt_debug(dev, DL_ERROR,
  16048. "%s: PARADE No HW detect function pointer\n",
  16049. __func__);
  16050. /*
  16051. * "hw_reset" is not needed in the "if" statement,
  16052. * because "hw_reset" is already included in "hw_detect"
  16053. * function.
  16054. */
  16055. rc = pt_hw_hard_reset(cd);
  16056. if (rc)
  16057. pt_debug(cd->dev, DL_ERROR,
  16058. "%s: FAILED to execute HARD reset\n",
  16059. __func__);
  16060. }
  16061. if (cd->cpdata->setup_irq) {
  16062. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16063. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16064. if (rc) {
  16065. pt_debug(dev, DL_ERROR,
  16066. "%s: Error, couldn't setup IRQ\n", __func__);
  16067. goto error_setup_irq;
  16068. }
  16069. } else {
  16070. pt_debug(dev, DL_ERROR,
  16071. "%s: IRQ function pointer not setup\n",
  16072. __func__);
  16073. goto error_setup_irq;
  16074. }
  16075. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16076. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16077. (unsigned long)cd);
  16078. #else
  16079. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16080. #endif
  16081. pt_stop_wd_timer(cd);
  16082. #ifdef TTHE_TUNER_SUPPORT
  16083. mutex_init(&cd->tthe_lock);
  16084. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16085. 0644, NULL, cd, &tthe_debugfs_fops);
  16086. #endif
  16087. rc = device_init_wakeup(dev, 1);
  16088. if (rc < 0)
  16089. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16090. __func__, rc);
  16091. if (!enable_irq_wake(cd->irq)) {
  16092. cd->irq_wake = 1;
  16093. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16094. }
  16095. pm_runtime_get_noresume(dev);
  16096. pm_runtime_set_active(dev);
  16097. pm_runtime_enable(dev);
  16098. /* If IRQ asserted, read out all from buffer to release INT pin */
  16099. if (cd->cpdata->irq_stat) {
  16100. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16101. } else {
  16102. /* Force a read in case the reset sentinel already arrived */
  16103. rc = pt_read_input(cd);
  16104. if (!rc)
  16105. pt_parse_input(cd);
  16106. }
  16107. /* Without sleep DUT is not ready and will NAK the first write */
  16108. msleep(150);
  16109. /* Attempt to set the DUT generation if not yet set */
  16110. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16111. if (cd->bl_pip_ver_ready ||
  16112. (cd->app_pip_ver_ready &&
  16113. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16114. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16115. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16116. __func__, cd->active_dut_generation);
  16117. } else {
  16118. rc = _pt_detect_dut_generation(cd->dev,
  16119. &status, &cd->active_dut_generation,
  16120. &cd->mode);
  16121. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16122. || rc) {
  16123. pt_debug(cd->dev, DL_ERROR,
  16124. " === DUT Gen unknown, Skip Enum ===\n");
  16125. goto skip_enum;
  16126. }
  16127. }
  16128. }
  16129. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16130. &pip_ver_major, &pip_ver_minor);
  16131. if (pip_ver_major == 2) {
  16132. cd->bl_pip_ver_ready = true;
  16133. pt_debug(dev, DL_ERROR,
  16134. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16135. pip_ver_minor);
  16136. cd->hw_detected = true;
  16137. } else if (pip_ver_major == 1) {
  16138. cd->app_pip_ver_ready = true;
  16139. pt_debug(dev, DL_ERROR,
  16140. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16141. cd->hw_detected = true;
  16142. } else {
  16143. cd->sysinfo.ttdata.pip_ver_major = 0;
  16144. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16145. cd->app_pip_ver_ready = false;
  16146. cd->hw_detected = false;
  16147. pt_debug(dev, DL_ERROR,
  16148. " === PIP Version Not Detected, Skip Enum ===\n");
  16149. /* For legacy DUTS proceed, enum will attempt to launch app */
  16150. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16151. goto skip_enum;
  16152. }
  16153. rc = pt_enum_with_dut(cd, false, &status);
  16154. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16155. __func__, cd->startup_status, status);
  16156. if (rc == -ENODEV) {
  16157. pt_debug(cd->dev, DL_ERROR,
  16158. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16159. /* For PtSBC don't error out, allow TTDL to stay up */
  16160. rc = -EPROBE_DEFER;
  16161. goto error_after_startup;
  16162. }
  16163. /* Suspend scanning until probe is complete to avoid asyc touches */
  16164. pt_pip_suspend_scanning_(cd);
  16165. if (cd->hw_detected) {
  16166. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16167. __func__);
  16168. rc = add_sysfs_interfaces(dev);
  16169. if (rc < 0) {
  16170. pt_debug(dev, DL_ERROR,
  16171. "%s: Error, fail sysfs init\n", __func__);
  16172. goto error_after_startup;
  16173. }
  16174. } else {
  16175. pt_debug(dev, DL_ERROR,
  16176. "%s: No HW detected, sysfs interfaces not added\n",
  16177. __func__);
  16178. }
  16179. skip_enum:
  16180. pm_runtime_put_sync(dev);
  16181. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16182. rc = pt_mt_probe(dev);
  16183. if (rc < 0) {
  16184. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16185. __func__);
  16186. goto error_after_sysfs_create;
  16187. }
  16188. rc = pt_btn_probe(dev);
  16189. if (rc < 0) {
  16190. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16191. __func__);
  16192. goto error_after_startup_mt;
  16193. }
  16194. pt_probe_modules(cd);
  16195. #ifdef CONFIG_HAS_EARLYSUSPEND
  16196. pt_setup_early_suspend(cd);
  16197. #elif defined(CONFIG_DRM)
  16198. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16199. pt_setup_drm_notifier(cd);
  16200. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16201. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16202. #elif defined(CONFIG_FB)
  16203. pt_setup_fb_notifier(cd);
  16204. #endif
  16205. #ifdef NEED_SUSPEND_NOTIFIER
  16206. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16207. register_pm_notifier(&cd->pm_notifier);
  16208. #endif
  16209. pt_pip_resume_scanning_(cd);
  16210. mutex_lock(&cd->system_lock);
  16211. cd->startup_status |= status;
  16212. cd->core_probe_complete = 1;
  16213. mutex_unlock(&cd->system_lock);
  16214. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16215. __func__);
  16216. return 0;
  16217. error_after_startup_mt:
  16218. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16219. pt_mt_release(dev);
  16220. error_after_sysfs_create:
  16221. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16222. pm_runtime_disable(dev);
  16223. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16224. device_wakeup_disable(dev);
  16225. #endif
  16226. device_init_wakeup(dev, 0);
  16227. cancel_work_sync(&cd->enum_work);
  16228. pt_stop_wd_timer(cd);
  16229. pt_free_si_ptrs(cd);
  16230. remove_sysfs_interfaces(dev);
  16231. error_after_startup:
  16232. pr_err("%s PARADE error_after_startup\n", __func__);
  16233. del_timer(&cd->watchdog_timer);
  16234. if (cd->cpdata->setup_irq)
  16235. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16236. error_setup_irq:
  16237. error_detect:
  16238. if (cd->cpdata->init)
  16239. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16240. if (cd->cpdata->setup_power)
  16241. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16242. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16243. error_enable_regulator:
  16244. pt_del_core(dev);
  16245. dev_set_drvdata(dev, NULL);
  16246. pt_enable_regulator(cd, false);
  16247. error_get_regulator:
  16248. pt_get_regulator(cd, false);
  16249. error_alloc_data:
  16250. kfree(cd);
  16251. error_no_pdata:
  16252. pr_err("%s failed.\n", __func__);
  16253. return rc;
  16254. }
  16255. EXPORT_SYMBOL_GPL(pt_probe);
  16256. /*******************************************************************************
  16257. * FUNCTION: pt_release
  16258. *
  16259. * SUMMARY: This function does the following cleanup:
  16260. * - Releases all probed modules
  16261. * - Stops the watchdog
  16262. * - Cancels all pending work tasks
  16263. * - Removes all created sysfs nodes
  16264. * - Removes all created debugfs nodes
  16265. * - Frees the IRQ
  16266. * - Deletes the core
  16267. * - Frees all pointers and HID reports
  16268. *
  16269. * RETURN:
  16270. * 0 = success
  16271. * !0 = failure
  16272. *
  16273. * PARAMETERS:
  16274. * *cd - pointer to the core data structure
  16275. ******************************************************************************/
  16276. int pt_release(struct pt_core_data *cd)
  16277. {
  16278. struct device *dev = cd->dev;
  16279. /*
  16280. * Suspend the device before freeing the startup_work and stopping
  16281. * the watchdog since sleep function restarts watchdog on failure
  16282. */
  16283. pm_runtime_suspend(dev);
  16284. pm_runtime_disable(dev);
  16285. /*
  16286. * Any 'work' that can trigger a new thread should be canceled first.
  16287. * The watchdog is also stopped again because another thread could have
  16288. * restarted it. The irq_work is cancelled last because it is used for
  16289. * all I2C/SPI communication.
  16290. */
  16291. pt_stop_wd_timer(cd);
  16292. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16293. cancel_work_sync(&cd->ttdl_restart_work);
  16294. cancel_work_sync(&cd->enum_work);
  16295. cancel_work_sync(&cd->resume_offload_work);
  16296. cancel_work_sync(&cd->suspend_offload_work);
  16297. cancel_work_sync(&cd->resume_work);
  16298. cancel_work_sync(&cd->suspend_work);
  16299. destroy_workqueue(cd->pt_workqueue);
  16300. pt_stop_wd_timer(cd);
  16301. pt_release_modules(cd);
  16302. pt_proximity_release(dev);
  16303. pt_btn_release(dev);
  16304. pt_mt_release(dev);
  16305. #ifdef CONFIG_HAS_EARLYSUSPEND
  16306. unregister_early_suspend(&cd->es);
  16307. #elif defined(CONFIG_DRM)
  16308. if (active_panel)
  16309. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16310. #elif defined(CONFIG_FB)
  16311. fb_unregister_client(&cd->fb_notifier);
  16312. #endif
  16313. #ifdef NEED_SUSPEND_NOTIFIER
  16314. unregister_pm_notifier(&cd->pm_notifier);
  16315. #endif
  16316. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16317. device_wakeup_disable(dev);
  16318. #endif
  16319. device_init_wakeup(dev, 0);
  16320. #ifdef TTHE_TUNER_SUPPORT
  16321. mutex_lock(&cd->tthe_lock);
  16322. cd->tthe_exit = 1;
  16323. wake_up(&cd->wait_q);
  16324. mutex_unlock(&cd->tthe_lock);
  16325. debugfs_remove(cd->tthe_debugfs);
  16326. #endif
  16327. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16328. remove_sysfs_interfaces(dev);
  16329. disable_irq_nosync(cd->irq);
  16330. if (cd->cpdata->setup_irq)
  16331. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16332. if (cd->cpdata->init)
  16333. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16334. if (cd->cpdata->setup_power)
  16335. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16336. dev_set_drvdata(dev, NULL);
  16337. pt_del_core(dev);
  16338. pt_enable_regulator(cd, false);
  16339. pt_get_regulator(cd, false);
  16340. pt_free_si_ptrs(cd);
  16341. kfree(cd);
  16342. return 0;
  16343. }
  16344. EXPORT_SYMBOL_GPL(pt_release);
  16345. MODULE_LICENSE("GPL");
  16346. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16347. MODULE_AUTHOR("Parade Technologies <[email protected]>");