pt_core.c 532 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492
  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. #if defined(CONFIG_PANEL_NOTIFIER)
  35. #include <linux/soc/qcom/panel_event_notifier.h>
  36. #endif
  37. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  38. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  39. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  40. #define FT_VTG_MIN_UV 2800000
  41. #define FT_VTG_MAX_UV 2800000
  42. #define FT_I2C_VTG_MIN_UV 1800000
  43. #define FT_I2C_VTG_MAX_UV 1800000
  44. #define PWR_SUSPEND_LOAD_UA 165
  45. #define I2C_SUSPEND_LOAD_UA 100
  46. #define PWR_ACTIVE_LOAD_MA 12000
  47. #define I2C_ACTIVE_LOAD_MA 30000
  48. #define PT_CORE_STARTUP_RETRY_COUNT 3
  49. #define PT_STATUS_STR_LEN (50)
  50. #define PT_DATA_SIZE (2 * 256)
  51. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  52. static struct drm_panel *active_panel;
  53. #endif
  54. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  55. #define ENABLE_I2C_REG_ONLY
  56. enum core_states {
  57. STATE_NONE,
  58. STATE_RESUME,
  59. STATE_SUSPEND
  60. };
  61. #ifdef ENABLE_I2C_REG_ONLY
  62. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  63. #endif
  64. static int pt_device_exit(struct i2c_client *client);
  65. int pt_device_entry(struct device *dev,
  66. u16 irq, size_t xfer_buf_size);
  67. static const char *pt_driver_core_name = PT_CORE_NAME;
  68. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  69. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  70. enum core_states pt_core_state = STATE_NONE;
  71. struct pt_hid_field {
  72. int report_count;
  73. int report_size;
  74. int size; /* report_count * report_size */
  75. int offset;
  76. int data_type;
  77. int logical_min;
  78. int logical_max;
  79. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  80. u32 usage_page;
  81. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  82. struct pt_hid_report *report;
  83. bool record_field;
  84. };
  85. struct pt_hid_report {
  86. u8 id;
  87. u8 type;
  88. int size;
  89. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  90. int num_fields;
  91. int record_field_index;
  92. int header_size;
  93. int record_size;
  94. u32 usage_page;
  95. };
  96. struct atten_node {
  97. struct list_head node;
  98. char *id;
  99. struct device *dev;
  100. int (*func)(struct device *dev);
  101. int mode;
  102. };
  103. struct param_node {
  104. struct list_head node;
  105. u8 id;
  106. u32 value;
  107. u8 size;
  108. };
  109. struct module_node {
  110. struct list_head node;
  111. struct pt_module *module;
  112. void *data;
  113. };
  114. struct pt_hid_cmd {
  115. u8 opcode;
  116. u8 report_type;
  117. union {
  118. u8 report_id;
  119. u8 power_state;
  120. };
  121. u8 has_data_register;
  122. size_t write_length;
  123. u8 *write_buf;
  124. u8 *read_buf;
  125. u8 wait_interrupt;
  126. u8 reset_cmd;
  127. u16 timeout_ms;
  128. };
  129. struct pt_hid_output {
  130. u8 cmd_type;
  131. u16 length;
  132. u8 command_code;
  133. size_t write_length;
  134. u8 *write_buf;
  135. u8 novalidate;
  136. u8 reset_expected;
  137. u16 timeout_ms;
  138. };
  139. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  140. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  141. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  142. #define CREATE_PIP1_FW_CMD(command) \
  143. .cmd_type = PIP1_CMD_TYPE_FW, \
  144. .command_code = command
  145. #define CREATE_PIP1_BL_CMD(command) \
  146. .cmd_type = PIP1_CMD_TYPE_BL, \
  147. .command_code = command
  148. #define PT_MAX_PR_BUF_SIZE 2048
  149. /*******************************************************************************
  150. * FUNCTION: pt_pr_buf
  151. *
  152. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  153. *
  154. * RETURN: Void
  155. *
  156. * PARAMETERS:
  157. * *dev - pointer to Device structure
  158. * debug_level - requested debug level to print at
  159. * *buf - pointer to buffer to print
  160. * buf_len - size of buf
  161. * *data_name - Descriptive name of data prefixed to data
  162. ******************************************************************************/
  163. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  164. u16 buf_len, const char *data_name)
  165. {
  166. struct pt_core_data *cd = dev_get_drvdata(dev);
  167. int i;
  168. ssize_t pr_buf_index = 0;
  169. int max_size;
  170. /* only proceed if valid debug level and there is data to print */
  171. if (debug_level <= cd->debug_level && buf_len > 0) {
  172. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  173. if (!pr_buf)
  174. return;
  175. /*
  176. * With a space each printed char takes 3 bytes, subtract
  177. * the length of the data_name prefix as well as 11 bytes
  178. * for the " [0..xxx]: " printed before the data.
  179. */
  180. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  181. /* Ensure pr_buf_index stays within the 1018 size */
  182. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  183. data_name);
  184. for (i = 0; i < buf_len && i < max_size; i++)
  185. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  186. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  187. pt_debug(dev, debug_level, "%s\n", pr_buf);
  188. kfree(pr_buf);
  189. }
  190. }
  191. EXPORT_SYMBOL_GPL(pt_pr_buf);
  192. #ifdef TTHE_TUNER_SUPPORT
  193. /*******************************************************************************
  194. * FUNCTION: tthe_print
  195. *
  196. * SUMMARY: Format data name and time stamp as the header and format the
  197. * content of input buffer with hex base to "tthe_buf". And then wake up event
  198. * semaphore for tthe debugfs node.
  199. *
  200. * RETURN:
  201. * 0 = success
  202. * !0 = failure
  203. *
  204. * PARAMETERS:
  205. * *cd - pointer to core data
  206. * *buf - pointer to input buffer
  207. * buf_len - size of input buffer
  208. * *data_name - pointer to data name
  209. ******************************************************************************/
  210. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  211. const u8 *data_name)
  212. {
  213. int name_len = strlen(data_name);
  214. int i, n;
  215. u8 *p;
  216. int remain;
  217. u8 data_name_with_time_stamp[100];
  218. /* Prepend timestamp, if requested, to data_name */
  219. if (cd->show_timestamp) {
  220. scnprintf(data_name_with_time_stamp,
  221. sizeof(data_name_with_time_stamp),
  222. "[%u] %s", pt_get_time_stamp(), data_name);
  223. data_name = data_name_with_time_stamp;
  224. name_len = strlen(data_name);
  225. }
  226. mutex_lock(&cd->tthe_lock);
  227. if (!cd->tthe_buf)
  228. goto exit;
  229. /* Add 1 due to the '\n' that is appended at the end */
  230. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  231. goto exit;
  232. if (name_len + buf_len == 0)
  233. goto exit;
  234. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  235. if (remain < name_len)
  236. name_len = remain;
  237. p = cd->tthe_buf + cd->tthe_buf_len;
  238. memcpy(p, data_name, name_len);
  239. cd->tthe_buf_len += name_len;
  240. p += name_len;
  241. remain -= name_len;
  242. *p = 0;
  243. for (i = 0; i < buf_len; i++) {
  244. n = scnprintf(p, remain, "%02X ", buf[i]);
  245. if (n <= 0)
  246. break;
  247. p += n;
  248. remain -= n;
  249. cd->tthe_buf_len += n;
  250. }
  251. n = scnprintf(p, remain, "\n");
  252. cd->tthe_buf_len += n;
  253. exit:
  254. wake_up(&cd->wait_q);
  255. mutex_unlock(&cd->tthe_lock);
  256. return 0;
  257. }
  258. /*******************************************************************************
  259. * FUNCTION: _pt_request_tthe_print
  260. *
  261. * SUMMARY: Function pointer included in core_cmds to allow other modules
  262. * to request to print data to the "tthe_buffer".
  263. *
  264. * RETURN:
  265. * 0 = success
  266. * !0 = failure
  267. *
  268. * PARAMETERS:
  269. * *dev - pointer to device structure
  270. ******************************************************************************/
  271. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  272. int buf_len, const u8 *data_name)
  273. {
  274. struct pt_core_data *cd = dev_get_drvdata(dev);
  275. return tthe_print(cd, buf, buf_len, data_name);
  276. }
  277. #endif
  278. /*******************************************************************************
  279. * FUNCTION: pt_platform_detect_read
  280. *
  281. * SUMMARY: To be passed to platform dectect function to perform a read
  282. * operation.
  283. *
  284. * RETURN:
  285. * 0 = success
  286. * !0 = failure
  287. *
  288. * PARAMETERS:
  289. * *dev - pointer to Device structure
  290. * *buf - pointer to buffer where the data read will be stored
  291. * size - size to be read
  292. ******************************************************************************/
  293. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  294. {
  295. struct pt_core_data *cd = dev_get_drvdata(dev);
  296. return pt_adap_read_default(cd, buf, size);
  297. }
  298. /*******************************************************************************
  299. * FUNCTION: pt_add_parameter
  300. *
  301. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  302. * On every reset of the DUT this linked list is traversed and all
  303. * parameters in it are restored to the DUT.
  304. *
  305. * RETURN:
  306. * 0 = success
  307. * !0 = failure
  308. *
  309. * PARAMETERS:
  310. * *cd - pointer to core data
  311. * param_id - parameter ID to add
  312. * param_value - Value corresponding to the ID
  313. * param_size - Size of param_value
  314. ******************************************************************************/
  315. static int pt_add_parameter(struct pt_core_data *cd,
  316. u8 param_id, u32 param_value, u8 param_size)
  317. {
  318. struct param_node *param, *param_new;
  319. /* Check if parameter already exists in the list */
  320. spin_lock(&cd->spinlock);
  321. list_for_each_entry(param, &cd->param_list, node) {
  322. if (param->id == param_id) {
  323. /* Update parameter */
  324. param->value = param_value;
  325. pt_debug(cd->dev, DL_INFO,
  326. "%s: Update parameter id:%d value:%d size:%d\n",
  327. __func__, param_id, param_value, param_size);
  328. goto exit_unlock;
  329. }
  330. }
  331. spin_unlock(&cd->spinlock);
  332. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  333. if (!param_new)
  334. return -ENOMEM;
  335. param_new->id = param_id;
  336. param_new->value = param_value;
  337. param_new->size = param_size;
  338. pt_debug(cd->dev, DL_INFO,
  339. "%s: Add parameter id:%d value:%d size:%d\n",
  340. __func__, param_id, param_value, param_size);
  341. spin_lock(&cd->spinlock);
  342. list_add(&param_new->node, &cd->param_list);
  343. exit_unlock:
  344. spin_unlock(&cd->spinlock);
  345. return 0;
  346. }
  347. #ifdef TTDL_DIAGNOSTICS
  348. /*******************************************************************************
  349. * FUNCTION: pt_erase_parameter_list
  350. *
  351. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  352. * pairs. In some test cases this functionality is needed to ensure DUT
  353. * returns to a virgin state after a reset and no parameters are restored.
  354. *
  355. * RETURN:
  356. * 0 = success
  357. * !0 = failure
  358. *
  359. * PARAMETERS:
  360. * *cd - pointer to core data
  361. ******************************************************************************/
  362. static int pt_erase_parameter_list(struct pt_core_data *cd)
  363. {
  364. struct param_node *pos, *temp;
  365. spin_lock(&cd->spinlock);
  366. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  367. pt_debug(cd->dev, DL_INFO,
  368. "%s: Parameter Restore List - remove 0x%02x\n",
  369. __func__, pos->id);
  370. list_del(&pos->node);
  371. kfree(pos);
  372. }
  373. spin_unlock(&cd->spinlock);
  374. return 0;
  375. }
  376. /*******************************************************************************
  377. * FUNCTION: pt_count_parameter_list
  378. *
  379. * SUMMARY: Count the items in the RAM parameter restor list
  380. *
  381. * RETURN:
  382. * 0 = success
  383. * !0 = failure
  384. *
  385. * PARAMETERS:
  386. * *cd - pointer to core data
  387. ******************************************************************************/
  388. static int pt_count_parameter_list(struct pt_core_data *cd)
  389. {
  390. struct param_node *pos, *temp;
  391. int entries = 0;
  392. spin_lock(&cd->spinlock);
  393. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  394. entries++;
  395. spin_unlock(&cd->spinlock);
  396. return entries;
  397. }
  398. #endif /* TTDL_DIAGNOSTICS */
  399. /*******************************************************************************
  400. * FUNCTION: request_exclusive
  401. *
  402. * SUMMARY: Request exclusive access to the DUT
  403. *
  404. * RETURN:
  405. * 0 = success
  406. * !0 = failure
  407. *
  408. * PARAMETERS:
  409. * *cd - pointer to core data
  410. * *ownptr - pointer to device
  411. * timeout_ms - Timeout value
  412. ******************************************************************************/
  413. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  414. int timeout_ms)
  415. {
  416. int t = msecs_to_jiffies(timeout_ms);
  417. bool with_timeout = (timeout_ms != 0);
  418. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  419. __func__, timeout_ms);
  420. mutex_lock(&cd->system_lock);
  421. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  422. cd->exclusive_dev = ownptr;
  423. goto exit;
  424. }
  425. cd->exclusive_waits++;
  426. wait:
  427. mutex_unlock(&cd->system_lock);
  428. if (with_timeout) {
  429. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  430. if (IS_TMO(t)) {
  431. pt_debug(cd->dev, DL_ERROR,
  432. "%s: tmo waiting exclusive access\n", __func__);
  433. return -ETIME;
  434. }
  435. } else {
  436. wait_event(cd->wait_q, !cd->exclusive_dev);
  437. }
  438. mutex_lock(&cd->system_lock);
  439. if (cd->exclusive_dev)
  440. goto wait;
  441. cd->exclusive_dev = ownptr;
  442. cd->exclusive_waits--;
  443. exit:
  444. mutex_unlock(&cd->system_lock);
  445. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  446. __func__, ownptr);
  447. return 0;
  448. }
  449. /*******************************************************************************
  450. * FUNCTION: release_exclusive_
  451. *
  452. * SUMMARY: Release exclusive access to the DUT
  453. *
  454. * RETURN:
  455. * 0 = success
  456. *
  457. * PARAMETERS:
  458. * *cd - pointer to core data
  459. * *ownptr - pointer to device
  460. ******************************************************************************/
  461. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  462. {
  463. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  464. __func__);
  465. if (cd->exclusive_dev != ownptr)
  466. return -EINVAL;
  467. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  468. __func__, cd->exclusive_dev);
  469. cd->exclusive_dev = NULL;
  470. wake_up(&cd->wait_q);
  471. return 0;
  472. }
  473. /*******************************************************************************
  474. * FUNCTION: release_exclusive
  475. *
  476. * SUMMARY: Protected wrapper to release_exclusive_()
  477. *
  478. * RETURN:
  479. * 0 = success
  480. * !0 = failure
  481. *
  482. * PARAMETERS:
  483. * *cd - pointer to core data
  484. * *ownptr - pointer to device
  485. ******************************************************************************/
  486. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  487. {
  488. int rc;
  489. mutex_lock(&cd->system_lock);
  490. rc = release_exclusive_(cd, ownptr);
  491. mutex_unlock(&cd->system_lock);
  492. return rc;
  493. }
  494. /*******************************************************************************
  495. * FUNCTION: pt_hid_exec_cmd_
  496. *
  497. * SUMMARY: Send the HID command to the DUT
  498. *
  499. * RETURN:
  500. * 0 = success
  501. * !0 = failure
  502. *
  503. * PARAMETERS:
  504. * *cd - pointer to core data
  505. * *hid_cmd - pointer to the HID command to send
  506. ******************************************************************************/
  507. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  508. struct pt_hid_cmd *hid_cmd)
  509. {
  510. int rc = 0;
  511. u8 *cmd;
  512. u16 cmd_length;
  513. u8 cmd_offset = 0;
  514. cmd_length = 2 /* command register */
  515. + 2 /* command */
  516. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  517. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  518. + hid_cmd->write_length; /* Data length */
  519. if (cmd_length < 4 || cmd_length > PT_MAX_PIP1_MSG_SIZE)
  520. return -EPROTO;
  521. cmd = kzalloc(cmd_length, GFP_KERNEL);
  522. if (!cmd)
  523. return -ENOMEM;
  524. /* Set Command register */
  525. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  526. sizeof(cd->hid_desc.command_register));
  527. cmd_offset += sizeof(cd->hid_desc.command_register);
  528. /* Set Command */
  529. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  530. if (hid_cmd->report_id >= 0XF)
  531. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  532. else
  533. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  534. cmd_offset++;
  535. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  536. cmd_offset++;
  537. if (hid_cmd->report_id >= 0XF) {
  538. cmd[cmd_offset] = hid_cmd->report_id;
  539. cmd_offset++;
  540. }
  541. /* Set Data register */
  542. if (hid_cmd->has_data_register) {
  543. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  544. sizeof(cd->hid_desc.data_register));
  545. cmd_offset += sizeof(cd->hid_desc.data_register);
  546. }
  547. /* Set Data */
  548. if (hid_cmd->write_length && hid_cmd->write_buf) {
  549. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  550. hid_cmd->write_length);
  551. cmd_offset += hid_cmd->write_length;
  552. }
  553. pt_debug(cd->dev, DL_INFO,
  554. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  555. __func__, cmd_length, hid_cmd->report_id);
  556. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  557. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  558. hid_cmd->read_buf);
  559. if (rc)
  560. pt_debug(cd->dev, DL_ERROR,
  561. "%s: Fail pt_adap_transfer\n", __func__);
  562. kfree(cmd);
  563. return rc;
  564. }
  565. #ifdef TTDL_DIAGNOSTICS
  566. /*******************************************************************************
  567. * FUNCTION: pt_toggle_err_gpio
  568. *
  569. * SUMMARY: Toggles the pre-defined error GPIO
  570. *
  571. * RETURN: n/a
  572. *
  573. * PARAMETERS:
  574. * *cd - pointer to core data
  575. * type - type of err that occurred
  576. ******************************************************************************/
  577. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  578. {
  579. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  580. __func__, type);
  581. if (cd->err_gpio && type == cd->err_gpio_type) {
  582. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  583. gpio_direction_output(cd->err_gpio,
  584. !gpio_get_value(cd->err_gpio));
  585. }
  586. }
  587. /*******************************************************************************
  588. * FUNCTION: _pt_request_toggle_err_gpio
  589. *
  590. * SUMMARY: Function pointer included in core_cmds to allow other modules
  591. * to request to toggle the err_gpio
  592. *
  593. * RETURN: n/a
  594. *
  595. * PARAMETERS:
  596. * *cd - pointer to core data
  597. * type - type of err that occurred
  598. ******************************************************************************/
  599. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  600. {
  601. struct pt_core_data *cd = dev_get_drvdata(dev);
  602. pt_toggle_err_gpio(cd, type);
  603. }
  604. #endif /* TTDL_DIAGNOSTICS */
  605. /*******************************************************************************
  606. * FUNCTION: pt_hid_exec_cmd_and_wait_
  607. *
  608. * SUMMARY: Send the HID command to the DUT and wait for the response
  609. *
  610. * RETURN:
  611. * 0 = success
  612. * !0 = failure
  613. *
  614. * PARAMETERS:
  615. * *cd - pointer to core data
  616. * *hid_cmd - pointer to the HID command to send
  617. ******************************************************************************/
  618. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  619. struct pt_hid_cmd *hid_cmd)
  620. {
  621. int rc = 0;
  622. int t;
  623. u16 timeout_ms;
  624. int *cmd_state;
  625. if (hid_cmd->reset_cmd)
  626. cmd_state = &cd->hid_reset_cmd_state;
  627. else
  628. cmd_state = &cd->hid_cmd_state;
  629. if (hid_cmd->wait_interrupt) {
  630. mutex_lock(&cd->system_lock);
  631. *cmd_state = 1;
  632. mutex_unlock(&cd->system_lock);
  633. }
  634. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  635. if (rc) {
  636. if (hid_cmd->wait_interrupt)
  637. goto error;
  638. goto exit;
  639. }
  640. if (!hid_cmd->wait_interrupt)
  641. goto exit;
  642. if (hid_cmd->timeout_ms)
  643. timeout_ms = hid_cmd->timeout_ms;
  644. else
  645. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  646. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  647. msecs_to_jiffies(timeout_ms));
  648. if (IS_TMO(t)) {
  649. #ifdef TTDL_DIAGNOSTICS
  650. cd->bus_transmit_error_count++;
  651. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  652. #endif /* TTDL_DIAGNOSTICS */
  653. pt_debug(cd->dev, DL_ERROR,
  654. "%s: HID output cmd execution timed out\n",
  655. __func__);
  656. rc = -ETIME;
  657. goto error;
  658. }
  659. goto exit;
  660. error:
  661. mutex_lock(&cd->system_lock);
  662. *cmd_state = 0;
  663. mutex_unlock(&cd->system_lock);
  664. exit:
  665. return rc;
  666. }
  667. /*******************************************************************************
  668. * FUNCTION: pt_hid_cmd_reset_
  669. *
  670. * SUMMARY: Send the HID RESET command to the DUT
  671. *
  672. * RETURN:
  673. * 0 = success
  674. * !0 = failure
  675. *
  676. * PARAMETERS:
  677. * *cd - pointer to core data
  678. ******************************************************************************/
  679. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  680. {
  681. struct pt_hid_cmd hid_cmd = {
  682. .opcode = HID_CMD_RESET,
  683. .wait_interrupt = 1,
  684. .reset_cmd = 1,
  685. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  686. };
  687. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  688. }
  689. /*******************************************************************************
  690. * FUNCTION: pt_hid_cmd_reset
  691. *
  692. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  693. * access.
  694. *
  695. * RETURN:
  696. * 0 = success
  697. * !0 = failure
  698. *
  699. * PARAMETERS:
  700. * *cd - pointer to core data
  701. ******************************************************************************/
  702. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  703. {
  704. int rc = 0;
  705. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  706. if (rc < 0) {
  707. pt_debug(cd->dev, DL_ERROR,
  708. "%s: fail get exclusive ex=%p own=%p\n",
  709. __func__, cd->exclusive_dev, cd->dev);
  710. return rc;
  711. }
  712. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  713. rc = pt_hid_cmd_reset_(cd);
  714. if (release_exclusive(cd, cd->dev) < 0)
  715. pt_debug(cd->dev, DL_ERROR,
  716. "%s: fail to release exclusive\n", __func__);
  717. return rc;
  718. }
  719. /*******************************************************************************
  720. * FUNCTION: pt_hid_cmd_set_power_
  721. *
  722. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  723. *
  724. * RETURN:
  725. * 0 = success
  726. * !0 = failure
  727. *
  728. * PARAMETERS:
  729. * *cd - pointer to core data
  730. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  731. ******************************************************************************/
  732. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  733. u8 power_state)
  734. {
  735. int rc = 0;
  736. struct pt_hid_cmd hid_cmd = {
  737. .opcode = HID_CMD_SET_POWER,
  738. .wait_interrupt = 1,
  739. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  740. };
  741. hid_cmd.power_state = power_state;
  742. /* The chip won't give response if goes to Deep Standby */
  743. if (power_state == HID_POWER_STANDBY) {
  744. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  745. if (rc)
  746. pt_debug(cd->dev, DL_ERROR,
  747. "%s: Failed to set power to state:%d\n",
  748. __func__, power_state);
  749. else
  750. cd->fw_sys_mode_in_standby_state = true;
  751. return rc;
  752. }
  753. cd->fw_sys_mode_in_standby_state = false;
  754. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  755. if (rc) {
  756. pt_debug(cd->dev, DL_ERROR,
  757. "%s: Failed to set power to state:%d\n",
  758. __func__, power_state);
  759. return rc;
  760. }
  761. /* validate */
  762. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  763. || ((cd->response_buf[3] & 0x3) != power_state)
  764. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  765. rc = -EINVAL;
  766. return rc;
  767. }
  768. /*******************************************************************************
  769. * FUNCTION: pt_hid_cmd_set_power
  770. *
  771. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  772. * exclusive access.
  773. *
  774. * RETURN:
  775. * 0 = success
  776. * !0 = failure
  777. *
  778. * PARAMETERS:
  779. * *cd - pointer to core data
  780. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  781. ******************************************************************************/
  782. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  783. u8 power_state)
  784. {
  785. int rc;
  786. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  787. if (rc < 0) {
  788. pt_debug(cd->dev, DL_ERROR,
  789. "%s: fail get exclusive ex=%p own=%p\n",
  790. __func__, cd->exclusive_dev, cd->dev);
  791. return rc;
  792. }
  793. rc = pt_hid_cmd_set_power_(cd, power_state);
  794. if (release_exclusive(cd, cd->dev) < 0)
  795. pt_debug(cd->dev, DL_ERROR,
  796. "%s: fail to release exclusive\n", __func__);
  797. return rc;
  798. }
  799. static const u16 crc_table[16] = {
  800. 0x0000, 0x1021, 0x2042, 0x3063,
  801. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  802. 0x8108, 0x9129, 0xa14a, 0xb16b,
  803. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  804. };
  805. /*******************************************************************************
  806. * FUNCTION: _pt_compute_crc
  807. *
  808. * SUMMARY: Calculate CRC by CRC table.
  809. *
  810. * RETURN:
  811. * CRC calculation result
  812. *
  813. * PARAMETERS:
  814. * *buf - pointer to the data array to be calculated
  815. * size - size of data array
  816. ******************************************************************************/
  817. static u16 _pt_compute_crc(u8 *buf, u32 size)
  818. {
  819. u16 remainder = 0xFFFF;
  820. u16 xor_mask = 0x0000;
  821. u32 index;
  822. u32 byte_value;
  823. u32 table_index;
  824. u32 crc_bit_width = sizeof(u16) * 8;
  825. /* Divide the message by polynomial, via the table. */
  826. for (index = 0; index < size; index++) {
  827. byte_value = buf[index];
  828. table_index = ((byte_value >> 4) & 0x0F)
  829. ^ (remainder >> (crc_bit_width - 4));
  830. remainder = crc_table[table_index] ^ (remainder << 4);
  831. table_index = (byte_value & 0x0F)
  832. ^ (remainder >> (crc_bit_width - 4));
  833. remainder = crc_table[table_index] ^ (remainder << 4);
  834. }
  835. /* Perform the final remainder CRC. */
  836. return remainder ^ xor_mask;
  837. }
  838. u16 ccitt_Table[] = {
  839. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  840. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  841. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  842. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  843. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  844. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  845. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  846. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  847. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  848. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  849. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  850. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  851. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  852. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  853. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  854. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  855. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  856. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  857. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  858. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  859. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  860. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  861. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  862. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  863. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  864. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  865. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  866. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  867. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  868. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  869. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  870. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  871. };
  872. /*******************************************************************************
  873. * FUNCTION: crc_ccitt_calculate
  874. *
  875. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  876. *
  877. * RETURN:
  878. * CRC calculation result
  879. *
  880. * PARAMETERS:
  881. * *q - pointer to the data array to be calculated
  882. * len - size of data array
  883. ******************************************************************************/
  884. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  885. {
  886. unsigned short crc = 0xffff;
  887. while (len-- > 0)
  888. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  889. return crc;
  890. }
  891. /*******************************************************************************
  892. * FUNCTION: pt_pip2_cmd_calculate_crc
  893. *
  894. * SUMMARY: Calculate the CRC of a command packet
  895. *
  896. * RETURN: void
  897. *
  898. * PARAMETERS:
  899. * *cmd - pointer to command data
  900. * extra_bytes - Extra bytes included in command length
  901. ******************************************************************************/
  902. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  903. u8 extra_bytes)
  904. {
  905. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  906. unsigned short crc;
  907. buf[0] = cmd->len & 0xff;
  908. buf[1] = (cmd->len & 0xff00) >> 8;
  909. buf[2] = cmd->seq;
  910. buf[3] = cmd->id;
  911. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  912. /* Calculate the CRC for the first 4 bytes above and the data payload */
  913. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  914. cmd->crc[0] = (crc & 0xff00) >> 8;
  915. cmd->crc[1] = (crc & 0xff);
  916. }
  917. /*******************************************************************************
  918. * FUNCTION: pt_pip2_get_next_cmd_seq
  919. *
  920. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  921. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  922. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  923. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  924. * increment, just use the forced value.
  925. *
  926. * RETURN: Next command sequence number [0x08-0x0F]
  927. *
  928. * PARAMETERS:
  929. * *cd - pointer to core data
  930. ******************************************************************************/
  931. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  932. {
  933. #ifdef TTDL_DIAGNOSTICS
  934. if (cd->force_pip2_seq <= 0x07) {
  935. cd->pip2_cmd_tag_seq++;
  936. if (cd->pip2_cmd_tag_seq > 0x0F)
  937. cd->pip2_cmd_tag_seq = 0x08;
  938. } else {
  939. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  940. }
  941. #else
  942. cd->pip2_cmd_tag_seq++;
  943. if (cd->pip2_cmd_tag_seq > 0x0F)
  944. cd->pip2_cmd_tag_seq = 0x08;
  945. #endif
  946. return cd->pip2_cmd_tag_seq;
  947. }
  948. /*
  949. * Following macros are to define the response time (the interval between PIP2
  950. * command finishes sending and INT pin falls). The unit is in microsecond.
  951. * It has different time settings between the solution GPIO polling and Bus
  952. * polling due to the considration for system load.
  953. */
  954. #ifdef PT_POLL_RESP_BY_BUS
  955. #define POLL_RETRY_DEFAULT_INTERVAL 50
  956. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  957. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  958. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  959. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  960. #else
  961. #define POLL_RETRY_DEFAULT_INTERVAL 20
  962. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  963. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  964. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  965. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  966. #endif
  967. /*
  968. * id: the command id defined in PIP2
  969. * response_len: the (maximum) length of response.
  970. * response_time_min: minimum response time in microsecond
  971. * response_time_max: maximum response time in microsecond
  972. */
  973. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  974. {.id = PIP2_CMD_ID_PING,
  975. .response_len = 255,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_STATUS,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_CTRL,
  983. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  984. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  985. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  986. {.id = PIP2_CMD_ID_CONFIG,
  987. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_CLEAR,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  992. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_RESET,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  998. {.id = PIP2_CMD_ID_VERSION,
  999. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_FILE_OPEN,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  1004. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_FILE_CLOSE,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_FILE_READ,
  1011. .response_len = 255,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_FILE_WRITE,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1016. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1022. {.id = PIP2_CMD_ID_FLASH_INFO,
  1023. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1024. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1025. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1026. {.id = PIP2_CMD_ID_EXECUTE,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_ID_READ_GPIO,
  1039. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1042. {.id = PIP2_CMD_EXECUTE_SCAN,
  1043. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1044. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1045. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1046. {.id = PIP2_CMD_SET_PARAMETER,
  1047. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1048. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1049. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1050. {.id = PIP2_CMD_GET_PARAMETER,
  1051. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1052. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1053. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1054. {.id = PIP2_CMD_SET_DDI_REG,
  1055. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1056. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1057. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1058. {.id = PIP2_CMD_GET_DDI_REG,
  1059. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1060. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1061. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1062. {.id = PIP2_CMD_ID_END,
  1063. .response_len = 255,
  1064. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1065. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1066. };
  1067. /*******************************************************************************
  1068. * FUNCTION: pt_pip2_get_cmd_response_len
  1069. *
  1070. * SUMMARY: Gets the expected response length based on the command ID
  1071. *
  1072. * RETURN: Expected response length
  1073. *
  1074. * PARAMETERS:
  1075. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1076. ******************************************************************************/
  1077. static int pt_pip2_get_cmd_response_len(u8 id)
  1078. {
  1079. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1080. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1081. p++;
  1082. if (p->id != PIP2_CMD_ID_END)
  1083. return p->response_len;
  1084. else
  1085. return -EPERM;
  1086. }
  1087. /*******************************************************************************
  1088. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1089. *
  1090. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1091. * finishes sending and INT pin falls) based on the command ID
  1092. *
  1093. * RETURN: Estimated minimum response time in microsecond
  1094. *
  1095. * PARAMETERS:
  1096. * id - Command ID
  1097. ******************************************************************************/
  1098. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1099. {
  1100. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1101. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1102. p++;
  1103. if (p->id != PIP2_CMD_ID_END)
  1104. return p->response_time_min;
  1105. else
  1106. return PIP2_RESP_DEFAULT_TIME_MIN;
  1107. }
  1108. /*******************************************************************************
  1109. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1110. *
  1111. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1112. * finishes sending and INT pin falls) based on the command ID
  1113. *
  1114. * RETURN: Estimated maximum response time in microsecond
  1115. *
  1116. * PARAMETERS:
  1117. * id - Command ID
  1118. ******************************************************************************/
  1119. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1120. {
  1121. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1122. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1123. p++;
  1124. if (p->id != PIP2_CMD_ID_END)
  1125. return p->response_time_max;
  1126. else
  1127. return PIP2_RESP_DEFAULT_TIME_MAX;
  1128. }
  1129. /*******************************************************************************
  1130. * FUNCTION: pt_pip2_validate_response
  1131. *
  1132. * SUMMARY: Validate the response of PIP2 command.
  1133. *
  1134. * RETURN:
  1135. * 0 = success
  1136. * !0 = failure
  1137. *
  1138. * PARAMETERS:
  1139. * *cd - pointer to core data
  1140. * *pip2_cmd - pointer to PIP2 command to send
  1141. * *read_buf - pointer to response buffer
  1142. * actual_read_len - actual read length of the response
  1143. ******************************************************************************/
  1144. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1145. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1146. u16 actual_read_len)
  1147. {
  1148. int rc = 0;
  1149. u8 response_seq = 0;
  1150. u8 reserved_bits = 0;
  1151. u8 cmd_id = 0;
  1152. u8 response_bit = 0;
  1153. unsigned short calc_crc = 0;
  1154. unsigned short resp_crc = 0;
  1155. /* Verify the length of response buffer */
  1156. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1157. pt_debug(cd->dev, DL_ERROR,
  1158. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1159. __func__, pip2_cmd->id, actual_read_len);
  1160. rc = -EINVAL;
  1161. goto exit;
  1162. }
  1163. /* Verify the CRC */
  1164. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1165. resp_crc = read_buf[actual_read_len - 2] << 8;
  1166. resp_crc |= read_buf[actual_read_len - 1];
  1167. if (resp_crc != calc_crc) {
  1168. pt_debug(cd->dev, DL_ERROR,
  1169. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1170. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1171. #ifdef TTDL_DIAGNOSTICS
  1172. cd->pip2_crc_error_count++;
  1173. #endif /* TTDL_DIAGNOSTICS */
  1174. rc = -EINVAL;
  1175. goto exit;
  1176. }
  1177. /* Verify the response bit is set */
  1178. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1179. if (!response_bit) {
  1180. pt_debug(cd->dev, DL_ERROR,
  1181. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1182. __func__, pip2_cmd->id, response_bit);
  1183. rc = -EINVAL;
  1184. goto exit;
  1185. }
  1186. /* Verify the command ID matches from command to response */
  1187. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1188. if (cmd_id != pip2_cmd->id) {
  1189. pt_debug(cd->dev, DL_ERROR,
  1190. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1191. __func__, pip2_cmd->id, cmd_id);
  1192. rc = -EINVAL;
  1193. goto exit;
  1194. }
  1195. /* Verify the SEQ number matches from command to response */
  1196. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1197. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1198. pt_debug(cd->dev, DL_ERROR,
  1199. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1200. __func__, pip2_cmd->id,
  1201. pip2_cmd->seq, response_seq);
  1202. rc = -EINVAL;
  1203. goto exit;
  1204. }
  1205. /* Verify the reserved bits are 0 */
  1206. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1207. if (reserved_bits)
  1208. pt_debug(cd->dev, DL_WARN,
  1209. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1210. __func__, pip2_cmd->id, reserved_bits);
  1211. exit:
  1212. if (rc)
  1213. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1214. "PIP RSP:");
  1215. return rc;
  1216. }
  1217. /*******************************************************************************
  1218. * FUNCTION: pt_hid_output_validate_bl_response
  1219. *
  1220. * SUMMARY: Validate the response of bootloader command.
  1221. *
  1222. * RETURN:
  1223. * 0 = success
  1224. * !0 = failure
  1225. *
  1226. * PARAMETERS:
  1227. * *cd - pointer to core data
  1228. * *hid_output - pointer to hid output data structure
  1229. ******************************************************************************/
  1230. static int pt_hid_output_validate_bl_response(
  1231. struct pt_core_data *cd,
  1232. struct pt_hid_output *hid_output)
  1233. {
  1234. u16 size;
  1235. u16 crc;
  1236. u8 status;
  1237. size = get_unaligned_le16(&cd->response_buf[0]);
  1238. if (hid_output->reset_expected && !size)
  1239. return 0;
  1240. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1241. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1242. pt_debug(cd->dev, DL_ERROR,
  1243. "%s: BL output response, wrong report_id\n", __func__);
  1244. return -EPROTO;
  1245. }
  1246. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1247. pt_debug(cd->dev, DL_ERROR,
  1248. "%s: BL output response, wrong SOP\n", __func__);
  1249. return -EPROTO;
  1250. }
  1251. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1252. pt_debug(cd->dev, DL_ERROR,
  1253. "%s: BL output response, wrong EOP\n", __func__);
  1254. return -EPROTO;
  1255. }
  1256. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1257. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1258. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1259. pt_debug(cd->dev, DL_ERROR,
  1260. "%s: BL output response, wrong CRC 0x%X\n",
  1261. __func__, crc);
  1262. return -EPROTO;
  1263. }
  1264. status = cd->response_buf[5];
  1265. if (status) {
  1266. pt_debug(cd->dev, DL_ERROR,
  1267. "%s: BL output response, ERROR:%d\n",
  1268. __func__, status);
  1269. return -EPROTO;
  1270. }
  1271. return 0;
  1272. }
  1273. /*******************************************************************************
  1274. * FUNCTION: pt_hid_output_validate_app_response
  1275. *
  1276. * SUMMARY: Validate the response of application command.
  1277. *
  1278. * RETURN:
  1279. * 0 = success
  1280. * !0 = failure
  1281. *
  1282. * PARAMETERS:
  1283. * *cd - pointer to core data
  1284. * *hid_output - pointer to hid output data structure
  1285. ******************************************************************************/
  1286. static int pt_hid_output_validate_app_response(
  1287. struct pt_core_data *cd,
  1288. struct pt_hid_output *hid_output)
  1289. {
  1290. int command_code;
  1291. u16 size;
  1292. size = get_unaligned_le16(&cd->response_buf[0]);
  1293. if (hid_output->reset_expected && !size)
  1294. return 0;
  1295. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1296. != PT_PIP_NON_HID_RESPONSE_ID) {
  1297. pt_debug(cd->dev, DL_ERROR,
  1298. "%s: APP output response, wrong report_id\n", __func__);
  1299. return -EPROTO;
  1300. }
  1301. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1302. & PIP1_RESP_COMMAND_ID_MASK;
  1303. if (command_code != hid_output->command_code) {
  1304. pt_debug(cd->dev, DL_ERROR,
  1305. "%s: APP output response, wrong command_code:%X\n",
  1306. __func__, command_code);
  1307. return -EPROTO;
  1308. }
  1309. return 0;
  1310. }
  1311. /*******************************************************************************
  1312. * FUNCTION: pt_check_set_parameter
  1313. *
  1314. * SUMMARY: Check command input and response for Set Parameter command.And
  1315. * store the parameter to the list for resume work if pass the check.
  1316. *
  1317. * PARAMETERS:
  1318. * *cd - pointer to core data
  1319. * *hid_output - pointer to hid output data structure
  1320. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1321. ******************************************************************************/
  1322. static void pt_check_set_parameter(struct pt_core_data *cd,
  1323. struct pt_hid_output *hid_output, bool raw)
  1324. {
  1325. u8 *param_buf;
  1326. u32 param_value = 0;
  1327. u8 param_size;
  1328. u8 param_id;
  1329. int i = 0;
  1330. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1331. return;
  1332. /* Check command input for Set Parameter command */
  1333. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1334. && !memcmp(&hid_output->write_buf[0],
  1335. &cd->hid_desc.output_register,
  1336. sizeof(cd->hid_desc.output_register))
  1337. && hid_output->write_buf[4] ==
  1338. PT_PIP_NON_HID_COMMAND_ID
  1339. && hid_output->write_buf[6] ==
  1340. PIP1_CMD_ID_SET_PARAM)
  1341. param_buf = &hid_output->write_buf[7];
  1342. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1343. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1344. && hid_output->write_length >= 3
  1345. && hid_output->write_length <= 6)
  1346. param_buf = &hid_output->write_buf[0];
  1347. else
  1348. return;
  1349. /* Get parameter ID, size and value */
  1350. param_id = param_buf[0];
  1351. param_size = param_buf[1];
  1352. if (param_size > 4) {
  1353. pt_debug(cd->dev, DL_ERROR,
  1354. "%s: Invalid parameter size\n", __func__);
  1355. return;
  1356. }
  1357. param_buf = &param_buf[2];
  1358. while (i < param_size)
  1359. param_value += *(param_buf++) << (8 * i++);
  1360. /* Check command response for Set Parameter command */
  1361. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1362. || (cd->response_buf[4] &
  1363. PIP1_RESP_COMMAND_ID_MASK) !=
  1364. PIP1_CMD_ID_SET_PARAM
  1365. || cd->response_buf[5] != param_id
  1366. || cd->response_buf[6] != param_size) {
  1367. pt_debug(cd->dev, DL_ERROR,
  1368. "%s: Set Parameter command not successful\n",
  1369. __func__);
  1370. return;
  1371. }
  1372. pt_add_parameter(cd, param_id, param_value, param_size);
  1373. }
  1374. /*******************************************************************************
  1375. * FUNCTION: pt_check_command
  1376. *
  1377. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1378. * called here to check output command and store parameter to the list.
  1379. *
  1380. * PARAMETERS:
  1381. * *cd - pointer to core data
  1382. * *hid_output - pointer to hid output data structure
  1383. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1384. ******************************************************************************/
  1385. static void pt_check_command(struct pt_core_data *cd,
  1386. struct pt_hid_output *hid_output, bool raw)
  1387. {
  1388. pt_check_set_parameter(cd, hid_output, raw);
  1389. }
  1390. /*******************************************************************************
  1391. * FUNCTION: pt_hid_output_validate_response
  1392. *
  1393. * SUMMARY: Validate the response of application or bootloader command.
  1394. *
  1395. * RETURN:
  1396. * 0 = success
  1397. * !0 = failure
  1398. *
  1399. * PARAMETERS:
  1400. * *cd - pointer to core data
  1401. * *hid_output - pointer to hid output data structure
  1402. ******************************************************************************/
  1403. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1404. struct pt_hid_output *hid_output)
  1405. {
  1406. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1407. return pt_hid_output_validate_bl_response(cd, hid_output);
  1408. return pt_hid_output_validate_app_response(cd, hid_output);
  1409. }
  1410. /*******************************************************************************
  1411. * FUNCTION: pt_hid_send_output_user_
  1412. *
  1413. * SUMMARY: Blindly send user data to the DUT.
  1414. *
  1415. * RETURN:
  1416. * 0 = success
  1417. * !0 = failure
  1418. *
  1419. * PARAMETERS:
  1420. * *cd - pointer to core data
  1421. * *hid_output - pointer to the command to send
  1422. ******************************************************************************/
  1423. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1424. struct pt_hid_output *hid_output)
  1425. {
  1426. int rc = 0;
  1427. int cmd;
  1428. if (!hid_output->length || !hid_output->write_buf)
  1429. return -EINVAL;
  1430. if (cd->pip2_prot_active) {
  1431. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1432. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1433. } else
  1434. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1435. pt_debug(cd->dev, DL_INFO,
  1436. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1437. __func__, hid_output->length, cmd);
  1438. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1439. hid_output->length, ">>> User CMD");
  1440. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1441. hid_output->write_buf, NULL);
  1442. if (rc)
  1443. pt_debug(cd->dev, DL_ERROR,
  1444. "%s: Fail pt_adap_transfer\n", __func__);
  1445. return rc;
  1446. }
  1447. /*******************************************************************************
  1448. * FUNCTION: pt_hid_send_output_user_and_wait_
  1449. *
  1450. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1451. *
  1452. * RETURN:
  1453. * 0 = success
  1454. * !0 = failure
  1455. *
  1456. * PARAMETERS:
  1457. * *cd - pointer to core data
  1458. * *hid_output - pointer to the command to send
  1459. ******************************************************************************/
  1460. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1461. struct pt_hid_output *hid_output)
  1462. {
  1463. int rc = 0;
  1464. int t;
  1465. mutex_lock(&cd->system_lock);
  1466. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1467. mutex_unlock(&cd->system_lock);
  1468. rc = pt_hid_send_output_user_(cd, hid_output);
  1469. if (rc)
  1470. goto error;
  1471. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1472. msecs_to_jiffies(cd->pip_cmd_timeout));
  1473. if (IS_TMO(t)) {
  1474. #ifdef TTDL_DIAGNOSTICS
  1475. cd->bus_transmit_error_count++;
  1476. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1477. #endif /* TTDL_DIAGNOSTICS */
  1478. pt_debug(cd->dev, DL_ERROR,
  1479. "%s: HID output cmd execution timed out\n",
  1480. __func__);
  1481. rc = -ETIME;
  1482. goto error;
  1483. }
  1484. pt_check_command(cd, hid_output, true);
  1485. goto exit;
  1486. error:
  1487. mutex_lock(&cd->system_lock);
  1488. cd->hid_cmd_state = 0;
  1489. mutex_unlock(&cd->system_lock);
  1490. exit:
  1491. return rc;
  1492. }
  1493. /*******************************************************************************
  1494. * FUNCTION: pt_check_irq_asserted
  1495. *
  1496. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1497. * the FW can hold the INT line low ~150us after the read is complete.
  1498. * NOTE: if irq_stat is not defined this function will return false
  1499. *
  1500. * RETURN:
  1501. * true = IRQ asserted
  1502. * false = IRQ not asserted
  1503. *
  1504. * PARAMETERS:
  1505. * *cd - pointer to core data
  1506. ******************************************************************************/
  1507. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1508. {
  1509. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1510. /*
  1511. * Workaround for FW defect, CDT165308
  1512. * bl_launch app creates a glitch in IRQ line
  1513. */
  1514. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1515. && cd->cpdata->irq_stat) {
  1516. /*
  1517. * in X1S panel and GC1546 panel, the width for the INT
  1518. * glitch is about 4us,the normal INT width of response
  1519. * will last more than 200us, so use 10us delay
  1520. * for distinguish the glitch the normal INT is enough.
  1521. */
  1522. udelay(10);
  1523. }
  1524. #endif
  1525. if (cd->cpdata->irq_stat) {
  1526. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1527. == PT_IRQ_ASSERTED_VALUE) {
  1528. /* Debounce to allow FW to release INT */
  1529. usleep_range(100, 200);
  1530. }
  1531. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1532. == PT_IRQ_ASSERTED_VALUE)
  1533. return true;
  1534. else
  1535. return false;
  1536. }
  1537. return true;
  1538. }
  1539. /*******************************************************************************
  1540. * FUNCTION: pt_flush_bus
  1541. *
  1542. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1543. * Used if IRQ is found to be stuck low
  1544. *
  1545. * RETURN: Length of bytes read from bus
  1546. *
  1547. * PARAMETERS:
  1548. * *cd - pointer to core data
  1549. * flush_type - type of flush
  1550. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1551. * - PT_FLUSH_BUS_FULL_256_READ
  1552. * *read_buf - pointer to store read data
  1553. ******************************************************************************/
  1554. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1555. u8 flush_type, u8 *read_buf)
  1556. {
  1557. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1558. u16 pip_len;
  1559. int bytes_read;
  1560. int rc = 0;
  1561. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1562. rc = pt_adap_read_default(cd, buf, 2);
  1563. if (rc) {
  1564. bytes_read = 0;
  1565. goto exit;
  1566. }
  1567. pip_len = get_unaligned_le16(&buf[0]);
  1568. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1569. #ifdef TTDL_DIAGNOSTICS
  1570. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1571. #endif
  1572. bytes_read = 2;
  1573. pt_debug(cd->dev, DL_INFO,
  1574. "%s: Empty buf detected - len=0x%04X\n",
  1575. __func__, pip_len);
  1576. } else if (pip_len == 0) {
  1577. bytes_read = 0;
  1578. pt_debug(cd->dev, DL_INFO,
  1579. "%s: Sentinel detected\n", __func__);
  1580. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1581. pt_debug(cd->dev, DL_ERROR,
  1582. "%s: Illegal len=0x%04x, force %d byte read\n",
  1583. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1584. rc = pt_adap_read_default(cd, buf,
  1585. PT_MAX_PIP2_MSG_SIZE);
  1586. if (!rc)
  1587. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1588. else
  1589. bytes_read = 0;
  1590. } else {
  1591. pt_debug(cd->dev, DL_INFO,
  1592. "%s: Flush read of %d bytes...\n",
  1593. __func__, pip_len);
  1594. rc = pt_adap_read_default(cd, buf, pip_len);
  1595. if (!rc)
  1596. bytes_read = pip_len;
  1597. else
  1598. bytes_read = 0;
  1599. }
  1600. } else {
  1601. pt_debug(cd->dev, DL_INFO,
  1602. "%s: Forced flush of max %d bytes...\n",
  1603. __func__, PT_MAX_PIP2_MSG_SIZE);
  1604. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1605. if (!rc)
  1606. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1607. else
  1608. bytes_read = 0;
  1609. }
  1610. if (read_buf && (bytes_read > 3))
  1611. memcpy(read_buf, buf, bytes_read);
  1612. exit:
  1613. return bytes_read;
  1614. }
  1615. /*******************************************************************************
  1616. * FUNCTION: pt_flush_bus_if_irq_asserted
  1617. *
  1618. * SUMMARY: This function will flush the active bus if the INT is found to be
  1619. * asserted.
  1620. *
  1621. * RETURN: bytes cleared from bus
  1622. *
  1623. * PARAMETERS:
  1624. * *cd - pointer the core data structure
  1625. * flush_type - type of flush
  1626. * - PT_FLUSH_BUS_BASED_ON_LEN
  1627. * - PT_FLUSH_BUS_FULL_256_READ
  1628. ******************************************************************************/
  1629. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1630. {
  1631. int count = 0;
  1632. int bytes_read = 0;
  1633. while (pt_check_irq_asserted(cd) && count < 5) {
  1634. count++;
  1635. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1636. if (bytes_read) {
  1637. pt_debug(cd->dev, DL_WARN,
  1638. "%s: Cleared %d bytes off bus\n",
  1639. __func__, bytes_read);
  1640. }
  1641. }
  1642. if (pt_check_irq_asserted(cd)) {
  1643. pt_debug(cd->dev, DL_ERROR,
  1644. "%s: IRQ still asserted, %d bytes read\n",
  1645. __func__, bytes_read);
  1646. } else {
  1647. pt_debug(cd->dev, DL_INFO,
  1648. "%s: IRQ cleared, %d bytes read\n",
  1649. __func__, bytes_read);
  1650. }
  1651. return bytes_read;
  1652. }
  1653. /*******************************************************************************
  1654. * FUNCTION: pt_hid_send_output_
  1655. *
  1656. * SUMMARY: Send a touch application command to the DUT
  1657. *
  1658. * RETURN:
  1659. * 0 = success
  1660. * !0 = failure
  1661. *
  1662. * PARAMETERS:
  1663. * *cd - pointer to core data
  1664. * *hid_output - pointer to the command to send
  1665. ******************************************************************************/
  1666. static int pt_hid_send_output_(struct pt_core_data *cd,
  1667. struct pt_hid_output *hid_output)
  1668. {
  1669. int rc = 0;
  1670. u8 *cmd;
  1671. u16 length;
  1672. u16 crc;
  1673. u8 report_id;
  1674. u8 cmd_offset = 0;
  1675. u8 cmd_allocated = 0;
  1676. switch (hid_output->cmd_type) {
  1677. case PIP1_CMD_TYPE_FW:
  1678. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1679. length = 5;
  1680. break;
  1681. case PIP1_CMD_TYPE_BL:
  1682. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1683. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1684. break;
  1685. default:
  1686. return -EINVAL;
  1687. }
  1688. length += hid_output->write_length;
  1689. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1690. cmd = kzalloc(length + 2, GFP_KERNEL);
  1691. if (!cmd)
  1692. return -ENOMEM;
  1693. cmd_allocated = 1;
  1694. } else {
  1695. cmd = cd->cmd_buf;
  1696. }
  1697. /* Set Output register */
  1698. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1699. sizeof(cd->hid_desc.output_register));
  1700. cmd_offset += sizeof(cd->hid_desc.output_register);
  1701. cmd[cmd_offset++] = LOW_BYTE(length);
  1702. cmd[cmd_offset++] = HI_BYTE(length);
  1703. cmd[cmd_offset++] = report_id;
  1704. cmd[cmd_offset++] = 0x0; /* reserved */
  1705. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1706. cmd[cmd_offset++] = PIP1_BL_SOP;
  1707. cmd[cmd_offset++] = hid_output->command_code;
  1708. /* Set Data Length for bootloader */
  1709. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1710. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1711. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1712. }
  1713. /* Set Data */
  1714. if (hid_output->write_length && hid_output->write_buf) {
  1715. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1716. hid_output->write_length);
  1717. cmd_offset += hid_output->write_length;
  1718. }
  1719. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1720. crc = _pt_compute_crc(&cmd[6],
  1721. hid_output->write_length + 4);
  1722. cmd[cmd_offset++] = LOW_BYTE(crc);
  1723. cmd[cmd_offset++] = HI_BYTE(crc);
  1724. cmd[cmd_offset++] = PIP1_BL_EOP;
  1725. }
  1726. pt_debug(cd->dev, DL_INFO,
  1727. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1728. __func__, length + 2, hid_output->command_code);
  1729. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1730. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1731. if (rc)
  1732. pt_debug(cd->dev, DL_ERROR,
  1733. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1734. if (cmd_allocated)
  1735. kfree(cmd);
  1736. return rc;
  1737. }
  1738. /*******************************************************************************
  1739. * FUNCTION: pt_pip1_send_output_and_wait_
  1740. *
  1741. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1742. *
  1743. * RETURN:
  1744. * 0 = success
  1745. * !0 = failure
  1746. *
  1747. * PARAMETERS:
  1748. * *cd - pointer to core data
  1749. * *hid_output - pointer to the command to send
  1750. ******************************************************************************/
  1751. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1752. struct pt_hid_output *hid_output)
  1753. {
  1754. int rc = 0;
  1755. int t;
  1756. u16 timeout_ms;
  1757. mutex_lock(&cd->system_lock);
  1758. cd->hid_cmd_state = hid_output->command_code + 1;
  1759. mutex_unlock(&cd->system_lock);
  1760. if (hid_output->timeout_ms)
  1761. timeout_ms = hid_output->timeout_ms;
  1762. else
  1763. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1764. rc = pt_hid_send_output_(cd, hid_output);
  1765. if (rc)
  1766. goto error;
  1767. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1768. msecs_to_jiffies(timeout_ms));
  1769. if (IS_TMO(t)) {
  1770. #ifdef TTDL_DIAGNOSTICS
  1771. cd->bus_transmit_error_count++;
  1772. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1773. #endif /* TTDL_DIAGNOSTICS */
  1774. pt_debug(cd->dev, DL_ERROR,
  1775. "%s: HID output cmd execution timed out (%dms)\n",
  1776. __func__, timeout_ms);
  1777. rc = -ETIME;
  1778. goto error;
  1779. }
  1780. if (!hid_output->novalidate)
  1781. rc = pt_hid_output_validate_response(cd, hid_output);
  1782. pt_check_command(cd, hid_output, false);
  1783. goto exit;
  1784. error:
  1785. mutex_lock(&cd->system_lock);
  1786. cd->hid_cmd_state = 0;
  1787. mutex_unlock(&cd->system_lock);
  1788. exit:
  1789. return rc;
  1790. }
  1791. /*******************************************************************************
  1792. * FUNCTION: pt_hid_output_user_cmd_
  1793. *
  1794. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1795. * to the DUT waiting for the response and loading it into the read buffer
  1796. *
  1797. * RETURN:
  1798. * 0 = success
  1799. * !0 = failure
  1800. *
  1801. * PARAMETERS:
  1802. * *cd - pointer to core data
  1803. * read_len - expected read length of the response
  1804. * *read_buf - pointer to where the response will be loaded
  1805. * write_len - length of the write buffer
  1806. * *write_buf - pointer to the write buffer
  1807. * *actual_read_len - pointer to the actual amount of data read back
  1808. ******************************************************************************/
  1809. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1810. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1811. u16 *actual_read_len)
  1812. {
  1813. int rc = 0;
  1814. u16 size;
  1815. struct pt_hid_output hid_output = {
  1816. .length = write_len,
  1817. .write_buf = write_buf,
  1818. };
  1819. #ifdef TTHE_TUNER_SUPPORT
  1820. if (!cd->pip2_send_user_cmd) {
  1821. int command_code = 0;
  1822. int len;
  1823. /* Print up to cmd ID */
  1824. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1825. if (write_len < len)
  1826. len = write_len;
  1827. else
  1828. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1829. & PIP1_CMD_COMMAND_ID_MASK;
  1830. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1831. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1832. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1833. tthe_print(cd, write_buf, len, "CMD=");
  1834. }
  1835. #endif
  1836. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1837. if (rc)
  1838. return rc;
  1839. /* Get the response size from the first 2 bytes in the response */
  1840. size = get_unaligned_le16(&cd->response_buf[0]);
  1841. /* Ensure size is not greater than max buffer size */
  1842. if (size > PT_MAX_PIP2_MSG_SIZE)
  1843. size = PT_MAX_PIP2_MSG_SIZE;
  1844. /* Minimum size to read is the 2 byte len field */
  1845. if (size == 0)
  1846. size = 2;
  1847. if (size > read_len) {
  1848. pt_debug(cd->dev, DL_ERROR,
  1849. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1850. __func__, size, read_len);
  1851. *actual_read_len = 0;
  1852. return -EIO;
  1853. }
  1854. memcpy(read_buf, cd->response_buf, size);
  1855. *actual_read_len = size;
  1856. return 0;
  1857. }
  1858. /*******************************************************************************
  1859. * FUNCTION: pt_hid_output_user_cmd
  1860. *
  1861. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1862. * the DUT.
  1863. *
  1864. * RETURN:
  1865. * 0 = success
  1866. * !0 = failure
  1867. *
  1868. * PARAMETERS:
  1869. * *cd - pointer to core data
  1870. * read_len - length of data to read
  1871. * *read_buf - pointer to store read data
  1872. * write_len - length of data to write
  1873. * *write_buf - pointer to buffer to write
  1874. * *actual_read_len - pointer to store data length actually read
  1875. ******************************************************************************/
  1876. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1877. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1878. u16 *actual_read_len)
  1879. {
  1880. int rc;
  1881. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1882. if (rc < 0) {
  1883. pt_debug(cd->dev, DL_ERROR,
  1884. "%s: fail get exclusive ex=%p own=%p\n",
  1885. __func__, cd->exclusive_dev, cd->dev);
  1886. return rc;
  1887. }
  1888. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1889. write_len, write_buf, actual_read_len);
  1890. if (release_exclusive(cd, cd->dev) < 0)
  1891. pt_debug(cd->dev, DL_ERROR,
  1892. "%s: fail to release exclusive\n", __func__);
  1893. return rc;
  1894. }
  1895. /*******************************************************************************
  1896. * FUNCTION: _pt_request_pip2_send_cmd
  1897. *
  1898. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1899. * interrupt and reads response data to read_buf
  1900. *
  1901. * RETURN:
  1902. * 0 = success
  1903. * !0 = failure
  1904. *
  1905. * PARAMETERS:
  1906. * *dev - pointer to device structure
  1907. * protect - flag to run in protected mode
  1908. * id - ID of PIP command
  1909. * *data - pointer to PIP data payload
  1910. * report_body_len - report length
  1911. * *read_buf - pointer to response buffer
  1912. * *actual_read_len - pointer to response buffer length
  1913. ******************************************************************************/
  1914. static int _pt_request_pip2_send_cmd(struct device *dev,
  1915. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1916. u16 *actual_read_len)
  1917. {
  1918. struct pt_core_data *cd = dev_get_drvdata(dev);
  1919. struct pip2_cmd_structure pip2_cmd;
  1920. int rc = 0;
  1921. u16 i = 0;
  1922. u16 j = 0;
  1923. u16 write_len;
  1924. u8 *write_buf = NULL;
  1925. u16 read_len;
  1926. u8 extra_bytes;
  1927. if (report_body_len > 247)
  1928. return -EPROTO;
  1929. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1930. /* Hard coded register for PIP2.x */
  1931. pip2_cmd.reg[0] = 0x01;
  1932. pip2_cmd.reg[1] = 0x01;
  1933. /*
  1934. * For PIP2.1+ the length field value includes itself:
  1935. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1936. *
  1937. * The overall write length must include only the register:
  1938. * ADD 2: 2 (Register)
  1939. */
  1940. extra_bytes = 6;
  1941. write_len = 2;
  1942. /* PIP2 the CMD ID is a 7bit field */
  1943. if (id > PIP2_CMD_ID_END) {
  1944. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1945. __func__, id);
  1946. rc = -EINVAL;
  1947. goto exit;
  1948. }
  1949. pip2_cmd.len = report_body_len + extra_bytes;
  1950. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1951. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1952. pip2_cmd.data = data;
  1953. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1954. /* Add the command length to the extra bytes based on PIP version */
  1955. write_len += pip2_cmd.len;
  1956. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1957. __func__, pip2_cmd.len, write_len);
  1958. write_buf = kzalloc(write_len, GFP_KERNEL);
  1959. if (write_buf == NULL) {
  1960. rc = -ENOMEM;
  1961. goto exit;
  1962. }
  1963. write_buf[i++] = pip2_cmd.reg[0];
  1964. write_buf[i++] = pip2_cmd.reg[1];
  1965. write_buf[i++] = pip2_cmd.len & 0xff;
  1966. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1967. write_buf[i++] = pip2_cmd.seq;
  1968. write_buf[i++] = pip2_cmd.id;
  1969. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1970. write_buf[j] = pip2_cmd.data[j-i];
  1971. write_buf[j++] = pip2_cmd.crc[0];
  1972. write_buf[j++] = pip2_cmd.crc[1];
  1973. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1974. if (read_len < 0)
  1975. read_len = 255;
  1976. pt_debug(dev, DL_INFO,
  1977. "%s cmd_id[0x%02X] expected response length:%d ",
  1978. __func__, pip2_cmd.id, read_len);
  1979. /*
  1980. * All PIP2 commands come through this function.
  1981. * Set flag for PIP2.x interface to allow response parsing to know
  1982. * how to decode the protocol header.
  1983. */
  1984. mutex_lock(&cd->system_lock);
  1985. cd->pip2_prot_active = true;
  1986. cd->pip2_send_user_cmd = true;
  1987. mutex_unlock(&cd->system_lock);
  1988. if (protect == PT_CORE_CMD_PROTECTED)
  1989. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1990. write_len, write_buf, actual_read_len);
  1991. else {
  1992. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1993. write_len, write_buf, actual_read_len);
  1994. }
  1995. if (rc) {
  1996. pt_debug(dev, DL_ERROR,
  1997. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1998. __func__, rc);
  1999. goto exit;
  2000. }
  2001. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  2002. *actual_read_len);
  2003. exit:
  2004. mutex_lock(&cd->system_lock);
  2005. cd->pip2_prot_active = false;
  2006. cd->pip2_send_user_cmd = false;
  2007. mutex_unlock(&cd->system_lock);
  2008. kfree(write_buf);
  2009. return rc;
  2010. }
  2011. /*******************************************************************************
  2012. * FUNCTION: _pt_pip2_send_cmd_no_int
  2013. *
  2014. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  2015. * reads response data to read_buf if response is available.
  2016. *
  2017. * NOTE:
  2018. * Interrupt MUST be disabled before to call this function.
  2019. *
  2020. * RETURN:
  2021. * 0 = success
  2022. * !0 = failure
  2023. *
  2024. * PARAMETERS:
  2025. * *dev - pointer to device structure
  2026. * protect - flag to run in protected mode
  2027. * id - ID of PIP command
  2028. * *data - pointer to PIP data payload
  2029. * report_body_len - report length
  2030. * *read_buf - pointer to response buffer
  2031. * *actual_read_len - pointer to response buffer length
  2032. ******************************************************************************/
  2033. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2034. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2035. u16 *actual_read_len)
  2036. {
  2037. int max_retry = 0;
  2038. int retry = 0;
  2039. int rc = 0;
  2040. u16 i = 0;
  2041. u16 j = 0;
  2042. u16 write_len;
  2043. u8 *write_buf = NULL;
  2044. u16 read_len;
  2045. u16 size = 0;
  2046. u8 response_seq = 0;
  2047. u8 extra_bytes;
  2048. u32 retry_interval = 0;
  2049. u32 retry_total_time = 0;
  2050. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2051. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2052. struct pt_core_data *cd = dev_get_drvdata(dev);
  2053. struct pip2_cmd_structure pip2_cmd;
  2054. if (report_body_len > 247)
  2055. return -EPROTO;
  2056. if (protect == PT_CORE_CMD_PROTECTED) {
  2057. rc = request_exclusive(cd,
  2058. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2059. if (rc < 0) {
  2060. pt_debug(cd->dev, DL_ERROR,
  2061. "%s: fail get exclusive ex=%p own=%p\n",
  2062. __func__, cd->exclusive_dev, cd->dev);
  2063. return rc;
  2064. }
  2065. }
  2066. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2067. /* Hard coded register for PIP2.x */
  2068. pip2_cmd.reg[0] = 0x01;
  2069. pip2_cmd.reg[1] = 0x01;
  2070. /*
  2071. * For PIP2.1+ the length field value includes itself:
  2072. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2073. *
  2074. * The overall write length must include only the register:
  2075. * ADD 2: 2 (Register)
  2076. */
  2077. extra_bytes = 6;
  2078. write_len = 2;
  2079. pip2_cmd.len = report_body_len + extra_bytes;
  2080. pip2_cmd.id = id;
  2081. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2082. pip2_cmd.data = data;
  2083. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2084. /* Add the command length to the extra bytes based on PIP version */
  2085. write_len += pip2_cmd.len;
  2086. write_buf = kzalloc(write_len, GFP_KERNEL);
  2087. if (write_buf == NULL) {
  2088. rc = -ENOMEM;
  2089. goto exit;
  2090. }
  2091. write_buf[i++] = pip2_cmd.reg[0];
  2092. write_buf[i++] = pip2_cmd.reg[1];
  2093. write_buf[i++] = pip2_cmd.len & 0xff;
  2094. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2095. write_buf[i++] = pip2_cmd.seq;
  2096. write_buf[i++] = pip2_cmd.id;
  2097. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2098. write_buf[j] = pip2_cmd.data[j-i];
  2099. write_buf[j++] = pip2_cmd.crc[0];
  2100. write_buf[j++] = pip2_cmd.crc[1];
  2101. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2102. if (read_len < 0)
  2103. read_len = 255;
  2104. pt_debug(dev, DL_INFO,
  2105. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2106. __func__, pip2_cmd.id, read_len);
  2107. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2108. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2109. if (rc) {
  2110. pt_debug(dev, DL_ERROR,
  2111. "%s: SPI write Error = %d\n",
  2112. __func__, rc);
  2113. goto exit;
  2114. }
  2115. #ifdef PT_POLL_RESP_BY_BUS
  2116. /*
  2117. * Frequent bus read can increase system load obviously. The expected
  2118. * first bus read should be valid and timely. The tollerance for
  2119. * usleep_range should be limited. The minimum response delay (between
  2120. * command finishes sending and INT pin falls) is less than 50
  2121. * microseconds. So the 10 microseconds should be maximum tollerance
  2122. * with the consideration that the unit to calculate the response delay
  2123. * is 10 microseconds and more precise is not necessary. Every
  2124. * additional 10 microseconds only contribute less than 3 milliseconds
  2125. * for whole BL.
  2126. */
  2127. usleep_range(resp_time_min, resp_time_min+10);
  2128. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2129. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2130. rc = pt_adap_read_default(cd, read_buf, read_len);
  2131. if (rc) {
  2132. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2133. __func__, rc);
  2134. break;
  2135. }
  2136. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2137. size = get_unaligned_le16(&read_buf[0]);
  2138. if ((size <= read_len) &&
  2139. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2140. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2141. break;
  2142. }
  2143. /*
  2144. * To reduce the bus and system load, increase the sleep
  2145. * step gradually:
  2146. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2147. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2148. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2149. */
  2150. retry++;
  2151. if (retry < 20) {
  2152. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2153. usleep_range(retry_interval,
  2154. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2155. } else if (retry < 40) {
  2156. retry_interval += 1000;
  2157. usleep_range(retry_interval, retry_interval + 1000);
  2158. } else {
  2159. retry_interval += 50000;
  2160. msleep(retry_interval/1000);
  2161. }
  2162. retry_total_time += retry_interval;
  2163. }
  2164. #else
  2165. /*
  2166. * Frequent GPIO read will not increase CPU/system load heavily if the
  2167. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2168. * fixed interval: 20 us.
  2169. */
  2170. usleep_range(resp_time_min, resp_time_min+10);
  2171. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2172. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2173. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2174. rc = pt_adap_read_default(cd, read_buf, read_len);
  2175. size = get_unaligned_le16(&read_buf[0]);
  2176. if (rc)
  2177. pt_debug(dev, DL_ERROR,
  2178. "%s: SPI read Error = %d\n",
  2179. __func__, rc);
  2180. else if (size > read_len) {
  2181. pt_debug(cd->dev, DL_ERROR,
  2182. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2183. __func__, size, read_len);
  2184. rc = -EIO;
  2185. }
  2186. break;
  2187. }
  2188. /*
  2189. * Poll GPIO with fixed interval 20 us, and tollerance is
  2190. * limited to 10 us to speed up the process.
  2191. */
  2192. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2193. usleep_range(retry_interval, retry_interval+10);
  2194. retry_total_time += retry_interval;
  2195. }
  2196. #endif
  2197. *actual_read_len = size;
  2198. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2199. pt_debug(dev, DL_ERROR,
  2200. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2201. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2202. *actual_read_len = 0;
  2203. rc = -EINVAL;
  2204. }
  2205. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2206. "<<< NO_INT Read");
  2207. exit:
  2208. kfree(write_buf);
  2209. if (protect == PT_CORE_CMD_PROTECTED) {
  2210. if (release_exclusive(cd, cd->dev) < 0)
  2211. pt_debug(cd->dev, DL_ERROR,
  2212. "%s: fail to release exclusive\n", __func__);
  2213. }
  2214. return rc;
  2215. }
  2216. /*******************************************************************************
  2217. * FUNCTION: pt_pip_null_
  2218. *
  2219. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2220. * This function is used by watchdog to check if the fw corrupts.
  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. struct pt_hid_output hid_output = {
  2232. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2233. };
  2234. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2235. }
  2236. /*******************************************************************************
  2237. * FUNCTION: pt_pip_null
  2238. *
  2239. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2240. *
  2241. * RETURN:
  2242. * 0 = success
  2243. * !0 = failure
  2244. *
  2245. * PARAMETERS:
  2246. * *cd - pointer to core data
  2247. ******************************************************************************/
  2248. static int pt_pip_null(struct pt_core_data *cd)
  2249. {
  2250. int rc;
  2251. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2252. if (rc < 0) {
  2253. pt_debug(cd->dev, DL_ERROR,
  2254. "%s: fail get exclusive ex=%p own=%p\n",
  2255. __func__, cd->exclusive_dev, cd->dev);
  2256. return rc;
  2257. }
  2258. rc = pt_pip_null_(cd);
  2259. if (release_exclusive(cd, cd->dev) < 0)
  2260. pt_debug(cd->dev, DL_ERROR,
  2261. "%s: fail to release exclusive\n", __func__);
  2262. return rc;
  2263. }
  2264. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2265. /*******************************************************************************
  2266. * FUNCTION: pt_pip_start_bootloader_
  2267. *
  2268. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2269. *
  2270. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2271. * the WD active could cause this function to fail!
  2272. *
  2273. * RETURN:
  2274. * 0 = success
  2275. * !0 = failure
  2276. *
  2277. * PARAMETERS:
  2278. * *cd - pointer to core data
  2279. ******************************************************************************/
  2280. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2281. {
  2282. int rc = 0;
  2283. struct pt_hid_output hid_output = {
  2284. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2285. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2286. .reset_expected = 1,
  2287. };
  2288. if (cd->watchdog_enabled) {
  2289. pt_debug(cd->dev, DL_WARN,
  2290. "%s: watchdog isn't stopped before enter bl\n",
  2291. __func__);
  2292. goto exit;
  2293. }
  2294. /* Reset startup status after entering BL, new DUT enum required */
  2295. cd->startup_status = STARTUP_STATUS_START;
  2296. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2297. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2298. if (rc) {
  2299. pt_debug(cd->dev, DL_ERROR,
  2300. "%s: Start BL PIP cmd failed. rc = %d\n",
  2301. __func__, rc);
  2302. }
  2303. exit:
  2304. return rc;
  2305. }
  2306. /*******************************************************************************
  2307. * FUNCTION: pt_pip_start_bootloader
  2308. *
  2309. * SUMMARY: Protected function to force DUT to enter the BL
  2310. *
  2311. * RETURN:
  2312. * 0 = success
  2313. * !0 = failure
  2314. *
  2315. * PARAMETERS:
  2316. * *cd - pointer to core data structure
  2317. ******************************************************************************/
  2318. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2319. {
  2320. int rc;
  2321. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2322. if (rc < 0) {
  2323. pt_debug(cd->dev, DL_ERROR,
  2324. "%s: fail get exclusive ex=%p own=%p\n",
  2325. __func__, cd->exclusive_dev, cd->dev);
  2326. return rc;
  2327. }
  2328. rc = pt_pip_start_bootloader_(cd);
  2329. if (release_exclusive(cd, cd->dev) < 0)
  2330. pt_debug(cd->dev, DL_ERROR,
  2331. "%s: fail to release exclusive\n", __func__);
  2332. return rc;
  2333. }
  2334. /*******************************************************************************
  2335. * FUNCTION: _pt_request_pip_start_bl
  2336. *
  2337. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2338. * modules to request the DUT to enter the BL
  2339. *
  2340. * RETURN:
  2341. * 0 = success
  2342. * !0 = failure
  2343. *
  2344. * PARAMETERS:
  2345. * *dev - pointer to device structure
  2346. * protect - flag to run in protected mode
  2347. ******************************************************************************/
  2348. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2349. {
  2350. struct pt_core_data *cd = dev_get_drvdata(dev);
  2351. if (protect)
  2352. return pt_pip_start_bootloader(cd);
  2353. return pt_pip_start_bootloader_(cd);
  2354. }
  2355. /*******************************************************************************
  2356. * FUNCTION: pt_pip2_ver_load_ttdata
  2357. *
  2358. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2359. * command into the core data struct.
  2360. *
  2361. * RETURN: n/a
  2362. *
  2363. * PARAMETERS:
  2364. * *cd - pointer to core data structure
  2365. * len - Length of data in response_buf
  2366. ******************************************************************************/
  2367. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2368. {
  2369. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2370. struct pt_pip2_version_full *full_ver;
  2371. struct pt_pip2_version *ver;
  2372. /*
  2373. * The PIP2 VERSION command can return different lengths of data.
  2374. * The additional LOT fields are included when the packet
  2375. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2376. * NOTE:
  2377. * - The FW would swap the BL and FW versions when reporting
  2378. * the small packet.
  2379. * - Sub Lot bytes 16 and 17 are reserved.
  2380. */
  2381. if (len >= 0x1D) {
  2382. full_ver = (struct pt_pip2_version_full *)
  2383. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2384. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2385. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2386. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2387. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2388. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2389. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2390. /*
  2391. * BL PIP 2.02 and greater the version fields are
  2392. * swapped
  2393. */
  2394. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2395. ttdata->chip_rev =
  2396. get_unaligned_le16(&full_ver->chip_rev);
  2397. ttdata->chip_id =
  2398. get_unaligned_le16(&full_ver->chip_id);
  2399. } else {
  2400. ttdata->chip_rev =
  2401. get_unaligned_le16(&full_ver->chip_id);
  2402. ttdata->chip_id =
  2403. get_unaligned_le16(&full_ver->chip_rev);
  2404. }
  2405. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2406. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2407. sizeof(struct pt_pip2_version_full),
  2408. "PIP2 VERSION FULL");
  2409. } else {
  2410. ver = (struct pt_pip2_version *)
  2411. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2412. ttdata->pip_ver_major = ver->pip2_version_msb;
  2413. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2414. ttdata->bl_ver_major = ver->bl_version_msb;
  2415. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2416. ttdata->fw_ver_major = ver->fw_version_msb;
  2417. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2418. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2419. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2420. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2421. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2422. }
  2423. }
  2424. /*******************************************************************************
  2425. * FUNCTION: pt_si_get_ttdata
  2426. *
  2427. * SUMMARY: Function to load the version information from the system information
  2428. * PIP command into the core data struct.
  2429. *
  2430. * RETURN: n/a
  2431. *
  2432. * PARAMETERS:
  2433. * *cd - pointer to core data structure
  2434. ******************************************************************************/
  2435. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2436. {
  2437. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2438. struct pt_ttdata_dev *ttdata_dev =
  2439. (struct pt_ttdata_dev *)
  2440. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2441. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2442. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2443. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2444. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2445. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2446. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2447. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2448. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2449. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2450. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2451. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2452. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2453. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2454. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2455. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2456. }
  2457. /*******************************************************************************
  2458. * FUNCTION: pt_si_get_sensing_conf_data
  2459. *
  2460. * SUMMARY: Function to load the sensing information from the system information
  2461. * PIP command into the core data struct.
  2462. *
  2463. * RETURN: n/a
  2464. *
  2465. * PARAMETERS:
  2466. * *cd - pointer to core data structure
  2467. ******************************************************************************/
  2468. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2469. {
  2470. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2471. struct pt_sensing_conf_data_dev *scd_dev =
  2472. (struct pt_sensing_conf_data_dev *)
  2473. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2474. scd->electrodes_x = scd_dev->electrodes_x;
  2475. scd->electrodes_y = scd_dev->electrodes_y;
  2476. scd->origin_x = scd_dev->origin_x;
  2477. scd->origin_y = scd_dev->origin_y;
  2478. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2479. if (scd->origin_x & 0x02) {
  2480. scd->tx_num = scd->electrodes_x;
  2481. scd->rx_num = scd->electrodes_y;
  2482. } else {
  2483. scd->tx_num = scd->electrodes_y;
  2484. scd->rx_num = scd->electrodes_x;
  2485. }
  2486. /*
  2487. * When the Panel ID is coming from an XY pin and not a dedicated
  2488. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2489. * other DUTs as the loader will use cd->pid_for_loader to generate
  2490. * the bin file name but will ignore it if pid_for_loader is still
  2491. * set to PANEL_ID_NOT_ENABLED
  2492. */
  2493. if (cd->panel_id_support &
  2494. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2495. mutex_lock(&cd->system_lock);
  2496. cd->pid_for_loader = scd_dev->panel_id;
  2497. mutex_unlock(&cd->system_lock);
  2498. }
  2499. scd->panel_id = scd_dev->panel_id;
  2500. scd->btn = scd_dev->btn;
  2501. scd->scan_mode = scd_dev->scan_mode;
  2502. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2503. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2504. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2505. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2506. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2507. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2508. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2509. sizeof(struct pt_sensing_conf_data_dev),
  2510. "sensing_conf_data");
  2511. }
  2512. /*******************************************************************************
  2513. * FUNCTION: pt_si_setup
  2514. *
  2515. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2516. *
  2517. * RETURN:
  2518. * 0 = success
  2519. * !0 = failure
  2520. *
  2521. * PARAMETERS:
  2522. * *cd - pointer to core data structure
  2523. ******************************************************************************/
  2524. static int pt_si_setup(struct pt_core_data *cd)
  2525. {
  2526. struct pt_sysinfo *si = &cd->sysinfo;
  2527. int max_tch = si->sensing_conf_data.max_tch;
  2528. if (!si->xy_data)
  2529. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2530. GFP_KERNEL);
  2531. if (!si->xy_data)
  2532. return -ENOMEM;
  2533. if (!si->xy_mode)
  2534. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2535. if (!si->xy_mode) {
  2536. kfree(si->xy_data);
  2537. return -ENOMEM;
  2538. }
  2539. return 0;
  2540. }
  2541. /*******************************************************************************
  2542. * FUNCTION: pt_si_get_btn_data
  2543. *
  2544. * SUMMARY: Setup the core data button information based on the response of the
  2545. * System Information PIP command.
  2546. *
  2547. * RETURN:
  2548. * 0 = success
  2549. * !0 = failure
  2550. *
  2551. * PARAMETERS:
  2552. * *cd - pointer to core data structure
  2553. ******************************************************************************/
  2554. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2555. {
  2556. struct pt_sysinfo *si = &cd->sysinfo;
  2557. int num_btns = 0;
  2558. int num_defined_keys;
  2559. u16 *key_table;
  2560. int btn;
  2561. int i;
  2562. int rc = 0;
  2563. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2564. & PIP1_SYSINFO_BTN_MASK;
  2565. size_t btn_keys_size;
  2566. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2567. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2568. if (btns & (1 << i))
  2569. num_btns++;
  2570. }
  2571. si->num_btns = num_btns;
  2572. if (num_btns) {
  2573. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2574. if (!si->btn)
  2575. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2576. if (!si->btn)
  2577. return -ENOMEM;
  2578. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2579. num_defined_keys = 0;
  2580. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2581. num_defined_keys = 0;
  2582. else
  2583. num_defined_keys = cd->cpdata->sett
  2584. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2585. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2586. key_table = (u16 *)cd->cpdata->sett
  2587. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2588. si->btn[btn].key_code = key_table[btn];
  2589. si->btn[btn].enabled = true;
  2590. }
  2591. for (; btn < num_btns; btn++) {
  2592. si->btn[btn].key_code = KEY_RESERVED;
  2593. si->btn[btn].enabled = true;
  2594. }
  2595. return rc;
  2596. }
  2597. kfree(si->btn);
  2598. si->btn = NULL;
  2599. return rc;
  2600. }
  2601. /*******************************************************************************
  2602. * FUNCTION: pt_si_put_log_data
  2603. *
  2604. * SUMMARY: Prints all sys info data to kmsg log
  2605. *
  2606. * RETURN: n/a
  2607. *
  2608. * PARAMETERS:
  2609. * *cd - pointer to core data structure
  2610. ******************************************************************************/
  2611. static void pt_si_put_log_data(struct pt_core_data *cd)
  2612. {
  2613. struct pt_sysinfo *si = &cd->sysinfo;
  2614. struct pt_ttdata *ttdata = &si->ttdata;
  2615. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2616. int i;
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2618. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2620. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2622. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2624. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2626. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2628. __func__, ttdata->revctrl, ttdata->revctrl);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2630. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2632. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2634. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2636. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2638. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2639. for (i = 0; i < PT_NUM_MFGID; i++)
  2640. pt_debug(cd->dev, DL_DEBUG,
  2641. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2642. __func__, i, ttdata->mfg_id[i],
  2643. ttdata->mfg_id[i]);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2645. __func__, ttdata->post_code, ttdata->post_code);
  2646. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2647. __func__, scd->electrodes_x, scd->electrodes_x);
  2648. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2649. __func__, scd->electrodes_y, scd->electrodes_y);
  2650. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2651. __func__, scd->len_x, scd->len_x);
  2652. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2653. __func__, scd->len_y, scd->len_y);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2655. __func__, scd->res_x, scd->res_x);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2657. __func__, scd->res_y, scd->res_y);
  2658. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2659. __func__, scd->max_z, scd->max_z);
  2660. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2661. __func__, scd->origin_x, scd->origin_x);
  2662. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2663. __func__, scd->origin_y, scd->origin_y);
  2664. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2665. __func__, scd->panel_id, scd->panel_id);
  2666. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2667. __func__, scd->btn, scd->btn);
  2668. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2669. __func__, scd->scan_mode, scd->scan_mode);
  2670. pt_debug(cd->dev, DL_DEBUG,
  2671. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2672. __func__, scd->max_tch, scd->max_tch);
  2673. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2674. __func__, si->xy_mode);
  2675. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2676. __func__, si->xy_data);
  2677. }
  2678. /*******************************************************************************
  2679. * FUNCTION: pt_get_sysinfo_regs
  2680. *
  2681. * SUMMARY: Setup all the core data System information based on the response
  2682. * of the System Information PIP command.
  2683. *
  2684. * RETURN:
  2685. * 0 = success
  2686. * !0 = failure
  2687. *
  2688. * PARAMETERS:
  2689. * *cd - pointer to core data structure
  2690. ******************************************************************************/
  2691. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2692. {
  2693. struct pt_sysinfo *si = &cd->sysinfo;
  2694. int rc;
  2695. rc = pt_si_get_btn_data(cd);
  2696. if (rc < 0)
  2697. return rc;
  2698. pt_si_get_ttdata(cd);
  2699. pt_si_get_sensing_conf_data(cd);
  2700. pt_si_setup(cd);
  2701. pt_si_put_log_data(cd);
  2702. si->ready = true;
  2703. return rc;
  2704. }
  2705. /*******************************************************************************
  2706. * FUNCTION: pt_free_si_ptrs
  2707. *
  2708. * SUMMARY: Frees all memory associated with the System Information within
  2709. * core data
  2710. *
  2711. * RETURN: n/a
  2712. *
  2713. * PARAMETERS:
  2714. * *cd - pointer to core data structure
  2715. ******************************************************************************/
  2716. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2717. {
  2718. struct pt_sysinfo *si = &cd->sysinfo;
  2719. kfree(si->btn);
  2720. kfree(si->xy_mode);
  2721. kfree(si->xy_data);
  2722. }
  2723. /*******************************************************************************
  2724. * FUNCTION: pt_hid_output_get_sysinfo_
  2725. *
  2726. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2727. * response.
  2728. *
  2729. * RETURN::
  2730. * 0 = success
  2731. * !0 = failure
  2732. *
  2733. * PARAMETERS:
  2734. * *cd - pointer to core data structure
  2735. ******************************************************************************/
  2736. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2737. {
  2738. int rc = 0;
  2739. struct pt_hid_output hid_output = {
  2740. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2741. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2742. };
  2743. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2744. if (rc)
  2745. return rc;
  2746. /* Parse the sysinfo data */
  2747. rc = pt_get_sysinfo_regs(cd);
  2748. if (rc)
  2749. pt_free_si_ptrs(cd);
  2750. return rc;
  2751. }
  2752. /*******************************************************************************
  2753. * FUNCTION: pt_hid_output_get_sysinfo
  2754. *
  2755. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2756. *
  2757. * RETURN::
  2758. * 0 = success
  2759. * !0 = failure
  2760. *
  2761. * PARAMETERS:
  2762. * *cd - pointer to core data structure
  2763. ******************************************************************************/
  2764. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2765. {
  2766. int rc;
  2767. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2768. if (rc < 0) {
  2769. pt_debug(cd->dev, DL_ERROR,
  2770. "%s: fail get exclusive ex=%p own=%p\n",
  2771. __func__, cd->exclusive_dev, cd->dev);
  2772. return rc;
  2773. }
  2774. rc = pt_hid_output_get_sysinfo_(cd);
  2775. if (release_exclusive(cd, cd->dev) < 0)
  2776. pt_debug(cd->dev, DL_ERROR,
  2777. "%s: fail to release exclusive\n", __func__);
  2778. return rc;
  2779. }
  2780. /*******************************************************************************
  2781. * FUNCTION: pt_pip_suspend_scanning_
  2782. *
  2783. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2784. *
  2785. * RETURN::
  2786. * 0 = success
  2787. * !0 = failure
  2788. *
  2789. * PARAMETERS:
  2790. * *cd - pointer to core data structure
  2791. ******************************************************************************/
  2792. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2793. {
  2794. int rc = 0;
  2795. struct pt_hid_output hid_output = {
  2796. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2797. };
  2798. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2799. if (rc) {
  2800. pt_debug(cd->dev, DL_ERROR,
  2801. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2802. __func__, rc);
  2803. }
  2804. return rc;
  2805. }
  2806. /*******************************************************************************
  2807. * FUNCTION: pt_pip_suspend_scanning
  2808. *
  2809. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2810. *
  2811. * RETURN::
  2812. * 0 = success
  2813. * !0 = failure
  2814. *
  2815. * PARAMETERS:
  2816. * *cd - pointer to core data structure
  2817. ******************************************************************************/
  2818. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2819. {
  2820. int rc;
  2821. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2822. if (rc < 0) {
  2823. pt_debug(cd->dev, DL_ERROR,
  2824. "%s: fail get exclusive ex=%p own=%p\n",
  2825. __func__, cd->exclusive_dev, cd->dev);
  2826. return rc;
  2827. }
  2828. rc = pt_pip_suspend_scanning_(cd);
  2829. if (release_exclusive(cd, cd->dev) < 0)
  2830. pt_debug(cd->dev, DL_ERROR,
  2831. "%s: fail to release exclusive\n", __func__);
  2832. return rc;
  2833. }
  2834. /*******************************************************************************
  2835. * FUNCTION: _pt_request_pip_suspend_scanning
  2836. *
  2837. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2838. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2839. *
  2840. * RETURN:
  2841. * 0 = success
  2842. * !0 = failure
  2843. *
  2844. * PARAMETERS:
  2845. * *dev - pointer to device structure
  2846. * protect - 0 = call non-protected function
  2847. * 1 = call protected function
  2848. ******************************************************************************/
  2849. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2850. int protect)
  2851. {
  2852. struct pt_core_data *cd = dev_get_drvdata(dev);
  2853. if (protect)
  2854. return pt_pip_suspend_scanning(cd);
  2855. return pt_pip_suspend_scanning_(cd);
  2856. }
  2857. /*******************************************************************************
  2858. * FUNCTION: pt_pip_resume_scanning_
  2859. *
  2860. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2861. *
  2862. * RETURN::
  2863. * 0 = success
  2864. * !0 = failure
  2865. *
  2866. * PARAMETERS:
  2867. * *cd - pointer to core data structure
  2868. ******************************************************************************/
  2869. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2870. {
  2871. int rc = 0;
  2872. struct pt_hid_output hid_output = {
  2873. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2874. };
  2875. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2876. if (rc) {
  2877. pt_debug(cd->dev, DL_ERROR,
  2878. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2879. __func__, rc);
  2880. }
  2881. return rc;
  2882. }
  2883. /*******************************************************************************
  2884. * FUNCTION: pt_pip_resume_scanning
  2885. *
  2886. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2887. *
  2888. * RETURN::
  2889. * 0 = success
  2890. * !0 = failure
  2891. *
  2892. * PARAMETERS:
  2893. * *cd - pointer to core data structure
  2894. ******************************************************************************/
  2895. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2896. {
  2897. int rc;
  2898. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2899. if (rc < 0) {
  2900. pt_debug(cd->dev, DL_ERROR,
  2901. "%s: fail get exclusive ex=%p own=%p\n",
  2902. __func__, cd->exclusive_dev, cd->dev);
  2903. return rc;
  2904. }
  2905. rc = pt_pip_resume_scanning_(cd);
  2906. if (release_exclusive(cd, cd->dev) < 0)
  2907. pt_debug(cd->dev, DL_ERROR,
  2908. "%s: fail to release exclusive\n", __func__);
  2909. return rc;
  2910. }
  2911. /*******************************************************************************
  2912. * FUNCTION: _pt_request_pip_resume_scanning
  2913. *
  2914. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2915. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2916. *
  2917. * RETURN::
  2918. * 0 = success
  2919. * !0 = failure
  2920. *
  2921. * PARAMETERS:
  2922. * *dev - pointer to device structure
  2923. * protect - 0 = call non-protected function
  2924. * 1 = call protected function
  2925. ******************************************************************************/
  2926. static int _pt_request_pip_resume_scanning(struct device *dev,
  2927. int protect)
  2928. {
  2929. struct pt_core_data *cd = dev_get_drvdata(dev);
  2930. if (protect)
  2931. return pt_pip_resume_scanning(cd);
  2932. return pt_pip_resume_scanning_(cd);
  2933. }
  2934. /*******************************************************************************
  2935. * FUNCTION: pt_pip_get_param_
  2936. *
  2937. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2938. * the 32bit parameter value
  2939. *
  2940. * RETURN:
  2941. * 0 = success
  2942. * !0 = failure
  2943. *
  2944. * PARAMETERS:
  2945. * *cd - pointer to core data
  2946. * param_id - parameter ID to retrieve
  2947. * *value - value of DUT parameter
  2948. ******************************************************************************/
  2949. static int pt_pip_get_param_(struct pt_core_data *cd,
  2950. u8 param_id, u32 *value)
  2951. {
  2952. int write_length = 1;
  2953. u8 param[1] = { param_id };
  2954. u8 read_param_id;
  2955. int param_size;
  2956. u8 *ptr;
  2957. int rc = 0;
  2958. int i;
  2959. struct pt_hid_output hid_output = {
  2960. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2961. .write_length = write_length,
  2962. .write_buf = param,
  2963. };
  2964. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2965. if (rc)
  2966. return rc;
  2967. read_param_id = cd->response_buf[5];
  2968. if (read_param_id != param_id)
  2969. return -EPROTO;
  2970. param_size = cd->response_buf[6];
  2971. ptr = &cd->response_buf[7];
  2972. *value = 0;
  2973. for (i = 0; i < param_size; i++)
  2974. *value += ptr[i] << (i * 8);
  2975. return 0;
  2976. }
  2977. /*******************************************************************************
  2978. * FUNCTION: pt_pip_get_param
  2979. *
  2980. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2981. *
  2982. * RETURN:
  2983. * 0 = success
  2984. * !0 = failure
  2985. *
  2986. * PARAMETERS:
  2987. * *cd - pointer to core data
  2988. * param_id - parameter ID to retrieve
  2989. * *value - value of DUT parameter
  2990. ******************************************************************************/
  2991. static int pt_pip_get_param(struct pt_core_data *cd,
  2992. u8 param_id, u32 *value)
  2993. {
  2994. int rc;
  2995. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2996. if (rc < 0) {
  2997. pt_debug(cd->dev, DL_ERROR,
  2998. "%s: fail get exclusive ex=%p own=%p\n",
  2999. __func__, cd->exclusive_dev, cd->dev);
  3000. return rc;
  3001. }
  3002. rc = pt_pip_get_param_(cd, param_id, value);
  3003. if (release_exclusive(cd, cd->dev) < 0)
  3004. pt_debug(cd->dev, DL_ERROR,
  3005. "%s: fail to release exclusive\n", __func__);
  3006. return rc;
  3007. }
  3008. /*******************************************************************************
  3009. * FUNCTION: _pt_request_pip_get_param
  3010. *
  3011. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3012. * calls to the protected or unprotected call to pt_pip_get_param
  3013. *
  3014. * RETURN:
  3015. * 0 = success
  3016. * !0 = failure
  3017. *
  3018. * PARAMETERS:
  3019. * *dev - pointer to device structure
  3020. * protect - flag to call protected or non protected function
  3021. * param_id - parameter ID to retrieve
  3022. * *value - value of DUT parameter
  3023. ******************************************************************************/
  3024. int _pt_request_pip_get_param(struct device *dev,
  3025. int protect, u8 param_id, u32 *value)
  3026. {
  3027. struct pt_core_data *cd = dev_get_drvdata(dev);
  3028. if (protect)
  3029. return pt_pip_get_param(cd, param_id, value);
  3030. return pt_pip_get_param_(cd, param_id, value);
  3031. }
  3032. /*******************************************************************************
  3033. * FUNCTION: pt_pip_set_param_
  3034. *
  3035. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3036. * passed in value to flash
  3037. *
  3038. * RETURN:
  3039. * 0 = success
  3040. * !0 = failure
  3041. *
  3042. * PARAMETERS:
  3043. * *cd - pointer to core data
  3044. * param_id - parameter ID to set
  3045. * value - value to write
  3046. * size - size to write
  3047. ******************************************************************************/
  3048. static int pt_pip_set_param_(struct pt_core_data *cd,
  3049. u8 param_id, u32 value, u8 size)
  3050. {
  3051. u8 write_buf[6];
  3052. u8 *ptr = &write_buf[2];
  3053. int rc = 0;
  3054. int i;
  3055. struct pt_hid_output hid_output = {
  3056. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3057. .write_buf = write_buf,
  3058. };
  3059. write_buf[0] = param_id;
  3060. write_buf[1] = size;
  3061. for (i = 0; i < size; i++) {
  3062. ptr[i] = value & 0xFF;
  3063. value = value >> 8;
  3064. }
  3065. hid_output.write_length = 2 + size;
  3066. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3067. if (rc)
  3068. return rc;
  3069. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3070. return -EPROTO;
  3071. return 0;
  3072. }
  3073. /*******************************************************************************
  3074. * FUNCTION: pt_pip_set_param
  3075. *
  3076. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3077. *
  3078. * RETURN:
  3079. * 0 = success
  3080. * !0 = failure
  3081. *
  3082. * PARAMETERS:
  3083. * *cd - pointer to core data
  3084. * param_id - parameter ID to set
  3085. * value - value to write
  3086. * size - size to write
  3087. ******************************************************************************/
  3088. static int pt_pip_set_param(struct pt_core_data *cd,
  3089. u8 param_id, u32 value, u8 size)
  3090. {
  3091. int rc;
  3092. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3093. if (rc < 0) {
  3094. pt_debug(cd->dev, DL_ERROR,
  3095. "%s: fail get exclusive ex=%p own=%p\n",
  3096. __func__, cd->exclusive_dev, cd->dev);
  3097. return rc;
  3098. }
  3099. rc = pt_pip_set_param_(cd, param_id, value, size);
  3100. if (release_exclusive(cd, cd->dev) < 0)
  3101. pt_debug(cd->dev, DL_ERROR,
  3102. "%s: fail to release exclusive\n", __func__);
  3103. return rc;
  3104. }
  3105. /*******************************************************************************
  3106. * FUNCTION: _pt_request_pip_set_param
  3107. *
  3108. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3109. * calls to the protected or unprotected call to pt_pip_set_param
  3110. *
  3111. * RETURN:
  3112. * 0 = success
  3113. * !0 = failure
  3114. *
  3115. * PARAMETERS:
  3116. * *dev - pointer to device structure
  3117. * protect - flag to call protected or non-protected
  3118. * param_id - parameter ID to set
  3119. * value - value to write
  3120. * size - size to write
  3121. ******************************************************************************/
  3122. int _pt_request_pip_set_param(struct device *dev, int protect,
  3123. u8 param_id, u32 value, u8 size)
  3124. {
  3125. struct pt_core_data *cd = dev_get_drvdata(dev);
  3126. if (protect)
  3127. return pt_pip_set_param(cd, param_id, value, size);
  3128. return pt_pip_set_param_(cd, param_id, value, size);
  3129. }
  3130. /*******************************************************************************
  3131. * FUNCTION: _pt_pip_enter_easywake_state_
  3132. *
  3133. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3134. *
  3135. * RETURN:
  3136. * 0 = success
  3137. * !0 = failure
  3138. *
  3139. * PARAMETERS:
  3140. * *dev - pointer to device structure
  3141. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3142. * *return_data - return status if easywake was entered
  3143. ******************************************************************************/
  3144. static int pt_hid_output_enter_easywake_state_(
  3145. struct pt_core_data *cd, u8 data, u8 *return_data)
  3146. {
  3147. int write_length = 1;
  3148. u8 param[1] = { data };
  3149. int rc = 0;
  3150. struct pt_hid_output hid_output = {
  3151. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3152. .write_length = write_length,
  3153. .write_buf = param,
  3154. };
  3155. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3156. if (rc)
  3157. return rc;
  3158. *return_data = cd->response_buf[5];
  3159. return rc;
  3160. }
  3161. /*******************************************************************************
  3162. * FUNCTION: pt_pip_verify_config_block_crc_
  3163. *
  3164. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3165. *
  3166. * RETURN:
  3167. * 0 = success
  3168. * !0 = failure
  3169. *
  3170. * PARAMETERS:
  3171. * *cd - pointer the core data structure
  3172. * ebid - enumerated block ID
  3173. * *status - PIP command status
  3174. * calculated_crc - calculated CRC
  3175. * stored_crc - stored CRC in config area
  3176. ******************************************************************************/
  3177. static int pt_pip_verify_config_block_crc_(
  3178. struct pt_core_data *cd, u8 ebid, u8 *status,
  3179. u16 *calculated_crc, u16 *stored_crc)
  3180. {
  3181. int write_length = 1;
  3182. u8 param[1] = { ebid };
  3183. u8 *ptr;
  3184. int rc = 0;
  3185. struct pt_hid_output hid_output = {
  3186. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3187. .write_length = write_length,
  3188. .write_buf = param,
  3189. };
  3190. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3191. if (rc)
  3192. return rc;
  3193. ptr = &cd->response_buf[5];
  3194. *status = ptr[0];
  3195. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3196. *stored_crc = get_unaligned_le16(&ptr[3]);
  3197. return 0;
  3198. }
  3199. /*******************************************************************************
  3200. * FUNCTION: pt_pip_verify_config_block_crc
  3201. *
  3202. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3203. * an exclusive access to the DUT.
  3204. *
  3205. * RETURN:
  3206. * 0 = success
  3207. * !0 = failure
  3208. *
  3209. * PARAMETERS:
  3210. * *cd - pointer the core data structure
  3211. * ebid - enumerated block ID
  3212. * *status - PIP command status
  3213. * calculated_crc - calculated CRC
  3214. * stored_crc - stored CRC in config area
  3215. ******************************************************************************/
  3216. static int pt_pip_verify_config_block_crc(
  3217. struct pt_core_data *cd, u8 ebid, u8 *status,
  3218. u16 *calculated_crc, u16 *stored_crc)
  3219. {
  3220. int rc;
  3221. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3222. if (rc < 0) {
  3223. pt_debug(cd->dev, DL_ERROR,
  3224. "%s: fail get exclusive ex=%p own=%p\n",
  3225. __func__, cd->exclusive_dev, cd->dev);
  3226. return rc;
  3227. }
  3228. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3229. calculated_crc, stored_crc);
  3230. if (release_exclusive(cd, cd->dev) < 0)
  3231. pt_debug(cd->dev, DL_ERROR,
  3232. "%s: fail to release exclusive\n", __func__);
  3233. return rc;
  3234. }
  3235. /*******************************************************************************
  3236. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3237. *
  3238. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3239. * calls to the protected or unprotected call to
  3240. * pt_pip_verify_config_block_crc_
  3241. *
  3242. * RETURN:
  3243. * 0 = success
  3244. * !0 = failure
  3245. *
  3246. * PARAMETERS:
  3247. * *dev - pointer to device structure
  3248. * protect - flag to call protected or non-protected
  3249. * ebid - enumerated block ID
  3250. * *status - PIP command status
  3251. * calculated_crc - calculated CRC
  3252. * stored_crc - stored CRC in config area
  3253. ******************************************************************************/
  3254. static int _pt_request_pip_verify_config_block_crc(
  3255. struct device *dev, int protect, u8 ebid, u8 *status,
  3256. u16 *calculated_crc, u16 *stored_crc)
  3257. {
  3258. struct pt_core_data *cd = dev_get_drvdata(dev);
  3259. if (protect)
  3260. return pt_pip_verify_config_block_crc(cd, ebid,
  3261. status, calculated_crc, stored_crc);
  3262. return pt_pip_verify_config_block_crc_(cd, ebid,
  3263. status, calculated_crc, stored_crc);
  3264. }
  3265. /*******************************************************************************
  3266. * FUNCTION: pt_pip_get_config_row_size_
  3267. *
  3268. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3269. *
  3270. * RETURN:
  3271. * 0 = success
  3272. * !0 = failure
  3273. *
  3274. * PARAMETERS:
  3275. * *cd - pointer to core data
  3276. * protect - flag to call protected or non-protected
  3277. * *row_size - pointer to store the retrieved row size
  3278. ******************************************************************************/
  3279. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3280. u16 *row_size)
  3281. {
  3282. int rc = 0;
  3283. struct pt_hid_output hid_output = {
  3284. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3285. };
  3286. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3287. if (rc)
  3288. return rc;
  3289. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3290. return 0;
  3291. }
  3292. /*******************************************************************************
  3293. * FUNCTION: pt_pip_get_config_row_size
  3294. *
  3295. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3296. * an exclusive access to the DUT.
  3297. *
  3298. * RETURN:
  3299. * 0 = success
  3300. * !0 = failure
  3301. *
  3302. * PARAMETERS:
  3303. * *cd - pointer to core data
  3304. * protect - flag to call protected or non-protected
  3305. * *row_size - pointer to store the retrieved row size
  3306. ******************************************************************************/
  3307. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3308. u16 *row_size)
  3309. {
  3310. int rc;
  3311. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3312. if (rc < 0) {
  3313. pt_debug(cd->dev, DL_ERROR,
  3314. "%s: fail get exclusive ex=%p own=%p\n",
  3315. __func__, cd->exclusive_dev, cd->dev);
  3316. return rc;
  3317. }
  3318. rc = pt_pip_get_config_row_size_(cd, row_size);
  3319. if (release_exclusive(cd, cd->dev) < 0)
  3320. pt_debug(cd->dev, DL_ERROR,
  3321. "%s: fail to release exclusive\n", __func__);
  3322. return rc;
  3323. }
  3324. /*******************************************************************************
  3325. * FUNCTION: _pt_request_pip_get_config_row_size
  3326. *
  3327. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3328. * calls to the protected or unprotected call to
  3329. * pt_pip_get_config_row_size_
  3330. *
  3331. * RETURN:
  3332. * 0 = success
  3333. * !0 = failure
  3334. *
  3335. * PARAMETERS:
  3336. * *dev - pointer to device structure
  3337. * protect - flag to call protected or non-protected
  3338. * *row_size - pointer to store the retrieved row size
  3339. ******************************************************************************/
  3340. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3341. int protect, u16 *row_size)
  3342. {
  3343. struct pt_core_data *cd = dev_get_drvdata(dev);
  3344. if (protect)
  3345. return pt_pip_get_config_row_size(cd, row_size);
  3346. return pt_pip_get_config_row_size_(cd, row_size);
  3347. }
  3348. /*******************************************************************************
  3349. * FUNCTION: pt_pip1_read_data_block_
  3350. *
  3351. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3352. * output data to the "read_buf" and update "crc".
  3353. *
  3354. * RETURN:
  3355. * 0 = success
  3356. * !0 = failure
  3357. *
  3358. * PARAMETERS:
  3359. * *cd - pointer to core data
  3360. * row_number - row number
  3361. * length - length of data to read
  3362. * ebid - block id
  3363. * *actual_read_len - Actual data length read
  3364. * *read_buf - pointer to the buffer to store read data
  3365. * read_buf_size - size of read_buf
  3366. * *crc - pointer to store CRC of row data
  3367. ******************************************************************************/
  3368. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3369. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3370. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3371. {
  3372. int read_ebid;
  3373. int status;
  3374. int rc = 0;
  3375. int write_length = 5;
  3376. u8 write_buf[5];
  3377. u8 cmd_offset = 0;
  3378. u16 calc_crc;
  3379. struct pt_hid_output hid_output = {
  3380. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3381. .write_length = write_length,
  3382. .write_buf = write_buf,
  3383. };
  3384. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3385. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3386. write_buf[cmd_offset++] = LOW_BYTE(length);
  3387. write_buf[cmd_offset++] = HI_BYTE(length);
  3388. write_buf[cmd_offset++] = ebid;
  3389. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3390. if (rc)
  3391. return rc;
  3392. status = cd->response_buf[5];
  3393. if (status)
  3394. return status;
  3395. read_ebid = cd->response_buf[6];
  3396. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3397. return -EPROTO;
  3398. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3399. if (length == 0 || *actual_read_len == 0)
  3400. return 0;
  3401. if (read_buf_size >= *actual_read_len &&
  3402. *actual_read_len < PT_MAX_PIP2_MSG_SIZE)
  3403. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3404. else
  3405. return -EPROTO;
  3406. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3407. /* Validate Row Data CRC */
  3408. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3409. if (*crc == calc_crc) {
  3410. return 0;
  3411. } else {
  3412. pt_debug(cd->dev, DL_ERROR,
  3413. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3414. __func__, *crc, calc_crc);
  3415. return -EPROTO;
  3416. }
  3417. }
  3418. /*******************************************************************************
  3419. * FUNCTION: _pt_request_pip_read_data_block
  3420. *
  3421. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3422. * calls to pt_pip1_read_data_block_
  3423. *
  3424. * RETURN:
  3425. * 0 = success
  3426. * !0 = failure
  3427. *
  3428. * PARAMETERS:
  3429. * *dev - pointer to device structure
  3430. * row_number - row number
  3431. * length - length of data to read
  3432. * ebid - block id
  3433. * *actual_read_len - Actual data length read
  3434. * *read_buf - pointer to the buffer to store read data
  3435. * *crc - pointer to store CRC of row data
  3436. ******************************************************************************/
  3437. static int _pt_request_pip_read_data_block(struct device *dev,
  3438. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3439. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3440. {
  3441. struct pt_core_data *cd = dev_get_drvdata(dev);
  3442. return pt_pip1_read_data_block_(cd, row_number, length,
  3443. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3444. }
  3445. /*******************************************************************************
  3446. * FUNCTION: pt_pip1_write_data_block_
  3447. *
  3448. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3449. * write data to the data block.
  3450. *
  3451. * RETURN:
  3452. * 0 = success
  3453. * !0 = failure
  3454. *
  3455. * PARAMETERS:
  3456. * *cd - pointer to core data
  3457. * row_number - row in config block to write to
  3458. * write_length - length of data to write
  3459. * ebid - enumerated block ID
  3460. * *write_buf - pointer to buffer to write
  3461. * *security_key - pointer to security key to allow write
  3462. * *actual_write_len - pointer to store data length actually written
  3463. ******************************************************************************/
  3464. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3465. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3466. u8 *security_key, u16 *actual_write_len)
  3467. {
  3468. /* row_number + write_len + ebid + security_key + crc */
  3469. u16 full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3470. u8 *full_write_buf;
  3471. u8 cmd_offset = 0;
  3472. u16 crc;
  3473. int status;
  3474. int rc = 0;
  3475. int read_ebid;
  3476. u8 *data;
  3477. struct pt_hid_output hid_output = {
  3478. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3479. .write_length = full_write_length,
  3480. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3481. };
  3482. if (write_length > PT_CAL_DATA_ROW_SIZE)
  3483. return -EINVAL;
  3484. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3485. if (!full_write_buf)
  3486. return -ENOMEM;
  3487. hid_output.write_buf = full_write_buf;
  3488. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3489. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3490. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3491. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3492. full_write_buf[cmd_offset++] = ebid;
  3493. data = &full_write_buf[cmd_offset];
  3494. memcpy(data, write_buf, write_length);
  3495. cmd_offset += write_length;
  3496. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3497. cmd_offset += 8;
  3498. crc = _pt_compute_crc(data, write_length);
  3499. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3500. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3501. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3502. if (rc)
  3503. goto exit;
  3504. status = cd->response_buf[5];
  3505. if (status) {
  3506. rc = -EINVAL;
  3507. goto exit;
  3508. }
  3509. read_ebid = cd->response_buf[6];
  3510. if (read_ebid != ebid) {
  3511. rc = -EPROTO;
  3512. goto exit;
  3513. }
  3514. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3515. exit:
  3516. kfree(full_write_buf);
  3517. return rc;
  3518. }
  3519. /*******************************************************************************
  3520. * FUNCTION: _pt_request_pip_write_data_block
  3521. *
  3522. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3523. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3524. *
  3525. * RETURN:
  3526. * 0 = success
  3527. * !0 = failure
  3528. *
  3529. * PARAMETERS:
  3530. * *dev - pointer to device structure
  3531. * protect - flag to call protected or non-protected
  3532. * row_number - row in config block to write to
  3533. * write_length - length of data to write
  3534. * ebid - enumerated block ID
  3535. * *write_buf - pointer to buffer to write
  3536. * *security_key - pointer to security key to allow write
  3537. * *actual_write_len - pointer to store data length actually written
  3538. ******************************************************************************/
  3539. static int _pt_request_pip_write_data_block(struct device *dev,
  3540. u16 row_number, u16 write_length, u8 ebid,
  3541. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3542. {
  3543. struct pt_core_data *cd = dev_get_drvdata(dev);
  3544. return pt_pip1_write_data_block_(cd, row_number,
  3545. write_length, ebid, write_buf, security_key,
  3546. actual_write_len);
  3547. }
  3548. /*******************************************************************************
  3549. * FUNCTION: pt_pip_get_data_structure_
  3550. *
  3551. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3552. * returning a structure of data defined by data_id
  3553. *
  3554. * RETURN:
  3555. * 0 = success
  3556. * !0 = failure
  3557. *
  3558. * PARAMETERS:
  3559. * *cd - pointer to core data
  3560. * read_offset - read pointer offset
  3561. * read_length - length of data to read
  3562. * data_id - data ID to read
  3563. * *status - pointer to store the read response status
  3564. * *data_format - pointer to store format of data read
  3565. * *actual_read_len - pointer to store data length actually read
  3566. * *data - pointer to store data read
  3567. ******************************************************************************/
  3568. static int pt_pip_get_data_structure_(
  3569. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3570. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3571. u8 *data)
  3572. {
  3573. int rc = 0;
  3574. u16 total_read_len = 0;
  3575. u16 read_len;
  3576. u16 off_buf = 0;
  3577. u8 write_buf[5];
  3578. u8 read_data_id;
  3579. struct pt_hid_output hid_output = {
  3580. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3581. .write_length = 5,
  3582. .write_buf = write_buf,
  3583. };
  3584. again:
  3585. write_buf[0] = LOW_BYTE(read_offset);
  3586. write_buf[1] = HI_BYTE(read_offset);
  3587. write_buf[2] = LOW_BYTE(read_length);
  3588. write_buf[3] = HI_BYTE(read_length);
  3589. write_buf[4] = data_id;
  3590. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3591. if (rc)
  3592. return rc;
  3593. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3594. goto set_status;
  3595. read_data_id = cd->response_buf[6];
  3596. if (read_data_id != data_id)
  3597. return -EPROTO;
  3598. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3599. if (read_len && data) {
  3600. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3601. total_read_len += read_len;
  3602. if (read_len < read_length) {
  3603. read_offset += read_len;
  3604. off_buf += read_len;
  3605. read_length -= read_len;
  3606. goto again;
  3607. }
  3608. }
  3609. if (data_format)
  3610. *data_format = cd->response_buf[9];
  3611. if (actual_read_len)
  3612. *actual_read_len = total_read_len;
  3613. set_status:
  3614. if (status)
  3615. *status = cd->response_buf[5];
  3616. return rc;
  3617. }
  3618. /*******************************************************************************
  3619. * FUNCTION: pt_pip_get_data_structure
  3620. *
  3621. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3622. * an exclusive access to the DUT.
  3623. *
  3624. * RETURN:
  3625. * 0 = success
  3626. * !0 = failure
  3627. *
  3628. * PARAMETERS:
  3629. * *cd - pointer to core data
  3630. * read_offset - read pointer offset
  3631. * read_length - length of data to read
  3632. * data_id - data ID to read
  3633. * *status - pointer to store the read response status
  3634. * *data_format - pointer to store format of data read
  3635. * *actual_read_len - pointer to store data length actually read
  3636. * *data - pointer to store data read
  3637. ******************************************************************************/
  3638. static int pt_pip_get_data_structure(
  3639. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3640. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3641. u8 *data)
  3642. {
  3643. int rc;
  3644. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3645. if (rc < 0) {
  3646. pt_debug(cd->dev, DL_ERROR,
  3647. "%s: fail get exclusive ex=%p own=%p\n",
  3648. __func__, cd->exclusive_dev, cd->dev);
  3649. return rc;
  3650. }
  3651. rc = pt_pip_get_data_structure_(cd, read_offset,
  3652. read_length, data_id, status, data_format,
  3653. actual_read_len, data);
  3654. if (release_exclusive(cd, cd->dev) < 0)
  3655. pt_debug(cd->dev, DL_ERROR,
  3656. "%s: fail to release exclusive\n", __func__);
  3657. return rc;
  3658. }
  3659. /*******************************************************************************
  3660. * FUNCTION: _pt_request_pip_get_data_structure
  3661. *
  3662. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3663. * calls to the protected or unprotected call to
  3664. * pt_pip_get_data_structure
  3665. *
  3666. * RETURN:
  3667. * 0 = success
  3668. * !0 = failure
  3669. *
  3670. * PARAMETERS:
  3671. * *dev - pointer to device structure
  3672. * protect - flag to call protected or non-protected
  3673. * read_offset - read pointer offset
  3674. * read_length - length of data to read
  3675. * data_id - data ID to read
  3676. * *status - pointer to store the read response status
  3677. * *data_format - pointer to store format of data read
  3678. * *actual_read_len - pointer to store data length actually read
  3679. * *data - pointer to store data read
  3680. ******************************************************************************/
  3681. static int _pt_request_pip_get_data_structure(struct device *dev,
  3682. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3683. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3684. {
  3685. struct pt_core_data *cd = dev_get_drvdata(dev);
  3686. if (protect)
  3687. return pt_pip_get_data_structure(cd,
  3688. read_offset, read_length, data_id, status,
  3689. data_format, actual_read_len, data);
  3690. return pt_pip_get_data_structure_(cd,
  3691. read_offset, read_length, data_id, status,
  3692. data_format, actual_read_len, data);
  3693. }
  3694. /*******************************************************************************
  3695. * FUNCTION: _pt_manage_local_cal_data
  3696. *
  3697. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3698. * CALIBRATION data. It stores it in a local static array and can be
  3699. * cleared, loaded or used to restore the CAL data back to the running FW.
  3700. * The CAL data is read or restored by use of the PIP1 commands:
  3701. * - READ_DATA_BLOCK (0x22)
  3702. * - WRITE_DATA_BLOCK (0x23)
  3703. *
  3704. * RETURN:
  3705. * 0 = success
  3706. * !0 = failure
  3707. *
  3708. * PARAMETERS:
  3709. * *dev - pointer to device structure
  3710. * action - One of the following actions:
  3711. * - PT_CAL_DATA_SAVE
  3712. * - PT_CAL_DATA_RESTORE
  3713. * - PT_CAL_DATA_CLEAR
  3714. * - PT_CAL_DATA_SIZE
  3715. * *size - pointer to the number of bytes transferred
  3716. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3717. ******************************************************************************/
  3718. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3719. unsigned short *crc)
  3720. {
  3721. struct pt_core_data *cd = dev_get_drvdata(dev);
  3722. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3723. unsigned short calc_id_crc = 0;
  3724. static u8 *cal_cache_data;
  3725. static u16 cal_cache_len;
  3726. static unsigned short cal_cache_chip_id;
  3727. int rc = 0;
  3728. u8 *tmp_data = NULL;
  3729. u8 row_number = 0;
  3730. u8 prefix[20];
  3731. u16 cal_size = 0;
  3732. u16 transfer_size;
  3733. u16 act_trans_len = 0;
  3734. u16 byte_offset = 0;
  3735. u16 cal_blk_size;
  3736. u16 total_rows;
  3737. u16 remain_bytes;
  3738. u16 data_block_crc;
  3739. u16 buf_size = 12;
  3740. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3741. __func__, action);
  3742. switch (action) {
  3743. case PT_CAL_DATA_SAVE:
  3744. /* Read the size of the CAL block and calculate # rows */
  3745. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3746. if (!tmp_data) {
  3747. rc = -ENOMEM;
  3748. goto exit;
  3749. }
  3750. /*
  3751. * Don't check rc as doing a read size will give a false
  3752. * error on the CRC check.
  3753. */
  3754. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3755. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3756. cal_blk_size = act_trans_len;
  3757. kfree(tmp_data);
  3758. pt_debug(dev, DL_INFO,
  3759. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3760. __func__, cal_cache_len, cal_blk_size);
  3761. /* Safety net to ensure we didn't read incorrect size */
  3762. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3763. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3764. __func__);
  3765. rc = 1;
  3766. goto exit;
  3767. }
  3768. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3769. if (cal_blk_size != cal_cache_len) {
  3770. kfree(cal_cache_data);
  3771. cal_cache_data = kzalloc(cal_blk_size + 2,
  3772. GFP_KERNEL);
  3773. if (!cal_cache_data) {
  3774. rc = -ENOMEM;
  3775. goto exit;
  3776. }
  3777. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3778. __func__);
  3779. }
  3780. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3781. /* Calculate how many rows [0-n] (PIP Transactions) */
  3782. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3783. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3784. /* Add row if we have a last partial row */
  3785. if (remain_bytes > 0)
  3786. total_rows++;
  3787. pt_debug(dev, DL_INFO,
  3788. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3789. __func__, cal_blk_size, total_rows, remain_bytes);
  3790. /* Read all rows unless an error occurs */
  3791. rc = 0;
  3792. while (rc == 0 && row_number <= total_rows) {
  3793. act_trans_len = 0;
  3794. if (remain_bytes > 0 && row_number == total_rows)
  3795. transfer_size = remain_bytes;
  3796. else
  3797. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3798. rc = pt_pip1_read_data_block_(cd, row_number,
  3799. transfer_size, PT_CAL_EBID,
  3800. &act_trans_len,
  3801. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3802. &data_block_crc);
  3803. if (rc) {
  3804. /* Error occurred, exit loop */
  3805. cal_size = 0;
  3806. break;
  3807. }
  3808. pt_debug(dev, DL_INFO,
  3809. "%s: CAL read rc=%d actual read len=%d\n",
  3810. __func__, rc, act_trans_len);
  3811. byte_offset += act_trans_len;
  3812. cal_size = byte_offset;
  3813. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3814. pt_pr_buf(dev, DL_INFO,
  3815. &cal_cache_data[byte_offset - act_trans_len],
  3816. act_trans_len, prefix);
  3817. row_number++;
  3818. }
  3819. if (cal_size > 0) {
  3820. /* Save a CRC of the chip info the CAL was saved from */
  3821. calc_id_crc = crc_ccitt_calculate(
  3822. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3823. cal_cache_chip_id = calc_id_crc;
  3824. cal_cache_len = cal_size;
  3825. pt_debug(dev, DL_INFO,
  3826. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3827. __func__, calc_id_crc, cal_size);
  3828. }
  3829. *size = cal_size;
  3830. *crc = calc_id_crc;
  3831. break;
  3832. case PT_CAL_DATA_RESTORE:
  3833. cal_size = cal_cache_len;
  3834. while ((rc == 0) && (byte_offset < cal_size)) {
  3835. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3836. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3837. else
  3838. transfer_size = cal_size - byte_offset;
  3839. rc = pt_pip1_write_data_block_(cd, row_number,
  3840. transfer_size, PT_CAL_EBID,
  3841. &cal_cache_data[byte_offset],
  3842. (u8 *)pt_data_block_security_key,
  3843. &act_trans_len);
  3844. byte_offset += act_trans_len;
  3845. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3846. __func__, byte_offset);
  3847. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3848. pt_pr_buf(dev, DL_INFO,
  3849. &cal_cache_data[byte_offset - act_trans_len],
  3850. act_trans_len, prefix);
  3851. if ((byte_offset > cal_size) ||
  3852. (act_trans_len != transfer_size))
  3853. rc = -EIO;
  3854. row_number++;
  3855. }
  3856. *size = byte_offset;
  3857. *crc = cal_cache_chip_id;
  3858. break;
  3859. case PT_CAL_DATA_CLEAR:
  3860. if (cal_cache_data)
  3861. memset(&cal_cache_data[0], 0, cal_cache_len);
  3862. cal_cache_len = 0;
  3863. cal_cache_chip_id = 0;
  3864. *size = 0;
  3865. *crc = 0;
  3866. break;
  3867. case PT_CAL_DATA_INFO:
  3868. default:
  3869. *size = cal_cache_len;
  3870. *crc = cal_cache_chip_id;
  3871. pt_debug(dev, DL_INFO,
  3872. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3873. __func__, cal_cache_chip_id,
  3874. cal_cache_len);
  3875. break;
  3876. }
  3877. exit:
  3878. pt_debug(dev, DL_INFO,
  3879. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3880. __func__, rc, *crc, *size);
  3881. return rc;
  3882. }
  3883. /*******************************************************************************
  3884. * FUNCTION: pt_pip_run_selftest_
  3885. *
  3886. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3887. * to execute a FW built in self test
  3888. *
  3889. * RETURN:
  3890. * 0 = success
  3891. * !0 = failure
  3892. *
  3893. * PARAMETERS:
  3894. * *cd - pointer to core data
  3895. * test_id - enumerated test ID to run
  3896. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3897. * *status - pointer to store the read response status
  3898. * *summary_results - pointer to store the results summary
  3899. * *results_available - pointer to store if results are available
  3900. *****************************************************************************/
  3901. static int pt_pip_run_selftest_(
  3902. struct pt_core_data *cd, u8 test_id,
  3903. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3904. u8 *results_available)
  3905. {
  3906. int rc = 0;
  3907. u8 write_buf[2];
  3908. struct pt_hid_output hid_output = {
  3909. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3910. .write_length = 2,
  3911. .write_buf = write_buf,
  3912. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3913. };
  3914. write_buf[0] = test_id;
  3915. write_buf[1] = write_idacs_to_flash;
  3916. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3917. hid_output.write_length = 1;
  3918. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3919. if (rc)
  3920. return rc;
  3921. if (status)
  3922. *status = cd->response_buf[5];
  3923. if (summary_result)
  3924. *summary_result = cd->response_buf[6];
  3925. /* results_available only available before PIP 1.03 */
  3926. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3927. if (results_available)
  3928. *results_available = cd->response_buf[7];
  3929. }
  3930. return rc;
  3931. }
  3932. /*******************************************************************************
  3933. * FUNCTION: pt_pip_run_selftest
  3934. *
  3935. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3936. * an exclusive access to the DUT.
  3937. *
  3938. * RETURN:
  3939. * 0 = success
  3940. * !0 = failure
  3941. *
  3942. * PARAMETERS:
  3943. * *cd - pointer to core data
  3944. * test_id - enumerated test ID to run
  3945. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3946. * *status - pointer to store the read response status
  3947. * *summary_results - pointer to store the results summary
  3948. * *results_available - pointer to store if results are available
  3949. ******************************************************************************/
  3950. static int pt_pip_run_selftest(
  3951. struct pt_core_data *cd, u8 test_id,
  3952. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3953. u8 *results_available)
  3954. {
  3955. int rc;
  3956. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3957. if (rc < 0) {
  3958. pt_debug(cd->dev, DL_ERROR,
  3959. "%s: fail get exclusive ex=%p own=%p\n",
  3960. __func__, cd->exclusive_dev, cd->dev);
  3961. return rc;
  3962. }
  3963. rc = pt_pip_run_selftest_(cd, test_id,
  3964. write_idacs_to_flash, status, summary_result,
  3965. results_available);
  3966. if (release_exclusive(cd, cd->dev) < 0)
  3967. pt_debug(cd->dev, DL_ERROR,
  3968. "%s: fail to release exclusive\n", __func__);
  3969. return rc;
  3970. }
  3971. /*******************************************************************************
  3972. * FUNCTION: _pt_request_pip_run_selftest
  3973. *
  3974. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3975. * calls to the protected or unprotected call to pt_pip_run_selftest
  3976. *
  3977. * RETURN:
  3978. * 0 = success
  3979. * !0 = failure
  3980. *
  3981. * PARAMETERS:
  3982. * *dev - pointer to device structure
  3983. * protect - flag to call protected or non-protected
  3984. * test_id - enumerated test ID to run
  3985. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3986. * *status - pointer to store the read response status
  3987. * *summary_results - pointer to store the results summary
  3988. * *results_available - pointer to store if results are available
  3989. ******************************************************************************/
  3990. static int _pt_request_pip_run_selftest(struct device *dev,
  3991. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3992. u8 *summary_result, u8 *results_available)
  3993. {
  3994. struct pt_core_data *cd = dev_get_drvdata(dev);
  3995. if (protect)
  3996. return pt_pip_run_selftest(cd, test_id,
  3997. write_idacs_to_flash, status, summary_result,
  3998. results_available);
  3999. return pt_pip_run_selftest_(cd, test_id,
  4000. write_idacs_to_flash, status, summary_result,
  4001. results_available);
  4002. }
  4003. /*******************************************************************************
  4004. * FUNCTION: _pt_pip_get_selftest_result_
  4005. *
  4006. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  4007. * to retrieve the self test results from the self test already executed
  4008. *
  4009. * RETURN:
  4010. * 0 = success
  4011. * !0 = failure
  4012. *
  4013. * PARAMETERS:
  4014. * *cd - pointer to core data
  4015. * read_offset - read pointer offset
  4016. * read_length - length of data to read
  4017. * test_id - enumerated test ID to read selftest results from
  4018. * *status - pointer to store the read response status
  4019. * *actual_read_len - pointer to store data length actually read
  4020. * *status - pointer to where the cmd response statas is stored
  4021. ******************************************************************************/
  4022. static int pt_pip_get_selftest_result_(
  4023. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4024. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4025. {
  4026. int rc = 0;
  4027. u16 total_read_len = 0;
  4028. u16 read_len;
  4029. u16 off_buf = 0;
  4030. u8 write_buf[5];
  4031. u8 read_test_id;
  4032. bool repeat;
  4033. struct pt_hid_output hid_output = {
  4034. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4035. .write_length = 5,
  4036. .write_buf = write_buf,
  4037. };
  4038. /*
  4039. * Do not repeat reading for Auto Shorts test
  4040. * when PIP version < 1.3
  4041. */
  4042. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4043. || test_id != PT_ST_ID_AUTOSHORTS;
  4044. again:
  4045. write_buf[0] = LOW_BYTE(read_offset);
  4046. write_buf[1] = HI_BYTE(read_offset);
  4047. write_buf[2] = LOW_BYTE(read_length);
  4048. write_buf[3] = HI_BYTE(read_length);
  4049. write_buf[4] = test_id;
  4050. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4051. if (rc)
  4052. return rc;
  4053. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4054. goto set_status;
  4055. read_test_id = cd->response_buf[6];
  4056. if (read_test_id != test_id)
  4057. return -EPROTO;
  4058. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4059. if (read_len && data) {
  4060. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4061. total_read_len += read_len;
  4062. if (repeat && read_len < read_length) {
  4063. read_offset += read_len;
  4064. off_buf += read_len;
  4065. read_length -= read_len;
  4066. goto again;
  4067. }
  4068. }
  4069. if (actual_read_len)
  4070. *actual_read_len = total_read_len;
  4071. set_status:
  4072. if (status)
  4073. *status = cd->response_buf[5];
  4074. return rc;
  4075. }
  4076. /*******************************************************************************
  4077. * FUNCTION: _pt_pip_get_selftest_result
  4078. *
  4079. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4080. * access to the DUT
  4081. *
  4082. * RETURN:
  4083. * 0 = success
  4084. * !0 = failure
  4085. *
  4086. * PARAMETERS:
  4087. * *cd - pointer to core data
  4088. * read_offset - read pointer offset
  4089. * read_length - length of data to read
  4090. * test_id - enumerated test ID to read selftest results from
  4091. * *status - pointer to store the read response status
  4092. * *actual_read_len - pointer to store data length actually read
  4093. * *status - pointer to where the cmd response statas is stored
  4094. ******************************************************************************/
  4095. static int pt_pip_get_selftest_result(
  4096. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4097. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4098. {
  4099. int rc;
  4100. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4101. if (rc < 0) {
  4102. pt_debug(cd->dev, DL_ERROR,
  4103. "%s: fail get exclusive ex=%p own=%p\n",
  4104. __func__, cd->exclusive_dev, cd->dev);
  4105. return rc;
  4106. }
  4107. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4108. read_length, test_id, status, actual_read_len, data);
  4109. if (release_exclusive(cd, cd->dev) < 0)
  4110. pt_debug(cd->dev, DL_ERROR,
  4111. "%s: fail to release exclusive\n", __func__);
  4112. return rc;
  4113. }
  4114. /*******************************************************************************
  4115. * FUNCTION: _pt_request_pip_get_selftest_result
  4116. *
  4117. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4118. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4119. *
  4120. * RETURN:
  4121. * 0 = success
  4122. * !0 = failure
  4123. *
  4124. * PARAMETERS:
  4125. * *dev - pointer to device structure
  4126. * protect - flag to call protected or non-protected
  4127. * read_offset - read pointer offset
  4128. * read_length - length of data to read
  4129. * test_id - enumerated test ID to read selftest results from
  4130. * *status - pointer to store the read response status
  4131. * *actual_read_len - pointer to store data length actually read
  4132. * *data - pointer to where the data read is stored
  4133. ******************************************************************************/
  4134. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4135. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4136. u8 *status, u16 *actual_read_len, u8 *data)
  4137. {
  4138. struct pt_core_data *cd = dev_get_drvdata(dev);
  4139. if (protect)
  4140. return pt_pip_get_selftest_result(cd, read_offset,
  4141. read_length, test_id, status, actual_read_len,
  4142. data);
  4143. return pt_pip_get_selftest_result_(cd, read_offset,
  4144. read_length, test_id, status, actual_read_len,
  4145. data);
  4146. }
  4147. /*******************************************************************************
  4148. * FUNCTION: _pt_pip_load_self_test_param
  4149. *
  4150. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4151. * to load parameters needed by a self test
  4152. *
  4153. * RETURN:
  4154. * 0 = success
  4155. * !0 = failure
  4156. *
  4157. * PARAMETERS:
  4158. * *cd - pointer to core data
  4159. * self_test_id - enumerated test ID for which the parmeters belong
  4160. * load_offset - mem offset to where to load parameters
  4161. * load_length - length of parameter data to load
  4162. * *parameters - pointer to list of parameter data
  4163. * *status - pointer to store the response status
  4164. * *ret_test_id - pointer to returned test id the parameters were stored
  4165. * *act_load_len - pointer to store the actual load length that was writen
  4166. ******************************************************************************/
  4167. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4168. u8 self_test_id, u16 load_offset, u16 load_length,
  4169. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4170. {
  4171. int rc = 0;
  4172. int i;
  4173. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4174. struct pt_hid_output hid_output = {
  4175. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4176. .write_length = 5 + load_length,
  4177. .write_buf = write_buf,
  4178. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4179. };
  4180. write_buf[0] = LOW_BYTE(load_offset);
  4181. write_buf[1] = HI_BYTE(load_offset);
  4182. write_buf[2] = LOW_BYTE(load_length);
  4183. write_buf[3] = HI_BYTE(load_length);
  4184. write_buf[4] = self_test_id;
  4185. for (i = 0; i < load_length; i++)
  4186. write_buf[i + 5] = parameters[i];
  4187. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4188. if (rc)
  4189. return rc;
  4190. if (status)
  4191. *status = cd->response_buf[5];
  4192. if (ret_test_id)
  4193. *ret_test_id = cd->response_buf[6];
  4194. if (act_load_len)
  4195. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4196. return rc;
  4197. }
  4198. /*******************************************************************************
  4199. * FUNCTION: pt_pip_load_self_test_param
  4200. *
  4201. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4202. * access to the DUT.
  4203. *
  4204. * RETURN:
  4205. * 0 = success
  4206. * !0 = failure
  4207. *
  4208. * PARAMETERS:
  4209. * *cd - pointer to core data
  4210. * self_test_id - enumerated test ID for which the parmeters belong
  4211. * load_offset - mem offset to where to load parameters
  4212. * load_length - length of parameter data to load
  4213. * *parameters - pointer to list of parameter data
  4214. * *status - pointer to store the response status
  4215. * *ret_test_id - pointer to returned test id the parameters were stored
  4216. * *act_load_len - pointer to store the actual load length that was writen
  4217. ******************************************************************************/
  4218. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4219. u8 self_test_id, u16 load_offset, u16 load_length,
  4220. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4221. {
  4222. int rc;
  4223. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4224. if (rc < 0) {
  4225. pt_debug(cd->dev, DL_ERROR,
  4226. "%s: fail get exclusive ex=%p own=%p\n",
  4227. __func__, cd->exclusive_dev, cd->dev);
  4228. return rc;
  4229. }
  4230. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4231. load_length, parameters, status, ret_test_id, act_load_len);
  4232. if (release_exclusive(cd, cd->dev) < 0)
  4233. pt_debug(cd->dev, DL_ERROR,
  4234. "%s: fail to release exclusive\n", __func__);
  4235. return rc;
  4236. }
  4237. /*******************************************************************************
  4238. * FUNCTION: _pt_request_pip_load_self_test_param
  4239. *
  4240. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4241. * calls to the protected or unprotected call to
  4242. * pt_pip_load_self_test_param
  4243. *
  4244. * RETURN:
  4245. * 0 = success
  4246. * !0 = failure
  4247. *
  4248. * PARAMETERS:
  4249. * *dev - pointer to device structure
  4250. * protect - flag to call protected or non-protected
  4251. * self_test_id - enumerated test ID for which the parmeters belong
  4252. * load_offset - mem offset to where to load parameters
  4253. * load_length - length of parameter data to load
  4254. * *parameters - pointer to list of parameter data
  4255. * *status - pointer to store the response status
  4256. * *ret_test_id - pointer to returned test id the parameters were stored
  4257. * *act_load_len - pointer to store the actual load length that was writen
  4258. ******************************************************************************/
  4259. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4260. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4261. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4262. {
  4263. struct pt_core_data *cd = dev_get_drvdata(dev);
  4264. if (protect)
  4265. return pt_pip_load_self_test_param(cd, self_test_id,
  4266. load_offset, load_length, parameters, status, ret_test_id,
  4267. act_load_len);
  4268. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4269. load_length, parameters, status, ret_test_id, act_load_len);
  4270. }
  4271. /*******************************************************************************
  4272. * FUNCTION: pt_pip_calibrate_ext_
  4273. *
  4274. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4275. * for the response
  4276. *
  4277. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4278. *
  4279. * RETURN:
  4280. * 0 = success
  4281. * !0 = failure
  4282. *
  4283. * PARAMETERS:
  4284. * *cd - pointer to core data
  4285. * *cal_data - pointer to extended calibration data structure
  4286. * *status - pointer to where the command response status is stored
  4287. ******************************************************************************/
  4288. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4289. struct pt_cal_ext_data *cal_data, u8 *status)
  4290. {
  4291. int rc = 0;
  4292. int write_length = 4;
  4293. u8 write_buf[4];
  4294. u16 size = 0;
  4295. unsigned short crc = 0;
  4296. struct pt_hid_output hid_output = {
  4297. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4298. .write_length = write_length,
  4299. .write_buf = write_buf,
  4300. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4301. };
  4302. if (cal_data == NULL)
  4303. return -EINVAL;
  4304. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4305. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4306. if (rc)
  4307. return rc;
  4308. if (status)
  4309. *status = cd->response_buf[5];
  4310. /*
  4311. * When doing a calibration on a flashless DUT, save CAL data in
  4312. * the TTDL cache on any successful calibration
  4313. */
  4314. if (cd->response_buf[5] == 0 && cd->cal_cache_in_host) {
  4315. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4316. __func__);
  4317. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4318. &size, &crc);
  4319. if (rc)
  4320. pt_debug(cd->dev, DL_ERROR,
  4321. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4322. else
  4323. pt_debug(cd->dev, DL_INFO,
  4324. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4325. __func__, crc, size);
  4326. }
  4327. return 0;
  4328. }
  4329. /*******************************************************************************
  4330. * FUNCTION: pt_pip_calibrate_ext
  4331. *
  4332. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4333. * DUT
  4334. *
  4335. * RETURN:
  4336. * 0 = success
  4337. * !0 = failure
  4338. *
  4339. * PARAMETERS:
  4340. * *cd - pointer to core data
  4341. * *cal_data - pointer to extended calibration data structure
  4342. * *status - pointer to where the command response status is stored
  4343. ******************************************************************************/
  4344. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4345. struct pt_cal_ext_data *cal_data, u8 *status)
  4346. {
  4347. int rc;
  4348. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4349. if (rc < 0) {
  4350. pt_debug(cd->dev, DL_ERROR,
  4351. "%s: fail get exclusive ex=%p own=%p\n",
  4352. __func__, cd->exclusive_dev, cd->dev);
  4353. return rc;
  4354. }
  4355. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4356. if (release_exclusive(cd, cd->dev) < 0)
  4357. pt_debug(cd->dev, DL_ERROR,
  4358. "%s: fail to release exclusive\n", __func__);
  4359. return rc;
  4360. }
  4361. /*******************************************************************************
  4362. * FUNCTION: _pt_request_pip_calibrate_ext
  4363. *
  4364. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4365. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4366. *
  4367. * RETURN:
  4368. * 0 = success
  4369. * !0 = failure
  4370. *
  4371. * PARAMETERS:
  4372. * *dev - pointer to device structure
  4373. * protect - flag to call protected or non-protected
  4374. * *cal_data - pointer to extended calibration data structure
  4375. * *status - pointer to where the command response status is stored
  4376. ******************************************************************************/
  4377. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4378. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4379. {
  4380. struct pt_core_data *cd = dev_get_drvdata(dev);
  4381. if (protect)
  4382. return pt_pip_calibrate_ext(cd, cal_data, status);
  4383. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4384. }
  4385. /*******************************************************************************
  4386. * FUNCTION: pt_pip_calibrate_idacs_
  4387. *
  4388. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4389. * for the response
  4390. *
  4391. * RETURN:
  4392. * 0 = success
  4393. * !0 = failure
  4394. *
  4395. * PARAMETERS:
  4396. * *cd - pointer to core data
  4397. * mode - sense mode to calibrate (0-5)
  4398. * *status - pointer to where the command response status is stored
  4399. ******************************************************************************/
  4400. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4401. u8 mode, u8 *status)
  4402. {
  4403. int rc = 0;
  4404. int write_length = 1;
  4405. u8 write_buf[1];
  4406. u8 cmd_offset = 0;
  4407. struct pt_hid_output hid_output = {
  4408. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4409. .write_length = write_length,
  4410. .write_buf = write_buf,
  4411. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4412. };
  4413. write_buf[cmd_offset++] = mode;
  4414. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4415. if (rc)
  4416. return rc;
  4417. *status = cd->response_buf[5];
  4418. if (*status)
  4419. return -EINVAL;
  4420. return 0;
  4421. }
  4422. /*******************************************************************************
  4423. * FUNCTION: pt_pip_calibrate_idacs
  4424. *
  4425. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4426. * access to the DUT
  4427. *
  4428. * RETURN:
  4429. * 0 = success
  4430. * !0 = failure
  4431. *
  4432. * PARAMETERS:
  4433. * *cd - pointer to core data
  4434. * mode - sense mode to calibrate (0-5)
  4435. * *status - pointer to where the command response status is stored
  4436. ******************************************************************************/
  4437. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4438. u8 mode, u8 *status)
  4439. {
  4440. int rc;
  4441. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4442. if (rc < 0) {
  4443. pt_debug(cd->dev, DL_ERROR,
  4444. "%s: fail get exclusive ex=%p own=%p\n",
  4445. __func__, cd->exclusive_dev, cd->dev);
  4446. return rc;
  4447. }
  4448. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4449. if (release_exclusive(cd, cd->dev) < 0)
  4450. pt_debug(cd->dev, DL_ERROR,
  4451. "%s: fail to release exclusive\n", __func__);
  4452. return rc;
  4453. }
  4454. /*******************************************************************************
  4455. * FUNCTION: _pt_request_pip_calibrate_idacs
  4456. *
  4457. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4458. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4459. *
  4460. * RETURN:
  4461. * 0 = success
  4462. * !0 = failure
  4463. *
  4464. * PARAMETERS:
  4465. * *dev - pointer to device structure
  4466. * protect - flag to call protected or non-protected
  4467. * mode - sense mode to calibrate (0-5)
  4468. * *status - pointer to where the command response status is stored
  4469. ******************************************************************************/
  4470. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4471. int protect, u8 mode, u8 *status)
  4472. {
  4473. struct pt_core_data *cd = dev_get_drvdata(dev);
  4474. if (protect)
  4475. return pt_pip_calibrate_idacs(cd, mode, status);
  4476. return pt_pip_calibrate_idacs_(cd, mode, status);
  4477. }
  4478. /*******************************************************************************
  4479. * FUNCTION: pt_hid_output_initialize_baselines_
  4480. *
  4481. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4482. * waiting for the response.
  4483. *
  4484. * RETURN:
  4485. * 0 = success
  4486. * !0 = failure
  4487. *
  4488. * PARAMETERS:
  4489. * *cd - pointer to core data
  4490. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4491. * each or together with a single command.
  4492. * *status - pointer to where the command response status is stored
  4493. ******************************************************************************/
  4494. static int pt_hid_output_initialize_baselines_(
  4495. struct pt_core_data *cd, u8 test_id, u8 *status)
  4496. {
  4497. int rc = 0;
  4498. int write_length = 1;
  4499. u8 write_buf[1];
  4500. u8 cmd_offset = 0;
  4501. struct pt_hid_output hid_output = {
  4502. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4503. .write_length = write_length,
  4504. .write_buf = write_buf,
  4505. };
  4506. write_buf[cmd_offset++] = test_id;
  4507. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4508. if (rc)
  4509. return rc;
  4510. *status = cd->response_buf[5];
  4511. if (*status)
  4512. return -EINVAL;
  4513. return rc;
  4514. }
  4515. /*******************************************************************************
  4516. * FUNCTION: pt_hid_output_initialize_baselines
  4517. *
  4518. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4519. * access to the DUT
  4520. *
  4521. * RETURN:
  4522. * 0 = success
  4523. * !0 = failure
  4524. *
  4525. * PARAMETERS:
  4526. * *cd - pointer to core data
  4527. * test_id - enumerated ID against which to initialize the baseline
  4528. * *status - pointer to where the command response status is stored
  4529. ******************************************************************************/
  4530. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4531. u8 test_id, u8 *status)
  4532. {
  4533. int rc;
  4534. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4535. if (rc < 0) {
  4536. pt_debug(cd->dev, DL_ERROR,
  4537. "%s: fail get exclusive ex=%p own=%p\n",
  4538. __func__, cd->exclusive_dev, cd->dev);
  4539. return rc;
  4540. }
  4541. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4542. if (release_exclusive(cd, cd->dev) < 0)
  4543. pt_debug(cd->dev, DL_ERROR,
  4544. "%s: fail to release exclusive\n", __func__);
  4545. return rc;
  4546. }
  4547. /*******************************************************************************
  4548. * FUNCTION: _pt_request_pip_initialize_baselines
  4549. *
  4550. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4551. * calls to the protected or unprotected call to
  4552. * pt_pip_initialize_baselines
  4553. *
  4554. * RETURN:
  4555. * 0 = success
  4556. * !0 = failure
  4557. *
  4558. * PARAMETERS:
  4559. * *dev - pointer to device structure
  4560. * protect - flag to call protected or non-protected
  4561. * test_id - enumerated ID against which to initialize the baseline
  4562. * *status - pointer to where the command response status is stored
  4563. ******************************************************************************/
  4564. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4565. int protect, u8 test_id, u8 *status)
  4566. {
  4567. struct pt_core_data *cd = dev_get_drvdata(dev);
  4568. if (protect)
  4569. return pt_hid_output_initialize_baselines(cd, test_id,
  4570. status);
  4571. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4572. }
  4573. /*******************************************************************************
  4574. * FUNCTION: pt_hid_output_exec_panel_scan_
  4575. *
  4576. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4577. * the response
  4578. *
  4579. * RETURN:
  4580. * 0 = success
  4581. * !0 = failure
  4582. *
  4583. * PARAMETERS:
  4584. * *cd - pointer to core data
  4585. ******************************************************************************/
  4586. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4587. {
  4588. struct pt_hid_output hid_output = {
  4589. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4590. };
  4591. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4592. }
  4593. /*******************************************************************************
  4594. * FUNCTION: pt_hid_output_exec_panel_scan
  4595. *
  4596. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4597. * access to the DUT.
  4598. *
  4599. * RETURN:
  4600. * 0 = success
  4601. * !0 = failure
  4602. *
  4603. * PARAMETERS:
  4604. * *cd - pointer to core data
  4605. ******************************************************************************/
  4606. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4607. {
  4608. int rc;
  4609. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4610. if (rc < 0) {
  4611. pt_debug(cd->dev, DL_ERROR,
  4612. "%s: fail get exclusive ex=%p own=%p\n",
  4613. __func__, cd->exclusive_dev, cd->dev);
  4614. return rc;
  4615. }
  4616. rc = pt_hid_output_exec_panel_scan_(cd);
  4617. if (release_exclusive(cd, cd->dev) < 0)
  4618. pt_debug(cd->dev, DL_ERROR,
  4619. "%s: fail to release exclusive\n", __func__);
  4620. return rc;
  4621. }
  4622. /*******************************************************************************
  4623. * FUNCTION: pt_pip2_exec_panel_scan_
  4624. *
  4625. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4626. * the response
  4627. *
  4628. * RETURN:
  4629. * 0 = success
  4630. * !0 = failure
  4631. *
  4632. * PARAMETERS:
  4633. * *cd - pointer to core data
  4634. * scan_type - type of panel scan to perform (PIP2 only)
  4635. ******************************************************************************/
  4636. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4637. {
  4638. int rc = 0;
  4639. u8 data[2];
  4640. u8 read_buf[10];
  4641. u16 actual_read_len;
  4642. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4643. __func__, scan_type);
  4644. data[0] = scan_type;
  4645. rc = _pt_request_pip2_send_cmd(cd->dev,
  4646. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4647. data, 1, read_buf, &actual_read_len);
  4648. if (rc) {
  4649. pt_debug(cd->dev, DL_ERROR,
  4650. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4651. __func__, scan_type, rc);
  4652. }
  4653. return rc;
  4654. }
  4655. /*******************************************************************************
  4656. * FUNCTION: pt_pip2_exec_panel_scan
  4657. *
  4658. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4659. * access to the DUT.
  4660. *
  4661. * RETURN:
  4662. * 0 = success
  4663. * !0 = failure
  4664. *
  4665. * PARAMETERS:
  4666. * *cd - pointer to core data
  4667. * scan_type - type of panel scan to perform (PIP2 only)
  4668. ******************************************************************************/
  4669. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4670. {
  4671. int rc;
  4672. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4673. if (rc < 0) {
  4674. pt_debug(cd->dev, DL_ERROR,
  4675. "%s: fail get exclusive ex=%p own=%p\n",
  4676. __func__, cd->exclusive_dev, cd->dev);
  4677. return rc;
  4678. }
  4679. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4680. if (release_exclusive(cd, cd->dev) < 0)
  4681. pt_debug(cd->dev, DL_ERROR,
  4682. "%s: fail to release exclusive\n", __func__);
  4683. return rc;
  4684. }
  4685. /*******************************************************************************
  4686. * FUNCTION: _pt_request_pip_exec_panel_scan
  4687. *
  4688. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4689. * calls to the protected or unprotected call to
  4690. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4691. *
  4692. * RETURN:
  4693. * 0 = success
  4694. * !0 = failure
  4695. *
  4696. * PARAMETERS:
  4697. * *dev - pointer to device structure
  4698. * protect - flag to call protected or non-protected
  4699. * scan_type - type of panel scan to perform (PIP2 only)
  4700. ******************************************************************************/
  4701. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4702. int protect, u8 scan_type)
  4703. {
  4704. struct pt_core_data *cd = dev_get_drvdata(dev);
  4705. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4706. if (protect)
  4707. return pt_pip2_exec_panel_scan(cd, scan_type);
  4708. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4709. }
  4710. if (protect)
  4711. return pt_hid_output_exec_panel_scan(cd);
  4712. return pt_hid_output_exec_panel_scan_(cd);
  4713. }
  4714. /*******************************************************************************
  4715. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4716. *
  4717. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4718. * to retrieve the specified data type for a the last successful Execute
  4719. * Panel Scan command.
  4720. *
  4721. * RETURN:
  4722. * 0 = success
  4723. * !0 = failure
  4724. *
  4725. * PARAMETERS:
  4726. * *dev - pointer to device structure
  4727. * protect - flag to call protected or non-protected
  4728. * read_offset - read pointer offset
  4729. * read_count - length of data to read
  4730. * data_id - enumerated test ID to read selftest results from
  4731. * *response - pointer to store the read response status
  4732. * *config - pointer to store config data
  4733. * *actual_read_len - pointer to store data length actually read
  4734. * *read_buf - pointer to the read buffer
  4735. ******************************************************************************/
  4736. static int pt_hid_output_retrieve_panel_scan_(
  4737. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4738. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4739. u8 *read_buf)
  4740. {
  4741. int status;
  4742. u8 read_data_id;
  4743. int rc = 0;
  4744. int write_length = 5;
  4745. u8 write_buf[5];
  4746. u8 cmd_offset = 0;
  4747. u8 data_elem_size;
  4748. int size;
  4749. int data_size;
  4750. struct pt_hid_output hid_output = {
  4751. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4752. .write_length = write_length,
  4753. .write_buf = write_buf,
  4754. };
  4755. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4756. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4757. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4758. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4759. write_buf[cmd_offset++] = data_id;
  4760. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4761. if (rc)
  4762. return rc;
  4763. status = cd->response_buf[5];
  4764. if (status)
  4765. return -EINVAL;
  4766. read_data_id = cd->response_buf[6];
  4767. if (read_data_id != data_id)
  4768. return -EPROTO;
  4769. size = get_unaligned_le16(&cd->response_buf[0]);
  4770. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4771. *config = cd->response_buf[9];
  4772. data_elem_size = *config & 0x07;
  4773. data_size = *actual_read_len * data_elem_size;
  4774. if (read_buf)
  4775. memcpy(read_buf, &cd->response_buf[10], data_size);
  4776. if (response)
  4777. memcpy(response, cd->response_buf, size);
  4778. return rc;
  4779. }
  4780. /*******************************************************************************
  4781. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4782. *
  4783. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4784. * access to the DUT.
  4785. *
  4786. * RETURN:
  4787. * 0 = success
  4788. * !0 = failure
  4789. *
  4790. * PARAMETERS:
  4791. * *dev - pointer to device structure
  4792. * protect - flag to call protected or non-protected
  4793. * read_offset - read pointer offset
  4794. * read_count - length of data to read
  4795. * data_id - enumerated test ID to read selftest results from
  4796. * *response - pointer to store the read response status
  4797. * *config - pointer to store config data
  4798. * *actual_read_len - pointer to store data length actually read
  4799. * *read_buf - pointer to the read buffer
  4800. ******************************************************************************/
  4801. static int pt_hid_output_retrieve_panel_scan(
  4802. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4803. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4804. u8 *read_buf)
  4805. {
  4806. int rc;
  4807. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4808. if (rc < 0) {
  4809. pt_debug(cd->dev, DL_ERROR,
  4810. "%s: fail get exclusive ex=%p own=%p\n",
  4811. __func__, cd->exclusive_dev, cd->dev);
  4812. return rc;
  4813. }
  4814. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4815. read_count, data_id, response, config,
  4816. actual_read_len, read_buf);
  4817. if (release_exclusive(cd, cd->dev) < 0)
  4818. pt_debug(cd->dev, DL_ERROR,
  4819. "%s: fail to release exclusive\n", __func__);
  4820. return rc;
  4821. }
  4822. /*******************************************************************************
  4823. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4824. *
  4825. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4826. * calls to the protected or unprotected call to
  4827. * pt_hid_output_retrieve_panel_scan
  4828. *
  4829. * RETURN:
  4830. * 0 = success
  4831. * !0 = failure
  4832. *
  4833. * PARAMETERS:
  4834. * *dev - pointer to device structure
  4835. * protect - flag to call protected or non-protected
  4836. * read_offset - read pointer offset
  4837. * read_count - length of data to read
  4838. * data_id - enumerated test ID to read selftest results from
  4839. * *response - pointer to store the read response status
  4840. * *config - pointer to store config data
  4841. * *actual_read_len - pointer to store data length actually read
  4842. * *read_buf - pointer to the read buffer
  4843. ******************************************************************************/
  4844. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4845. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4846. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4847. {
  4848. struct pt_core_data *cd = dev_get_drvdata(dev);
  4849. if (protect)
  4850. return pt_hid_output_retrieve_panel_scan(cd,
  4851. read_offset, read_count, data_id, response,
  4852. config, actual_read_len, read_buf);
  4853. return pt_hid_output_retrieve_panel_scan_(cd,
  4854. read_offset, read_count, data_id, response,
  4855. config, actual_read_len, read_buf);
  4856. }
  4857. /*******************************************************************************
  4858. * FUNCTION: _pt_request_pip_user_cmd
  4859. *
  4860. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4861. * calls to the protected or unprotected call to
  4862. * pt_hid_output_user_cmd
  4863. *
  4864. * RETURN:
  4865. * 0 = success
  4866. * !0 = failure
  4867. *
  4868. * PARAMETERS:
  4869. * *dev - pointer to device structure
  4870. * protect - flag to call protected or non-protected
  4871. * read_len - length of data to read
  4872. * *read_buf - pointer to store read data
  4873. * write_len - length of data to write
  4874. * *write_buf - pointer to buffer to write
  4875. * *actual_read_len - pointer to store data length actually read
  4876. ******************************************************************************/
  4877. static int _pt_request_pip_user_cmd(struct device *dev,
  4878. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4879. u8 *write_buf, u16 *actual_read_len)
  4880. {
  4881. struct pt_core_data *cd = dev_get_drvdata(dev);
  4882. if (protect)
  4883. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4884. write_len, write_buf, actual_read_len);
  4885. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4886. write_len, write_buf, actual_read_len);
  4887. }
  4888. /*******************************************************************************
  4889. * FUNCTION: pt_hid_output_bl_get_information_
  4890. *
  4891. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4892. * DUT to retrieve bootloader version and chip identification information.
  4893. *
  4894. * RETURN:
  4895. * 0 = success
  4896. * !0 = failure
  4897. *
  4898. * PARAMETERS:
  4899. * *cd - pointer to core data
  4900. * *return_data - pointer to store the return data
  4901. *****************************************************************************/
  4902. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4903. u8 *return_data)
  4904. {
  4905. int rc;
  4906. int data_len;
  4907. struct pt_hid_output hid_output = {
  4908. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4909. };
  4910. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4911. if (rc)
  4912. return rc;
  4913. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4914. if (!data_len)
  4915. return -EPROTO;
  4916. memcpy(return_data, &cd->response_buf[8], data_len);
  4917. return 0;
  4918. }
  4919. /*******************************************************************************
  4920. * FUNCTION: pt_hid_output_bl_get_information
  4921. *
  4922. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4923. * access to the DUT.
  4924. *
  4925. * RETURN:
  4926. * 0 = success
  4927. * !0 = failure
  4928. *
  4929. * PARAMETERS:
  4930. * *cd - pointer to core data
  4931. * *return_data - pointer to store the return data
  4932. ******************************************************************************/
  4933. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4934. u8 *return_data)
  4935. {
  4936. int rc;
  4937. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4938. if (rc < 0) {
  4939. pt_debug(cd->dev, DL_ERROR,
  4940. "%s: fail get exclusive ex=%p own=%p\n",
  4941. __func__, cd->exclusive_dev, cd->dev);
  4942. return rc;
  4943. }
  4944. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4945. if (release_exclusive(cd, cd->dev) < 0)
  4946. pt_debug(cd->dev, DL_ERROR,
  4947. "%s: fail to release exclusive\n", __func__);
  4948. return rc;
  4949. }
  4950. /*******************************************************************************
  4951. * FUNCTION: _pt_request_pip_bl_get_information
  4952. *
  4953. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4954. * calls to the protected or unprotected call to
  4955. * pt_hid_output_bl_get_information
  4956. *
  4957. * RETURN:
  4958. * 0 = success
  4959. * !0 = failure
  4960. *
  4961. * PARAMETERS:
  4962. * *dev - pointer to device structure
  4963. * protect - flag to call protected or non-protected
  4964. * *return_data - pointer to store bl data
  4965. ******************************************************************************/
  4966. static int _pt_request_pip_bl_get_information(struct device *dev,
  4967. int protect, u8 *return_data)
  4968. {
  4969. struct pt_core_data *cd = dev_get_drvdata(dev);
  4970. if (protect)
  4971. return pt_hid_output_bl_get_information(cd, return_data);
  4972. return pt_hid_output_bl_get_information_(cd, return_data);
  4973. }
  4974. /*******************************************************************************
  4975. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4976. *
  4977. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4978. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4979. * and Design Data block in flash and enables the host to execute the Program
  4980. * and Verify Row command to bootload the application image and data.
  4981. *
  4982. * RETURN:
  4983. * 0 = success
  4984. * !0 = failure
  4985. *
  4986. * PARAMETERS:
  4987. * *cd - pointer to core data
  4988. * protect - flag to call protected or non-protected
  4989. * key_size - size of key
  4990. * *key_buf - pointer to key data to allow operation
  4991. * row_size - size of the meta data row
  4992. * *metadata_row_buf - pointer to meta data to write
  4993. ******************************************************************************/
  4994. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4995. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4996. {
  4997. u16 write_length = key_size + row_size;
  4998. u8 *write_buf;
  4999. int rc = 0;
  5000. struct pt_hid_output hid_output = {
  5001. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  5002. .write_length = write_length,
  5003. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  5004. };
  5005. write_buf = kzalloc(write_length, GFP_KERNEL);
  5006. if (!write_buf)
  5007. return -ENOMEM;
  5008. hid_output.write_buf = write_buf;
  5009. if (key_size)
  5010. memcpy(write_buf, key_buf, key_size);
  5011. if (row_size)
  5012. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  5013. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5014. kfree(write_buf);
  5015. return rc;
  5016. }
  5017. /*******************************************************************************
  5018. * FUNCTION: pt_hid_output_bl_initiate_bl
  5019. *
  5020. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  5021. * access to the DUT.
  5022. *
  5023. * RETURN:
  5024. * 0 = success
  5025. * !0 = failure
  5026. *
  5027. * PARAMETERS:
  5028. * *cd - pointer to core data
  5029. * key_size - size of key
  5030. * *key_buf - pointer to key data to allow operation
  5031. * row_size - size of the meta data row
  5032. * *metadata_row_buf - pointer to meta data to write
  5033. ******************************************************************************/
  5034. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5035. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5036. {
  5037. int rc;
  5038. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5039. if (rc < 0) {
  5040. pt_debug(cd->dev, DL_ERROR,
  5041. "%s: fail get exclusive ex=%p own=%p\n",
  5042. __func__, cd->exclusive_dev, cd->dev);
  5043. return rc;
  5044. }
  5045. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5046. row_size, metadata_row_buf);
  5047. if (release_exclusive(cd, cd->dev) < 0)
  5048. pt_debug(cd->dev, DL_ERROR,
  5049. "%s: fail to release exclusive\n", __func__);
  5050. return rc;
  5051. }
  5052. /*******************************************************************************
  5053. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5054. *
  5055. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5056. * calls to the protected or unprotected call to
  5057. * pt_hid_output_bl_initiate_bl
  5058. *
  5059. * RETURN:
  5060. * 0 = success
  5061. * !0 = failure
  5062. *
  5063. * PARAMETERS:
  5064. * *dev - pointer to device structure
  5065. * protect - flag to call protected or non-protected
  5066. * key_size - size of key
  5067. * *key_buf - pointer to key data to allow operation
  5068. * row_size - size of the meta data row
  5069. * *metadata_row_buf - pointer to meta data to write
  5070. ******************************************************************************/
  5071. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5072. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5073. u8 *metadata_row_buf)
  5074. {
  5075. struct pt_core_data *cd = dev_get_drvdata(dev);
  5076. if (protect)
  5077. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5078. row_size, metadata_row_buf);
  5079. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5080. row_size, metadata_row_buf);
  5081. }
  5082. /*******************************************************************************
  5083. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5084. *
  5085. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5086. * and program a 128-byte row into the flash, and then verifies written data.
  5087. *
  5088. * RETURN:
  5089. * 0 = success
  5090. * !0 = failure
  5091. *
  5092. * PARAMETERS:
  5093. * *cd - pointer to core data
  5094. * data_len - length of data_buf
  5095. * *data_buf - firmware image to program
  5096. ******************************************************************************/
  5097. static int pt_hid_output_bl_program_and_verify_(
  5098. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5099. {
  5100. struct pt_hid_output hid_output = {
  5101. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5102. .write_length = data_len,
  5103. .write_buf = data_buf,
  5104. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5105. };
  5106. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5107. }
  5108. /*******************************************************************************
  5109. * FUNCTION: pt_hid_output_bl_program_and_verify
  5110. *
  5111. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5112. * access to the DUT.
  5113. *
  5114. * RETURN:
  5115. * 0 = success
  5116. * !0 = failure
  5117. *
  5118. * PARAMETERS:
  5119. * *cd - pointer to core data
  5120. * data_len - length of data_buf
  5121. * *data_buf - firmware image to program
  5122. ******************************************************************************/
  5123. static int pt_hid_output_bl_program_and_verify(
  5124. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5125. {
  5126. int rc;
  5127. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5128. if (rc < 0) {
  5129. pt_debug(cd->dev, DL_ERROR,
  5130. "%s: fail get exclusive ex=%p own=%p\n",
  5131. __func__, cd->exclusive_dev, cd->dev);
  5132. return rc;
  5133. }
  5134. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5135. if (release_exclusive(cd, cd->dev) < 0)
  5136. pt_debug(cd->dev, DL_ERROR,
  5137. "%s: fail to release exclusive\n", __func__);
  5138. return rc;
  5139. }
  5140. /*******************************************************************************
  5141. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5142. *
  5143. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5144. * to request to have the BL program and verify a FW image
  5145. *
  5146. * RETURN:
  5147. * 0 = success
  5148. * !0 = failure
  5149. *
  5150. * PARAMETERS:
  5151. * *dev - pointer to device structure
  5152. * protect - boolean to determine to call the protected function
  5153. * data_len - length of data_buf
  5154. * *data_buf - firmware image to program
  5155. ******************************************************************************/
  5156. static int _pt_request_pip_bl_program_and_verify(
  5157. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5158. {
  5159. struct pt_core_data *cd = dev_get_drvdata(dev);
  5160. if (protect)
  5161. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5162. data_buf);
  5163. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5164. data_buf);
  5165. }
  5166. /*******************************************************************************
  5167. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5168. *
  5169. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5170. * perform a full verification of the application integrity by calculating the
  5171. * CRC of the image in flash and compare it to the expected CRC stored in the
  5172. * Metadata row.
  5173. *
  5174. * RETURN:
  5175. * 0 = success
  5176. * !0 = failure
  5177. *
  5178. * PARAMETERS:
  5179. * *cd - pointer to core data
  5180. * *result - pointer to store result
  5181. ******************************************************************************/
  5182. static int pt_hid_output_bl_verify_app_integrity_(
  5183. struct pt_core_data *cd, u8 *result)
  5184. {
  5185. int rc;
  5186. struct pt_hid_output hid_output = {
  5187. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5188. };
  5189. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5190. if (rc) {
  5191. *result = 0;
  5192. return rc;
  5193. }
  5194. *result = cd->response_buf[8];
  5195. return 0;
  5196. }
  5197. /*******************************************************************************
  5198. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5199. *
  5200. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5201. * exclusive access to the DUT.
  5202. *
  5203. * RETURN:
  5204. * 0 = success
  5205. * !0 = failure
  5206. *
  5207. * PARAMETERS:
  5208. * *cd - pointer to core data
  5209. * *result - pointer to store result
  5210. ******************************************************************************/
  5211. static int pt_hid_output_bl_verify_app_integrity(
  5212. struct pt_core_data *cd, u8 *result)
  5213. {
  5214. int rc;
  5215. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5216. if (rc < 0) {
  5217. pt_debug(cd->dev, DL_ERROR,
  5218. "%s: fail get exclusive ex=%p own=%p\n",
  5219. __func__, cd->exclusive_dev, cd->dev);
  5220. return rc;
  5221. }
  5222. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5223. if (release_exclusive(cd, cd->dev) < 0)
  5224. pt_debug(cd->dev, DL_ERROR,
  5225. "%s: fail to release exclusive\n", __func__);
  5226. return rc;
  5227. }
  5228. /*******************************************************************************
  5229. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5230. *
  5231. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5232. * to request to have the BL verify the application integrity (PIP1.x only)
  5233. *
  5234. * RETURN:
  5235. * 0 = success
  5236. * !0 = failure
  5237. *
  5238. * PARAMETERS:
  5239. * *dev - pointer to device structure
  5240. * protect - boolean to determine to call the protected function
  5241. * *result - pointer to store result
  5242. ******************************************************************************/
  5243. static int _pt_request_pip_bl_verify_app_integrity(
  5244. struct device *dev, int protect, u8 *result)
  5245. {
  5246. struct pt_core_data *cd = dev_get_drvdata(dev);
  5247. if (protect)
  5248. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5249. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5250. }
  5251. /*******************************************************************************
  5252. * FUNCTION: pt_hid_output_bl_launch_app_
  5253. *
  5254. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5255. * application from bootloader (PIP1.x only).
  5256. *
  5257. * RETURN:
  5258. * 0 = success
  5259. * !0 = failure
  5260. *
  5261. * PARAMETERS:
  5262. * *cd - pointer to core data
  5263. ******************************************************************************/
  5264. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5265. {
  5266. struct pt_hid_output hid_output = {
  5267. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5268. .reset_expected = 1,
  5269. };
  5270. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5271. }
  5272. /*******************************************************************************
  5273. * FUNCTION: pt_hid_output_bl_launch_app
  5274. *
  5275. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5276. * to the DUT.
  5277. *
  5278. * RETURN:
  5279. * 0 = success
  5280. * !0 = failure
  5281. *
  5282. * PARAMETERS:
  5283. * *cd - pointer to core data
  5284. ******************************************************************************/
  5285. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5286. {
  5287. int rc;
  5288. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5289. if (rc < 0) {
  5290. pt_debug(cd->dev, DL_ERROR,
  5291. "%s: fail get exclusive ex=%p own=%p\n",
  5292. __func__, cd->exclusive_dev, cd->dev);
  5293. return rc;
  5294. }
  5295. rc = pt_hid_output_bl_launch_app_(cd);
  5296. if (release_exclusive(cd, cd->dev) < 0)
  5297. pt_debug(cd->dev, DL_ERROR,
  5298. "%s: fail to release exclusive\n", __func__);
  5299. return rc;
  5300. }
  5301. /*******************************************************************************
  5302. * FUNCTION: _pt_request_pip_launch_app
  5303. *
  5304. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5305. * to request to have the BL launch the application. (PIP1.x only)
  5306. *
  5307. * RETURN:
  5308. * 0 = success
  5309. * !0 = failure
  5310. *
  5311. * PARAMETERS:
  5312. * *dev - pointer to device structure
  5313. * protect - boolean to determine to call the protected function
  5314. ******************************************************************************/
  5315. static int _pt_request_pip_launch_app(struct device *dev,
  5316. int protect)
  5317. {
  5318. struct pt_core_data *cd = dev_get_drvdata(dev);
  5319. if (protect)
  5320. return pt_hid_output_bl_launch_app(cd);
  5321. return pt_hid_output_bl_launch_app_(cd);
  5322. }
  5323. /*******************************************************************************
  5324. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5325. *
  5326. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5327. * value store in the System Information.
  5328. *
  5329. * RETURN:
  5330. * 0 = success
  5331. * !0 = failure
  5332. *
  5333. * PARAMETERS:
  5334. * *cd - pointer to core data
  5335. * *panel_id - pointer to where the panel ID will be stored
  5336. ******************************************************************************/
  5337. static int pt_hid_output_bl_get_panel_id_(
  5338. struct pt_core_data *cd, u8 *panel_id)
  5339. {
  5340. int rc;
  5341. struct pt_hid_output hid_output = {
  5342. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5343. };
  5344. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5345. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5346. pt_debug(cd->dev, DL_ERROR,
  5347. "%s: Get Panel ID command not supported\n",
  5348. __func__);
  5349. *panel_id = PANEL_ID_NOT_ENABLED;
  5350. return 0;
  5351. } else if (rc < 0) {
  5352. pt_debug(cd->dev, DL_ERROR,
  5353. "%s: Error on Get Panel ID command\n", __func__);
  5354. return rc;
  5355. }
  5356. *panel_id = cd->response_buf[8];
  5357. return 0;
  5358. }
  5359. /*******************************************************************************
  5360. * FUNCTION: pt_hid_output_bl_get_panel_id
  5361. *
  5362. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5363. * to the DUT.
  5364. *
  5365. * RETURN:
  5366. * 0 = success
  5367. * !0 = failure
  5368. *
  5369. * PARAMETERS:
  5370. * *cd - pointer to core data
  5371. * *panel_id - pointer to where the panel ID will be stored
  5372. ******************************************************************************/
  5373. static int pt_hid_output_bl_get_panel_id(
  5374. struct pt_core_data *cd, u8 *panel_id)
  5375. {
  5376. int rc;
  5377. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5378. if (rc < 0) {
  5379. pt_debug(cd->dev, DL_ERROR,
  5380. "%s: fail get exclusive ex=%p own=%p\n",
  5381. __func__, cd->exclusive_dev, cd->dev);
  5382. return rc;
  5383. }
  5384. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5385. if (release_exclusive(cd, cd->dev) < 0)
  5386. pt_debug(cd->dev, DL_ERROR,
  5387. "%s: fail to release exclusive\n", __func__);
  5388. return rc;
  5389. }
  5390. /*******************************************************************************
  5391. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5392. *
  5393. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5394. * to have the BL retrieve the panel ID
  5395. *
  5396. * RETURN:
  5397. * 0 = success
  5398. * !0 = failure
  5399. *
  5400. * PARAMETERS:
  5401. * *dev - pointer to device structure
  5402. * protect - flag to run in protected mode
  5403. * *panel_id - pointer to where the panel ID will be stored
  5404. ******************************************************************************/
  5405. static int _pt_request_pip_bl_get_panel_id(
  5406. struct device *dev, int protect, u8 *panel_id)
  5407. {
  5408. struct pt_core_data *cd = dev_get_drvdata(dev);
  5409. if (protect)
  5410. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5411. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5412. }
  5413. /*******************************************************************************
  5414. * FUNCTION: pt_pip2_get_mode_sysmode_
  5415. *
  5416. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5417. * PIP2 STATUS command.
  5418. *
  5419. * RETURN:
  5420. * 0 = success
  5421. * !0 = failure
  5422. *
  5423. * PARAMETERS:
  5424. * *cd - pointer to core data structure
  5425. * *mode - pointer to store the retrieved mode
  5426. * *sys_mode - pointer to store the FW system mode
  5427. ******************************************************************************/
  5428. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5429. u8 *mode, u8 *sys_mode)
  5430. {
  5431. u16 actual_read_len;
  5432. u8 read_buf[12];
  5433. u8 status, boot;
  5434. int rc = 0;
  5435. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5436. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5437. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5438. __func__, rc);
  5439. if (!rc) {
  5440. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5441. "PIP2 STATUS");
  5442. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5443. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5444. if (sys_mode) {
  5445. if (status == PIP2_RSP_ERR_NONE &&
  5446. boot == PIP2_STATUS_APP_EXEC)
  5447. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5448. else
  5449. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5450. }
  5451. if (mode) {
  5452. if (status == PIP2_RSP_ERR_NONE &&
  5453. boot == PIP2_STATUS_BOOT_EXEC)
  5454. *mode = PT_MODE_BOOTLOADER;
  5455. else if (status == PIP2_RSP_ERR_NONE &&
  5456. boot == PIP2_STATUS_APP_EXEC)
  5457. *mode = PT_MODE_OPERATIONAL;
  5458. else
  5459. *mode = PT_MODE_UNKNOWN;
  5460. }
  5461. } else {
  5462. if (mode)
  5463. *mode = PT_MODE_UNKNOWN;
  5464. if (sys_mode)
  5465. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5466. pt_debug(cd->dev, DL_WARN,
  5467. "%s: Mode and sys_mode could not be determined\n",
  5468. __func__);
  5469. }
  5470. return rc;
  5471. }
  5472. /*******************************************************************************
  5473. * FUNCTION: pt_pip2_get_mode_sysmode
  5474. *
  5475. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5476. * access to the DUT.
  5477. *
  5478. * RETURN:
  5479. * 0 = success
  5480. * !0 = failure
  5481. *
  5482. * PARAMETERS:
  5483. * *cd - pointer to core data structure
  5484. * *mode - pointer to store the retrieved mode
  5485. * *sys_mode - pointer to store the FW system mode
  5486. ******************************************************************************/
  5487. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5488. u8 *mode, u8 *sys_mode)
  5489. {
  5490. int rc;
  5491. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5492. if (rc < 0) {
  5493. pt_debug(cd->dev, DL_ERROR,
  5494. "%s: fail get exclusive ex=%p own=%p\n",
  5495. __func__, cd->exclusive_dev, cd->dev);
  5496. return rc;
  5497. }
  5498. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5499. if (release_exclusive(cd, cd->dev) < 0)
  5500. pt_debug(cd->dev, DL_ERROR,
  5501. "%s: fail to release exclusive\n", __func__);
  5502. return rc;
  5503. }
  5504. /*******************************************************************************
  5505. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5506. *
  5507. * SUMMARY: Function pointer included in core_commands struct for external
  5508. * calls to the protected or unprotected call to
  5509. * pt_pip2_get_mode_sysmode
  5510. *
  5511. * RETURN:
  5512. * 0 = success
  5513. * !0 = failure
  5514. *
  5515. * PARAMETERS:
  5516. * *dev - pointer to device structure
  5517. * *mode - pointer to store the retrieved mode
  5518. * *sys_mode - pointer to store the FW system mode
  5519. ******************************************************************************/
  5520. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5521. int protect, u8 *mode, u8 *sys_mode)
  5522. {
  5523. struct pt_core_data *cd = dev_get_drvdata(dev);
  5524. if (protect)
  5525. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5526. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5527. }
  5528. /*******************************************************************************
  5529. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5530. *
  5531. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5532. * mode only the following PIP commands will be serviced, any other PIP
  5533. * command the FW will respond with an "Invalid PIP Command" response.
  5534. * - Get HID Descriptor (Register 0x0001, no command ID)
  5535. * - Reset (Register 0x0005, RESET HID request)
  5536. * - Ping (Register 0x0004, Command ID 0x00
  5537. * - Get System Information (Register 0x0004, Command ID 0x02)
  5538. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5539. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5540. * This function will loop on the results of the STATUS command until
  5541. * the FW reports it is out of BOOT mode.
  5542. *
  5543. * NOTE:
  5544. * - This function will update cd->fw_system_mode
  5545. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5546. *
  5547. * RETURN:
  5548. * 0 = success
  5549. * !0 = failure
  5550. *
  5551. * PARAMETERS:
  5552. * *cd - pointer to core data
  5553. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5554. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5555. ******************************************************************************/
  5556. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5557. int *actual_wait)
  5558. {
  5559. int loop = 0;
  5560. u8 sys_mode = cd->fw_system_mode;
  5561. u8 pause = 10; /* in ms */
  5562. int rc = 0;
  5563. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5564. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5565. /*
  5566. * For PIP <1.11, no support for polling wait so do a hard
  5567. * coded wait and assume the FW is out of BOOT. Added 1 to
  5568. * timeout to make it clear in kmsg if non polling was done.
  5569. */
  5570. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5571. pt_debug(cd->dev, DL_ERROR,
  5572. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5573. __func__,
  5574. cd->sysinfo.ttdata.pip_ver_major,
  5575. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5576. msleep(*actual_wait);
  5577. sys_mode = FW_SYS_MODE_SCANNING;
  5578. }
  5579. if (sys_mode == FW_SYS_MODE_BOOT) {
  5580. while (!rc && loop <= max_loop &&
  5581. (sys_mode == FW_SYS_MODE_BOOT)) {
  5582. loop++;
  5583. usleep_range(9000, pause * 1000);
  5584. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5585. pt_debug(cd->dev, DL_DEBUG,
  5586. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5587. __func__, loop * pause, sys_mode);
  5588. }
  5589. *actual_wait = (int)(loop * pause);
  5590. pt_debug(cd->dev, DL_WARN,
  5591. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5592. __func__, *actual_wait, sys_mode);
  5593. if (rc)
  5594. sys_mode = FW_SYS_MODE_UNDEFINED;
  5595. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5596. sys_mode == FW_SYS_MODE_UNDEFINED)
  5597. rc = -EBUSY;
  5598. }
  5599. mutex_lock(&cd->system_lock);
  5600. cd->fw_system_mode = sys_mode;
  5601. mutex_unlock(&cd->system_lock);
  5602. return rc;
  5603. }
  5604. /*******************************************************************************
  5605. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5606. *
  5607. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5608. * access to the DUT.
  5609. *
  5610. * RETURN:
  5611. * 0 = success
  5612. * !0 = failure
  5613. *
  5614. * PARAMETERS:
  5615. * *cd - pointer to core data
  5616. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5617. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5618. ******************************************************************************/
  5619. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5620. int *actual_wait)
  5621. {
  5622. int rc;
  5623. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5624. if (rc < 0) {
  5625. pt_debug(cd->dev, DL_ERROR,
  5626. "%s: fail get exclusive ex=%p own=%p\n",
  5627. __func__, cd->exclusive_dev, cd->dev);
  5628. return rc;
  5629. }
  5630. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5631. if (release_exclusive(cd, cd->dev) < 0)
  5632. pt_debug(cd->dev, DL_ERROR,
  5633. "%s: fail to release exclusive\n", __func__);
  5634. return rc;
  5635. }
  5636. /*******************************************************************************
  5637. * FUNCTION: _pt_get_fw_sys_mode
  5638. *
  5639. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5640. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5641. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5642. * information but any PIP command above 0x1F requires scanning to be disabled
  5643. * before it will be operational. If scanning was not disabled before sending
  5644. * these PIP commands the FW will respond with a 6 byte error response. So to
  5645. * safely determine the scanning state, a PIP message that will not affect the
  5646. * operation of the FW was chosen:
  5647. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5648. * received scanning is active.
  5649. *
  5650. * RETURN:
  5651. * 0 = success
  5652. * !0 = failure
  5653. *
  5654. * PARAMETERS:
  5655. * *cd - pointer to core data
  5656. * *sys_mode - pointer to FW System mode
  5657. * *mode - pointer to mode (BL/FW)
  5658. ******************************************************************************/
  5659. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5660. {
  5661. int write_length = 1;
  5662. int report_length;
  5663. int rc = 0;
  5664. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5665. u8 tmp_mode = PT_MODE_UNKNOWN;
  5666. u8 param[1] = { PT_TCH_PARM_EBID };
  5667. struct pt_hid_output hid_output = {
  5668. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5669. .write_length = write_length,
  5670. .write_buf = param,
  5671. .novalidate = true,
  5672. };
  5673. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5674. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5675. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5676. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5677. __func__, tmp_sys_mode, tmp_mode);
  5678. if (!rc) {
  5679. if (tmp_mode != PT_MODE_OPERATIONAL)
  5680. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5681. }
  5682. goto exit;
  5683. }
  5684. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5685. if (cd->mode != PT_MODE_OPERATIONAL) {
  5686. tmp_mode = cd->mode;
  5687. goto exit;
  5688. }
  5689. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5690. if (rc)
  5691. goto exit;
  5692. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5693. if ((report_length == 0x06) &&
  5694. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5695. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5696. tmp_mode = PIP2_STATUS_APP_EXEC;
  5697. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5698. } else if ((report_length == 0x0A) &&
  5699. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5700. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5701. tmp_mode = PIP2_STATUS_APP_EXEC;
  5702. tmp_sys_mode = FW_SYS_MODE_TEST;
  5703. }
  5704. exit:
  5705. if (mode)
  5706. *mode = tmp_mode;
  5707. if (sys_mode)
  5708. *sys_mode = tmp_sys_mode;
  5709. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5710. __func__, tmp_mode, tmp_sys_mode);
  5711. return rc;
  5712. }
  5713. /*******************************************************************************
  5714. * FUNCTION: pt_get_fw_sys_mode
  5715. *
  5716. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5717. * is active or not.
  5718. *
  5719. * RETURN:
  5720. * 0 = success
  5721. * !0 = failure
  5722. *
  5723. * PARAMETERS:
  5724. * *cd - pointer to core data
  5725. * *sys_mode - pointer to fw system mode
  5726. * *mode - pointer to mode
  5727. ******************************************************************************/
  5728. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5729. {
  5730. int rc;
  5731. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5732. if (rc < 0) {
  5733. pt_debug(cd->dev, DL_ERROR,
  5734. "%s: fail get exclusive ex=%p own=%p\n",
  5735. __func__, cd->exclusive_dev, cd->dev);
  5736. return rc;
  5737. }
  5738. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5739. if (release_exclusive(cd, cd->dev) < 0)
  5740. pt_debug(cd->dev, DL_ERROR,
  5741. "%s: fail to release exclusive\n", __func__);
  5742. return rc;
  5743. }
  5744. /*******************************************************************************
  5745. * FUNCTION: _pt_request_get_fw_sys_mode
  5746. *
  5747. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5748. * to request to get scan state
  5749. *
  5750. * RETURN:
  5751. * 0 = success
  5752. * !0 = failure
  5753. *
  5754. * PARAMETERS:
  5755. * *dev - pointer to device structure
  5756. * protect - flag to call protected or non-protected
  5757. * *sys_mode - pointer to FW system mode
  5758. * *mode - pointer to mode
  5759. ******************************************************************************/
  5760. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5761. u8 *sys_mode, u8 *mode)
  5762. {
  5763. struct pt_core_data *cd = dev_get_drvdata(dev);
  5764. if (protect)
  5765. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5766. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5767. }
  5768. /* Default hid descriptor to provide basic register map */
  5769. const struct pt_hid_desc hid_desc_default = {
  5770. 230, /* hid_desc_len */
  5771. HID_APP_REPORT_ID, /* packet_id */
  5772. 0x00, /* reserved_byte */
  5773. 0x0100, /* bcd_version */
  5774. 0x00EC, /* report_desc_len */
  5775. 0x0002, /* report_desc_register */
  5776. 0x0003, /* input_register */
  5777. 0x00FE, /* max_input_len */
  5778. 0x0004, /* output_register */
  5779. 0x00FE, /* max_output_len */
  5780. 0x0005, /* command_register */
  5781. 0x0006, /* data_register */
  5782. 0x04B4, /* vendor_id */
  5783. 0xC101, /* product_id */
  5784. 0x0100, /* version_id */
  5785. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5786. };
  5787. /*******************************************************************************
  5788. * FUNCTION: pt_init_hid_descriptor
  5789. *
  5790. * SUMMARY: Setup default values for HID descriptor structure
  5791. *
  5792. *
  5793. * PARAMETERS:
  5794. * *desc - pointer to the HID descriptor data read back from DUT
  5795. ******************************************************************************/
  5796. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5797. {
  5798. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5799. }
  5800. /*******************************************************************************
  5801. * FUNCTION: pt_get_hid_descriptor_
  5802. *
  5803. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5804. * into the HID descriptor structure
  5805. *
  5806. * RETURN:
  5807. * 0 = success
  5808. * !0 = failure
  5809. *
  5810. * PARAMETERS:
  5811. * *cd - pointer to core data
  5812. * *desc - pointer to the HID descriptor data read back from DUT
  5813. ******************************************************************************/
  5814. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5815. struct pt_hid_desc *desc)
  5816. {
  5817. struct device *dev = cd->dev;
  5818. int rc = 0;
  5819. int t;
  5820. u8 cmd[2];
  5821. /*
  5822. * During startup the HID descriptor is required for all future
  5823. * processing. If IRQ is already asserted due to an early touch report
  5824. * the report must be cleared before sending command.
  5825. */
  5826. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5827. /* Read HID descriptor length and version */
  5828. mutex_lock(&cd->system_lock);
  5829. cd->hid_cmd_state = 1;
  5830. mutex_unlock(&cd->system_lock);
  5831. /* Set HID descriptor register */
  5832. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5833. sizeof(cd->hid_core.hid_desc_register));
  5834. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5835. __func__, sizeof(cmd));
  5836. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5837. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5838. if (rc) {
  5839. pt_debug(dev, DL_ERROR,
  5840. "%s: failed to get HID descriptor, rc=%d\n",
  5841. __func__, rc);
  5842. goto error;
  5843. }
  5844. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5845. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5846. if (IS_TMO(t)) {
  5847. #ifdef TTDL_DIAGNOSTICS
  5848. cd->bus_transmit_error_count++;
  5849. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5850. #endif /* TTDL_DIAGNOSTICS */
  5851. pt_debug(cd->dev, DL_ERROR,
  5852. "%s: HID get descriptor timed out\n", __func__);
  5853. rc = -ETIME;
  5854. goto error;
  5855. } else {
  5856. cd->hw_detected = true;
  5857. }
  5858. /* Load the HID descriptor including all registers */
  5859. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5860. /* Check HID descriptor length and version */
  5861. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5862. le16_to_cpu(desc->hid_desc_len),
  5863. le16_to_cpu(desc->bcd_version));
  5864. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5865. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5866. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5867. __func__);
  5868. return -ENODEV;
  5869. }
  5870. goto exit;
  5871. error:
  5872. mutex_lock(&cd->system_lock);
  5873. cd->hid_cmd_state = 0;
  5874. mutex_unlock(&cd->system_lock);
  5875. exit:
  5876. return rc;
  5877. }
  5878. /*******************************************************************************
  5879. * FUNCTION: pt_get_hid_descriptor
  5880. *
  5881. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5882. *
  5883. * RETURN:
  5884. * 0 = success
  5885. * !0 = failure
  5886. *
  5887. * PARAMETERS:
  5888. * *cd - pointer to core data
  5889. * *desc - pointer to the HID descriptor data read back from DUT
  5890. ******************************************************************************/
  5891. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5892. struct pt_hid_desc *desc)
  5893. {
  5894. int rc;
  5895. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5896. if (rc < 0) {
  5897. pt_debug(cd->dev, DL_ERROR,
  5898. "%s: fail get exclusive ex=%p own=%p\n",
  5899. __func__, cd->exclusive_dev, cd->dev);
  5900. return rc;
  5901. }
  5902. rc = pt_get_hid_descriptor_(cd, desc);
  5903. if (release_exclusive(cd, cd->dev) < 0)
  5904. pt_debug(cd->dev, DL_ERROR,
  5905. "%s: fail to release exclusive\n", __func__);
  5906. return rc;
  5907. }
  5908. /*******************************************************************************
  5909. * FUNCTION: pt_pip2_get_version_
  5910. *
  5911. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5912. * cd-ttdata
  5913. *
  5914. * RETURN:
  5915. * 0 = success
  5916. * !0 = failure
  5917. *
  5918. * PARAMETERS:
  5919. * *cd - pointer to core data
  5920. ******************************************************************************/
  5921. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5922. {
  5923. int rc = 0;
  5924. int status;
  5925. u8 read_buf[64];
  5926. u16 actual_read_len;
  5927. rc = _pt_request_pip2_send_cmd(cd->dev,
  5928. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5929. NULL, 0, read_buf, &actual_read_len);
  5930. if (rc) {
  5931. pt_debug(cd->dev, DL_ERROR,
  5932. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5933. __func__, rc);
  5934. return rc;
  5935. }
  5936. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5937. if (status == 0) {
  5938. /* Parse the PIP2 VERSION response into ttdata */
  5939. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5940. } else {
  5941. pt_debug(cd->dev, DL_ERROR,
  5942. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5943. __func__, status);
  5944. return status;
  5945. }
  5946. return rc;
  5947. }
  5948. /*******************************************************************************
  5949. * FUNCTION: pt_pip2_get_version
  5950. *
  5951. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5952. * access to the DUT.
  5953. *
  5954. * RETURN:
  5955. * 0 = success
  5956. * !0 = failure
  5957. *
  5958. * PARAMETERS:
  5959. * *cd - pointer to core data structure
  5960. ******************************************************************************/
  5961. static int pt_pip2_get_version(struct pt_core_data *cd)
  5962. {
  5963. int rc;
  5964. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5965. if (rc < 0) {
  5966. pt_debug(cd->dev, DL_ERROR,
  5967. "%s: fail get exclusive ex=%p own=%p\n",
  5968. __func__, cd->exclusive_dev, cd->dev);
  5969. return rc;
  5970. }
  5971. rc = pt_pip2_get_version_(cd);
  5972. if (release_exclusive(cd, cd->dev) < 0)
  5973. pt_debug(cd->dev, DL_ERROR,
  5974. "%s: fail to release exclusive\n", __func__);
  5975. return rc;
  5976. }
  5977. /*******************************************************************************
  5978. * FUNCTION: _pt_request_active_pip_protocol
  5979. *
  5980. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5981. * Function will return PIP version of BL or application based on
  5982. * when it's called.
  5983. *
  5984. * RETURN:
  5985. * 0 = success
  5986. * !0 = failure
  5987. *
  5988. * PARAMETERS:
  5989. * *dev - pointer to device structure
  5990. * protect - flag to run in protected mode
  5991. * *pip_version_major - pointer to store PIP major version
  5992. * *pip_version_minor - pointer to store PIP minor version
  5993. ******************************************************************************/
  5994. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5995. u8 *pip_version_major, u8 *pip_version_minor)
  5996. {
  5997. struct pt_core_data *cd = dev_get_drvdata(dev);
  5998. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5999. int rc = 0;
  6000. struct pt_hid_output sys_info = {
  6001. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  6002. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  6003. };
  6004. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  6005. /* Skip PIP2 command if DUT generation is confirmed */
  6006. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6007. goto skip_pip2_command;
  6008. rc = pt_pip2_get_version_(cd);
  6009. if (!rc) {
  6010. *pip_version_major = ttdata->pip_ver_major;
  6011. *pip_version_minor = ttdata->pip_ver_minor;
  6012. pt_debug(dev, DL_INFO,
  6013. "%s: pip_version = %d.%d\n", __func__,
  6014. *pip_version_major, *pip_version_minor);
  6015. } else {
  6016. /*
  6017. * Legacy products do not support the pip2 protocol to get
  6018. * pip version. However, they do support the "get sysinfo"
  6019. * command to get pip version from FW, but the bootloader
  6020. * does not support it. This function will try "get sysinfo"
  6021. * command if the pip2 command failed but this cmd could also
  6022. * fail if DUT is stuck in bootloader mode.
  6023. */
  6024. pt_debug(dev, DL_INFO,
  6025. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6026. __func__, rc);
  6027. skip_pip2_command:
  6028. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6029. if (!rc) {
  6030. *pip_version_minor =
  6031. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6032. *pip_version_major =
  6033. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6034. pt_debug(dev, DL_INFO,
  6035. "%s: pip_version = %d.%d\n", __func__,
  6036. *pip_version_major, *pip_version_minor);
  6037. } else {
  6038. *pip_version_major = 0;
  6039. *pip_version_minor = 0;
  6040. pt_debug(dev, DL_ERROR,
  6041. "%s: pip_version Not Detected\n", __func__);
  6042. }
  6043. }
  6044. return rc;
  6045. }
  6046. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6047. /*******************************************************************************
  6048. * FUNCTION: _pt_detect_dut_generation
  6049. *
  6050. * SUMMARY: Determine the generation of device that we are communicating with:
  6051. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6052. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6053. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6054. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6055. * coming form the BL or the FW:
  6056. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6057. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6058. * This function will return a modified status if it detects the DUT
  6059. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6060. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6061. *
  6062. * RETURN:
  6063. * 0 = success
  6064. * !0 = failure
  6065. *
  6066. * PARAMETERS:
  6067. * *dev - pointer to device structure
  6068. * *status - pointer to status bitmask
  6069. * *dut_gen - pointer to store the dut_generation
  6070. * *mode - pointer to store the PT_MODE
  6071. ******************************************************************************/
  6072. static int _pt_detect_dut_generation(struct device *dev,
  6073. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6074. {
  6075. int rc = 0;
  6076. u8 dut_gen_tmp = DUT_UNKNOWN;
  6077. u8 mode_tmp = PT_MODE_UNKNOWN;
  6078. u8 attempt = 1;
  6079. u32 status_tmp = STARTUP_STATUS_START;
  6080. struct pt_core_data *cd = dev_get_drvdata(dev);
  6081. struct pt_hid_desc hid_desc;
  6082. memset(&hid_desc, 0, sizeof(hid_desc));
  6083. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6084. while (rc && attempt < 3) {
  6085. attempt++;
  6086. usleep_range(2000, 5000);
  6087. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6088. }
  6089. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6090. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6091. mode_tmp = PT_MODE_BOOTLOADER;
  6092. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6093. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6094. rc = pt_pip2_get_version_(cd);
  6095. if (!rc)
  6096. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6097. else
  6098. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6099. mode_tmp = PT_MODE_OPERATIONAL;
  6100. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6101. rc = 0; /* To return success instead of error code */
  6102. } else if (rc) {
  6103. rc = pt_pip2_get_version_(cd);
  6104. if (!rc) {
  6105. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6106. mode_tmp = PT_MODE_BOOTLOADER;
  6107. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6108. }
  6109. }
  6110. mutex_lock(&cd->system_lock);
  6111. if (dut_gen)
  6112. *dut_gen = dut_gen_tmp;
  6113. if (mode)
  6114. *mode = mode_tmp;
  6115. if (status)
  6116. *status = status_tmp;
  6117. mutex_unlock(&cd->system_lock);
  6118. #ifdef TTDL_DIAGNOSTICS
  6119. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6120. __func__, dut_gen_tmp, mode_tmp);
  6121. #endif /* TTDL_DIAGNOSTICS */
  6122. return rc;
  6123. }
  6124. /*******************************************************************************
  6125. * FUNCTION: _pt_request_dut_generation
  6126. *
  6127. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6128. * to get current dut generation.
  6129. *
  6130. * NOTE: This function WILL NOT try to determine dut generation.
  6131. *
  6132. * RETURN:
  6133. * The current dut generation.
  6134. *
  6135. * PARAMETERS:
  6136. * *dev - pointer to device structure
  6137. ******************************************************************************/
  6138. static int _pt_request_dut_generation(struct device *dev)
  6139. {
  6140. struct pt_core_data *cd = dev_get_drvdata(dev);
  6141. return cd->active_dut_generation;
  6142. }
  6143. #define HW_VERSION_LEN_MAX 13
  6144. /*******************************************************************************
  6145. * FUNCTION: _legacy_generate_hw_version
  6146. *
  6147. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6148. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6149. * command) to the hw_version.
  6150. *
  6151. * RETURN:
  6152. * 0 = success
  6153. * !0 = failure
  6154. *
  6155. * PARAMETERS:
  6156. * *cd - pointer to core data
  6157. * *hw_version - pointer to store the hardware version
  6158. ******************************************************************************/
  6159. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6160. char *hw_version)
  6161. {
  6162. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6163. if (cd->sysinfo.ready) {
  6164. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6165. ttdata->jtag_id_h, cd->pid_for_loader);
  6166. return 0;
  6167. } else if (cd->bl_info.ready) {
  6168. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6169. cd->bl_info.chip_id, cd->pid_for_loader);
  6170. return 0;
  6171. } else {
  6172. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6173. pt_debug(cd->dev, DL_ERROR,
  6174. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6175. return -ENODATA;
  6176. }
  6177. }
  6178. /*******************************************************************************
  6179. * FUNCTION: _pip2_generate_hw_version
  6180. *
  6181. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6182. * VERSION command) to the hw_version.
  6183. *
  6184. * RETURN:
  6185. * 0 = success
  6186. * !0 = failure
  6187. *
  6188. * PARAMETERS:
  6189. * *cd - pointer to core data
  6190. * *hw_version - pointer to store the hardware version
  6191. ******************************************************************************/
  6192. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6193. {
  6194. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6195. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6196. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6197. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6198. return 0;
  6199. } else {
  6200. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6201. pt_debug(cd->dev, DL_ERROR,
  6202. "%s: PIP Version are not ready\n", __func__);
  6203. return -ENODATA;
  6204. }
  6205. }
  6206. /*******************************************************************************
  6207. * FUNCTION: pt_generate_hw_version
  6208. *
  6209. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6210. * hw_version from static data.
  6211. *
  6212. * RETURN:
  6213. * 0 = success
  6214. * !0 = failure
  6215. *
  6216. * PARAMETERS:
  6217. * *cd - pointer to core data
  6218. * *hw_version - pointer to store the hardware version
  6219. ******************************************************************************/
  6220. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6221. {
  6222. int rc = 0;
  6223. if (!hw_version)
  6224. return -ENOMEM;
  6225. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6226. rc = _legacy_generate_hw_version(cd, hw_version);
  6227. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6228. rc = _pip2_generate_hw_version(cd, hw_version);
  6229. else {
  6230. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6231. rc = -ENODATA;
  6232. }
  6233. return rc;
  6234. }
  6235. /*******************************************************************************
  6236. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6237. *
  6238. * NOTE: The calling function must ensure to free *hw_version
  6239. *
  6240. * RETURN:
  6241. * 0 = success
  6242. * !0 = Failure
  6243. *
  6244. * PARAMETERS:
  6245. * *dev - pointer to device structure
  6246. * *hw_version - pointer to where the hw_version string will be stored
  6247. ******************************************************************************/
  6248. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6249. {
  6250. int rc = 0;
  6251. u16 actual_read_len;
  6252. u16 pip_ver;
  6253. u8 rd_buf[256];
  6254. u8 status;
  6255. u8 index = PIP2_RESP_STATUS_OFFSET;
  6256. u8 return_data[8];
  6257. u8 panel_id;
  6258. struct pt_core_data *cd = dev_get_drvdata(dev);
  6259. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6260. if (!hw_version)
  6261. return -ENOMEM;
  6262. if (!cd->hw_detected) {
  6263. /* No HW detected */
  6264. rc = -ENODEV;
  6265. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6266. __func__);
  6267. goto exit_error;
  6268. }
  6269. memset(return_data, 0, ARRAY_SIZE(return_data));
  6270. /* For Parade TC or TT parts */
  6271. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6272. rc = _pt_request_pip2_send_cmd(dev,
  6273. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6274. NULL, 0, rd_buf, &actual_read_len);
  6275. if (rc) {
  6276. pt_debug(dev, DL_ERROR,
  6277. "%s: Failed to send PIP2 VERSION cmd\n",
  6278. __func__);
  6279. goto exit_error;
  6280. }
  6281. status = rd_buf[index];
  6282. if (status == 0) {
  6283. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6284. /*
  6285. * BL PIP 2.02 and greater the version fields are
  6286. * swapped
  6287. */
  6288. if (pip_ver >= 0x0202) {
  6289. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6290. "%02X%02X.%02X%02X.FF",
  6291. rd_buf[index + 10], rd_buf[index + 9],
  6292. rd_buf[index + 8], rd_buf[index + 7]);
  6293. } else {
  6294. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6295. "%02X%02X.%02X%02X.FF",
  6296. rd_buf[index + 8], rd_buf[index + 7],
  6297. rd_buf[index + 10], rd_buf[index + 9]);
  6298. }
  6299. return STATUS_SUCCESS;
  6300. } else {
  6301. rc = status;
  6302. pt_debug(dev, DL_WARN,
  6303. "%s: PIP2 VERSION cmd response error\n",
  6304. __func__);
  6305. }
  6306. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6307. /*
  6308. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6309. * hard coded to FFFF
  6310. */
  6311. if (cd->mode == PT_MODE_OPERATIONAL) {
  6312. rc = pt_hid_output_get_sysinfo(cd);
  6313. if (!rc) {
  6314. panel_id =
  6315. cd->sysinfo.sensing_conf_data.panel_id;
  6316. } else {
  6317. panel_id = PANEL_ID_NOT_ENABLED;
  6318. }
  6319. /* In FW - simply retrieve from ttdata struct */
  6320. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6321. "%04X.FFFF.%02X",
  6322. ttdata->jtag_id_h,
  6323. panel_id);
  6324. return STATUS_SUCCESS;
  6325. } else {
  6326. /*
  6327. * Return the stored value if PT_PANEL_ID_BY_BL
  6328. * is not supported while other feature is.
  6329. */
  6330. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6331. rc = pt_hid_output_bl_get_information(
  6332. cd, return_data);
  6333. if (!rc) {
  6334. rc = pt_hid_output_bl_get_panel_id(
  6335. cd, &panel_id);
  6336. }
  6337. } else
  6338. panel_id = cd->pid_for_loader;
  6339. if (!rc) {
  6340. snprintf(hw_version,
  6341. HW_VERSION_LEN_MAX,
  6342. "%02X%02X.FFFF.%02X",
  6343. return_data[3], return_data[2],
  6344. panel_id);
  6345. return STATUS_SUCCESS;
  6346. }
  6347. }
  6348. } else {
  6349. /* Unknown generation */
  6350. rc = -ENODEV;
  6351. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6352. __func__);
  6353. }
  6354. exit_error:
  6355. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6356. return rc;
  6357. }
  6358. /*******************************************************************************
  6359. * FUNCTION: pt_start_wd_timer
  6360. *
  6361. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6362. *
  6363. * RETURN: void
  6364. *
  6365. * PARAMETERS:
  6366. * *cd - pointer to core data
  6367. ******************************************************************************/
  6368. static void pt_start_wd_timer(struct pt_core_data *cd)
  6369. {
  6370. if (cd->watchdog_interval < 100) {
  6371. pt_debug(cd->dev, DL_ERROR,
  6372. "%s: WARNING: Invalid watchdog interval: %d\n",
  6373. __func__, cd->watchdog_interval);
  6374. return;
  6375. }
  6376. if (cd->watchdog_force_stop) {
  6377. pt_debug(cd->dev, DL_INFO,
  6378. "%s: TTDL WD Forced stop\n", __func__);
  6379. return;
  6380. }
  6381. mod_timer(&cd->watchdog_timer, jiffies +
  6382. msecs_to_jiffies(cd->watchdog_interval));
  6383. cd->watchdog_enabled = 1;
  6384. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6385. }
  6386. /*******************************************************************************
  6387. * FUNCTION: pt_stop_wd_timer
  6388. *
  6389. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6390. *
  6391. * RETURN: void
  6392. *
  6393. * PARAMETERS:
  6394. * *cd - pointer to core data
  6395. ******************************************************************************/
  6396. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6397. {
  6398. if (!cd->watchdog_interval)
  6399. return;
  6400. /*
  6401. * Ensure we wait until the watchdog timer
  6402. * running on a different CPU finishes
  6403. */
  6404. del_timer_sync(&cd->watchdog_timer);
  6405. cancel_work_sync(&cd->watchdog_work);
  6406. del_timer_sync(&cd->watchdog_timer);
  6407. cd->watchdog_enabled = 0;
  6408. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6409. }
  6410. /*******************************************************************************
  6411. * FUNCTION: pt_hw_soft_reset
  6412. *
  6413. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6414. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6415. * schedule an enum if it fires when the DUT is being reset.
  6416. * This can cause a double reset.
  6417. *
  6418. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6419. * the WD active could cause this function to fail!
  6420. *
  6421. * RETURN:
  6422. * 0 = success
  6423. * !0 = failure
  6424. *
  6425. * PARAMETERS:
  6426. * *cd - pointer to core data struct
  6427. * protect - flag to call protected or non-protected
  6428. ******************************************************************************/
  6429. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6430. {
  6431. int rc = 0;
  6432. mutex_lock(&cd->system_lock);
  6433. cd->startup_status = STARTUP_STATUS_START;
  6434. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6435. mutex_unlock(&cd->system_lock);
  6436. if (protect)
  6437. rc = pt_hid_cmd_reset(cd);
  6438. else
  6439. rc = pt_hid_cmd_reset_(cd);
  6440. if (rc < 0) {
  6441. pt_debug(cd->dev, DL_ERROR,
  6442. "%s: FAILED to execute SOFT reset\n", __func__);
  6443. return rc;
  6444. }
  6445. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6446. __func__);
  6447. return 0;
  6448. }
  6449. /*******************************************************************************
  6450. * FUNCTION: pt_hw_hard_reset
  6451. *
  6452. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6453. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6454. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6455. * schedule an enum if it fires when the DUT is being reset.
  6456. * This can cause a double reset.
  6457. *
  6458. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6459. * the WD active could cause this function to fail!
  6460. *
  6461. * RETURN:
  6462. * 0 = success
  6463. * !0 = failure
  6464. *
  6465. * PARAMETERS:
  6466. * *cd - pointer to core data struct
  6467. ******************************************************************************/
  6468. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6469. {
  6470. if (cd->cpdata->xres) {
  6471. cd->startup_status = STARTUP_STATUS_START;
  6472. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6473. __func__);
  6474. cd->cpdata->xres(cd->cpdata, cd->dev);
  6475. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6476. __func__);
  6477. return 0;
  6478. }
  6479. pt_debug(cd->dev, DL_ERROR,
  6480. "%s: FAILED to execute HARD reset\n", __func__);
  6481. return -ENODEV;
  6482. }
  6483. /*******************************************************************************
  6484. * FUNCTION: pt_dut_reset
  6485. *
  6486. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6487. * soft reset.
  6488. *
  6489. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6490. * the WD active could cause this function to fail!
  6491. * NOTE: "protect" flag is only used for soft reset.
  6492. *
  6493. * RETURN:
  6494. * 0 = success
  6495. * !0 = failure
  6496. *
  6497. * PARAMETERS:
  6498. * *cd - pointer to core data structure
  6499. * protect - flag to call protected or non-protected
  6500. ******************************************************************************/
  6501. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6502. {
  6503. int rc = 0;
  6504. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6505. mutex_lock(&cd->system_lock);
  6506. cd->hid_reset_cmd_state = 1;
  6507. rc = pt_hw_hard_reset(cd);
  6508. mutex_unlock(&cd->system_lock);
  6509. if (rc == -ENODEV) {
  6510. mutex_lock(&cd->system_lock);
  6511. cd->hid_reset_cmd_state = 0;
  6512. mutex_unlock(&cd->system_lock);
  6513. pt_debug(cd->dev, DL_ERROR,
  6514. "%s: Hard reset failed, try soft reset\n", __func__);
  6515. rc = pt_hw_soft_reset(cd, protect);
  6516. }
  6517. if (rc)
  6518. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6519. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6520. return rc;
  6521. }
  6522. /*******************************************************************************
  6523. * FUNCTION: pt_dut_reset_and_wait
  6524. *
  6525. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6526. * complete
  6527. *
  6528. * RETURN:
  6529. * 0 = success
  6530. * !0 = failure
  6531. *
  6532. * PARAMETERS:
  6533. * *cd - pointer to core data structure
  6534. ******************************************************************************/
  6535. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6536. {
  6537. int rc = 0;
  6538. int t;
  6539. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6540. if (rc < 0)
  6541. goto exit;
  6542. t = wait_event_timeout(cd->wait_q,
  6543. (cd->hid_reset_cmd_state == 0),
  6544. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6545. if (IS_TMO(t)) {
  6546. #ifdef TTDL_DIAGNOSTICS
  6547. cd->bus_transmit_error_count++;
  6548. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6549. #endif /* TTDL_DIAGNOSTICS */
  6550. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6551. __func__);
  6552. rc = -ETIME;
  6553. goto exit;
  6554. }
  6555. exit:
  6556. return rc;
  6557. }
  6558. /*
  6559. * touch default parameters (from report descriptor) to resolve protocol for
  6560. * touch report
  6561. */
  6562. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6563. /* byte offset, size, min, max, bit offset, report */
  6564. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6565. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6566. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6567. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6568. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6569. };
  6570. /*
  6571. * button default parameters (from report descriptor) to resolve protocol for
  6572. * button report
  6573. */
  6574. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6575. /* byte offset, size, min, max, bit offset, report */
  6576. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6577. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6578. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6579. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6580. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6581. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6582. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6583. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6584. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6585. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6586. };
  6587. /*******************************************************************************
  6588. * FUNCTION: pt_init_pip_report_fields
  6589. *
  6590. * SUMMARY: Setup default values for touch/button report parsing.
  6591. *
  6592. * PARAMETERS:
  6593. * *cd - pointer to core data structure
  6594. ******************************************************************************/
  6595. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6596. {
  6597. struct pt_sysinfo *si = &cd->sysinfo;
  6598. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6599. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6600. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6601. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6602. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6603. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6604. cd->features.easywake = 1;
  6605. cd->features.noise_metric = 1;
  6606. cd->features.tracking_heatmap = 1;
  6607. cd->features.sensor_data = 1;
  6608. }
  6609. /*******************************************************************************
  6610. * FUNCTION: pt_get_mode
  6611. *
  6612. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6613. * message
  6614. *
  6615. * RETURN: Enum of the current mode
  6616. *
  6617. * PARAMETERS:
  6618. * *cd - pointer to the Core Data structure
  6619. * protect - run command in protected mode
  6620. * *mode - pointer to store the retrieved mode
  6621. ******************************************************************************/
  6622. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6623. {
  6624. if (desc->packet_id == HID_APP_REPORT_ID)
  6625. return PT_MODE_OPERATIONAL;
  6626. else if (desc->packet_id == HID_BL_REPORT_ID)
  6627. return PT_MODE_BOOTLOADER;
  6628. return PT_MODE_UNKNOWN;
  6629. }
  6630. /*******************************************************************************
  6631. * FUNCTION: _pt_request_get_mode
  6632. *
  6633. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6634. * to determine the current mode of the DUT by use of the Get HID
  6635. * Descriptor command.
  6636. *
  6637. * RETURN:
  6638. * 0 = success
  6639. * !0 = failure
  6640. *
  6641. * PARAMETERS:
  6642. * *dev - pointer to device structure
  6643. * protect - run command in protected mode
  6644. * *mode - pointer to store the retrieved mode
  6645. ******************************************************************************/
  6646. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6647. {
  6648. struct pt_core_data *cd = dev_get_drvdata(dev);
  6649. struct pt_hid_desc hid_desc;
  6650. int rc = 0;
  6651. memset(&hid_desc, 0, sizeof(hid_desc));
  6652. if (protect)
  6653. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6654. else
  6655. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6656. if (rc)
  6657. *mode = PT_MODE_UNKNOWN;
  6658. else
  6659. *mode = pt_get_mode(cd, &hid_desc);
  6660. return rc;
  6661. }
  6662. /*******************************************************************************
  6663. * FUNCTION: pt_queue_enum_
  6664. *
  6665. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6666. * function. It won't try to add/delete sysfs node or modules.
  6667. *
  6668. * RETURN: void
  6669. *
  6670. * PARAMETERS:
  6671. * *cd - pointer to core data
  6672. ******************************************************************************/
  6673. static void pt_queue_enum_(struct pt_core_data *cd)
  6674. {
  6675. if (cd->startup_state == STARTUP_NONE) {
  6676. cd->startup_state = STARTUP_QUEUED;
  6677. #ifdef TTDL_DIAGNOSTICS
  6678. if (!cd->bridge_mode)
  6679. schedule_work(&cd->enum_work);
  6680. else
  6681. cd->startup_state = STARTUP_NONE;
  6682. #else
  6683. schedule_work(&cd->enum_work);
  6684. #endif
  6685. pt_debug(cd->dev, DL_INFO,
  6686. "%s: enum_work queued\n", __func__);
  6687. } else {
  6688. pt_debug(cd->dev, DL_WARN,
  6689. "%s: Enum not queued - startup_state = %d\n",
  6690. __func__, cd->startup_state);
  6691. }
  6692. }
  6693. /*******************************************************************************
  6694. * FUNCTION: pt_queue_enum
  6695. *
  6696. * SUMMARY: Queues a TTDL enum within a mutex lock
  6697. *
  6698. * RETURN: void
  6699. *
  6700. * PARAMETERS:
  6701. * *cd - pointer to core data
  6702. ******************************************************************************/
  6703. static void pt_queue_enum(struct pt_core_data *cd)
  6704. {
  6705. mutex_lock(&cd->system_lock);
  6706. pt_queue_enum_(cd);
  6707. mutex_unlock(&cd->system_lock);
  6708. }
  6709. static void remove_sysfs_and_modules(struct device *dev);
  6710. /*******************************************************************************
  6711. * FUNCTION: pt_queue_restart
  6712. *
  6713. * SUMMARY: Queues a TTDL restart within a mutex lock
  6714. *
  6715. * RETURN: void
  6716. *
  6717. * PARAMETERS:
  6718. * *cd - pointer to core data
  6719. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6720. * False: will not perform remove action
  6721. ******************************************************************************/
  6722. static void pt_queue_restart(struct pt_core_data *cd)
  6723. {
  6724. mutex_lock(&cd->system_lock);
  6725. if (cd->startup_state == STARTUP_NONE) {
  6726. cd->startup_state = STARTUP_QUEUED;
  6727. schedule_work(&cd->ttdl_restart_work);
  6728. pt_debug(cd->dev, DL_INFO,
  6729. "%s: pt_ttdl_restart queued\n", __func__);
  6730. } else {
  6731. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6732. __func__, cd->startup_state);
  6733. }
  6734. mutex_unlock(&cd->system_lock);
  6735. }
  6736. /*******************************************************************************
  6737. * FUNCTION: call_atten_cb
  6738. *
  6739. * SUMMARY: Iterate over attention list call the function that registered.
  6740. *
  6741. * RETURN: void
  6742. *
  6743. * PARAMETERS:
  6744. * *cd - pointer to core data
  6745. * type - type of attention list
  6746. * mode - condition for execution
  6747. ******************************************************************************/
  6748. static void call_atten_cb(struct pt_core_data *cd,
  6749. enum pt_atten_type type, int mode)
  6750. {
  6751. struct atten_node *atten, *atten_n;
  6752. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6753. __func__, type, mode);
  6754. spin_lock(&cd->spinlock);
  6755. list_for_each_entry_safe(atten, atten_n,
  6756. &cd->atten_list[type], node) {
  6757. if (!mode || atten->mode & mode) {
  6758. spin_unlock(&cd->spinlock);
  6759. pt_debug(cd->dev, DL_DEBUG,
  6760. "%s: attention for '%s'",
  6761. __func__, dev_name(atten->dev));
  6762. atten->func(atten->dev);
  6763. spin_lock(&cd->spinlock);
  6764. }
  6765. }
  6766. spin_unlock(&cd->spinlock);
  6767. }
  6768. /*******************************************************************************
  6769. * FUNCTION: start_fw_upgrade
  6770. *
  6771. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6772. * start firmware upgrade.
  6773. *
  6774. * RETURN:
  6775. * 0 = success
  6776. *
  6777. * PARAMETERS:
  6778. * *data - pointer to core data
  6779. ******************************************************************************/
  6780. static int start_fw_upgrade(void *data)
  6781. {
  6782. struct pt_core_data *cd = (struct pt_core_data *)data;
  6783. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6784. return 0;
  6785. }
  6786. /*******************************************************************************
  6787. * FUNCTION: pt_put_device_into_easy_wakeup_
  6788. *
  6789. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6790. * wake up state.
  6791. *
  6792. * RETURN:
  6793. * 0 = success
  6794. * !0 = failure
  6795. *
  6796. * PARAMETERS:
  6797. * *cd - pointer to core data
  6798. ******************************************************************************/
  6799. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6800. {
  6801. int rc = 0;
  6802. u8 status = 0;
  6803. rc = pt_hid_output_enter_easywake_state_(cd,
  6804. cd->easy_wakeup_gesture, &status);
  6805. if (rc || status == 0)
  6806. return -EBUSY;
  6807. return rc;
  6808. }
  6809. /*******************************************************************************
  6810. * FUNCTION: pt_put_device_into_deep_sleep_
  6811. *
  6812. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  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_sleep_(struct pt_core_data *cd)
  6822. {
  6823. int rc = 0;
  6824. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6825. if (rc)
  6826. rc = -EBUSY;
  6827. return rc;
  6828. }
  6829. /*******************************************************************************
  6830. * FUNCTION: pt_put_device_into_deep_standby_
  6831. *
  6832. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6833. *
  6834. * RETURN:
  6835. * 0 = success
  6836. * !0 = error
  6837. *
  6838. * PARAMETERS:
  6839. * *cd - pointer to core data
  6840. ******************************************************************************/
  6841. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6842. {
  6843. int rc = 0;
  6844. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6845. if (rc)
  6846. rc = -EBUSY;
  6847. return rc;
  6848. }
  6849. /*******************************************************************************
  6850. * FUNCTION: pt_core_poweroff_device_
  6851. *
  6852. * SUMMARY: Disable IRQ and HW power down the device.
  6853. *
  6854. * RETURN:
  6855. * 0 = success
  6856. * !0 = failure
  6857. *
  6858. * PARAMETERS:
  6859. * *cd - pointer to core data
  6860. ******************************************************************************/
  6861. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6862. {
  6863. int rc;
  6864. if (cd->irq_enabled) {
  6865. cd->irq_enabled = false;
  6866. disable_irq_nosync(cd->irq);
  6867. }
  6868. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6869. if (rc < 0)
  6870. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6871. __func__, rc);
  6872. return rc;
  6873. }
  6874. /*******************************************************************************
  6875. * FUNCTION: pt_core_sleep_
  6876. *
  6877. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6878. * configuration in the core platform data structure.
  6879. *
  6880. * RETURN:
  6881. * 0 = success
  6882. * !0 = failure
  6883. *
  6884. * PARAMETERS:
  6885. * *cd - pointer to core data
  6886. ******************************************************************************/
  6887. static int pt_core_sleep_(struct pt_core_data *cd)
  6888. {
  6889. int rc = 0;
  6890. mutex_lock(&cd->system_lock);
  6891. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6892. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6893. mutex_unlock(&cd->system_lock);
  6894. pt_debug(cd->dev, DL_INFO,
  6895. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6896. return 0;
  6897. } else {
  6898. cd->sleep_state = SS_SLEEPING;
  6899. }
  6900. mutex_unlock(&cd->system_lock);
  6901. /* Ensure watchdog and startup works stopped */
  6902. pt_stop_wd_timer(cd);
  6903. cancel_work_sync(&cd->enum_work);
  6904. pt_stop_wd_timer(cd);
  6905. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6906. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6907. rc = pt_core_poweroff_device_(cd);
  6908. if (rc)
  6909. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6910. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6911. pt_debug(cd->dev, DL_INFO,
  6912. "%s: Entering into deep standby mode:\n", __func__);
  6913. rc = pt_put_device_into_deep_standby_(cd);
  6914. if (rc)
  6915. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6916. } else {
  6917. pt_debug(cd->dev, DL_INFO,
  6918. "%s: Entering into deep sleep mode:\n", __func__);
  6919. rc = pt_put_device_into_deep_sleep_(cd);
  6920. if (rc)
  6921. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6922. }
  6923. mutex_lock(&cd->system_lock);
  6924. cd->sleep_state = SS_SLEEP_ON;
  6925. mutex_unlock(&cd->system_lock);
  6926. return rc;
  6927. }
  6928. /*******************************************************************************
  6929. * FUNCTION: pt_core_easywake_on_
  6930. *
  6931. * SUMMARY: Suspend the device with easy wake on the
  6932. * configuration in the core platform data structure.
  6933. *
  6934. * RETURN:
  6935. * 0 = success
  6936. * !0 = failure
  6937. *
  6938. * PARAMETERS:
  6939. * *cd - pointer to core data
  6940. ******************************************************************************/
  6941. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6942. {
  6943. int rc = 0;
  6944. mutex_lock(&cd->system_lock);
  6945. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6946. mutex_unlock(&cd->system_lock);
  6947. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6948. __func__, cd->sleep_state);
  6949. return 0;
  6950. }
  6951. mutex_unlock(&cd->system_lock);
  6952. /* Ensure watchdog and startup works stopped */
  6953. pt_stop_wd_timer(cd);
  6954. cancel_work_sync(&cd->enum_work);
  6955. pt_stop_wd_timer(cd);
  6956. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6957. rc = pt_put_device_into_easy_wakeup_(cd);
  6958. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6959. if (rc)
  6960. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6961. }
  6962. mutex_lock(&cd->system_lock);
  6963. cd->sleep_state = SS_EASY_WAKING_ON;
  6964. mutex_unlock(&cd->system_lock);
  6965. return rc;
  6966. }
  6967. /*******************************************************************************
  6968. * FUNCTION: pt_core_easywake_on
  6969. *
  6970. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6971. *
  6972. * RETURN:
  6973. * 0 = success
  6974. * !0 = failure
  6975. *
  6976. * PARAMETERS:
  6977. * *cd - pointer to core data
  6978. ******************************************************************************/
  6979. static int pt_core_easywake_on(struct pt_core_data *cd)
  6980. {
  6981. int rc = 0;
  6982. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6983. if (rc < 0) {
  6984. pt_debug(cd->dev, DL_ERROR,
  6985. "%s: fail get exclusive ex=%p own=%p\n",
  6986. __func__, cd->exclusive_dev, cd->dev);
  6987. return rc;
  6988. }
  6989. rc = pt_core_easywake_on_(cd);
  6990. if (release_exclusive(cd, cd->dev) < 0)
  6991. pt_debug(cd->dev, DL_ERROR,
  6992. "%s: fail to release exclusive\n", __func__);
  6993. else
  6994. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6995. __func__);
  6996. return rc;
  6997. }
  6998. /*******************************************************************************
  6999. * FUNCTION: pt_core_sleep
  7000. *
  7001. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  7002. *
  7003. * RETURN:
  7004. * 0 = success
  7005. * !0 = failure
  7006. *
  7007. * PARAMETERS:
  7008. * *cd - pointer to core data
  7009. ******************************************************************************/
  7010. static int pt_core_sleep(struct pt_core_data *cd)
  7011. {
  7012. int rc = 0;
  7013. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  7014. if (rc < 0) {
  7015. pt_debug(cd->dev, DL_ERROR,
  7016. "%s: fail get exclusive ex=%p own=%p\n",
  7017. __func__, cd->exclusive_dev, cd->dev);
  7018. return rc;
  7019. }
  7020. rc = pt_core_sleep_(cd);
  7021. if (release_exclusive(cd, cd->dev) < 0)
  7022. pt_debug(cd->dev, DL_ERROR,
  7023. "%s: fail to release exclusive\n", __func__);
  7024. else
  7025. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7026. __func__);
  7027. return rc;
  7028. }
  7029. /*******************************************************************************
  7030. * FUNCTION: pt_wakeup_host
  7031. *
  7032. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7033. *
  7034. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7035. *
  7036. * RETURN:
  7037. * 0 = success
  7038. * !0 = failure
  7039. *
  7040. * PARAMETERS:
  7041. * *cd - pointer to core data
  7042. ******************************************************************************/
  7043. static int pt_wakeup_host(struct pt_core_data *cd)
  7044. {
  7045. #ifndef EASYWAKE_TSG6
  7046. /* TSG5 EasyWake */
  7047. int rc = 0;
  7048. int event_id;
  7049. int size = get_unaligned_le16(&cd->input_buf[0]);
  7050. /* Validate report */
  7051. if (size != 4 || cd->input_buf[2] != 4)
  7052. rc = -EINVAL;
  7053. event_id = cd->input_buf[3];
  7054. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7055. __func__, event_id, rc);
  7056. if (rc) {
  7057. pt_core_sleep_(cd);
  7058. goto exit;
  7059. }
  7060. /* attention WAKE */
  7061. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7062. exit:
  7063. return rc;
  7064. #else
  7065. /* TSG6 FW1.3 EasyWake */
  7066. int rc = 0;
  7067. int i = 0;
  7068. int report_length;
  7069. /* Validate report */
  7070. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7071. rc = -EINVAL;
  7072. pt_core_sleep_(cd);
  7073. goto exit;
  7074. }
  7075. /* Get gesture id and gesture data length */
  7076. cd->gesture_id = cd->input_buf[3];
  7077. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7078. cd->gesture_data_length = report_length - 4;
  7079. pt_debug(cd->dev, DL_INFO,
  7080. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7081. __func__, cd->gesture_id, cd->gesture_data_length);
  7082. for (i = 0; i < cd->gesture_data_length; i++)
  7083. cd->gesture_data[i] = cd->input_buf[4 + i];
  7084. /* attention WAKE */
  7085. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7086. exit:
  7087. return rc;
  7088. #endif
  7089. }
  7090. /*******************************************************************************
  7091. * FUNCTION: pt_get_touch_axis
  7092. *
  7093. * SUMMARY: Function to calculate touch axis
  7094. *
  7095. * PARAMETERS:
  7096. * *cd - pointer to core data structure
  7097. * *axis - pointer to axis calculation result
  7098. * size - size in bytes
  7099. * max - max value of result
  7100. * *xy_data - pointer to input data to be parsed
  7101. * bofs - bit offset
  7102. ******************************************************************************/
  7103. static void pt_get_touch_axis(struct pt_core_data *cd,
  7104. int *axis, int size, int max, u8 *data, int bofs)
  7105. {
  7106. int nbyte;
  7107. int next;
  7108. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7109. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7110. next++;
  7111. }
  7112. *axis &= max - 1;
  7113. }
  7114. /*******************************************************************************
  7115. * FUNCTION: move_tracking_heatmap_data
  7116. *
  7117. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7118. * system information structure, xy_mode and xy_data.
  7119. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7120. * the tthe_tuner sysfs node under the label "THM"
  7121. *
  7122. * RETURN:
  7123. * 0 = success
  7124. * !0 = failure
  7125. *
  7126. * PARAMETERS:
  7127. * *cd - pointer to core data
  7128. * *si - pointer to the system information structure
  7129. ******************************************************************************/
  7130. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7131. struct pt_sysinfo *si)
  7132. {
  7133. #ifdef TTHE_TUNER_SUPPORT
  7134. int size = get_unaligned_le16(&cd->input_buf[0]);
  7135. if (size)
  7136. tthe_print(cd, cd->input_buf, size, "THM=");
  7137. #endif
  7138. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7139. return 0;
  7140. }
  7141. /*******************************************************************************
  7142. * FUNCTION: move_sensor_data
  7143. *
  7144. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7145. * information structure, xy_mode and xy_data.
  7146. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7147. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7148. *
  7149. * RETURN:
  7150. * 0 = success
  7151. * !0 = failure
  7152. *
  7153. * PARAMETERS:
  7154. * *cd - pointer to core data
  7155. * *si - pointer to the system information structure
  7156. ******************************************************************************/
  7157. static int move_sensor_data(struct pt_core_data *cd,
  7158. struct pt_sysinfo *si)
  7159. {
  7160. #ifdef TTHE_TUNER_SUPPORT
  7161. int size = get_unaligned_le16(&cd->input_buf[0]);
  7162. if (size)
  7163. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7164. #endif
  7165. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7166. return 0;
  7167. }
  7168. /*******************************************************************************
  7169. * FUNCTION: move_button_data
  7170. *
  7171. * SUMMARY: Move the valid button data from the input buffer into the system
  7172. * information structure, xy_mode and xy_data.
  7173. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7174. * the tthe_tuner sysfs node under the label "OpModeData"
  7175. *
  7176. * RETURN:
  7177. * 0 = success
  7178. * !0 = failure
  7179. *
  7180. * PARAMETERS:
  7181. * *cd - pointer to core data
  7182. * *si - pointer to the system information structure
  7183. ******************************************************************************/
  7184. static int move_button_data(struct pt_core_data *cd,
  7185. struct pt_sysinfo *si)
  7186. {
  7187. #ifdef TTHE_TUNER_SUPPORT
  7188. int size = get_unaligned_le16(&cd->input_buf[0]);
  7189. if (size)
  7190. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7191. #endif
  7192. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7193. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7194. "xy_mode");
  7195. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7196. BTN_REPORT_SIZE);
  7197. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7198. "xy_data");
  7199. return 0;
  7200. }
  7201. /*******************************************************************************
  7202. * FUNCTION: move_touch_data
  7203. *
  7204. * SUMMARY: Move the valid touch data from the input buffer into the system
  7205. * information structure, xy_mode and xy_data.
  7206. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7207. * the tthe_tuner sysfs node under the label "OpModeData"
  7208. *
  7209. * RETURN:
  7210. * 0 = success
  7211. * !0 = failure
  7212. *
  7213. * PARAMETERS:
  7214. * *cd - pointer to core data
  7215. * *si - pointer to the system information structure
  7216. ******************************************************************************/
  7217. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7218. {
  7219. int max_tch = si->sensing_conf_data.max_tch;
  7220. int num_cur_tch;
  7221. int length;
  7222. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7223. #ifdef TTHE_TUNER_SUPPORT
  7224. int size = get_unaligned_le16(&cd->input_buf[0]);
  7225. if (size)
  7226. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7227. #endif
  7228. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7229. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7230. si->desc.tch_header_size, "xy_mode");
  7231. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7232. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7233. if (unlikely(num_cur_tch > max_tch))
  7234. num_cur_tch = max_tch;
  7235. length = num_cur_tch * si->desc.tch_record_size;
  7236. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7237. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7238. return 0;
  7239. }
  7240. /*******************************************************************************
  7241. * FUNCTION: move_hid_pen_data
  7242. *
  7243. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7244. * information structure, xy_mode and xy_data.
  7245. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7246. * the tthe_tuner sysfs node under the label "HID" starting with the
  7247. * report ID.
  7248. *
  7249. * RETURN:
  7250. * 0 = success
  7251. * !0 = failure
  7252. *
  7253. * PARAMETERS:
  7254. * *cd - pointer to core data
  7255. * *si - pointer to the system information structure
  7256. ******************************************************************************/
  7257. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7258. {
  7259. #ifdef TTHE_TUNER_SUPPORT
  7260. int size = get_unaligned_le16(&cd->input_buf[0]);
  7261. if (size) {
  7262. /*
  7263. * HID over USB does not require the two byte length field, so
  7264. * this should print from input_buf[2] but to keep both finger
  7265. * and pen reports the same the length is included
  7266. */
  7267. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7268. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7269. "HID-USB=");
  7270. else
  7271. tthe_print(cd, &(cd->input_buf[0]), size,
  7272. "HID-I2C=");
  7273. }
  7274. #endif
  7275. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7276. return 0;
  7277. }
  7278. /*******************************************************************************
  7279. * FUNCTION: parse_touch_input
  7280. *
  7281. * SUMMARY: Parse the touch report and take action based on the touch
  7282. * report_id.
  7283. *
  7284. * RETURN:
  7285. * 0 = success
  7286. * !0 = failure
  7287. *
  7288. * PARAMETERS:
  7289. * *cd - pointer to core data
  7290. * size - size of touch record
  7291. ******************************************************************************/
  7292. static int parse_touch_input(struct pt_core_data *cd, int size)
  7293. {
  7294. struct pt_sysinfo *si = &cd->sysinfo;
  7295. int report_id = cd->input_buf[2];
  7296. int rc = -EINVAL;
  7297. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7298. __func__);
  7299. if (!si->ready) {
  7300. pt_debug(cd->dev, DL_ERROR,
  7301. "%s: Need system information to parse touches\n",
  7302. __func__);
  7303. return 0;
  7304. }
  7305. if (!si->xy_mode || !si->xy_data)
  7306. return rc;
  7307. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7308. rc = move_touch_data(cd, si);
  7309. else if (report_id == PT_HID_PEN_REPORT_ID)
  7310. rc = move_hid_pen_data(cd, si);
  7311. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7312. rc = move_button_data(cd, si);
  7313. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7314. rc = move_sensor_data(cd, si);
  7315. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7316. rc = move_tracking_heatmap_data(cd, si);
  7317. if (rc)
  7318. return rc;
  7319. /* attention IRQ */
  7320. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7321. return 0;
  7322. }
  7323. /*******************************************************************************
  7324. * FUNCTION: parse_command_input
  7325. *
  7326. * SUMMARY: Move the response data from the input buffer to the response buffer
  7327. *
  7328. * RETURN:
  7329. * 0 = success
  7330. * !0 = failure
  7331. *
  7332. * PARAMETERS:
  7333. * *cd - pointer to core data
  7334. * size - size of response data
  7335. ******************************************************************************/
  7336. static int parse_command_input(struct pt_core_data *cd, int size)
  7337. {
  7338. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7339. __func__);
  7340. memcpy(cd->response_buf, cd->input_buf, size);
  7341. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7342. if (size && cd->show_tt_data) {
  7343. if (cd->pip2_prot_active)
  7344. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7345. else
  7346. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7347. }
  7348. #endif
  7349. mutex_lock(&cd->system_lock);
  7350. cd->hid_cmd_state = 0;
  7351. mutex_unlock(&cd->system_lock);
  7352. wake_up(&cd->wait_q);
  7353. return 0;
  7354. }
  7355. /*******************************************************************************
  7356. * FUNCTION: pt_allow_enumeration
  7357. *
  7358. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7359. * FW sentinel is seen.
  7360. *
  7361. * RETURN:
  7362. * true = allow enumeration or fully re-probe
  7363. * false = skip enumeration and fully re-probe
  7364. *
  7365. * PARAMETERS:
  7366. * *cd - pointer to core data
  7367. ******************************************************************************/
  7368. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7369. {
  7370. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7371. (!cd->hid_reset_cmd_state) &&
  7372. (cd->core_probe_complete) &&
  7373. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7374. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7375. (cd->mode == PT_MODE_OPERATIONAL)) {
  7376. return true;
  7377. }
  7378. if ((!cd->hid_reset_cmd_state) &&
  7379. (cd->core_probe_complete) &&
  7380. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7381. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7382. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7383. return true;
  7384. }
  7385. pt_debug(cd->dev, DL_INFO,
  7386. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7387. __func__,
  7388. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7389. "core_probe_complete(1)", cd->core_probe_complete,
  7390. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7391. "active_dut_gen(0,2)", cd->active_dut_generation);
  7392. return false;
  7393. }
  7394. /*******************************************************************************
  7395. * FUNCTION: pt_is_touch_report
  7396. *
  7397. * SUMMARY: Determine if a report ID should be treated as a touch report
  7398. *
  7399. * RETURN:
  7400. * true = report ID is a touch report
  7401. * false = report ID is not a touch report
  7402. *
  7403. * PARAMETERS:
  7404. * id - Report ID
  7405. ******************************************************************************/
  7406. static bool pt_is_touch_report(int id)
  7407. {
  7408. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7409. id == PT_HID_PEN_REPORT_ID ||
  7410. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7411. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7412. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7413. return true;
  7414. else
  7415. return false;
  7416. }
  7417. /*******************************************************************************
  7418. * FUNCTION: pt_parse_input
  7419. *
  7420. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7421. * on if its a response to a command or asynchronous touch data / reset
  7422. * sentinel. PIP2.x messages have additional error checking that is
  7423. * parsed (SEQ match from cmd to rsp, CRC valid).
  7424. * Look for special packets based on unique lengths:
  7425. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7426. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7427. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7428. * 0xFFXX - Empty buffer (PIP 1.7+)
  7429. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7430. * the initial probe has not yet been done.
  7431. *
  7432. * RETURN:
  7433. * 0 = success
  7434. * !0 = failure
  7435. *
  7436. * PARAMETERS:
  7437. * *cd - pointer to core data
  7438. ******************************************************************************/
  7439. static int pt_parse_input(struct pt_core_data *cd)
  7440. {
  7441. int report_id;
  7442. int cmd_id;
  7443. int is_command = 0;
  7444. int size;
  7445. int print_size;
  7446. bool touch_report = true;
  7447. unsigned short calc_crc;
  7448. unsigned short resp_crc;
  7449. cd->fw_sys_mode_in_standby_state = false;
  7450. size = get_unaligned_le16(&cd->input_buf[0]);
  7451. print_size = size;
  7452. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7453. __func__, print_size);
  7454. if (print_size <= PT_MAX_INPUT)
  7455. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7456. "<<< Read buf");
  7457. if (size == 0 ||
  7458. (size == 11 &&
  7459. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7460. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7461. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7462. PIP2_CMD_COMMAND_ID_MASK) ==
  7463. PIP2_CMD_ID_STATUS)) {
  7464. touch_report = false;
  7465. cd->hw_detected = true;
  7466. cd->bl_pip_ver_ready = false;
  7467. cd->app_pip_ver_ready = false;
  7468. if (size == 0) {
  7469. mutex_lock(&cd->system_lock);
  7470. cd->pip2_prot_active = false;
  7471. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7472. /*
  7473. * For Gen5/6 this sentinel could be from
  7474. * the BL or FW. Attempt to set the correct
  7475. * mode based on the previous PIP command.
  7476. */
  7477. if (cd->hid_cmd_state ==
  7478. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7479. cd->mode = PT_MODE_OPERATIONAL;
  7480. cd->startup_status =
  7481. STARTUP_STATUS_FW_RESET_SENTINEL;
  7482. } else if (cd->hid_cmd_state ==
  7483. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7484. cd->hid_reset_cmd_state) {
  7485. cd->mode = PT_MODE_BOOTLOADER;
  7486. cd->startup_status =
  7487. STARTUP_STATUS_BL_RESET_SENTINEL;
  7488. } else {
  7489. cd->mode = PT_MODE_UNKNOWN;
  7490. cd->startup_status =
  7491. STARTUP_STATUS_START;
  7492. }
  7493. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7494. pt_debug(cd->dev, DL_INFO,
  7495. "%s: ATM Gen5/6 %s sentinel received\n",
  7496. __func__,
  7497. (cd->mode == PT_MODE_OPERATIONAL ?
  7498. "FW" :
  7499. (cd->mode == PT_MODE_BOOTLOADER ?
  7500. "BL" : "Unknown")));
  7501. } else {
  7502. cd->mode = PT_MODE_OPERATIONAL;
  7503. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7504. cd->startup_status =
  7505. STARTUP_STATUS_FW_RESET_SENTINEL;
  7506. pt_debug(cd->dev, DL_INFO,
  7507. "%s: ATM PT/TT FW sentinel received\n",
  7508. __func__);
  7509. }
  7510. mutex_unlock(&cd->system_lock);
  7511. if (pt_allow_enumeration(cd)) {
  7512. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7513. pt_debug(cd->dev, DL_INFO,
  7514. "%s: Queue Restart\n", __func__);
  7515. pt_queue_restart(cd);
  7516. } else {
  7517. pt_debug(cd->dev, DL_INFO,
  7518. "%s: Queue Enum\n", __func__);
  7519. pt_queue_enum(cd);
  7520. }
  7521. } else {
  7522. pt_debug(cd->dev, DL_INFO,
  7523. "%s: Sentinel - No Queued Action\n",
  7524. __func__);
  7525. }
  7526. } else {
  7527. /* Sentinel must be from TT/TC BL */
  7528. mutex_lock(&cd->system_lock);
  7529. cd->pip2_prot_active = true;
  7530. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7531. cd->mode = PT_MODE_BOOTLOADER;
  7532. cd->sysinfo.ready = false;
  7533. mutex_unlock(&cd->system_lock);
  7534. pt_debug(cd->dev, DL_INFO,
  7535. "%s: BL Reset sentinel received\n", __func__);
  7536. if (cd->flashless_dut &&
  7537. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7538. pt_debug(cd->dev, DL_INFO,
  7539. "%s: BL to RAM for flashless DUT\n",
  7540. __func__);
  7541. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7542. }
  7543. }
  7544. mutex_lock(&cd->system_lock);
  7545. memcpy(cd->response_buf, cd->input_buf, 2);
  7546. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7547. mutex_unlock(&cd->system_lock);
  7548. pt_debug(cd->dev, DL_WARN,
  7549. "%s: Device Initiated Reset\n", __func__);
  7550. wake_up(&cd->wait_q);
  7551. return 0;
  7552. }
  7553. cd->hid_reset_cmd_state = 0;
  7554. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7555. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7556. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7557. cd->hid_cmd_state = 0;
  7558. wake_up(&cd->wait_q);
  7559. mutex_unlock(&cd->system_lock);
  7560. return 0;
  7561. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7562. /*
  7563. * This debug message below is used by PBATS to calculate the
  7564. * time from the last lift off IRQ to when FW exits LFT mode.
  7565. */
  7566. touch_report = false;
  7567. pt_debug(cd->dev, DL_WARN,
  7568. "%s: DUT - Empty buffer detected\n", __func__);
  7569. return 0;
  7570. } else if (size > PT_MAX_INPUT || size < 0) {
  7571. pt_debug(cd->dev, DL_ERROR,
  7572. "%s: DUT - Unexpected len field in active bus data!\n",
  7573. __func__);
  7574. return -EINVAL;
  7575. }
  7576. if (cd->pip2_prot_active) {
  7577. pt_debug(cd->dev, DL_DEBUG,
  7578. "%s: Decode PIP2.x Response\n", __func__);
  7579. /* PIP2 does not have a report id, hard code it */
  7580. report_id = 0x00;
  7581. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7582. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7583. resp_crc = cd->input_buf[size - 2] << 8;
  7584. resp_crc |= cd->input_buf[size - 1];
  7585. if ((cd->pip2_cmd_tag_seq !=
  7586. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7587. (resp_crc != calc_crc) &&
  7588. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7589. == PT_PIP_TOUCH_REPORT_ID) ||
  7590. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7591. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7592. pt_debug(cd->dev, DL_WARN,
  7593. "%s: %s %d %s\n",
  7594. __func__,
  7595. "Received PIP1 report id =",
  7596. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7597. "when expecting a PIP2 report - IGNORE report");
  7598. return 0;
  7599. }
  7600. is_command = 1;
  7601. touch_report = false;
  7602. } else {
  7603. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7604. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7605. }
  7606. #ifdef TTDL_DIAGNOSTICS
  7607. pt_debug(cd->dev, DL_DEBUG,
  7608. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7609. __func__, cd->pip2_prot_active, report_id,
  7610. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7611. #endif /* TTDL_DIAGNOSTICS */
  7612. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7613. pt_wakeup_host(cd);
  7614. #ifdef TTHE_TUNER_SUPPORT
  7615. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7616. #endif
  7617. return 0;
  7618. }
  7619. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7620. msecs_to_jiffies(cd->watchdog_interval));
  7621. /*
  7622. * If it is PIP2 response, the report_id has been set to 0,
  7623. * so it will not be parsed as a touch packet.
  7624. */
  7625. if (!pt_is_touch_report(report_id)) {
  7626. is_command = 1;
  7627. touch_report = false;
  7628. }
  7629. if (unlikely(is_command)) {
  7630. parse_command_input(cd, size);
  7631. return 0;
  7632. }
  7633. if (touch_report)
  7634. parse_touch_input(cd, size);
  7635. return 0;
  7636. }
  7637. /*******************************************************************************
  7638. * FUNCTION: pt_read_input
  7639. *
  7640. * SUMMARY: Reads incoming data off of the active bus
  7641. *
  7642. * RETURN:
  7643. * 0 = success
  7644. * !0 = failure
  7645. *
  7646. * PARAMETERS:
  7647. * *cd - pointer to core data
  7648. ******************************************************************************/
  7649. static int pt_read_input(struct pt_core_data *cd)
  7650. {
  7651. struct device *dev = cd->dev;
  7652. int rc = 0;
  7653. int t;
  7654. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7655. /*
  7656. * Workaround for easywake failure
  7657. * Interrupt for easywake, wait for bus controller to wake
  7658. */
  7659. mutex_lock(&cd->system_lock);
  7660. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7661. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7662. mutex_unlock(&cd->system_lock);
  7663. if (!dev->power.is_suspended)
  7664. goto read;
  7665. t = wait_event_timeout(cd->wait_q,
  7666. (cd->wait_until_wake == 1),
  7667. msecs_to_jiffies(2000));
  7668. #ifdef TTDL_DIAGNOSTICS
  7669. if (IS_TMO(t)) {
  7670. cd->bus_transmit_error_count++;
  7671. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7672. pt_debug(dev, DL_ERROR,
  7673. "%s: !!!I2C Transmission Error %d\n",
  7674. __func__,
  7675. cd->bus_transmit_error_count);
  7676. }
  7677. #else
  7678. if (IS_TMO(t))
  7679. pt_queue_enum(cd);
  7680. #endif /* TTDL_DIAGNOSTICS */
  7681. goto read;
  7682. }
  7683. }
  7684. mutex_unlock(&cd->system_lock);
  7685. read:
  7686. /* Try reading up to 'retry' times */
  7687. while (retry-- != 0) {
  7688. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7689. PT_MAX_INPUT);
  7690. if (!rc) {
  7691. pt_debug(dev, DL_DEBUG,
  7692. "%s: Read input successfully\n", __func__);
  7693. goto read_exit;
  7694. }
  7695. usleep_range(1000, 2000);
  7696. }
  7697. pt_debug(dev, DL_ERROR,
  7698. "%s: Error getting report, rc=%d\n", __func__, rc);
  7699. read_exit:
  7700. return rc;
  7701. }
  7702. /*******************************************************************************
  7703. * FUNCTION: pt_irq
  7704. *
  7705. * SUMMARY: Process all detected interrupts
  7706. *
  7707. * RETURN:
  7708. * IRQ_HANDLED - Finished processing the interrupt
  7709. *
  7710. * PARAMETERS:
  7711. * irq - IRQ number
  7712. * *handle - pointer to core data struct
  7713. ******************************************************************************/
  7714. irqreturn_t pt_irq(int irq, void *handle)
  7715. {
  7716. struct pt_core_data *cd = handle;
  7717. int rc = 0;
  7718. if (!pt_check_irq_asserted(cd))
  7719. return IRQ_HANDLED;
  7720. rc = pt_read_input(cd);
  7721. #ifdef TTDL_DIAGNOSTICS
  7722. cd->irq_count++;
  7723. /* Used to calculate T-Refresh */
  7724. if (cd->t_refresh_active) {
  7725. if (cd->t_refresh_count == 0) {
  7726. cd->t_refresh_time = jiffies;
  7727. cd->t_refresh_count++;
  7728. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7729. cd->t_refresh_count++;
  7730. } else {
  7731. cd->t_refresh_active = 0;
  7732. cd->t_refresh_time =
  7733. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7734. }
  7735. }
  7736. #endif /* TTDL_DIAGNOSTICS */
  7737. if (!rc)
  7738. pt_parse_input(cd);
  7739. return IRQ_HANDLED;
  7740. }
  7741. /*******************************************************************************
  7742. * FUNCTION: _pt_subscribe_attention
  7743. *
  7744. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7745. * to subscribe themselves into the TTDL attention list
  7746. *
  7747. * RETURN:
  7748. * 0 = success
  7749. * !0 = failure
  7750. *
  7751. * PARAMETERS:
  7752. * *dev - pointer to device structure
  7753. * type - attention type enum
  7754. * *id - ID of the module calling this function
  7755. * *func - callback function pointer to be called when notified
  7756. * mode - attention mode
  7757. ******************************************************************************/
  7758. int _pt_subscribe_attention(struct device *dev,
  7759. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7760. int mode)
  7761. {
  7762. struct pt_core_data *cd = dev_get_drvdata(dev);
  7763. struct atten_node *atten, *atten_new;
  7764. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7765. if (!atten_new)
  7766. return -ENOMEM;
  7767. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7768. dev_name(cd->dev));
  7769. spin_lock(&cd->spinlock);
  7770. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7771. if (atten->id == id && atten->mode == mode) {
  7772. spin_unlock(&cd->spinlock);
  7773. kfree(atten_new);
  7774. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7775. __func__,
  7776. "already subscribed attention",
  7777. dev, "mode", mode);
  7778. return 0;
  7779. }
  7780. }
  7781. atten_new->id = id;
  7782. atten_new->dev = dev;
  7783. atten_new->mode = mode;
  7784. atten_new->func = func;
  7785. list_add(&atten_new->node, &cd->atten_list[type]);
  7786. spin_unlock(&cd->spinlock);
  7787. return 0;
  7788. }
  7789. /*******************************************************************************
  7790. * FUNCTION: _pt_unsubscribe_attention
  7791. *
  7792. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7793. * to unsubscribe themselves from the TTDL attention list
  7794. *
  7795. * RETURN:
  7796. * 0 = success
  7797. * !0 = failure
  7798. *
  7799. * PARAMETERS:
  7800. * *dev - pointer to device structure
  7801. * type - attention type enum
  7802. * *id - ID of the module calling this function
  7803. * *func - function pointer
  7804. * mode - attention mode
  7805. ******************************************************************************/
  7806. int _pt_unsubscribe_attention(struct device *dev,
  7807. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7808. int mode)
  7809. {
  7810. struct pt_core_data *cd = dev_get_drvdata(dev);
  7811. struct atten_node *atten, *atten_n;
  7812. spin_lock(&cd->spinlock);
  7813. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7814. if (atten->id == id && atten->mode == mode) {
  7815. list_del(&atten->node);
  7816. spin_unlock(&cd->spinlock);
  7817. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7818. __func__,
  7819. "unsub for atten->dev", atten->dev,
  7820. "atten->mode", atten->mode);
  7821. kfree(atten);
  7822. return 0;
  7823. }
  7824. }
  7825. spin_unlock(&cd->spinlock);
  7826. return -ENODEV;
  7827. }
  7828. /*******************************************************************************
  7829. * FUNCTION: _pt_request_exclusive
  7830. *
  7831. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7832. * to request exclusive access
  7833. *
  7834. * RETURN:
  7835. * 0 = success
  7836. * !0 = failure
  7837. *
  7838. * PARAMETERS:
  7839. * *dev - pointer to device structure
  7840. * timeout_ms - timeout to wait for exclusive access
  7841. ******************************************************************************/
  7842. static int _pt_request_exclusive(struct device *dev,
  7843. int timeout_ms)
  7844. {
  7845. struct pt_core_data *cd = dev_get_drvdata(dev);
  7846. return request_exclusive(cd, (void *)dev, timeout_ms);
  7847. }
  7848. /*******************************************************************************
  7849. * FUNCTION: _pt_release_exclusive
  7850. *
  7851. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7852. * to release exclusive access
  7853. *
  7854. * RETURN:
  7855. * 0 = success
  7856. * !0 = failure
  7857. *
  7858. * PARAMETERS:
  7859. * *dev - pointer to device structure
  7860. ******************************************************************************/
  7861. static int _pt_release_exclusive(struct device *dev)
  7862. {
  7863. struct pt_core_data *cd = dev_get_drvdata(dev);
  7864. return release_exclusive(cd, (void *)dev);
  7865. }
  7866. /*******************************************************************************
  7867. * FUNCTION: _pt_request_reset
  7868. *
  7869. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7870. * to request the DUT to be reset. Function returns err if refused or
  7871. * timeout occurs (Note: core uses fixed timeout period).
  7872. *
  7873. * NOTE: Function blocks until ISR occurs.
  7874. *
  7875. * RETURN:
  7876. * 0 = success
  7877. * !0 = failure
  7878. *
  7879. * PARAMETERS:
  7880. * *dev - pointer to device structure
  7881. * protect - flag to call protected or non-protected
  7882. ******************************************************************************/
  7883. static int _pt_request_reset(struct device *dev, int protect)
  7884. {
  7885. struct pt_core_data *cd = dev_get_drvdata(dev);
  7886. int rc;
  7887. rc = pt_dut_reset(cd, protect);
  7888. if (rc < 0) {
  7889. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7890. __func__, rc);
  7891. }
  7892. return rc;
  7893. }
  7894. /*******************************************************************************
  7895. * FUNCTION: _pt_request_enum
  7896. *
  7897. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7898. * to request TTDL to queue an enum. This function will return err
  7899. * if refused; if no error then enum has completed and system is in
  7900. * normal operation mode.
  7901. *
  7902. * RETURN:
  7903. * 0 = success
  7904. * !0 = failure
  7905. *
  7906. * PARAMETERS:
  7907. * *dev - pointer to device structure
  7908. * wait - boolean to determine if to wait for startup event
  7909. ******************************************************************************/
  7910. static int _pt_request_enum(struct device *dev, bool wait)
  7911. {
  7912. struct pt_core_data *cd = dev_get_drvdata(dev);
  7913. pt_queue_enum(cd);
  7914. if (wait)
  7915. wait_event_timeout(cd->wait_q,
  7916. cd->startup_state == STARTUP_NONE,
  7917. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7918. return 0;
  7919. }
  7920. /*******************************************************************************
  7921. * FUNCTION: _pt_request_sysinfo
  7922. *
  7923. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7924. * to request the pointer to the system information structure. This
  7925. * function will return NULL if sysinfo has not been acquired from the
  7926. * DUT yet.
  7927. *
  7928. * RETURN: Pointer to the system information struct
  7929. *
  7930. * PARAMETERS:
  7931. * *dev - pointer to device structure
  7932. ******************************************************************************/
  7933. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7934. {
  7935. struct pt_core_data *cd = dev_get_drvdata(dev);
  7936. int rc = 0;
  7937. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7938. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7939. !cd->sysinfo.ready) {
  7940. rc = pt_hid_output_get_sysinfo_(cd);
  7941. if (rc) {
  7942. pt_debug(cd->dev, DL_ERROR,
  7943. "%s: Error getting sysinfo rc=%d\n",
  7944. __func__, rc);
  7945. }
  7946. }
  7947. if (cd->sysinfo.ready)
  7948. return &cd->sysinfo;
  7949. return NULL;
  7950. }
  7951. /*******************************************************************************
  7952. * FUNCTION: _pt_request_loader_pdata
  7953. *
  7954. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7955. * to request the pointer to the loader platform data
  7956. *
  7957. * RETURN: Pointer to the loader platform data struct
  7958. *
  7959. * PARAMETERS:
  7960. * *dev - pointer to device structure
  7961. ******************************************************************************/
  7962. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7963. struct device *dev)
  7964. {
  7965. struct pt_core_data *cd = dev_get_drvdata(dev);
  7966. return cd->pdata->loader_pdata;
  7967. }
  7968. /*******************************************************************************
  7969. * FUNCTION: _pt_request_start_wd
  7970. *
  7971. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7972. * to request to start the TTDL watchdog
  7973. *
  7974. * RETURN:
  7975. * 0 = success
  7976. * !0 = failure
  7977. *
  7978. * PARAMETERS:
  7979. * *dev - pointer to device structure
  7980. ******************************************************************************/
  7981. static int _pt_request_start_wd(struct device *dev)
  7982. {
  7983. struct pt_core_data *cd = dev_get_drvdata(dev);
  7984. pt_start_wd_timer(cd);
  7985. return 0;
  7986. }
  7987. /*******************************************************************************
  7988. * FUNCTION: _pt_request_stop_wd
  7989. *
  7990. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7991. * to request to stop the TTDL watchdog
  7992. *
  7993. * RETURN:
  7994. * 0 = success
  7995. * !0 = failure
  7996. *
  7997. * PARAMETERS:
  7998. * *dev - pointer to device structure
  7999. ******************************************************************************/
  8000. static int _pt_request_stop_wd(struct device *dev)
  8001. {
  8002. struct pt_core_data *cd = dev_get_drvdata(dev);
  8003. pt_stop_wd_timer(cd);
  8004. return 0;
  8005. }
  8006. /*******************************************************************************
  8007. * FUNCTION: pt_pip2_launch_app
  8008. *
  8009. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  8010. * the FW reset sentinel to indicate the function succeeded.
  8011. *
  8012. * NOTE: Calling this function when the DUT is in Application mode WILL result
  8013. * in a timeout delay and with the DUT being reset with an XRES.
  8014. *
  8015. * RETURN:
  8016. * 0 = success
  8017. * !0 = failure
  8018. *
  8019. * PARAMETERS:
  8020. * *cd - pointer to core data structure
  8021. * protect - flag to call protected or non-protected
  8022. ******************************************************************************/
  8023. static int pt_pip2_launch_app(struct device *dev, int protect)
  8024. {
  8025. struct pt_core_data *cd = dev_get_drvdata(dev);
  8026. u16 actual_read_len;
  8027. u16 tmp_startup_status = cd->startup_status;
  8028. u8 read_buf[12];
  8029. u8 status;
  8030. int time = 0;
  8031. int rc = 0;
  8032. mutex_lock(&cd->system_lock);
  8033. cd->startup_status = STARTUP_STATUS_START;
  8034. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8035. mutex_unlock(&cd->system_lock);
  8036. rc = _pt_request_pip2_send_cmd(dev, protect,
  8037. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8038. &actual_read_len);
  8039. if (rc) {
  8040. pt_debug(dev, DL_ERROR,
  8041. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8042. __func__, rc);
  8043. } else {
  8044. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8045. /* Test for no or invalid image in FLASH, no point to reset */
  8046. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8047. rc = status;
  8048. goto exit;
  8049. }
  8050. /* Any other boot failure */
  8051. if (status != 0) {
  8052. pt_debug(dev, DL_ERROR,
  8053. "%s: FW did not EXECUTE, status = %d\n",
  8054. __func__, status);
  8055. rc = status;
  8056. }
  8057. }
  8058. if (rc) {
  8059. pt_debug(dev, DL_ERROR,
  8060. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8061. __func__, rc);
  8062. goto exit;
  8063. }
  8064. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8065. msleep(20);
  8066. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8067. __func__, time, cd->startup_status);
  8068. time += 20;
  8069. }
  8070. if (cd->startup_status == STARTUP_STATUS_START) {
  8071. pt_debug(cd->dev, DL_WARN,
  8072. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8073. rc = -ETIME;
  8074. }
  8075. exit:
  8076. if (cd->startup_status == STARTUP_STATUS_START) {
  8077. /* Reset to original state because we could be stuck in BL */
  8078. mutex_lock(&cd->system_lock);
  8079. cd->startup_status = tmp_startup_status;
  8080. mutex_unlock(&cd->system_lock);
  8081. }
  8082. return rc;
  8083. }
  8084. /*******************************************************************************
  8085. * FUNCTION: _pt_request_pip2_launch_app
  8086. *
  8087. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8088. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8089. * consumed.
  8090. *
  8091. * RETURN:
  8092. * 0 = success
  8093. * !0 = failure
  8094. *
  8095. * PARAMETERS:
  8096. * *cd - pointer to core data structure
  8097. ******************************************************************************/
  8098. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8099. {
  8100. return pt_pip2_launch_app(dev, protect);
  8101. }
  8102. /*******************************************************************************
  8103. * FUNCTION: _pt_request_wait_for_enum_state
  8104. *
  8105. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8106. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8107. *
  8108. * RETURN:
  8109. * 0 = success
  8110. * !0 = failure
  8111. *
  8112. * PARAMETERS:
  8113. * *cd - pointer to core data structure
  8114. * timeout - timeout for how long to wait
  8115. * state - enum state to wait for
  8116. ******************************************************************************/
  8117. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8118. int state)
  8119. {
  8120. int rc = 0;
  8121. int t;
  8122. struct pt_core_data *cd = dev_get_drvdata(dev);
  8123. t = wait_event_timeout(cd->wait_q,
  8124. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8125. msecs_to_jiffies(timeout));
  8126. if (IS_TMO(t)) {
  8127. pt_debug(cd->dev, DL_ERROR,
  8128. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8129. __func__, state, timeout);
  8130. pt_debug(cd->dev, DL_WARN,
  8131. "%s: enum state reached 0x%04X\n",
  8132. __func__, cd->startup_status);
  8133. rc = -ETIME;
  8134. } else if (cd->startup_status & state) {
  8135. pt_debug(cd->dev, DL_INFO,
  8136. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8137. __func__, cd->startup_status,
  8138. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8139. } else {
  8140. if (t == 1) {
  8141. pt_debug(
  8142. cd->dev, DL_ERROR,
  8143. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8144. __func__, state, timeout);
  8145. rc = -ETIME;
  8146. } else {
  8147. pt_debug(
  8148. cd->dev, DL_ERROR,
  8149. "%s: Enum state 0x%04X not reached in %dms\n",
  8150. __func__, state, timeout - jiffies_to_msecs(t));
  8151. rc = -EINVAL;
  8152. }
  8153. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8154. __func__, cd->startup_status);
  8155. }
  8156. return rc;
  8157. }
  8158. /*******************************************************************************
  8159. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8160. *
  8161. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8162. * deep sleep.
  8163. *
  8164. * RETURN:
  8165. * 0 = success
  8166. * !0 = error
  8167. *
  8168. * PARAMETERS:
  8169. * *cd - pointer to core data
  8170. ******************************************************************************/
  8171. static int pt_core_wake_device_from_deep_sleep_(
  8172. struct pt_core_data *cd)
  8173. {
  8174. int rc;
  8175. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8176. if (rc)
  8177. rc = -EAGAIN;
  8178. /* Prevent failure on sequential wake/sleep requests from OS */
  8179. msleep(20);
  8180. return rc;
  8181. }
  8182. /*******************************************************************************
  8183. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8184. *
  8185. * SUMMARY: Wake up device from Easy-Wake state.
  8186. *
  8187. * RETURN:
  8188. * 0 = success
  8189. * !0 = failure
  8190. *
  8191. * PARAMETERS:
  8192. * *cd - pointer to core data
  8193. ******************************************************************************/
  8194. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8195. {
  8196. int rc = 0;
  8197. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8198. return rc;
  8199. }
  8200. /*******************************************************************************
  8201. * FUNCTION: pt_restore_parameters_
  8202. *
  8203. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8204. * back to the DUT
  8205. *
  8206. * RETURN:
  8207. * 0 = success
  8208. * !0 = failure
  8209. *
  8210. * PARAMETERS:
  8211. * *cd - pointer the core data structure
  8212. ******************************************************************************/
  8213. static int pt_restore_parameters_(struct pt_core_data *cd)
  8214. {
  8215. struct param_node *param;
  8216. int rc = 0;
  8217. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8218. goto exit;
  8219. spin_lock(&cd->spinlock);
  8220. list_for_each_entry(param, &cd->param_list, node) {
  8221. spin_unlock(&cd->spinlock);
  8222. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8223. __func__, param->id, param->value);
  8224. rc = pt_pip_set_param_(cd, param->id,
  8225. param->value, param->size);
  8226. if (rc)
  8227. goto exit;
  8228. spin_lock(&cd->spinlock);
  8229. }
  8230. spin_unlock(&cd->spinlock);
  8231. exit:
  8232. return rc;
  8233. }
  8234. /*******************************************************************************
  8235. * FUNCTION: pt_pip2_exit_bl_
  8236. *
  8237. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8238. * a DUT that may not have flash and will need FW to be loaded into RAM
  8239. *
  8240. * RETURN:
  8241. * 0 = success
  8242. * !0 = failure
  8243. *
  8244. * PARAMETERS:
  8245. * *cd - pointer the core data structure
  8246. * *status_str - pointer to optional status string buffer
  8247. * buf_size - size of status_str buffer
  8248. ******************************************************************************/
  8249. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8250. {
  8251. int rc;
  8252. int wait_time = 0;
  8253. u8 mode = PT_MODE_UNKNOWN;
  8254. bool load_status_str = false;
  8255. /*
  8256. * Below function has protective call to ensure no enum is still on
  8257. * going, while this kind of protection should be applied widely in
  8258. * future (TODO).
  8259. */
  8260. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8261. if (status_str && buf_size <= 50)
  8262. load_status_str = true;
  8263. if (mode == PT_MODE_BOOTLOADER) {
  8264. if (cd->flashless_dut == 1) {
  8265. rc = pt_hw_hard_reset(cd);
  8266. } else {
  8267. rc = pt_pip2_launch_app(cd->dev,
  8268. PT_CORE_CMD_UNPROTECTED);
  8269. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8270. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8271. __func__, "Invalid image in FLASH rc", rc);
  8272. } else if (rc) {
  8273. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8274. __func__, "Failed to launch app rc", rc);
  8275. }
  8276. }
  8277. if (!rc) {
  8278. if (cd->flashless_dut == 1) {
  8279. /* Wait for BL to complete before enum */
  8280. rc = _pt_request_wait_for_enum_state(cd->dev,
  8281. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8282. if (rc && load_status_str) {
  8283. strlcpy(status_str, "No FW sentinel after BL",
  8284. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8285. goto exit;
  8286. }
  8287. }
  8288. /*
  8289. * If the host wants to interact with the FW or do a
  8290. * forced calibration, the FW must be out of BOOT mode
  8291. * and the system information must have been retrieved.
  8292. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8293. * If, however, the enumeration does not reach this
  8294. * point, the DUT may still be in APP mode so test
  8295. * for all conditions.
  8296. */
  8297. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8298. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8299. if (!rc || cd->startup_status >=
  8300. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8301. mutex_lock(&cd->system_lock);
  8302. cd->mode = PT_MODE_OPERATIONAL;
  8303. mutex_unlock(&cd->system_lock);
  8304. }
  8305. if (rc) {
  8306. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8307. __func__, "Failed to enum with DUT",
  8308. cd->startup_status);
  8309. if (load_status_str && !(cd->startup_status &
  8310. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8311. strlcpy(status_str, "FW Stuck in Boot mode",
  8312. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8313. goto exit;
  8314. }
  8315. }
  8316. /*
  8317. * The coming FW sentinel could wake up the event
  8318. * queue, which has chance to be taken by next command
  8319. * wrongly. Following delay is a workaround to avoid
  8320. * this issue for most situations.
  8321. */
  8322. msleep(20);
  8323. pt_start_wd_timer(cd);
  8324. }
  8325. if (load_status_str) {
  8326. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8327. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8328. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8329. else if (!rc)
  8330. strlcpy(status_str, "Entered APP from BL mode",
  8331. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8332. else
  8333. strlcpy(status_str, "Failed to enter APP from BL mode",
  8334. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8335. }
  8336. } else if (mode == PT_MODE_OPERATIONAL) {
  8337. mutex_lock(&cd->system_lock);
  8338. cd->mode = mode;
  8339. mutex_unlock(&cd->system_lock);
  8340. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8341. if (load_status_str) {
  8342. if (!rc)
  8343. strlcpy(status_str, "Already in APP mode",
  8344. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8345. else
  8346. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8347. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8348. }
  8349. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8350. mutex_lock(&cd->system_lock);
  8351. cd->mode = mode;
  8352. mutex_unlock(&cd->system_lock);
  8353. if (load_status_str)
  8354. strlcpy(status_str, "Failed to determine active mode",
  8355. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8356. }
  8357. exit:
  8358. if (!rc)
  8359. pt_start_wd_timer(cd);
  8360. return rc;
  8361. }
  8362. /*******************************************************************************
  8363. * FUNCTION: pt_pip2_exit_bl
  8364. *
  8365. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8366. * access.
  8367. *
  8368. * RETURN:
  8369. * 0 = success
  8370. * !0 = failure
  8371. *
  8372. * PARAMETERS:
  8373. * *cd - pointer the core data structure
  8374. * *status_str - pointer to optional status string buffer
  8375. * buf_size - size of status_str buffer
  8376. ******************************************************************************/
  8377. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8378. {
  8379. int rc;
  8380. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8381. if (rc < 0) {
  8382. pt_debug(cd->dev, DL_ERROR,
  8383. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8384. cd->exclusive_dev, cd->dev);
  8385. return rc;
  8386. }
  8387. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8388. if (release_exclusive(cd, cd->dev) < 0)
  8389. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8390. __func__);
  8391. return rc;
  8392. }
  8393. /*******************************************************************************
  8394. * FUNCTION: _fast_startup
  8395. *
  8396. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8397. *
  8398. * RETURN:
  8399. * 0 = success
  8400. * !0 = failure
  8401. *
  8402. * PARAMETERS:
  8403. * *cd - pointer the core data structure
  8404. ******************************************************************************/
  8405. static int _fast_startup(struct pt_core_data *cd)
  8406. {
  8407. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8408. int rc = 0;
  8409. u8 mode = PT_MODE_UNKNOWN;
  8410. struct pt_hid_desc hid_desc;
  8411. int wait_time = 0;
  8412. memset(&hid_desc, 0, sizeof(hid_desc));
  8413. reset:
  8414. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8415. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8416. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8417. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8418. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8419. __func__);
  8420. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8421. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8422. if (rc < 0) {
  8423. pt_debug(cd->dev, DL_ERROR,
  8424. "%s: Error on getting HID descriptor r=%d\n",
  8425. __func__, rc);
  8426. if (retry--)
  8427. goto reset;
  8428. goto exit;
  8429. }
  8430. cd->mode = pt_get_mode(cd, &hid_desc);
  8431. if (cd->mode == PT_MODE_BOOTLOADER) {
  8432. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8433. __func__);
  8434. rc = pt_hid_output_bl_launch_app_(cd);
  8435. if (rc < 0) {
  8436. pt_debug(cd->dev, DL_ERROR,
  8437. "%s: Error on launch app r=%d\n",
  8438. __func__, rc);
  8439. if (retry--)
  8440. goto reset;
  8441. goto exit;
  8442. }
  8443. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8444. if (rc < 0) {
  8445. pt_debug(cd->dev, DL_ERROR,
  8446. "%s: Error on getting HID descriptor r=%d\n",
  8447. __func__, rc);
  8448. if (retry--)
  8449. goto reset;
  8450. goto exit;
  8451. }
  8452. cd->mode = pt_get_mode(cd, &hid_desc);
  8453. if (cd->mode == PT_MODE_BOOTLOADER) {
  8454. if (retry--)
  8455. goto reset;
  8456. goto exit;
  8457. }
  8458. }
  8459. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8460. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8461. } else {
  8462. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8463. __func__);
  8464. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8465. /* Wait for any sentinel before first try */
  8466. rc = _pt_request_wait_for_enum_state(
  8467. cd->dev, 150,
  8468. STARTUP_STATUS_BL_RESET_SENTINEL |
  8469. STARTUP_STATUS_FW_RESET_SENTINEL);
  8470. if (rc)
  8471. pt_debug(cd->dev, DL_ERROR,
  8472. "%s: No Sentinel detected rc = %d\n",
  8473. __func__, rc);
  8474. } else
  8475. pt_flush_bus_if_irq_asserted(cd,
  8476. PT_FLUSH_BUS_BASED_ON_LEN);
  8477. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8478. if (rc) {
  8479. pt_debug(cd->dev, DL_ERROR,
  8480. "%s: Get mode failed, mode unknown\n",
  8481. __func__);
  8482. mode = PT_MODE_UNKNOWN;
  8483. }
  8484. cd->mode = mode;
  8485. if (cd->mode == PT_MODE_BOOTLOADER) {
  8486. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8487. __func__);
  8488. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8489. if (rc) {
  8490. pt_debug(cd->dev, DL_ERROR,
  8491. "%s Failed to exit bootloader\n",
  8492. __func__);
  8493. msleep(50);
  8494. rc = -ENODEV;
  8495. if (retry--)
  8496. goto reset;
  8497. goto exit;
  8498. } else {
  8499. pt_debug(cd->dev, DL_INFO,
  8500. "%s: Exit bootloader successfully\n",
  8501. __func__);
  8502. }
  8503. if (cd->mode != PT_MODE_OPERATIONAL) {
  8504. pt_debug(cd->dev, DL_WARN,
  8505. "%s: restore mode failure mode = %d\n",
  8506. __func__, cd->mode);
  8507. if (retry--)
  8508. goto reset;
  8509. goto exit;
  8510. }
  8511. }
  8512. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8513. }
  8514. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8515. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8516. if (!rc) {
  8517. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8518. pt_debug(cd->dev, DL_WARN,
  8519. "%s: Exit FW BOOT Mode after %dms\n",
  8520. __func__, wait_time);
  8521. } else {
  8522. pt_debug(cd->dev, DL_WARN,
  8523. "%s: FW stuck in BOOT Mode after %dms\n",
  8524. __func__, wait_time);
  8525. goto exit;
  8526. }
  8527. if (!cd->sysinfo.ready) {
  8528. rc = pt_hid_output_get_sysinfo_(cd);
  8529. if (rc) {
  8530. pt_debug(cd->dev, DL_ERROR,
  8531. "%s: Error on getting sysinfo r=%d\n",
  8532. __func__, rc);
  8533. if (retry--)
  8534. goto reset;
  8535. goto exit;
  8536. }
  8537. }
  8538. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8539. rc = pt_restore_parameters_(cd);
  8540. if (rc)
  8541. pt_debug(cd->dev, DL_ERROR,
  8542. "%s: failed to restore parameters rc=%d\n",
  8543. __func__, rc);
  8544. else
  8545. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8546. exit:
  8547. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8548. return rc;
  8549. }
  8550. /*******************************************************************************
  8551. * FUNCTION: pt_core_poweron_device_
  8552. *
  8553. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8554. *
  8555. * RETURN:
  8556. * 0 = success
  8557. * !0 = failure
  8558. *
  8559. * PARAMETERS:
  8560. * *cd - pointer to core data
  8561. ******************************************************************************/
  8562. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8563. {
  8564. struct device *dev = cd->dev;
  8565. int rc = 0;
  8566. /*
  8567. * After power on action, the chip can general FW sentinel. It can
  8568. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8569. * _fast_startup() can perform a quick enumeration too, here doesn't
  8570. * need another enumeration.
  8571. */
  8572. mutex_lock(&cd->system_lock);
  8573. cd->startup_status = STARTUP_STATUS_START;
  8574. cd->hid_reset_cmd_state = 1;
  8575. mutex_unlock(&cd->system_lock);
  8576. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8577. if (rc < 0) {
  8578. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8579. __func__, rc);
  8580. goto exit;
  8581. }
  8582. if (!cd->irq_enabled) {
  8583. cd->irq_enabled = true;
  8584. enable_irq(cd->irq);
  8585. }
  8586. /* TBD: following function doesn't update startup_status */
  8587. rc = _fast_startup(cd);
  8588. exit:
  8589. return rc;
  8590. }
  8591. /*******************************************************************************
  8592. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8593. *
  8594. * SUMMARY: Reset device, and then trigger a full enumeration.
  8595. *
  8596. * RETURN:
  8597. * 0 = success
  8598. * !0 = failure
  8599. *
  8600. * PARAMETERS:
  8601. * *cd - pointer to core data
  8602. ******************************************************************************/
  8603. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8604. {
  8605. int rc;
  8606. rc = pt_dut_reset_and_wait(cd);
  8607. if (rc < 0) {
  8608. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8609. __func__, rc);
  8610. goto exit;
  8611. }
  8612. rc = _fast_startup(cd);
  8613. exit:
  8614. return rc;
  8615. }
  8616. /*******************************************************************************
  8617. * FUNCTION: pt_core_easywake_off_
  8618. *
  8619. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8620. * the configuration in the core platform data structure.
  8621. *
  8622. * RETURN:
  8623. * 0 = success
  8624. * !0 = failure
  8625. *
  8626. * PARAMETERS:
  8627. * *cd - pointer to core data
  8628. ******************************************************************************/
  8629. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8630. {
  8631. int rc = 0;
  8632. mutex_lock(&cd->system_lock);
  8633. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8634. mutex_unlock(&cd->system_lock);
  8635. pt_debug(cd->dev, DL_INFO,
  8636. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8637. return 0;
  8638. }
  8639. mutex_unlock(&cd->system_lock);
  8640. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8641. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8642. rc = pt_core_wake_device_from_easy_wake_(cd);
  8643. if (rc < 0)
  8644. pt_debug(cd->dev, DL_ERROR,
  8645. "%s - %d failed %d\n", __func__, rc);
  8646. }
  8647. mutex_lock(&cd->system_lock);
  8648. cd->sleep_state = SS_EASY_WAKING_OFF;
  8649. mutex_unlock(&cd->system_lock);
  8650. pt_start_wd_timer(cd);
  8651. return rc;
  8652. }
  8653. /*******************************************************************************
  8654. * FUNCTION: pt_core_easywake_off
  8655. *
  8656. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8657. *
  8658. * RETURN:
  8659. * 0 = success
  8660. * !0 = failure
  8661. *
  8662. * PARAMETERS:
  8663. * *cd - pointer to core data
  8664. ******************************************************************************/
  8665. static int pt_core_easywake_off(struct pt_core_data *cd)
  8666. {
  8667. int rc;
  8668. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8669. if (rc < 0) {
  8670. pt_debug(cd->dev, DL_ERROR,
  8671. "%s: fail get exclusive ex=%p own=%p\n",
  8672. __func__, cd->exclusive_dev, cd->dev);
  8673. return rc;
  8674. }
  8675. rc = pt_core_easywake_off_(cd);
  8676. if (release_exclusive(cd, cd->dev) < 0)
  8677. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8678. __func__);
  8679. else
  8680. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8681. __func__);
  8682. return rc;
  8683. }
  8684. /*******************************************************************************
  8685. * FUNCTION: pt_core_wake_
  8686. *
  8687. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8688. * the configuration in the core platform data structure.
  8689. *
  8690. * RETURN:
  8691. * 0 = success
  8692. * !0 = failure
  8693. *
  8694. * PARAMETERS:
  8695. * *cd - pointer to core data
  8696. ******************************************************************************/
  8697. static int pt_core_wake_(struct pt_core_data *cd)
  8698. {
  8699. int rc = 0;
  8700. mutex_lock(&cd->system_lock);
  8701. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8702. mutex_unlock(&cd->system_lock);
  8703. pt_debug(cd->dev, DL_INFO,
  8704. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8705. return 0;
  8706. } else {
  8707. cd->sleep_state = SS_WAKING;
  8708. }
  8709. mutex_unlock(&cd->system_lock);
  8710. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8711. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8712. pt_debug(cd->dev, DL_INFO,
  8713. "%s: Entering into poweron mode:\n", __func__);
  8714. rc = pt_core_poweron_device_(cd);
  8715. if (rc < 0)
  8716. pr_err("%s: Poweron error detected: rc=%d\n",
  8717. __func__, rc);
  8718. }
  8719. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8720. rc = pt_core_wake_device_from_deep_standby_(cd);
  8721. else /* Default action to exit DeepSleep */
  8722. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8723. }
  8724. mutex_lock(&cd->system_lock);
  8725. cd->sleep_state = SS_SLEEP_OFF;
  8726. mutex_unlock(&cd->system_lock);
  8727. pt_start_wd_timer(cd);
  8728. return rc;
  8729. }
  8730. /*******************************************************************************
  8731. * FUNCTION: pt_core_wake_
  8732. *
  8733. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8734. *
  8735. * RETURN:
  8736. * 0 = success
  8737. * !0 = failure
  8738. *
  8739. * PARAMETERS:
  8740. * *cd - pointer to core data
  8741. ******************************************************************************/
  8742. static int pt_core_wake(struct pt_core_data *cd)
  8743. {
  8744. int rc = 0;
  8745. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8746. if (rc < 0) {
  8747. pt_debug(cd->dev, DL_ERROR,
  8748. "%s: fail get exclusive ex=%p own=%p\n",
  8749. __func__, cd->exclusive_dev, cd->dev);
  8750. return rc;
  8751. }
  8752. rc = pt_core_wake_(cd);
  8753. if (release_exclusive(cd, cd->dev) < 0)
  8754. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8755. __func__);
  8756. else
  8757. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8758. __func__);
  8759. return rc;
  8760. }
  8761. /*******************************************************************************
  8762. * FUNCTION: pt_get_ic_crc_
  8763. *
  8764. * SUMMARY: This function retrieves the config block CRC
  8765. *
  8766. * NOTE: The post condition of calling this function will be that the DUT will
  8767. * be in SCANNINING mode if no failures occur
  8768. *
  8769. * RETURN:
  8770. * 0 = success
  8771. * !0 = failure
  8772. *
  8773. * PARAMETERS:
  8774. * *cd - pointer the core data structure
  8775. * ebid - enumerated block ID
  8776. ******************************************************************************/
  8777. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8778. {
  8779. struct pt_sysinfo *si = &cd->sysinfo;
  8780. int rc = 0;
  8781. u8 status;
  8782. u16 calculated_crc = 0;
  8783. u16 stored_crc = 0;
  8784. rc = pt_pip_suspend_scanning_(cd);
  8785. if (rc)
  8786. goto error;
  8787. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8788. &calculated_crc, &stored_crc);
  8789. if (rc)
  8790. goto exit;
  8791. if (status) {
  8792. rc = -EINVAL;
  8793. goto exit;
  8794. }
  8795. si->ttconfig.crc = stored_crc;
  8796. exit:
  8797. pt_pip_resume_scanning_(cd);
  8798. error:
  8799. pt_debug(cd->dev, DL_INFO,
  8800. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8801. __func__, ebid, calculated_crc, stored_crc, rc);
  8802. return rc;
  8803. }
  8804. /*******************************************************************************
  8805. * FUNCTION: pt_pip2_read_gpio
  8806. *
  8807. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8808. * bits into the passed in variable
  8809. *
  8810. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8811. *
  8812. * RETURN:
  8813. * 0 = success
  8814. * !0 = failure
  8815. *
  8816. * PARAMETERS:
  8817. * *dev - pointer to device structure
  8818. * *status - pointer to where the command response status is stored
  8819. * *gpio - pointer to device attributes structure
  8820. ******************************************************************************/
  8821. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8822. {
  8823. int rc = 0;
  8824. u16 actual_read_len;
  8825. u8 read_buf[12];
  8826. u8 tmp_status = 0;
  8827. u8 index = PIP2_RESP_STATUS_OFFSET;
  8828. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8829. rc = _pt_request_pip2_send_cmd(dev,
  8830. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8831. NULL, 0, read_buf, &actual_read_len);
  8832. if (rc) {
  8833. pt_debug(dev, DL_ERROR,
  8834. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8835. rc = -ECOMM;
  8836. } else {
  8837. tmp_status = read_buf[index];
  8838. }
  8839. if (status)
  8840. *status = tmp_status;
  8841. if (!rc && gpio && (tmp_status == 0)) {
  8842. *gpio = ((read_buf[index + 4] << 24) |
  8843. (read_buf[index + 3] << 16) |
  8844. (read_buf[index + 2] << 8) |
  8845. (read_buf[index + 1]));
  8846. }
  8847. return rc;
  8848. }
  8849. /*******************************************************************************
  8850. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8851. *
  8852. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8853. *
  8854. * RETURN:
  8855. * 0 = success
  8856. * !0 = failure
  8857. *
  8858. * PARAMETERS:
  8859. * *cd - pointer the core data structure
  8860. * *pid - pointer to store panel ID
  8861. ******************************************************************************/
  8862. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8863. {
  8864. u32 gpio_value = 0;
  8865. u8 status = 0;
  8866. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8867. int rc = 0;
  8868. if (!pid)
  8869. return -ENOMEM;
  8870. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8871. if (!rc) {
  8872. if (status == 0) {
  8873. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8874. PT_PANEL_ID_SHIFT;
  8875. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8876. __func__,
  8877. "BL mode PID", panel_id, "gpio", gpio_value);
  8878. *pid = panel_id;
  8879. } else {
  8880. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8881. __func__,
  8882. "BL read gpio failed status", status);
  8883. }
  8884. } else {
  8885. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8886. __func__,
  8887. "BL read gpio failed status", status);
  8888. }
  8889. return rc;
  8890. }
  8891. /*******************************************************************************
  8892. * FUNCTION: pt_enum_with_dut_
  8893. *
  8894. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8895. * The core data (cd) startup_status will store, as a bitmask, each
  8896. * state of the enumeration process. The startup will be attempted
  8897. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8898. *
  8899. * RETURN:
  8900. * 0 = success
  8901. * !0 = failure
  8902. *
  8903. * PARAMETERS:
  8904. * *cd - pointer the core data structure
  8905. * reset - Flag to reset the DUT before attempting to enumerate
  8906. * *status - poionter to store the enum status bitmask flags
  8907. ******************************************************************************/
  8908. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8909. u32 *enum_status)
  8910. {
  8911. int try = 1;
  8912. int rc = 0;
  8913. int wait_time = 0;
  8914. bool detected = false;
  8915. u8 return_data[8];
  8916. u8 mode = PT_MODE_UNKNOWN;
  8917. u8 pid = PANEL_ID_NOT_ENABLED;
  8918. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8919. struct pt_hid_desc hid_desc;
  8920. memset(&hid_desc, 0, sizeof(hid_desc));
  8921. #ifdef TTHE_TUNER_SUPPORT
  8922. tthe_print(cd, NULL, 0, "enter startup");
  8923. #endif
  8924. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8925. __func__, cd->startup_status, reset);
  8926. pt_stop_wd_timer(cd);
  8927. reset:
  8928. if (try > 1)
  8929. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8930. __func__, try);
  8931. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8932. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8933. pt_debug(cd->dev, DL_INFO,
  8934. "%s: PIP1 Enumeration start\n", __func__);
  8935. /* Only reset the DUT after the first try */
  8936. if (reset || (try > 1)) {
  8937. /*
  8938. * Reset hardware only for Legacy parts. Skip for TT/TC
  8939. * parts because if the FW image was loaded directly
  8940. * to SRAM issueing a reset ill wipe out what was just
  8941. * loaded.
  8942. */
  8943. rc = pt_dut_reset_and_wait(cd);
  8944. if (rc < 0) {
  8945. pt_debug(cd->dev, DL_ERROR,
  8946. "%s: Error on h/w reset r=%d\n",
  8947. __func__, rc);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. goto exit;
  8951. }
  8952. /* sleep to allow FW to be launched if available */
  8953. msleep(120);
  8954. }
  8955. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8956. if (rc < 0) {
  8957. pt_debug(cd->dev, DL_ERROR,
  8958. "%s: Error getting HID Descriptor r=%d\n",
  8959. __func__, rc);
  8960. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8961. goto reset;
  8962. rc = -EIO;
  8963. goto exit;
  8964. }
  8965. detected = true;
  8966. cd->mode = pt_get_mode(cd, &hid_desc);
  8967. /*
  8968. * Most systems do not use an XY pin as the panel_id and so
  8969. * the BL is used to retrieve the panel_id, however for
  8970. * systems that do use an XY pin, the panel_id MUST be
  8971. * retrieved from the system information when running FW
  8972. * (done below) and so this section of code is skipped.
  8973. * Entering the BL here is only needed on XY_PIN systems.
  8974. */
  8975. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8976. if (cd->mode == PT_MODE_OPERATIONAL) {
  8977. rc = pt_pip_start_bootloader_(cd);
  8978. if (rc < 0) {
  8979. pt_debug(cd->dev, DL_ERROR,
  8980. "%s: Error on start bootloader r=%d\n",
  8981. __func__, rc);
  8982. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8983. goto reset;
  8984. goto exit;
  8985. }
  8986. cd->mode = PT_MODE_BOOTLOADER;
  8987. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8988. __func__);
  8989. }
  8990. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8991. if (!rc) {
  8992. cd->bl_info.ready = true;
  8993. cd->bl_info.chip_id =
  8994. get_unaligned_le16(&return_data[2]);
  8995. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8996. __func__, cd->bl_info.chip_id);
  8997. } else {
  8998. pt_debug(cd->dev, DL_ERROR,
  8999. "%s: failed to get chip ID, r=%d\n",
  9000. __func__, rc);
  9001. }
  9002. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  9003. mutex_lock(&cd->system_lock);
  9004. if (!rc) {
  9005. cd->pid_for_loader = pid;
  9006. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  9007. __func__, cd->pid_for_loader);
  9008. } else {
  9009. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  9010. pt_debug(cd->dev, DL_WARN,
  9011. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9012. __func__, cd->pid_for_loader);
  9013. }
  9014. mutex_unlock(&cd->system_lock);
  9015. }
  9016. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9017. if (cd->mode == PT_MODE_BOOTLOADER) {
  9018. rc = pt_hid_output_bl_launch_app_(cd);
  9019. if (rc < 0) {
  9020. pt_debug(cd->dev, DL_ERROR,
  9021. "%s: Error on launch app r=%d\n",
  9022. __func__, rc);
  9023. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9024. goto reset;
  9025. rc = -ENODEV;
  9026. goto exit;
  9027. }
  9028. /* sleep to allow FW to be launched if available */
  9029. msleep(120);
  9030. /* Ensure the DUT is now in Application mode */
  9031. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9032. if (rc < 0) {
  9033. pt_debug(cd->dev, DL_ERROR,
  9034. "%s: Error getting HID Desc r=%d\n",
  9035. __func__, rc);
  9036. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9037. goto reset;
  9038. rc = -EIO;
  9039. goto exit;
  9040. }
  9041. cd->mode = pt_get_mode(cd, &hid_desc);
  9042. if (cd->mode == PT_MODE_BOOTLOADER) {
  9043. pt_debug(cd->dev, DL_WARN,
  9044. "%s: Error confiming exit BL\n",
  9045. __func__);
  9046. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9047. goto reset;
  9048. rc = -ENODEV;
  9049. goto exit;
  9050. }
  9051. }
  9052. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9053. cd->mode = PT_MODE_OPERATIONAL;
  9054. *enum_status |= STARTUP_STATUS_GET_DESC;
  9055. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9056. } else {
  9057. /* Generation is PIP2 Capable */
  9058. pt_debug(cd->dev, DL_INFO,
  9059. "%s: PIP2 Enumeration start\n", __func__);
  9060. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9061. if (rc) {
  9062. pt_debug(cd->dev, DL_ERROR,
  9063. "%s: Get mode failed, mode unknown\n",
  9064. __func__);
  9065. mode = PT_MODE_UNKNOWN;
  9066. } else
  9067. detected = true;
  9068. cd->mode = mode;
  9069. switch (cd->mode) {
  9070. case PT_MODE_OPERATIONAL:
  9071. pt_debug(cd->dev, DL_INFO,
  9072. "%s: Operational mode\n", __func__);
  9073. if (cd->app_pip_ver_ready == false) {
  9074. rc = pt_pip2_get_version_(cd);
  9075. if (!rc)
  9076. cd->app_pip_ver_ready = true;
  9077. else {
  9078. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9079. goto reset;
  9080. goto exit;
  9081. }
  9082. }
  9083. break;
  9084. case PT_MODE_BOOTLOADER:
  9085. pt_debug(cd->dev, DL_INFO,
  9086. "%s: Bootloader mode\n", __func__);
  9087. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9088. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9089. mutex_lock(&cd->system_lock);
  9090. if (!rc) {
  9091. cd->pid_for_loader = pid;
  9092. pt_debug(cd->dev, DL_INFO,
  9093. "%s: Panel ID: 0x%02X\n",
  9094. __func__, cd->pid_for_loader);
  9095. } else {
  9096. cd->pid_for_loader =
  9097. PANEL_ID_NOT_ENABLED;
  9098. pt_debug(cd->dev, DL_WARN,
  9099. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9100. __func__, cd->pid_for_loader);
  9101. }
  9102. mutex_unlock(&cd->system_lock);
  9103. }
  9104. if (cd->bl_pip_ver_ready == false) {
  9105. rc = pt_pip2_get_version_(cd);
  9106. if (!rc)
  9107. cd->bl_pip_ver_ready = true;
  9108. else {
  9109. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9110. goto reset;
  9111. goto exit;
  9112. }
  9113. }
  9114. /*
  9115. * Launch app command will fail in flashless mode.
  9116. * Skip launch app command here to save time for
  9117. * enumeration flow.
  9118. */
  9119. if (cd->flashless_dut)
  9120. goto exit;
  9121. /*
  9122. * pt_pip2_launch_app() is needed here instead of
  9123. * pt_pip2_exit_bl() because exit_bl will cause several
  9124. * hw_resets to occur and the auto BL on a flashless
  9125. * DUT will fail.
  9126. */
  9127. rc = pt_pip2_launch_app(cd->dev,
  9128. PT_CORE_CMD_UNPROTECTED);
  9129. if (rc) {
  9130. pt_debug(cd->dev, DL_ERROR,
  9131. "%s: Error on launch app r=%d\n",
  9132. __func__, rc);
  9133. msleep(50);
  9134. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9135. goto reset;
  9136. goto exit;
  9137. }
  9138. /*
  9139. * IRQ thread can be delayed if the serial log print is
  9140. * enabled. It causes next command to get wrong response
  9141. * Here the delay is to ensure pt_parse_input() to be
  9142. * finished.
  9143. */
  9144. msleep(60);
  9145. /* Check and update the mode */
  9146. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9147. if (rc) {
  9148. pt_debug(cd->dev, DL_ERROR,
  9149. "%s: Get mode failed, mode unknown\n",
  9150. __func__);
  9151. mode = PT_MODE_UNKNOWN;
  9152. }
  9153. cd->mode = mode;
  9154. if (cd->mode == PT_MODE_OPERATIONAL) {
  9155. pt_debug(cd->dev, DL_INFO,
  9156. "%s: Launched to Operational mode\n",
  9157. __func__);
  9158. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9159. pt_debug(cd->dev, DL_ERROR,
  9160. "%s: Launch failed, Bootloader mode\n",
  9161. __func__);
  9162. goto exit;
  9163. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9164. pt_debug(cd->dev, DL_ERROR,
  9165. "%s: Launch failed, Unknown mode\n",
  9166. __func__);
  9167. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9168. goto reset;
  9169. goto exit;
  9170. }
  9171. if (cd->app_pip_ver_ready == false) {
  9172. rc = pt_pip2_get_version_(cd);
  9173. if (!rc)
  9174. cd->app_pip_ver_ready = true;
  9175. else {
  9176. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9177. goto reset;
  9178. goto exit;
  9179. }
  9180. }
  9181. break;
  9182. default:
  9183. pt_debug(cd->dev, DL_ERROR,
  9184. "%s: Unknown mode\n", __func__);
  9185. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9186. goto reset;
  9187. goto exit;
  9188. }
  9189. *enum_status |= STARTUP_STATUS_GET_DESC;
  9190. }
  9191. pt_init_pip_report_fields(cd);
  9192. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9193. if (!cd->features.easywake)
  9194. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9195. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9196. rc = pt_hid_output_get_sysinfo_(cd);
  9197. if (rc) {
  9198. pt_debug(cd->dev, DL_ERROR,
  9199. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9200. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9201. goto reset;
  9202. goto exit;
  9203. }
  9204. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9205. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9206. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9207. if (!rc) {
  9208. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9209. pt_debug(cd->dev, DL_WARN,
  9210. "%s: Exit FW BOOT Mode after %dms\n",
  9211. __func__, wait_time);
  9212. } else {
  9213. pt_debug(cd->dev, DL_WARN,
  9214. "%s: FW stuck in BOOT Mode after %dms\n",
  9215. __func__, wait_time);
  9216. goto exit;
  9217. }
  9218. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9219. __func__,
  9220. cd->sysinfo.ttdata.pip_ver_major,
  9221. cd->sysinfo.ttdata.pip_ver_minor);
  9222. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9223. if (rc) {
  9224. pt_debug(cd->dev, DL_ERROR,
  9225. "%s: DUT Config block CRC failure rc=%d\n",
  9226. __func__, rc);
  9227. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9228. goto reset;
  9229. } else {
  9230. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9231. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9232. pt_debug(cd->dev, DL_ERROR,
  9233. "%s: scan state: %d, retry: %d\n",
  9234. __func__, sys_mode, try);
  9235. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9236. goto reset;
  9237. } else
  9238. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9239. }
  9240. rc = pt_restore_parameters_(cd);
  9241. if (rc) {
  9242. pt_debug(cd->dev, DL_ERROR,
  9243. "%s: Failed to restore parameters rc=%d\n",
  9244. __func__, rc);
  9245. } else
  9246. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9247. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9248. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9249. cd->startup_retry_count = 0;
  9250. exit:
  9251. /* Generate the HW Version of the connected DUT and store in cd */
  9252. pt_generate_hw_version(cd, cd->hw_version);
  9253. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9254. cd->hw_version);
  9255. pt_start_wd_timer(cd);
  9256. if (!detected)
  9257. rc = -ENODEV;
  9258. #ifdef TTHE_TUNER_SUPPORT
  9259. tthe_print(cd, NULL, 0, "exit startup");
  9260. #endif
  9261. pt_debug(cd->dev, DL_WARN,
  9262. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9263. __func__, rc, try);
  9264. return rc;
  9265. }
  9266. /*******************************************************************************
  9267. * FUNCTION: pt_enum_with_dut
  9268. *
  9269. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9270. * requesting exclusive access around the call.
  9271. *
  9272. * RETURN:
  9273. * 0 = success
  9274. * !0 = failure
  9275. *
  9276. * PARAMETERS:
  9277. * *cd - pointer the core data structure
  9278. * reset - Flag to reset the DUT before attempting to enumerate
  9279. * *status - pointer to store the enum status bitmask flags
  9280. ******************************************************************************/
  9281. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9282. {
  9283. int rc = 0;
  9284. mutex_lock(&cd->system_lock);
  9285. cd->startup_state = STARTUP_RUNNING;
  9286. mutex_unlock(&cd->system_lock);
  9287. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9288. if (rc) {
  9289. pt_debug(cd->dev, DL_ERROR,
  9290. "%s: fail get exclusive ex=%p own=%p\n",
  9291. __func__, cd->exclusive_dev, cd->dev);
  9292. goto exit;
  9293. }
  9294. rc = pt_enum_with_dut_(cd, reset, status);
  9295. if (release_exclusive(cd, cd->dev) < 0)
  9296. /* Don't return fail code, mode is already changed. */
  9297. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9298. __func__);
  9299. else
  9300. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9301. __func__);
  9302. exit:
  9303. mutex_lock(&cd->system_lock);
  9304. /* Clear startup state for any tasks waiting for startup completion */
  9305. cd->startup_state = STARTUP_NONE;
  9306. mutex_unlock(&cd->system_lock);
  9307. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9308. *status |= STARTUP_STATUS_COMPLETE;
  9309. /* Wake the waiters for end of startup */
  9310. wake_up(&cd->wait_q);
  9311. return rc;
  9312. }
  9313. static int add_sysfs_interfaces(struct device *dev);
  9314. static void remove_sysfs_interfaces(struct device *dev);
  9315. static void remove_sysfs_and_modules(struct device *dev);
  9316. static void pt_release_modules(struct pt_core_data *cd);
  9317. static void pt_probe_modules(struct pt_core_data *cd);
  9318. /*******************************************************************************
  9319. * FUNCTION: _pt_ttdl_restart
  9320. *
  9321. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9322. *
  9323. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9324. * existing sysfs nodes will produce an error.
  9325. *
  9326. * RETURN:
  9327. * 0 = success
  9328. * !0 = failure
  9329. *
  9330. * PARAMETERS:
  9331. * *dev - pointer to core device
  9332. ******************************************************************************/
  9333. static int _pt_ttdl_restart(struct device *dev)
  9334. {
  9335. int rc = 0;
  9336. struct pt_core_data *cd = dev_get_drvdata(dev);
  9337. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9338. struct i2c_client *client =
  9339. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9340. #endif
  9341. /*
  9342. * Make sure the device is awake, pt_mt_release function will
  9343. * cause pm sleep function and lead to deadlock.
  9344. */
  9345. pm_runtime_get_sync(dev);
  9346. /* Use ttdl_restart_lock to avoid reentry */
  9347. mutex_lock(&cd->ttdl_restart_lock);
  9348. remove_sysfs_and_modules(cd->dev);
  9349. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9350. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9351. pt_debug(dev, DL_ERROR,
  9352. "%s I2C functionality not Supported\n", __func__);
  9353. rc = -EIO;
  9354. goto ttdl_no_error;
  9355. }
  9356. #endif
  9357. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9358. rc = _pt_detect_dut_generation(cd->dev,
  9359. &cd->startup_status, &cd->active_dut_generation,
  9360. &cd->mode);
  9361. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9362. pt_debug(dev, DL_ERROR,
  9363. "%s: Error, Unknown DUT Generation rc=%d\n",
  9364. __func__, rc);
  9365. }
  9366. }
  9367. rc = add_sysfs_interfaces(cd->dev);
  9368. if (rc < 0)
  9369. pt_debug(cd->dev, DL_ERROR,
  9370. "%s: Error, failed sysfs nodes rc=%d\n",
  9371. __func__, rc);
  9372. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9373. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9374. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9375. if (rc < 0)
  9376. pt_debug(dev, DL_ERROR,
  9377. "%s: Error, Failed to Enumerate\n", __func__);
  9378. }
  9379. rc = pt_mt_probe(dev);
  9380. if (rc < 0) {
  9381. pt_debug(dev, DL_ERROR,
  9382. "%s: Error, fail mt probe\n", __func__);
  9383. }
  9384. rc = pt_btn_probe(dev);
  9385. if (rc < 0) {
  9386. pt_debug(dev, DL_ERROR,
  9387. "%s: Error, fail btn probe\n", __func__);
  9388. }
  9389. pt_probe_modules(cd);
  9390. pt_debug(cd->dev, DL_WARN,
  9391. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9392. rc = 0;
  9393. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9394. ttdl_no_error:
  9395. #endif
  9396. mutex_unlock(&cd->ttdl_restart_lock);
  9397. mutex_lock(&cd->system_lock);
  9398. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9399. cd->startup_state = STARTUP_NONE;
  9400. mutex_unlock(&cd->system_lock);
  9401. pm_runtime_put(dev);
  9402. return rc;
  9403. }
  9404. /*******************************************************************************
  9405. * FUNCTION: pt_restart_work_function
  9406. *
  9407. * SUMMARY: This is the wrapper function placed in a work queue to call
  9408. * _pt_ttdl_restart()
  9409. *
  9410. * RETURN: none
  9411. *
  9412. * PARAMETERS:
  9413. * *work - pointer to the work_struct
  9414. ******************************************************************************/
  9415. static void pt_restart_work_function(struct work_struct *work)
  9416. {
  9417. struct pt_core_data *cd = container_of(work,
  9418. struct pt_core_data, ttdl_restart_work);
  9419. int rc = 0;
  9420. rc = _pt_ttdl_restart(cd->dev);
  9421. if (rc < 0)
  9422. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9423. __func__, rc);
  9424. }
  9425. /*******************************************************************************
  9426. * FUNCTION: pt_enum_work_function
  9427. *
  9428. * SUMMARY: This is the wrapper function placed in a work queue to call
  9429. * pt_enum_with_dut()
  9430. *
  9431. * RETURN: none
  9432. *
  9433. * PARAMETERS:
  9434. * *work - pointer to the work_struct
  9435. ******************************************************************************/
  9436. static void pt_enum_work_function(struct work_struct *work)
  9437. {
  9438. struct pt_core_data *cd = container_of(work,
  9439. struct pt_core_data, enum_work);
  9440. int rc;
  9441. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9442. if (rc < 0)
  9443. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9444. __func__, rc);
  9445. }
  9446. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9447. {
  9448. int rc;
  9449. if (!get) {
  9450. rc = 0;
  9451. goto regulator_put;
  9452. }
  9453. cd->vdd = regulator_get(cd->dev, "vdd");
  9454. if (IS_ERR(cd->vdd)) {
  9455. rc = PTR_ERR(cd->vdd);
  9456. dev_err(cd->dev,
  9457. "Regulator get failed vdd rc=%d\n", rc);
  9458. goto regulator_put;
  9459. }
  9460. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9461. if (IS_ERR(cd->vcc_i2c)) {
  9462. rc = PTR_ERR(cd->vcc_i2c);
  9463. dev_err(cd->dev,
  9464. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9465. goto regulator_put;
  9466. }
  9467. return 0;
  9468. regulator_put:
  9469. if (cd->vdd) {
  9470. regulator_put(cd->vdd);
  9471. cd->vdd = NULL;
  9472. }
  9473. if (cd->vcc_i2c) {
  9474. regulator_put(cd->vcc_i2c);
  9475. cd->vcc_i2c = NULL;
  9476. }
  9477. return rc;
  9478. }
  9479. #ifdef ENABLE_I2C_REG_ONLY
  9480. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9481. {
  9482. int rc = 0;
  9483. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9484. if (!en) {
  9485. rc = 0;
  9486. goto disable_vcc_i2c_reg_only;
  9487. }
  9488. if (cd->vcc_i2c) {
  9489. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9490. if (rc < 0)
  9491. pt_debug(cd->dev, DL_INFO,
  9492. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9493. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9494. __func__, rc);
  9495. }
  9496. return 0;
  9497. disable_vcc_i2c_reg_only:
  9498. if (cd->vcc_i2c) {
  9499. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9500. if (rc < 0)
  9501. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9502. __func__, rc);
  9503. }
  9504. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9505. return rc;
  9506. }
  9507. #endif
  9508. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9509. {
  9510. int rc;
  9511. if (!en) {
  9512. rc = 0;
  9513. goto disable_vcc_i2c_reg;
  9514. }
  9515. if (cd->vdd) {
  9516. if (regulator_count_voltages(cd->vdd) > 0) {
  9517. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9518. FT_VTG_MAX_UV);
  9519. if (rc) {
  9520. dev_err(cd->dev,
  9521. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9522. goto exit;
  9523. }
  9524. }
  9525. rc = regulator_enable(cd->vdd);
  9526. if (rc) {
  9527. dev_err(cd->dev,
  9528. "Regulator vdd enable failed rc=%d\n", rc);
  9529. goto exit;
  9530. }
  9531. dev_info(cd->dev, "%s: VDD regulator enabled:\n", __func__);
  9532. }
  9533. if (cd->vcc_i2c) {
  9534. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9535. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9536. FT_I2C_VTG_MAX_UV);
  9537. if (rc) {
  9538. dev_err(cd->dev,
  9539. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9540. goto disable_vdd_reg;
  9541. }
  9542. }
  9543. rc = regulator_enable(cd->vcc_i2c);
  9544. if (rc) {
  9545. dev_err(cd->dev,
  9546. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9547. goto disable_vdd_reg;
  9548. }
  9549. dev_info(cd->dev, "%s: VCC I2C regulator enabled:\n", __func__);
  9550. }
  9551. return 0;
  9552. disable_vcc_i2c_reg:
  9553. if (cd->vcc_i2c) {
  9554. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9555. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9556. FT_I2C_VTG_MAX_UV);
  9557. regulator_disable(cd->vcc_i2c);
  9558. dev_info(cd->dev, "%s: VCC I2C regulator disabled:\n", __func__);
  9559. }
  9560. disable_vdd_reg:
  9561. if (cd->vdd) {
  9562. if (regulator_count_voltages(cd->vdd) > 0)
  9563. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9564. FT_VTG_MAX_UV);
  9565. regulator_disable(cd->vdd);
  9566. dev_info(cd->dev, "%s: VDD regulator disabled:\n", __func__);
  9567. }
  9568. exit:
  9569. return rc;
  9570. }
  9571. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9572. #define KERNEL_VER_GT_3_19
  9573. #endif
  9574. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9575. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9576. #if defined(CONFIG_PM_SLEEP)
  9577. /*******************************************************************************
  9578. * FUNCTION: pt_core_rt_suspend
  9579. *
  9580. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9581. *
  9582. * RETURN:
  9583. * 0 = success
  9584. * !0 = failure
  9585. *
  9586. * PARAMETERS:
  9587. * *dev - pointer to core device
  9588. ******************************************************************************/
  9589. static int pt_core_rt_suspend(struct device *dev)
  9590. {
  9591. struct pt_core_data *cd = dev_get_drvdata(dev);
  9592. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9593. __func__, cd->core_probe_complete);
  9594. return 0;
  9595. }
  9596. /*******************************************************************************
  9597. * FUNCTION: pt_core_rt_resume
  9598. *
  9599. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9600. *
  9601. * RETURN:
  9602. * 0 = success
  9603. * !0 = failure
  9604. *
  9605. * PARAMETERS:
  9606. * *dev - pointer to core device
  9607. ******************************************************************************/
  9608. static int pt_core_rt_resume(struct device *dev)
  9609. {
  9610. struct pt_core_data *cd = dev_get_drvdata(dev);
  9611. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9612. __func__, cd->core_probe_complete);
  9613. return 0;
  9614. }
  9615. #endif /* CONFIG_PM_SLEEP */
  9616. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9617. #if defined(CONFIG_PM_SLEEP)
  9618. /*******************************************************************************
  9619. * FUNCTION: pt_core_suspend_
  9620. *
  9621. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9622. * pt_core_sleep. This function may disable IRQ during sleep state.
  9623. *
  9624. * RETURN:
  9625. * 0 = success
  9626. * !0 = failure
  9627. *
  9628. * PARAMETERS:
  9629. * *dev - pointer to core device
  9630. ******************************************************************************/
  9631. static int pt_core_suspend_(struct device *dev)
  9632. {
  9633. int rc;
  9634. struct pt_core_data *cd = dev_get_drvdata(dev);
  9635. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9636. rc = pt_core_sleep(cd);
  9637. if (rc) {
  9638. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9639. __func__, rc);
  9640. return -EAGAIN;
  9641. }
  9642. rc = pt_enable_regulator(cd, false);
  9643. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9644. return 0;
  9645. /* Required to prevent interrupts before bus awake */
  9646. disable_irq(cd->irq);
  9647. cd->irq_disabled = 1;
  9648. if (device_may_wakeup(dev)) {
  9649. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9650. __func__);
  9651. if (!enable_irq_wake(cd->irq))
  9652. cd->irq_wake = 1;
  9653. } else {
  9654. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9655. __func__);
  9656. }
  9657. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9658. return rc;
  9659. }
  9660. /*******************************************************************************
  9661. * FUNCTION: pt_core_suspend
  9662. *
  9663. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9664. * woke up or put to sleep based on Kernel power state even when the display
  9665. * is off based on the check of TTDL core platform flag.
  9666. *
  9667. * RETURN:
  9668. * 0 = success
  9669. * !0 = failure
  9670. *
  9671. * PARAMETERS:
  9672. * *dev - pointer to core device
  9673. ******************************************************************************/
  9674. static int pt_core_suspend(struct device *dev)
  9675. {
  9676. struct pt_core_data *cd = dev_get_drvdata(dev);
  9677. int rc = 0;
  9678. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9679. return 0;
  9680. if (pt_core_state == STATE_SUSPEND)
  9681. {
  9682. pt_debug(cd->dev, DL_INFO, "%s Already in Suspend state\n", __func__);
  9683. return 0;
  9684. }
  9685. pt_debug(cd->dev, DL_INFO, "%s Suspend start\n", __func__);
  9686. cancel_work_sync(&cd->resume_work);
  9687. cancel_work_sync(&cd->suspend_work);
  9688. mutex_lock(&cd->system_lock);
  9689. cd->wait_until_wake = 0;
  9690. mutex_unlock(&cd->system_lock);
  9691. if (pm_suspend_via_firmware() || cd->touch_offload) {
  9692. rc = pt_core_suspend_(cd->dev);
  9693. cd->quick_boot = true;
  9694. } else {
  9695. rc = pt_enable_i2c_regulator(cd, false);
  9696. if (rc < 0)
  9697. pt_debug(cd->dev, DL_ERROR,
  9698. "%s: Error on disabling i2c regulator\n", __func__);
  9699. }
  9700. pt_debug(cd->dev, DL_INFO, "%s Suspend exit - rc = %d\n", __func__, rc);
  9701. return rc;
  9702. }
  9703. /*******************************************************************************
  9704. * FUNCTION: pt_core_resume_
  9705. *
  9706. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9707. * pt_core_wake. This function may enable IRQ before wake up.
  9708. *
  9709. * RETURN:
  9710. * 0 = success
  9711. * !0 = failure
  9712. *
  9713. * PARAMETERS:
  9714. * *dev - pointer to core device
  9715. ******************************************************************************/
  9716. static int pt_core_resume_(struct device *dev)
  9717. {
  9718. int rc = 0;
  9719. struct pt_core_data *cd = dev_get_drvdata(dev);
  9720. dev_info(dev, "%s: Entering into resume mode:\n",
  9721. __func__);
  9722. rc = pt_enable_regulator(cd, true);
  9723. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9724. __func__, rc);
  9725. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9726. goto exit;
  9727. /*
  9728. * Bus pm does not call suspend if device runtime suspended
  9729. * This flag is covers that case
  9730. */
  9731. if (cd->irq_disabled) {
  9732. enable_irq(cd->irq);
  9733. cd->irq_disabled = 0;
  9734. }
  9735. if (device_may_wakeup(dev)) {
  9736. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9737. __func__);
  9738. if (cd->irq_wake) {
  9739. disable_irq_wake(cd->irq);
  9740. cd->irq_wake = 0;
  9741. }
  9742. } else {
  9743. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9744. __func__);
  9745. }
  9746. exit:
  9747. rc = pt_core_wake(cd);
  9748. if (rc) {
  9749. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9750. __func__, rc);
  9751. return -EAGAIN;
  9752. }
  9753. return rc;
  9754. }
  9755. /*******************************************************************************
  9756. * FUNCTION: pt_core_restore
  9757. *
  9758. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9759. * pt_core_wake. This function may enable IRQ before wake up.
  9760. *
  9761. * RETURN:
  9762. * 0 = success
  9763. * !0 = failure
  9764. *
  9765. * PARAMETERS:
  9766. * *dev - pointer to core device
  9767. ******************************************************************************/
  9768. static int pt_core_restore(struct device *dev)
  9769. {
  9770. int rc = 0;
  9771. struct pt_core_data *cd = dev_get_drvdata(dev);
  9772. dev_info(dev, "%s: Entering into resume mode:\n",
  9773. __func__);
  9774. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9775. return rc;
  9776. }
  9777. /*******************************************************************************
  9778. * FUNCTION: suspend_offload_work
  9779. *
  9780. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9781. * along with kernel power state even the display is off based on the check of
  9782. * TTDL core platform flag.
  9783. *
  9784. * RETURN:
  9785. * 0 = success
  9786. * !0 = failure
  9787. *
  9788. * PARAMETERS:
  9789. * *dev - pointer to core device
  9790. ******************************************************************************/
  9791. static void pt_suspend_offload_work(struct work_struct *work)
  9792. {
  9793. int rc = 0;
  9794. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9795. suspend_offload_work);
  9796. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9797. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9798. return;
  9799. rc = pt_core_suspend_(cd->dev);
  9800. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9801. }
  9802. /*******************************************************************************
  9803. * FUNCTION: resume_offload_work
  9804. *
  9805. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9806. * along with kernel power state even the display is off based on the check of
  9807. * TTDL core platform flag.
  9808. *
  9809. * RETURN:
  9810. * 0 = success
  9811. * !0 = failure
  9812. *
  9813. * PARAMETERS:
  9814. * *dev - pointer to core device
  9815. ******************************************************************************/
  9816. static void pt_resume_offload_work(struct work_struct *work)
  9817. {
  9818. int rc = 0;
  9819. int retry_count = 1000;
  9820. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9821. resume_offload_work);
  9822. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9823. do {
  9824. retry_count--;
  9825. rc = pt_core_resume_(pt_data->dev);
  9826. if (rc < 0)
  9827. pt_debug(pt_data->dev, DL_ERROR,
  9828. "%s: Error on wake\n", __func__);
  9829. } while (retry_count && rc < 0);
  9830. if (rc < 0){
  9831. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9832. return;
  9833. }
  9834. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9835. rc = pt_core_easywake_on(pt_data);
  9836. if (rc < 0) {
  9837. pt_debug(pt_data->dev, DL_ERROR,
  9838. "%s: Error on enable touch to wake key\n",
  9839. __func__);
  9840. return;
  9841. }
  9842. pt_data->fb_state = FB_OFF;
  9843. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9844. #endif
  9845. pt_data->quick_boot = false;
  9846. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9847. }
  9848. /*******************************************************************************
  9849. * FUNCTION: pt_core_resume
  9850. *
  9851. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9852. * along with kernel power state even the display is off based on the check of
  9853. * TTDL core platform flag.
  9854. *
  9855. * RETURN:
  9856. * 0 = success
  9857. * !0 = failure
  9858. *
  9859. * PARAMETERS:
  9860. * *dev - pointer to core device
  9861. ******************************************************************************/
  9862. static int pt_core_resume(struct device *dev)
  9863. {
  9864. struct pt_core_data *cd = dev_get_drvdata(dev);
  9865. int rc = 0;
  9866. if (cd->drv_debug_suspend || (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP))
  9867. return 0;
  9868. if (pm_suspend_via_firmware() || cd->touch_offload) {
  9869. rc = pt_core_restore(cd->dev);
  9870. } else {
  9871. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9872. rc = pt_enable_i2c_regulator(cd, true);
  9873. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9874. }
  9875. mutex_lock(&cd->system_lock);
  9876. cd->wait_until_wake = 1;
  9877. mutex_unlock(&cd->system_lock);
  9878. wake_up(&cd->wait_q);
  9879. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9880. return rc;
  9881. }
  9882. #endif
  9883. #ifdef NEED_SUSPEND_NOTIFIER
  9884. /*******************************************************************************
  9885. * FUNCTION: pt_pm_notifier
  9886. *
  9887. * SUMMARY: This function is registered to notifier chain and will perform
  9888. * suspend operation if match event PM_SUSPEND_PREPARE.
  9889. *
  9890. * RETURN:
  9891. * 0 = success
  9892. * !0 = failure
  9893. *
  9894. * PARAMETERS:
  9895. * *nb - pointer to notifier_block structure
  9896. * action - notifier event type
  9897. * *data - void pointer
  9898. ******************************************************************************/
  9899. static int pt_pm_notifier(struct notifier_block *nb,
  9900. unsigned long action, void *data)
  9901. {
  9902. struct pt_core_data *cd = container_of(nb,
  9903. struct pt_core_data, pm_notifier);
  9904. if (action == PM_SUSPEND_PREPARE) {
  9905. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9906. __func__);
  9907. /*
  9908. * If PM runtime is not suspended, either call runtime
  9909. * PM suspend callback or wait until it finishes
  9910. */
  9911. if (!pm_runtime_suspended(cd->dev))
  9912. pm_runtime_suspend(cd->dev);
  9913. (void) pt_core_suspend(cd->dev);
  9914. }
  9915. return NOTIFY_DONE;
  9916. }
  9917. #endif
  9918. const struct dev_pm_ops pt_pm_ops = {
  9919. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9920. .freeze = pt_core_suspend,
  9921. .restore = pt_core_restore,
  9922. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9923. NULL)
  9924. };
  9925. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9926. /*******************************************************************************
  9927. * FUNCTION: _pt_request_pip2_enter_bl
  9928. *
  9929. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9930. * XRES pin or a soft reset.
  9931. *
  9932. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9933. * the WD active could cause this function to fail!
  9934. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9935. * will not try to determine the current mode but will proceed with
  9936. * resetting the DUT and entering the BL.
  9937. *
  9938. * NOTE: The definition of result code:
  9939. * PT_ENTER_BL_PASS (0)
  9940. * PT_ENTER_BL_ERROR (1)
  9941. * PT_ENTER_BL_RESET_FAIL (2)
  9942. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9943. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9944. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9945. *
  9946. * RETURNS:
  9947. * 0 = success
  9948. * !0 = failure
  9949. *
  9950. *
  9951. * PARAMETERS:
  9952. * *dev - pointer to device structure
  9953. * *start_mode - pointer to the mode the DUT was in when this function
  9954. * starts
  9955. * *result - pointer to store the result when to enter BL
  9956. ******************************************************************************/
  9957. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9958. {
  9959. int rc = 0;
  9960. int t;
  9961. int tmp_result = PT_ENTER_BL_ERROR;
  9962. int flash_info_retry = 2;
  9963. u8 mode = PT_MODE_UNKNOWN;
  9964. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9965. u8 read_buf[32];
  9966. u16 actual_read_len;
  9967. struct pt_core_data *cd = dev_get_drvdata(dev);
  9968. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9969. u8 time = 0;
  9970. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9971. if (cd->watchdog_enabled) {
  9972. pt_debug(dev, DL_WARN,
  9973. "%s: Watchdog must be stopped before entering BL\n",
  9974. __func__);
  9975. goto exit;
  9976. }
  9977. cancel_work_sync(&cd->enum_work);
  9978. cancel_work_sync(&cd->watchdog_work);
  9979. /* if undefined assume operational/test to bypass all checks */
  9980. if (*start_mode == PT_MODE_IGNORE) {
  9981. mode = PT_MODE_OPERATIONAL;
  9982. sys_mode = FW_SYS_MODE_TEST;
  9983. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9984. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9985. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9986. if (rc) {
  9987. pt_debug(dev, DL_ERROR,
  9988. "%s: Get mode failed, mode unknown\n",
  9989. __func__);
  9990. }
  9991. *start_mode = mode;
  9992. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9993. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9994. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9995. sys_mode = FW_SYS_MODE_SCANNING;
  9996. }
  9997. _retry:
  9998. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9999. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  10000. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  10001. switch (mode) {
  10002. case PT_MODE_UNKNOWN:
  10003. /*
  10004. * When the mode could not be determined the DUT could be
  10005. * in App mode running corrupted FW or FW that is not
  10006. * responding to the mode request, assume no communication
  10007. * and do a hard reset
  10008. */
  10009. mutex_lock(&cd->system_lock);
  10010. cd->startup_status = STARTUP_STATUS_START;
  10011. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10012. mutex_unlock(&cd->system_lock);
  10013. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10014. if (rc) {
  10015. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10016. goto exit;
  10017. }
  10018. break;
  10019. case PT_MODE_OPERATIONAL:
  10020. if (sys_mode == FW_SYS_MODE_SCANNING) {
  10021. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  10022. __func__);
  10023. rc = pt_pip_suspend_scanning_(cd);
  10024. if (rc) {
  10025. /*
  10026. * Print to log but don't exit, the FW could be
  10027. * running but be hung or fail to respond to
  10028. * this request
  10029. */
  10030. pt_debug(dev, DL_ERROR,
  10031. "%s Suspend Scan Failed\n", __func__);
  10032. }
  10033. /* sleep to allow the suspend scan to be processed */
  10034. usleep_range(1000, 2000);
  10035. }
  10036. mutex_lock(&cd->system_lock);
  10037. cd->startup_status = STARTUP_STATUS_START;
  10038. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10039. mutex_unlock(&cd->system_lock);
  10040. /* Reset device to enter the BL */
  10041. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10042. if (rc) {
  10043. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10044. goto exit;
  10045. }
  10046. break;
  10047. case PT_MODE_BOOTLOADER:
  10048. /* Do nothing as we are already in the BL */
  10049. tmp_result = PT_ENTER_BL_PASS;
  10050. goto exit;
  10051. default:
  10052. /* Should NEVER get here */
  10053. tmp_result = PT_ENTER_BL_ERROR;
  10054. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10055. goto exit;
  10056. }
  10057. if (!cd->flashless_dut &&
  10058. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10059. /*
  10060. * Sending the special "Host Mode" command will instruct the
  10061. * BL to not execute the FW it has loaded into RAM.
  10062. * The command must be sent within a 40ms window from releasing
  10063. * the XRES pin. If the messages is sent too early it will NAK,
  10064. * so keep sending it every 2ms until it is accepted by the BL.
  10065. * A no-flash DUT does not require this command as there is no
  10066. * FW for the BL to load and execute.
  10067. */
  10068. usleep_range(4000, 6000);
  10069. pt_debug(cd->dev, DL_INFO,
  10070. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10071. __func__, (int)sizeof(host_mode_cmd));
  10072. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10073. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10074. rc = 1;
  10075. while (rc && time < 34) {
  10076. rc = pt_adap_write_read_specific(cd, 4,
  10077. host_mode_cmd, NULL);
  10078. usleep_range(1800, 2000);
  10079. time += 2;
  10080. }
  10081. /* Sleep to allow the BL to come up */
  10082. usleep_range(1000, 2000);
  10083. }
  10084. /*
  10085. * To avoid the case that next PIP command can be confused by BL/FW
  10086. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10087. * followed by "wakeup event" function can lower the failure rate.
  10088. */
  10089. t = wait_event_timeout(cd->wait_q,
  10090. ((cd->startup_status != STARTUP_STATUS_START)
  10091. && (cd->hid_reset_cmd_state == 0)),
  10092. msecs_to_jiffies(300));
  10093. if (IS_TMO(t)) {
  10094. pt_debug(cd->dev, DL_ERROR,
  10095. "%s: TMO waiting for BL sentinel\n", __func__);
  10096. }
  10097. /* Check if device is now in BL mode */
  10098. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10099. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10100. cd->startup_status);
  10101. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10102. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10103. mutex_lock(&cd->system_lock);
  10104. cd->pip2_prot_active = true;
  10105. cd->mode = PT_MODE_BOOTLOADER;
  10106. mutex_unlock(&cd->system_lock);
  10107. tmp_result = PT_ENTER_BL_PASS;
  10108. } else {
  10109. /*
  10110. * If the device doesn't enter BL mode as expected and rc is
  10111. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10112. * the function should return an error code to indicate this
  10113. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10114. */
  10115. if (!rc)
  10116. rc = -EINVAL;
  10117. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10118. mutex_lock(&cd->system_lock);
  10119. cd->mode = mode;
  10120. mutex_unlock(&cd->system_lock);
  10121. pt_debug(dev, DL_ERROR,
  10122. "%s ERROR: Not in BL as expected", __func__);
  10123. }
  10124. exit:
  10125. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10126. /* Check to get (buffered) flash information */
  10127. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10128. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10129. read_buf, &actual_read_len);
  10130. if (!rc) {
  10131. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10132. pt_debug(
  10133. dev, DL_WARN,
  10134. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10135. __func__,
  10136. read_buf[PIP2_RESP_BODY_OFFSET]);
  10137. /*
  10138. * If the BL was unable to cache the correct
  10139. * values when entering the first time due to
  10140. * the Flash part not having been powered up
  10141. * long enough, re-enter the BL to trigger the
  10142. * BL to re-attempt to cache the values.
  10143. */
  10144. if (flash_info_retry-- > 0) {
  10145. mode = PT_MODE_UNKNOWN;
  10146. pt_debug(dev, DL_WARN,
  10147. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10148. __func__, flash_info_retry);
  10149. goto _retry;
  10150. } else {
  10151. pt_debug(dev, DL_WARN,
  10152. "%s Manufacturer ID Unknown\n",
  10153. __func__);
  10154. tmp_result = PT_ENTER_BL_PASS;
  10155. }
  10156. }
  10157. } else {
  10158. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10159. pt_debug(
  10160. dev, DL_ERROR,
  10161. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10162. __func__);
  10163. }
  10164. }
  10165. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10166. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10167. "SUPPRESS");
  10168. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10169. if (result)
  10170. *result = tmp_result;
  10171. return rc;
  10172. }
  10173. /*******************************************************************************
  10174. * FUNCTION: _pt_pip2_file_open
  10175. *
  10176. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10177. *
  10178. * NOTE: The DUT must be in BL mode for this command to work
  10179. *
  10180. * RETURNS:
  10181. * <0 = Error
  10182. * >0 = file handle opened
  10183. *
  10184. * PARAMETERS:
  10185. * *dev - pointer to device structure
  10186. * file_no - PIP2 file number to open
  10187. ******************************************************************************/
  10188. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10189. {
  10190. int ret = 0;
  10191. u16 status;
  10192. u16 actual_read_len;
  10193. u8 file_handle;
  10194. u8 data[2];
  10195. u8 read_buf[10];
  10196. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10197. data[0] = file_no;
  10198. ret = _pt_request_pip2_send_cmd(dev,
  10199. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10200. data, 1, read_buf, &actual_read_len);
  10201. if (ret) {
  10202. pt_debug(dev, DL_ERROR,
  10203. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10204. __func__, file_no);
  10205. return -PIP2_RSP_ERR_NOT_OPEN;
  10206. }
  10207. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10208. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10209. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10210. (file_handle != file_no)) {
  10211. pt_debug(dev, DL_ERROR,
  10212. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10213. __func__, status, file_handle);
  10214. return -status;
  10215. }
  10216. return file_handle;
  10217. }
  10218. /*******************************************************************************
  10219. * FUNCTION: _pt_pip2_file_close
  10220. *
  10221. * SUMMARY: Using the BL PIP2 commands close a file
  10222. *
  10223. * NOTE: The DUT must be in BL mode for this command to work
  10224. *
  10225. * RETURNS:
  10226. * <0 = Error
  10227. * >0 = file handle closed
  10228. *
  10229. * PARAMETERS:
  10230. * *dev - pointer to device structure
  10231. * file_handle - handle to the file to be closed
  10232. ******************************************************************************/
  10233. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10234. {
  10235. int ret = 0;
  10236. u16 status;
  10237. u16 actual_read_len;
  10238. u8 data[2];
  10239. u8 read_buf[32];
  10240. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10241. data[0] = file_handle;
  10242. ret = _pt_request_pip2_send_cmd(dev,
  10243. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10244. data, 1, read_buf, &actual_read_len);
  10245. if (ret) {
  10246. pt_debug(dev, DL_ERROR,
  10247. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10248. __func__, file_handle);
  10249. return -ETIME;
  10250. }
  10251. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10252. if (status != 0x00) {
  10253. pt_debug(dev, DL_ERROR,
  10254. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10255. __func__, status, file_handle);
  10256. return -status;
  10257. }
  10258. return file_handle;
  10259. }
  10260. /*******************************************************************************
  10261. * FUNCTION: _pt_pip2_file_erase
  10262. *
  10263. * SUMMARY: Using the BL PIP2 commands erase a file
  10264. *
  10265. * NOTE: The DUT must be in BL mode for this command to work
  10266. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10267. * one retry is attempted for each sector in a file.
  10268. *
  10269. * RETURNS:
  10270. * <0 = Error
  10271. * >0 = file handle closed
  10272. *
  10273. * PARAMETERS:
  10274. * *dev - pointer to device structure
  10275. * file_handle - handle to the file to be erased
  10276. * *status - PIP2 erase status code
  10277. ******************************************************************************/
  10278. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10279. {
  10280. int ret = 0;
  10281. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10282. int retry = 1;
  10283. u16 actual_read_len;
  10284. u8 data[2];
  10285. u8 read_buf[10];
  10286. struct pt_core_data *cd = dev_get_drvdata(dev);
  10287. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10288. data[0] = file_handle;
  10289. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10290. *status = PIP2_RSP_ERR_TIMEOUT;
  10291. /* Increase waiting time for large file erase */
  10292. mutex_lock(&cd->system_lock);
  10293. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10294. mutex_unlock(&cd->system_lock);
  10295. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10296. ret = _pt_request_pip2_send_cmd(dev,
  10297. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10298. data, 2, read_buf, &actual_read_len);
  10299. if (ret)
  10300. break;
  10301. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10302. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10303. #ifdef TTDL_DIAGNOSTICS
  10304. cd->file_erase_timeout_count++;
  10305. #endif
  10306. pt_debug(dev, DL_WARN,
  10307. "%s: ERASE timeout %d for file = %d\n",
  10308. __func__, retry, file_handle);
  10309. }
  10310. retry++;
  10311. }
  10312. mutex_lock(&cd->system_lock);
  10313. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10314. mutex_unlock(&cd->system_lock);
  10315. if (ret) {
  10316. pt_debug(dev, DL_ERROR,
  10317. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10318. __func__, ret, file_handle);
  10319. return -EIO;
  10320. }
  10321. if (*status != 0x00) {
  10322. pt_debug(dev, DL_ERROR,
  10323. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10324. __func__, *status, file_handle);
  10325. return -EIO;
  10326. }
  10327. return file_handle;
  10328. }
  10329. /*******************************************************************************
  10330. * FUNCTION: _pt_pip2_file_read
  10331. *
  10332. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10333. *
  10334. * NOTE: The DUT must be in BL mode for this command to work
  10335. *
  10336. * RETURNS:
  10337. * <0 = Error
  10338. * >0 = number of bytes read
  10339. *
  10340. * PARAMETERS:
  10341. * *dev - pointer to device structure
  10342. * file_handle - File handle to read from
  10343. * num_bytes - number of bytes to read
  10344. ******************************************************************************/
  10345. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10346. u8 *read_buf)
  10347. {
  10348. int ret = 0;
  10349. u16 status;
  10350. u16 actual_read_len;
  10351. u8 data[3];
  10352. data[0] = file_handle;
  10353. data[1] = (num_bytes & 0x00FF);
  10354. data[2] = (num_bytes & 0xFF00) >> 8;
  10355. ret = _pt_request_pip2_send_cmd(dev,
  10356. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10357. data, 3, read_buf, &actual_read_len);
  10358. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10359. if (ret || ((status != 0x00) && (status != 0x03))) {
  10360. pt_debug(dev, DL_ERROR,
  10361. "%s File open failure with error code = %d\n",
  10362. __func__, status);
  10363. return -EPERM;
  10364. }
  10365. ret = num_bytes;
  10366. return ret;
  10367. }
  10368. /*******************************************************************************
  10369. * FUNCTION: _pt_read_us_file
  10370. *
  10371. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10372. * then read the entire file.
  10373. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10374. *
  10375. * RETURN:
  10376. * 0 = success
  10377. * !0 = failure
  10378. *
  10379. * PARAMETERS:
  10380. * *file_path - pointer to the file path
  10381. * *buf - pointer to the buffer to store the file contents
  10382. * *size - pointer to the size of the file
  10383. ******************************************************************************/
  10384. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10385. {
  10386. struct file *filp = NULL;
  10387. struct inode *inode = NULL;
  10388. unsigned int file_len = 0;
  10389. unsigned int read_len = 0;
  10390. mm_segment_t oldfs;
  10391. int rc = 0;
  10392. if (file_path == NULL || buf == NULL) {
  10393. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10394. return -EINVAL;
  10395. }
  10396. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10397. oldfs = force_uaccess_begin();
  10398. filp = filp_open_block(file_path, O_RDONLY, 0400);
  10399. if (IS_ERR(filp)) {
  10400. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10401. file_path);
  10402. rc = -ENOENT;
  10403. goto err;
  10404. }
  10405. if (filp->f_op == NULL) {
  10406. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10407. __func__);
  10408. rc = -EINVAL;
  10409. goto exit;
  10410. }
  10411. inode = filp->f_path.dentry->d_inode;
  10412. if (inode == NULL) {
  10413. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10414. __func__);
  10415. rc = -EINVAL;
  10416. goto exit;
  10417. }
  10418. file_len = i_size_read(inode->i_mapping->host);
  10419. if (file_len == 0) {
  10420. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10421. __func__, file_len);
  10422. rc = -EINVAL;
  10423. goto exit;
  10424. }
  10425. if (*size == 0)
  10426. read_len = file_len;
  10427. else
  10428. read_len = *size;
  10429. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10430. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10431. __func__, read_len);
  10432. rc = -EINVAL;
  10433. goto exit;
  10434. }
  10435. filp->private_data = inode->i_private;
  10436. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  10437. if (filp->f_op->read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10438. #else
  10439. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10440. #endif
  10441. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10442. rc = -EINVAL;
  10443. goto exit;
  10444. }
  10445. *size = read_len;
  10446. exit:
  10447. if (filp_close(filp, NULL) != 0)
  10448. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10449. err:
  10450. force_uaccess_end(oldfs);
  10451. return rc;
  10452. }
  10453. /*******************************************************************************
  10454. * FUNCTION: _pt_request_pip2_bin_hdr
  10455. *
  10456. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10457. * in the case of a flashless DUT, and parse the contents
  10458. *
  10459. * RETURNS:
  10460. * 0 = Success
  10461. * !0 = Error condition
  10462. *
  10463. * PARAMETERS:
  10464. * *dev - pointer to device structure
  10465. ******************************************************************************/
  10466. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10467. {
  10468. struct pt_core_data *cd = dev_get_drvdata(dev);
  10469. u8 file_handle;
  10470. u8 read_buf[255];
  10471. u8 hdr_len = 0;
  10472. u8 i;
  10473. int bytes_read;
  10474. int read_size;
  10475. int ret = 0;
  10476. int rc = 0;
  10477. bool load_hdr_struct = false;
  10478. if (cd->flashless_dut) {
  10479. read_size = sizeof(read_buf);
  10480. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10481. read_buf, &read_size);
  10482. if (rc) {
  10483. ret = rc;
  10484. pt_debug(dev, DL_ERROR,
  10485. "%s Failed to read fw image from US, rc=%d\n",
  10486. __func__, rc);
  10487. goto exit;
  10488. }
  10489. load_hdr_struct = true;
  10490. hdr_len = read_buf[0];
  10491. i = 0;
  10492. } else {
  10493. if (cd->mode != PT_MODE_BOOTLOADER) {
  10494. ret = -EPERM;
  10495. goto exit;
  10496. }
  10497. /* Open the bin file in Flash */
  10498. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10499. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10500. if (file_handle != PIP2_FW_FILE) {
  10501. ret = -ENOENT;
  10502. pt_debug(dev, DL_ERROR,
  10503. "%s Failed to open bin file\n", __func__);
  10504. goto exit;
  10505. }
  10506. /* Read the header length from the file */
  10507. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10508. read_size = 1;
  10509. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10510. read_buf);
  10511. if (bytes_read != read_size) {
  10512. ret = -EX_ERR_FREAD;
  10513. pt_debug(dev, DL_ERROR,
  10514. "%s Failed to bin file header len\n", __func__);
  10515. goto exit_close_file;
  10516. }
  10517. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10518. if (hdr_len == 0xFF) {
  10519. ret = -EX_ERR_FLEN;
  10520. pt_debug(dev, DL_ERROR,
  10521. "%s Bin header len is invalid\n", __func__);
  10522. goto exit_close_file;
  10523. }
  10524. /* Read the rest of the header from the bin file */
  10525. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10526. memset(read_buf, 0, sizeof(read_buf));
  10527. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10528. read_buf);
  10529. if (bytes_read != hdr_len) {
  10530. ret = -EX_ERR_FREAD;
  10531. pt_debug(dev, DL_ERROR,
  10532. "%s Failed to read bin file\n", __func__);
  10533. goto exit_close_file;
  10534. }
  10535. load_hdr_struct = true;
  10536. exit_close_file:
  10537. /* Close the file */
  10538. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10539. ret = -EX_ERR_FCLOSE;
  10540. pt_debug(dev, DL_ERROR,
  10541. "%s Failed to close bin file\n", __func__);
  10542. }
  10543. /*
  10544. * The length was already read so subtract 1 to make the rest of
  10545. * the offsets match the spec
  10546. */
  10547. i = PIP2_RESP_BODY_OFFSET - 1;
  10548. }
  10549. if (load_hdr_struct) {
  10550. hdr->length = hdr_len;
  10551. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10552. hdr->fw_major = (read_buf[i+3]);
  10553. hdr->fw_minor = (read_buf[i+4]);
  10554. hdr->fw_crc = (read_buf[i+5] << 24) |
  10555. (read_buf[i+6] << 16) |
  10556. (read_buf[i+7] << 8) |
  10557. (read_buf[i+8]);
  10558. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10559. (read_buf[i+10] << 16) |
  10560. (read_buf[i+11] << 8) |
  10561. (read_buf[i+12]);
  10562. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10563. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10564. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10565. if (hdr_len >= 22) {
  10566. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10567. (read_buf[i+20] << 16) |
  10568. (read_buf[i+21] << 8) |
  10569. (read_buf[i+22]);
  10570. } else {
  10571. hdr->hex_file_size = 0;
  10572. }
  10573. }
  10574. exit:
  10575. return ret;
  10576. }
  10577. /*******************************************************************************
  10578. * FUNCTION: _pt_pip2_file_get_stats
  10579. *
  10580. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10581. * opened file
  10582. *
  10583. * NOTE: The DUT must be in BL mode for this command to work
  10584. *
  10585. * RETURNS:
  10586. * !0 = Error
  10587. * 0 = Success
  10588. *
  10589. * PARAMETERS:
  10590. * *dev - pointer to device structure
  10591. * file_handle - File handle to read from
  10592. * *address - pointer to store address of file
  10593. * *file_size _ pointer to store size of file
  10594. ******************************************************************************/
  10595. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10596. u32 *file_size)
  10597. {
  10598. int ret = 1;
  10599. u16 status;
  10600. u16 actual_read_len;
  10601. u8 data[2];
  10602. u8 read_buf[16];
  10603. data[0] = file_handle;
  10604. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10605. ret = _pt_request_pip2_send_cmd(dev,
  10606. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10607. data, 2, read_buf, &actual_read_len);
  10608. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10609. if (ret || (status != 0x00)) {
  10610. pt_debug(dev, DL_ERROR,
  10611. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10612. __func__, status, file_handle, ret);
  10613. ret = -EIO;
  10614. goto exit;
  10615. }
  10616. pt_debug(dev, DL_DEBUG,
  10617. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10618. if (address) {
  10619. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10620. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10621. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10622. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10623. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10624. __func__, *address);
  10625. }
  10626. if (file_size) {
  10627. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10628. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10629. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10630. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10631. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10632. __func__, *file_size);
  10633. }
  10634. exit:
  10635. return ret;
  10636. }
  10637. /*******************************************************************************
  10638. * FUNCTION: _pt_pip2_file_seek_offset
  10639. *
  10640. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10641. * opened file
  10642. *
  10643. * NOTE: The DUT must be in BL mode for this command to work
  10644. * NOTE: File open/erase command can reset the offset
  10645. *
  10646. * RETURNS:
  10647. * !0 = Error
  10648. * 0 = Success
  10649. *
  10650. * PARAMETERS:
  10651. * *dev - pointer to device structure
  10652. * file_handle - File handle to read from
  10653. * read_offset - read offset of file
  10654. * write_offset - write offset of file
  10655. ******************************************************************************/
  10656. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10657. u32 read_offset, u32 write_offset)
  10658. {
  10659. int ret = 1;
  10660. u16 status;
  10661. u16 actual_read_len;
  10662. u8 data[10];
  10663. u8 read_buf[16];
  10664. data[0] = file_handle;
  10665. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10666. data[2] = 0xff & read_offset;
  10667. data[3] = 0xff & (read_offset >> 8);
  10668. data[4] = 0xff & (read_offset >> 16);
  10669. data[5] = 0xff & (read_offset >> 24);
  10670. data[6] = 0xff & write_offset;
  10671. data[7] = 0xff & (write_offset >> 8);
  10672. data[8] = 0xff & (write_offset >> 16);
  10673. data[9] = 0xff & (write_offset >> 24);
  10674. ret = _pt_request_pip2_send_cmd(dev,
  10675. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10676. data, 10, read_buf, &actual_read_len);
  10677. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10678. if (ret || (status != 0x00)) {
  10679. pt_debug(dev, DL_ERROR,
  10680. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10681. __func__, status, ret, file_handle);
  10682. ret = -EIO;
  10683. }
  10684. return ret;
  10685. }
  10686. /*******************************************************************************
  10687. * FUNCTION: _pt_pip2_file_crc
  10688. *
  10689. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10690. * the file.
  10691. *
  10692. * NOTE: The DUT must be in BL mode for this command to work
  10693. * NOTE: This command only can be used for BL version 1.8 or greater.
  10694. * BL version 1.8 added this change according to PGV-173.
  10695. *
  10696. * RETURNS:
  10697. * !0 = Error
  10698. * 0 = Success
  10699. *
  10700. * PARAMETERS:
  10701. * *dev - pointer to device structure
  10702. * file_handle - File handle to read from
  10703. * offset - start offset for CRC calculation
  10704. * length - number of bytes to calculate CRC over
  10705. * read_buf - pointer to the read buffer
  10706. ******************************************************************************/
  10707. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10708. u32 offset, u32 length, u8 *read_buf)
  10709. {
  10710. int rc = 1;
  10711. u16 actual_read_len;
  10712. u8 data[10];
  10713. data[0] = file_handle;
  10714. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10715. data[2] = 0xff & offset;
  10716. data[3] = 0xff & (offset >> 8);
  10717. data[4] = 0xff & (offset >> 16);
  10718. data[5] = 0xff & (offset >> 24);
  10719. data[6] = 0xff & length;
  10720. data[7] = 0xff & (length >> 8);
  10721. data[8] = 0xff & (length >> 16);
  10722. data[9] = 0xff & (length >> 24);
  10723. rc = _pt_request_pip2_send_cmd(dev,
  10724. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10725. data, 10, read_buf, &actual_read_len);
  10726. if (rc)
  10727. pt_debug(dev, DL_ERROR,
  10728. "%s Return FILE_CRC failure, rc = %d\n",
  10729. __func__, rc);
  10730. return rc;
  10731. }
  10732. /*******************************************************************************
  10733. * FUNCTION: pt_pip2_ping_test
  10734. *
  10735. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10736. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10737. * response payload matches what was sent.
  10738. * The max payload size is 247:
  10739. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10740. *
  10741. * RETURN:
  10742. * 0 = success
  10743. * !0 = failure
  10744. *
  10745. * PARAMETERS:
  10746. * *dev - pointer to device structure
  10747. * *attr - pointer to device attributes
  10748. * *buf - pointer to output buffer
  10749. ******************************************************************************/
  10750. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10751. {
  10752. u16 actual_read_len;
  10753. u8 *read_buf = NULL;
  10754. u8 *data = NULL;
  10755. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10756. int i = 1;
  10757. int j = 0;
  10758. int rc = 0;
  10759. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10760. if (!read_buf)
  10761. goto ping_test_exit;
  10762. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10763. if (!data)
  10764. goto ping_test_exit;
  10765. /* Load data payload with an array of walking 1's */
  10766. for (i = 0; i < 255; i++)
  10767. data[i] = 0x01 << (i % 8);
  10768. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10769. for (i = 0; i <= max_bytes; i++) {
  10770. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10771. PIP2_CMD_ID_PING, data, i, read_buf,
  10772. &actual_read_len);
  10773. if (rc) {
  10774. pt_debug(dev, DL_ERROR,
  10775. "%s: PING failed with %d byte payload\n",
  10776. __func__, i);
  10777. break;
  10778. }
  10779. /* Verify data returned matches data sent */
  10780. for (j = 0; j < i; j++) {
  10781. if (read_buf[data_offset + j] != data[j]) {
  10782. pt_debug(dev, DL_DEBUG,
  10783. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10784. __func__, i, j, data[j], j,
  10785. read_buf[data_offset + j]);
  10786. goto ping_test_exit;
  10787. }
  10788. }
  10789. }
  10790. ping_test_exit:
  10791. *last_packet_size = i - 1;
  10792. kfree(read_buf);
  10793. kfree(data);
  10794. return rc;
  10795. }
  10796. /*******************************************************************************
  10797. * FUNCTION: _pt_ic_parse_input_hex
  10798. *
  10799. * SUMMARY: Parse a char data array as space delimited hex values into
  10800. * an int array.
  10801. *
  10802. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10803. * buffer to store data is "u32". This function is still needed by the
  10804. * "command" sysfs node because the buffer type to store data is "u8".
  10805. *
  10806. * RETURN: Length of parsed data
  10807. *
  10808. * PARAMETERS:
  10809. * *dev - pointer to device structure
  10810. * *buf - pointer to buffer that holds the input array to parse
  10811. * buf_size - size of buf
  10812. * *ic_buf - pointer to array to store parsed data
  10813. * ic_buf_size - max size of ic_buf
  10814. ******************************************************************************/
  10815. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10816. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10817. {
  10818. const char *pbuf = buf;
  10819. unsigned long value;
  10820. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10821. u32 i = 0;
  10822. u32 j;
  10823. int last = 0;
  10824. int ret;
  10825. pt_debug(dev, DL_DEBUG,
  10826. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10827. __func__, pbuf, buf, buf_size, "scan buf size",
  10828. PT_MAX_PIP2_MSG_SIZE, buf);
  10829. while (pbuf <= (buf + buf_size)) {
  10830. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10831. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10832. __func__, "Max cmd size exceeded", i,
  10833. PT_MAX_PIP2_MSG_SIZE);
  10834. return -EINVAL;
  10835. }
  10836. if (i >= ic_buf_size) {
  10837. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10838. __func__, "Buffer size exceeded", i,
  10839. ic_buf_size);
  10840. return -EINVAL;
  10841. }
  10842. while (((*pbuf == ' ') || (*pbuf == ','))
  10843. && (pbuf < (buf + buf_size))) {
  10844. last = *pbuf;
  10845. pbuf++;
  10846. }
  10847. if (pbuf >= (buf + buf_size))
  10848. break;
  10849. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10850. if ((last == ',') && (*pbuf == ',')) {
  10851. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10852. __func__, "Invalid data format.");
  10853. return -EINVAL;
  10854. }
  10855. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10856. && (pbuf < (buf + buf_size))
  10857. && (*pbuf != ' ')
  10858. && (*pbuf != ','); j++) {
  10859. last = *pbuf;
  10860. scan_buf[j] = *pbuf++;
  10861. }
  10862. ret = kstrtoul(scan_buf, 16, &value);
  10863. if (ret < 0) {
  10864. pt_debug(dev, DL_ERROR,
  10865. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10866. "Invalid data format. ", scan_buf,
  10867. "Use \"0xHH,...,0xHH\"", " instead.",
  10868. i, ret);
  10869. return ret;
  10870. }
  10871. ic_buf[i] = value;
  10872. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10873. __func__, i, value);
  10874. i++;
  10875. }
  10876. return i;
  10877. }
  10878. /*******************************************************************************
  10879. * FUNCTION: _pt_ic_parse_input
  10880. *
  10881. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10882. * hex values or dec values into an int array with the following rules:
  10883. * 1) Hex values must have a "0x" prefix for each element or the first element
  10884. * only
  10885. * 2) Dec values do not have any prefix
  10886. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10887. * string
  10888. *
  10889. * RETURN: Number of values parsed
  10890. *
  10891. * PARAMETERS:
  10892. * *dev - pointer to device structure
  10893. * *buf - pointer to buffer that holds the input array to parse
  10894. * buf_size - size of buf
  10895. * *out_buf - pointer to array to store parsed data
  10896. * out_buf_size - max size of buffer to be stored
  10897. ******************************************************************************/
  10898. static int _pt_ic_parse_input(struct device *dev,
  10899. const char *buf, size_t buf_size,
  10900. u32 *out_buf, size_t out_buf_size)
  10901. {
  10902. const char *pbuf = buf;
  10903. unsigned long value;
  10904. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10905. u32 i = 0;
  10906. u32 j;
  10907. int last = 0;
  10908. int ret = 0;
  10909. u8 str_base = 0;
  10910. pt_debug(dev, DL_DEBUG,
  10911. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10912. __func__, buf_size, out_buf_size, "scan buf size",
  10913. PT_MAX_PIP2_MSG_SIZE, buf);
  10914. while (pbuf <= (buf + buf_size)) {
  10915. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10916. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10917. __func__, "Max cmd size exceeded", i,
  10918. PT_MAX_PIP2_MSG_SIZE);
  10919. ret = -EINVAL;
  10920. goto error;
  10921. }
  10922. if (i >= out_buf_size) {
  10923. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10924. __func__, "Buffer size exceeded", i,
  10925. out_buf_size);
  10926. ret = -EINVAL;
  10927. goto error;
  10928. }
  10929. while (((*pbuf == ' ') || (*pbuf == ','))
  10930. && (pbuf < (buf + buf_size))) {
  10931. last = *pbuf;
  10932. pbuf++;
  10933. }
  10934. if (pbuf >= (buf + buf_size))
  10935. break;
  10936. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10937. if ((last == ',') && (*pbuf == ',')) {
  10938. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10939. __func__, "Invalid data format.");
  10940. ret = -EINVAL;
  10941. goto error;
  10942. }
  10943. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10944. && (pbuf < (buf + buf_size))
  10945. && (*pbuf != ' ')
  10946. && (*pbuf != ','); j++) {
  10947. last = *pbuf;
  10948. scan_buf[j] = *pbuf++;
  10949. }
  10950. if (i == 0) {
  10951. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10952. (strncmp(scan_buf, "0X", 2) == 0))
  10953. str_base = 16;
  10954. else
  10955. str_base = 10;
  10956. } else {
  10957. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10958. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10959. (str_base == 10)) {
  10960. pt_debug(dev, DL_ERROR,
  10961. "%s: Decimal and Heximal data mixed\n",
  10962. __func__);
  10963. ret = -EINVAL;
  10964. goto error;
  10965. }
  10966. }
  10967. ret = kstrtoul(scan_buf, str_base, &value);
  10968. if (ret < 0) {
  10969. pt_debug(dev, DL_ERROR,
  10970. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10971. "Invalid data format. ", scan_buf,
  10972. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10973. " instead.", i, ret);
  10974. goto error;
  10975. }
  10976. out_buf[i] = value;
  10977. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10978. __func__, i, value, value);
  10979. i++;
  10980. }
  10981. ret = i;
  10982. error:
  10983. return ret;
  10984. }
  10985. #ifdef TTHE_TUNER_SUPPORT
  10986. /*******************************************************************************
  10987. * FUNCTION: tthe_debugfs_open
  10988. *
  10989. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10990. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10991. * printing a large number of fingers and sensor data without overflowing
  10992. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10993. * even multiple of PAGE_SIZE
  10994. *
  10995. * RETURN:
  10996. * 0 = success
  10997. * !0 = failure
  10998. *
  10999. * PARAMETERS:
  11000. * *inode - file inode number
  11001. * *filp - file pointer to debugfs file
  11002. ******************************************************************************/
  11003. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  11004. {
  11005. struct pt_core_data *cd = inode->i_private;
  11006. u32 buf_size = PT_MAX_PRBUF_SIZE;
  11007. filp->private_data = inode->i_private;
  11008. if (cd->tthe_buf)
  11009. return -EBUSY;
  11010. while (buf_size < 4096)
  11011. buf_size = buf_size << 1;
  11012. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  11013. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  11014. cd->tthe_buf_size = buf_size;
  11015. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  11016. if (!cd->tthe_buf)
  11017. return -ENOMEM;
  11018. return 0;
  11019. }
  11020. /*******************************************************************************
  11021. * FUNCTION: tthe_debugfs_close
  11022. *
  11023. * SUMMARY: Close method for tthe_tuner debugfs node.
  11024. *
  11025. * RETURN:
  11026. * 0 = success
  11027. * !0 = failure
  11028. *
  11029. * PARAMETERS:
  11030. * *inode - file inode number
  11031. * *filp - file pointer to debugfs file
  11032. ******************************************************************************/
  11033. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  11034. {
  11035. struct pt_core_data *cd = filp->private_data;
  11036. filp->private_data = NULL;
  11037. kfree(cd->tthe_buf);
  11038. cd->tthe_buf = NULL;
  11039. return 0;
  11040. }
  11041. /*******************************************************************************
  11042. * FUNCTION: tthe_debugfs_read
  11043. *
  11044. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11045. * tthe_buf to user buffer.
  11046. *
  11047. * RETURN: Size of debugfs data print
  11048. *
  11049. * PARAMETERS:
  11050. * *filp - file pointer to debugfs file
  11051. * *buf - the user space buffer to read to
  11052. * count - the maximum number of bytes to read
  11053. * *ppos - the current position in the buffer
  11054. ******************************************************************************/
  11055. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11056. size_t count, loff_t *ppos)
  11057. {
  11058. struct pt_core_data *cd = filp->private_data;
  11059. int size;
  11060. int ret;
  11061. static int partial_read;
  11062. wait_event_interruptible(cd->wait_q,
  11063. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11064. mutex_lock(&cd->tthe_lock);
  11065. if (cd->tthe_exit) {
  11066. mutex_unlock(&cd->tthe_lock);
  11067. return 0;
  11068. }
  11069. if (count > cd->tthe_buf_len)
  11070. size = cd->tthe_buf_len;
  11071. else
  11072. size = count;
  11073. if (!size) {
  11074. mutex_unlock(&cd->tthe_lock);
  11075. return 0;
  11076. }
  11077. if (partial_read) {
  11078. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11079. partial_read = 0;
  11080. } else {
  11081. ret = copy_to_user(buf, cd->tthe_buf, size);
  11082. }
  11083. if (size == count)
  11084. partial_read = count;
  11085. if (ret == size)
  11086. return -EFAULT;
  11087. size -= ret;
  11088. cd->tthe_buf_len -= size;
  11089. mutex_unlock(&cd->tthe_lock);
  11090. *ppos += size;
  11091. return size;
  11092. }
  11093. static const struct file_operations tthe_debugfs_fops = {
  11094. .open = tthe_debugfs_open,
  11095. .release = tthe_debugfs_close,
  11096. .read = tthe_debugfs_read,
  11097. };
  11098. #endif
  11099. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11100. .start_bl = _pt_request_pip_start_bl,
  11101. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11102. .resume_scanning = _pt_request_pip_resume_scanning,
  11103. .get_param = _pt_request_pip_get_param,
  11104. .set_param = _pt_request_pip_set_param,
  11105. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11106. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11107. .get_data_structure = _pt_request_pip_get_data_structure,
  11108. .run_selftest = _pt_request_pip_run_selftest,
  11109. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11110. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11111. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11112. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11113. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11114. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11115. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11116. .read_data_block = _pt_request_pip_read_data_block,
  11117. .write_data_block = _pt_request_pip_write_data_block,
  11118. .user_cmd = _pt_request_pip_user_cmd,
  11119. .get_bl_info = _pt_request_pip_bl_get_information,
  11120. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11121. .launch_app = _pt_request_pip_launch_app,
  11122. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11123. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11124. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11125. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11126. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11127. .pip2_file_open = _pt_pip2_file_open,
  11128. .pip2_file_close = _pt_pip2_file_close,
  11129. .pip2_file_erase = _pt_pip2_file_erase,
  11130. .read_us_file = _pt_read_us_file,
  11131. .manage_cal_data = _pt_manage_local_cal_data,
  11132. .calc_crc = crc_ccitt_calculate,
  11133. #ifdef TTDL_DIAGNOSTICS
  11134. .pip2_file_read = _pt_pip2_file_read,
  11135. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11136. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11137. .pip2_file_crc = _pt_pip2_file_crc,
  11138. #endif
  11139. };
  11140. static struct pt_core_commands _pt_core_commands = {
  11141. .subscribe_attention = _pt_subscribe_attention,
  11142. .unsubscribe_attention = _pt_unsubscribe_attention,
  11143. .request_exclusive = _pt_request_exclusive,
  11144. .release_exclusive = _pt_release_exclusive,
  11145. .request_reset = _pt_request_reset,
  11146. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11147. .request_enum = _pt_request_enum,
  11148. .request_sysinfo = _pt_request_sysinfo,
  11149. .request_loader_pdata = _pt_request_loader_pdata,
  11150. .request_stop_wd = _pt_request_stop_wd,
  11151. .request_start_wd = _pt_request_start_wd,
  11152. .request_get_mode = _pt_request_get_mode,
  11153. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11154. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11155. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11156. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11157. .request_dut_generation = _pt_request_dut_generation,
  11158. .request_hw_version = _pt_request_hw_version,
  11159. .parse_sysfs_input = _pt_ic_parse_input,
  11160. #ifdef TTHE_TUNER_SUPPORT
  11161. .request_tthe_print = _pt_request_tthe_print,
  11162. #endif
  11163. #ifdef TTDL_DIAGNOSTICS
  11164. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11165. #endif
  11166. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11167. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11168. };
  11169. struct pt_core_commands *pt_get_commands(void)
  11170. {
  11171. return &_pt_core_commands;
  11172. }
  11173. EXPORT_SYMBOL_GPL(pt_get_commands);
  11174. static DEFINE_MUTEX(core_list_lock);
  11175. static LIST_HEAD(core_list);
  11176. static DEFINE_MUTEX(module_list_lock);
  11177. static LIST_HEAD(module_list);
  11178. static int core_number;
  11179. /*******************************************************************************
  11180. * FUNCTION: pt_probe_module
  11181. *
  11182. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11183. *
  11184. * RETURN:
  11185. * 0 = success
  11186. * !0 = failure
  11187. *
  11188. * PARAMETERS:
  11189. * *cd - pointer to core data
  11190. * *module - pointer to module structure
  11191. ******************************************************************************/
  11192. static int pt_probe_module(struct pt_core_data *cd,
  11193. struct pt_module *module)
  11194. {
  11195. struct module_node *module_node;
  11196. int rc = 0;
  11197. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11198. if (!module_node)
  11199. return -ENOMEM;
  11200. module_node->module = module;
  11201. mutex_lock(&cd->module_list_lock);
  11202. list_add(&module_node->node, &cd->module_list);
  11203. mutex_unlock(&cd->module_list_lock);
  11204. rc = module->probe(cd->dev, &module_node->data);
  11205. if (rc) {
  11206. /*
  11207. * Remove from the list when probe fails
  11208. * in order not to call release
  11209. */
  11210. mutex_lock(&cd->module_list_lock);
  11211. list_del(&module_node->node);
  11212. mutex_unlock(&cd->module_list_lock);
  11213. kfree(module_node);
  11214. goto exit;
  11215. }
  11216. exit:
  11217. return rc;
  11218. }
  11219. /*******************************************************************************
  11220. * FUNCTION: pt_release_module
  11221. *
  11222. * SUMMARY: Call the release pointer and remove the module pointer from
  11223. * module_list.
  11224. *
  11225. * PARAMETERS:
  11226. * *cd - pointer to core data
  11227. * *module - pointer to module structure
  11228. ******************************************************************************/
  11229. static void pt_release_module(struct pt_core_data *cd,
  11230. struct pt_module *module)
  11231. {
  11232. struct module_node *m, *m_n;
  11233. mutex_lock(&cd->module_list_lock);
  11234. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11235. if (m->module == module) {
  11236. module->release(cd->dev, m->data);
  11237. list_del(&m->node);
  11238. kfree(m);
  11239. break;
  11240. }
  11241. mutex_unlock(&cd->module_list_lock);
  11242. }
  11243. /*******************************************************************************
  11244. * FUNCTION: pt_probe_modules
  11245. *
  11246. * SUMMARY: Iterate module_list and probe each module.
  11247. *
  11248. * PARAMETERS:
  11249. * *cd - pointer to core data
  11250. ******************************************************************************/
  11251. static void pt_probe_modules(struct pt_core_data *cd)
  11252. {
  11253. struct pt_module *m;
  11254. int rc = 0;
  11255. mutex_lock(&module_list_lock);
  11256. list_for_each_entry(m, &module_list, node) {
  11257. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11258. __func__, m->name);
  11259. rc = pt_probe_module(cd, m);
  11260. if (rc)
  11261. pt_debug(cd->dev, DL_ERROR,
  11262. "%s: Probe fails for module %s\n",
  11263. __func__, m->name);
  11264. }
  11265. mutex_unlock(&module_list_lock);
  11266. }
  11267. /*******************************************************************************
  11268. * FUNCTION: pt_release_modules
  11269. *
  11270. * SUMMARY: Iterate module_list and remove each module.
  11271. *
  11272. * PARAMETERS:
  11273. * *cd - pointer to core data
  11274. ******************************************************************************/
  11275. static void pt_release_modules(struct pt_core_data *cd)
  11276. {
  11277. struct pt_module *m;
  11278. mutex_lock(&module_list_lock);
  11279. list_for_each_entry(m, &module_list, node)
  11280. pt_release_module(cd, m);
  11281. mutex_unlock(&module_list_lock);
  11282. }
  11283. /*******************************************************************************
  11284. * FUNCTION: pt_get_core_data
  11285. *
  11286. * SUMMARY: Iterate core_list and get core data.
  11287. *
  11288. * RETURN:
  11289. * pointer to core data or null pointer if fail
  11290. *
  11291. * PARAMETERS:
  11292. * *id - pointer to core id
  11293. ******************************************************************************/
  11294. struct pt_core_data *pt_get_core_data(char *id)
  11295. {
  11296. struct pt_core_data *d;
  11297. list_for_each_entry(d, &core_list, node)
  11298. if (!strncmp(d->core_id, id, 20))
  11299. return d;
  11300. return NULL;
  11301. }
  11302. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11303. /*******************************************************************************
  11304. * FUNCTION: pt_add_core
  11305. *
  11306. * SUMMARY: Add core data to the core_list.
  11307. *
  11308. * PARAMETERS:
  11309. * *dev - pointer to device structure
  11310. ******************************************************************************/
  11311. static void pt_add_core(struct device *dev)
  11312. {
  11313. struct pt_core_data *d;
  11314. struct pt_core_data *cd = dev_get_drvdata(dev);
  11315. mutex_lock(&core_list_lock);
  11316. list_for_each_entry(d, &core_list, node)
  11317. if (d->dev == dev)
  11318. goto unlock;
  11319. list_add(&cd->node, &core_list);
  11320. unlock:
  11321. mutex_unlock(&core_list_lock);
  11322. }
  11323. /*******************************************************************************
  11324. * FUNCTION: pt_del_core
  11325. *
  11326. * SUMMARY: Remove core data from the core_list.
  11327. *
  11328. * PARAMETERS:
  11329. * *dev - pointer to device structure
  11330. ******************************************************************************/
  11331. static void pt_del_core(struct device *dev)
  11332. {
  11333. struct pt_core_data *d, *d_n;
  11334. mutex_lock(&core_list_lock);
  11335. list_for_each_entry_safe(d, d_n, &core_list, node)
  11336. if (d->dev == dev) {
  11337. list_del(&d->node);
  11338. goto unlock;
  11339. }
  11340. unlock:
  11341. mutex_unlock(&core_list_lock);
  11342. }
  11343. /*******************************************************************************
  11344. * FUNCTION: pt_register_module
  11345. *
  11346. * SUMMARY: Register the module to module_list and probe the module for each
  11347. * core.
  11348. *
  11349. * RETURN:
  11350. * 0 = success
  11351. * !0 = failure
  11352. *
  11353. * PARAMETERS:
  11354. * *module - pointer to module structure
  11355. ******************************************************************************/
  11356. int pt_register_module(struct pt_module *module)
  11357. {
  11358. struct pt_module *m;
  11359. struct pt_core_data *cd;
  11360. int rc = 0;
  11361. if (!module || !module->probe || !module->release)
  11362. return -EINVAL;
  11363. mutex_lock(&module_list_lock);
  11364. list_for_each_entry(m, &module_list, node)
  11365. if (m == module) {
  11366. rc = -EEXIST;
  11367. goto unlock;
  11368. }
  11369. list_add(&module->node, &module_list);
  11370. /* Probe the module for each core */
  11371. mutex_lock(&core_list_lock);
  11372. list_for_each_entry(cd, &core_list, node)
  11373. pt_probe_module(cd, module);
  11374. mutex_unlock(&core_list_lock);
  11375. unlock:
  11376. mutex_unlock(&module_list_lock);
  11377. return rc;
  11378. }
  11379. EXPORT_SYMBOL_GPL(pt_register_module);
  11380. /*******************************************************************************
  11381. * FUNCTION: pt_unregister_module
  11382. *
  11383. * SUMMARY: Release the module for each core and remove the module from
  11384. * module_list.
  11385. *
  11386. * RETURN:
  11387. * 0 = success
  11388. * !0 = failure
  11389. *
  11390. * PARAMETERS:
  11391. * *module - pointer to module structure
  11392. ******************************************************************************/
  11393. void pt_unregister_module(struct pt_module *module)
  11394. {
  11395. struct pt_module *m, *m_n;
  11396. struct pt_core_data *cd;
  11397. if (!module)
  11398. return;
  11399. mutex_lock(&module_list_lock);
  11400. /* Release the module for each core */
  11401. mutex_lock(&core_list_lock);
  11402. list_for_each_entry(cd, &core_list, node)
  11403. pt_release_module(cd, module);
  11404. mutex_unlock(&core_list_lock);
  11405. list_for_each_entry_safe(m, m_n, &module_list, node)
  11406. if (m == module) {
  11407. list_del(&m->node);
  11408. break;
  11409. }
  11410. mutex_unlock(&module_list_lock);
  11411. }
  11412. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11413. /*******************************************************************************
  11414. * FUNCTION: pt_get_module_data
  11415. *
  11416. * SUMMARY: Get module data from module_node by module_list.
  11417. *
  11418. * RETURN:
  11419. * pointer to module data
  11420. *
  11421. * PARAMETERS:
  11422. * *dev - pointer to device structure
  11423. * *module - pointer to module structure
  11424. ******************************************************************************/
  11425. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11426. {
  11427. struct pt_core_data *cd = dev_get_drvdata(dev);
  11428. struct module_node *m;
  11429. void *data = NULL;
  11430. mutex_lock(&cd->module_list_lock);
  11431. list_for_each_entry(m, &cd->module_list, node)
  11432. if (m->module == module) {
  11433. data = m->data;
  11434. break;
  11435. }
  11436. mutex_unlock(&cd->module_list_lock);
  11437. return data;
  11438. }
  11439. EXPORT_SYMBOL(pt_get_module_data);
  11440. #ifdef CONFIG_HAS_EARLYSUSPEND
  11441. /*******************************************************************************
  11442. * FUNCTION: pt_early_suspend
  11443. *
  11444. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11445. * attention list.
  11446. *
  11447. * PARAMETERS:
  11448. * *h - pointer to early_suspend structure
  11449. ******************************************************************************/
  11450. static void pt_early_suspend(struct early_suspend *h)
  11451. {
  11452. struct pt_core_data *cd =
  11453. container_of(h, struct pt_core_data, es);
  11454. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11455. }
  11456. /*******************************************************************************
  11457. * FUNCTION: pt_late_resume
  11458. *
  11459. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11460. * attention list.
  11461. *
  11462. * PARAMETERS:
  11463. * *h - pointer to early_suspend structure
  11464. ******************************************************************************/
  11465. static void pt_late_resume(struct early_suspend *h)
  11466. {
  11467. struct pt_core_data *cd =
  11468. container_of(h, struct pt_core_data, es);
  11469. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11470. }
  11471. /*******************************************************************************
  11472. * FUNCTION: pt_setup_early_suspend
  11473. *
  11474. * SUMMARY: Register early/suspend function to the system.
  11475. *
  11476. * PARAMETERS:
  11477. * *cd - pointer to core data
  11478. ******************************************************************************/
  11479. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11480. {
  11481. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11482. cd->es.suspend = pt_early_suspend;
  11483. cd->es.resume = pt_late_resume;
  11484. register_early_suspend(&cd->es);
  11485. }
  11486. #elif defined(CONFIG_DRM)
  11487. static void pt_resume_work(struct work_struct *work)
  11488. {
  11489. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11490. resume_work);
  11491. int rc = 0;
  11492. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11493. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11494. return;
  11495. rc = pt_core_easywake_off(pt_data);
  11496. if (rc < 0) {
  11497. pt_debug(pt_data->dev, DL_ERROR,
  11498. "%s: Error on wake\n", __func__);
  11499. }
  11500. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11501. return;
  11502. }
  11503. static void pt_suspend_work(struct work_struct *work)
  11504. {
  11505. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11506. suspend_work);
  11507. int rc = 0;
  11508. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11509. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11510. return;
  11511. rc = pt_core_easywake_on(pt_data);
  11512. if (rc < 0) {
  11513. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11514. return;
  11515. }
  11516. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11517. return;
  11518. }
  11519. #if defined(CONFIG_PANEL_NOTIFIER)
  11520. /*******************************************************************************
  11521. * FUNCTION: panel_event_notifier_callback
  11522. *
  11523. * SUMMARY: Call back function for Panel Event notifier to allow to call
  11524. * resume/suspend attention list.
  11525. *
  11526. * PARAMETERS:
  11527. * tag - type of input panel.
  11528. * *notification - pointer to notification details.
  11529. * *client_data - pointer to core data
  11530. ******************************************************************************/
  11531. static void panel_event_notifier_callback(enum panel_event_notifier_tag tag,
  11532. struct panel_event_notification *notification, void *client_data)
  11533. {
  11534. struct pt_core_data *cd = client_data;
  11535. if(!notification)
  11536. {
  11537. pt_debug(cd->dev,DL_INFO, "%s: Invalid notification\n", __func__);
  11538. return;
  11539. }
  11540. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11541. if (cd->quick_boot || cd->drv_debug_suspend)
  11542. goto exit;
  11543. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%d,fb_state %d",
  11544. __func__, notification->notif_type, cd->fb_state);
  11545. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11546. __func__, (notification->notif_type == DRM_PANEL_EVENT_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11547. if (notification->notif_type == DRM_PANEL_EVENT_UNBLANK) {
  11548. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11549. if (notification->notif_data.early_trigger) {
  11550. pr_err("%s: resume: event = %d, not care\n", __func__, notification->notif_type);
  11551. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %d, not care\n",
  11552. __func__, notification->notif_type);
  11553. } else {
  11554. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11555. __func__);
  11556. cancel_work_sync(&cd->resume_offload_work);
  11557. cancel_work_sync(&cd->suspend_offload_work);
  11558. cancel_work_sync(&cd->resume_work);
  11559. cancel_work_sync(&cd->suspend_work);
  11560. queue_work(cd->pt_workqueue, &cd->resume_work);
  11561. #if defined(CONFIG_PM_SLEEP)
  11562. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11563. __func__);
  11564. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11565. pt_core_resume_(cd->dev);
  11566. #endif
  11567. cd->fb_state = FB_ON;
  11568. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11569. }
  11570. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK) {
  11571. pt_debug(cd->dev, DL_INFO, "%s: BLANK!\n", __func__);
  11572. if (notification->notif_data.early_trigger) {
  11573. #if defined(CONFIG_PM_SLEEP)
  11574. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11575. __func__);
  11576. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11577. pt_core_suspend_(cd->dev);
  11578. #endif
  11579. cancel_work_sync(&cd->resume_offload_work);
  11580. cancel_work_sync(&cd->suspend_offload_work);
  11581. cancel_work_sync(&cd->resume_work);
  11582. cancel_work_sync(&cd->suspend_work);
  11583. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11584. cd->fb_state = FB_OFF;
  11585. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11586. } else {
  11587. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11588. __func__, notification->notif_type);
  11589. }
  11590. } else if (notification->notif_type == DRM_PANEL_EVENT_BLANK_LP) {
  11591. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11592. if (notification->notif_data.early_trigger) {
  11593. #if defined(CONFIG_PM_SLEEP)
  11594. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n", __func__);
  11595. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11596. pt_core_suspend_(cd->dev);
  11597. #endif
  11598. cancel_work_sync(&cd->resume_offload_work);
  11599. cancel_work_sync(&cd->suspend_offload_work);
  11600. cancel_work_sync(&cd->resume_work);
  11601. cancel_work_sync(&cd->suspend_work);
  11602. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11603. cd->fb_state = FB_OFF;
  11604. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11605. } else {
  11606. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %d, not care\n",
  11607. __func__, notification->notif_type);
  11608. }
  11609. } else {
  11610. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11611. __func__, notification->notif_type);
  11612. }
  11613. exit:
  11614. return;
  11615. }
  11616. /*******************************************************************************
  11617. * FUNCTION: pt_setup_panel_event_notifier
  11618. *
  11619. * SUMMARY: Set up call back function into drm notifier.
  11620. *
  11621. * PARAMETERS:
  11622. * *cd - pointer to core data
  11623. ******************************************************************************/
  11624. static void pt_setup_panel_event_notifier(struct pt_core_data *cd)
  11625. {
  11626. void *cookie = NULL;
  11627. if (!active_panel)
  11628. pt_debug(cd->dev, DL_ERROR,
  11629. "%s: Active panel not registered!\n", __func__);
  11630. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11631. if (!cd->pt_workqueue) {
  11632. pt_debug(cd->dev, DL_ERROR,
  11633. "%s: worker thread creation failed !\n", __func__);
  11634. }
  11635. if (cd->pt_workqueue) {
  11636. INIT_WORK(&cd->resume_work, pt_resume_work);
  11637. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11638. }
  11639. cookie = panel_event_notifier_register(PANEL_EVENT_NOTIFICATION_PRIMARY,
  11640. PANEL_EVENT_NOTIFIER_CLIENT_PRIMARY_TOUCH,
  11641. active_panel,&panel_event_notifier_callback, cd);
  11642. if (active_panel && !cookie)
  11643. {
  11644. pt_debug(cd->dev, DL_ERROR,
  11645. "%s: Register notifier failed!\n", __func__);
  11646. }
  11647. }
  11648. #else
  11649. /*******************************************************************************
  11650. * FUNCTION: drm_notifier_callback
  11651. *
  11652. * SUMMARY: Call back function for DRM notifier to allow to call
  11653. * resume/suspend attention list.
  11654. *
  11655. * RETURN:
  11656. * 0 = success
  11657. *
  11658. * PARAMETERS:
  11659. * *self - pointer to notifier_block structure
  11660. * event - event type of fb notifier
  11661. * *data - pointer to fb_event structure
  11662. ******************************************************************************/
  11663. static int drm_notifier_callback(struct notifier_block *self,
  11664. unsigned long event, void *data)
  11665. {
  11666. struct pt_core_data *cd =
  11667. container_of(self, struct pt_core_data, fb_notifier);
  11668. struct drm_panel_notifier *evdata = data;
  11669. int *blank;
  11670. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11671. if (!evdata)
  11672. goto exit;
  11673. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11674. event == DRM_PANEL_EVENT_BLANK)) {
  11675. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11676. __func__, event);
  11677. goto exit;
  11678. }
  11679. if (cd->quick_boot || cd->drv_debug_suspend)
  11680. goto exit;
  11681. blank = evdata->data;
  11682. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11683. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11684. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11685. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11686. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11687. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11688. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11689. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11690. __func__, event);
  11691. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11692. if (cd->fb_state != FB_ON) {
  11693. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11694. __func__);
  11695. cancel_work_sync(&cd->resume_offload_work);
  11696. cancel_work_sync(&cd->suspend_offload_work);
  11697. cancel_work_sync(&cd->resume_work);
  11698. cancel_work_sync(&cd->suspend_work);
  11699. queue_work(cd->pt_workqueue, &cd->resume_work);
  11700. #if defined(CONFIG_PM_SLEEP)
  11701. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11702. __func__);
  11703. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11704. pt_core_resume_(cd->dev);
  11705. #endif
  11706. cd->fb_state = FB_ON;
  11707. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11708. }
  11709. }
  11710. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11711. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11712. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11713. if (cd->fb_state != FB_OFF) {
  11714. #if defined(CONFIG_PM_SLEEP)
  11715. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11716. __func__);
  11717. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11718. pt_core_suspend_(cd->dev);
  11719. #endif
  11720. cancel_work_sync(&cd->resume_offload_work);
  11721. cancel_work_sync(&cd->suspend_offload_work);
  11722. cancel_work_sync(&cd->resume_work);
  11723. cancel_work_sync(&cd->suspend_work);
  11724. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11725. cd->fb_state = FB_OFF;
  11726. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11727. }
  11728. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11729. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11730. __func__, event);
  11731. }
  11732. } else {
  11733. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11734. __func__, *blank);
  11735. }
  11736. exit:
  11737. return 0;
  11738. }
  11739. /*******************************************************************************
  11740. * FUNCTION: pt_setup_drm_notifier
  11741. *
  11742. * SUMMARY: Set up call back function into drm notifier.
  11743. *
  11744. * PARAMETERS:
  11745. * *cd - pointer to core data
  11746. ******************************************************************************/
  11747. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11748. {
  11749. cd->fb_state = FB_NONE;
  11750. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11751. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11752. if (!active_panel)
  11753. pt_debug(cd->dev, DL_ERROR,
  11754. "%s: Active panel not registered!\n", __func__);
  11755. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11756. if (!cd->pt_workqueue) {
  11757. pt_debug(cd->dev, DL_ERROR,
  11758. "%s: worker thread creation failed !\n", __func__);
  11759. }
  11760. if (cd->pt_workqueue) {
  11761. INIT_WORK(&cd->resume_work, pt_resume_work);
  11762. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11763. }
  11764. if (active_panel &&
  11765. drm_panel_notifier_register(active_panel,
  11766. &cd->fb_notifier) < 0)
  11767. pt_debug(cd->dev, DL_ERROR,
  11768. "%s: Register notifier failed!\n", __func__);
  11769. }
  11770. #endif
  11771. #elif defined(CONFIG_FB)
  11772. /*******************************************************************************
  11773. * FUNCTION: fb_notifier_callback
  11774. *
  11775. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11776. * resume/suspend attention list.
  11777. *
  11778. * RETURN:
  11779. * 0 = success
  11780. *
  11781. * PARAMETERS:
  11782. * *self - pointer to notifier_block structure
  11783. * event - event type of fb notifier
  11784. * *data - pointer to fb_event structure
  11785. ******************************************************************************/
  11786. static int fb_notifier_callback(struct notifier_block *self,
  11787. unsigned long event, void *data)
  11788. {
  11789. struct pt_core_data *cd =
  11790. container_of(self, struct pt_core_data, fb_notifier);
  11791. struct fb_event *evdata = data;
  11792. int *blank;
  11793. if (event != FB_EVENT_BLANK || !evdata)
  11794. goto exit;
  11795. blank = evdata->data;
  11796. if (*blank == FB_BLANK_UNBLANK) {
  11797. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11798. if (cd->fb_state != FB_ON) {
  11799. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11800. #if defined(CONFIG_PM_SLEEP)
  11801. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11802. pt_core_resume_(cd->dev);
  11803. #endif
  11804. cd->fb_state = FB_ON;
  11805. }
  11806. } else if (*blank == FB_BLANK_POWERDOWN) {
  11807. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11808. if (cd->fb_state != FB_OFF) {
  11809. #if defined(CONFIG_PM_SLEEP)
  11810. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11811. pt_core_suspend_(cd->dev);
  11812. #endif
  11813. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11814. cd->fb_state = FB_OFF;
  11815. }
  11816. }
  11817. exit:
  11818. return 0;
  11819. }
  11820. /*******************************************************************************
  11821. * FUNCTION: pt_setup_fb_notifier
  11822. *
  11823. * SUMMARY: Set up call back function into fb notifier.
  11824. *
  11825. * PARAMETERS:
  11826. * *cd - pointer to core data
  11827. ******************************************************************************/
  11828. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11829. {
  11830. int rc = 0;
  11831. cd->fb_state = FB_ON;
  11832. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11833. rc = fb_register_client(&cd->fb_notifier);
  11834. if (rc)
  11835. pt_debug(cd->dev, DL_ERROR,
  11836. "Unable to register fb_notifier: %d\n", rc);
  11837. }
  11838. #endif
  11839. /*******************************************************************************
  11840. * FUNCTION: pt_watchdog_work
  11841. *
  11842. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11843. * sleeping then this function simply returns. If the DUT is awake the
  11844. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11845. * somehow a response is waiting on the DUT that has not been read. If
  11846. * this occurs the message is simply consumed. If or once the IRQ is
  11847. * cleared, a PIP PING message is sent to the DUT and if the response
  11848. * is received the watchdog succeeds and exits, if no response is seen
  11849. * a startup is queued unless the maximum number of startups have already
  11850. * been attempted, in that case a BL is attempted.
  11851. *
  11852. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11853. * work thread
  11854. *
  11855. * RETURN: void
  11856. *
  11857. * PARAMETERS:
  11858. * *work - pointer to a work structure for the watchdog work queue
  11859. ******************************************************************************/
  11860. static void pt_watchdog_work(struct work_struct *work)
  11861. {
  11862. int rc = 0;
  11863. struct pt_core_data *cd = container_of(work,
  11864. struct pt_core_data, watchdog_work);
  11865. /*
  11866. * if found the current sleep_state is SS_SLEEPING
  11867. * then no need to request_exclusive, directly return
  11868. */
  11869. if (cd->sleep_state == SS_SLEEPING)
  11870. return;
  11871. #ifdef TTDL_DIAGNOSTICS
  11872. cd->watchdog_count++;
  11873. #endif /* TTDL_DIAGNOSTICS */
  11874. /*
  11875. * The first WD interval was extended to allow DDI to come up.
  11876. * If the WD interval is not the default then adjust timer to the
  11877. * current setting. The user can override value with drv_debug sysfs.
  11878. */
  11879. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11880. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11881. msecs_to_jiffies(cd->watchdog_interval));
  11882. }
  11883. if (pt_check_irq_asserted(cd)) {
  11884. #ifdef TTDL_DIAGNOSTICS
  11885. cd->watchdog_irq_stuck_count++;
  11886. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11887. #endif /* TTDL_DIAGNOSTICS */
  11888. pt_debug(cd->dev, DL_ERROR,
  11889. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11890. __func__);
  11891. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11892. }
  11893. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11894. if (rc < 0) {
  11895. pt_debug(cd->dev, DL_ERROR,
  11896. "%s: fail get exclusive ex=%p own=%p\n",
  11897. __func__, cd->exclusive_dev, cd->dev);
  11898. goto queue_startup;
  11899. }
  11900. rc = pt_pip_null_(cd);
  11901. if (release_exclusive(cd, cd->dev) < 0)
  11902. pt_debug(cd->dev, DL_ERROR,
  11903. "%s: fail to release exclusive\n", __func__);
  11904. queue_startup:
  11905. if (rc) {
  11906. #ifdef TTDL_DIAGNOSTICS
  11907. cd->watchdog_failed_access_count++;
  11908. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11909. #endif /* TTDL_DIAGNOSTICS */
  11910. pt_debug(cd->dev, DL_ERROR,
  11911. "%s: failed to access device in WD, retry count=%d\n",
  11912. __func__, cd->startup_retry_count);
  11913. /* Already tried FW upgrade because of watchdog but failed */
  11914. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11915. return;
  11916. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11917. /*
  11918. * Any wrapper function that trys to disable the
  11919. * WD killing this worker cannot be called here.
  11920. */
  11921. rc = request_exclusive(cd, cd->dev,
  11922. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11923. if (rc < 0) {
  11924. pt_debug(cd->dev, DL_ERROR,
  11925. "%s: fail get exclusive ex=%p own=%p\n",
  11926. __func__, cd->exclusive_dev, cd->dev);
  11927. goto exit;
  11928. }
  11929. cd->hw_detected = false;
  11930. cd->startup_status = STARTUP_STATUS_START;
  11931. pt_debug(cd->dev, DL_DEBUG,
  11932. "%s: Startup Status Reset\n", __func__);
  11933. rc = pt_dut_reset_and_wait(cd);
  11934. if (release_exclusive(cd, cd->dev) < 0)
  11935. pt_debug(cd->dev, DL_ERROR,
  11936. "%s: fail to release exclusive\n",
  11937. __func__);
  11938. if (!rc) {
  11939. cd->hw_detected = true;
  11940. if (!cd->flashless_dut)
  11941. pt_queue_enum(cd);
  11942. }
  11943. #ifdef TTDL_DIAGNOSTICS
  11944. cd->wd_xres_count++;
  11945. pt_debug(cd->dev, DL_ERROR,
  11946. "%s: Comm Failed - DUT reset [#%d]\n",
  11947. __func__, cd->wd_xres_count);
  11948. #endif /* TTDL_DIAGNOSTICS */
  11949. } else {
  11950. /*
  11951. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11952. * reset the part to regain communications, try to BL
  11953. */
  11954. pt_debug(cd->dev, DL_ERROR,
  11955. "%s: WD DUT access failure, Start FW Upgrade\n",
  11956. __func__);
  11957. #ifdef TTDL_DIAGNOSTICS
  11958. /*
  11959. * When diagnostics is enabled allow TTDL to keep
  11960. * trying to find the DUT. This allows the DUT to be
  11961. * hot swap-able while the host stays running. In
  11962. * production this may not be wanted as a customer
  11963. * may have several touch drivers and any driver
  11964. * that doesn't match the current DUT should give
  11965. * up trying and give up using the bus.
  11966. */
  11967. pt_debug(cd->dev, DL_INFO,
  11968. "%s: Resetting startup_retry_count\n",
  11969. __func__);
  11970. cd->startup_retry_count = 0;
  11971. #endif /* TTDL_DIAGNOSTICS */
  11972. /*
  11973. * Since fw may be broken,reset sysinfo ready flag
  11974. * to let upgrade function work.
  11975. */
  11976. mutex_lock(&cd->system_lock);
  11977. cd->sysinfo.ready = false;
  11978. mutex_unlock(&cd->system_lock);
  11979. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11980. pt_debug(cd->dev, DL_ERROR,
  11981. "%s: Queue Restart\n", __func__);
  11982. pt_queue_restart(cd);
  11983. } else
  11984. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11985. }
  11986. } else {
  11987. cd->hw_detected = true;
  11988. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11989. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11990. pt_debug(cd->dev, DL_ERROR,
  11991. "%s: HW detected but not enumerated\n",
  11992. __func__);
  11993. pt_queue_enum(cd);
  11994. }
  11995. }
  11996. exit:
  11997. pt_start_wd_timer(cd);
  11998. }
  11999. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  12000. /*******************************************************************************
  12001. * FUNCTION: pt_watchdog_timer
  12002. *
  12003. * SUMMARY: The function that is called when the WD timer expires. If the
  12004. * watchdog work is not already busy schedule the watchdog work queue.
  12005. *
  12006. * RETURN: void
  12007. *
  12008. * PARAMETERS:
  12009. * handle - Handle to the watchdog timer
  12010. ******************************************************************************/
  12011. static void pt_watchdog_timer(unsigned long handle)
  12012. {
  12013. struct pt_core_data *cd = (struct pt_core_data *)handle;
  12014. if (!cd)
  12015. return;
  12016. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12017. __func__);
  12018. if (!work_pending(&cd->watchdog_work))
  12019. schedule_work(&cd->watchdog_work);
  12020. }
  12021. #else
  12022. /*******************************************************************************
  12023. * FUNCTION: pt_watchdog_timer
  12024. *
  12025. * SUMMARY: The function that is called when the WD timer expires. If the
  12026. * watchdog work is not already busy schedule the watchdog work queue.
  12027. *
  12028. * RETURN: void
  12029. *
  12030. * PARAMETERS:
  12031. * *t - Pointer to timer list
  12032. ******************************************************************************/
  12033. static void pt_watchdog_timer(struct timer_list *t)
  12034. {
  12035. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  12036. if (!cd)
  12037. return;
  12038. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  12039. __func__);
  12040. if (!work_pending(&cd->watchdog_work))
  12041. schedule_work(&cd->watchdog_work);
  12042. }
  12043. #endif
  12044. /*******************************************************************************
  12045. * Core sysfs show and store functions
  12046. ******************************************************************************/
  12047. /*******************************************************************************
  12048. * FUNCTION: pt_hw_version_show
  12049. *
  12050. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  12051. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  12052. *
  12053. * RETURN: size of data written to sysfs node
  12054. *
  12055. * PARAMETERS:
  12056. * *dev - pointer to device structure
  12057. * *attr - pointer to device attributes structure
  12058. * *buf - pointer to print output buffer
  12059. ******************************************************************************/
  12060. static ssize_t pt_hw_version_show(struct device *dev,
  12061. struct device_attribute *attr, char *buf)
  12062. {
  12063. struct pt_core_data *cd = dev_get_drvdata(dev);
  12064. _pt_request_hw_version(dev, cd->hw_version);
  12065. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  12066. }
  12067. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  12068. /*******************************************************************************
  12069. * FUNCTION: pt_drv_version_show
  12070. *
  12071. * SUMMARY: Show method for the drv_version sysfs node that will show the
  12072. * TTDL version information
  12073. *
  12074. * RETURN: Char buffer with printed TTDL version information
  12075. *
  12076. * PARAMETERS:
  12077. * *dev - pointer to device structure
  12078. * *attr - pointer to device attributes
  12079. * *buf - pointer to output buffer
  12080. ******************************************************************************/
  12081. static ssize_t pt_drv_version_show(struct device *dev,
  12082. struct device_attribute *attr, char *buf)
  12083. {
  12084. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12085. "Driver: %s\nVersion: %s\nDate: %s\n",
  12086. pt_driver_core_name, pt_driver_core_version,
  12087. pt_driver_core_date);
  12088. }
  12089. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  12090. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  12091. /*******************************************************************************
  12092. * FUNCTION: pt_fw_version_show
  12093. *
  12094. * SUMMARY: Show method for the fw_version sysfs node that will
  12095. * show the firmware, bootloader and PIP version information
  12096. *
  12097. * RETURN: Size of printed buffer
  12098. *
  12099. * PARAMETERS:
  12100. * *dev - pointer to device structure
  12101. * *attr - pointer to device attributes
  12102. * *buf - pointer to output buffer
  12103. ******************************************************************************/
  12104. static ssize_t pt_fw_version_show(struct device *dev,
  12105. struct device_attribute *attr, char *buf)
  12106. {
  12107. struct pt_core_data *cd = dev_get_drvdata(dev);
  12108. struct pt_ttdata *ttdata;
  12109. int rc = 0;
  12110. if (cd->mode == PT_MODE_OPERATIONAL)
  12111. rc = pt_hid_output_get_sysinfo_(cd);
  12112. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12113. __func__, cd->mode, cd->sysinfo.ready);
  12114. if (cd->sysinfo.ready)
  12115. ttdata = &cd->sysinfo.ttdata;
  12116. else
  12117. rc = -ENODATA;
  12118. if (!rc) {
  12119. return scnprintf(buf, strlen(buf),
  12120. "Status: %d\n"
  12121. "FW : %d.%d.%d\n"
  12122. "Config: %d\n"
  12123. "BL : %d.%d\n"
  12124. "PIP : %d.%d\n",
  12125. rc,
  12126. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12127. ttdata->revctrl,
  12128. ttdata->fw_ver_conf,
  12129. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12130. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  12131. } else {
  12132. return scnprintf(buf, strlen(buf),
  12133. "Status: %d\n"
  12134. "FW : n/a\n"
  12135. "Config: n/a\n"
  12136. "BL : n/a\n"
  12137. "PIP : n/a\n",
  12138. rc);
  12139. }
  12140. }
  12141. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  12142. /*******************************************************************************
  12143. * FUNCTION: pt_sysinfo_show
  12144. *
  12145. * SUMMARY: Show method for the sysinfo sysfs node that will
  12146. * show all the information from get system information command.
  12147. *
  12148. * RETURN: Size of printed buffer
  12149. *
  12150. * PARAMETERS:
  12151. * *dev - pointer to device structure
  12152. * *attr - pointer to device attributes
  12153. * *buf - pointer to output buffer
  12154. ******************************************************************************/
  12155. static ssize_t pt_sysinfo_show(struct device *dev,
  12156. struct device_attribute *attr, char *buf)
  12157. {
  12158. struct pt_core_data *cd = dev_get_drvdata(dev);
  12159. struct pt_sysinfo *si;
  12160. struct pt_ttdata *ttdata = NULL;
  12161. struct pt_sensing_conf_data *scd = NULL;
  12162. int rc = 0;
  12163. if (cd->mode == PT_MODE_OPERATIONAL) {
  12164. rc = pt_hid_output_get_sysinfo_(cd);
  12165. if (cd->sysinfo.ready) {
  12166. si = &cd->sysinfo;
  12167. ttdata = &si->ttdata;
  12168. scd = &si->sensing_conf_data;
  12169. } else
  12170. rc = -ENODATA;
  12171. } else
  12172. rc = -EPERM;
  12173. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12174. __func__, cd->mode, cd->sysinfo.ready);
  12175. if (!rc && ttdata && scd) {
  12176. return scnprintf(buf, strlen(buf),
  12177. "Status: %d\n"
  12178. "pip_ver_major: 0x%02X\n"
  12179. "pip_ver_minor: 0x%02X\n"
  12180. "fw_pid : 0x%04X\n"
  12181. "fw_ver_major : 0x%02X\n"
  12182. "fw_ver_minor : 0x%02X\n"
  12183. "revctrl : 0x%08X\n"
  12184. "fw_ver_conf : 0x%04X\n"
  12185. "bl_ver_major : 0x%02X\n"
  12186. "bl_ver_minor : 0x%02X\n"
  12187. "jtag_id_h : 0x%04X\n"
  12188. "jtag_id_l : 0x%04X\n"
  12189. "mfg_id[0] : 0x%02X\n"
  12190. "mfg_id[1] : 0x%02X\n"
  12191. "mfg_id[2] : 0x%02X\n"
  12192. "mfg_id[3] : 0x%02X\n"
  12193. "mfg_id[4] : 0x%02X\n"
  12194. "mfg_id[5] : 0x%02X\n"
  12195. "mfg_id[6] : 0x%02X\n"
  12196. "mfg_id[7] : 0x%02X\n"
  12197. "post_code : 0x%04X\n"
  12198. "electrodes_x : 0x%02X\n"
  12199. "electrodes_y : 0x%02X\n"
  12200. "len_x : 0x%04X\n"
  12201. "len_y : 0x%04X\n"
  12202. "res_x : 0x%04X\n"
  12203. "res_y : 0x%04X\n"
  12204. "max_z : 0x%04X\n"
  12205. "origin_x : 0x%02X\n"
  12206. "origin_y : 0x%02X\n"
  12207. "panel_id : 0x%02X\n"
  12208. "btn : 0x%02X\n"
  12209. "scan_mode : 0x%02X\n"
  12210. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12211. rc,
  12212. ttdata->pip_ver_major,
  12213. ttdata->pip_ver_minor,
  12214. ttdata->fw_pid,
  12215. ttdata->fw_ver_major,
  12216. ttdata->fw_ver_minor,
  12217. ttdata->revctrl,
  12218. ttdata->fw_ver_conf,
  12219. ttdata->bl_ver_major,
  12220. ttdata->bl_ver_minor,
  12221. ttdata->jtag_id_h,
  12222. ttdata->jtag_id_l,
  12223. ttdata->mfg_id[0],
  12224. ttdata->mfg_id[1],
  12225. ttdata->mfg_id[2],
  12226. ttdata->mfg_id[3],
  12227. ttdata->mfg_id[4],
  12228. ttdata->mfg_id[5],
  12229. ttdata->mfg_id[6],
  12230. ttdata->mfg_id[7],
  12231. ttdata->post_code,
  12232. scd->electrodes_x,
  12233. scd->electrodes_y,
  12234. scd->len_x,
  12235. scd->len_y,
  12236. scd->res_x,
  12237. scd->res_y,
  12238. scd->max_z,
  12239. scd->origin_x,
  12240. scd->origin_y,
  12241. scd->panel_id,
  12242. scd->btn,
  12243. scd->scan_mode,
  12244. scd->max_tch);
  12245. } else {
  12246. return scnprintf(buf, strlen(buf),
  12247. "Status: %d\n",
  12248. rc);
  12249. }
  12250. }
  12251. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12252. /*******************************************************************************
  12253. * FUNCTION: pt_hw_reset_show
  12254. *
  12255. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12256. * by toggling the XRES line and then calls the startup function to
  12257. * allow TTDL to re-enumerate the DUT.
  12258. * The printed value reflects the status of the full reset/enum.
  12259. *
  12260. * PARAMETERS:
  12261. * *dev - pointer to Device structure
  12262. * *attr - pointer to the device attribute structure
  12263. * *buf - pointer to buffer to print
  12264. ******************************************************************************/
  12265. static ssize_t pt_hw_reset_show(struct device *dev,
  12266. struct device_attribute *attr, char *buf)
  12267. {
  12268. struct pt_core_data *cd = dev_get_drvdata(dev);
  12269. int rc = 0;
  12270. int time = 0;
  12271. u8 reset_status = 0;
  12272. int t;
  12273. struct pt_hid_desc hid_desc;
  12274. memset(&hid_desc, 0, sizeof(hid_desc));
  12275. /* Only allow DUT reset if no active BL in progress */
  12276. mutex_lock(&cd->firmware_class_lock);
  12277. mutex_lock(&cd->system_lock);
  12278. cd->startup_state = STARTUP_NONE;
  12279. mutex_unlock(&(cd->system_lock));
  12280. pt_stop_wd_timer(cd);
  12281. /* ensure no left over exclusive access is still locked */
  12282. release_exclusive(cd, cd->dev);
  12283. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12284. if (rc) {
  12285. mutex_unlock(&cd->firmware_class_lock);
  12286. pt_debug(cd->dev, DL_ERROR,
  12287. "%s: HW reset failed rc = %d\n", __func__, rc);
  12288. goto exit_hw_reset;
  12289. }
  12290. reset_status |= 0x01 << 0;
  12291. if (cd->flashless_dut) {
  12292. mutex_unlock(&cd->firmware_class_lock);
  12293. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12294. msecs_to_jiffies(200));
  12295. if (IS_TMO(t)) {
  12296. pt_debug(dev, DL_ERROR,
  12297. "%s: Timeout waiting for FW update",
  12298. __func__);
  12299. rc = -ETIME;
  12300. goto exit_hw_reset;
  12301. } else {
  12302. pt_debug(dev, DL_INFO,
  12303. "%s: ----- Wait FW Loading ----",
  12304. __func__);
  12305. rc = _pt_request_wait_for_enum_state(
  12306. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12307. if (rc) {
  12308. pt_debug(cd->dev, DL_ERROR,
  12309. "%s: No FW Sentinel detected rc = %d\n",
  12310. __func__, rc);
  12311. goto exit_hw_reset;
  12312. }
  12313. reset_status |= 0x01 << 1;
  12314. }
  12315. } else {
  12316. /* Wait for any sentinel */
  12317. rc = _pt_request_wait_for_enum_state(dev, 150,
  12318. STARTUP_STATUS_BL_RESET_SENTINEL |
  12319. STARTUP_STATUS_FW_RESET_SENTINEL);
  12320. if (rc) {
  12321. mutex_unlock(&cd->firmware_class_lock);
  12322. pt_debug(cd->dev, DL_ERROR,
  12323. "%s: No Sentinel detected rc = %d\n",
  12324. __func__, rc);
  12325. goto exit_hw_reset;
  12326. }
  12327. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12328. msleep(30);
  12329. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12330. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12331. if (rc < 0) {
  12332. pt_debug(cd->dev, DL_ERROR,
  12333. "%s: Error on getting HID descriptor r=%d\n",
  12334. __func__, rc);
  12335. goto exit_hw_reset;
  12336. }
  12337. cd->mode = pt_get_mode(cd, &hid_desc);
  12338. if (cd->mode == PT_MODE_BOOTLOADER)
  12339. rc = pt_hid_output_bl_launch_app_(cd);
  12340. } else {
  12341. if (cd->mode == PT_MODE_BOOTLOADER)
  12342. rc = pt_pip2_launch_app(dev,
  12343. PT_CORE_CMD_UNPROTECTED);
  12344. }
  12345. if (rc) {
  12346. mutex_unlock(&cd->firmware_class_lock);
  12347. pt_debug(cd->dev, DL_ERROR,
  12348. "%s: PIP launch app failed rc = %d\n",
  12349. __func__, rc);
  12350. goto exit_hw_reset;
  12351. }
  12352. mutex_unlock(&cd->firmware_class_lock);
  12353. reset_status |= 0x01 << 1;
  12354. msleep(20);
  12355. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12356. (cd->mode != PT_MODE_OPERATIONAL))
  12357. pt_queue_restart(cd);
  12358. else
  12359. pt_queue_enum(cd);
  12360. }
  12361. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12362. msleep(50);
  12363. pt_debug(cd->dev, DL_INFO,
  12364. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12365. __func__, time, STARTUP_STATUS_COMPLETE,
  12366. cd->startup_status);
  12367. time += 50;
  12368. }
  12369. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12370. rc = -ETIME;
  12371. goto exit_hw_reset;
  12372. }
  12373. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12374. __func__, cd->startup_status);
  12375. reset_status |= 0x01 << 2;
  12376. pt_start_wd_timer(cd);
  12377. exit_hw_reset:
  12378. return scnprintf(buf, strlen(buf),
  12379. "Status: %d\n"
  12380. "Reset Status: 0x%02X\n", rc, reset_status);
  12381. }
  12382. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12383. /*******************************************************************************
  12384. * FUNCTION: pt_pip2_cmd_rsp_store
  12385. *
  12386. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12387. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12388. * Command byte order:
  12389. * Byte [0] - PIP2 command ID
  12390. * Byte [1-n] - PIP2 command payload
  12391. *
  12392. * RETURN: Size of passed in buffer
  12393. *
  12394. * PARAMETERS:
  12395. * *dev - pointer to device structure
  12396. * *attr - pointer to device attributes
  12397. * *buf - pointer to buffer that hold the command parameters
  12398. * size - size of buf
  12399. ******************************************************************************/
  12400. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12401. struct device_attribute *attr, const char *buf, size_t size)
  12402. {
  12403. struct pt_core_data *cd = dev_get_drvdata(dev);
  12404. u16 actual_read_len;
  12405. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12406. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12407. u8 pip2_cmd_id = 0x00;
  12408. u8 *pip2_cmd_data = NULL;
  12409. int data_len = 0;
  12410. int length;
  12411. int rc = 0;
  12412. /* clear shared data */
  12413. mutex_lock(&cd->sysfs_lock);
  12414. cd->raw_cmd_status = 0;
  12415. cd->cmd_rsp_buf_len = 0;
  12416. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12417. mutex_unlock(&cd->sysfs_lock);
  12418. length = _pt_ic_parse_input_hex(dev, buf, size,
  12419. input_data, PT_MAX_PIP2_MSG_SIZE);
  12420. if (length <= 0 || length > (PT_MAX_PIP2_MSG_SIZE - 2)) {
  12421. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12422. __func__);
  12423. rc = -EINVAL;
  12424. goto exit;
  12425. }
  12426. /* Send PIP2 command if enough data was provided */
  12427. if (length >= 1) {
  12428. pip2_cmd_id = input_data[0];
  12429. pip2_cmd_data = &input_data[1];
  12430. data_len = length - 1;
  12431. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12432. pip2_cmd_id, pip2_cmd_data, data_len,
  12433. read_buf, &actual_read_len);
  12434. cd->raw_cmd_status = rc;
  12435. if (rc) {
  12436. pt_debug(dev, DL_ERROR,
  12437. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12438. __func__, pip2_cmd_id, rc);
  12439. goto exit;
  12440. } else {
  12441. cd->cmd_rsp_buf_len = actual_read_len;
  12442. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12443. pt_debug(dev, DL_ERROR,
  12444. "%s: PIP2 actual_read_len = %d\n",
  12445. __func__, actual_read_len);
  12446. }
  12447. } else {
  12448. rc = -EINVAL;
  12449. pt_debug(dev, DL_ERROR,
  12450. "%s: Insufficient data provided for PIP2 cmd\n",
  12451. __func__);
  12452. }
  12453. exit:
  12454. if (rc)
  12455. return rc;
  12456. return size;
  12457. }
  12458. /*******************************************************************************
  12459. * FUNCTION: pt_pip2_cmd_rsp_show
  12460. *
  12461. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12462. * response generated after using the store method of the pip2_cmd_rsp
  12463. * sysfs node, are available to be read here.
  12464. *
  12465. * PARAMETERS:
  12466. * *dev - pointer to Device structure
  12467. * *attr - pointer to the device attribute structure
  12468. * *buf - pointer to buffer to print
  12469. ******************************************************************************/
  12470. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12471. struct device_attribute *attr, char *buf)
  12472. {
  12473. struct pt_core_data *cd = dev_get_drvdata(dev);
  12474. int i;
  12475. ssize_t data_len;
  12476. int index;
  12477. mutex_lock(&cd->sysfs_lock);
  12478. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12479. "Status: %d\n", cd->raw_cmd_status);
  12480. if (cd->raw_cmd_status)
  12481. goto error;
  12482. /* Remove the CRC from the length of the response */
  12483. data_len = cd->cmd_rsp_buf_len - 2;
  12484. /* Start printing from the data payload */
  12485. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12486. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12487. "%02X ", cd->cmd_rsp_buf[i]);
  12488. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12489. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12490. "\n(%zd bytes)\n",
  12491. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12492. } else {
  12493. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12494. "\n(%zd bytes)\n", 0);
  12495. }
  12496. error:
  12497. mutex_unlock(&cd->sysfs_lock);
  12498. return index;
  12499. }
  12500. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12501. pt_pip2_cmd_rsp_store);
  12502. /*******************************************************************************
  12503. * FUNCTION: pt_command_store
  12504. *
  12505. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12506. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12507. * TTDL will not parse the command.
  12508. *
  12509. * RETURN: Size of passed in buffer
  12510. *
  12511. * PARAMETERS:
  12512. * *dev - pointer to device structure
  12513. * *attr - pointer to device attributes
  12514. * *buf - pointer to buffer that hold the command parameters
  12515. * size - size of buf
  12516. ******************************************************************************/
  12517. static ssize_t pt_command_store(struct device *dev,
  12518. struct device_attribute *attr, const char *buf, size_t size)
  12519. {
  12520. struct pt_core_data *cd = dev_get_drvdata(dev);
  12521. unsigned short crc;
  12522. u16 actual_read_len;
  12523. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12524. int length;
  12525. int len_field;
  12526. int rc = 0;
  12527. mutex_lock(&cd->sysfs_lock);
  12528. cd->cmd_rsp_buf_len = 0;
  12529. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12530. mutex_unlock(&cd->sysfs_lock);
  12531. length = _pt_ic_parse_input_hex(dev, buf, size,
  12532. input_data, PT_MAX_PIP2_MSG_SIZE);
  12533. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12534. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12535. __func__);
  12536. rc = -EINVAL;
  12537. goto pt_command_store_exit;
  12538. }
  12539. /* PIP2 messages begin with 01 01 */
  12540. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12541. cd->pip2_prot_active = 1;
  12542. /* Override next seq tag with what was sent */
  12543. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12544. /* For PIP2 cmd if length does not include crc, add it */
  12545. len_field = (input_data[3] << 8) | input_data[2];
  12546. if (len_field == length && length <= 254) {
  12547. crc = crc_ccitt_calculate(&input_data[2],
  12548. length - 2);
  12549. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12550. __func__, length, crc);
  12551. input_data[length] = (crc & 0xFF00) >> 8;
  12552. input_data[length + 1] = crc & 0x00FF;
  12553. length = length + 2;
  12554. }
  12555. }
  12556. /* write PIP command to log */
  12557. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12558. pm_runtime_get_sync(dev);
  12559. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12560. length, input_data, &actual_read_len);
  12561. pm_runtime_put(dev);
  12562. mutex_lock(&cd->sysfs_lock);
  12563. cd->raw_cmd_status = rc;
  12564. if (rc) {
  12565. cd->cmd_rsp_buf_len = 0;
  12566. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12567. __func__, buf);
  12568. } else {
  12569. cd->cmd_rsp_buf_len = actual_read_len;
  12570. }
  12571. cd->pip2_prot_active = 0;
  12572. mutex_unlock(&cd->sysfs_lock);
  12573. pt_command_store_exit:
  12574. if (rc)
  12575. return rc;
  12576. return size;
  12577. }
  12578. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12579. /*******************************************************************************
  12580. * FUNCTION: pt_response_show
  12581. *
  12582. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12583. * response generated after using the pt_command_store sysfs node, are
  12584. * available to be read here.
  12585. *
  12586. * PARAMETERS:
  12587. * *dev - pointer to Device structure
  12588. * *attr - pointer to the device attribute structure
  12589. * *buf - pointer to buffer to print
  12590. ******************************************************************************/
  12591. static ssize_t pt_response_show(struct device *dev,
  12592. struct device_attribute *attr, char *buf)
  12593. {
  12594. struct pt_core_data *cd = dev_get_drvdata(dev);
  12595. int i;
  12596. ssize_t num_read;
  12597. int index;
  12598. mutex_lock(&cd->sysfs_lock);
  12599. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12600. "Status: %d\n", cd->raw_cmd_status);
  12601. if (cd->raw_cmd_status)
  12602. goto error;
  12603. num_read = cd->cmd_rsp_buf_len;
  12604. for (i = 0; i < num_read; i++)
  12605. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12606. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12607. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12608. "(%zd bytes)\n", num_read);
  12609. error:
  12610. mutex_unlock(&cd->sysfs_lock);
  12611. return index;
  12612. }
  12613. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12614. /*******************************************************************************
  12615. * FUNCTION: pt_dut_debug_show
  12616. *
  12617. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12618. * are available for the store method.
  12619. *
  12620. * RETURN:
  12621. * 0 = success
  12622. * !0 = failure
  12623. *
  12624. * PARAMETERS:
  12625. * *dev - pointer to device structure
  12626. * *attr - pointer to device attributes
  12627. * *buf - pointer to output buffer
  12628. ******************************************************************************/
  12629. static ssize_t pt_dut_debug_show(struct device *dev,
  12630. struct device_attribute *attr, char *buf)
  12631. {
  12632. ssize_t ret;
  12633. ret = scnprintf(buf, strlen(buf),
  12634. "Status: 0\n"
  12635. "dut_debug sends the following commands to the DUT:\n"
  12636. "%d %s \t- %s\n"
  12637. "%d %s \t- %s\n"
  12638. "%d %s \t- %s\n"
  12639. "%d %s \t- %s\n"
  12640. "%d %s \t- %s\n"
  12641. "%d %s \t- %s\n"
  12642. "%d %s \t- %s\n"
  12643. "%d %s \t- %s\n"
  12644. "%d %s \t- %s\n"
  12645. "%d %s \t- %s\n"
  12646. "%d %s \t- %s\n"
  12647. "%d %s \t- %s\n"
  12648. "%d %s \t- %s\n"
  12649. "%d %s \t- %s\n"
  12650. "%d %s \t- %s\n"
  12651. "%d %s \t- %s\n"
  12652. "%d %s \t- %s\n"
  12653. ,
  12654. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12655. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12656. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12657. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12658. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12659. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12660. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12661. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12662. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12663. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12664. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12665. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12666. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12667. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12668. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12669. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12670. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12671. );
  12672. return ret;
  12673. }
  12674. /*******************************************************************************
  12675. * FUNCTION: pt_drv_debug_show
  12676. *
  12677. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12678. * are available for the store method.
  12679. *
  12680. * RETURN:
  12681. * 0 = success
  12682. * !0 = failure
  12683. *
  12684. * PARAMETERS:
  12685. * *dev - pointer to device structure
  12686. * *attr - pointer to device attributes
  12687. * *buf - pointer to output buffer
  12688. ******************************************************************************/
  12689. static ssize_t pt_drv_debug_show(struct device *dev,
  12690. struct device_attribute *attr, char *buf)
  12691. {
  12692. ssize_t ret;
  12693. ret = scnprintf(buf, strlen(buf),
  12694. "Status: 0\n"
  12695. "drv_debug supports the following values:\n"
  12696. "%d %s \t- %s\n"
  12697. "%d %s \t- %s\n"
  12698. "%d %s \t- %s\n"
  12699. "%d %s \t- %s\n"
  12700. "%d %s \t- %s\n"
  12701. "%d %s \t- %s\n"
  12702. "%d %s \t- %s\n"
  12703. "%d %s \t- %s\n"
  12704. "%d %s \t- %s\n"
  12705. "%d %s - %s\n"
  12706. "%d %s \t- %s\n"
  12707. "%d %s \t- %s\n"
  12708. #ifdef TTDL_DIAGNOSTICS
  12709. "%d %s \t- %s\n"
  12710. "%d %s \t- %s\n"
  12711. "%d %s \t- %s\n"
  12712. #endif /* TTDL_DIAGNOSTICS */
  12713. "%d %s \t- %s\n"
  12714. #ifdef TTDL_DIAGNOSTICS
  12715. "%d %s \t- %s\n"
  12716. "%d %s \t- %s\n"
  12717. "%d %s \t- %s\n"
  12718. "%d %s \t- %s\n"
  12719. "%d %s \t- %s\n"
  12720. "%d %s \t- %s\n"
  12721. "%d %s \t- %s\n"
  12722. "%d %s \t- %s\n"
  12723. "%d %s \t- %s\n"
  12724. #endif /* TTDL_DIAGNOSTICS */
  12725. ,
  12726. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12727. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12728. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12729. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12730. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12731. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12732. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12733. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12734. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12735. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12736. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12737. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12738. #ifdef TTDL_DIAGNOSTICS
  12739. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12740. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12741. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12742. #endif /* TTDL_DIAGNOSTICS */
  12743. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12744. #ifdef TTDL_DIAGNOSTICS
  12745. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12746. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12747. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12748. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12749. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12750. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12751. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12752. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12753. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12754. "TTHE_TUNER HID USB Format"
  12755. #endif /* TTDL_DIAGNOSTICS */
  12756. );
  12757. return ret;
  12758. }
  12759. /*******************************************************************************
  12760. * FUNCTION: pt_drv_debug_store
  12761. *
  12762. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12763. * dut_debug. Drv_debug will contain all functionality that can be run
  12764. * without a DUT preset and is available anytime TTDL is running.
  12765. * Dut_debug requires a DUT to be available and will only be created after
  12766. * a DUT has been detected.
  12767. * This function will eventually be split into two but until the overlap
  12768. * has been depricated this function contains all commands that can be
  12769. * used for TTDL/DUT debugging status and control.
  12770. * All commands require at least one value to be passed in *buf with some
  12771. * requiring two.
  12772. *
  12773. * RETURN: Size of passed in buffer
  12774. *
  12775. * PARAMETERS:
  12776. * *dev - pointer to device structure
  12777. * *attr - pointer to device attributes
  12778. * *buf - pointer to buffer that hold the command parameters
  12779. * size - size of buf
  12780. ******************************************************************************/
  12781. static ssize_t pt_drv_debug_store(struct device *dev,
  12782. struct device_attribute *attr, const char *buf, size_t size)
  12783. {
  12784. struct pt_core_data *cd = dev_get_drvdata(dev);
  12785. unsigned long value;
  12786. int rc = 0;
  12787. u8 return_data[8];
  12788. static u8 wd_disabled;
  12789. u32 input_data[3];
  12790. int length;
  12791. #ifdef TTDL_DIAGNOSTICS
  12792. struct i2c_client *client = to_i2c_client(dev);
  12793. unsigned short crc = 0;
  12794. u16 cal_size;
  12795. #endif
  12796. input_data[0] = 0;
  12797. input_data[1] = 0;
  12798. /* Maximmum input is two elements */
  12799. length = _pt_ic_parse_input(dev, buf, size,
  12800. input_data, ARRAY_SIZE(input_data));
  12801. if (length < 1 || length > 2) {
  12802. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12803. __func__);
  12804. rc = -EINVAL;
  12805. goto pt_drv_debug_store_exit;
  12806. }
  12807. value = input_data[0];
  12808. if (length == 1) {
  12809. pt_debug(dev, DL_DEBUG,
  12810. "%s: Debug Cmd Received (id=%d)\n",
  12811. __func__, input_data[0]);
  12812. } else if (length == 2) {
  12813. pt_debug(dev, DL_DEBUG,
  12814. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12815. __func__, input_data[0], input_data[1]);
  12816. } else {
  12817. pt_debug(dev, DL_DEBUG,
  12818. "%s: Invalid arguments received\n", __func__);
  12819. rc = -EINVAL;
  12820. goto pt_drv_debug_store_exit;
  12821. }
  12822. /* Start watchdog timer command */
  12823. if (value == PT_DRV_DBG_START_WD) {
  12824. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12825. wd_disabled = 0;
  12826. cd->watchdog_force_stop = false;
  12827. pt_start_wd_timer(cd);
  12828. goto pt_drv_debug_store_exit;
  12829. }
  12830. /* Stop watchdog timer temporarily */
  12831. pt_stop_wd_timer(cd);
  12832. if (value == PT_DRV_DBG_STOP_WD) {
  12833. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12834. wd_disabled = 1;
  12835. cd->watchdog_force_stop = true;
  12836. goto pt_drv_debug_store_exit;
  12837. }
  12838. switch (value) {
  12839. case PT_DRV_DBG_SUSPEND: /* 4 */
  12840. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12841. wd_disabled = 1;
  12842. rc = pt_core_suspend_(cd->dev);
  12843. if (rc)
  12844. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12845. __func__, rc);
  12846. else {
  12847. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12848. __func__);
  12849. cd->drv_debug_suspend = true;
  12850. pt_debug(dev, DL_INFO, "%s: Debugfs flag set:\n", __func__);
  12851. }
  12852. break;
  12853. case PT_DRV_DBG_RESUME: /* 5 */
  12854. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12855. rc = pt_core_resume_(cd->dev);
  12856. if (rc)
  12857. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12858. __func__, rc);
  12859. else {
  12860. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12861. __func__);
  12862. cd->drv_debug_suspend = false;
  12863. pt_debug(dev, DL_INFO, "%s: Debugfs flag reset:\n", __func__);
  12864. }
  12865. break;
  12866. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12867. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12868. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12869. break;
  12870. case PT_DUT_DBG_HID_RESET: /* 50 */
  12871. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12872. pt_hid_cmd_reset(cd);
  12873. break;
  12874. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12875. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12876. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12877. wd_disabled = 0;
  12878. break;
  12879. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12880. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12881. __func__);
  12882. wd_disabled = 1;
  12883. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12884. break;
  12885. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12886. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12887. __func__);
  12888. wd_disabled = 1;
  12889. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12890. break;
  12891. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12892. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12893. pt_hid_output_bl_get_information(cd, return_data);
  12894. break;
  12895. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12896. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12897. __func__);
  12898. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12899. break;
  12900. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12901. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12902. pt_hid_output_bl_launch_app(cd);
  12903. break;
  12904. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12905. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12906. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12907. break;
  12908. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12909. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12910. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12911. break;
  12912. case PT_DUT_DBG_RESET: /* 98 */
  12913. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12914. rc = pt_hw_hard_reset(cd);
  12915. break;
  12916. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12917. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12918. pt_pip_null(cd);
  12919. break;
  12920. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12921. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12922. rc = pt_pip_start_bootloader(cd);
  12923. if (!rc) {
  12924. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12925. cd->mode = PT_MODE_BOOTLOADER;
  12926. }
  12927. break;
  12928. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12929. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12930. pt_hid_output_get_sysinfo(cd);
  12931. break;
  12932. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12933. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12934. pt_pip_suspend_scanning(cd);
  12935. break;
  12936. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12937. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12938. pt_pip_resume_scanning(cd);
  12939. break;
  12940. #ifdef TTHE_TUNER_SUPPORT
  12941. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12942. cd->tthe_exit = 1;
  12943. wake_up(&cd->wait_q);
  12944. kfree(cd->tthe_buf);
  12945. cd->tthe_buf = NULL;
  12946. cd->tthe_exit = 0;
  12947. break;
  12948. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12949. if (cd->tthe_buf)
  12950. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12951. else
  12952. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12953. __func__);
  12954. break;
  12955. #endif
  12956. #ifdef TTDL_DIAGNOSTICS
  12957. case PT_DUT_DBG_HID_DESC: /* 109 */
  12958. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12959. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12960. break;
  12961. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12962. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12963. __func__);
  12964. pt_erase_parameter_list(cd);
  12965. break;
  12966. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12967. rc = pt_read_input(cd);
  12968. if (!rc)
  12969. pt_parse_input(cd);
  12970. break;
  12971. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12972. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12973. &cal_size, &crc);
  12974. if (rc)
  12975. pt_debug(dev, DL_ERROR,
  12976. "%s: CAL Data clear failed rc=%d\n",
  12977. __func__, rc);
  12978. else
  12979. pt_debug(dev, DL_INFO,
  12980. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12981. __func__, crc, size);
  12982. break;
  12983. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12984. mutex_lock(&cd->system_lock);
  12985. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12986. cd->debug_level = input_data[1];
  12987. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12988. __func__, cd->debug_level);
  12989. } else {
  12990. rc = -EINVAL;
  12991. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12992. __func__, input_data[1]);
  12993. }
  12994. mutex_unlock(&(cd->system_lock));
  12995. break;
  12996. #endif
  12997. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12998. mutex_lock(&cd->system_lock);
  12999. if (input_data[1] > 100) {
  13000. cd->watchdog_interval = input_data[1];
  13001. pt_debug(dev, DL_INFO,
  13002. "%s: Set watchdog_ interval to: %d\n",
  13003. __func__, cd->watchdog_interval);
  13004. pt_start_wd_timer(cd);
  13005. } else {
  13006. rc = -EINVAL;
  13007. pt_debug(dev, DL_ERROR,
  13008. "%s: Invalid watchdog interval: %d\n",
  13009. __func__, input_data[1]);
  13010. }
  13011. mutex_unlock(&(cd->system_lock));
  13012. break;
  13013. #ifdef TTDL_DIAGNOSTICS
  13014. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  13015. mutex_lock(&cd->system_lock);
  13016. if (input_data[1] == 0) {
  13017. cd->show_timestamp = 0;
  13018. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  13019. __func__);
  13020. } else if (input_data[1] == 1) {
  13021. cd->show_timestamp = 1;
  13022. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  13023. __func__);
  13024. } else {
  13025. rc = -EINVAL;
  13026. pt_debug(dev, DL_ERROR,
  13027. "%s: Invalid parameter: %d\n",
  13028. __func__, input_data[1]);
  13029. }
  13030. mutex_unlock(&(cd->system_lock));
  13031. break;
  13032. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  13033. if (input_data[1] == 0) {
  13034. cd->cpdata->setup_power(cd->cpdata,
  13035. PT_MT_POWER_OFF, cd->dev);
  13036. pt_debug(dev, DL_INFO,
  13037. "%s: Initiate Power Off\n", __func__);
  13038. } else if (input_data[1] == 1) {
  13039. cd->cpdata->setup_power(cd->cpdata,
  13040. PT_MT_POWER_ON, cd->dev);
  13041. pt_debug(dev, DL_INFO,
  13042. "%s: Initiate Power On\n", __func__);
  13043. } else {
  13044. rc = -EINVAL;
  13045. pt_debug(dev, DL_ERROR,
  13046. "%s: Invalid parameter: %d\n",
  13047. __func__, input_data[1]);
  13048. }
  13049. break;
  13050. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  13051. if (input_data[1] == 0) {
  13052. pm_runtime_put(dev);
  13053. pt_debug(dev, DL_ERROR,
  13054. "%s: Force call pm_runtime_put()\n", __func__);
  13055. } else if (input_data[1] == 1) {
  13056. pm_runtime_get_sync(dev);
  13057. pt_debug(dev, DL_ERROR,
  13058. "%s: Force call pm_runtime_get_sync()\n",
  13059. __func__);
  13060. } else {
  13061. rc = -EINVAL;
  13062. pt_debug(dev, DL_ERROR,
  13063. "%s: WARNING: Invalid parameter: %d\n",
  13064. __func__, input_data[1]);
  13065. }
  13066. break;
  13067. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  13068. mutex_lock(&cd->system_lock);
  13069. if (input_data[1] == 0) {
  13070. cd->show_tt_data = false;
  13071. pt_debug(dev, DL_INFO,
  13072. "%s: Disable TT_DATA\n", __func__);
  13073. } else if (input_data[1] == 1) {
  13074. cd->show_tt_data = true;
  13075. pt_debug(dev, DL_INFO,
  13076. "%s: Enable TT_DATA\n", __func__);
  13077. } else {
  13078. rc = -EINVAL;
  13079. pt_debug(dev, DL_ERROR,
  13080. "%s: Invalid parameter: %d\n",
  13081. __func__, input_data[1]);
  13082. }
  13083. mutex_unlock(&(cd->system_lock));
  13084. break;
  13085. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  13086. if (input_data[1] == cd->active_dut_generation) {
  13087. mutex_lock(&cd->system_lock);
  13088. cd->set_dut_generation = true;
  13089. mutex_unlock(&(cd->system_lock));
  13090. } else {
  13091. mutex_lock(&cd->system_lock);
  13092. if (input_data[1] == 0) {
  13093. cd->active_dut_generation = DUT_UNKNOWN;
  13094. cd->set_dut_generation = false;
  13095. } else if (input_data[1] == 1) {
  13096. cd->active_dut_generation = DUT_PIP1_ONLY;
  13097. cd->set_dut_generation = true;
  13098. } else if (input_data[1] == 2) {
  13099. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  13100. cd->set_dut_generation = true;
  13101. } else {
  13102. rc = -EINVAL;
  13103. pt_debug(dev, DL_ERROR,
  13104. "%s: Invalid parameter: %d\n",
  13105. __func__, input_data[1]);
  13106. break;
  13107. }
  13108. cd->startup_status = STARTUP_STATUS_START;
  13109. pt_debug(cd->dev, DL_DEBUG,
  13110. "%s: Startup Status Reset\n", __func__);
  13111. mutex_unlock(&(cd->system_lock));
  13112. pt_debug(dev, DL_INFO,
  13113. "%s: Active DUT Generation Set to: %d\n",
  13114. __func__, cd->active_dut_generation);
  13115. /* Changing DUT generations full restart needed */
  13116. pt_queue_restart(cd);
  13117. }
  13118. break;
  13119. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  13120. mutex_lock(&cd->system_lock);
  13121. if (input_data[1] == 0) {
  13122. cd->bridge_mode = false;
  13123. pt_debug(dev, DL_INFO,
  13124. "%s: Disable Bridge Mode\n", __func__);
  13125. } else if (input_data[1] == 1) {
  13126. cd->bridge_mode = true;
  13127. pt_debug(dev, DL_INFO,
  13128. "%s: Enable Bridge Mode\n", __func__);
  13129. } else {
  13130. rc = -EINVAL;
  13131. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13132. __func__, input_data[1]);
  13133. }
  13134. mutex_unlock(&(cd->system_lock));
  13135. break;
  13136. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  13137. mutex_lock(&cd->system_lock);
  13138. /* Only a 7bit address is valid */
  13139. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  13140. client->addr = input_data[1];
  13141. pt_debug(dev, DL_INFO,
  13142. "%s: Set I2C Address: 0x%2X\n",
  13143. __func__, client->addr);
  13144. } else {
  13145. rc = -EINVAL;
  13146. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  13147. __func__, input_data[1]);
  13148. client->addr = 0x24;
  13149. }
  13150. mutex_unlock(&(cd->system_lock));
  13151. break;
  13152. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  13153. mutex_lock(&cd->system_lock);
  13154. if (input_data[1] == 0) {
  13155. cd->flashless_dut = 0;
  13156. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  13157. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  13158. __func__);
  13159. } else if (input_data[1] == 1) {
  13160. cd->flashless_dut = 1;
  13161. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  13162. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  13163. __func__);
  13164. }
  13165. mutex_unlock(&(cd->system_lock));
  13166. break;
  13167. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  13168. mutex_lock(&cd->system_lock);
  13169. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  13170. cd->force_pip2_seq = input_data[1];
  13171. cd->pip2_cmd_tag_seq = input_data[1];
  13172. pt_debug(dev, DL_INFO,
  13173. "%s: Force PIP2 Seq to: 0x%02X\n",
  13174. __func__, input_data[1]);
  13175. } else {
  13176. cd->force_pip2_seq = 0;
  13177. pt_debug(dev, DL_INFO,
  13178. "%s: Clear Forced PIP2 Seq\n", __func__);
  13179. }
  13180. mutex_unlock(&(cd->system_lock));
  13181. break;
  13182. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13183. mutex_lock(&cd->system_lock);
  13184. if (input_data[1] == 0) {
  13185. cd->bl_with_no_int = 0;
  13186. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13187. } else if (input_data[1] == 1) {
  13188. cd->bl_with_no_int = 1;
  13189. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13190. __func__);
  13191. } else {
  13192. rc = -EINVAL;
  13193. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13194. __func__, input_data[1]);
  13195. }
  13196. mutex_unlock(&(cd->system_lock));
  13197. break;
  13198. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13199. mutex_lock(&cd->system_lock);
  13200. if (input_data[1] == 0) {
  13201. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13202. pt_debug(dev, DL_INFO,
  13203. "%s: Disable Calibration cache in host\n",
  13204. __func__);
  13205. } else if (input_data[1] == 1) {
  13206. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13207. pt_debug(dev, DL_INFO,
  13208. "%s: Enable Calibration cache in host\n",
  13209. __func__);
  13210. } else {
  13211. rc = -EINVAL;
  13212. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13213. __func__, input_data[1]);
  13214. }
  13215. mutex_unlock(&(cd->system_lock));
  13216. break;
  13217. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13218. mutex_lock(&cd->system_lock);
  13219. if (input_data[1] == 0) {
  13220. cd->multi_chip = PT_FEATURE_DISABLE;
  13221. cd->ttdl_bist_select = 0x07;
  13222. pt_debug(dev, DL_INFO,
  13223. "%s: Disable Multi-chip support\n", __func__);
  13224. } else if (input_data[1] == 1) {
  13225. cd->multi_chip = PT_FEATURE_ENABLE;
  13226. cd->ttdl_bist_select = 0x3F;
  13227. pt_debug(dev, DL_INFO,
  13228. "%s: Enable Multi-chip support\n", __func__);
  13229. } else {
  13230. rc = -EINVAL;
  13231. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13232. __func__, input_data[1]);
  13233. }
  13234. mutex_unlock(&(cd->system_lock));
  13235. break;
  13236. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13237. mutex_lock(&cd->system_lock);
  13238. if (input_data[1] <= 0x07) {
  13239. cd->panel_id_support = input_data[1];
  13240. pt_debug(dev, DL_INFO,
  13241. "%s: Set panel_id_support to %d\n",
  13242. __func__, cd->panel_id_support);
  13243. } else {
  13244. rc = -EINVAL;
  13245. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13246. __func__, input_data[1]);
  13247. }
  13248. mutex_unlock(&(cd->system_lock));
  13249. break;
  13250. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13251. mutex_lock(&cd->system_lock);
  13252. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13253. /*
  13254. * The timeout is changed for some cases so the
  13255. * pip_cmd_timeout_default is used to retore back to
  13256. * what the user requested as the new timeout.
  13257. */
  13258. cd->pip_cmd_timeout_default = input_data[1];
  13259. cd->pip_cmd_timeout = input_data[1];
  13260. pt_debug(dev, DL_INFO,
  13261. "%s: PIP Timeout = %d\n", __func__,
  13262. cd->pip_cmd_timeout_default);
  13263. } else {
  13264. rc = -EINVAL;
  13265. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13266. __func__, input_data[1]);
  13267. }
  13268. mutex_unlock(&(cd->system_lock));
  13269. break;
  13270. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13271. mutex_lock(&cd->system_lock);
  13272. if (input_data[1] == 0) {
  13273. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13274. pt_debug(dev, DL_INFO,
  13275. "%s: Disable tthe_tuner HID-USB format\n",
  13276. __func__);
  13277. } else if (input_data[1] == 1) {
  13278. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13279. pt_debug(dev, DL_INFO,
  13280. "%s: Enable tthe_tuner HID-USB format\n",
  13281. __func__);
  13282. } else {
  13283. rc = -EINVAL;
  13284. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13285. __func__, input_data[1]);
  13286. }
  13287. mutex_unlock(&(cd->system_lock));
  13288. break;
  13289. #endif /* TTDL_DIAGNOSTICS */
  13290. default:
  13291. rc = -EINVAL;
  13292. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13293. }
  13294. /* Enable watchdog timer */
  13295. if (!wd_disabled)
  13296. pt_start_wd_timer(cd);
  13297. pt_drv_debug_store_exit:
  13298. if (rc)
  13299. return rc;
  13300. return size;
  13301. }
  13302. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13303. pt_drv_debug_store);
  13304. /*******************************************************************************
  13305. * FUNCTION: pt_sleep_status_show
  13306. *
  13307. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13308. * sleep status as either ON or OFF
  13309. *
  13310. * RETURN:
  13311. * 0 = success
  13312. * !0 = failure
  13313. *
  13314. * PARAMETERS:
  13315. * *dev - pointer to device structure
  13316. * *attr - pointer to device attributes
  13317. * *buf - pointer to output buffer
  13318. ******************************************************************************/
  13319. static ssize_t pt_sleep_status_show(struct device *dev,
  13320. struct device_attribute *attr, char *buf)
  13321. {
  13322. struct pt_core_data *cd = dev_get_drvdata(dev);
  13323. ssize_t ret;
  13324. mutex_lock(&cd->system_lock);
  13325. if (cd->sleep_state == SS_SLEEP_ON)
  13326. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13327. else
  13328. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13329. mutex_unlock(&cd->system_lock);
  13330. return ret;
  13331. }
  13332. /*******************************************************************************
  13333. * FUNCTION: pt_panel_id_show
  13334. *
  13335. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13336. * detected panel ID from the DUT
  13337. *
  13338. * RETURN:
  13339. * 0 = success
  13340. * !0 = failure
  13341. *
  13342. * PARAMETERS:
  13343. * *dev - pointer to device structure
  13344. * *attr - pointer to device attributes
  13345. * *buf - pointer to output buffer
  13346. ******************************************************************************/
  13347. static ssize_t pt_panel_id_show(struct device *dev,
  13348. struct device_attribute *attr, char *buf)
  13349. {
  13350. struct pt_core_data *cd = dev_get_drvdata(dev);
  13351. ssize_t ret;
  13352. u8 pid = PANEL_ID_NOT_ENABLED;
  13353. int rc = 0;
  13354. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13355. /*
  13356. * The DUT should report the same panel ID from both the BL and
  13357. * the FW unless the panel_id feature is set to only
  13358. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13359. * to retrieve the panel_id.
  13360. */
  13361. if (cd->mode == PT_MODE_BOOTLOADER) {
  13362. /*
  13363. * Return the stored value if PT_PANEL_ID_BY_BL
  13364. * is not supported while other feature exits.
  13365. */
  13366. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13367. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13368. if (rc) {
  13369. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13370. "Failed to retrieve Panel ID. ",
  13371. "Using cached value\n",
  13372. __func__);
  13373. }
  13374. }
  13375. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13376. if (cd->panel_id_support &
  13377. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13378. /* For all systems sysinfo has the panel_id */
  13379. rc = pt_hid_output_get_sysinfo(cd);
  13380. if (!rc)
  13381. pid =
  13382. cd->sysinfo.sensing_conf_data.panel_id;
  13383. pt_debug(dev, DL_ERROR,
  13384. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13385. __func__, rc, pid);
  13386. }
  13387. } else {
  13388. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13389. __func__);
  13390. rc = -EPERM;
  13391. }
  13392. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13393. if (cd->mode == PT_MODE_BOOTLOADER) {
  13394. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13395. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13396. if (rc) {
  13397. pt_debug(dev, DL_ERROR,
  13398. "%s: BL get panel ID failed rc=%d\n",
  13399. __func__, rc);
  13400. }
  13401. }
  13402. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13403. if (cd->panel_id_support &
  13404. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13405. rc = pt_hid_output_get_sysinfo(cd);
  13406. if (!rc)
  13407. pid =
  13408. cd->sysinfo.sensing_conf_data.panel_id;
  13409. pt_debug(dev, DL_ERROR,
  13410. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13411. __func__, rc, pid);
  13412. }
  13413. } else {
  13414. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13415. __func__);
  13416. rc = -EPERM;
  13417. }
  13418. } else {
  13419. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13420. __func__);
  13421. rc = -EPERM;
  13422. }
  13423. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13424. rc, pid);
  13425. return ret;
  13426. }
  13427. /*******************************************************************************
  13428. * FUNCTION: pt_get_param_store
  13429. *
  13430. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13431. * ID to retrieve with the show method.
  13432. *
  13433. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13434. *
  13435. * RETURN: Size of passed in buffer if successful
  13436. *
  13437. * PARAMETERS:
  13438. * *dev - pointer to device structure
  13439. * *attr - pointer to device attributes
  13440. * *buf - pointer to output buffer
  13441. * size - size of buf
  13442. ******************************************************************************/
  13443. static ssize_t pt_get_param_store(struct device *dev,
  13444. struct device_attribute *attr, const char *buf, size_t size)
  13445. {
  13446. struct pt_core_data *cd = dev_get_drvdata(dev);
  13447. u32 input_data[2];
  13448. int length;
  13449. int rc = 0;
  13450. /* Maximum input of one value */
  13451. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13452. ARRAY_SIZE(input_data));
  13453. if (length != 1) {
  13454. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13455. __func__);
  13456. rc = -EINVAL;
  13457. goto exit;
  13458. }
  13459. mutex_lock(&cd->system_lock);
  13460. cd->get_param_id = input_data[0];
  13461. mutex_unlock(&(cd->system_lock));
  13462. exit:
  13463. if (rc)
  13464. return rc;
  13465. return size;
  13466. }
  13467. /*******************************************************************************
  13468. * FUNCTION: pt_get_param_show
  13469. *
  13470. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13471. * parameter data from the DUT based on the ID stored in the core
  13472. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13473. * communicate or some other error occures, an error status is returned
  13474. * with no value following.
  13475. * Output is in the form:
  13476. * Status: x
  13477. * 0xyyyyyyyy
  13478. * The 32bit data will only follow the status code if the status == 0
  13479. *
  13480. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13481. *
  13482. * RETURN:
  13483. * 0 = success
  13484. * !0 = failure
  13485. *
  13486. * PARAMETERS:
  13487. * *dev - pointer to device structure
  13488. * *attr - pointer to device attributes
  13489. * *buf - pointer to output buffer
  13490. ******************************************************************************/
  13491. static ssize_t pt_get_param_show(struct device *dev,
  13492. struct device_attribute *attr, char *buf)
  13493. {
  13494. struct pt_core_data *cd = dev_get_drvdata(dev);
  13495. ssize_t ret = 0;
  13496. int status;
  13497. u32 value = 0;
  13498. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13499. if (status) {
  13500. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13501. __func__, "pt_get_param", status);
  13502. ret = scnprintf(buf, strlen(buf),
  13503. "%s %d\n",
  13504. "Status:", status);
  13505. } else {
  13506. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13507. __func__, cd->get_param_id, value);
  13508. ret = scnprintf(buf, strlen(buf),
  13509. "Status: %d\n"
  13510. "0x%04X\n",
  13511. status, value);
  13512. }
  13513. return ret;
  13514. }
  13515. /*******************************************************************************
  13516. * FUNCTION: pt_ttdl_restart_show
  13517. *
  13518. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13519. * probed modules, calls startup() and then re-probes modules.
  13520. *
  13521. * RETURN: size of data written to sysfs node
  13522. *
  13523. * PARAMETERS:
  13524. * *dev - pointer to device structure
  13525. * *attr - pointer to device attributes structure
  13526. * *buf - pointer to print output buffer
  13527. ******************************************************************************/
  13528. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13529. struct device_attribute *attr, char *buf)
  13530. {
  13531. struct pt_core_data *cd = dev_get_drvdata(dev);
  13532. int t;
  13533. int rc = 0;
  13534. mutex_lock(&cd->system_lock);
  13535. cd->startup_state = STARTUP_NONE;
  13536. mutex_unlock(&(cd->system_lock));
  13537. /* ensure no left over exclusive access is still locked */
  13538. release_exclusive(cd, cd->dev);
  13539. pt_queue_restart(cd);
  13540. t = wait_event_timeout(cd->wait_q,
  13541. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13542. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13543. if (IS_TMO(t)) {
  13544. pt_debug(cd->dev, DL_ERROR,
  13545. "%s: TMO waiting for FW sentinel\n", __func__);
  13546. rc = -ETIME;
  13547. }
  13548. return scnprintf(buf, strlen(buf),
  13549. "Status: %d\n"
  13550. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13551. }
  13552. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13553. /*******************************************************************************
  13554. * FUNCTION: pt_pip2_gpio_read_show
  13555. *
  13556. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13557. * contents of the response to the passed in output buffer.
  13558. *
  13559. * RETURN: size of data written to sysfs node
  13560. *
  13561. * PARAMETERS:
  13562. * *dev - pointer to device structure
  13563. * *attr - pointer to device attributes structure
  13564. * *buf - pointer to print output buffer
  13565. ******************************************************************************/
  13566. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13567. struct device_attribute *attr, char *buf)
  13568. {
  13569. struct pt_core_data *cd = dev_get_drvdata(dev);
  13570. u8 status = 0;
  13571. u32 gpio_value = 0;
  13572. int rc = 0;
  13573. /* This functionality is only available in the BL */
  13574. if (cd->mode == PT_MODE_BOOTLOADER)
  13575. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13576. else
  13577. rc = -EPERM;
  13578. if (!rc) {
  13579. if (status == 0)
  13580. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13581. "Status: %d\n"
  13582. "DUT GPIO Reg: 0x%08X\n",
  13583. rc, gpio_value);
  13584. else
  13585. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13586. "Status: %d\n"
  13587. "DUT GPIO Reg: n/a\n",
  13588. status);
  13589. } else
  13590. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13591. "Status: %d\n"
  13592. "DUT GPIO Reg: n/a\n",
  13593. rc);
  13594. }
  13595. /*******************************************************************************
  13596. * FUNCTION: pt_device_exit
  13597. *
  13598. * SUMMARY: Remove functon for the I2C module
  13599. *
  13600. * PARAMETERS:
  13601. * *client - pointer to i2c client structure
  13602. ******************************************************************************/
  13603. static int pt_device_exit(struct i2c_client *client)
  13604. {
  13605. struct pt_core_data *cd = i2c_get_clientdata(client);
  13606. struct device *dev = cd->dev;
  13607. pt_debug(dev, DL_INFO,"%s: Start pt_device_exit\n", __func__);
  13608. if (active_panel)
  13609. panel_event_notifier_unregister(&cd->fb_notifier);
  13610. pt_core_state = STATE_SUSPEND;
  13611. pm_runtime_suspend(dev);
  13612. pm_runtime_disable(dev);
  13613. pt_stop_wd_timer(cd);
  13614. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  13615. cancel_work_sync(&cd->ttdl_restart_work);
  13616. cancel_work_sync(&cd->enum_work);
  13617. cancel_work_sync(&cd->resume_offload_work);
  13618. cancel_work_sync(&cd->suspend_offload_work);
  13619. cancel_work_sync(&cd->resume_work);
  13620. cancel_work_sync(&cd->suspend_work);
  13621. pt_stop_wd_timer(cd);
  13622. device_init_wakeup(dev, 0);
  13623. disable_irq_nosync(cd->irq);
  13624. if (cd->cpdata->setup_irq)
  13625. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  13626. if (cd->cpdata->init)
  13627. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  13628. if (cd->cpdata->setup_power)
  13629. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  13630. pt_debug(dev, DL_INFO,"%s: End pt_device_exit \n", __func__);
  13631. return 0;
  13632. }
  13633. /*******************************************************************************
  13634. * FUNCTION: pt_touch_offload_store
  13635. *
  13636. * SUMMARY: The store method for the touch_offload sysfs node that allows the TTDL
  13637. * to be enabled/disabled.
  13638. *
  13639. * RETURN: Size of passed in buffer
  13640. *
  13641. * PARAMETERS:
  13642. * *dev - pointer to device structure
  13643. * *attr - pointer to device attributes
  13644. * *buf - pointer to buffer that hold the command parameters
  13645. * size - size of buf
  13646. ******************************************************************************/
  13647. static ssize_t pt_touch_offload_store(struct device *dev,
  13648. struct device_attribute *attr, const char *buf, size_t size)
  13649. {
  13650. struct pt_core_data *cd = dev_get_drvdata(dev);
  13651. struct i2c_client *client = to_i2c_client(dev);
  13652. u32 input_data[2];
  13653. int length;
  13654. int rc = 0;
  13655. /* Maximum input of one value */
  13656. length = _pt_ic_parse_input(dev, buf, size, input_data, ARRAY_SIZE(input_data));
  13657. if (length != 1) {
  13658. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n", __func__);
  13659. rc = -EINVAL;
  13660. goto exit;
  13661. }
  13662. switch (input_data[0]) {
  13663. case 0:
  13664. pt_debug(dev, DL_ERROR, "%s: TTDL: Core Touch Offload OFF\n", __func__);
  13665. cd->touch_offload = true;
  13666. rc = pt_device_exit(client);
  13667. if (rc)
  13668. pt_debug(dev, DL_ERROR, "%s: Power off error detected rc=%d\n",
  13669. __func__, rc);
  13670. else {
  13671. cd->touch_offload = true;
  13672. pt_debug(dev, DL_ERROR, "%s: Debugfs PT DEVICE EXIT flag set:\n",
  13673. __func__);
  13674. }
  13675. break;
  13676. case 1:
  13677. pt_debug(dev, DL_ERROR, "%s: TTDL: Core Touch Offload ON\n", __func__);
  13678. rc = pt_device_entry(&client->dev, client->irq, PT_DATA_SIZE);
  13679. if (rc)
  13680. pt_debug(dev, DL_ERROR, "%s: Power on error detected rc=%d\n",
  13681. __func__, rc);
  13682. else {
  13683. cd->touch_offload = false;
  13684. pt_debug(dev, DL_ERROR, "%s: Debugfs PT DEVICE ENTRY flag set:\n",
  13685. __func__);
  13686. }
  13687. break;
  13688. default:
  13689. rc = -EINVAL;
  13690. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13691. }
  13692. exit:
  13693. if (rc)
  13694. return rc;
  13695. return size;
  13696. }
  13697. /*******************************************************************************
  13698. * FUNCTION: pt_pip2_version_show
  13699. *
  13700. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13701. * contents of the response to the passed in output buffer.
  13702. *
  13703. * RETURN: size of data written to sysfs node
  13704. *
  13705. * PARAMETERS:
  13706. * *dev - pointer to device structure
  13707. * *attr - pointer to device attributes structure
  13708. * *buf - pointer to print output buffer
  13709. ******************************************************************************/
  13710. static ssize_t pt_pip2_version_show(struct device *dev,
  13711. struct device_attribute *attr, char *buf)
  13712. {
  13713. int rc = 0;
  13714. struct pt_core_data *cd = dev_get_drvdata(dev);
  13715. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13716. rc = pt_pip2_get_version(cd);
  13717. if (!rc) {
  13718. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13719. "PIP VERSION : %02X.%02X\n"
  13720. "BL VERSION : %02X.%02X\n"
  13721. "FW VERSION : %02X.%02X\n"
  13722. "SILICON ID : %04X.%04X\n"
  13723. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13724. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13725. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13726. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13727. ttdata->chip_id, ttdata->chip_rev,
  13728. ttdata->uid[0], ttdata->uid[1],
  13729. ttdata->uid[2], ttdata->uid[3],
  13730. ttdata->uid[4], ttdata->uid[5],
  13731. ttdata->uid[6], ttdata->uid[7],
  13732. ttdata->uid[8], ttdata->uid[9],
  13733. ttdata->uid[10], ttdata->uid[11]);
  13734. } else {
  13735. pt_debug(dev, DL_ERROR,
  13736. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13737. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13738. "PIP VERSION : -\n"
  13739. "BL VERSION : -\n"
  13740. "FW VERSION : -\n"
  13741. "SILICON ID : -\n"
  13742. "UID : -\n");
  13743. }
  13744. }
  13745. #ifdef TTDL_DIAGNOSTICS
  13746. /*******************************************************************************
  13747. * FUNCTION: pt_ttdl_status_show
  13748. *
  13749. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13750. * variable states and GPIO levels. Additional information printed when
  13751. * TTDL_DIAGNOSTICS is enabled.
  13752. *
  13753. * NOTE: All counters will be reset to 0 when this function is called.
  13754. *
  13755. * RETURN:
  13756. * 0 = success
  13757. * !0 = failure
  13758. *
  13759. * PARAMETERS:
  13760. * *dev - pointer to device structure
  13761. * *attr - pointer to device attributes
  13762. * *buf - pointer to output buffer
  13763. ******************************************************************************/
  13764. static ssize_t pt_ttdl_status_show(struct device *dev,
  13765. struct device_attribute *attr, char *buf)
  13766. {
  13767. struct pt_core_data *cd = dev_get_drvdata(dev);
  13768. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13769. struct i2c_client *client = to_i2c_client(dev);
  13770. ssize_t ret;
  13771. u16 cal_size = 0;
  13772. unsigned short crc = 0;
  13773. if (cd->cal_cache_in_host)
  13774. _pt_manage_local_cal_data(dev,
  13775. PT_CAL_DATA_INFO, &cal_size, &crc);
  13776. ret = scnprintf(buf, strlen(buf),
  13777. "%s: 0x%04X\n"
  13778. "%s: %d\n"
  13779. "%s: %s\n"
  13780. "%s: %s %s\n"
  13781. "%s: %s\n"
  13782. "%s: 0x%02X\n"
  13783. "%s: %s\n"
  13784. "%s: %s\n"
  13785. "%s: %s\n"
  13786. "%s: %s\n"
  13787. "%s: %d\n"
  13788. "%s: %d\n"
  13789. "%s: %s\n"
  13790. "%s: %s\n"
  13791. "%s: %d\n"
  13792. #ifdef TTDL_DIAGNOSTICS
  13793. "%s: %d\n"
  13794. "%s: %d\n"
  13795. "%s: %d\n"
  13796. "%s: %d\n"
  13797. "%s: %d\n"
  13798. "%s: %d\n"
  13799. "%s: %d\n"
  13800. "%s: %d\n"
  13801. "%s: %d\n"
  13802. "%s: %d\n"
  13803. "%s: %s\n"
  13804. "%s: %s\n"
  13805. "%s: %s\n"
  13806. "%s: %d\n"
  13807. "%s: 0x%04X\n"
  13808. "%s: %s\n"
  13809. #endif /* TTDL_DIAGNOSTICS */
  13810. ,
  13811. "Startup Status ", cd->startup_status,
  13812. "TTDL Debug Level ", cd->debug_level,
  13813. "Mode ",
  13814. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13815. "Operational" : "BL") : "Unknown",
  13816. "DUT Generation ",
  13817. cd->active_dut_generation ?
  13818. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13819. "PT TC/TT" : "Gen5/6") : "Unknown",
  13820. cd->active_dut_generation ?
  13821. (cd->set_dut_generation == true ?
  13822. "(Set)" : "(Detected)") : "",
  13823. "HW Detected ",
  13824. cd->hw_detected ? "True" : "False",
  13825. "I2C Address ",
  13826. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13827. "Active Bus Module ",
  13828. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13829. "Flashless Mode ",
  13830. cd->flashless_dut == 1 ? "Yes" : "No",
  13831. "GPIO state - IRQ ",
  13832. cd->cpdata->irq_stat ?
  13833. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13834. "High" : "Low") : "not defined",
  13835. "GPIO state - TP_XRES ",
  13836. pdata->core_pdata->rst_gpio ?
  13837. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13838. "High" : "Low") : "not defined",
  13839. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13840. "Startup Retry Count ", cd->startup_retry_count,
  13841. "WD - Manual Force Stop ",
  13842. cd->watchdog_force_stop ? "True" : "False",
  13843. "WD - Enabled ",
  13844. cd->watchdog_enabled ? "True" : "False",
  13845. "WD - Interval (ms) ", cd->watchdog_interval
  13846. #ifdef TTDL_DIAGNOSTICS
  13847. , "WD - Triggered Count ", cd->watchdog_count,
  13848. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13849. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13850. "WD - XRES Count ", cd->wd_xres_count,
  13851. "IRQ Triggered Count ", cd->irq_count,
  13852. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13853. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13854. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13855. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13856. "Error GPIO trigger type ", cd->err_gpio_type,
  13857. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13858. "Suppress No-Flash Auto BL ",
  13859. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13860. "Yes" : "No",
  13861. "Calibration Cache on host ",
  13862. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13863. "Yes" : "No",
  13864. "Calibration Cache size ", cal_size,
  13865. "Calibration Cache chip ID ", crc,
  13866. "Multi-Chip Support ",
  13867. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13868. #endif /* TTDL_DIAGNOSTICS */
  13869. );
  13870. #ifdef TTDL_DIAGNOSTICS
  13871. /* Reset all diagnostic counters */
  13872. cd->watchdog_count = 0;
  13873. cd->watchdog_irq_stuck_count = 0;
  13874. cd->watchdog_failed_access_count = 0;
  13875. cd->wd_xres_count = 0;
  13876. cd->irq_count = 0;
  13877. cd->bl_retry_packet_count = 0;
  13878. cd->pip2_crc_error_count = 0;
  13879. cd->bus_transmit_error_count = 0;
  13880. #endif
  13881. return ret;
  13882. }
  13883. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13884. /*******************************************************************************
  13885. * FUNCTION: pt_pip2_enter_bl_show
  13886. *
  13887. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13888. * the DUT into the BL and show the success or failure of entering the BL
  13889. *
  13890. * RETURN:
  13891. * 0 = success
  13892. * !0 = failure
  13893. *
  13894. * PARAMETERS:
  13895. * *dev - pointer to device structure
  13896. * *attr - pointer to device attributes
  13897. * *buf - pointer to output buffer
  13898. ******************************************************************************/
  13899. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13900. struct device_attribute *attr, char *buf)
  13901. {
  13902. ssize_t ret = 0;
  13903. int rc = 0;
  13904. int result = 0;
  13905. u8 mode = PT_MODE_UNKNOWN;
  13906. struct pt_core_data *cd = dev_get_drvdata(dev);
  13907. /* Turn off the TTDL WD before enter bootloader */
  13908. pt_stop_wd_timer(cd);
  13909. /* Ensure NO enumeration work is queued or will be queued */
  13910. cancel_work_sync(&cd->enum_work);
  13911. mutex_lock(&cd->system_lock);
  13912. cd->bridge_mode = true;
  13913. mutex_unlock(&cd->system_lock);
  13914. /* set mode to operational to avoid any extra PIP traffic */
  13915. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13916. switch (result) {
  13917. case PT_ENTER_BL_PASS:
  13918. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13919. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13920. break;
  13921. case PT_ENTER_BL_ERROR:
  13922. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13923. rc,
  13924. " Unknown Error");
  13925. break;
  13926. case PT_ENTER_BL_RESET_FAIL:
  13927. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13928. rc,
  13929. " Soft Reset Failed");
  13930. break;
  13931. case PT_ENTER_BL_HID_START_BL_FAIL:
  13932. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13933. rc,
  13934. " PIP Start BL Cmd Failed");
  13935. break;
  13936. case PT_ENTER_BL_CONFIRM_FAIL:
  13937. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13938. rc,
  13939. " Error confirming DUT entered BL");
  13940. break;
  13941. default:
  13942. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13943. rc, " Unknown Error");
  13944. break;
  13945. };
  13946. /* Allow enumeration work to be queued again */
  13947. cd->bridge_mode = false;
  13948. return ret;
  13949. }
  13950. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13951. /*******************************************************************************
  13952. * FUNCTION: pt_pip2_exit_bl_show
  13953. *
  13954. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13955. * launch the APP and put the DUT Application mode
  13956. *
  13957. * RETURN:
  13958. * 0 = success
  13959. * !0 = failure
  13960. *
  13961. * PARAMETERS:
  13962. * *dev - pointer to device structure
  13963. * *attr - pointer to device attributes
  13964. * *buf - pointer to output buffer
  13965. ******************************************************************************/
  13966. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13967. struct device_attribute *attr, char *buf)
  13968. {
  13969. struct pt_core_data *cd = dev_get_drvdata(dev);
  13970. ssize_t ret = 0;
  13971. int rc = 0;
  13972. u8 status_str[PT_STATUS_STR_LEN];
  13973. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13974. /*
  13975. * Perform enum if startup_status doesn't reach to
  13976. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13977. */
  13978. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13979. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13980. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13981. strlcpy(status_str,
  13982. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13983. }
  13984. }
  13985. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13986. rc, status_str);
  13987. return ret;
  13988. }
  13989. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13990. #endif
  13991. #ifdef EASYWAKE_TSG6
  13992. /*******************************************************************************
  13993. * FUNCTION: pt_easy_wakeup_gesture_show
  13994. *
  13995. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13996. * current easy wakeup gesture
  13997. *
  13998. * RETURN:
  13999. * 0 = success
  14000. * !0 = failure
  14001. *
  14002. * PARAMETERS:
  14003. * *dev - pointer to device structure
  14004. * *attr - pointer to device attributes
  14005. * *buf - pointer to output buffer
  14006. ******************************************************************************/
  14007. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  14008. struct device_attribute *attr, char *buf)
  14009. {
  14010. struct pt_core_data *cd = dev_get_drvdata(dev);
  14011. ssize_t ret;
  14012. mutex_lock(&cd->system_lock);
  14013. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  14014. cd->easy_wakeup_gesture);
  14015. mutex_unlock(&cd->system_lock);
  14016. return ret;
  14017. }
  14018. /*******************************************************************************
  14019. * FUNCTION: pt_easy_wakeup_gesture_store
  14020. *
  14021. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  14022. * allows the wake gesture to be set to a custom value.
  14023. *
  14024. * RETURN: Size of passed in buffer is success
  14025. *
  14026. * PARAMETERS:
  14027. * *dev - pointer to device structure
  14028. * *attr - pointer to device attributes
  14029. * *buf - pointer to buffer that hold the command parameters
  14030. * size - size of buf
  14031. ******************************************************************************/
  14032. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  14033. struct device_attribute *attr, const char *buf, size_t size)
  14034. {
  14035. struct pt_core_data *cd = dev_get_drvdata(dev);
  14036. u32 input_data[2];
  14037. int length;
  14038. int rc = 0;
  14039. /* Maximum input of one value */
  14040. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14041. ARRAY_SIZE(input_data));
  14042. if (length != 1) {
  14043. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14044. __func__);
  14045. rc = -EINVAL;
  14046. goto exit;
  14047. }
  14048. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  14049. __func__, cd->features.easywake);
  14050. if (!cd->features.easywake || input_data[0] > 0xFF) {
  14051. rc = -EINVAL;
  14052. goto exit;
  14053. }
  14054. pm_runtime_get_sync(dev);
  14055. mutex_lock(&cd->system_lock);
  14056. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  14057. cd->easy_wakeup_gesture = (u8)input_data[0];
  14058. pt_debug(dev, DL_INFO,
  14059. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  14060. __func__, cd->easy_wakeup_gesture);
  14061. } else
  14062. rc = -ENODEV;
  14063. mutex_unlock(&cd->system_lock);
  14064. pm_runtime_put(dev);
  14065. exit:
  14066. if (rc)
  14067. return rc;
  14068. return size;
  14069. }
  14070. /*******************************************************************************
  14071. * FUNCTION: pt_easy_wakeup_gesture_id_show
  14072. *
  14073. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  14074. * show the TSG6 easywake gesture ID
  14075. *
  14076. * RETURN:
  14077. * 0 = success
  14078. * !0 = failure
  14079. *
  14080. * PARAMETERS:
  14081. * *dev - pointer to device structure
  14082. * *attr - pointer to device attributes
  14083. * *buf - pointer to output buffer
  14084. ******************************************************************************/
  14085. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  14086. struct device_attribute *attr, char *buf)
  14087. {
  14088. struct pt_core_data *cd = dev_get_drvdata(dev);
  14089. ssize_t ret;
  14090. mutex_lock(&cd->system_lock);
  14091. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  14092. cd->gesture_id);
  14093. mutex_unlock(&cd->system_lock);
  14094. return ret;
  14095. }
  14096. /*******************************************************************************
  14097. * FUNCTION: pt_easy_wakeup_gesture_data_show
  14098. *
  14099. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  14100. * show the TSG6 easywake gesture data in the following format:
  14101. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  14102. *
  14103. * RETURN:
  14104. * 0 = success
  14105. * !0 = failure
  14106. *
  14107. * PARAMETERS:
  14108. * *dev - pointer to device structure
  14109. * *attr - pointer to device attributes
  14110. * *buf - pointer to output buffer
  14111. ******************************************************************************/
  14112. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  14113. struct device_attribute *attr, char *buf)
  14114. {
  14115. struct pt_core_data *cd = dev_get_drvdata(dev);
  14116. ssize_t ret = 0;
  14117. int i;
  14118. mutex_lock(&cd->system_lock);
  14119. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  14120. for (i = 0; i < cd->gesture_data_length; i++)
  14121. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14122. "0x%02X\n", cd->gesture_data[i]);
  14123. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14124. "(%d bytes)\n", cd->gesture_data_length);
  14125. mutex_unlock(&cd->system_lock);
  14126. return ret;
  14127. }
  14128. #endif /* EASYWAKE_TSG6 */
  14129. #ifdef TTDL_DIAGNOSTICS
  14130. /*******************************************************************************
  14131. * FUNCTION: pt_err_gpio_show
  14132. *
  14133. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  14134. * setting up the gpio was successful
  14135. *
  14136. * RETURN: Char buffer with printed GPIO creation state
  14137. *
  14138. * PARAMETERS:
  14139. * *dev - pointer to device structure
  14140. * *attr - pointer to device attributes
  14141. * *buf - pointer to output buffer
  14142. ******************************************************************************/
  14143. static ssize_t pt_err_gpio_show(struct device *dev,
  14144. struct device_attribute *attr, char *buf)
  14145. {
  14146. struct pt_core_data *cd = dev_get_drvdata(dev);
  14147. return scnprintf(buf, strlen(buf), "Status: 0\n"
  14148. "Err GPIO (%d) : %s\n"
  14149. "Err GPIO trig type: %d\n",
  14150. cd->err_gpio,
  14151. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  14152. "HIGH" : "low") : "not defined"),
  14153. cd->err_gpio_type);
  14154. }
  14155. /*******************************************************************************
  14156. * FUNCTION: pt_err_gpio_store
  14157. *
  14158. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  14159. * available host GPIO to be used to trigger when TTDL detects a PIP
  14160. * command/response timeout.
  14161. *
  14162. * RETURN:
  14163. * 0 = success
  14164. * !0 = failure
  14165. *
  14166. * PARAMETERS:
  14167. * *dev - pointer to device structure
  14168. * *attr - pointer to device attributes
  14169. * *buf - pointer to buffer that hold the command parameters
  14170. * size - size of buf
  14171. ******************************************************************************/
  14172. static ssize_t pt_err_gpio_store(struct device *dev,
  14173. struct device_attribute *attr, const char *buf, size_t size)
  14174. {
  14175. struct pt_core_data *cd = dev_get_drvdata(dev);
  14176. unsigned long gpio;
  14177. int rc = 0;
  14178. u32 input_data[3];
  14179. int length;
  14180. u8 err_type;
  14181. input_data[0] = 0;
  14182. input_data[1] = 0;
  14183. /* Maximmum input is two elements */
  14184. length = _pt_ic_parse_input(dev, buf, size,
  14185. input_data, ARRAY_SIZE(input_data));
  14186. if (length < 1 || length > 2) {
  14187. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14188. __func__);
  14189. rc = -EINVAL;
  14190. goto exit;
  14191. }
  14192. gpio = input_data[0];
  14193. err_type = (u8)input_data[1];
  14194. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  14195. rc = -EINVAL;
  14196. goto exit;
  14197. }
  14198. mutex_lock(&cd->system_lock);
  14199. gpio_free(gpio);
  14200. rc = gpio_request(gpio, NULL);
  14201. if (rc) {
  14202. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  14203. rc = -ENODEV;
  14204. } else {
  14205. cd->err_gpio = gpio;
  14206. cd->err_gpio_type = err_type;
  14207. gpio_direction_output(gpio, 0);
  14208. }
  14209. mutex_unlock(&cd->system_lock);
  14210. exit:
  14211. if (rc)
  14212. return rc;
  14213. return size;
  14214. }
  14215. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  14216. pt_err_gpio_store);
  14217. /*******************************************************************************
  14218. * FUNCTION: pt_drv_irq_show
  14219. *
  14220. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  14221. * TTDL interrupt is enabled/disabled
  14222. *
  14223. * RETURN:
  14224. * 0 = success
  14225. * !0 = failure
  14226. *
  14227. * PARAMETERS:
  14228. * *dev - pointer to device structure
  14229. * *attr - pointer to device attributes
  14230. * *buf - pointer to output buffer
  14231. ******************************************************************************/
  14232. static ssize_t pt_drv_irq_show(struct device *dev,
  14233. struct device_attribute *attr, char *buf)
  14234. {
  14235. struct pt_core_data *cd = dev_get_drvdata(dev);
  14236. ssize_t ret = 0;
  14237. mutex_lock(&cd->system_lock);
  14238. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  14239. if (cd->irq_enabled)
  14240. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14241. "Driver interrupt: ENABLED\n");
  14242. else
  14243. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  14244. "Driver interrupt: DISABLED\n");
  14245. mutex_unlock(&cd->system_lock);
  14246. return ret;
  14247. }
  14248. /*******************************************************************************
  14249. * FUNCTION: pt_drv_irq_store
  14250. *
  14251. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  14252. * IRQ to be enabled/disabled.
  14253. *
  14254. * RETURN: Size of passed in buffer
  14255. *
  14256. * PARAMETERS:
  14257. * *dev - pointer to device structure
  14258. * *attr - pointer to device attributes
  14259. * *buf - pointer to buffer that hold the command parameters
  14260. * size - size of buf
  14261. ******************************************************************************/
  14262. static ssize_t pt_drv_irq_store(struct device *dev,
  14263. struct device_attribute *attr, const char *buf, size_t size)
  14264. {
  14265. struct pt_core_data *cd = dev_get_drvdata(dev);
  14266. u32 input_data[2];
  14267. int length;
  14268. int rc = 0;
  14269. /* Maximum input of one value */
  14270. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14271. ARRAY_SIZE(input_data));
  14272. if (length != 1) {
  14273. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14274. __func__);
  14275. rc = -EINVAL;
  14276. goto exit;
  14277. }
  14278. mutex_lock(&cd->system_lock);
  14279. switch (input_data[0]) {
  14280. case 0:
  14281. if (cd->irq_enabled) {
  14282. cd->irq_enabled = false;
  14283. /* Disable IRQ has no return value to check */
  14284. disable_irq_nosync(cd->irq);
  14285. pt_debug(dev, DL_INFO,
  14286. "%s: Driver IRQ now disabled\n",
  14287. __func__);
  14288. } else
  14289. pt_debug(dev, DL_INFO,
  14290. "%s: Driver IRQ already disabled\n",
  14291. __func__);
  14292. break;
  14293. case 1:
  14294. if (cd->irq_enabled == false) {
  14295. cd->irq_enabled = true;
  14296. enable_irq(cd->irq);
  14297. pt_debug(dev, DL_INFO,
  14298. "%s: Driver IRQ now enabled\n",
  14299. __func__);
  14300. } else
  14301. pt_debug(dev, DL_INFO,
  14302. "%s: Driver IRQ already enabled\n",
  14303. __func__);
  14304. break;
  14305. default:
  14306. rc = -EINVAL;
  14307. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14308. }
  14309. mutex_unlock(&(cd->system_lock));
  14310. exit:
  14311. if (rc)
  14312. return rc;
  14313. return size;
  14314. }
  14315. /*******************************************************************************
  14316. * FUNCTION: pt_pip2_bin_hdr_show
  14317. *
  14318. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14319. * the bin file header from flash and show each field
  14320. *
  14321. * RETURN:
  14322. * 0 = success
  14323. * !0 = failure
  14324. *
  14325. * PARAMETERS:
  14326. * *dev - pointer to device structure
  14327. * *attr - pointer to device attributes
  14328. * *buf - pointer to output buffer
  14329. ******************************************************************************/
  14330. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14331. struct device_attribute *attr, char *buf)
  14332. {
  14333. ssize_t ret = 0;
  14334. struct pt_bin_file_hdr hdr = {0};
  14335. int rc;
  14336. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14337. ret = scnprintf(buf, strlen(buf),
  14338. "%s: %d\n"
  14339. "%s: %d\n"
  14340. "%s: 0x%04X\n"
  14341. "%s: %d\n"
  14342. "%s: %d\n"
  14343. "%s: %d\n"
  14344. "%s: 0x%08X\n"
  14345. "%s: 0x%04X\n"
  14346. "%s: 0x%04X\n"
  14347. "%s: %d\n"
  14348. "%s: %d\n",
  14349. "Status", rc,
  14350. "Header Length ", hdr.length,
  14351. "TTPID ", hdr.ttpid,
  14352. "FW Major Ver ", hdr.fw_major,
  14353. "FW Minor Ver ", hdr.fw_minor,
  14354. "FW Rev Control ", hdr.fw_rev_ctrl,
  14355. "FW CRC ", hdr.fw_crc,
  14356. "Silicon Rev ", hdr.si_rev,
  14357. "Silicon ID ", hdr.si_id,
  14358. "Config Ver ", hdr.config_ver,
  14359. "HEX File Size ", hdr.hex_file_size
  14360. );
  14361. return ret;
  14362. }
  14363. /*******************************************************************************
  14364. * FUNCTION: pt_platform_data_show
  14365. *
  14366. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14367. * active platform data including: GPIOs, Vendor and Product IDs,
  14368. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14369. * HID registers, and Easy wake gesture
  14370. *
  14371. * RETURN: Size of printed data
  14372. *
  14373. * PARAMETERS:
  14374. * *dev - pointer to device structure
  14375. * *attr - pointer to device attributes
  14376. * *buf - pointer to output buffer
  14377. ******************************************************************************/
  14378. static ssize_t pt_platform_data_show(struct device *dev,
  14379. struct device_attribute *attr, char *buf)
  14380. {
  14381. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14382. struct pt_core_data *cd = dev_get_drvdata(dev);
  14383. ssize_t ret;
  14384. ret = scnprintf(buf, strlen(buf),
  14385. "%s: %d\n"
  14386. "%s: %d\n"
  14387. "%s: %d\n"
  14388. "%s: %d\n"
  14389. "%s: %d\n"
  14390. "%s: %d\n"
  14391. "%s: %d\n"
  14392. "%s: %d\n"
  14393. "%s: %d\n"
  14394. "%s: %d\n"
  14395. "%s: %d\n"
  14396. "%s: %d\n"
  14397. "%s: %d\n"
  14398. "%s: %d\n"
  14399. "%s: %d\n"
  14400. "%s: %d\n"
  14401. "%s: %d\n"
  14402. "%s: %d\n"
  14403. "%s: %d\n"
  14404. "%s: %d\n"
  14405. "%s: %d\n"
  14406. "%s: %d\n"
  14407. "%s: %d\n"
  14408. "%s: %d\n"
  14409. "%s: %d\n"
  14410. "%s: %d\n"
  14411. "%s: %d\n"
  14412. "%s: %s\n"
  14413. "%s: %s\n"
  14414. "%s: %d\n",
  14415. "Status", 0,
  14416. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14417. "Interrupt GPIO Value ",
  14418. pdata->core_pdata->irq_gpio ?
  14419. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14420. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14421. "Reset GPIO Value ",
  14422. pdata->core_pdata->rst_gpio ?
  14423. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14424. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14425. "DDI Reset GPIO Value ",
  14426. pdata->core_pdata->ddi_rst_gpio ?
  14427. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14428. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14429. "VDDI GPIO Value ",
  14430. pdata->core_pdata->vddi_gpio ?
  14431. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14432. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14433. "VCC GPIO Value ",
  14434. pdata->core_pdata->vcc_gpio ?
  14435. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14436. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14437. "AVDD GPIO Value ",
  14438. pdata->core_pdata->avdd_gpio ?
  14439. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14440. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14441. "AVEE GPIO Value ",
  14442. pdata->core_pdata->avee_gpio ?
  14443. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14444. "Vendor ID ", pdata->core_pdata->vendor_id,
  14445. "Product ID ", pdata->core_pdata->product_id,
  14446. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14447. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14448. "Core data flags ", pdata->core_pdata->flags,
  14449. "MT data flags ", pdata->mt_pdata->flags,
  14450. "Loader data flags ", pdata->loader_pdata->flags,
  14451. "Level trigger delay (us) ",
  14452. pdata->core_pdata->level_irq_udelay,
  14453. "HID Descriptor Register ",
  14454. pdata->core_pdata->hid_desc_register,
  14455. "HID Output Register ",
  14456. cd->hid_desc.output_register,
  14457. "HID Command Register ",
  14458. cd->hid_desc.command_register,
  14459. "Easy wakeup gesture ",
  14460. pdata->core_pdata->easy_wakeup_gesture,
  14461. "Config DUT generation ",
  14462. pdata->core_pdata->config_dut_generation ?
  14463. (pdata->core_pdata->config_dut_generation ==
  14464. CONFIG_DUT_PIP2_CAPABLE ?
  14465. "PT TC/TT" : "Gen5/6") : "Auto",
  14466. "Watchdog Force Stop ",
  14467. pdata->core_pdata->watchdog_force_stop ?
  14468. "True" : "False",
  14469. "Panel ID Support ",
  14470. pdata->core_pdata->panel_id_support);
  14471. return ret;
  14472. }
  14473. #define PT_ERR_STR_SIZE 64
  14474. /*******************************************************************************
  14475. * FUNCTION: pt_bist_bus_test
  14476. *
  14477. * SUMMARY: Tests the connectivity of the active bus pins:
  14478. * I2C - SDA and SCL
  14479. * SPI - MOSI, MISO, CLK
  14480. *
  14481. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14482. * bus. If any data is read we know the I2C/SPI pins are connected
  14483. *
  14484. * RETURN:
  14485. * 0 = Success
  14486. * !0 = Failure
  14487. *
  14488. * PARAMETERS:
  14489. * *dev - pointer to device structure
  14490. * *net_toggled - pointer to where to store if bus toggled
  14491. * *err_str - pointer to error string buffer
  14492. ******************************************************************************/
  14493. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14494. {
  14495. struct pt_core_data *cd = dev_get_drvdata(dev);
  14496. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14497. u8 *read_buf = NULL;
  14498. int bytes_read = 0;
  14499. int rc = 0;
  14500. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14501. if (read_buf == NULL) {
  14502. rc = -ENOMEM;
  14503. goto exit;
  14504. }
  14505. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14506. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14507. disable_irq(cd->irq);
  14508. mutex_lock(&cd->system_lock);
  14509. cd->irq_disabled = true;
  14510. mutex_unlock(&cd->system_lock);
  14511. /*
  14512. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14513. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14514. */
  14515. usleep_range(5000, 6000);
  14516. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14517. __func__, (int)sizeof(ver_cmd));
  14518. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14519. ">>> User CMD");
  14520. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14521. if (rc) {
  14522. pt_debug(dev, DL_ERROR,
  14523. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14524. *net_toggled = 0;
  14525. strlcpy(err_str,
  14526. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14527. goto exit_enable_irq;
  14528. }
  14529. usleep_range(4000, 5000);
  14530. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14531. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14532. __func__, bytes_read);
  14533. if (bytes_read == 0) {
  14534. *net_toggled = 0;
  14535. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14536. __func__);
  14537. strlcpy(err_str,
  14538. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14539. rc = -EIO;
  14540. goto exit_enable_irq;
  14541. } else {
  14542. if (cd->bus_ops->bustype == BUS_I2C)
  14543. *net_toggled = 1;
  14544. else {
  14545. if ((bytes_read > 3) &&
  14546. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14547. PIP2_CMD_ID_VERSION)
  14548. *net_toggled = 1;
  14549. else {
  14550. *net_toggled = 0;
  14551. pt_debug(dev, DL_INFO,
  14552. "%s: BUS Read Failed, %d bytes read\n",
  14553. __func__, bytes_read);
  14554. strlcpy(err_str,
  14555. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14556. }
  14557. }
  14558. }
  14559. exit_enable_irq:
  14560. enable_irq(cd->irq);
  14561. usleep_range(5000, 6000);
  14562. mutex_lock(&cd->system_lock);
  14563. cd->irq_disabled = false;
  14564. mutex_unlock(&cd->system_lock);
  14565. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14566. exit:
  14567. kfree(read_buf);
  14568. return rc;
  14569. }
  14570. /*******************************************************************************
  14571. * FUNCTION: pt_bist_irq_test
  14572. *
  14573. * SUMMARY: Tests the connectivity of the IRQ net
  14574. *
  14575. * This test will ensure there is a good connection between the host
  14576. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14577. * asserted and if so keep reading messages off of the bus until
  14578. * it de-asserts. Possible outcomes:
  14579. * - IRQ was already de-asserted: Send a PIP command and if an
  14580. * interrupt is generated the test passes.
  14581. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14582. * test passes.
  14583. * - IRQ stays asserted: After reading the bus multiple times
  14584. * the IRQ stays asserted. Likely open or shorted to GND
  14585. *
  14586. * RETURN:
  14587. * 0 = Success
  14588. * !0 = Failure
  14589. *
  14590. * PARAMETERS:
  14591. * *dev - pointer to device structure
  14592. * *bus_toggled - pointer to where to store if bus toggled
  14593. * *irq_toggled - pointer to where to store if IRQ toggled
  14594. * *xres_toggled - pointer to where to store if XRES toggled
  14595. * *err_str - pointer to error string buffer
  14596. ******************************************************************************/
  14597. static int pt_bist_irq_test(struct device *dev,
  14598. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14599. {
  14600. struct pt_core_data *cd = dev_get_drvdata(dev);
  14601. u8 *read_buf = NULL;
  14602. u8 mode = PT_MODE_UNKNOWN;
  14603. u16 actual_read_len;
  14604. int bytes_read = 0;
  14605. int count = 0;
  14606. int rc = 0;
  14607. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14608. if (read_buf == NULL) {
  14609. rc = -ENOMEM;
  14610. goto exit;
  14611. }
  14612. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14613. cd->irq_count = 0;
  14614. /*
  14615. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14616. * the bus based on the 2 byte initial length read. Try up to 5x.
  14617. */
  14618. while (pt_check_irq_asserted(cd) && count < 5) {
  14619. count++;
  14620. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14621. }
  14622. if (count > 1 && count < 5 && bytes_read > 0) {
  14623. /*
  14624. * IRQ was stuck but data was successfully read from the
  14625. * bus releasing the IRQ line.
  14626. */
  14627. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14628. __func__, count, bytes_read);
  14629. *bus_toggled = 1;
  14630. *irq_toggled = 1;
  14631. goto exit;
  14632. }
  14633. if (count == 5 && bytes_read == 0) {
  14634. /*
  14635. * Looped 5x and read nothing off the bus yet the IRQ is still
  14636. * asserted. Possible conditions:
  14637. * - IRQ open circuit
  14638. * - IRQ shorted to GND
  14639. * - I2C/SPI bus is disconnected
  14640. * - FW holding the pin low
  14641. * Try entering the BL to see if communication works there.
  14642. */
  14643. mode = PT_MODE_IGNORE;
  14644. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14645. if (rc) {
  14646. pt_debug(dev, DL_ERROR,
  14647. "%s Failed to enter BL\n", __func__);
  14648. strlcpy(err_str,
  14649. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14650. *irq_toggled = 0;
  14651. goto exit;
  14652. }
  14653. /*
  14654. * If original mode was operational and we successfully
  14655. * entered the BL, then the XRES net must have toggled
  14656. */
  14657. if (mode == PT_MODE_OPERATIONAL)
  14658. *xres_toggled = 1;
  14659. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14660. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14661. &actual_read_len);
  14662. if (rc) {
  14663. /*
  14664. * Could not communicate to DUT in BL mode. Save the
  14665. * error string, slim chance but the XRES test below may
  14666. * show the IRQ is actually working.
  14667. */
  14668. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14669. pt_debug(dev, DL_ERROR,
  14670. "%s: %s, count=%d bytes_read=%d\n",
  14671. __func__, err_str, count, bytes_read);
  14672. *irq_toggled = 0;
  14673. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14674. goto exit;
  14675. } else {
  14676. *bus_toggled = 1;
  14677. *irq_toggled = 1;
  14678. goto exit;
  14679. }
  14680. }
  14681. if (pt_check_irq_asserted(cd)) {
  14682. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14683. rc = -EIO;
  14684. *irq_toggled = 0;
  14685. goto exit;
  14686. }
  14687. /* Try sending a PIP command to see if we get a response */
  14688. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14689. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14690. if (rc) {
  14691. /*
  14692. * Potential IRQ issue, no communication in App mode, attempt
  14693. * the same command in the BL
  14694. */
  14695. mode = PT_MODE_IGNORE;
  14696. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14697. if (rc) {
  14698. pt_debug(dev, DL_ERROR,
  14699. "%s Failed to enter BL\n", __func__);
  14700. *irq_toggled = 0;
  14701. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14702. goto exit;
  14703. }
  14704. /*
  14705. * If original mode was operational and we successfully
  14706. * entered the BL, this will be useful info for the tp_xres
  14707. * test below.
  14708. */
  14709. if (mode == PT_MODE_OPERATIONAL)
  14710. *xres_toggled = 1;
  14711. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14712. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14713. &actual_read_len);
  14714. if (rc) {
  14715. /*
  14716. * Could not communicate in FW mode or BL mode. Save the
  14717. * error string, slim chance but the XRES test below may
  14718. * show the IRQ is actually working.
  14719. */
  14720. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14721. pt_debug(dev, DL_ERROR,
  14722. "%s: request_active_pip_prot failed\n",
  14723. __func__);
  14724. *irq_toggled = 0;
  14725. goto exit;
  14726. }
  14727. }
  14728. if (cd->irq_count > 0) {
  14729. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14730. cd->irq_count);
  14731. *bus_toggled = 1;
  14732. *irq_toggled = 1;
  14733. goto exit;
  14734. }
  14735. exit:
  14736. kfree(read_buf);
  14737. return rc;
  14738. }
  14739. /*******************************************************************************
  14740. * FUNCTION: pt_bist_xres_test
  14741. *
  14742. * SUMMARY: Tests the connectivity of the TP_XRES net
  14743. *
  14744. * This test will ensure there is a good connection between the host
  14745. * and the DUT on the tp_xres pin. The pin will be toggled to
  14746. * generate a TP reset which will cause the DUT to output a reset
  14747. * sentinel. If the reset sentinel is seen the test passes. If it is
  14748. * not seen the test will attempt to send a soft reset to simply gain
  14749. * some additional information on the failure:
  14750. * - soft reset fails to send: XRES and IRQ likely open
  14751. * - soft reset passes: XRES likely open or stuck de-asserted
  14752. * - soft reset fails: XRES likely stuck asserted
  14753. *
  14754. * RETURN:
  14755. * 0 = Success
  14756. * !0 = Failure
  14757. *
  14758. * PARAMETERS:
  14759. * *dev - pointer to device structure
  14760. * *bus_toggled - pointer to where to store if bus toggled
  14761. * *irq_toggled - pointer to where to store if IRQ toggled
  14762. * *xres_toggled - pointer to where to store if XRES toggled
  14763. * *err_str - pointer to error string buffer
  14764. ******************************************************************************/
  14765. static int pt_bist_xres_test(struct device *dev,
  14766. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14767. {
  14768. struct pt_core_data *cd = dev_get_drvdata(dev);
  14769. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14770. u8 *read_buf = NULL;
  14771. u8 mode = PT_MODE_UNKNOWN;
  14772. int rc = 0;
  14773. int t = 0;
  14774. int timeout = 300;
  14775. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14776. if (read_buf == NULL) {
  14777. rc = -ENOMEM;
  14778. goto exit;
  14779. }
  14780. /* Clear the startup bit mask, reset and enum will re-populate it */
  14781. cd->startup_status = STARTUP_STATUS_START;
  14782. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14783. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14784. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14785. rc = -ENODEV;
  14786. goto exit;
  14787. }
  14788. /* Ensure we have nothing pending on active bus */
  14789. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14790. /* Perform a hard XRES toggle and wait for reset sentinel */
  14791. mutex_lock(&cd->system_lock);
  14792. cd->hid_reset_cmd_state = 1;
  14793. mutex_unlock(&cd->system_lock);
  14794. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14795. rc = pt_hw_hard_reset(cd);
  14796. /* Set timeout to 1s for the flashless case where a BL could be done */
  14797. if (cd->flashless_dut)
  14798. timeout = 1000;
  14799. /*
  14800. * To avoid the case that next PIP command can be confused by BL/FW
  14801. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14802. * followed by "wakeup event" function can lower the failure rate.
  14803. */
  14804. t = wait_event_timeout(cd->wait_q,
  14805. ((cd->startup_status != STARTUP_STATUS_START)
  14806. && (cd->hid_reset_cmd_state == 0)),
  14807. msecs_to_jiffies(timeout));
  14808. if (IS_TMO(t)) {
  14809. pt_debug(cd->dev, DL_ERROR,
  14810. "%s: TMO waiting for sentinel\n", __func__);
  14811. *xres_toggled = 0;
  14812. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14813. /*
  14814. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14815. * To enter the BL we need to generate an XRES so first try to
  14816. * launch the applicaiton
  14817. */
  14818. if (cd->mode == PT_MODE_BOOTLOADER)
  14819. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14820. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14821. __func__);
  14822. mode = PT_MODE_IGNORE;
  14823. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14824. if (rc) {
  14825. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14826. __func__);
  14827. *xres_toggled = 0;
  14828. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14829. goto exit;
  14830. } else {
  14831. /* Wait for the BL sentinel */
  14832. t = wait_event_timeout(cd->wait_q,
  14833. (cd->startup_status != STARTUP_STATUS_START),
  14834. msecs_to_jiffies(500));
  14835. if (IS_TMO(t)) {
  14836. pt_debug(cd->dev, DL_ERROR,
  14837. "%s: TMO waiting for BL sentinel\n",
  14838. __func__);
  14839. *xres_toggled = 0;
  14840. strlcpy(err_str,
  14841. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14842. rc = -ETIME;
  14843. goto exit;
  14844. }
  14845. }
  14846. }
  14847. mutex_lock(&cd->system_lock);
  14848. cd->hid_reset_cmd_state = 0;
  14849. mutex_unlock(&cd->system_lock);
  14850. /* Look for BL or FW reset sentinels */
  14851. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14852. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14853. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14854. /* If a sentinel was seen, all nets are working */
  14855. *xres_toggled = 1;
  14856. *irq_toggled = 1;
  14857. /*
  14858. * For SPI test, bus read result can be confused as FW sentinel
  14859. * if MISO(slave) is connected to MISO(host).
  14860. */
  14861. if (cd->bus_ops->bustype == BUS_I2C)
  14862. *bus_toggled = 1;
  14863. } else {
  14864. /*
  14865. * Hard reset failed, however some additional information
  14866. * could be determined. Try a soft reset to see if DUT resets
  14867. * with the possible outcomes:
  14868. * - if it resets the line is not stuck asserted
  14869. * - if it does not reset the line could be stuck asserted
  14870. */
  14871. *xres_toggled = 0;
  14872. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14873. msleep(30);
  14874. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14875. __func__, rc);
  14876. if (rc) {
  14877. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14878. pt_debug(dev, DL_ERROR,
  14879. "%s: Hard reset failed, soft reset failed %s\n",
  14880. __func__, err_str);
  14881. goto exit;
  14882. }
  14883. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14884. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14885. strlcpy(err_str,
  14886. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14887. pt_debug(dev, DL_ERROR,
  14888. "%s: Hard reset failed, soft reset passed-%s\n",
  14889. __func__, err_str);
  14890. } else if (cd->startup_status == 0) {
  14891. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14892. pt_debug(dev, DL_ERROR,
  14893. "%s: Hard reset failed, soft reset failed-%s\n",
  14894. __func__, err_str);
  14895. } else {
  14896. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14897. pt_debug(dev, DL_ERROR,
  14898. "%s: Hard and Soft reset failed - %s\n",
  14899. __func__, err_str);
  14900. }
  14901. }
  14902. exit:
  14903. kfree(read_buf);
  14904. return rc;
  14905. }
  14906. /*******************************************************************************
  14907. * FUNCTION: pt_bist_slave_irq_test
  14908. *
  14909. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14910. *
  14911. * This test will ensure there is a good IRQ connection between the master
  14912. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14913. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14914. * saw each slave trigger the IRQ with it's reset sentinel.
  14915. *
  14916. * RETURN:
  14917. * 0 = Success
  14918. * !0 = Failure
  14919. *
  14920. * PARAMETERS:
  14921. * *dev - pointer to device structure
  14922. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14923. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14924. * *err_str - pointer to error string buffer
  14925. * *slave_detect - pointer to slave detect buffer
  14926. * *boot_err - pointer to boot_err buffer
  14927. ******************************************************************************/
  14928. static int pt_bist_slave_irq_test(struct device *dev,
  14929. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14930. u8 *slave_detect, u8 *boot_err)
  14931. {
  14932. struct pt_core_data *cd = dev_get_drvdata(dev);
  14933. u8 mode = PT_MODE_UNKNOWN;
  14934. u8 status;
  14935. u8 boot;
  14936. u8 read_buf[12];
  14937. u8 detected = 0;
  14938. u8 last_err = -1;
  14939. u16 actual_read_len;
  14940. int result = 0;
  14941. int rc = 0;
  14942. /*
  14943. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14944. * detect bits. If the DUT was in FW, entering the BL will cause an
  14945. * XRES signal which will inadvertently test the XRES net as well
  14946. */
  14947. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14948. if (rc) {
  14949. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14950. __func__, rc);
  14951. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14952. goto exit;
  14953. }
  14954. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14955. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14956. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14957. &actual_read_len);
  14958. if (!rc) {
  14959. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14960. "PIP2 STATUS");
  14961. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14962. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14963. /* Slave detect is only valid if status ok and in boot exec */
  14964. if (status == PIP2_RSP_ERR_NONE &&
  14965. boot == PIP2_STATUS_BOOT_EXEC) {
  14966. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14967. SLAVE_DETECT_MASK;
  14968. } else {
  14969. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14970. rc = -EPERM;
  14971. }
  14972. } else {
  14973. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14974. __func__);
  14975. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14976. goto exit;
  14977. }
  14978. /*
  14979. * Retrieve boot error regardless of the state of the slave detect
  14980. * bit because the IRQ could have been stuck high or low.
  14981. */
  14982. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14983. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14984. read_buf, &actual_read_len);
  14985. if (!rc) {
  14986. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14987. "PIP2 GET_LAST_ERRNO");
  14988. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14989. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14990. if (last_err) {
  14991. pt_debug(cd->dev, DL_ERROR,
  14992. "%s: Master Boot Last Err = 0x%02X\n",
  14993. __func__, last_err);
  14994. }
  14995. } else {
  14996. pt_debug(cd->dev, DL_ERROR,
  14997. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14998. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14999. }
  15000. exit:
  15001. pt_debug(cd->dev, DL_INFO,
  15002. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  15003. __func__, rc, detected, last_err);
  15004. if (err_str && last_err) {
  15005. if (detected)
  15006. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15007. "- Likely stuck low. Boot Error:",
  15008. last_err);
  15009. else
  15010. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15011. "- Likely stuck high. Boot Error:",
  15012. last_err);
  15013. }
  15014. /* Ignore an invalid image error as BIST doesn't need valid FW */
  15015. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  15016. last_err = PIP2_RSP_ERR_NONE;
  15017. if (slave_irq_toggled)
  15018. *slave_irq_toggled = (detected && !last_err) ? true : false;
  15019. if (slave_bus_toggled) {
  15020. /* Leave as UNTEST if slave not detected */
  15021. if (detected)
  15022. *slave_bus_toggled = !last_err ? true : false;
  15023. }
  15024. if (slave_detect)
  15025. *slave_detect = detected;
  15026. if (boot_err)
  15027. *boot_err = last_err;
  15028. if (slave_irq_toggled && slave_bus_toggled)
  15029. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  15030. __func__,
  15031. "Detected", detected,
  15032. "slave_irq_toggled", *slave_irq_toggled,
  15033. "slave_bus_toggled", *slave_bus_toggled);
  15034. return rc;
  15035. }
  15036. /*******************************************************************************
  15037. * FUNCTION: pt_bist_slave_xres_test
  15038. *
  15039. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  15040. *
  15041. * This test will ensure there is a good TP_XRES connection between the
  15042. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  15043. * master, the STATUS command is sent and the 'Slave Detect' bits are
  15044. * verified to ensure the master DUT saw each slave trigger the IRQ with
  15045. * it's reset sentinel.
  15046. *
  15047. * RETURN:
  15048. * 0 = Success
  15049. * !0 = Failure
  15050. *
  15051. * PARAMETERS:
  15052. * *dev - pointer to device structure
  15053. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  15054. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  15055. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  15056. * *err_str - pointer to error string buffer
  15057. ******************************************************************************/
  15058. static int pt_bist_slave_xres_test(struct device *dev,
  15059. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  15060. u8 *err_str)
  15061. {
  15062. struct pt_core_data *cd = dev_get_drvdata(dev);
  15063. u8 slave_detect = 0;
  15064. u8 boot_err = 0;
  15065. int rc = 0;
  15066. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  15067. mutex_lock(&cd->system_lock);
  15068. cd->hid_reset_cmd_state = 1;
  15069. mutex_unlock(&cd->system_lock);
  15070. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  15071. pt_hw_hard_reset(cd);
  15072. msleep(100);
  15073. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  15074. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  15075. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  15076. if (!rc && *slave_irq_toggled == false) {
  15077. /*
  15078. * If the slave IRQ did not toggle, either the slave_detect
  15079. * bit was not set or we had a boot error. If the slave
  15080. * detect was not set the slave did not reset causing a boot
  15081. * error.
  15082. */
  15083. if (!slave_detect)
  15084. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  15085. else
  15086. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  15087. "- likely open or an IRQ issue. Boot Error:",
  15088. boot_err);
  15089. }
  15090. if (slave_xres_toggled) {
  15091. if (!rc)
  15092. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  15093. else
  15094. *slave_xres_toggled = false;
  15095. }
  15096. return rc;
  15097. }
  15098. /*******************************************************************************
  15099. * FUNCTION: pt_bist_slave_bus_test
  15100. *
  15101. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  15102. *
  15103. * This test will ensure a good SPI bus connection between the
  15104. * master DUT and the slave DUT(s). This bus connection is ensured by
  15105. * opening file 0 (SRAM loader). If there is a slave and the open fails
  15106. * then there is a master/slave communication issue. Opening file 0 on
  15107. * the master will open it on the slave as well if the slave was detected.
  15108. *
  15109. * RETURN:
  15110. * 0 = Success
  15111. * !0 = Failure
  15112. *
  15113. * PARAMETERS:
  15114. * *dev - pointer to device structure
  15115. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  15116. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  15117. * *err_str - pointer to error string buffer
  15118. ******************************************************************************/
  15119. static int pt_bist_slave_bus_test(struct device *dev,
  15120. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  15121. {
  15122. struct pt_core_data *cd = dev_get_drvdata(dev);
  15123. u8 mode = PT_MODE_UNKNOWN;
  15124. u8 bus_toggled = false;
  15125. u8 file_handle;
  15126. int result = 0;
  15127. int rc = 0;
  15128. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  15129. if (rc) {
  15130. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  15131. __func__, rc);
  15132. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  15133. goto exit;
  15134. }
  15135. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  15136. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  15137. if (file_handle != PIP2_RAM_FILE) {
  15138. rc = -ENOENT;
  15139. bus_toggled = false;
  15140. pt_debug(dev, DL_ERROR,
  15141. "%s Failed to open bin file\n", __func__);
  15142. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  15143. goto exit;
  15144. } else {
  15145. bus_toggled = true;
  15146. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  15147. pt_debug(dev, DL_ERROR,
  15148. "%s: File Close failed, file_handle=%d\n",
  15149. __func__, file_handle);
  15150. }
  15151. }
  15152. exit:
  15153. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  15154. if (slave_irq_toggled)
  15155. *slave_irq_toggled = bus_toggled;
  15156. if (slave_bus_toggled)
  15157. *slave_bus_toggled = bus_toggled;
  15158. return rc;
  15159. }
  15160. /*******************************************************************************
  15161. * FUNCTION: pt_ttdl_bist_show
  15162. *
  15163. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  15164. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  15165. *
  15166. * NOTE: This function will reset the DUT and the startup_status bit
  15167. * mask. A pt_enum will be queued after completion.
  15168. *
  15169. * NOTE: The order of the net tests is done to optimize the time it takes
  15170. * to run. The first test is capable of verifying all nets, each subsequent
  15171. * test is only run if the previous was not able to see all nets toggle.
  15172. *
  15173. * RETURN:
  15174. * 0 = success
  15175. * !0 = failure
  15176. *
  15177. * PARAMETERS:
  15178. * *dev - pointer to device structure
  15179. * *attr - pointer to device attributes
  15180. * *buf - pointer to output buffer
  15181. ******************************************************************************/
  15182. static ssize_t pt_ttdl_bist_show(struct device *dev,
  15183. struct device_attribute *attr, char *buf)
  15184. {
  15185. struct pt_core_data *cd = dev_get_drvdata(dev);
  15186. ssize_t ret;
  15187. char *bus_err_str = NULL;
  15188. char *irq_err_str = NULL;
  15189. char *xres_err_str = NULL;
  15190. char *slave_bus_err_str = NULL;
  15191. char *slave_irq_err_str = NULL;
  15192. char *slave_xres_err_str = NULL;
  15193. u8 tests;
  15194. int rc = 0;
  15195. int num_tests = 0;
  15196. int status = 1; /* 0 = Pass, !0 = fail */
  15197. u8 bus_toggled = 0x0F; /* default to untested */
  15198. u8 i2c_toggled = 0x0F; /* default to untested */
  15199. u8 spi_toggled = 0x0F; /* default to untested */
  15200. u8 irq_toggled = 0x0F; /* default to untested */
  15201. u8 xres_toggled = 0x0F; /* default to untested */
  15202. u8 slave_bus_toggled = 0x0F; /* default to untested */
  15203. u8 slave_irq_toggled = 0x0F; /* default to untested */
  15204. u8 slave_xres_toggled = 0x0F; /* default to untested */
  15205. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15206. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15207. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15208. if (!bus_err_str || !irq_err_str || !xres_err_str)
  15209. goto print_results;
  15210. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15211. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15212. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15213. if (cd->multi_chip) {
  15214. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15215. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15216. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  15217. if (!slave_bus_err_str ||
  15218. !slave_irq_err_str ||
  15219. !slave_xres_err_str)
  15220. goto print_results;
  15221. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15222. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15223. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15224. }
  15225. /* Turn off the TTDL WD during the test */
  15226. pt_stop_wd_timer(cd);
  15227. /* Shorten default PIP cmd timeout while running BIST */
  15228. cd->pip_cmd_timeout = 200;
  15229. /* Count the number of tests to run */
  15230. tests = cd->ttdl_bist_select;
  15231. while (tests) {
  15232. num_tests += tests & 1;
  15233. tests >>= 1;
  15234. }
  15235. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  15236. __func__, cd->ttdl_bist_select, num_tests);
  15237. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  15238. if (cd->flashless_dut) {
  15239. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  15240. __func__);
  15241. mutex_lock(&cd->system_lock);
  15242. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15243. mutex_unlock(&cd->system_lock);
  15244. }
  15245. /* --------------- TP_XRES BIST TEST --------------- */
  15246. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  15247. pt_debug(dev, DL_INFO,
  15248. "%s: ----- Start TP_XRES BIST -----", __func__);
  15249. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  15250. &xres_toggled, xres_err_str);
  15251. /* Done if the rest of all nets toggled */
  15252. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  15253. goto host_nets_complete;
  15254. }
  15255. /* Flush bus in case a PIP response is waiting from previous test */
  15256. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15257. /* --------------- IRQ BIST TEST --------------- */
  15258. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  15259. pt_debug(dev, DL_INFO,
  15260. "%s: ----- Start IRQ BIST -----", __func__);
  15261. bus_toggled = 0xFF;
  15262. irq_toggled = 0xFF;
  15263. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  15264. &xres_toggled, irq_err_str);
  15265. /* If this net failed clear results from previous net */
  15266. if (irq_toggled != 1) {
  15267. xres_toggled = 0x0F;
  15268. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15269. }
  15270. if (bus_toggled == 1 && irq_toggled == 1)
  15271. goto host_nets_complete;
  15272. }
  15273. /* Flush bus in case a PIP response is waiting from previous test */
  15274. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  15275. /* --------------- BUS BIST TEST --------------- */
  15276. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15277. pt_debug(dev, DL_INFO,
  15278. "%s: ----- Start BUS BIST -----", __func__);
  15279. bus_toggled = 0xFF;
  15280. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15281. /* If this net failed clear results from previous net */
  15282. if (bus_toggled == 0) {
  15283. irq_toggled = 0x0F;
  15284. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15285. }
  15286. }
  15287. host_nets_complete:
  15288. /* --------------- SLAVE XRES BIST TEST --------------- */
  15289. if (cd->multi_chip &&
  15290. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15291. pt_debug(dev, DL_INFO,
  15292. "%s: ----- Start Slave XRES BIST -----", __func__);
  15293. slave_xres_toggled = 0xFF;
  15294. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15295. &slave_bus_toggled, &slave_xres_toggled,
  15296. slave_xres_err_str);
  15297. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15298. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15299. goto print_results;
  15300. }
  15301. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15302. if (cd->multi_chip &&
  15303. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15304. pt_debug(dev, DL_INFO,
  15305. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15306. slave_irq_toggled = 0xFF;
  15307. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15308. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15309. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15310. __func__, slave_irq_toggled);
  15311. if (slave_irq_toggled == 1) {
  15312. slave_bus_toggled = 1;
  15313. goto print_results;
  15314. }
  15315. }
  15316. /* --------------- SLAVE BUS BIST TEST --------------- */
  15317. if (cd->multi_chip &&
  15318. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15319. pt_debug(dev, DL_INFO,
  15320. "%s: ----- Start Slave BUS BIST -----", __func__);
  15321. slave_bus_toggled = 0xFF;
  15322. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15323. &slave_bus_toggled, slave_bus_err_str);
  15324. }
  15325. print_results:
  15326. /* Restore PIP command timeout */
  15327. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15328. /*
  15329. * We're done! - Perform a hard XRES toggle, allowing BL
  15330. * to load FW if there is any in Flash
  15331. */
  15332. mutex_lock(&cd->system_lock);
  15333. cd->hid_reset_cmd_state = 0;
  15334. mutex_unlock(&cd->system_lock);
  15335. pt_debug(dev, DL_INFO,
  15336. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15337. if (cd->flashless_dut) {
  15338. /*
  15339. * For Flashless solution, FW update is triggered after BL is
  15340. * seen that several miliseconds delay is needed.
  15341. */
  15342. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15343. __func__);
  15344. mutex_lock(&cd->system_lock);
  15345. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15346. mutex_unlock(&cd->system_lock);
  15347. /* Reset DUT and wait 100ms to see if loader started */
  15348. pt_hw_hard_reset(cd);
  15349. msleep(100);
  15350. if (cd->fw_updating) {
  15351. pt_debug(dev, DL_INFO,
  15352. "%s: ----- BIST Wait FW Loading ----",
  15353. __func__);
  15354. rc = _pt_request_wait_for_enum_state(
  15355. dev, 4000, STARTUP_STATUS_COMPLETE);
  15356. }
  15357. } else {
  15358. if (cd->mode == PT_MODE_BOOTLOADER) {
  15359. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15360. if (rc) {
  15361. pt_debug(dev, DL_ERROR,
  15362. "%s Failed to launch app\n", __func__);
  15363. rc = pt_hw_hard_reset(cd);
  15364. }
  15365. }
  15366. /*
  15367. * If FW exists the BL may have just started or will start soon,
  15368. * so the FW sentinel may be on it's way but with no FW it will
  15369. * not arrive, wait for it before deciding if we need to queue
  15370. * an enum.
  15371. */
  15372. rc = _pt_request_wait_for_enum_state(
  15373. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15374. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15375. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15376. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15377. __func__);
  15378. pt_queue_enum(cd);
  15379. rc = _pt_request_wait_for_enum_state(
  15380. dev, 2000, STARTUP_STATUS_COMPLETE);
  15381. }
  15382. }
  15383. msleep(20);
  15384. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15385. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15386. pt_start_wd_timer(cd);
  15387. /* Canned print if any memory allocation issues */
  15388. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15389. ret = scnprintf(buf, strlen(buf),
  15390. "Status: %d\n"
  15391. "I2C (SDA,SCL): [UNTEST]\n"
  15392. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15393. "IRQ connection: [UNTEST]\n"
  15394. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15395. if (cd->multi_chip) {
  15396. ret += scnprintf(buf + ret, strlen(buf),
  15397. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15398. "I/P IRQ connection: [UNTEST]\n"
  15399. "I/P TP_XRES connection: [UNTEST]\n");
  15400. }
  15401. } else {
  15402. status = 0;
  15403. if (bus_toggled == 1)
  15404. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15405. if (irq_toggled == 1)
  15406. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15407. if (xres_toggled == 1)
  15408. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15409. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15410. status += bus_toggled;
  15411. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15412. status += irq_toggled;
  15413. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15414. status += xres_toggled;
  15415. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15416. __func__, status, bus_toggled, irq_toggled,
  15417. xres_toggled);
  15418. if (cd->multi_chip) {
  15419. if (slave_irq_toggled == 1)
  15420. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15421. if (slave_xres_toggled == 1)
  15422. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15423. if (slave_bus_toggled == 1)
  15424. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15425. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15426. status += slave_bus_toggled;
  15427. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15428. status += slave_irq_toggled;
  15429. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15430. status += slave_xres_toggled;
  15431. pt_debug(dev, DL_ERROR,
  15432. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15433. __func__, status, bus_toggled, irq_toggled,
  15434. xres_toggled, slave_bus_toggled,
  15435. slave_irq_toggled, slave_xres_toggled);
  15436. }
  15437. if (cd->bus_ops->bustype == BUS_I2C)
  15438. i2c_toggled = bus_toggled;
  15439. else
  15440. spi_toggled = bus_toggled;
  15441. ret = scnprintf(buf, strlen(buf),
  15442. "Status: %d\n"
  15443. "I2C (SDA,SCL): %s %s\n"
  15444. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15445. "IRQ connection: %s %s\n"
  15446. "TP_XRES connection: %s %s\n",
  15447. status == num_tests ? 0 : 1,
  15448. i2c_toggled == 0x0F ? "[UNTEST]" :
  15449. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15450. i2c_toggled == 0x0F ? "" : bus_err_str,
  15451. spi_toggled == 0x0F ? "[UNTEST]" :
  15452. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15453. spi_toggled == 0x0F ? "" : bus_err_str,
  15454. irq_toggled == 0x0F ? "[UNTEST]" :
  15455. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15456. irq_err_str,
  15457. xres_toggled == 0x0F ? "[UNTEST]" :
  15458. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15459. xres_err_str);
  15460. if (cd->multi_chip) {
  15461. ret += scnprintf(buf + ret, strlen(buf),
  15462. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15463. "I/P IRQ connection: %s %s\n"
  15464. "I/P TP_XRES connection: %s %s\n",
  15465. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15466. slave_bus_toggled == 1 ? "[ OK ]" :
  15467. "[FAILED]", slave_bus_err_str,
  15468. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15469. slave_irq_toggled == 1 ? "[ OK ]" :
  15470. "[FAILED]", slave_irq_err_str,
  15471. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15472. slave_xres_toggled == 1 ? "[ OK ]" :
  15473. "[FAILED]", slave_xres_err_str);
  15474. }
  15475. }
  15476. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15477. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15478. __func__, cd->startup_status);
  15479. kfree(bus_err_str);
  15480. kfree(irq_err_str);
  15481. kfree(xres_err_str);
  15482. return ret;
  15483. }
  15484. /*******************************************************************************
  15485. * FUNCTION: pt_ttdl_bist_store
  15486. *
  15487. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15488. *
  15489. * RETURN: Size of passed in buffer if successful
  15490. *
  15491. * PARAMETERS:
  15492. * *dev - pointer to device structure
  15493. * *attr - pointer to device attributes
  15494. * *buf - pointer to command buffer
  15495. * size - size of buf
  15496. ******************************************************************************/
  15497. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15498. struct device_attribute *attr, const char *buf, size_t size)
  15499. {
  15500. struct pt_core_data *cd = dev_get_drvdata(dev);
  15501. u32 input_data[2] = {0};
  15502. int length;
  15503. int rc = 0;
  15504. /* Maximum input of one value */
  15505. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15506. ARRAY_SIZE(input_data));
  15507. if (length != 1) {
  15508. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15509. __func__);
  15510. rc = -EINVAL;
  15511. goto exit;
  15512. } else {
  15513. mutex_lock(&cd->system_lock);
  15514. cd->ttdl_bist_select = input_data[0];
  15515. mutex_unlock(&cd->system_lock);
  15516. }
  15517. exit:
  15518. if (rc)
  15519. return rc;
  15520. return size;
  15521. }
  15522. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15523. pt_ttdl_bist_store);
  15524. /*******************************************************************************
  15525. * FUNCTION: pt_flush_bus_store
  15526. *
  15527. * SUMMARY: Store method for the flush_bus sysfs node.
  15528. *
  15529. * RETURN: Size of passed in buffer if successful
  15530. *
  15531. * PARAMETERS:
  15532. * *dev - pointer to device structure
  15533. * *attr - pointer to device attributes
  15534. * *buf - pointer to command buffer
  15535. * size - size of buf
  15536. ******************************************************************************/
  15537. static ssize_t pt_flush_bus_store(struct device *dev,
  15538. struct device_attribute *attr, const char *buf, size_t size)
  15539. {
  15540. struct pt_core_data *cd = dev_get_drvdata(dev);
  15541. u32 input_data[2] = {0};
  15542. int length;
  15543. int rc = 0;
  15544. /* Maximum input of one value */
  15545. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15546. ARRAY_SIZE(input_data));
  15547. if (length != 1) {
  15548. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15549. __func__);
  15550. rc = -EINVAL;
  15551. goto exit;
  15552. }
  15553. mutex_lock(&cd->system_lock);
  15554. if (input_data[0] == 0)
  15555. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15556. else if (input_data[0] == 1)
  15557. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15558. else
  15559. rc = -EINVAL;
  15560. mutex_unlock(&cd->system_lock);
  15561. exit:
  15562. if (rc)
  15563. return rc;
  15564. return size;
  15565. }
  15566. /*******************************************************************************
  15567. * FUNCTION: pt_flush_bus_show
  15568. *
  15569. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15570. * based on either the size of the first two bytes or a blind 256 bytes.
  15571. *
  15572. * RETURN:
  15573. * 0 = success
  15574. * !0 = failure
  15575. *
  15576. * PARAMETERS:
  15577. * *dev - pointer to device structure
  15578. * *attr - pointer to device attributes
  15579. * *buf - pointer to output buffer
  15580. ******************************************************************************/
  15581. static ssize_t pt_flush_bus_show(struct device *dev,
  15582. struct device_attribute *attr, char *buf)
  15583. {
  15584. ssize_t ret = 0;
  15585. ssize_t bytes = 0;
  15586. struct pt_core_data *cd = dev_get_drvdata(dev);
  15587. mutex_lock(&cd->system_lock);
  15588. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15589. ret = scnprintf(buf, strlen(buf),
  15590. "Status: 0\n"
  15591. "%s: %zd\n",
  15592. "Bytes flushed", bytes);
  15593. mutex_unlock(&cd->system_lock);
  15594. return ret;
  15595. }
  15596. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15597. pt_flush_bus_store);
  15598. /*******************************************************************************
  15599. * FUNCTION: pt_pip2_ping_test_store
  15600. *
  15601. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15602. *
  15603. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15604. * someone may want to test sending invalid packet lengths so any values
  15605. * up to 255 are allowed.
  15606. *
  15607. * RETURN: Size of passed in buffer if successful
  15608. *
  15609. * PARAMETERS:
  15610. * *dev - pointer to device structure
  15611. * *attr - pointer to device attributes
  15612. * *buf - pointer to command buffer
  15613. * size - size of buf
  15614. ******************************************************************************/
  15615. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15616. struct device_attribute *attr, const char *buf, size_t size)
  15617. {
  15618. struct pt_core_data *cd = dev_get_drvdata(dev);
  15619. u32 input_data[2];
  15620. int length;
  15621. int rc = 0;
  15622. /* Maximum input of one value */
  15623. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15624. ARRAY_SIZE(input_data));
  15625. if (length != 1) {
  15626. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15627. __func__);
  15628. rc = -EINVAL;
  15629. goto exit;
  15630. }
  15631. mutex_lock(&cd->system_lock);
  15632. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15633. cd->ping_test_size = input_data[0];
  15634. else
  15635. rc = -EINVAL;
  15636. mutex_unlock(&cd->system_lock);
  15637. exit:
  15638. if (rc)
  15639. return rc;
  15640. return size;
  15641. }
  15642. /*******************************************************************************
  15643. * FUNCTION: pt_ping_test_show
  15644. *
  15645. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15646. * PING command and ramps up the optional payload from 0 to
  15647. * ping_test_size.
  15648. * The max payload size is 247:
  15649. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15650. *
  15651. * RETURN:
  15652. * 0 = success
  15653. * !0 = failure
  15654. *
  15655. * PARAMETERS:
  15656. * *dev - pointer to device structure
  15657. * *attr - pointer to device attributes
  15658. * *buf - pointer to output buffer
  15659. ******************************************************************************/
  15660. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15661. struct device_attribute *attr, char *buf)
  15662. {
  15663. struct pt_core_data *cd = dev_get_drvdata(dev);
  15664. ssize_t ret;
  15665. int last_packet_size;
  15666. int rc = 0;
  15667. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15668. if (rc) {
  15669. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15670. return ret;
  15671. }
  15672. ret = scnprintf(buf, strlen(buf),
  15673. "Status: %d\n"
  15674. "PING payload test passed with packet sizes 0 - %d\n",
  15675. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15676. last_packet_size);
  15677. return ret;
  15678. }
  15679. /*******************************************************************************
  15680. * FUNCTION: pt_t_refresh_store
  15681. *
  15682. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15683. * in integer as the number of interrupts to count. A timer is started to
  15684. * calculate the total time it takes to see that number of interrupts.
  15685. *
  15686. * RETURN: Size of passed in buffer if successful
  15687. *
  15688. * PARAMETERS:
  15689. * *dev - pointer to device structure
  15690. * *attr - pointer to device attributes
  15691. * *buf - pointer to buffer that hold the command parameters
  15692. * size - size of buf
  15693. ******************************************************************************/
  15694. static ssize_t pt_t_refresh_store(struct device *dev,
  15695. struct device_attribute *attr, const char *buf, size_t size)
  15696. {
  15697. struct pt_core_data *cd = dev_get_drvdata(dev);
  15698. u32 input_data[2];
  15699. int length;
  15700. int rc = 0;
  15701. /* Maximum input of one value */
  15702. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15703. ARRAY_SIZE(input_data));
  15704. if (length != 1) {
  15705. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15706. __func__);
  15707. rc = -EINVAL;
  15708. goto exit;
  15709. }
  15710. mutex_lock(&cd->system_lock);
  15711. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15712. input_data[0]);
  15713. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15714. cd->t_refresh_total = input_data[0];
  15715. cd->t_refresh_count = 0;
  15716. cd->t_refresh_active = 1;
  15717. } else {
  15718. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15719. rc = -EINVAL;
  15720. }
  15721. mutex_unlock(&cd->system_lock);
  15722. exit:
  15723. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15724. if (rc)
  15725. return rc;
  15726. return size;
  15727. }
  15728. /*******************************************************************************
  15729. * FUNCTION: pt_t_refresh_show
  15730. *
  15731. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15732. * of the T-Refresh timer counting the time it takes to see a user defined
  15733. * number of interrupts.
  15734. *
  15735. * RETURN:
  15736. * 0 = success
  15737. * !0 = failure
  15738. *
  15739. * PARAMETERS:
  15740. * *dev - pointer to device structure
  15741. * *attr - pointer to device attributes
  15742. * *buf - pointer to output buffer
  15743. ******************************************************************************/
  15744. static ssize_t pt_t_refresh_show(struct device *dev,
  15745. struct device_attribute *attr, char *buf)
  15746. {
  15747. ssize_t ret = 0;
  15748. struct pt_core_data *cd = dev_get_drvdata(dev);
  15749. u32 whole;
  15750. u16 fraction;
  15751. mutex_lock(&cd->system_lock);
  15752. /* Check if we have counted the number requested */
  15753. if (cd->t_refresh_count != cd->t_refresh_total) {
  15754. ret = scnprintf(buf, strlen(buf),
  15755. "Status: 0\n"
  15756. "%s: %d\n",
  15757. "Still counting... current IRQ count",
  15758. cd->t_refresh_count);
  15759. } else {
  15760. /* Ensure T-Refresh is de-activated */
  15761. cd->t_refresh_active = 0;
  15762. whole = cd->t_refresh_time / cd->t_refresh_count;
  15763. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15764. fraction = fraction * 1000 / cd->t_refresh_count;
  15765. ret = scnprintf(buf, strlen(buf),
  15766. "Status: 0\n"
  15767. "%s: %d\n"
  15768. "%s: %d\n"
  15769. "%s: %d\n"
  15770. "%s: %d.%02d\n",
  15771. "Requested IRQ Count ", cd->t_refresh_total,
  15772. "IRQ Counted ", cd->t_refresh_count,
  15773. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15774. "Average T-Refresh (ms) ", whole, fraction);
  15775. }
  15776. mutex_unlock(&cd->system_lock);
  15777. return ret;
  15778. }
  15779. /*******************************************************************************
  15780. * FUNCTION: pt_dut_status_show
  15781. *
  15782. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15783. * more items such as operation mode,easywake state are added in the future.
  15784. *
  15785. * RETURN: Char buffer with printed scan status information
  15786. *
  15787. * PARAMETERS:
  15788. * *dev - pointer to device structure
  15789. * *attr - pointer to device attributes
  15790. * *buf - pointer to output buffer
  15791. ******************************************************************************/
  15792. static ssize_t pt_dut_status_show(struct device *dev,
  15793. struct device_attribute *attr, char *buf)
  15794. {
  15795. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15796. u8 mode = PT_MODE_UNKNOWN;
  15797. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15798. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15799. struct pt_core_data *cd = dev_get_drvdata(dev);
  15800. ssize_t ret;
  15801. u16 calculated_crc = 0;
  15802. u16 stored_crc = 0;
  15803. u8 status;
  15804. int rc = 0;
  15805. /* In STANDBY the DUT will not repond to any PIP cmd */
  15806. if (cd->fw_sys_mode_in_standby_state) {
  15807. mode = PT_MODE_OPERATIONAL;
  15808. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15809. goto print_limited_results;
  15810. }
  15811. /* Retrieve mode and FW system mode which can only be 0-4 */
  15812. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15813. if (rc || mode == PT_MODE_UNKNOWN) {
  15814. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15815. "%s: %d\n"
  15816. "%s: n/a\n"
  15817. "%s: n/a\n"
  15818. "%s: n/a\n"
  15819. "%s: n/a\n",
  15820. "Status", rc,
  15821. "Active Exec ",
  15822. "FW System Mode ",
  15823. "Stored CRC ",
  15824. "Calculated CRC ");
  15825. return ret;
  15826. } else {
  15827. if (mode == PT_MODE_OPERATIONAL) {
  15828. if (sys_mode > FW_SYS_MODE_MAX)
  15829. sys_mode = FW_SYS_MODE_UNDEFINED;
  15830. if (sys_mode != FW_SYS_MODE_TEST)
  15831. goto print_limited_results;
  15832. rc = pt_pip_verify_config_block_crc_(cd,
  15833. PT_TCH_PARM_EBID, &status,
  15834. &calculated_crc, &stored_crc);
  15835. if (rc)
  15836. goto print_limited_results;
  15837. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15838. "%s: %d\n"
  15839. "%s: %s\n"
  15840. "%s: %s\n"
  15841. "%s: 0x%04X\n"
  15842. "%s: 0x%04X\n",
  15843. "Status", rc,
  15844. "Active Exec ", "FW",
  15845. "FW System Mode ", outputstring[sys_mode],
  15846. "Stored CRC ", stored_crc,
  15847. "Calculated CRC ", calculated_crc);
  15848. return ret;
  15849. } else {
  15850. /* When in BL or unknon mode Active Exec is "n/a" */
  15851. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15852. }
  15853. }
  15854. print_limited_results:
  15855. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15856. "%s: %d\n"
  15857. "%s: %s\n"
  15858. "%s: %s\n"
  15859. "%s: n/a\n"
  15860. "%s: n/a\n",
  15861. "Status", rc,
  15862. "Active Exec ",
  15863. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15864. "FW System Mode ", outputstring[sys_mode],
  15865. "Stored CRC ",
  15866. "Calculated CRC ");
  15867. return ret;
  15868. }
  15869. #endif /* TTDL_DIAGNOSTICS */
  15870. /*******************************************************************************
  15871. * Structures of sysfs attributes for all DUT dependent sysfs node
  15872. ******************************************************************************/
  15873. static struct attribute *early_attrs[] = {
  15874. &dev_attr_hw_version.attr,
  15875. &dev_attr_drv_version.attr,
  15876. &dev_attr_drv_ver.attr,
  15877. &dev_attr_fw_version.attr,
  15878. &dev_attr_sysinfo.attr,
  15879. &dev_attr_pip2_cmd_rsp.attr,
  15880. &dev_attr_command.attr,
  15881. &dev_attr_drv_debug.attr,
  15882. &dev_attr_hw_reset.attr,
  15883. &dev_attr_response.attr,
  15884. &dev_attr_ttdl_restart.attr,
  15885. #ifdef TTDL_DIAGNOSTICS
  15886. &dev_attr_ttdl_status.attr,
  15887. &dev_attr_pip2_enter_bl.attr,
  15888. &dev_attr_pip2_exit_bl.attr,
  15889. &dev_attr_err_gpio.attr,
  15890. &dev_attr_flush_bus.attr,
  15891. &dev_attr_ttdl_bist.attr,
  15892. #endif /* TTDL_DIAGNOSTICS */
  15893. NULL,
  15894. };
  15895. static struct attribute_group early_attr_group = {
  15896. .attrs = early_attrs,
  15897. };
  15898. static struct device_attribute pip2_attributes[] = {
  15899. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15900. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15901. #ifdef TTDL_DIAGNOSTICS
  15902. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15903. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15904. pt_pip2_ping_test_store),
  15905. #endif
  15906. };
  15907. static struct device_attribute attributes[] = {
  15908. __ATTR(dut_debug, 0644,
  15909. pt_dut_debug_show, pt_drv_debug_store),
  15910. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15911. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15912. __ATTR(get_param, 0644,
  15913. pt_get_param_show, pt_get_param_store),
  15914. __ATTR(pt_touch_offload, 0644,
  15915. NULL, pt_touch_offload_store),
  15916. #ifdef EASYWAKE_TSG6
  15917. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15918. pt_easy_wakeup_gesture_store),
  15919. __ATTR(easy_wakeup_gesture_id, 0444,
  15920. pt_easy_wakeup_gesture_id_show, NULL),
  15921. __ATTR(easy_wakeup_gesture_data, 0444,
  15922. pt_easy_wakeup_gesture_data_show, NULL),
  15923. #endif
  15924. #ifdef TTDL_DIAGNOSTICS
  15925. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15926. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15927. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15928. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15929. #endif /* TTDL_DIAGNOSTICS */
  15930. };
  15931. /*******************************************************************************
  15932. * FUNCTION: add_sysfs_interfaces
  15933. *
  15934. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15935. *
  15936. * RETURN:
  15937. * 0 = success
  15938. * !0 = failure
  15939. *
  15940. * PARAMETERS:
  15941. * *dev - pointer to device structure
  15942. ******************************************************************************/
  15943. static int add_sysfs_interfaces(struct device *dev)
  15944. {
  15945. int i;
  15946. int j = 0;
  15947. struct pt_core_data *cd = dev_get_drvdata(dev);
  15948. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15949. if (device_create_file(dev, attributes + i))
  15950. goto undo;
  15951. }
  15952. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15953. __func__, cd->active_dut_generation);
  15954. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15955. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15956. if (device_create_file(dev, pip2_attributes + j))
  15957. goto undo;
  15958. }
  15959. }
  15960. return 0;
  15961. undo:
  15962. for (i--; i >= 0; i--)
  15963. device_remove_file(dev, attributes + i);
  15964. for (j--; j >= 0; j--)
  15965. device_remove_file(dev, pip2_attributes + j);
  15966. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15967. __func__);
  15968. return -ENODEV;
  15969. }
  15970. /*******************************************************************************
  15971. * FUNCTION: remove_sysfs_interfaces
  15972. *
  15973. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15974. *
  15975. * RETURN: void
  15976. *
  15977. * PARAMETERS:
  15978. * *dev - pointer to device structure
  15979. ******************************************************************************/
  15980. static void remove_sysfs_interfaces(struct device *dev)
  15981. {
  15982. int i;
  15983. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15984. device_remove_file(dev, attributes + i);
  15985. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15986. device_remove_file(dev, pip2_attributes + i);
  15987. }
  15988. /*******************************************************************************
  15989. * FUNCTION: remove_sysfs_and_modules
  15990. *
  15991. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15992. *
  15993. * RETURN: void
  15994. *
  15995. * PARAMETERS:
  15996. * *dev - pointer to device structure
  15997. ******************************************************************************/
  15998. static void remove_sysfs_and_modules(struct device *dev)
  15999. {
  16000. struct pt_core_data *cd = dev_get_drvdata(dev);
  16001. /* Queued work should be removed before to release loader module */
  16002. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16003. pt_release_modules(cd);
  16004. pt_btn_release(dev);
  16005. pt_mt_release(dev);
  16006. remove_sysfs_interfaces(dev);
  16007. }
  16008. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  16009. {
  16010. int retval;
  16011. /* Get pinctrl if target uses pinctrl */
  16012. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  16013. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  16014. retval = PTR_ERR(cd->ts_pinctrl);
  16015. dev_dbg(cd->dev,
  16016. "Target does not use pinctrl %d\n", retval);
  16017. goto err_pinctrl_get;
  16018. }
  16019. cd->pinctrl_state_active
  16020. = pinctrl_lookup_state(cd->ts_pinctrl,
  16021. PINCTRL_STATE_ACTIVE);
  16022. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  16023. retval = PTR_ERR(cd->pinctrl_state_active);
  16024. dev_err(cd->dev,
  16025. "Can not lookup %s pinstate %d\n",
  16026. PINCTRL_STATE_ACTIVE, retval);
  16027. goto err_pinctrl_lookup;
  16028. }
  16029. cd->pinctrl_state_suspend
  16030. = pinctrl_lookup_state(cd->ts_pinctrl,
  16031. PINCTRL_STATE_SUSPEND);
  16032. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  16033. retval = PTR_ERR(cd->pinctrl_state_suspend);
  16034. dev_err(cd->dev,
  16035. "Can not lookup %s pinstate %d\n",
  16036. PINCTRL_STATE_SUSPEND, retval);
  16037. goto err_pinctrl_lookup;
  16038. }
  16039. cd->pinctrl_state_release
  16040. = pinctrl_lookup_state(cd->ts_pinctrl,
  16041. PINCTRL_STATE_RELEASE);
  16042. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  16043. retval = PTR_ERR(cd->pinctrl_state_release);
  16044. dev_dbg(cd->dev,
  16045. "Can not lookup %s pinstate %d\n",
  16046. PINCTRL_STATE_RELEASE, retval);
  16047. }
  16048. return 0;
  16049. err_pinctrl_lookup:
  16050. devm_pinctrl_put(cd->ts_pinctrl);
  16051. err_pinctrl_get:
  16052. cd->ts_pinctrl = NULL;
  16053. return retval;
  16054. }
  16055. /*******************************************************************************
  16056. *******************************************************************************
  16057. * FUNCTION: pt_probe
  16058. *
  16059. * SUMMARY: Probe of the core module.
  16060. *
  16061. * NOTE: For the Parade Technologies development platform (PtSBC) the
  16062. * probe functionality is split into two functions; pt_probe() and
  16063. * pt_probe_complete(). the initial setup is done in this function which
  16064. * then creates a WORK task which runs after the probe timer expires. This
  16065. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  16066. *
  16067. * RETURN:
  16068. * 0 = success
  16069. * !0 = failure
  16070. *
  16071. * PARAMETERS:
  16072. * *ops - pointer to the bus
  16073. * *dev - pointer to the device structure
  16074. * irq - IRQ
  16075. * xfer_buf_size - size of the buffer
  16076. ******************************************************************************/
  16077. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  16078. u16 irq, size_t xfer_buf_size)
  16079. {
  16080. struct pt_core_data *cd;
  16081. struct pt_platform_data *pdata = dev_get_platdata(dev);
  16082. enum pt_atten_type type;
  16083. struct i2c_client *client = to_i2c_client(dev);
  16084. int rc = 0;
  16085. u8 pip_ver_major;
  16086. u8 pip_ver_minor;
  16087. u32 status = STARTUP_STATUS_START;
  16088. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  16089. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  16090. __func__);
  16091. rc = -ENODEV;
  16092. goto error_no_pdata;
  16093. }
  16094. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  16095. if (!pdata->core_pdata->power) {
  16096. pt_debug(dev, DL_ERROR,
  16097. "%s: Missing platform data function\n",
  16098. __func__);
  16099. rc = -ENODEV;
  16100. goto error_no_pdata;
  16101. }
  16102. }
  16103. /* get context and debug print buffers */
  16104. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  16105. if (!cd) {
  16106. rc = -ENOMEM;
  16107. goto error_no_pdata;
  16108. }
  16109. /* Initialize device info */
  16110. cd->dev = dev;
  16111. cd->pdata = pdata;
  16112. cd->cpdata = pdata->core_pdata;
  16113. cd->bus_ops = ops;
  16114. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  16115. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  16116. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  16117. cd->hw_detected = false;
  16118. cd->pip2_prot_active = false;
  16119. cd->pip2_send_user_cmd = false;
  16120. cd->bl_pip_ver_ready = false;
  16121. cd->app_pip_ver_ready = false;
  16122. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  16123. cd->get_param_id = 0;
  16124. cd->watchdog_enabled = 0;
  16125. cd->startup_retry_count = 0;
  16126. cd->core_probe_complete = 0;
  16127. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  16128. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16129. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  16130. cd->flashless_dut = 0;
  16131. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  16132. cd->bl_with_no_int = 0;
  16133. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  16134. cd->multi_chip = PT_FEATURE_DISABLE;
  16135. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  16136. cd->sleep_state = SS_SLEEP_NONE;
  16137. cd->quick_boot = false;
  16138. cd->drv_debug_suspend = false;
  16139. cd->touch_offload = false;
  16140. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  16141. cd->set_dut_generation = true;
  16142. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16143. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  16144. cd->set_dut_generation = true;
  16145. cd->active_dut_generation = DUT_PIP1_ONLY;
  16146. } else {
  16147. cd->set_dut_generation = false;
  16148. cd->active_dut_generation = DUT_UNKNOWN;
  16149. }
  16150. /* Initialize with platform data */
  16151. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  16152. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  16153. cd->hid_cmd_state = 1;
  16154. cd->fw_updating = false;
  16155. cd->multi_chip = 0;
  16156. #ifdef TTDL_DIAGNOSTICS
  16157. cd->t_refresh_active = 0;
  16158. cd->t_refresh_count = 0;
  16159. cd->pip2_crc_error_count = 0;
  16160. cd->wd_xres_count = 0;
  16161. cd->bl_retry_packet_count = 0;
  16162. cd->file_erase_timeout_count = 0;
  16163. cd->show_tt_data = false;
  16164. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  16165. cd->err_gpio = 0;
  16166. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  16167. cd->ttdl_bist_select = 0x07;
  16168. cd->force_pip2_seq = 0;
  16169. #endif /* TTDL_DIAGNOSTICS */
  16170. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  16171. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  16172. sizeof(PT_PIP2_BIN_FILE_PATH));
  16173. pt_init_hid_descriptor(&cd->hid_desc);
  16174. /* Read and store the descriptor lengths */
  16175. cd->hid_core.hid_report_desc_len =
  16176. le16_to_cpu(cd->hid_desc.report_desc_len);
  16177. cd->hid_core.hid_max_input_len =
  16178. le16_to_cpu(cd->hid_desc.max_input_len);
  16179. cd->hid_core.hid_max_output_len =
  16180. le16_to_cpu(cd->hid_desc.max_output_len);
  16181. /* Initialize mutexes and spinlocks */
  16182. mutex_init(&cd->module_list_lock);
  16183. mutex_init(&cd->system_lock);
  16184. mutex_init(&cd->sysfs_lock);
  16185. mutex_init(&cd->ttdl_restart_lock);
  16186. mutex_init(&cd->firmware_class_lock);
  16187. spin_lock_init(&cd->spinlock);
  16188. /* Initialize module list */
  16189. INIT_LIST_HEAD(&cd->module_list);
  16190. /* Initialize attention lists */
  16191. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  16192. INIT_LIST_HEAD(&cd->atten_list[type]);
  16193. /* Initialize parameter list */
  16194. INIT_LIST_HEAD(&cd->param_list);
  16195. /* Initialize wait queue */
  16196. init_waitqueue_head(&cd->wait_q);
  16197. rc = pt_ts_pinctrl_init(cd);
  16198. if (!rc && cd->ts_pinctrl) {
  16199. /*
  16200. * Pinctrl handle is optional. If pinctrl handle is found
  16201. * let pins to be configured in active state. If not
  16202. * found continue further without error.
  16203. */
  16204. rc = pinctrl_select_state(cd->ts_pinctrl,
  16205. cd->pinctrl_state_active);
  16206. if (rc < 0)
  16207. dev_err(&client->dev, "failed to select pin to active state\n");
  16208. }
  16209. rc = pt_get_regulator(cd, true);
  16210. if (rc) {
  16211. dev_err(&client->dev, "Failed to get voltage regulators\n");
  16212. goto error_alloc_data;
  16213. }
  16214. rc = pt_enable_regulator(cd, true);
  16215. if (rc) {
  16216. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  16217. goto error_get_regulator;
  16218. }
  16219. /* Initialize works */
  16220. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  16221. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  16222. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  16223. /* Initialize HID specific data */
  16224. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  16225. cd->cpdata->vendor_id : HID_VENDOR_ID;
  16226. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  16227. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  16228. cd->hid_core.hid_desc_register =
  16229. cpu_to_le16(cd->cpdata->hid_desc_register);
  16230. /* Set platform easywake value */
  16231. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  16232. #if defined(CONFIG_DRM) || defined(CONFIG_PANEL_NOTIFIER)
  16233. /* Setup active dsi panel */
  16234. active_panel = cd->cpdata->active_panel;
  16235. #endif
  16236. /* Set platform panel_id value */
  16237. cd->panel_id_support = cd->cpdata->panel_id_support;
  16238. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  16239. /* Set Panel ID to default to 0 */
  16240. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  16241. else
  16242. /* Set Panel ID to Not Enabled */
  16243. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  16244. /* Initialize hw_version default to FFFF.FFFF.FF */
  16245. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  16246. dev_set_drvdata(dev, cd);
  16247. /* PtSBC builds will call this function in pt_probe_complete() */
  16248. pt_add_core(dev);
  16249. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  16250. if (rc) {
  16251. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  16252. __func__);
  16253. goto error_enable_regulator;
  16254. }
  16255. /*
  16256. * Save the pointer to a global value, which will be used
  16257. * in ttdl_restart function
  16258. */
  16259. cd->bus_ops = ops;
  16260. /*
  16261. * When the IRQ GPIO is not direclty accessible and no function is
  16262. * defined to get the IRQ status, the IRQ passed in must be assigned
  16263. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16264. */
  16265. if (!cd->cpdata->irq_stat) {
  16266. cd->irq = irq;
  16267. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  16268. __func__, cd->irq);
  16269. }
  16270. /* Call platform init function before setting up the GPIO's */
  16271. if (cd->cpdata->init) {
  16272. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16273. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16274. } else {
  16275. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  16276. __func__);
  16277. rc = 0;
  16278. }
  16279. if (rc < 0) {
  16280. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16281. __func__, rc);
  16282. }
  16283. /* Power on any needed regulator(s) */
  16284. if (cd->cpdata->setup_power) {
  16285. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16286. rc = cd->cpdata->setup_power(cd->cpdata,
  16287. PT_MT_POWER_ON, cd->dev);
  16288. } else {
  16289. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16290. __func__);
  16291. rc = 0;
  16292. }
  16293. if (rc < 0)
  16294. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16295. __func__, rc);
  16296. #ifdef TTDL_DIAGNOSTICS
  16297. cd->watchdog_irq_stuck_count = 0;
  16298. cd->bus_transmit_error_count = 0;
  16299. #endif /* TTDL_DIAGNOSTICS */
  16300. if (cd->cpdata->detect) {
  16301. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16302. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16303. pt_platform_detect_read);
  16304. if (!rc) {
  16305. cd->hw_detected = true;
  16306. pt_debug(cd->dev, DL_INFO,
  16307. "%s: HW detected\n", __func__);
  16308. } else {
  16309. cd->hw_detected = false;
  16310. pt_debug(cd->dev, DL_INFO,
  16311. "%s: No HW detected\n", __func__);
  16312. rc = -ENODEV;
  16313. goto error_detect;
  16314. }
  16315. } else {
  16316. pt_debug(dev, DL_ERROR,
  16317. "%s: PARADE No HW detect function pointer\n",
  16318. __func__);
  16319. /*
  16320. * "hw_reset" is not needed in the "if" statement,
  16321. * because "hw_reset" is already included in "hw_detect"
  16322. * function.
  16323. */
  16324. rc = pt_hw_hard_reset(cd);
  16325. if (rc)
  16326. pt_debug(cd->dev, DL_ERROR,
  16327. "%s: FAILED to execute HARD reset\n",
  16328. __func__);
  16329. }
  16330. if (cd->cpdata->setup_irq) {
  16331. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16332. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16333. if (rc) {
  16334. pt_debug(dev, DL_ERROR,
  16335. "%s: Error, couldn't setup IRQ\n", __func__);
  16336. goto error_setup_irq;
  16337. }
  16338. } else {
  16339. pt_debug(dev, DL_ERROR,
  16340. "%s: IRQ function pointer not setup\n",
  16341. __func__);
  16342. goto error_setup_irq;
  16343. }
  16344. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16345. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16346. (unsigned long)cd);
  16347. #else
  16348. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16349. #endif
  16350. pt_stop_wd_timer(cd);
  16351. #ifdef TTHE_TUNER_SUPPORT
  16352. mutex_init(&cd->tthe_lock);
  16353. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16354. 0644, NULL, cd, &tthe_debugfs_fops);
  16355. #endif
  16356. rc = device_init_wakeup(dev, 1);
  16357. if (rc < 0)
  16358. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16359. __func__, rc);
  16360. if (!enable_irq_wake(cd->irq)) {
  16361. cd->irq_wake = 1;
  16362. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16363. }
  16364. pm_runtime_get_noresume(dev);
  16365. pm_runtime_set_active(dev);
  16366. pm_runtime_enable(dev);
  16367. /* If IRQ asserted, read out all from buffer to release INT pin */
  16368. if (cd->cpdata->irq_stat) {
  16369. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16370. } else {
  16371. /* Force a read in case the reset sentinel already arrived */
  16372. rc = pt_read_input(cd);
  16373. if (!rc)
  16374. pt_parse_input(cd);
  16375. }
  16376. /* Without sleep DUT is not ready and will NAK the first write */
  16377. msleep(150);
  16378. /* Attempt to set the DUT generation if not yet set */
  16379. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16380. if (cd->bl_pip_ver_ready ||
  16381. (cd->app_pip_ver_ready &&
  16382. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16383. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16384. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16385. __func__, cd->active_dut_generation);
  16386. } else {
  16387. rc = _pt_detect_dut_generation(cd->dev,
  16388. &status, &cd->active_dut_generation,
  16389. &cd->mode);
  16390. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16391. || rc) {
  16392. pt_debug(cd->dev, DL_ERROR,
  16393. " === DUT Gen unknown, Skip Enum ===\n");
  16394. goto skip_enum;
  16395. }
  16396. }
  16397. }
  16398. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16399. &pip_ver_major, &pip_ver_minor);
  16400. if (pip_ver_major == 2) {
  16401. cd->bl_pip_ver_ready = true;
  16402. pt_debug(dev, DL_ERROR,
  16403. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16404. pip_ver_minor);
  16405. cd->hw_detected = true;
  16406. } else if (pip_ver_major == 1) {
  16407. cd->app_pip_ver_ready = true;
  16408. pt_debug(dev, DL_ERROR,
  16409. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16410. cd->hw_detected = true;
  16411. } else {
  16412. cd->sysinfo.ttdata.pip_ver_major = 0;
  16413. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16414. cd->app_pip_ver_ready = false;
  16415. cd->hw_detected = false;
  16416. pt_debug(dev, DL_ERROR,
  16417. " === PIP Version Not Detected, Skip Enum ===\n");
  16418. /* For legacy DUTS proceed, enum will attempt to launch app */
  16419. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16420. goto skip_enum;
  16421. }
  16422. rc = pt_enum_with_dut(cd, false, &status);
  16423. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16424. __func__, cd->startup_status, status);
  16425. if (rc == -ENODEV) {
  16426. pt_debug(cd->dev, DL_ERROR,
  16427. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16428. /* For PtSBC don't error out, allow TTDL to stay up */
  16429. rc = -EPROBE_DEFER;
  16430. goto error_after_startup;
  16431. }
  16432. /* Suspend scanning until probe is complete to avoid asyc touches */
  16433. pt_pip_suspend_scanning_(cd);
  16434. if (cd->hw_detected) {
  16435. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16436. __func__);
  16437. rc = add_sysfs_interfaces(dev);
  16438. if (rc < 0) {
  16439. pt_debug(dev, DL_ERROR,
  16440. "%s: Error, fail sysfs init\n", __func__);
  16441. goto error_after_startup;
  16442. }
  16443. } else {
  16444. pt_debug(dev, DL_ERROR,
  16445. "%s: No HW detected, sysfs interfaces not added\n",
  16446. __func__);
  16447. }
  16448. skip_enum:
  16449. pm_runtime_put_sync(dev);
  16450. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16451. rc = pt_mt_probe(dev);
  16452. if (rc < 0) {
  16453. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16454. __func__);
  16455. goto error_after_sysfs_create;
  16456. }
  16457. rc = pt_btn_probe(dev);
  16458. if (rc < 0) {
  16459. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16460. __func__);
  16461. goto error_after_startup_mt;
  16462. }
  16463. pt_probe_modules(cd);
  16464. #ifdef CONFIG_HAS_EARLYSUSPEND
  16465. pt_setup_early_suspend(cd);
  16466. #elif defined(CONFIG_PANEL_NOTIFIER)
  16467. pt_debug(dev, DL_ERROR, "%s: Probe: Setup Panel Event notifier\n", __func__);
  16468. pt_setup_panel_event_notifier(cd);
  16469. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16470. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16471. #elif defined(CONFIG_DRM)
  16472. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16473. pt_setup_drm_notifier(cd);
  16474. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16475. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16476. #elif defined(CONFIG_FB)
  16477. pt_setup_fb_notifier(cd);
  16478. #endif
  16479. #ifdef NEED_SUSPEND_NOTIFIER
  16480. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16481. register_pm_notifier(&cd->pm_notifier);
  16482. #endif
  16483. pt_pip_resume_scanning_(cd);
  16484. mutex_lock(&cd->system_lock);
  16485. cd->startup_status |= status;
  16486. cd->core_probe_complete = 1;
  16487. mutex_unlock(&cd->system_lock);
  16488. pt_core_state = STATE_RESUME;
  16489. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16490. __func__);
  16491. return 0;
  16492. error_after_startup_mt:
  16493. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16494. pt_mt_release(dev);
  16495. error_after_sysfs_create:
  16496. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16497. pm_runtime_disable(dev);
  16498. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16499. device_wakeup_disable(dev);
  16500. #endif
  16501. device_init_wakeup(dev, 0);
  16502. cancel_work_sync(&cd->enum_work);
  16503. pt_stop_wd_timer(cd);
  16504. pt_free_si_ptrs(cd);
  16505. remove_sysfs_interfaces(dev);
  16506. error_after_startup:
  16507. pr_err("%s PARADE error_after_startup\n", __func__);
  16508. del_timer(&cd->watchdog_timer);
  16509. if (cd->cpdata->setup_irq)
  16510. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16511. error_setup_irq:
  16512. error_detect:
  16513. if (cd->cpdata->init)
  16514. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16515. if (cd->cpdata->setup_power)
  16516. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16517. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16518. error_enable_regulator:
  16519. pt_del_core(dev);
  16520. dev_set_drvdata(dev, NULL);
  16521. pt_enable_regulator(cd, false);
  16522. error_get_regulator:
  16523. pt_get_regulator(cd, false);
  16524. error_alloc_data:
  16525. kfree(cd);
  16526. error_no_pdata:
  16527. pr_err("%s failed.\n", __func__);
  16528. return rc;
  16529. }
  16530. /*******************************************************************************
  16531. * FUNCTION: pt_device_entry
  16532. *
  16533. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  16534. * woke up or put to sleep based on Kernel power state even when the display
  16535. * is off based on the check of TTDL core platform flag.
  16536. *
  16537. * RETURN:
  16538. * 0 = success
  16539. * !0 = failure
  16540. *
  16541. * PARAMETERS:
  16542. * *dev - pointer to core device
  16543. ******************************************************************************/
  16544. int pt_device_entry(struct device *dev,
  16545. u16 irq, size_t xfer_buf_size)
  16546. {
  16547. struct pt_core_data *cd = dev_get_drvdata(dev);
  16548. struct pt_platform_data *pdata = dev_get_platdata(dev);
  16549. struct i2c_client *client = to_i2c_client(dev);
  16550. int rc = 0;
  16551. pt_debug(dev, DL_INFO, "%s: Start pt_device_entry\n", __func__);
  16552. cd->dev = dev;
  16553. cd->pdata = pdata;
  16554. cd->cpdata = pdata->core_pdata;
  16555. if (!rc && cd->ts_pinctrl) {
  16556. /*
  16557. * Pinctrl handle is optional. If pinctrl handle is found
  16558. * let pins to be configured in active state. If not
  16559. * found continue further without error.
  16560. */
  16561. rc = pinctrl_select_state(cd->ts_pinctrl, cd->pinctrl_state_active);
  16562. if (rc < 0)
  16563. dev_err(&client->dev, "failed to select pin to active state\n");
  16564. }
  16565. /* Set platform easywake value */
  16566. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  16567. /*
  16568. * When the IRQ GPIO is not direclty accessible and no function is
  16569. * defined to get the IRQ status, the IRQ passed in must be assigned
  16570. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  16571. */
  16572. if (!cd->cpdata->irq_stat) {
  16573. cd->irq = irq;
  16574. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n", __func__, cd->irq);
  16575. }
  16576. /* Call platform init function before setting up the GPIO's */
  16577. if (cd->cpdata->init) {
  16578. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  16579. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16580. } else {
  16581. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n", __func__);
  16582. rc = 0;
  16583. }
  16584. if (rc < 0) {
  16585. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n", __func__, rc);
  16586. }
  16587. /* Power on any needed regulator(s) */
  16588. if (cd->cpdata->setup_power) {
  16589. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16590. rc = cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  16591. } else {
  16592. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n", __func__);
  16593. rc = 0;
  16594. }
  16595. if (rc < 0)
  16596. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n", __func__, rc);
  16597. if (cd->cpdata->detect) {
  16598. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16599. rc = cd->cpdata->detect(cd->cpdata, cd->dev, pt_platform_detect_read);
  16600. if (!rc) {
  16601. cd->hw_detected = true;
  16602. pt_debug(cd->dev, DL_INFO, "%s: HW detected\n", __func__);
  16603. } else {
  16604. cd->hw_detected = false;
  16605. pt_debug(cd->dev, DL_INFO, "%s: No HW detected\n", __func__);
  16606. rc = -ENODEV;
  16607. goto pt_error_detect;
  16608. }
  16609. } else {
  16610. pt_debug(dev, DL_ERROR, "%s: PARADE No HW detect function pointer\n", __func__);
  16611. /*
  16612. * "hw_reset" is not needed in the "if" statement,
  16613. * because "hw_reset" is already included in "hw_detect"
  16614. * function.
  16615. */
  16616. rc = pt_hw_hard_reset(cd);
  16617. if (rc)
  16618. pt_debug(cd->dev, DL_ERROR, "%s: FAILED to execute HARD reset\n", __func__);
  16619. }
  16620. if (cd->cpdata->setup_irq) {
  16621. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16622. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16623. if (rc) {
  16624. pt_debug(dev, DL_ERROR, "%s: Error, couldn't setup IRQ\n", __func__);
  16625. goto pt_error_setup_irq;
  16626. }
  16627. } else {
  16628. pt_debug(dev, DL_ERROR, "%s: IRQ function pointer not setup\n", __func__);
  16629. goto pt_error_setup_irq;
  16630. }
  16631. rc = device_init_wakeup(dev, 1);
  16632. if (rc < 0)
  16633. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n", __func__, rc);
  16634. if (!enable_irq_wake(cd->irq)) {
  16635. cd->irq_wake = 1;
  16636. pt_debug(cd->dev, DL_WARN, "%s Device MAY wakeup\n", __func__);
  16637. }
  16638. pm_runtime_get_noresume(dev);
  16639. pm_runtime_set_active(dev);
  16640. pm_runtime_enable(dev);
  16641. /* Without sleep DUT is not ready and will NAK the first write */
  16642. msleep(150);
  16643. pm_runtime_put_sync(dev);
  16644. #if defined(CONFIG_PANEL_NOTIFIER)
  16645. /* Setup active dsi panel */
  16646. active_panel = cd->cpdata->active_panel;
  16647. pt_debug(dev, DL_ERROR, "%s: Probe: Setup Panel Event notifier\n", __func__);
  16648. pt_setup_panel_event_notifier(cd);
  16649. #endif
  16650. mutex_lock(&cd->system_lock);
  16651. cd->core_probe_complete = 1;
  16652. mutex_unlock(&cd->system_lock);
  16653. pt_debug(dev, DL_INFO, "%s: ####TTDL Core Device Probe Completed Successfully\n", __func__);
  16654. pt_core_state = STATE_RESUME;
  16655. return 0;
  16656. pt_error_setup_irq:
  16657. device_init_wakeup(dev, 0);
  16658. pt_error_detect:
  16659. if (cd->cpdata->init)
  16660. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16661. if (cd->cpdata->setup_power)
  16662. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16663. return rc;
  16664. }
  16665. EXPORT_SYMBOL_GPL(pt_probe);
  16666. /*******************************************************************************
  16667. * FUNCTION: pt_release
  16668. *
  16669. * SUMMARY: This function does the following cleanup:
  16670. * - Releases all probed modules
  16671. * - Stops the watchdog
  16672. * - Cancels all pending work tasks
  16673. * - Removes all created sysfs nodes
  16674. * - Removes all created debugfs nodes
  16675. * - Frees the IRQ
  16676. * - Deletes the core
  16677. * - Frees all pointers and HID reports
  16678. *
  16679. * RETURN:
  16680. * 0 = success
  16681. * !0 = failure
  16682. *
  16683. * PARAMETERS:
  16684. * *cd - pointer to the core data structure
  16685. ******************************************************************************/
  16686. int pt_release(struct pt_core_data *cd)
  16687. {
  16688. struct device *dev = cd->dev;
  16689. /*
  16690. * Suspend the device before freeing the startup_work and stopping
  16691. * the watchdog since sleep function restarts watchdog on failure
  16692. */
  16693. pm_runtime_suspend(dev);
  16694. pm_runtime_disable(dev);
  16695. /*
  16696. * Any 'work' that can trigger a new thread should be canceled first.
  16697. * The watchdog is also stopped again because another thread could have
  16698. * restarted it. The irq_work is cancelled last because it is used for
  16699. * all I2C/SPI communication.
  16700. */
  16701. pt_stop_wd_timer(cd);
  16702. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16703. cancel_work_sync(&cd->ttdl_restart_work);
  16704. cancel_work_sync(&cd->enum_work);
  16705. cancel_work_sync(&cd->resume_offload_work);
  16706. cancel_work_sync(&cd->suspend_offload_work);
  16707. cancel_work_sync(&cd->resume_work);
  16708. cancel_work_sync(&cd->suspend_work);
  16709. destroy_workqueue(cd->pt_workqueue);
  16710. pt_stop_wd_timer(cd);
  16711. pt_release_modules(cd);
  16712. pt_proximity_release(dev);
  16713. pt_btn_release(dev);
  16714. pt_mt_release(dev);
  16715. #ifdef CONFIG_HAS_EARLYSUSPEND
  16716. unregister_early_suspend(&cd->es);
  16717. #elif defined(CONFIG_PANEL_NOTIFIER)
  16718. if (active_panel)
  16719. panel_event_notifier_unregister(&cd->fb_notifier);
  16720. #elif defined(CONFIG_DRM)
  16721. if (active_panel)
  16722. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16723. #elif defined(CONFIG_FB)
  16724. fb_unregister_client(&cd->fb_notifier);
  16725. #endif
  16726. #ifdef NEED_SUSPEND_NOTIFIER
  16727. unregister_pm_notifier(&cd->pm_notifier);
  16728. #endif
  16729. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16730. device_wakeup_disable(dev);
  16731. #endif
  16732. device_init_wakeup(dev, 0);
  16733. #ifdef TTHE_TUNER_SUPPORT
  16734. mutex_lock(&cd->tthe_lock);
  16735. cd->tthe_exit = 1;
  16736. wake_up(&cd->wait_q);
  16737. mutex_unlock(&cd->tthe_lock);
  16738. debugfs_remove(cd->tthe_debugfs);
  16739. #endif
  16740. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16741. remove_sysfs_interfaces(dev);
  16742. disable_irq_nosync(cd->irq);
  16743. if (cd->cpdata->setup_irq)
  16744. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16745. if (cd->cpdata->init)
  16746. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16747. if (cd->cpdata->setup_power)
  16748. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16749. dev_set_drvdata(dev, NULL);
  16750. pt_del_core(dev);
  16751. if (cd->vcc_i2c)
  16752. regulator_set_load(cd->vcc_i2c, 0);
  16753. if (cd->vdd)
  16754. regulator_set_load(cd->vdd, 0);
  16755. pt_enable_regulator(cd, false);
  16756. pt_get_regulator(cd, false);
  16757. pt_free_si_ptrs(cd);
  16758. kfree(cd);
  16759. return 0;
  16760. }
  16761. EXPORT_SYMBOL_GPL(pt_release);
  16762. MODULE_LICENSE("GPL");
  16763. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16764. MODULE_AUTHOR("Parade Technologies <[email protected]>");