pt_core.c 516 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include <linux/suspend.h>
  33. #include "pt_regs.h"
  34. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  35. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  36. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  37. #define FT_VTG_MIN_UV 2800000
  38. #define FT_VTG_MAX_UV 2800000
  39. #define FT_I2C_VTG_MIN_UV 1800000
  40. #define FT_I2C_VTG_MAX_UV 1800000
  41. #define PWR_SUSPEND_LOAD_UA 165
  42. #define I2C_SUSPEND_LOAD_UA 100
  43. #define PWR_ACTIVE_LOAD_MA 12000
  44. #define I2C_ACTIVE_LOAD_MA 30000
  45. #define PT_CORE_STARTUP_RETRY_COUNT 3
  46. #define PT_STATUS_STR_LEN (50)
  47. #if defined(CONFIG_DRM)
  48. static struct drm_panel *active_panel;
  49. #endif
  50. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  51. #define ENABLE_I2C_REG_ONLY
  52. #ifdef ENABLE_I2C_REG_ONLY
  53. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en);
  54. #endif
  55. static const char *pt_driver_core_name = PT_CORE_NAME;
  56. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  57. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  58. struct pt_hid_field {
  59. int report_count;
  60. int report_size;
  61. int size; /* report_count * report_size */
  62. int offset;
  63. int data_type;
  64. int logical_min;
  65. int logical_max;
  66. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  67. u32 usage_page;
  68. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  69. struct pt_hid_report *report;
  70. bool record_field;
  71. };
  72. struct pt_hid_report {
  73. u8 id;
  74. u8 type;
  75. int size;
  76. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  77. int num_fields;
  78. int record_field_index;
  79. int header_size;
  80. int record_size;
  81. u32 usage_page;
  82. };
  83. struct atten_node {
  84. struct list_head node;
  85. char *id;
  86. struct device *dev;
  87. int (*func)(struct device *dev);
  88. int mode;
  89. };
  90. struct param_node {
  91. struct list_head node;
  92. u8 id;
  93. u32 value;
  94. u8 size;
  95. };
  96. struct module_node {
  97. struct list_head node;
  98. struct pt_module *module;
  99. void *data;
  100. };
  101. struct pt_hid_cmd {
  102. u8 opcode;
  103. u8 report_type;
  104. union {
  105. u8 report_id;
  106. u8 power_state;
  107. };
  108. u8 has_data_register;
  109. size_t write_length;
  110. u8 *write_buf;
  111. u8 *read_buf;
  112. u8 wait_interrupt;
  113. u8 reset_cmd;
  114. u16 timeout_ms;
  115. };
  116. struct pt_hid_output {
  117. u8 cmd_type;
  118. u16 length;
  119. u8 command_code;
  120. size_t write_length;
  121. u8 *write_buf;
  122. u8 novalidate;
  123. u8 reset_expected;
  124. u16 timeout_ms;
  125. };
  126. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  127. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  128. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  129. #define CREATE_PIP1_FW_CMD(command) \
  130. .cmd_type = PIP1_CMD_TYPE_FW, \
  131. .command_code = command
  132. #define CREATE_PIP1_BL_CMD(command) \
  133. .cmd_type = PIP1_CMD_TYPE_BL, \
  134. .command_code = command
  135. #define PT_MAX_PR_BUF_SIZE 2048
  136. /*******************************************************************************
  137. * FUNCTION: pt_pr_buf
  138. *
  139. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  140. *
  141. * RETURN: Void
  142. *
  143. * PARAMETERS:
  144. * *dev - pointer to Device structure
  145. * debug_level - requested debug level to print at
  146. * *buf - pointer to buffer to print
  147. * buf_len - size of buf
  148. * *data_name - Descriptive name of data prefixed to data
  149. ******************************************************************************/
  150. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  151. u16 buf_len, const char *data_name)
  152. {
  153. struct pt_core_data *cd = dev_get_drvdata(dev);
  154. int i;
  155. ssize_t pr_buf_index = 0;
  156. int max_size;
  157. /* only proceed if valid debug level and there is data to print */
  158. if (debug_level <= cd->debug_level && buf_len > 0) {
  159. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  160. if (!pr_buf)
  161. return;
  162. /*
  163. * With a space each printed char takes 3 bytes, subtract
  164. * the length of the data_name prefix as well as 11 bytes
  165. * for the " [0..xxx]: " printed before the data.
  166. */
  167. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  168. /* Ensure pr_buf_index stays within the 1018 size */
  169. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  170. data_name);
  171. for (i = 0; i < buf_len && i < max_size; i++)
  172. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  173. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  174. pt_debug(dev, debug_level, "%s\n", pr_buf);
  175. kfree(pr_buf);
  176. }
  177. }
  178. EXPORT_SYMBOL_GPL(pt_pr_buf);
  179. #ifdef TTHE_TUNER_SUPPORT
  180. /*******************************************************************************
  181. * FUNCTION: tthe_print
  182. *
  183. * SUMMARY: Format data name and time stamp as the header and format the
  184. * content of input buffer with hex base to "tthe_buf". And then wake up event
  185. * semaphore for tthe debugfs node.
  186. *
  187. * RETURN:
  188. * 0 = success
  189. * !0 = failure
  190. *
  191. * PARAMETERS:
  192. * *cd - pointer to core data
  193. * *buf - pointer to input buffer
  194. * buf_len - size of input buffer
  195. * *data_name - pointer to data name
  196. ******************************************************************************/
  197. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  198. const u8 *data_name)
  199. {
  200. int name_len = strlen(data_name);
  201. int i, n;
  202. u8 *p;
  203. int remain;
  204. u8 data_name_with_time_stamp[100];
  205. /* Prepend timestamp, if requested, to data_name */
  206. if (cd->show_timestamp) {
  207. scnprintf(data_name_with_time_stamp,
  208. sizeof(data_name_with_time_stamp),
  209. "[%u] %s", pt_get_time_stamp(), data_name);
  210. data_name = data_name_with_time_stamp;
  211. name_len = strlen(data_name);
  212. }
  213. mutex_lock(&cd->tthe_lock);
  214. if (!cd->tthe_buf)
  215. goto exit;
  216. /* Add 1 due to the '\n' that is appended at the end */
  217. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  218. goto exit;
  219. if (name_len + buf_len == 0)
  220. goto exit;
  221. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  222. if (remain < name_len)
  223. name_len = remain;
  224. p = cd->tthe_buf + cd->tthe_buf_len;
  225. memcpy(p, data_name, name_len);
  226. cd->tthe_buf_len += name_len;
  227. p += name_len;
  228. remain -= name_len;
  229. *p = 0;
  230. for (i = 0; i < buf_len; i++) {
  231. n = scnprintf(p, remain, "%02X ", buf[i]);
  232. if (n <= 0)
  233. break;
  234. p += n;
  235. remain -= n;
  236. cd->tthe_buf_len += n;
  237. }
  238. n = scnprintf(p, remain, "\n");
  239. cd->tthe_buf_len += n;
  240. exit:
  241. wake_up(&cd->wait_q);
  242. mutex_unlock(&cd->tthe_lock);
  243. return 0;
  244. }
  245. /*******************************************************************************
  246. * FUNCTION: _pt_request_tthe_print
  247. *
  248. * SUMMARY: Function pointer included in core_cmds to allow other modules
  249. * to request to print data to the "tthe_buffer".
  250. *
  251. * RETURN:
  252. * 0 = success
  253. * !0 = failure
  254. *
  255. * PARAMETERS:
  256. * *dev - pointer to device structure
  257. ******************************************************************************/
  258. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  259. int buf_len, const u8 *data_name)
  260. {
  261. struct pt_core_data *cd = dev_get_drvdata(dev);
  262. return tthe_print(cd, buf, buf_len, data_name);
  263. }
  264. #endif
  265. /*******************************************************************************
  266. * FUNCTION: pt_platform_detect_read
  267. *
  268. * SUMMARY: To be passed to platform dectect function to perform a read
  269. * operation.
  270. *
  271. * RETURN:
  272. * 0 = success
  273. * !0 = failure
  274. *
  275. * PARAMETERS:
  276. * *dev - pointer to Device structure
  277. * *buf - pointer to buffer where the data read will be stored
  278. * size - size to be read
  279. ******************************************************************************/
  280. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  281. {
  282. struct pt_core_data *cd = dev_get_drvdata(dev);
  283. return pt_adap_read_default(cd, buf, size);
  284. }
  285. /*******************************************************************************
  286. * FUNCTION: pt_add_parameter
  287. *
  288. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  289. * On every reset of the DUT this linked list is traversed and all
  290. * parameters in it are restored to the DUT.
  291. *
  292. * RETURN:
  293. * 0 = success
  294. * !0 = failure
  295. *
  296. * PARAMETERS:
  297. * *cd - pointer to core data
  298. * param_id - parameter ID to add
  299. * param_value - Value corresponding to the ID
  300. * param_size - Size of param_value
  301. ******************************************************************************/
  302. static int pt_add_parameter(struct pt_core_data *cd,
  303. u8 param_id, u32 param_value, u8 param_size)
  304. {
  305. struct param_node *param, *param_new;
  306. /* Check if parameter already exists in the list */
  307. spin_lock(&cd->spinlock);
  308. list_for_each_entry(param, &cd->param_list, node) {
  309. if (param->id == param_id) {
  310. /* Update parameter */
  311. param->value = param_value;
  312. pt_debug(cd->dev, DL_INFO,
  313. "%s: Update parameter id:%d value:%d size:%d\n",
  314. __func__, param_id, param_value, param_size);
  315. goto exit_unlock;
  316. }
  317. }
  318. spin_unlock(&cd->spinlock);
  319. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  320. if (!param_new)
  321. return -ENOMEM;
  322. param_new->id = param_id;
  323. param_new->value = param_value;
  324. param_new->size = param_size;
  325. pt_debug(cd->dev, DL_INFO,
  326. "%s: Add parameter id:%d value:%d size:%d\n",
  327. __func__, param_id, param_value, param_size);
  328. spin_lock(&cd->spinlock);
  329. list_add(&param_new->node, &cd->param_list);
  330. exit_unlock:
  331. spin_unlock(&cd->spinlock);
  332. return 0;
  333. }
  334. #ifdef TTDL_DIAGNOSTICS
  335. /*******************************************************************************
  336. * FUNCTION: pt_erase_parameter_list
  337. *
  338. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  339. * pairs. In some test cases this functionality is needed to ensure DUT
  340. * returns to a virgin state after a reset and no parameters are restored.
  341. *
  342. * RETURN:
  343. * 0 = success
  344. * !0 = failure
  345. *
  346. * PARAMETERS:
  347. * *cd - pointer to core data
  348. ******************************************************************************/
  349. static int pt_erase_parameter_list(struct pt_core_data *cd)
  350. {
  351. struct param_node *pos, *temp;
  352. spin_lock(&cd->spinlock);
  353. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  354. pt_debug(cd->dev, DL_INFO,
  355. "%s: Parameter Restore List - remove 0x%02x\n",
  356. __func__, pos->id);
  357. list_del(&pos->node);
  358. kfree(pos);
  359. }
  360. spin_unlock(&cd->spinlock);
  361. return 0;
  362. }
  363. /*******************************************************************************
  364. * FUNCTION: pt_count_parameter_list
  365. *
  366. * SUMMARY: Count the items in the RAM parameter restor list
  367. *
  368. * RETURN:
  369. * 0 = success
  370. * !0 = failure
  371. *
  372. * PARAMETERS:
  373. * *cd - pointer to core data
  374. ******************************************************************************/
  375. static int pt_count_parameter_list(struct pt_core_data *cd)
  376. {
  377. struct param_node *pos, *temp;
  378. int entries = 0;
  379. spin_lock(&cd->spinlock);
  380. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  381. entries++;
  382. spin_unlock(&cd->spinlock);
  383. return entries;
  384. }
  385. #endif /* TTDL_DIAGNOSTICS */
  386. /*******************************************************************************
  387. * FUNCTION: request_exclusive
  388. *
  389. * SUMMARY: Request exclusive access to the DUT
  390. *
  391. * RETURN:
  392. * 0 = success
  393. * !0 = failure
  394. *
  395. * PARAMETERS:
  396. * *cd - pointer to core data
  397. * *ownptr - pointer to device
  398. * timeout_ms - Timeout value
  399. ******************************************************************************/
  400. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  401. int timeout_ms)
  402. {
  403. int t = msecs_to_jiffies(timeout_ms);
  404. bool with_timeout = (timeout_ms != 0);
  405. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  406. __func__, timeout_ms);
  407. mutex_lock(&cd->system_lock);
  408. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  409. cd->exclusive_dev = ownptr;
  410. goto exit;
  411. }
  412. cd->exclusive_waits++;
  413. wait:
  414. mutex_unlock(&cd->system_lock);
  415. if (with_timeout) {
  416. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  417. if (IS_TMO(t)) {
  418. pt_debug(cd->dev, DL_ERROR,
  419. "%s: tmo waiting exclusive access\n", __func__);
  420. return -ETIME;
  421. }
  422. } else {
  423. wait_event(cd->wait_q, !cd->exclusive_dev);
  424. }
  425. mutex_lock(&cd->system_lock);
  426. if (cd->exclusive_dev)
  427. goto wait;
  428. cd->exclusive_dev = ownptr;
  429. cd->exclusive_waits--;
  430. exit:
  431. mutex_unlock(&cd->system_lock);
  432. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  433. __func__, ownptr);
  434. return 0;
  435. }
  436. /*******************************************************************************
  437. * FUNCTION: release_exclusive_
  438. *
  439. * SUMMARY: Release exclusive access to the DUT
  440. *
  441. * RETURN:
  442. * 0 = success
  443. *
  444. * PARAMETERS:
  445. * *cd - pointer to core data
  446. * *ownptr - pointer to device
  447. ******************************************************************************/
  448. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  449. {
  450. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  451. __func__);
  452. if (cd->exclusive_dev != ownptr)
  453. return -EINVAL;
  454. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  455. __func__, cd->exclusive_dev);
  456. cd->exclusive_dev = NULL;
  457. wake_up(&cd->wait_q);
  458. return 0;
  459. }
  460. /*******************************************************************************
  461. * FUNCTION: release_exclusive
  462. *
  463. * SUMMARY: Protected wrapper to release_exclusive_()
  464. *
  465. * RETURN:
  466. * 0 = success
  467. * !0 = failure
  468. *
  469. * PARAMETERS:
  470. * *cd - pointer to core data
  471. * *ownptr - pointer to device
  472. ******************************************************************************/
  473. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  474. {
  475. int rc;
  476. mutex_lock(&cd->system_lock);
  477. rc = release_exclusive_(cd, ownptr);
  478. mutex_unlock(&cd->system_lock);
  479. return rc;
  480. }
  481. /*******************************************************************************
  482. * FUNCTION: pt_hid_exec_cmd_
  483. *
  484. * SUMMARY: Send the HID command to the DUT
  485. *
  486. * RETURN:
  487. * 0 = success
  488. * !0 = failure
  489. *
  490. * PARAMETERS:
  491. * *cd - pointer to core data
  492. * *hid_cmd - pointer to the HID command to send
  493. ******************************************************************************/
  494. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  495. struct pt_hid_cmd *hid_cmd)
  496. {
  497. int rc = 0;
  498. u8 *cmd;
  499. u16 cmd_length;
  500. u8 cmd_offset = 0;
  501. cmd_length = 2 /* command register */
  502. + 2 /* command */
  503. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  504. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  505. + hid_cmd->write_length; /* Data length */
  506. cmd = kzalloc(cmd_length, GFP_KERNEL);
  507. if (!cmd)
  508. return -ENOMEM;
  509. /* Set Command register */
  510. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  511. sizeof(cd->hid_desc.command_register));
  512. cmd_offset += sizeof(cd->hid_desc.command_register);
  513. /* Set Command */
  514. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  515. if (hid_cmd->report_id >= 0XF)
  516. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  517. else
  518. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  519. cmd_offset++;
  520. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  521. cmd_offset++;
  522. if (hid_cmd->report_id >= 0XF) {
  523. cmd[cmd_offset] = hid_cmd->report_id;
  524. cmd_offset++;
  525. }
  526. /* Set Data register */
  527. if (hid_cmd->has_data_register) {
  528. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  529. sizeof(cd->hid_desc.data_register));
  530. cmd_offset += sizeof(cd->hid_desc.data_register);
  531. }
  532. /* Set Data */
  533. if (hid_cmd->write_length && hid_cmd->write_buf) {
  534. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  535. hid_cmd->write_length);
  536. cmd_offset += hid_cmd->write_length;
  537. }
  538. pt_debug(cd->dev, DL_INFO,
  539. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  540. __func__, cmd_length, hid_cmd->report_id);
  541. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  542. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  543. hid_cmd->read_buf);
  544. if (rc)
  545. pt_debug(cd->dev, DL_ERROR,
  546. "%s: Fail pt_adap_transfer\n", __func__);
  547. kfree(cmd);
  548. return rc;
  549. }
  550. #ifdef TTDL_DIAGNOSTICS
  551. /*******************************************************************************
  552. * FUNCTION: pt_toggle_err_gpio
  553. *
  554. * SUMMARY: Toggles the pre-defined error GPIO
  555. *
  556. * RETURN: n/a
  557. *
  558. * PARAMETERS:
  559. * *cd - pointer to core data
  560. * type - type of err that occurred
  561. ******************************************************************************/
  562. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  563. {
  564. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  565. __func__, type);
  566. if (cd->err_gpio && type == cd->err_gpio_type) {
  567. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  568. gpio_direction_output(cd->err_gpio,
  569. !gpio_get_value(cd->err_gpio));
  570. }
  571. }
  572. /*******************************************************************************
  573. * FUNCTION: _pt_request_toggle_err_gpio
  574. *
  575. * SUMMARY: Function pointer included in core_cmds to allow other modules
  576. * to request to toggle the err_gpio
  577. *
  578. * RETURN: n/a
  579. *
  580. * PARAMETERS:
  581. * *cd - pointer to core data
  582. * type - type of err that occurred
  583. ******************************************************************************/
  584. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  585. {
  586. struct pt_core_data *cd = dev_get_drvdata(dev);
  587. pt_toggle_err_gpio(cd, type);
  588. }
  589. #endif /* TTDL_DIAGNOSTICS */
  590. /*******************************************************************************
  591. * FUNCTION: pt_hid_exec_cmd_and_wait_
  592. *
  593. * SUMMARY: Send the HID command to the DUT and wait for the response
  594. *
  595. * RETURN:
  596. * 0 = success
  597. * !0 = failure
  598. *
  599. * PARAMETERS:
  600. * *cd - pointer to core data
  601. * *hid_cmd - pointer to the HID command to send
  602. ******************************************************************************/
  603. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  604. struct pt_hid_cmd *hid_cmd)
  605. {
  606. int rc = 0;
  607. int t;
  608. u16 timeout_ms;
  609. int *cmd_state;
  610. if (hid_cmd->reset_cmd)
  611. cmd_state = &cd->hid_reset_cmd_state;
  612. else
  613. cmd_state = &cd->hid_cmd_state;
  614. if (hid_cmd->wait_interrupt) {
  615. mutex_lock(&cd->system_lock);
  616. *cmd_state = 1;
  617. mutex_unlock(&cd->system_lock);
  618. }
  619. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  620. if (rc) {
  621. if (hid_cmd->wait_interrupt)
  622. goto error;
  623. goto exit;
  624. }
  625. if (!hid_cmd->wait_interrupt)
  626. goto exit;
  627. if (hid_cmd->timeout_ms)
  628. timeout_ms = hid_cmd->timeout_ms;
  629. else
  630. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  631. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  632. msecs_to_jiffies(timeout_ms));
  633. if (IS_TMO(t)) {
  634. #ifdef TTDL_DIAGNOSTICS
  635. cd->bus_transmit_error_count++;
  636. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  637. #endif /* TTDL_DIAGNOSTICS */
  638. pt_debug(cd->dev, DL_ERROR,
  639. "%s: HID output cmd execution timed out\n",
  640. __func__);
  641. rc = -ETIME;
  642. goto error;
  643. }
  644. goto exit;
  645. error:
  646. mutex_lock(&cd->system_lock);
  647. *cmd_state = 0;
  648. mutex_unlock(&cd->system_lock);
  649. exit:
  650. return rc;
  651. }
  652. /*******************************************************************************
  653. * FUNCTION: pt_hid_cmd_reset_
  654. *
  655. * SUMMARY: Send the HID RESET command to the DUT
  656. *
  657. * RETURN:
  658. * 0 = success
  659. * !0 = failure
  660. *
  661. * PARAMETERS:
  662. * *cd - pointer to core data
  663. ******************************************************************************/
  664. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  665. {
  666. struct pt_hid_cmd hid_cmd = {
  667. .opcode = HID_CMD_RESET,
  668. .wait_interrupt = 1,
  669. .reset_cmd = 1,
  670. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  671. };
  672. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  673. }
  674. /*******************************************************************************
  675. * FUNCTION: pt_hid_cmd_reset
  676. *
  677. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  678. * access.
  679. *
  680. * RETURN:
  681. * 0 = success
  682. * !0 = failure
  683. *
  684. * PARAMETERS:
  685. * *cd - pointer to core data
  686. ******************************************************************************/
  687. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  688. {
  689. int rc = 0;
  690. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  691. if (rc < 0) {
  692. pt_debug(cd->dev, DL_ERROR,
  693. "%s: fail get exclusive ex=%p own=%p\n",
  694. __func__, cd->exclusive_dev, cd->dev);
  695. return rc;
  696. }
  697. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  698. rc = pt_hid_cmd_reset_(cd);
  699. if (release_exclusive(cd, cd->dev) < 0)
  700. pt_debug(cd->dev, DL_ERROR,
  701. "%s: fail to release exclusive\n", __func__);
  702. return rc;
  703. }
  704. /*******************************************************************************
  705. * FUNCTION: pt_hid_cmd_set_power_
  706. *
  707. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  708. *
  709. * RETURN:
  710. * 0 = success
  711. * !0 = failure
  712. *
  713. * PARAMETERS:
  714. * *cd - pointer to core data
  715. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  716. ******************************************************************************/
  717. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  718. u8 power_state)
  719. {
  720. int rc = 0;
  721. struct pt_hid_cmd hid_cmd = {
  722. .opcode = HID_CMD_SET_POWER,
  723. .wait_interrupt = 1,
  724. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  725. };
  726. hid_cmd.power_state = power_state;
  727. /* The chip won't give response if goes to Deep Standby */
  728. if (power_state == HID_POWER_STANDBY) {
  729. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  730. if (rc)
  731. pt_debug(cd->dev, DL_ERROR,
  732. "%s: Failed to set power to state:%d\n",
  733. __func__, power_state);
  734. else
  735. cd->fw_sys_mode_in_standby_state = true;
  736. return rc;
  737. }
  738. cd->fw_sys_mode_in_standby_state = false;
  739. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  740. if (rc) {
  741. pt_debug(cd->dev, DL_ERROR,
  742. "%s: Failed to set power to state:%d\n",
  743. __func__, power_state);
  744. return rc;
  745. }
  746. /* validate */
  747. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  748. || ((cd->response_buf[3] & 0x3) != power_state)
  749. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  750. rc = -EINVAL;
  751. return rc;
  752. }
  753. /*******************************************************************************
  754. * FUNCTION: pt_hid_cmd_set_power
  755. *
  756. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  757. * exclusive access.
  758. *
  759. * RETURN:
  760. * 0 = success
  761. * !0 = failure
  762. *
  763. * PARAMETERS:
  764. * *cd - pointer to core data
  765. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  766. ******************************************************************************/
  767. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  768. u8 power_state)
  769. {
  770. int rc;
  771. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  772. if (rc < 0) {
  773. pt_debug(cd->dev, DL_ERROR,
  774. "%s: fail get exclusive ex=%p own=%p\n",
  775. __func__, cd->exclusive_dev, cd->dev);
  776. return rc;
  777. }
  778. rc = pt_hid_cmd_set_power_(cd, power_state);
  779. if (release_exclusive(cd, cd->dev) < 0)
  780. pt_debug(cd->dev, DL_ERROR,
  781. "%s: fail to release exclusive\n", __func__);
  782. return rc;
  783. }
  784. static const u16 crc_table[16] = {
  785. 0x0000, 0x1021, 0x2042, 0x3063,
  786. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  787. 0x8108, 0x9129, 0xa14a, 0xb16b,
  788. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  789. };
  790. /*******************************************************************************
  791. * FUNCTION: _pt_compute_crc
  792. *
  793. * SUMMARY: Calculate CRC by CRC table.
  794. *
  795. * RETURN:
  796. * CRC calculation result
  797. *
  798. * PARAMETERS:
  799. * *buf - pointer to the data array to be calculated
  800. * size - size of data array
  801. ******************************************************************************/
  802. static u16 _pt_compute_crc(u8 *buf, u32 size)
  803. {
  804. u16 remainder = 0xFFFF;
  805. u16 xor_mask = 0x0000;
  806. u32 index;
  807. u32 byte_value;
  808. u32 table_index;
  809. u32 crc_bit_width = sizeof(u16) * 8;
  810. /* Divide the message by polynomial, via the table. */
  811. for (index = 0; index < size; index++) {
  812. byte_value = buf[index];
  813. table_index = ((byte_value >> 4) & 0x0F)
  814. ^ (remainder >> (crc_bit_width - 4));
  815. remainder = crc_table[table_index] ^ (remainder << 4);
  816. table_index = (byte_value & 0x0F)
  817. ^ (remainder >> (crc_bit_width - 4));
  818. remainder = crc_table[table_index] ^ (remainder << 4);
  819. }
  820. /* Perform the final remainder CRC. */
  821. return remainder ^ xor_mask;
  822. }
  823. u16 ccitt_Table[] = {
  824. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  825. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  826. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  827. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  828. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  829. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  830. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  831. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  832. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  833. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  834. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  835. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  836. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  837. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  838. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  839. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  840. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  841. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  842. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  843. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  844. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  845. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  846. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  847. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  848. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  849. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  850. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  851. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  852. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  853. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  854. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  855. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  856. };
  857. /*******************************************************************************
  858. * FUNCTION: crc_ccitt_calculate
  859. *
  860. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  861. *
  862. * RETURN:
  863. * CRC calculation result
  864. *
  865. * PARAMETERS:
  866. * *q - pointer to the data array to be calculated
  867. * len - size of data array
  868. ******************************************************************************/
  869. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  870. {
  871. unsigned short crc = 0xffff;
  872. while (len-- > 0)
  873. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  874. return crc;
  875. }
  876. /*******************************************************************************
  877. * FUNCTION: pt_pip2_cmd_calculate_crc
  878. *
  879. * SUMMARY: Calculate the CRC of a command packet
  880. *
  881. * RETURN: void
  882. *
  883. * PARAMETERS:
  884. * *cmd - pointer to command data
  885. * extra_bytes - Extra bytes included in command length
  886. ******************************************************************************/
  887. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  888. u8 extra_bytes)
  889. {
  890. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  891. unsigned short crc;
  892. buf[0] = cmd->len & 0xff;
  893. buf[1] = (cmd->len & 0xff00) >> 8;
  894. buf[2] = cmd->seq;
  895. buf[3] = cmd->id;
  896. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  897. /* Calculate the CRC for the first 4 bytes above and the data payload */
  898. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  899. cmd->crc[0] = (crc & 0xff00) >> 8;
  900. cmd->crc[1] = (crc & 0xff);
  901. }
  902. /*******************************************************************************
  903. * FUNCTION: pt_pip2_get_next_cmd_seq
  904. *
  905. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  906. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  907. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  908. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  909. * increment, just use the forced value.
  910. *
  911. * RETURN: Next command sequence number [0x08-0x0F]
  912. *
  913. * PARAMETERS:
  914. * *cd - pointer to core data
  915. ******************************************************************************/
  916. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  917. {
  918. #ifdef TTDL_DIAGNOSTICS
  919. if (cd->force_pip2_seq <= 0x07) {
  920. cd->pip2_cmd_tag_seq++;
  921. if (cd->pip2_cmd_tag_seq > 0x0F)
  922. cd->pip2_cmd_tag_seq = 0x08;
  923. } else {
  924. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  925. }
  926. #else
  927. cd->pip2_cmd_tag_seq++;
  928. if (cd->pip2_cmd_tag_seq > 0x0F)
  929. cd->pip2_cmd_tag_seq = 0x08;
  930. #endif
  931. return cd->pip2_cmd_tag_seq;
  932. }
  933. /*
  934. * Following macros are to define the response time (the interval between PIP2
  935. * command finishes sending and INT pin falls). The unit is in microsecond.
  936. * It has different time settings between the solution GPIO polling and Bus
  937. * polling due to the considration for system load.
  938. */
  939. #ifdef PT_POLL_RESP_BY_BUS
  940. #define POLL_RETRY_DEFAULT_INTERVAL 50
  941. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  942. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  943. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  944. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  945. #else
  946. #define POLL_RETRY_DEFAULT_INTERVAL 20
  947. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  948. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  949. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  950. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  951. #endif
  952. /*
  953. * id: the command id defined in PIP2
  954. * response_len: the (maximum) length of response.
  955. * response_time_min: minimum response time in microsecond
  956. * response_time_max: maximum response time in microsecond
  957. */
  958. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  959. {.id = PIP2_CMD_ID_PING,
  960. .response_len = 255,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_STATUS,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  967. {.id = PIP2_CMD_ID_CTRL,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  971. {.id = PIP2_CMD_ID_CONFIG,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_CLEAR,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_RESET,
  980. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  981. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  982. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  983. {.id = PIP2_CMD_ID_VERSION,
  984. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_OPEN,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  989. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_CLOSE,
  992. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FILE_READ,
  996. .response_len = 255,
  997. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_FILE_WRITE,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1001. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_FILE_IOCTL,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_FLASH_INFO,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_EXECUTE,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1020. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1021. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1022. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1023. {.id = PIP2_CMD_ID_READ_GPIO,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_EXECUTE_SCAN,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_SET_PARAMETER,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_GET_PARAMETER,
  1036. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1039. {.id = PIP2_CMD_SET_DDI_REG,
  1040. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1041. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1042. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1043. {.id = PIP2_CMD_GET_DDI_REG,
  1044. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1045. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1046. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1047. {.id = PIP2_CMD_ID_END,
  1048. .response_len = 255,
  1049. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1050. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1051. };
  1052. /*******************************************************************************
  1053. * FUNCTION: pt_pip2_get_cmd_response_len
  1054. *
  1055. * SUMMARY: Gets the expected response length based on the command ID
  1056. *
  1057. * RETURN: Expected response length
  1058. *
  1059. * PARAMETERS:
  1060. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1061. ******************************************************************************/
  1062. static int pt_pip2_get_cmd_response_len(u8 id)
  1063. {
  1064. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1065. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1066. p++;
  1067. if (p->id != PIP2_CMD_ID_END)
  1068. return p->response_len;
  1069. else
  1070. return -EPERM;
  1071. }
  1072. /*******************************************************************************
  1073. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1074. *
  1075. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1076. * finishes sending and INT pin falls) based on the command ID
  1077. *
  1078. * RETURN: Estimated minimum response time in microsecond
  1079. *
  1080. * PARAMETERS:
  1081. * id - Command ID
  1082. ******************************************************************************/
  1083. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1084. {
  1085. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1086. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1087. p++;
  1088. if (p->id != PIP2_CMD_ID_END)
  1089. return p->response_time_min;
  1090. else
  1091. return PIP2_RESP_DEFAULT_TIME_MIN;
  1092. }
  1093. /*******************************************************************************
  1094. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1095. *
  1096. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1097. * finishes sending and INT pin falls) based on the command ID
  1098. *
  1099. * RETURN: Estimated maximum response time in microsecond
  1100. *
  1101. * PARAMETERS:
  1102. * id - Command ID
  1103. ******************************************************************************/
  1104. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1105. {
  1106. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1107. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1108. p++;
  1109. if (p->id != PIP2_CMD_ID_END)
  1110. return p->response_time_max;
  1111. else
  1112. return PIP2_RESP_DEFAULT_TIME_MAX;
  1113. }
  1114. /*******************************************************************************
  1115. * FUNCTION: pt_pip2_validate_response
  1116. *
  1117. * SUMMARY: Validate the response of PIP2 command.
  1118. *
  1119. * RETURN:
  1120. * 0 = success
  1121. * !0 = failure
  1122. *
  1123. * PARAMETERS:
  1124. * *cd - pointer to core data
  1125. * *pip2_cmd - pointer to PIP2 command to send
  1126. * *read_buf - pointer to response buffer
  1127. * actual_read_len - actual read length of the response
  1128. ******************************************************************************/
  1129. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1130. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1131. u16 actual_read_len)
  1132. {
  1133. int rc = 0;
  1134. u8 response_seq = 0;
  1135. u8 reserved_bits = 0;
  1136. u8 cmd_id = 0;
  1137. u8 response_bit = 0;
  1138. unsigned short calc_crc = 0;
  1139. unsigned short resp_crc = 0;
  1140. /* Verify the length of response buffer */
  1141. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1142. pt_debug(cd->dev, DL_ERROR,
  1143. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1144. __func__, pip2_cmd->id, actual_read_len);
  1145. rc = -EINVAL;
  1146. goto exit;
  1147. }
  1148. /* Verify the CRC */
  1149. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1150. resp_crc = read_buf[actual_read_len - 2] << 8;
  1151. resp_crc |= read_buf[actual_read_len - 1];
  1152. if (resp_crc != calc_crc) {
  1153. pt_debug(cd->dev, DL_ERROR,
  1154. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1155. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1156. #ifdef TTDL_DIAGNOSTICS
  1157. cd->pip2_crc_error_count++;
  1158. #endif /* TTDL_DIAGNOSTICS */
  1159. rc = -EINVAL;
  1160. goto exit;
  1161. }
  1162. /* Verify the response bit is set */
  1163. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1164. if (!response_bit) {
  1165. pt_debug(cd->dev, DL_ERROR,
  1166. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1167. __func__, pip2_cmd->id, response_bit);
  1168. rc = -EINVAL;
  1169. goto exit;
  1170. }
  1171. /* Verify the command ID matches from command to response */
  1172. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1173. if (cmd_id != pip2_cmd->id) {
  1174. pt_debug(cd->dev, DL_ERROR,
  1175. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1176. __func__, pip2_cmd->id, cmd_id);
  1177. rc = -EINVAL;
  1178. goto exit;
  1179. }
  1180. /* Verify the SEQ number matches from command to response */
  1181. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1182. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1183. pt_debug(cd->dev, DL_ERROR,
  1184. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1185. __func__, pip2_cmd->id,
  1186. pip2_cmd->seq, response_seq);
  1187. rc = -EINVAL;
  1188. goto exit;
  1189. }
  1190. /* Verify the reserved bits are 0 */
  1191. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1192. if (reserved_bits)
  1193. pt_debug(cd->dev, DL_WARN,
  1194. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1195. __func__, pip2_cmd->id, reserved_bits);
  1196. exit:
  1197. if (rc)
  1198. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1199. "PIP RSP:");
  1200. return rc;
  1201. }
  1202. /*******************************************************************************
  1203. * FUNCTION: pt_hid_output_validate_bl_response
  1204. *
  1205. * SUMMARY: Validate the response of bootloader command.
  1206. *
  1207. * RETURN:
  1208. * 0 = success
  1209. * !0 = failure
  1210. *
  1211. * PARAMETERS:
  1212. * *cd - pointer to core data
  1213. * *hid_output - pointer to hid output data structure
  1214. ******************************************************************************/
  1215. static int pt_hid_output_validate_bl_response(
  1216. struct pt_core_data *cd,
  1217. struct pt_hid_output *hid_output)
  1218. {
  1219. u16 size;
  1220. u16 crc;
  1221. u8 status;
  1222. size = get_unaligned_le16(&cd->response_buf[0]);
  1223. if (hid_output->reset_expected && !size)
  1224. return 0;
  1225. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1226. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1227. pt_debug(cd->dev, DL_ERROR,
  1228. "%s: BL output response, wrong report_id\n", __func__);
  1229. return -EPROTO;
  1230. }
  1231. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong SOP\n", __func__);
  1234. return -EPROTO;
  1235. }
  1236. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1237. pt_debug(cd->dev, DL_ERROR,
  1238. "%s: BL output response, wrong EOP\n", __func__);
  1239. return -EPROTO;
  1240. }
  1241. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1242. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1243. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1244. pt_debug(cd->dev, DL_ERROR,
  1245. "%s: BL output response, wrong CRC 0x%X\n",
  1246. __func__, crc);
  1247. return -EPROTO;
  1248. }
  1249. status = cd->response_buf[5];
  1250. if (status) {
  1251. pt_debug(cd->dev, DL_ERROR,
  1252. "%s: BL output response, ERROR:%d\n",
  1253. __func__, status);
  1254. return -EPROTO;
  1255. }
  1256. return 0;
  1257. }
  1258. /*******************************************************************************
  1259. * FUNCTION: pt_hid_output_validate_app_response
  1260. *
  1261. * SUMMARY: Validate the response of application command.
  1262. *
  1263. * RETURN:
  1264. * 0 = success
  1265. * !0 = failure
  1266. *
  1267. * PARAMETERS:
  1268. * *cd - pointer to core data
  1269. * *hid_output - pointer to hid output data structure
  1270. ******************************************************************************/
  1271. static int pt_hid_output_validate_app_response(
  1272. struct pt_core_data *cd,
  1273. struct pt_hid_output *hid_output)
  1274. {
  1275. int command_code;
  1276. u16 size;
  1277. size = get_unaligned_le16(&cd->response_buf[0]);
  1278. if (hid_output->reset_expected && !size)
  1279. return 0;
  1280. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1281. != PT_PIP_NON_HID_RESPONSE_ID) {
  1282. pt_debug(cd->dev, DL_ERROR,
  1283. "%s: APP output response, wrong report_id\n", __func__);
  1284. return -EPROTO;
  1285. }
  1286. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1287. & PIP1_RESP_COMMAND_ID_MASK;
  1288. if (command_code != hid_output->command_code) {
  1289. pt_debug(cd->dev, DL_ERROR,
  1290. "%s: APP output response, wrong command_code:%X\n",
  1291. __func__, command_code);
  1292. return -EPROTO;
  1293. }
  1294. return 0;
  1295. }
  1296. /*******************************************************************************
  1297. * FUNCTION: pt_check_set_parameter
  1298. *
  1299. * SUMMARY: Check command input and response for Set Parameter command.And
  1300. * store the parameter to the list for resume work if pass the check.
  1301. *
  1302. * PARAMETERS:
  1303. * *cd - pointer to core data
  1304. * *hid_output - pointer to hid output data structure
  1305. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1306. ******************************************************************************/
  1307. static void pt_check_set_parameter(struct pt_core_data *cd,
  1308. struct pt_hid_output *hid_output, bool raw)
  1309. {
  1310. u8 *param_buf;
  1311. u32 param_value = 0;
  1312. u8 param_size;
  1313. u8 param_id;
  1314. int i = 0;
  1315. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1316. return;
  1317. /* Check command input for Set Parameter command */
  1318. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1319. && !memcmp(&hid_output->write_buf[0],
  1320. &cd->hid_desc.output_register,
  1321. sizeof(cd->hid_desc.output_register))
  1322. && hid_output->write_buf[4] ==
  1323. PT_PIP_NON_HID_COMMAND_ID
  1324. && hid_output->write_buf[6] ==
  1325. PIP1_CMD_ID_SET_PARAM)
  1326. param_buf = &hid_output->write_buf[7];
  1327. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1328. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1329. && hid_output->write_length >= 3
  1330. && hid_output->write_length <= 6)
  1331. param_buf = &hid_output->write_buf[0];
  1332. else
  1333. return;
  1334. /* Get parameter ID, size and value */
  1335. param_id = param_buf[0];
  1336. param_size = param_buf[1];
  1337. if (param_size > 4) {
  1338. pt_debug(cd->dev, DL_ERROR,
  1339. "%s: Invalid parameter size\n", __func__);
  1340. return;
  1341. }
  1342. param_buf = &param_buf[2];
  1343. while (i < param_size)
  1344. param_value += *(param_buf++) << (8 * i++);
  1345. /* Check command response for Set Parameter command */
  1346. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1347. || (cd->response_buf[4] &
  1348. PIP1_RESP_COMMAND_ID_MASK) !=
  1349. PIP1_CMD_ID_SET_PARAM
  1350. || cd->response_buf[5] != param_id
  1351. || cd->response_buf[6] != param_size) {
  1352. pt_debug(cd->dev, DL_ERROR,
  1353. "%s: Set Parameter command not successful\n",
  1354. __func__);
  1355. return;
  1356. }
  1357. pt_add_parameter(cd, param_id, param_value, param_size);
  1358. }
  1359. /*******************************************************************************
  1360. * FUNCTION: pt_check_command
  1361. *
  1362. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1363. * called here to check output command and store parameter to the list.
  1364. *
  1365. * PARAMETERS:
  1366. * *cd - pointer to core data
  1367. * *hid_output - pointer to hid output data structure
  1368. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1369. ******************************************************************************/
  1370. static void pt_check_command(struct pt_core_data *cd,
  1371. struct pt_hid_output *hid_output, bool raw)
  1372. {
  1373. pt_check_set_parameter(cd, hid_output, raw);
  1374. }
  1375. /*******************************************************************************
  1376. * FUNCTION: pt_hid_output_validate_response
  1377. *
  1378. * SUMMARY: Validate the response of application or bootloader command.
  1379. *
  1380. * RETURN:
  1381. * 0 = success
  1382. * !0 = failure
  1383. *
  1384. * PARAMETERS:
  1385. * *cd - pointer to core data
  1386. * *hid_output - pointer to hid output data structure
  1387. ******************************************************************************/
  1388. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1389. struct pt_hid_output *hid_output)
  1390. {
  1391. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1392. return pt_hid_output_validate_bl_response(cd, hid_output);
  1393. return pt_hid_output_validate_app_response(cd, hid_output);
  1394. }
  1395. /*******************************************************************************
  1396. * FUNCTION: pt_hid_send_output_user_
  1397. *
  1398. * SUMMARY: Blindly send user data to the DUT.
  1399. *
  1400. * RETURN:
  1401. * 0 = success
  1402. * !0 = failure
  1403. *
  1404. * PARAMETERS:
  1405. * *cd - pointer to core data
  1406. * *hid_output - pointer to the command to send
  1407. ******************************************************************************/
  1408. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1409. struct pt_hid_output *hid_output)
  1410. {
  1411. int rc = 0;
  1412. int cmd;
  1413. if (!hid_output->length || !hid_output->write_buf)
  1414. return -EINVAL;
  1415. if (cd->pip2_prot_active) {
  1416. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1417. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1418. } else
  1419. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1420. pt_debug(cd->dev, DL_INFO,
  1421. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1422. __func__, hid_output->length, cmd);
  1423. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1424. hid_output->length, ">>> User CMD");
  1425. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1426. hid_output->write_buf, NULL);
  1427. if (rc)
  1428. pt_debug(cd->dev, DL_ERROR,
  1429. "%s: Fail pt_adap_transfer\n", __func__);
  1430. return rc;
  1431. }
  1432. /*******************************************************************************
  1433. * FUNCTION: pt_hid_send_output_user_and_wait_
  1434. *
  1435. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1436. *
  1437. * RETURN:
  1438. * 0 = success
  1439. * !0 = failure
  1440. *
  1441. * PARAMETERS:
  1442. * *cd - pointer to core data
  1443. * *hid_output - pointer to the command to send
  1444. ******************************************************************************/
  1445. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1446. struct pt_hid_output *hid_output)
  1447. {
  1448. int rc = 0;
  1449. int t;
  1450. mutex_lock(&cd->system_lock);
  1451. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1452. mutex_unlock(&cd->system_lock);
  1453. rc = pt_hid_send_output_user_(cd, hid_output);
  1454. if (rc)
  1455. goto error;
  1456. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1457. msecs_to_jiffies(cd->pip_cmd_timeout));
  1458. if (IS_TMO(t)) {
  1459. #ifdef TTDL_DIAGNOSTICS
  1460. cd->bus_transmit_error_count++;
  1461. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1462. #endif /* TTDL_DIAGNOSTICS */
  1463. pt_debug(cd->dev, DL_ERROR,
  1464. "%s: HID output cmd execution timed out\n",
  1465. __func__);
  1466. rc = -ETIME;
  1467. goto error;
  1468. }
  1469. pt_check_command(cd, hid_output, true);
  1470. goto exit;
  1471. error:
  1472. mutex_lock(&cd->system_lock);
  1473. cd->hid_cmd_state = 0;
  1474. mutex_unlock(&cd->system_lock);
  1475. exit:
  1476. return rc;
  1477. }
  1478. /*******************************************************************************
  1479. * FUNCTION: pt_check_irq_asserted
  1480. *
  1481. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1482. * the FW can hold the INT line low ~150us after the read is complete.
  1483. * NOTE: if irq_stat is not defined this function will return false
  1484. *
  1485. * RETURN:
  1486. * true = IRQ asserted
  1487. * false = IRQ not asserted
  1488. *
  1489. * PARAMETERS:
  1490. * *cd - pointer to core data
  1491. ******************************************************************************/
  1492. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1493. {
  1494. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1495. /*
  1496. * Workaround for FW defect, CDT165308
  1497. * bl_launch app creates a glitch in IRQ line
  1498. */
  1499. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1500. && cd->cpdata->irq_stat) {
  1501. /*
  1502. * in X1S panel and GC1546 panel, the width for the INT
  1503. * glitch is about 4us,the normal INT width of response
  1504. * will last more than 200us, so use 10us delay
  1505. * for distinguish the glitch the normal INT is enough.
  1506. */
  1507. udelay(10);
  1508. }
  1509. #endif
  1510. if (cd->cpdata->irq_stat) {
  1511. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1512. == PT_IRQ_ASSERTED_VALUE) {
  1513. /* Debounce to allow FW to release INT */
  1514. usleep_range(100, 200);
  1515. }
  1516. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1517. == PT_IRQ_ASSERTED_VALUE)
  1518. return true;
  1519. else
  1520. return false;
  1521. }
  1522. return true;
  1523. }
  1524. /*******************************************************************************
  1525. * FUNCTION: pt_flush_bus
  1526. *
  1527. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1528. * Used if IRQ is found to be stuck low
  1529. *
  1530. * RETURN: Length of bytes read from bus
  1531. *
  1532. * PARAMETERS:
  1533. * *cd - pointer to core data
  1534. * flush_type - type of flush
  1535. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1536. * - PT_FLUSH_BUS_FULL_256_READ
  1537. * *read_buf - pointer to store read data
  1538. ******************************************************************************/
  1539. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1540. u8 flush_type, u8 *read_buf)
  1541. {
  1542. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1543. u16 pip_len;
  1544. int bytes_read;
  1545. int rc = 0;
  1546. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1547. rc = pt_adap_read_default(cd, buf, 2);
  1548. if (rc) {
  1549. bytes_read = 0;
  1550. goto exit;
  1551. }
  1552. pip_len = get_unaligned_le16(&buf[0]);
  1553. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1554. #ifdef TTDL_DIAGNOSTICS
  1555. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1556. #endif
  1557. bytes_read = 2;
  1558. pt_debug(cd->dev, DL_INFO,
  1559. "%s: Empty buf detected - len=0x%04X\n",
  1560. __func__, pip_len);
  1561. } else if (pip_len == 0) {
  1562. bytes_read = 0;
  1563. pt_debug(cd->dev, DL_INFO,
  1564. "%s: Sentinel detected\n", __func__);
  1565. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1566. pt_debug(cd->dev, DL_ERROR,
  1567. "%s: Illegal len=0x%04x, force %d byte read\n",
  1568. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1569. rc = pt_adap_read_default(cd, buf,
  1570. PT_MAX_PIP2_MSG_SIZE);
  1571. if (!rc)
  1572. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1573. else
  1574. bytes_read = 0;
  1575. } else {
  1576. pt_debug(cd->dev, DL_INFO,
  1577. "%s: Flush read of %d bytes...\n",
  1578. __func__, pip_len);
  1579. rc = pt_adap_read_default(cd, buf, pip_len);
  1580. if (!rc)
  1581. bytes_read = pip_len;
  1582. else
  1583. bytes_read = 0;
  1584. }
  1585. } else {
  1586. pt_debug(cd->dev, DL_INFO,
  1587. "%s: Forced flush of max %d bytes...\n",
  1588. __func__, PT_MAX_PIP2_MSG_SIZE);
  1589. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1590. if (!rc)
  1591. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1592. else
  1593. bytes_read = 0;
  1594. }
  1595. if (read_buf && (bytes_read > 3))
  1596. memcpy(read_buf, buf, bytes_read);
  1597. exit:
  1598. return bytes_read;
  1599. }
  1600. /*******************************************************************************
  1601. * FUNCTION: pt_flush_bus_if_irq_asserted
  1602. *
  1603. * SUMMARY: This function will flush the active bus if the INT is found to be
  1604. * asserted.
  1605. *
  1606. * RETURN: bytes cleared from bus
  1607. *
  1608. * PARAMETERS:
  1609. * *cd - pointer the core data structure
  1610. * flush_type - type of flush
  1611. * - PT_FLUSH_BUS_BASED_ON_LEN
  1612. * - PT_FLUSH_BUS_FULL_256_READ
  1613. ******************************************************************************/
  1614. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1615. {
  1616. int count = 0;
  1617. int bytes_read = 0;
  1618. while (pt_check_irq_asserted(cd) && count < 5) {
  1619. count++;
  1620. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1621. if (bytes_read) {
  1622. pt_debug(cd->dev, DL_WARN,
  1623. "%s: Cleared %d bytes off bus\n",
  1624. __func__, bytes_read);
  1625. }
  1626. }
  1627. if (pt_check_irq_asserted(cd)) {
  1628. pt_debug(cd->dev, DL_ERROR,
  1629. "%s: IRQ still asserted, %d bytes read\n",
  1630. __func__, bytes_read);
  1631. } else {
  1632. pt_debug(cd->dev, DL_INFO,
  1633. "%s: IRQ cleared, %d bytes read\n",
  1634. __func__, bytes_read);
  1635. }
  1636. return bytes_read;
  1637. }
  1638. /*******************************************************************************
  1639. * FUNCTION: pt_hid_send_output_
  1640. *
  1641. * SUMMARY: Send a touch application command to the DUT
  1642. *
  1643. * RETURN:
  1644. * 0 = success
  1645. * !0 = failure
  1646. *
  1647. * PARAMETERS:
  1648. * *cd - pointer to core data
  1649. * *hid_output - pointer to the command to send
  1650. ******************************************************************************/
  1651. static int pt_hid_send_output_(struct pt_core_data *cd,
  1652. struct pt_hid_output *hid_output)
  1653. {
  1654. int rc = 0;
  1655. u8 *cmd;
  1656. u16 length;
  1657. u16 crc;
  1658. u8 report_id;
  1659. u8 cmd_offset = 0;
  1660. u8 cmd_allocated = 0;
  1661. switch (hid_output->cmd_type) {
  1662. case PIP1_CMD_TYPE_FW:
  1663. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1664. length = 5;
  1665. break;
  1666. case PIP1_CMD_TYPE_BL:
  1667. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1668. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1669. break;
  1670. default:
  1671. return -EINVAL;
  1672. }
  1673. length += hid_output->write_length;
  1674. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1675. cmd = kzalloc(length + 2, GFP_KERNEL);
  1676. if (!cmd)
  1677. return -ENOMEM;
  1678. cmd_allocated = 1;
  1679. } else {
  1680. cmd = cd->cmd_buf;
  1681. }
  1682. /* Set Output register */
  1683. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1684. sizeof(cd->hid_desc.output_register));
  1685. cmd_offset += sizeof(cd->hid_desc.output_register);
  1686. cmd[cmd_offset++] = LOW_BYTE(length);
  1687. cmd[cmd_offset++] = HI_BYTE(length);
  1688. cmd[cmd_offset++] = report_id;
  1689. cmd[cmd_offset++] = 0x0; /* reserved */
  1690. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1691. cmd[cmd_offset++] = PIP1_BL_SOP;
  1692. cmd[cmd_offset++] = hid_output->command_code;
  1693. /* Set Data Length for bootloader */
  1694. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1695. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1696. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1697. }
  1698. /* Set Data */
  1699. if (hid_output->write_length && hid_output->write_buf) {
  1700. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1701. hid_output->write_length);
  1702. cmd_offset += hid_output->write_length;
  1703. }
  1704. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1705. crc = _pt_compute_crc(&cmd[6],
  1706. hid_output->write_length + 4);
  1707. cmd[cmd_offset++] = LOW_BYTE(crc);
  1708. cmd[cmd_offset++] = HI_BYTE(crc);
  1709. cmd[cmd_offset++] = PIP1_BL_EOP;
  1710. }
  1711. pt_debug(cd->dev, DL_INFO,
  1712. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1713. __func__, length + 2, hid_output->command_code);
  1714. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1715. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1716. if (rc)
  1717. pt_debug(cd->dev, DL_ERROR,
  1718. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1719. if (cmd_allocated)
  1720. kfree(cmd);
  1721. return rc;
  1722. }
  1723. /*******************************************************************************
  1724. * FUNCTION: pt_pip1_send_output_and_wait_
  1725. *
  1726. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1727. *
  1728. * RETURN:
  1729. * 0 = success
  1730. * !0 = failure
  1731. *
  1732. * PARAMETERS:
  1733. * *cd - pointer to core data
  1734. * *hid_output - pointer to the command to send
  1735. ******************************************************************************/
  1736. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1737. struct pt_hid_output *hid_output)
  1738. {
  1739. int rc = 0;
  1740. int t;
  1741. u16 timeout_ms;
  1742. mutex_lock(&cd->system_lock);
  1743. cd->hid_cmd_state = hid_output->command_code + 1;
  1744. mutex_unlock(&cd->system_lock);
  1745. if (hid_output->timeout_ms)
  1746. timeout_ms = hid_output->timeout_ms;
  1747. else
  1748. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1749. rc = pt_hid_send_output_(cd, hid_output);
  1750. if (rc)
  1751. goto error;
  1752. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1753. msecs_to_jiffies(timeout_ms));
  1754. if (IS_TMO(t)) {
  1755. #ifdef TTDL_DIAGNOSTICS
  1756. cd->bus_transmit_error_count++;
  1757. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1758. #endif /* TTDL_DIAGNOSTICS */
  1759. pt_debug(cd->dev, DL_ERROR,
  1760. "%s: HID output cmd execution timed out (%dms)\n",
  1761. __func__, timeout_ms);
  1762. rc = -ETIME;
  1763. goto error;
  1764. }
  1765. if (!hid_output->novalidate)
  1766. rc = pt_hid_output_validate_response(cd, hid_output);
  1767. pt_check_command(cd, hid_output, false);
  1768. goto exit;
  1769. error:
  1770. mutex_lock(&cd->system_lock);
  1771. cd->hid_cmd_state = 0;
  1772. mutex_unlock(&cd->system_lock);
  1773. exit:
  1774. return rc;
  1775. }
  1776. /*******************************************************************************
  1777. * FUNCTION: pt_hid_output_user_cmd_
  1778. *
  1779. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1780. * to the DUT waiting for the response and loading it into the read buffer
  1781. *
  1782. * RETURN:
  1783. * 0 = success
  1784. * !0 = failure
  1785. *
  1786. * PARAMETERS:
  1787. * *cd - pointer to core data
  1788. * read_len - expected read length of the response
  1789. * *read_buf - pointer to where the response will be loaded
  1790. * write_len - length of the write buffer
  1791. * *write_buf - pointer to the write buffer
  1792. * *actual_read_len - pointer to the actual amount of data read back
  1793. ******************************************************************************/
  1794. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1795. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1796. u16 *actual_read_len)
  1797. {
  1798. int rc = 0;
  1799. u16 size;
  1800. struct pt_hid_output hid_output = {
  1801. .length = write_len,
  1802. .write_buf = write_buf,
  1803. };
  1804. #ifdef TTHE_TUNER_SUPPORT
  1805. if (!cd->pip2_send_user_cmd) {
  1806. int command_code = 0;
  1807. int len;
  1808. /* Print up to cmd ID */
  1809. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1810. if (write_len < len)
  1811. len = write_len;
  1812. else
  1813. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1814. & PIP1_CMD_COMMAND_ID_MASK;
  1815. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1816. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1817. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1818. tthe_print(cd, write_buf, len, "CMD=");
  1819. }
  1820. #endif
  1821. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1822. if (rc)
  1823. return rc;
  1824. /* Get the response size from the first 2 bytes in the response */
  1825. size = get_unaligned_le16(&cd->response_buf[0]);
  1826. /* Ensure size is not greater than max buffer size */
  1827. if (size > PT_MAX_PIP2_MSG_SIZE)
  1828. size = PT_MAX_PIP2_MSG_SIZE;
  1829. /* Minimum size to read is the 2 byte len field */
  1830. if (size == 0)
  1831. size = 2;
  1832. if (size > read_len) {
  1833. pt_debug(cd->dev, DL_ERROR,
  1834. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1835. __func__, size, read_len);
  1836. *actual_read_len = 0;
  1837. return -EIO;
  1838. }
  1839. memcpy(read_buf, cd->response_buf, size);
  1840. *actual_read_len = size;
  1841. return 0;
  1842. }
  1843. /*******************************************************************************
  1844. * FUNCTION: pt_hid_output_user_cmd
  1845. *
  1846. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1847. * the DUT.
  1848. *
  1849. * RETURN:
  1850. * 0 = success
  1851. * !0 = failure
  1852. *
  1853. * PARAMETERS:
  1854. * *cd - pointer to core data
  1855. * read_len - length of data to read
  1856. * *read_buf - pointer to store read data
  1857. * write_len - length of data to write
  1858. * *write_buf - pointer to buffer to write
  1859. * *actual_read_len - pointer to store data length actually read
  1860. ******************************************************************************/
  1861. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1862. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1863. u16 *actual_read_len)
  1864. {
  1865. int rc;
  1866. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1867. if (rc < 0) {
  1868. pt_debug(cd->dev, DL_ERROR,
  1869. "%s: fail get exclusive ex=%p own=%p\n",
  1870. __func__, cd->exclusive_dev, cd->dev);
  1871. return rc;
  1872. }
  1873. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1874. write_len, write_buf, actual_read_len);
  1875. if (release_exclusive(cd, cd->dev) < 0)
  1876. pt_debug(cd->dev, DL_ERROR,
  1877. "%s: fail to release exclusive\n", __func__);
  1878. return rc;
  1879. }
  1880. /*******************************************************************************
  1881. * FUNCTION: _pt_request_pip2_send_cmd
  1882. *
  1883. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1884. * interrupt and reads response data to read_buf
  1885. *
  1886. * RETURN:
  1887. * 0 = success
  1888. * !0 = failure
  1889. *
  1890. * PARAMETERS:
  1891. * *dev - pointer to device structure
  1892. * protect - flag to run in protected mode
  1893. * id - ID of PIP command
  1894. * *data - pointer to PIP data payload
  1895. * report_body_len - report length
  1896. * *read_buf - pointer to response buffer
  1897. * *actual_read_len - pointer to response buffer length
  1898. ******************************************************************************/
  1899. static int _pt_request_pip2_send_cmd(struct device *dev,
  1900. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1901. u16 *actual_read_len)
  1902. {
  1903. struct pt_core_data *cd = dev_get_drvdata(dev);
  1904. struct pip2_cmd_structure pip2_cmd;
  1905. int rc = 0;
  1906. int i = 0;
  1907. int j = 0;
  1908. u16 write_len;
  1909. u8 *write_buf = NULL;
  1910. u16 read_len;
  1911. u8 extra_bytes;
  1912. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1913. /* Hard coded register for PIP2.x */
  1914. pip2_cmd.reg[0] = 0x01;
  1915. pip2_cmd.reg[1] = 0x01;
  1916. /*
  1917. * For PIP2.1+ the length field value includes itself:
  1918. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1919. *
  1920. * The overall write length must include only the register:
  1921. * ADD 2: 2 (Register)
  1922. */
  1923. extra_bytes = 6;
  1924. write_len = 2;
  1925. /* PIP2 the CMD ID is a 7bit field */
  1926. if (id > PIP2_CMD_ID_END) {
  1927. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1928. __func__, id);
  1929. rc = -EINVAL;
  1930. goto exit;
  1931. }
  1932. pip2_cmd.len = report_body_len + extra_bytes;
  1933. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1934. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1935. pip2_cmd.data = data;
  1936. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1937. /* Add the command length to the extra bytes based on PIP version */
  1938. write_len += pip2_cmd.len;
  1939. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1940. __func__, pip2_cmd.len, write_len);
  1941. write_buf = kzalloc(write_len, GFP_KERNEL);
  1942. if (write_buf == NULL) {
  1943. rc = -ENOMEM;
  1944. goto exit;
  1945. }
  1946. write_buf[i++] = pip2_cmd.reg[0];
  1947. write_buf[i++] = pip2_cmd.reg[1];
  1948. write_buf[i++] = pip2_cmd.len & 0xff;
  1949. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1950. write_buf[i++] = pip2_cmd.seq;
  1951. write_buf[i++] = pip2_cmd.id;
  1952. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1953. write_buf[j] = pip2_cmd.data[j-i];
  1954. write_buf[j++] = pip2_cmd.crc[0];
  1955. write_buf[j++] = pip2_cmd.crc[1];
  1956. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1957. if (read_len < 0)
  1958. read_len = 255;
  1959. pt_debug(dev, DL_INFO,
  1960. "%s cmd_id[0x%02X] expected response length:%d ",
  1961. __func__, pip2_cmd.id, read_len);
  1962. /*
  1963. * All PIP2 commands come through this function.
  1964. * Set flag for PIP2.x interface to allow response parsing to know
  1965. * how to decode the protocol header.
  1966. */
  1967. mutex_lock(&cd->system_lock);
  1968. cd->pip2_prot_active = true;
  1969. cd->pip2_send_user_cmd = true;
  1970. mutex_unlock(&cd->system_lock);
  1971. if (protect == PT_CORE_CMD_PROTECTED)
  1972. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1973. write_len, write_buf, actual_read_len);
  1974. else {
  1975. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1976. write_len, write_buf, actual_read_len);
  1977. }
  1978. if (rc) {
  1979. pt_debug(dev, DL_ERROR,
  1980. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1981. __func__, rc);
  1982. goto exit;
  1983. }
  1984. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1985. *actual_read_len);
  1986. exit:
  1987. mutex_lock(&cd->system_lock);
  1988. cd->pip2_prot_active = false;
  1989. cd->pip2_send_user_cmd = false;
  1990. mutex_unlock(&cd->system_lock);
  1991. kfree(write_buf);
  1992. return rc;
  1993. }
  1994. /*******************************************************************************
  1995. * FUNCTION: _pt_pip2_send_cmd_no_int
  1996. *
  1997. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1998. * reads response data to read_buf if response is available.
  1999. *
  2000. * NOTE:
  2001. * Interrupt MUST be disabled before to call this function.
  2002. *
  2003. * RETURN:
  2004. * 0 = success
  2005. * !0 = failure
  2006. *
  2007. * PARAMETERS:
  2008. * *dev - pointer to device structure
  2009. * protect - flag to run in protected mode
  2010. * id - ID of PIP command
  2011. * *data - pointer to PIP data payload
  2012. * report_body_len - report length
  2013. * *read_buf - pointer to response buffer
  2014. * *actual_read_len - pointer to response buffer length
  2015. ******************************************************************************/
  2016. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2017. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2018. u16 *actual_read_len)
  2019. {
  2020. int max_retry = 0;
  2021. int retry = 0;
  2022. int rc = 0;
  2023. int i = 0;
  2024. int j = 0;
  2025. u16 write_len;
  2026. u8 *write_buf = NULL;
  2027. u16 read_len;
  2028. u16 size = 0;
  2029. u8 response_seq = 0;
  2030. u8 extra_bytes;
  2031. u32 retry_interval = 0;
  2032. u32 retry_total_time = 0;
  2033. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2034. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2035. struct pt_core_data *cd = dev_get_drvdata(dev);
  2036. struct pip2_cmd_structure pip2_cmd;
  2037. if (protect == PT_CORE_CMD_PROTECTED) {
  2038. rc = request_exclusive(cd,
  2039. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2040. if (rc < 0) {
  2041. pt_debug(cd->dev, DL_ERROR,
  2042. "%s: fail get exclusive ex=%p own=%p\n",
  2043. __func__, cd->exclusive_dev, cd->dev);
  2044. return rc;
  2045. }
  2046. }
  2047. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2048. /* Hard coded register for PIP2.x */
  2049. pip2_cmd.reg[0] = 0x01;
  2050. pip2_cmd.reg[1] = 0x01;
  2051. /*
  2052. * For PIP2.1+ the length field value includes itself:
  2053. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2054. *
  2055. * The overall write length must include only the register:
  2056. * ADD 2: 2 (Register)
  2057. */
  2058. extra_bytes = 6;
  2059. write_len = 2;
  2060. pip2_cmd.len = report_body_len + extra_bytes;
  2061. pip2_cmd.id = id;
  2062. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2063. pip2_cmd.data = data;
  2064. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2065. /* Add the command length to the extra bytes based on PIP version */
  2066. write_len += pip2_cmd.len;
  2067. write_buf = kzalloc(write_len, GFP_KERNEL);
  2068. if (write_buf == NULL) {
  2069. rc = -ENOMEM;
  2070. goto exit;
  2071. }
  2072. write_buf[i++] = pip2_cmd.reg[0];
  2073. write_buf[i++] = pip2_cmd.reg[1];
  2074. write_buf[i++] = pip2_cmd.len & 0xff;
  2075. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2076. write_buf[i++] = pip2_cmd.seq;
  2077. write_buf[i++] = pip2_cmd.id;
  2078. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2079. write_buf[j] = pip2_cmd.data[j-i];
  2080. write_buf[j++] = pip2_cmd.crc[0];
  2081. write_buf[j++] = pip2_cmd.crc[1];
  2082. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2083. if (read_len < 0)
  2084. read_len = 255;
  2085. pt_debug(dev, DL_INFO,
  2086. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2087. __func__, pip2_cmd.id, read_len);
  2088. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2089. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2090. if (rc) {
  2091. pt_debug(dev, DL_ERROR,
  2092. "%s: SPI write Error = %d\n",
  2093. __func__, rc);
  2094. goto exit;
  2095. }
  2096. #ifdef PT_POLL_RESP_BY_BUS
  2097. /*
  2098. * Frequent bus read can increase system load obviously. The expected
  2099. * first bus read should be valid and timely. The tollerance for
  2100. * usleep_range should be limited. The minimum response delay (between
  2101. * command finishes sending and INT pin falls) is less than 50
  2102. * microseconds. So the 10 microseconds should be maximum tollerance
  2103. * with the consideration that the unit to calculate the response delay
  2104. * is 10 microseconds and more precise is not necessary. Every
  2105. * additional 10 microseconds only contribute less than 3 milliseconds
  2106. * for whole BL.
  2107. */
  2108. usleep_range(resp_time_min, resp_time_min+10);
  2109. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2110. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2111. rc = pt_adap_read_default(cd, read_buf, read_len);
  2112. if (rc) {
  2113. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2114. __func__, rc);
  2115. break;
  2116. }
  2117. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2118. size = get_unaligned_le16(&read_buf[0]);
  2119. if ((size <= read_len) &&
  2120. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2121. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2122. break;
  2123. }
  2124. /*
  2125. * To reduce the bus and system load, increase the sleep
  2126. * step gradually:
  2127. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2128. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2129. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2130. */
  2131. retry++;
  2132. if (retry < 20) {
  2133. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2134. usleep_range(retry_interval,
  2135. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2136. } else if (retry < 40) {
  2137. retry_interval += 1000;
  2138. usleep_range(retry_interval, retry_interval + 1000);
  2139. } else {
  2140. retry_interval += 50000;
  2141. msleep(retry_interval/1000);
  2142. }
  2143. retry_total_time += retry_interval;
  2144. }
  2145. #else
  2146. /*
  2147. * Frequent GPIO read will not increase CPU/system load heavily if the
  2148. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2149. * fixed interval: 20 us.
  2150. */
  2151. usleep_range(resp_time_min, resp_time_min+10);
  2152. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2153. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2154. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2155. rc = pt_adap_read_default(cd, read_buf, read_len);
  2156. size = get_unaligned_le16(&read_buf[0]);
  2157. if (rc)
  2158. pt_debug(dev, DL_ERROR,
  2159. "%s: SPI read Error = %d\n",
  2160. __func__, rc);
  2161. else if (size > read_len) {
  2162. pt_debug(cd->dev, DL_ERROR,
  2163. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2164. __func__, size, read_len);
  2165. rc = -EIO;
  2166. }
  2167. break;
  2168. }
  2169. /*
  2170. * Poll GPIO with fixed interval 20 us, and tollerance is
  2171. * limited to 10 us to speed up the process.
  2172. */
  2173. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2174. usleep_range(retry_interval, retry_interval+10);
  2175. retry_total_time += retry_interval;
  2176. }
  2177. #endif
  2178. *actual_read_len = size;
  2179. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2180. pt_debug(dev, DL_ERROR,
  2181. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2182. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2183. *actual_read_len = 0;
  2184. rc = -EINVAL;
  2185. }
  2186. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2187. "<<< NO_INT Read");
  2188. exit:
  2189. kfree(write_buf);
  2190. if (protect == PT_CORE_CMD_PROTECTED) {
  2191. if (release_exclusive(cd, cd->dev) < 0)
  2192. pt_debug(cd->dev, DL_ERROR,
  2193. "%s: fail to release exclusive\n", __func__);
  2194. }
  2195. return rc;
  2196. }
  2197. /*******************************************************************************
  2198. * FUNCTION: pt_pip_null_
  2199. *
  2200. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2201. * This function is used by watchdog to check if the fw corrupts.
  2202. *
  2203. * RETURN:
  2204. * 0 = success
  2205. * !0 = failure
  2206. *
  2207. * PARAMETERS:
  2208. * *cd - pointer to core data
  2209. ******************************************************************************/
  2210. static int pt_pip_null_(struct pt_core_data *cd)
  2211. {
  2212. struct pt_hid_output hid_output = {
  2213. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2214. };
  2215. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2216. }
  2217. /*******************************************************************************
  2218. * FUNCTION: pt_pip_null
  2219. *
  2220. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2221. *
  2222. * RETURN:
  2223. * 0 = success
  2224. * !0 = failure
  2225. *
  2226. * PARAMETERS:
  2227. * *cd - pointer to core data
  2228. ******************************************************************************/
  2229. static int pt_pip_null(struct pt_core_data *cd)
  2230. {
  2231. int rc;
  2232. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2233. if (rc < 0) {
  2234. pt_debug(cd->dev, DL_ERROR,
  2235. "%s: fail get exclusive ex=%p own=%p\n",
  2236. __func__, cd->exclusive_dev, cd->dev);
  2237. return rc;
  2238. }
  2239. rc = pt_pip_null_(cd);
  2240. if (release_exclusive(cd, cd->dev) < 0)
  2241. pt_debug(cd->dev, DL_ERROR,
  2242. "%s: fail to release exclusive\n", __func__);
  2243. return rc;
  2244. }
  2245. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2246. /*******************************************************************************
  2247. * FUNCTION: pt_pip_start_bootloader_
  2248. *
  2249. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2250. *
  2251. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2252. * the WD active could cause this function to fail!
  2253. *
  2254. * RETURN:
  2255. * 0 = success
  2256. * !0 = failure
  2257. *
  2258. * PARAMETERS:
  2259. * *cd - pointer to core data
  2260. ******************************************************************************/
  2261. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2262. {
  2263. int rc = 0;
  2264. struct pt_hid_output hid_output = {
  2265. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2266. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2267. .reset_expected = 1,
  2268. };
  2269. if (cd->watchdog_enabled) {
  2270. pt_debug(cd->dev, DL_WARN,
  2271. "%s: watchdog isn't stopped before enter bl\n",
  2272. __func__);
  2273. goto exit;
  2274. }
  2275. /* Reset startup status after entering BL, new DUT enum required */
  2276. cd->startup_status = STARTUP_STATUS_START;
  2277. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2278. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2279. if (rc) {
  2280. pt_debug(cd->dev, DL_ERROR,
  2281. "%s: Start BL PIP cmd failed. rc = %d\n",
  2282. __func__, rc);
  2283. }
  2284. exit:
  2285. return rc;
  2286. }
  2287. /*******************************************************************************
  2288. * FUNCTION: pt_pip_start_bootloader
  2289. *
  2290. * SUMMARY: Protected function to force DUT to enter the BL
  2291. *
  2292. * RETURN:
  2293. * 0 = success
  2294. * !0 = failure
  2295. *
  2296. * PARAMETERS:
  2297. * *cd - pointer to core data structure
  2298. ******************************************************************************/
  2299. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2300. {
  2301. int rc;
  2302. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2303. if (rc < 0) {
  2304. pt_debug(cd->dev, DL_ERROR,
  2305. "%s: fail get exclusive ex=%p own=%p\n",
  2306. __func__, cd->exclusive_dev, cd->dev);
  2307. return rc;
  2308. }
  2309. rc = pt_pip_start_bootloader_(cd);
  2310. if (release_exclusive(cd, cd->dev) < 0)
  2311. pt_debug(cd->dev, DL_ERROR,
  2312. "%s: fail to release exclusive\n", __func__);
  2313. return rc;
  2314. }
  2315. /*******************************************************************************
  2316. * FUNCTION: _pt_request_pip_start_bl
  2317. *
  2318. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2319. * modules to request the DUT to enter the BL
  2320. *
  2321. * RETURN:
  2322. * 0 = success
  2323. * !0 = failure
  2324. *
  2325. * PARAMETERS:
  2326. * *dev - pointer to device structure
  2327. * protect - flag to run in protected mode
  2328. ******************************************************************************/
  2329. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2330. {
  2331. struct pt_core_data *cd = dev_get_drvdata(dev);
  2332. if (protect)
  2333. return pt_pip_start_bootloader(cd);
  2334. return pt_pip_start_bootloader_(cd);
  2335. }
  2336. /*******************************************************************************
  2337. * FUNCTION: pt_pip2_ver_load_ttdata
  2338. *
  2339. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2340. * command into the core data struct.
  2341. *
  2342. * RETURN: n/a
  2343. *
  2344. * PARAMETERS:
  2345. * *cd - pointer to core data structure
  2346. * len - Length of data in response_buf
  2347. ******************************************************************************/
  2348. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2349. {
  2350. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2351. struct pt_pip2_version_full *full_ver;
  2352. struct pt_pip2_version *ver;
  2353. /*
  2354. * The PIP2 VERSION command can return different lengths of data.
  2355. * The additional LOT fields are included when the packet
  2356. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2357. * NOTE:
  2358. * - The FW would swap the BL and FW versions when reporting
  2359. * the small packet.
  2360. * - Sub Lot bytes 16 and 17 are reserved.
  2361. */
  2362. if (len >= 0x1D) {
  2363. full_ver = (struct pt_pip2_version_full *)
  2364. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2365. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2366. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2367. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2368. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2369. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2370. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2371. /*
  2372. * BL PIP 2.02 and greater the version fields are
  2373. * swapped
  2374. */
  2375. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2376. ttdata->chip_rev =
  2377. get_unaligned_le16(&full_ver->chip_rev);
  2378. ttdata->chip_id =
  2379. get_unaligned_le16(&full_ver->chip_id);
  2380. } else {
  2381. ttdata->chip_rev =
  2382. get_unaligned_le16(&full_ver->chip_id);
  2383. ttdata->chip_id =
  2384. get_unaligned_le16(&full_ver->chip_rev);
  2385. }
  2386. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2387. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2388. sizeof(struct pt_pip2_version_full),
  2389. "PIP2 VERSION FULL");
  2390. } else {
  2391. ver = (struct pt_pip2_version *)
  2392. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2393. ttdata->pip_ver_major = ver->pip2_version_msb;
  2394. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2395. ttdata->bl_ver_major = ver->bl_version_msb;
  2396. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2397. ttdata->fw_ver_major = ver->fw_version_msb;
  2398. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2399. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2400. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2401. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2402. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2403. }
  2404. }
  2405. /*******************************************************************************
  2406. * FUNCTION: pt_si_get_ttdata
  2407. *
  2408. * SUMMARY: Function to load the version information from the system information
  2409. * PIP command into the core data struct.
  2410. *
  2411. * RETURN: n/a
  2412. *
  2413. * PARAMETERS:
  2414. * *cd - pointer to core data structure
  2415. ******************************************************************************/
  2416. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2417. {
  2418. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2419. struct pt_ttdata_dev *ttdata_dev =
  2420. (struct pt_ttdata_dev *)
  2421. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2422. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2423. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2424. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2425. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2426. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2427. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2428. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2429. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2430. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2431. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2432. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2433. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2434. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2435. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2436. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2437. }
  2438. /*******************************************************************************
  2439. * FUNCTION: pt_si_get_sensing_conf_data
  2440. *
  2441. * SUMMARY: Function to load the sensing information from the system information
  2442. * PIP command into the core data struct.
  2443. *
  2444. * RETURN: n/a
  2445. *
  2446. * PARAMETERS:
  2447. * *cd - pointer to core data structure
  2448. ******************************************************************************/
  2449. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2450. {
  2451. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2452. struct pt_sensing_conf_data_dev *scd_dev =
  2453. (struct pt_sensing_conf_data_dev *)
  2454. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2455. scd->electrodes_x = scd_dev->electrodes_x;
  2456. scd->electrodes_y = scd_dev->electrodes_y;
  2457. scd->origin_x = scd_dev->origin_x;
  2458. scd->origin_y = scd_dev->origin_y;
  2459. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2460. if (scd->origin_x & 0x02) {
  2461. scd->tx_num = scd->electrodes_x;
  2462. scd->rx_num = scd->electrodes_y;
  2463. } else {
  2464. scd->tx_num = scd->electrodes_y;
  2465. scd->rx_num = scd->electrodes_x;
  2466. }
  2467. /*
  2468. * When the Panel ID is coming from an XY pin and not a dedicated
  2469. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2470. * other DUTs as the loader will use cd->pid_for_loader to generate
  2471. * the bin file name but will ignore it if pid_for_loader is still
  2472. * set to PANEL_ID_NOT_ENABLED
  2473. */
  2474. if (cd->panel_id_support &
  2475. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2476. mutex_lock(&cd->system_lock);
  2477. cd->pid_for_loader = scd_dev->panel_id;
  2478. mutex_unlock(&cd->system_lock);
  2479. }
  2480. scd->panel_id = scd_dev->panel_id;
  2481. scd->btn = scd_dev->btn;
  2482. scd->scan_mode = scd_dev->scan_mode;
  2483. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2484. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2485. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2486. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2487. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2488. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2489. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2490. sizeof(struct pt_sensing_conf_data_dev),
  2491. "sensing_conf_data");
  2492. }
  2493. /*******************************************************************************
  2494. * FUNCTION: pt_si_setup
  2495. *
  2496. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2497. *
  2498. * RETURN:
  2499. * 0 = success
  2500. * !0 = failure
  2501. *
  2502. * PARAMETERS:
  2503. * *cd - pointer to core data structure
  2504. ******************************************************************************/
  2505. static int pt_si_setup(struct pt_core_data *cd)
  2506. {
  2507. struct pt_sysinfo *si = &cd->sysinfo;
  2508. int max_tch = si->sensing_conf_data.max_tch;
  2509. if (!si->xy_data)
  2510. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2511. GFP_KERNEL);
  2512. if (!si->xy_data)
  2513. return -ENOMEM;
  2514. if (!si->xy_mode)
  2515. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2516. if (!si->xy_mode) {
  2517. kfree(si->xy_data);
  2518. return -ENOMEM;
  2519. }
  2520. return 0;
  2521. }
  2522. /*******************************************************************************
  2523. * FUNCTION: pt_si_get_btn_data
  2524. *
  2525. * SUMMARY: Setup the core data button information based on the response of the
  2526. * System Information PIP command.
  2527. *
  2528. * RETURN:
  2529. * 0 = success
  2530. * !0 = failure
  2531. *
  2532. * PARAMETERS:
  2533. * *cd - pointer to core data structure
  2534. ******************************************************************************/
  2535. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2536. {
  2537. struct pt_sysinfo *si = &cd->sysinfo;
  2538. int num_btns = 0;
  2539. int num_defined_keys;
  2540. u16 *key_table;
  2541. int btn;
  2542. int i;
  2543. int rc = 0;
  2544. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2545. & PIP1_SYSINFO_BTN_MASK;
  2546. size_t btn_keys_size;
  2547. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2548. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2549. if (btns & (1 << i))
  2550. num_btns++;
  2551. }
  2552. si->num_btns = num_btns;
  2553. if (num_btns) {
  2554. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2555. if (!si->btn)
  2556. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2557. if (!si->btn)
  2558. return -ENOMEM;
  2559. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2560. num_defined_keys = 0;
  2561. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2562. num_defined_keys = 0;
  2563. else
  2564. num_defined_keys = cd->cpdata->sett
  2565. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2566. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2567. key_table = (u16 *)cd->cpdata->sett
  2568. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2569. si->btn[btn].key_code = key_table[btn];
  2570. si->btn[btn].enabled = true;
  2571. }
  2572. for (; btn < num_btns; btn++) {
  2573. si->btn[btn].key_code = KEY_RESERVED;
  2574. si->btn[btn].enabled = true;
  2575. }
  2576. return rc;
  2577. }
  2578. kfree(si->btn);
  2579. si->btn = NULL;
  2580. return rc;
  2581. }
  2582. /*******************************************************************************
  2583. * FUNCTION: pt_si_put_log_data
  2584. *
  2585. * SUMMARY: Prints all sys info data to kmsg log
  2586. *
  2587. * RETURN: n/a
  2588. *
  2589. * PARAMETERS:
  2590. * *cd - pointer to core data structure
  2591. ******************************************************************************/
  2592. static void pt_si_put_log_data(struct pt_core_data *cd)
  2593. {
  2594. struct pt_sysinfo *si = &cd->sysinfo;
  2595. struct pt_ttdata *ttdata = &si->ttdata;
  2596. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2597. int i;
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2599. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2601. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2603. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2605. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2607. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2608. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2609. __func__, ttdata->revctrl, ttdata->revctrl);
  2610. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2611. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2612. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2613. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2614. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2615. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2616. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2617. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2618. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2619. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2620. for (i = 0; i < PT_NUM_MFGID; i++)
  2621. pt_debug(cd->dev, DL_DEBUG,
  2622. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2623. __func__, i, ttdata->mfg_id[i],
  2624. ttdata->mfg_id[i]);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2626. __func__, ttdata->post_code, ttdata->post_code);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2628. __func__, scd->electrodes_x, scd->electrodes_x);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2630. __func__, scd->electrodes_y, scd->electrodes_y);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2632. __func__, scd->len_x, scd->len_x);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2634. __func__, scd->len_y, scd->len_y);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2636. __func__, scd->res_x, scd->res_x);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2638. __func__, scd->res_y, scd->res_y);
  2639. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2640. __func__, scd->max_z, scd->max_z);
  2641. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2642. __func__, scd->origin_x, scd->origin_x);
  2643. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2644. __func__, scd->origin_y, scd->origin_y);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2646. __func__, scd->panel_id, scd->panel_id);
  2647. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2648. __func__, scd->btn, scd->btn);
  2649. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2650. __func__, scd->scan_mode, scd->scan_mode);
  2651. pt_debug(cd->dev, DL_DEBUG,
  2652. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2653. __func__, scd->max_tch, scd->max_tch);
  2654. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2655. __func__, si->xy_mode);
  2656. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2657. __func__, si->xy_data);
  2658. }
  2659. /*******************************************************************************
  2660. * FUNCTION: pt_get_sysinfo_regs
  2661. *
  2662. * SUMMARY: Setup all the core data System information based on the response
  2663. * of the System Information PIP command.
  2664. *
  2665. * RETURN:
  2666. * 0 = success
  2667. * !0 = failure
  2668. *
  2669. * PARAMETERS:
  2670. * *cd - pointer to core data structure
  2671. ******************************************************************************/
  2672. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2673. {
  2674. struct pt_sysinfo *si = &cd->sysinfo;
  2675. int rc;
  2676. rc = pt_si_get_btn_data(cd);
  2677. if (rc < 0)
  2678. return rc;
  2679. pt_si_get_ttdata(cd);
  2680. pt_si_get_sensing_conf_data(cd);
  2681. pt_si_setup(cd);
  2682. pt_si_put_log_data(cd);
  2683. si->ready = true;
  2684. return rc;
  2685. }
  2686. /*******************************************************************************
  2687. * FUNCTION: pt_free_si_ptrs
  2688. *
  2689. * SUMMARY: Frees all memory associated with the System Information within
  2690. * core data
  2691. *
  2692. * RETURN: n/a
  2693. *
  2694. * PARAMETERS:
  2695. * *cd - pointer to core data structure
  2696. ******************************************************************************/
  2697. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2698. {
  2699. struct pt_sysinfo *si = &cd->sysinfo;
  2700. kfree(si->btn);
  2701. kfree(si->xy_mode);
  2702. kfree(si->xy_data);
  2703. }
  2704. /*******************************************************************************
  2705. * FUNCTION: pt_hid_output_get_sysinfo_
  2706. *
  2707. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2708. * response.
  2709. *
  2710. * RETURN::
  2711. * 0 = success
  2712. * !0 = failure
  2713. *
  2714. * PARAMETERS:
  2715. * *cd - pointer to core data structure
  2716. ******************************************************************************/
  2717. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2718. {
  2719. int rc = 0;
  2720. struct pt_hid_output hid_output = {
  2721. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2722. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2723. };
  2724. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2725. if (rc)
  2726. return rc;
  2727. /* Parse the sysinfo data */
  2728. rc = pt_get_sysinfo_regs(cd);
  2729. if (rc)
  2730. pt_free_si_ptrs(cd);
  2731. return rc;
  2732. }
  2733. /*******************************************************************************
  2734. * FUNCTION: pt_hid_output_get_sysinfo
  2735. *
  2736. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2737. *
  2738. * RETURN::
  2739. * 0 = success
  2740. * !0 = failure
  2741. *
  2742. * PARAMETERS:
  2743. * *cd - pointer to core data structure
  2744. ******************************************************************************/
  2745. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2746. {
  2747. int rc;
  2748. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2749. if (rc < 0) {
  2750. pt_debug(cd->dev, DL_ERROR,
  2751. "%s: fail get exclusive ex=%p own=%p\n",
  2752. __func__, cd->exclusive_dev, cd->dev);
  2753. return rc;
  2754. }
  2755. rc = pt_hid_output_get_sysinfo_(cd);
  2756. if (release_exclusive(cd, cd->dev) < 0)
  2757. pt_debug(cd->dev, DL_ERROR,
  2758. "%s: fail to release exclusive\n", __func__);
  2759. return rc;
  2760. }
  2761. /*******************************************************************************
  2762. * FUNCTION: pt_pip_suspend_scanning_
  2763. *
  2764. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2765. *
  2766. * RETURN::
  2767. * 0 = success
  2768. * !0 = failure
  2769. *
  2770. * PARAMETERS:
  2771. * *cd - pointer to core data structure
  2772. ******************************************************************************/
  2773. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2774. {
  2775. int rc = 0;
  2776. struct pt_hid_output hid_output = {
  2777. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2778. };
  2779. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2780. if (rc) {
  2781. pt_debug(cd->dev, DL_ERROR,
  2782. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2783. __func__, rc);
  2784. }
  2785. return rc;
  2786. }
  2787. /*******************************************************************************
  2788. * FUNCTION: pt_pip_suspend_scanning
  2789. *
  2790. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2791. *
  2792. * RETURN::
  2793. * 0 = success
  2794. * !0 = failure
  2795. *
  2796. * PARAMETERS:
  2797. * *cd - pointer to core data structure
  2798. ******************************************************************************/
  2799. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2800. {
  2801. int rc;
  2802. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2803. if (rc < 0) {
  2804. pt_debug(cd->dev, DL_ERROR,
  2805. "%s: fail get exclusive ex=%p own=%p\n",
  2806. __func__, cd->exclusive_dev, cd->dev);
  2807. return rc;
  2808. }
  2809. rc = pt_pip_suspend_scanning_(cd);
  2810. if (release_exclusive(cd, cd->dev) < 0)
  2811. pt_debug(cd->dev, DL_ERROR,
  2812. "%s: fail to release exclusive\n", __func__);
  2813. return rc;
  2814. }
  2815. /*******************************************************************************
  2816. * FUNCTION: _pt_request_pip_suspend_scanning
  2817. *
  2818. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2819. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2820. *
  2821. * RETURN:
  2822. * 0 = success
  2823. * !0 = failure
  2824. *
  2825. * PARAMETERS:
  2826. * *dev - pointer to device structure
  2827. * protect - 0 = call non-protected function
  2828. * 1 = call protected function
  2829. ******************************************************************************/
  2830. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2831. int protect)
  2832. {
  2833. struct pt_core_data *cd = dev_get_drvdata(dev);
  2834. if (protect)
  2835. return pt_pip_suspend_scanning(cd);
  2836. return pt_pip_suspend_scanning_(cd);
  2837. }
  2838. /*******************************************************************************
  2839. * FUNCTION: pt_pip_resume_scanning_
  2840. *
  2841. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2842. *
  2843. * RETURN::
  2844. * 0 = success
  2845. * !0 = failure
  2846. *
  2847. * PARAMETERS:
  2848. * *cd - pointer to core data structure
  2849. ******************************************************************************/
  2850. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2851. {
  2852. int rc = 0;
  2853. struct pt_hid_output hid_output = {
  2854. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2855. };
  2856. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2857. if (rc) {
  2858. pt_debug(cd->dev, DL_ERROR,
  2859. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2860. __func__, rc);
  2861. }
  2862. return rc;
  2863. }
  2864. /*******************************************************************************
  2865. * FUNCTION: pt_pip_resume_scanning
  2866. *
  2867. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2868. *
  2869. * RETURN::
  2870. * 0 = success
  2871. * !0 = failure
  2872. *
  2873. * PARAMETERS:
  2874. * *cd - pointer to core data structure
  2875. ******************************************************************************/
  2876. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2877. {
  2878. int rc;
  2879. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2880. if (rc < 0) {
  2881. pt_debug(cd->dev, DL_ERROR,
  2882. "%s: fail get exclusive ex=%p own=%p\n",
  2883. __func__, cd->exclusive_dev, cd->dev);
  2884. return rc;
  2885. }
  2886. rc = pt_pip_resume_scanning_(cd);
  2887. if (release_exclusive(cd, cd->dev) < 0)
  2888. pt_debug(cd->dev, DL_ERROR,
  2889. "%s: fail to release exclusive\n", __func__);
  2890. return rc;
  2891. }
  2892. /*******************************************************************************
  2893. * FUNCTION: _pt_request_pip_resume_scanning
  2894. *
  2895. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2896. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2897. *
  2898. * RETURN::
  2899. * 0 = success
  2900. * !0 = failure
  2901. *
  2902. * PARAMETERS:
  2903. * *dev - pointer to device structure
  2904. * protect - 0 = call non-protected function
  2905. * 1 = call protected function
  2906. ******************************************************************************/
  2907. static int _pt_request_pip_resume_scanning(struct device *dev,
  2908. int protect)
  2909. {
  2910. struct pt_core_data *cd = dev_get_drvdata(dev);
  2911. if (protect)
  2912. return pt_pip_resume_scanning(cd);
  2913. return pt_pip_resume_scanning_(cd);
  2914. }
  2915. /*******************************************************************************
  2916. * FUNCTION: pt_pip_get_param_
  2917. *
  2918. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2919. * the 32bit parameter value
  2920. *
  2921. * RETURN:
  2922. * 0 = success
  2923. * !0 = failure
  2924. *
  2925. * PARAMETERS:
  2926. * *cd - pointer to core data
  2927. * param_id - parameter ID to retrieve
  2928. * *value - value of DUT parameter
  2929. ******************************************************************************/
  2930. static int pt_pip_get_param_(struct pt_core_data *cd,
  2931. u8 param_id, u32 *value)
  2932. {
  2933. int write_length = 1;
  2934. u8 param[1] = { param_id };
  2935. u8 read_param_id;
  2936. int param_size;
  2937. u8 *ptr;
  2938. int rc = 0;
  2939. int i;
  2940. struct pt_hid_output hid_output = {
  2941. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2942. .write_length = write_length,
  2943. .write_buf = param,
  2944. };
  2945. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2946. if (rc)
  2947. return rc;
  2948. read_param_id = cd->response_buf[5];
  2949. if (read_param_id != param_id)
  2950. return -EPROTO;
  2951. param_size = cd->response_buf[6];
  2952. ptr = &cd->response_buf[7];
  2953. *value = 0;
  2954. for (i = 0; i < param_size; i++)
  2955. *value += ptr[i] << (i * 8);
  2956. return 0;
  2957. }
  2958. /*******************************************************************************
  2959. * FUNCTION: pt_pip_get_param
  2960. *
  2961. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2962. *
  2963. * RETURN:
  2964. * 0 = success
  2965. * !0 = failure
  2966. *
  2967. * PARAMETERS:
  2968. * *cd - pointer to core data
  2969. * param_id - parameter ID to retrieve
  2970. * *value - value of DUT parameter
  2971. ******************************************************************************/
  2972. static int pt_pip_get_param(struct pt_core_data *cd,
  2973. u8 param_id, u32 *value)
  2974. {
  2975. int rc;
  2976. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2977. if (rc < 0) {
  2978. pt_debug(cd->dev, DL_ERROR,
  2979. "%s: fail get exclusive ex=%p own=%p\n",
  2980. __func__, cd->exclusive_dev, cd->dev);
  2981. return rc;
  2982. }
  2983. rc = pt_pip_get_param_(cd, param_id, value);
  2984. if (release_exclusive(cd, cd->dev) < 0)
  2985. pt_debug(cd->dev, DL_ERROR,
  2986. "%s: fail to release exclusive\n", __func__);
  2987. return rc;
  2988. }
  2989. /*******************************************************************************
  2990. * FUNCTION: _pt_request_pip_get_param
  2991. *
  2992. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2993. * calls to the protected or unprotected call to pt_pip_get_param
  2994. *
  2995. * RETURN:
  2996. * 0 = success
  2997. * !0 = failure
  2998. *
  2999. * PARAMETERS:
  3000. * *dev - pointer to device structure
  3001. * protect - flag to call protected or non protected function
  3002. * param_id - parameter ID to retrieve
  3003. * *value - value of DUT parameter
  3004. ******************************************************************************/
  3005. int _pt_request_pip_get_param(struct device *dev,
  3006. int protect, u8 param_id, u32 *value)
  3007. {
  3008. struct pt_core_data *cd = dev_get_drvdata(dev);
  3009. if (protect)
  3010. return pt_pip_get_param(cd, param_id, value);
  3011. return pt_pip_get_param_(cd, param_id, value);
  3012. }
  3013. /*******************************************************************************
  3014. * FUNCTION: pt_pip_set_param_
  3015. *
  3016. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3017. * passed in value to flash
  3018. *
  3019. * RETURN:
  3020. * 0 = success
  3021. * !0 = failure
  3022. *
  3023. * PARAMETERS:
  3024. * *cd - pointer to core data
  3025. * param_id - parameter ID to set
  3026. * value - value to write
  3027. * size - size to write
  3028. ******************************************************************************/
  3029. static int pt_pip_set_param_(struct pt_core_data *cd,
  3030. u8 param_id, u32 value, u8 size)
  3031. {
  3032. u8 write_buf[6];
  3033. u8 *ptr = &write_buf[2];
  3034. int rc = 0;
  3035. int i;
  3036. struct pt_hid_output hid_output = {
  3037. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3038. .write_buf = write_buf,
  3039. };
  3040. write_buf[0] = param_id;
  3041. write_buf[1] = size;
  3042. for (i = 0; i < size; i++) {
  3043. ptr[i] = value & 0xFF;
  3044. value = value >> 8;
  3045. }
  3046. hid_output.write_length = 2 + size;
  3047. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3048. if (rc)
  3049. return rc;
  3050. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3051. return -EPROTO;
  3052. return 0;
  3053. }
  3054. /*******************************************************************************
  3055. * FUNCTION: pt_pip_set_param
  3056. *
  3057. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3058. *
  3059. * RETURN:
  3060. * 0 = success
  3061. * !0 = failure
  3062. *
  3063. * PARAMETERS:
  3064. * *cd - pointer to core data
  3065. * param_id - parameter ID to set
  3066. * value - value to write
  3067. * size - size to write
  3068. ******************************************************************************/
  3069. static int pt_pip_set_param(struct pt_core_data *cd,
  3070. u8 param_id, u32 value, u8 size)
  3071. {
  3072. int rc;
  3073. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3074. if (rc < 0) {
  3075. pt_debug(cd->dev, DL_ERROR,
  3076. "%s: fail get exclusive ex=%p own=%p\n",
  3077. __func__, cd->exclusive_dev, cd->dev);
  3078. return rc;
  3079. }
  3080. rc = pt_pip_set_param_(cd, param_id, value, size);
  3081. if (release_exclusive(cd, cd->dev) < 0)
  3082. pt_debug(cd->dev, DL_ERROR,
  3083. "%s: fail to release exclusive\n", __func__);
  3084. return rc;
  3085. }
  3086. /*******************************************************************************
  3087. * FUNCTION: _pt_request_pip_set_param
  3088. *
  3089. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3090. * calls to the protected or unprotected call to pt_pip_set_param
  3091. *
  3092. * RETURN:
  3093. * 0 = success
  3094. * !0 = failure
  3095. *
  3096. * PARAMETERS:
  3097. * *dev - pointer to device structure
  3098. * protect - flag to call protected or non-protected
  3099. * param_id - parameter ID to set
  3100. * value - value to write
  3101. * size - size to write
  3102. ******************************************************************************/
  3103. int _pt_request_pip_set_param(struct device *dev, int protect,
  3104. u8 param_id, u32 value, u8 size)
  3105. {
  3106. struct pt_core_data *cd = dev_get_drvdata(dev);
  3107. if (protect)
  3108. return pt_pip_set_param(cd, param_id, value, size);
  3109. return pt_pip_set_param_(cd, param_id, value, size);
  3110. }
  3111. /*******************************************************************************
  3112. * FUNCTION: _pt_pip_enter_easywake_state_
  3113. *
  3114. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3115. *
  3116. * RETURN:
  3117. * 0 = success
  3118. * !0 = failure
  3119. *
  3120. * PARAMETERS:
  3121. * *dev - pointer to device structure
  3122. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3123. * *return_data - return status if easywake was entered
  3124. ******************************************************************************/
  3125. static int pt_hid_output_enter_easywake_state_(
  3126. struct pt_core_data *cd, u8 data, u8 *return_data)
  3127. {
  3128. int write_length = 1;
  3129. u8 param[1] = { data };
  3130. int rc = 0;
  3131. struct pt_hid_output hid_output = {
  3132. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3133. .write_length = write_length,
  3134. .write_buf = param,
  3135. };
  3136. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3137. if (rc)
  3138. return rc;
  3139. *return_data = cd->response_buf[5];
  3140. return rc;
  3141. }
  3142. /*******************************************************************************
  3143. * FUNCTION: pt_pip_verify_config_block_crc_
  3144. *
  3145. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3146. *
  3147. * RETURN:
  3148. * 0 = success
  3149. * !0 = failure
  3150. *
  3151. * PARAMETERS:
  3152. * *cd - pointer the core data structure
  3153. * ebid - enumerated block ID
  3154. * *status - PIP command status
  3155. * calculated_crc - calculated CRC
  3156. * stored_crc - stored CRC in config area
  3157. ******************************************************************************/
  3158. static int pt_pip_verify_config_block_crc_(
  3159. struct pt_core_data *cd, u8 ebid, u8 *status,
  3160. u16 *calculated_crc, u16 *stored_crc)
  3161. {
  3162. int write_length = 1;
  3163. u8 param[1] = { ebid };
  3164. u8 *ptr;
  3165. int rc = 0;
  3166. struct pt_hid_output hid_output = {
  3167. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3168. .write_length = write_length,
  3169. .write_buf = param,
  3170. };
  3171. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3172. if (rc)
  3173. return rc;
  3174. ptr = &cd->response_buf[5];
  3175. *status = ptr[0];
  3176. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3177. *stored_crc = get_unaligned_le16(&ptr[3]);
  3178. return 0;
  3179. }
  3180. /*******************************************************************************
  3181. * FUNCTION: pt_pip_verify_config_block_crc
  3182. *
  3183. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3184. * an exclusive access to the DUT.
  3185. *
  3186. * RETURN:
  3187. * 0 = success
  3188. * !0 = failure
  3189. *
  3190. * PARAMETERS:
  3191. * *cd - pointer the core data structure
  3192. * ebid - enumerated block ID
  3193. * *status - PIP command status
  3194. * calculated_crc - calculated CRC
  3195. * stored_crc - stored CRC in config area
  3196. ******************************************************************************/
  3197. static int pt_pip_verify_config_block_crc(
  3198. struct pt_core_data *cd, u8 ebid, u8 *status,
  3199. u16 *calculated_crc, u16 *stored_crc)
  3200. {
  3201. int rc;
  3202. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3203. if (rc < 0) {
  3204. pt_debug(cd->dev, DL_ERROR,
  3205. "%s: fail get exclusive ex=%p own=%p\n",
  3206. __func__, cd->exclusive_dev, cd->dev);
  3207. return rc;
  3208. }
  3209. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3210. calculated_crc, stored_crc);
  3211. if (release_exclusive(cd, cd->dev) < 0)
  3212. pt_debug(cd->dev, DL_ERROR,
  3213. "%s: fail to release exclusive\n", __func__);
  3214. return rc;
  3215. }
  3216. /*******************************************************************************
  3217. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3218. *
  3219. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3220. * calls to the protected or unprotected call to
  3221. * pt_pip_verify_config_block_crc_
  3222. *
  3223. * RETURN:
  3224. * 0 = success
  3225. * !0 = failure
  3226. *
  3227. * PARAMETERS:
  3228. * *dev - pointer to device structure
  3229. * protect - flag to call protected or non-protected
  3230. * ebid - enumerated block ID
  3231. * *status - PIP command status
  3232. * calculated_crc - calculated CRC
  3233. * stored_crc - stored CRC in config area
  3234. ******************************************************************************/
  3235. static int _pt_request_pip_verify_config_block_crc(
  3236. struct device *dev, int protect, u8 ebid, u8 *status,
  3237. u16 *calculated_crc, u16 *stored_crc)
  3238. {
  3239. struct pt_core_data *cd = dev_get_drvdata(dev);
  3240. if (protect)
  3241. return pt_pip_verify_config_block_crc(cd, ebid,
  3242. status, calculated_crc, stored_crc);
  3243. return pt_pip_verify_config_block_crc_(cd, ebid,
  3244. status, calculated_crc, stored_crc);
  3245. }
  3246. /*******************************************************************************
  3247. * FUNCTION: pt_pip_get_config_row_size_
  3248. *
  3249. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3250. *
  3251. * RETURN:
  3252. * 0 = success
  3253. * !0 = failure
  3254. *
  3255. * PARAMETERS:
  3256. * *cd - pointer to core data
  3257. * protect - flag to call protected or non-protected
  3258. * *row_size - pointer to store the retrieved row size
  3259. ******************************************************************************/
  3260. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3261. u16 *row_size)
  3262. {
  3263. int rc = 0;
  3264. struct pt_hid_output hid_output = {
  3265. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3266. };
  3267. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3268. if (rc)
  3269. return rc;
  3270. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3271. return 0;
  3272. }
  3273. /*******************************************************************************
  3274. * FUNCTION: pt_pip_get_config_row_size
  3275. *
  3276. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3277. * an exclusive access to the DUT.
  3278. *
  3279. * RETURN:
  3280. * 0 = success
  3281. * !0 = failure
  3282. *
  3283. * PARAMETERS:
  3284. * *cd - pointer to core data
  3285. * protect - flag to call protected or non-protected
  3286. * *row_size - pointer to store the retrieved row size
  3287. ******************************************************************************/
  3288. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3289. u16 *row_size)
  3290. {
  3291. int rc;
  3292. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3293. if (rc < 0) {
  3294. pt_debug(cd->dev, DL_ERROR,
  3295. "%s: fail get exclusive ex=%p own=%p\n",
  3296. __func__, cd->exclusive_dev, cd->dev);
  3297. return rc;
  3298. }
  3299. rc = pt_pip_get_config_row_size_(cd, row_size);
  3300. if (release_exclusive(cd, cd->dev) < 0)
  3301. pt_debug(cd->dev, DL_ERROR,
  3302. "%s: fail to release exclusive\n", __func__);
  3303. return rc;
  3304. }
  3305. /*******************************************************************************
  3306. * FUNCTION: _pt_request_pip_get_config_row_size
  3307. *
  3308. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3309. * calls to the protected or unprotected call to
  3310. * pt_pip_get_config_row_size_
  3311. *
  3312. * RETURN:
  3313. * 0 = success
  3314. * !0 = failure
  3315. *
  3316. * PARAMETERS:
  3317. * *dev - pointer to device structure
  3318. * protect - flag to call protected or non-protected
  3319. * *row_size - pointer to store the retrieved row size
  3320. ******************************************************************************/
  3321. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3322. int protect, u16 *row_size)
  3323. {
  3324. struct pt_core_data *cd = dev_get_drvdata(dev);
  3325. if (protect)
  3326. return pt_pip_get_config_row_size(cd, row_size);
  3327. return pt_pip_get_config_row_size_(cd, row_size);
  3328. }
  3329. /*******************************************************************************
  3330. * FUNCTION: pt_pip1_read_data_block_
  3331. *
  3332. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3333. * output data to the "read_buf" and update "crc".
  3334. *
  3335. * RETURN:
  3336. * 0 = success
  3337. * !0 = failure
  3338. *
  3339. * PARAMETERS:
  3340. * *cd - pointer to core data
  3341. * row_number - row number
  3342. * length - length of data to read
  3343. * ebid - block id
  3344. * *actual_read_len - Actual data length read
  3345. * *read_buf - pointer to the buffer to store read data
  3346. * read_buf_size - size of read_buf
  3347. * *crc - pointer to store CRC of row data
  3348. ******************************************************************************/
  3349. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3350. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3351. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3352. {
  3353. int read_ebid;
  3354. int status;
  3355. int rc = 0;
  3356. int write_length = 5;
  3357. u8 write_buf[5];
  3358. u8 cmd_offset = 0;
  3359. u16 calc_crc;
  3360. struct pt_hid_output hid_output = {
  3361. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3362. .write_length = write_length,
  3363. .write_buf = write_buf,
  3364. };
  3365. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3366. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3367. write_buf[cmd_offset++] = LOW_BYTE(length);
  3368. write_buf[cmd_offset++] = HI_BYTE(length);
  3369. write_buf[cmd_offset++] = ebid;
  3370. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3371. if (rc)
  3372. return rc;
  3373. status = cd->response_buf[5];
  3374. if (status)
  3375. return status;
  3376. read_ebid = cd->response_buf[6];
  3377. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3378. return -EPROTO;
  3379. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3380. if (length == 0 || *actual_read_len == 0)
  3381. return 0;
  3382. if (read_buf_size >= *actual_read_len)
  3383. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3384. else
  3385. return -EPROTO;
  3386. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3387. /* Validate Row Data CRC */
  3388. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3389. if (*crc == calc_crc) {
  3390. return 0;
  3391. } else {
  3392. pt_debug(cd->dev, DL_ERROR,
  3393. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3394. __func__, *crc, calc_crc);
  3395. return -EPROTO;
  3396. }
  3397. }
  3398. /*******************************************************************************
  3399. * FUNCTION: _pt_request_pip_read_data_block
  3400. *
  3401. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3402. * calls to pt_pip1_read_data_block_
  3403. *
  3404. * RETURN:
  3405. * 0 = success
  3406. * !0 = failure
  3407. *
  3408. * PARAMETERS:
  3409. * *dev - pointer to device structure
  3410. * row_number - row number
  3411. * length - length of data to read
  3412. * ebid - block id
  3413. * *actual_read_len - Actual data length read
  3414. * *read_buf - pointer to the buffer to store read data
  3415. * *crc - pointer to store CRC of row data
  3416. ******************************************************************************/
  3417. static int _pt_request_pip_read_data_block(struct device *dev,
  3418. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3419. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3420. {
  3421. struct pt_core_data *cd = dev_get_drvdata(dev);
  3422. return pt_pip1_read_data_block_(cd, row_number, length,
  3423. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3424. }
  3425. /*******************************************************************************
  3426. * FUNCTION: pt_pip1_write_data_block_
  3427. *
  3428. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3429. * write data to the data block.
  3430. *
  3431. * RETURN:
  3432. * 0 = success
  3433. * !0 = failure
  3434. *
  3435. * PARAMETERS:
  3436. * *cd - pointer to core data
  3437. * row_number - row in config block to write to
  3438. * write_length - length of data to write
  3439. * ebid - enumerated block ID
  3440. * *write_buf - pointer to buffer to write
  3441. * *security_key - pointer to security key to allow write
  3442. * *actual_write_len - pointer to store data length actually written
  3443. ******************************************************************************/
  3444. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3445. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3446. u8 *security_key, u16 *actual_write_len)
  3447. {
  3448. /* row_number + write_len + ebid + security_key + crc */
  3449. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3450. u8 *full_write_buf;
  3451. u8 cmd_offset = 0;
  3452. u16 crc;
  3453. int status;
  3454. int rc = 0;
  3455. int read_ebid;
  3456. u8 *data;
  3457. struct pt_hid_output hid_output = {
  3458. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3459. .write_length = full_write_length,
  3460. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3461. };
  3462. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3463. if (!full_write_buf)
  3464. return -ENOMEM;
  3465. hid_output.write_buf = full_write_buf;
  3466. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3467. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3468. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3469. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3470. full_write_buf[cmd_offset++] = ebid;
  3471. data = &full_write_buf[cmd_offset];
  3472. memcpy(data, write_buf, write_length);
  3473. cmd_offset += write_length;
  3474. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3475. cmd_offset += 8;
  3476. crc = _pt_compute_crc(data, write_length);
  3477. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3478. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3479. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3480. if (rc)
  3481. goto exit;
  3482. status = cd->response_buf[5];
  3483. if (status) {
  3484. rc = -EINVAL;
  3485. goto exit;
  3486. }
  3487. read_ebid = cd->response_buf[6];
  3488. if (read_ebid != ebid) {
  3489. rc = -EPROTO;
  3490. goto exit;
  3491. }
  3492. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3493. exit:
  3494. kfree(full_write_buf);
  3495. return rc;
  3496. }
  3497. /*******************************************************************************
  3498. * FUNCTION: _pt_request_pip_write_data_block
  3499. *
  3500. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3501. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3502. *
  3503. * RETURN:
  3504. * 0 = success
  3505. * !0 = failure
  3506. *
  3507. * PARAMETERS:
  3508. * *dev - pointer to device structure
  3509. * protect - flag to call protected or non-protected
  3510. * row_number - row in config block to write to
  3511. * write_length - length of data to write
  3512. * ebid - enumerated block ID
  3513. * *write_buf - pointer to buffer to write
  3514. * *security_key - pointer to security key to allow write
  3515. * *actual_write_len - pointer to store data length actually written
  3516. ******************************************************************************/
  3517. static int _pt_request_pip_write_data_block(struct device *dev,
  3518. u16 row_number, u16 write_length, u8 ebid,
  3519. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3520. {
  3521. struct pt_core_data *cd = dev_get_drvdata(dev);
  3522. return pt_pip1_write_data_block_(cd, row_number,
  3523. write_length, ebid, write_buf, security_key,
  3524. actual_write_len);
  3525. }
  3526. /*******************************************************************************
  3527. * FUNCTION: pt_pip_get_data_structure_
  3528. *
  3529. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3530. * returning a structure of data defined by data_id
  3531. *
  3532. * RETURN:
  3533. * 0 = success
  3534. * !0 = failure
  3535. *
  3536. * PARAMETERS:
  3537. * *cd - pointer to core data
  3538. * read_offset - read pointer offset
  3539. * read_length - length of data to read
  3540. * data_id - data ID to read
  3541. * *status - pointer to store the read response status
  3542. * *data_format - pointer to store format of data read
  3543. * *actual_read_len - pointer to store data length actually read
  3544. * *data - pointer to store data read
  3545. ******************************************************************************/
  3546. static int pt_pip_get_data_structure_(
  3547. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3548. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3549. u8 *data)
  3550. {
  3551. int rc = 0;
  3552. u16 total_read_len = 0;
  3553. u16 read_len;
  3554. u16 off_buf = 0;
  3555. u8 write_buf[5];
  3556. u8 read_data_id;
  3557. struct pt_hid_output hid_output = {
  3558. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3559. .write_length = 5,
  3560. .write_buf = write_buf,
  3561. };
  3562. again:
  3563. write_buf[0] = LOW_BYTE(read_offset);
  3564. write_buf[1] = HI_BYTE(read_offset);
  3565. write_buf[2] = LOW_BYTE(read_length);
  3566. write_buf[3] = HI_BYTE(read_length);
  3567. write_buf[4] = data_id;
  3568. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3569. if (rc)
  3570. return rc;
  3571. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3572. goto set_status;
  3573. read_data_id = cd->response_buf[6];
  3574. if (read_data_id != data_id)
  3575. return -EPROTO;
  3576. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3577. if (read_len && data) {
  3578. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3579. total_read_len += read_len;
  3580. if (read_len < read_length) {
  3581. read_offset += read_len;
  3582. off_buf += read_len;
  3583. read_length -= read_len;
  3584. goto again;
  3585. }
  3586. }
  3587. if (data_format)
  3588. *data_format = cd->response_buf[9];
  3589. if (actual_read_len)
  3590. *actual_read_len = total_read_len;
  3591. set_status:
  3592. if (status)
  3593. *status = cd->response_buf[5];
  3594. return rc;
  3595. }
  3596. /*******************************************************************************
  3597. * FUNCTION: pt_pip_get_data_structure
  3598. *
  3599. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3600. * an exclusive access to the DUT.
  3601. *
  3602. * RETURN:
  3603. * 0 = success
  3604. * !0 = failure
  3605. *
  3606. * PARAMETERS:
  3607. * *cd - pointer to core data
  3608. * read_offset - read pointer offset
  3609. * read_length - length of data to read
  3610. * data_id - data ID to read
  3611. * *status - pointer to store the read response status
  3612. * *data_format - pointer to store format of data read
  3613. * *actual_read_len - pointer to store data length actually read
  3614. * *data - pointer to store data read
  3615. ******************************************************************************/
  3616. static int pt_pip_get_data_structure(
  3617. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3618. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3619. u8 *data)
  3620. {
  3621. int rc;
  3622. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3623. if (rc < 0) {
  3624. pt_debug(cd->dev, DL_ERROR,
  3625. "%s: fail get exclusive ex=%p own=%p\n",
  3626. __func__, cd->exclusive_dev, cd->dev);
  3627. return rc;
  3628. }
  3629. rc = pt_pip_get_data_structure_(cd, read_offset,
  3630. read_length, data_id, status, data_format,
  3631. actual_read_len, data);
  3632. if (release_exclusive(cd, cd->dev) < 0)
  3633. pt_debug(cd->dev, DL_ERROR,
  3634. "%s: fail to release exclusive\n", __func__);
  3635. return rc;
  3636. }
  3637. /*******************************************************************************
  3638. * FUNCTION: _pt_request_pip_get_data_structure
  3639. *
  3640. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3641. * calls to the protected or unprotected call to
  3642. * pt_pip_get_data_structure
  3643. *
  3644. * RETURN:
  3645. * 0 = success
  3646. * !0 = failure
  3647. *
  3648. * PARAMETERS:
  3649. * *dev - pointer to device structure
  3650. * protect - flag to call protected or non-protected
  3651. * read_offset - read pointer offset
  3652. * read_length - length of data to read
  3653. * data_id - data ID to read
  3654. * *status - pointer to store the read response status
  3655. * *data_format - pointer to store format of data read
  3656. * *actual_read_len - pointer to store data length actually read
  3657. * *data - pointer to store data read
  3658. ******************************************************************************/
  3659. static int _pt_request_pip_get_data_structure(struct device *dev,
  3660. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3661. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3662. {
  3663. struct pt_core_data *cd = dev_get_drvdata(dev);
  3664. if (protect)
  3665. return pt_pip_get_data_structure(cd,
  3666. read_offset, read_length, data_id, status,
  3667. data_format, actual_read_len, data);
  3668. return pt_pip_get_data_structure_(cd,
  3669. read_offset, read_length, data_id, status,
  3670. data_format, actual_read_len, data);
  3671. }
  3672. /*******************************************************************************
  3673. * FUNCTION: _pt_manage_local_cal_data
  3674. *
  3675. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3676. * CALIBRATION data. It stores it in a local static array and can be
  3677. * cleared, loaded or used to restore the CAL data back to the running FW.
  3678. * The CAL data is read or restored by use of the PIP1 commands:
  3679. * - READ_DATA_BLOCK (0x22)
  3680. * - WRITE_DATA_BLOCK (0x23)
  3681. *
  3682. * RETURN:
  3683. * 0 = success
  3684. * !0 = failure
  3685. *
  3686. * PARAMETERS:
  3687. * *dev - pointer to device structure
  3688. * action - One of the following actions:
  3689. * - PT_CAL_DATA_SAVE
  3690. * - PT_CAL_DATA_RESTORE
  3691. * - PT_CAL_DATA_CLEAR
  3692. * - PT_CAL_DATA_SIZE
  3693. * *size - pointer to the number of bytes transferred
  3694. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3695. ******************************************************************************/
  3696. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3697. unsigned short *crc)
  3698. {
  3699. struct pt_core_data *cd = dev_get_drvdata(dev);
  3700. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3701. unsigned short calc_id_crc = 0;
  3702. static u8 *cal_cache_data;
  3703. static u16 cal_cache_len;
  3704. static unsigned short cal_cache_chip_id;
  3705. int rc = 0;
  3706. u8 *tmp_data = NULL;
  3707. u8 row_number = 0;
  3708. u8 prefix[20];
  3709. u16 cal_size = 0;
  3710. u16 transfer_size;
  3711. u16 act_trans_len = 0;
  3712. u16 byte_offset = 0;
  3713. u16 cal_blk_size;
  3714. u16 total_rows;
  3715. u16 remain_bytes;
  3716. u16 data_block_crc;
  3717. u16 buf_size = 12;
  3718. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3719. __func__, action);
  3720. switch (action) {
  3721. case PT_CAL_DATA_SAVE:
  3722. /* Read the size of the CAL block and calculate # rows */
  3723. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3724. if (!tmp_data) {
  3725. rc = -ENOMEM;
  3726. goto exit;
  3727. }
  3728. /*
  3729. * Don't check rc as doing a read size will give a false
  3730. * error on the CRC check.
  3731. */
  3732. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3733. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3734. cal_blk_size = act_trans_len;
  3735. kfree(tmp_data);
  3736. pt_debug(dev, DL_INFO,
  3737. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3738. __func__, cal_cache_len, cal_blk_size);
  3739. /* Safety net to ensure we didn't read incorrect size */
  3740. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3741. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3742. __func__);
  3743. rc = 1;
  3744. goto exit;
  3745. }
  3746. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3747. if (cal_blk_size != cal_cache_len) {
  3748. kfree(cal_cache_data);
  3749. cal_cache_data = kzalloc(cal_blk_size + 2,
  3750. GFP_KERNEL);
  3751. if (!cal_cache_data) {
  3752. rc = -ENOMEM;
  3753. goto exit;
  3754. }
  3755. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3756. __func__);
  3757. }
  3758. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3759. /* Calculate how many rows [0-n] (PIP Transactions) */
  3760. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3761. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3762. /* Add row if we have a last partial row */
  3763. if (remain_bytes > 0)
  3764. total_rows++;
  3765. pt_debug(dev, DL_INFO,
  3766. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3767. __func__, cal_blk_size, total_rows, remain_bytes);
  3768. /* Read all rows unless an error occurs */
  3769. rc = 0;
  3770. while (rc == 0 && row_number <= total_rows) {
  3771. act_trans_len = 0;
  3772. if (remain_bytes > 0 && row_number == total_rows)
  3773. transfer_size = remain_bytes;
  3774. else
  3775. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3776. rc = pt_pip1_read_data_block_(cd, row_number,
  3777. transfer_size, PT_CAL_EBID,
  3778. &act_trans_len,
  3779. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3780. &data_block_crc);
  3781. if (rc) {
  3782. /* Error occurred, exit loop */
  3783. cal_size = 0;
  3784. break;
  3785. }
  3786. pt_debug(dev, DL_INFO,
  3787. "%s: CAL read rc=%d actual read len=%d\n",
  3788. __func__, rc, act_trans_len);
  3789. byte_offset += act_trans_len;
  3790. cal_size = byte_offset;
  3791. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3792. pt_pr_buf(dev, DL_INFO,
  3793. &cal_cache_data[byte_offset - act_trans_len],
  3794. act_trans_len, prefix);
  3795. row_number++;
  3796. }
  3797. if (cal_size > 0) {
  3798. /* Save a CRC of the chip info the CAL was saved from */
  3799. calc_id_crc = crc_ccitt_calculate(
  3800. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3801. cal_cache_chip_id = calc_id_crc;
  3802. cal_cache_len = cal_size;
  3803. pt_debug(dev, DL_INFO,
  3804. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3805. __func__, calc_id_crc, cal_size);
  3806. }
  3807. *size = cal_size;
  3808. *crc = calc_id_crc;
  3809. break;
  3810. case PT_CAL_DATA_RESTORE:
  3811. cal_size = cal_cache_len;
  3812. while ((rc == 0) && (byte_offset < cal_size)) {
  3813. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3814. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3815. else
  3816. transfer_size = cal_size - byte_offset;
  3817. rc = pt_pip1_write_data_block_(cd, row_number,
  3818. transfer_size, PT_CAL_EBID,
  3819. &cal_cache_data[byte_offset],
  3820. (u8 *)pt_data_block_security_key,
  3821. &act_trans_len);
  3822. byte_offset += act_trans_len;
  3823. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3824. __func__, byte_offset);
  3825. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3826. pt_pr_buf(dev, DL_INFO,
  3827. &cal_cache_data[byte_offset - act_trans_len],
  3828. act_trans_len, prefix);
  3829. if ((byte_offset > cal_size) ||
  3830. (act_trans_len != transfer_size))
  3831. rc = -EIO;
  3832. row_number++;
  3833. }
  3834. *size = byte_offset;
  3835. *crc = cal_cache_chip_id;
  3836. break;
  3837. case PT_CAL_DATA_CLEAR:
  3838. if (cal_cache_data)
  3839. memset(&cal_cache_data[0], 0, cal_cache_len);
  3840. cal_cache_len = 0;
  3841. cal_cache_chip_id = 0;
  3842. *size = 0;
  3843. *crc = 0;
  3844. break;
  3845. case PT_CAL_DATA_INFO:
  3846. default:
  3847. *size = cal_cache_len;
  3848. *crc = cal_cache_chip_id;
  3849. pt_debug(dev, DL_INFO,
  3850. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3851. __func__, cal_cache_chip_id,
  3852. cal_cache_len);
  3853. break;
  3854. }
  3855. exit:
  3856. pt_debug(dev, DL_INFO,
  3857. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3858. __func__, rc, *crc, *size);
  3859. return rc;
  3860. }
  3861. /*******************************************************************************
  3862. * FUNCTION: pt_pip_run_selftest_
  3863. *
  3864. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3865. * to execute a FW built in self test
  3866. *
  3867. * RETURN:
  3868. * 0 = success
  3869. * !0 = failure
  3870. *
  3871. * PARAMETERS:
  3872. * *cd - pointer to core data
  3873. * test_id - enumerated test ID to run
  3874. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3875. * *status - pointer to store the read response status
  3876. * *summary_results - pointer to store the results summary
  3877. * *results_available - pointer to store if results are available
  3878. *****************************************************************************/
  3879. static int pt_pip_run_selftest_(
  3880. struct pt_core_data *cd, u8 test_id,
  3881. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3882. u8 *results_available)
  3883. {
  3884. int rc = 0;
  3885. u8 write_buf[2];
  3886. struct pt_hid_output hid_output = {
  3887. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3888. .write_length = 2,
  3889. .write_buf = write_buf,
  3890. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3891. };
  3892. write_buf[0] = test_id;
  3893. write_buf[1] = write_idacs_to_flash;
  3894. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3895. hid_output.write_length = 1;
  3896. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3897. if (rc)
  3898. return rc;
  3899. if (status)
  3900. *status = cd->response_buf[5];
  3901. if (summary_result)
  3902. *summary_result = cd->response_buf[6];
  3903. /* results_available only available before PIP 1.03 */
  3904. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3905. if (results_available)
  3906. *results_available = cd->response_buf[7];
  3907. }
  3908. return rc;
  3909. }
  3910. /*******************************************************************************
  3911. * FUNCTION: pt_pip_run_selftest
  3912. *
  3913. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3914. * an exclusive access to the DUT.
  3915. *
  3916. * RETURN:
  3917. * 0 = success
  3918. * !0 = failure
  3919. *
  3920. * PARAMETERS:
  3921. * *cd - pointer to core data
  3922. * test_id - enumerated test ID to run
  3923. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3924. * *status - pointer to store the read response status
  3925. * *summary_results - pointer to store the results summary
  3926. * *results_available - pointer to store if results are available
  3927. ******************************************************************************/
  3928. static int pt_pip_run_selftest(
  3929. struct pt_core_data *cd, u8 test_id,
  3930. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3931. u8 *results_available)
  3932. {
  3933. int rc;
  3934. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3935. if (rc < 0) {
  3936. pt_debug(cd->dev, DL_ERROR,
  3937. "%s: fail get exclusive ex=%p own=%p\n",
  3938. __func__, cd->exclusive_dev, cd->dev);
  3939. return rc;
  3940. }
  3941. rc = pt_pip_run_selftest_(cd, test_id,
  3942. write_idacs_to_flash, status, summary_result,
  3943. results_available);
  3944. if (release_exclusive(cd, cd->dev) < 0)
  3945. pt_debug(cd->dev, DL_ERROR,
  3946. "%s: fail to release exclusive\n", __func__);
  3947. return rc;
  3948. }
  3949. /*******************************************************************************
  3950. * FUNCTION: _pt_request_pip_run_selftest
  3951. *
  3952. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3953. * calls to the protected or unprotected call to pt_pip_run_selftest
  3954. *
  3955. * RETURN:
  3956. * 0 = success
  3957. * !0 = failure
  3958. *
  3959. * PARAMETERS:
  3960. * *dev - pointer to device structure
  3961. * protect - flag to call protected or non-protected
  3962. * test_id - enumerated test ID to run
  3963. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3964. * *status - pointer to store the read response status
  3965. * *summary_results - pointer to store the results summary
  3966. * *results_available - pointer to store if results are available
  3967. ******************************************************************************/
  3968. static int _pt_request_pip_run_selftest(struct device *dev,
  3969. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3970. u8 *summary_result, u8 *results_available)
  3971. {
  3972. struct pt_core_data *cd = dev_get_drvdata(dev);
  3973. if (protect)
  3974. return pt_pip_run_selftest(cd, test_id,
  3975. write_idacs_to_flash, status, summary_result,
  3976. results_available);
  3977. return pt_pip_run_selftest_(cd, test_id,
  3978. write_idacs_to_flash, status, summary_result,
  3979. results_available);
  3980. }
  3981. /*******************************************************************************
  3982. * FUNCTION: _pt_pip_get_selftest_result_
  3983. *
  3984. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3985. * to retrieve the self test results from the self test already executed
  3986. *
  3987. * RETURN:
  3988. * 0 = success
  3989. * !0 = failure
  3990. *
  3991. * PARAMETERS:
  3992. * *cd - pointer to core data
  3993. * read_offset - read pointer offset
  3994. * read_length - length of data to read
  3995. * test_id - enumerated test ID to read selftest results from
  3996. * *status - pointer to store the read response status
  3997. * *actual_read_len - pointer to store data length actually read
  3998. * *status - pointer to where the cmd response statas is stored
  3999. ******************************************************************************/
  4000. static int pt_pip_get_selftest_result_(
  4001. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4002. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4003. {
  4004. int rc = 0;
  4005. u16 total_read_len = 0;
  4006. u16 read_len;
  4007. u16 off_buf = 0;
  4008. u8 write_buf[5];
  4009. u8 read_test_id;
  4010. bool repeat;
  4011. struct pt_hid_output hid_output = {
  4012. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4013. .write_length = 5,
  4014. .write_buf = write_buf,
  4015. };
  4016. /*
  4017. * Do not repeat reading for Auto Shorts test
  4018. * when PIP version < 1.3
  4019. */
  4020. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4021. || test_id != PT_ST_ID_AUTOSHORTS;
  4022. again:
  4023. write_buf[0] = LOW_BYTE(read_offset);
  4024. write_buf[1] = HI_BYTE(read_offset);
  4025. write_buf[2] = LOW_BYTE(read_length);
  4026. write_buf[3] = HI_BYTE(read_length);
  4027. write_buf[4] = test_id;
  4028. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4029. if (rc)
  4030. return rc;
  4031. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4032. goto set_status;
  4033. read_test_id = cd->response_buf[6];
  4034. if (read_test_id != test_id)
  4035. return -EPROTO;
  4036. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4037. if (read_len && data) {
  4038. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4039. total_read_len += read_len;
  4040. if (repeat && read_len < read_length) {
  4041. read_offset += read_len;
  4042. off_buf += read_len;
  4043. read_length -= read_len;
  4044. goto again;
  4045. }
  4046. }
  4047. if (actual_read_len)
  4048. *actual_read_len = total_read_len;
  4049. set_status:
  4050. if (status)
  4051. *status = cd->response_buf[5];
  4052. return rc;
  4053. }
  4054. /*******************************************************************************
  4055. * FUNCTION: _pt_pip_get_selftest_result
  4056. *
  4057. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4058. * access to the DUT
  4059. *
  4060. * RETURN:
  4061. * 0 = success
  4062. * !0 = failure
  4063. *
  4064. * PARAMETERS:
  4065. * *cd - pointer to core data
  4066. * read_offset - read pointer offset
  4067. * read_length - length of data to read
  4068. * test_id - enumerated test ID to read selftest results from
  4069. * *status - pointer to store the read response status
  4070. * *actual_read_len - pointer to store data length actually read
  4071. * *status - pointer to where the cmd response statas is stored
  4072. ******************************************************************************/
  4073. static int pt_pip_get_selftest_result(
  4074. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4075. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4076. {
  4077. int rc;
  4078. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4079. if (rc < 0) {
  4080. pt_debug(cd->dev, DL_ERROR,
  4081. "%s: fail get exclusive ex=%p own=%p\n",
  4082. __func__, cd->exclusive_dev, cd->dev);
  4083. return rc;
  4084. }
  4085. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4086. read_length, test_id, status, actual_read_len, data);
  4087. if (release_exclusive(cd, cd->dev) < 0)
  4088. pt_debug(cd->dev, DL_ERROR,
  4089. "%s: fail to release exclusive\n", __func__);
  4090. return rc;
  4091. }
  4092. /*******************************************************************************
  4093. * FUNCTION: _pt_request_pip_get_selftest_result
  4094. *
  4095. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4096. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4097. *
  4098. * RETURN:
  4099. * 0 = success
  4100. * !0 = failure
  4101. *
  4102. * PARAMETERS:
  4103. * *dev - pointer to device structure
  4104. * protect - flag to call protected or non-protected
  4105. * read_offset - read pointer offset
  4106. * read_length - length of data to read
  4107. * test_id - enumerated test ID to read selftest results from
  4108. * *status - pointer to store the read response status
  4109. * *actual_read_len - pointer to store data length actually read
  4110. * *data - pointer to where the data read is stored
  4111. ******************************************************************************/
  4112. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4113. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4114. u8 *status, u16 *actual_read_len, u8 *data)
  4115. {
  4116. struct pt_core_data *cd = dev_get_drvdata(dev);
  4117. if (protect)
  4118. return pt_pip_get_selftest_result(cd, read_offset,
  4119. read_length, test_id, status, actual_read_len,
  4120. data);
  4121. return pt_pip_get_selftest_result_(cd, read_offset,
  4122. read_length, test_id, status, actual_read_len,
  4123. data);
  4124. }
  4125. /*******************************************************************************
  4126. * FUNCTION: _pt_pip_load_self_test_param
  4127. *
  4128. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4129. * to load parameters needed by a self test
  4130. *
  4131. * RETURN:
  4132. * 0 = success
  4133. * !0 = failure
  4134. *
  4135. * PARAMETERS:
  4136. * *cd - pointer to core data
  4137. * self_test_id - enumerated test ID for which the parmeters belong
  4138. * load_offset - mem offset to where to load parameters
  4139. * load_length - length of parameter data to load
  4140. * *parameters - pointer to list of parameter data
  4141. * *status - pointer to store the response status
  4142. * *ret_test_id - pointer to returned test id the parameters were stored
  4143. * *act_load_len - pointer to store the actual load length that was writen
  4144. ******************************************************************************/
  4145. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4146. u8 self_test_id, u16 load_offset, u16 load_length,
  4147. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4148. {
  4149. int rc = 0;
  4150. int i;
  4151. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4152. struct pt_hid_output hid_output = {
  4153. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4154. .write_length = 5 + load_length,
  4155. .write_buf = write_buf,
  4156. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4157. };
  4158. write_buf[0] = LOW_BYTE(load_offset);
  4159. write_buf[1] = HI_BYTE(load_offset);
  4160. write_buf[2] = LOW_BYTE(load_length);
  4161. write_buf[3] = HI_BYTE(load_length);
  4162. write_buf[4] = self_test_id;
  4163. for (i = 0; i < load_length; i++)
  4164. write_buf[i + 5] = parameters[i];
  4165. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4166. if (rc)
  4167. return rc;
  4168. if (status)
  4169. *status = cd->response_buf[5];
  4170. if (ret_test_id)
  4171. *ret_test_id = cd->response_buf[6];
  4172. if (act_load_len)
  4173. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4174. return rc;
  4175. }
  4176. /*******************************************************************************
  4177. * FUNCTION: pt_pip_load_self_test_param
  4178. *
  4179. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4180. * access to the DUT.
  4181. *
  4182. * RETURN:
  4183. * 0 = success
  4184. * !0 = failure
  4185. *
  4186. * PARAMETERS:
  4187. * *cd - pointer to core data
  4188. * self_test_id - enumerated test ID for which the parmeters belong
  4189. * load_offset - mem offset to where to load parameters
  4190. * load_length - length of parameter data to load
  4191. * *parameters - pointer to list of parameter data
  4192. * *status - pointer to store the response status
  4193. * *ret_test_id - pointer to returned test id the parameters were stored
  4194. * *act_load_len - pointer to store the actual load length that was writen
  4195. ******************************************************************************/
  4196. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4197. u8 self_test_id, u16 load_offset, u16 load_length,
  4198. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4199. {
  4200. int rc;
  4201. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4202. if (rc < 0) {
  4203. pt_debug(cd->dev, DL_ERROR,
  4204. "%s: fail get exclusive ex=%p own=%p\n",
  4205. __func__, cd->exclusive_dev, cd->dev);
  4206. return rc;
  4207. }
  4208. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4209. load_length, parameters, status, ret_test_id, act_load_len);
  4210. if (release_exclusive(cd, cd->dev) < 0)
  4211. pt_debug(cd->dev, DL_ERROR,
  4212. "%s: fail to release exclusive\n", __func__);
  4213. return rc;
  4214. }
  4215. /*******************************************************************************
  4216. * FUNCTION: _pt_request_pip_load_self_test_param
  4217. *
  4218. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4219. * calls to the protected or unprotected call to
  4220. * pt_pip_load_self_test_param
  4221. *
  4222. * RETURN:
  4223. * 0 = success
  4224. * !0 = failure
  4225. *
  4226. * PARAMETERS:
  4227. * *dev - pointer to device structure
  4228. * protect - flag to call protected or non-protected
  4229. * self_test_id - enumerated test ID for which the parmeters belong
  4230. * load_offset - mem offset to where to load parameters
  4231. * load_length - length of parameter data to load
  4232. * *parameters - pointer to list of parameter data
  4233. * *status - pointer to store the response status
  4234. * *ret_test_id - pointer to returned test id the parameters were stored
  4235. * *act_load_len - pointer to store the actual load length that was writen
  4236. ******************************************************************************/
  4237. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4238. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4239. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4240. {
  4241. struct pt_core_data *cd = dev_get_drvdata(dev);
  4242. if (protect)
  4243. return pt_pip_load_self_test_param(cd, self_test_id,
  4244. load_offset, load_length, parameters, status, ret_test_id,
  4245. act_load_len);
  4246. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4247. load_length, parameters, status, ret_test_id, act_load_len);
  4248. }
  4249. /*******************************************************************************
  4250. * FUNCTION: pt_pip_calibrate_ext_
  4251. *
  4252. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4253. * for the response
  4254. *
  4255. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4256. *
  4257. * RETURN:
  4258. * 0 = success
  4259. * !0 = failure
  4260. *
  4261. * PARAMETERS:
  4262. * *cd - pointer to core data
  4263. * *cal_data - pointer to extended calibration data structure
  4264. * *status - pointer to where the command response status is stored
  4265. ******************************************************************************/
  4266. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4267. struct pt_cal_ext_data *cal_data, u8 *status)
  4268. {
  4269. int rc = 0;
  4270. int write_length = 4;
  4271. u8 write_buf[4];
  4272. u16 size = 0;
  4273. unsigned short crc = 0;
  4274. struct pt_hid_output hid_output = {
  4275. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4276. .write_length = write_length,
  4277. .write_buf = write_buf,
  4278. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4279. };
  4280. if (cal_data == NULL)
  4281. return -EINVAL;
  4282. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4283. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4284. if (rc)
  4285. return rc;
  4286. if (status)
  4287. *status = cd->response_buf[5];
  4288. /*
  4289. * When doing a calibration on a flashless DUT, save CAL data in
  4290. * the TTDL cache on any successful calibration
  4291. */
  4292. if (*status == 0 && cd->cal_cache_in_host) {
  4293. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4294. __func__);
  4295. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4296. &size, &crc);
  4297. if (rc)
  4298. pt_debug(cd->dev, DL_ERROR,
  4299. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4300. else
  4301. pt_debug(cd->dev, DL_INFO,
  4302. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4303. __func__, crc, size);
  4304. }
  4305. return 0;
  4306. }
  4307. /*******************************************************************************
  4308. * FUNCTION: pt_pip_calibrate_ext
  4309. *
  4310. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4311. * DUT
  4312. *
  4313. * RETURN:
  4314. * 0 = success
  4315. * !0 = failure
  4316. *
  4317. * PARAMETERS:
  4318. * *cd - pointer to core data
  4319. * *cal_data - pointer to extended calibration data structure
  4320. * *status - pointer to where the command response status is stored
  4321. ******************************************************************************/
  4322. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4323. struct pt_cal_ext_data *cal_data, u8 *status)
  4324. {
  4325. int rc;
  4326. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4327. if (rc < 0) {
  4328. pt_debug(cd->dev, DL_ERROR,
  4329. "%s: fail get exclusive ex=%p own=%p\n",
  4330. __func__, cd->exclusive_dev, cd->dev);
  4331. return rc;
  4332. }
  4333. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4334. if (release_exclusive(cd, cd->dev) < 0)
  4335. pt_debug(cd->dev, DL_ERROR,
  4336. "%s: fail to release exclusive\n", __func__);
  4337. return rc;
  4338. }
  4339. /*******************************************************************************
  4340. * FUNCTION: _pt_request_pip_calibrate_ext
  4341. *
  4342. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4343. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4344. *
  4345. * RETURN:
  4346. * 0 = success
  4347. * !0 = failure
  4348. *
  4349. * PARAMETERS:
  4350. * *dev - pointer to device structure
  4351. * protect - flag to call protected or non-protected
  4352. * *cal_data - pointer to extended calibration data structure
  4353. * *status - pointer to where the command response status is stored
  4354. ******************************************************************************/
  4355. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4356. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4357. {
  4358. struct pt_core_data *cd = dev_get_drvdata(dev);
  4359. if (protect)
  4360. return pt_pip_calibrate_ext(cd, cal_data, status);
  4361. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4362. }
  4363. /*******************************************************************************
  4364. * FUNCTION: pt_pip_calibrate_idacs_
  4365. *
  4366. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4367. * for the response
  4368. *
  4369. * RETURN:
  4370. * 0 = success
  4371. * !0 = failure
  4372. *
  4373. * PARAMETERS:
  4374. * *cd - pointer to core data
  4375. * mode - sense mode to calibrate (0-5)
  4376. * *status - pointer to where the command response status is stored
  4377. ******************************************************************************/
  4378. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4379. u8 mode, u8 *status)
  4380. {
  4381. int rc = 0;
  4382. int write_length = 1;
  4383. u8 write_buf[1];
  4384. u8 cmd_offset = 0;
  4385. struct pt_hid_output hid_output = {
  4386. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4387. .write_length = write_length,
  4388. .write_buf = write_buf,
  4389. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4390. };
  4391. write_buf[cmd_offset++] = mode;
  4392. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4393. if (rc)
  4394. return rc;
  4395. *status = cd->response_buf[5];
  4396. if (*status)
  4397. return -EINVAL;
  4398. return 0;
  4399. }
  4400. /*******************************************************************************
  4401. * FUNCTION: pt_pip_calibrate_idacs
  4402. *
  4403. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4404. * access to the DUT
  4405. *
  4406. * RETURN:
  4407. * 0 = success
  4408. * !0 = failure
  4409. *
  4410. * PARAMETERS:
  4411. * *cd - pointer to core data
  4412. * mode - sense mode to calibrate (0-5)
  4413. * *status - pointer to where the command response status is stored
  4414. ******************************************************************************/
  4415. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4416. u8 mode, u8 *status)
  4417. {
  4418. int rc;
  4419. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4420. if (rc < 0) {
  4421. pt_debug(cd->dev, DL_ERROR,
  4422. "%s: fail get exclusive ex=%p own=%p\n",
  4423. __func__, cd->exclusive_dev, cd->dev);
  4424. return rc;
  4425. }
  4426. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4427. if (release_exclusive(cd, cd->dev) < 0)
  4428. pt_debug(cd->dev, DL_ERROR,
  4429. "%s: fail to release exclusive\n", __func__);
  4430. return rc;
  4431. }
  4432. /*******************************************************************************
  4433. * FUNCTION: _pt_request_pip_calibrate_idacs
  4434. *
  4435. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4436. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4437. *
  4438. * RETURN:
  4439. * 0 = success
  4440. * !0 = failure
  4441. *
  4442. * PARAMETERS:
  4443. * *dev - pointer to device structure
  4444. * protect - flag to call protected or non-protected
  4445. * mode - sense mode to calibrate (0-5)
  4446. * *status - pointer to where the command response status is stored
  4447. ******************************************************************************/
  4448. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4449. int protect, u8 mode, u8 *status)
  4450. {
  4451. struct pt_core_data *cd = dev_get_drvdata(dev);
  4452. if (protect)
  4453. return pt_pip_calibrate_idacs(cd, mode, status);
  4454. return pt_pip_calibrate_idacs_(cd, mode, status);
  4455. }
  4456. /*******************************************************************************
  4457. * FUNCTION: pt_hid_output_initialize_baselines_
  4458. *
  4459. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4460. * waiting for the response.
  4461. *
  4462. * RETURN:
  4463. * 0 = success
  4464. * !0 = failure
  4465. *
  4466. * PARAMETERS:
  4467. * *cd - pointer to core data
  4468. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4469. * each or together with a single command.
  4470. * *status - pointer to where the command response status is stored
  4471. ******************************************************************************/
  4472. static int pt_hid_output_initialize_baselines_(
  4473. struct pt_core_data *cd, u8 test_id, u8 *status)
  4474. {
  4475. int rc = 0;
  4476. int write_length = 1;
  4477. u8 write_buf[1];
  4478. u8 cmd_offset = 0;
  4479. struct pt_hid_output hid_output = {
  4480. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4481. .write_length = write_length,
  4482. .write_buf = write_buf,
  4483. };
  4484. write_buf[cmd_offset++] = test_id;
  4485. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4486. if (rc)
  4487. return rc;
  4488. *status = cd->response_buf[5];
  4489. if (*status)
  4490. return -EINVAL;
  4491. return rc;
  4492. }
  4493. /*******************************************************************************
  4494. * FUNCTION: pt_hid_output_initialize_baselines
  4495. *
  4496. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4497. * access to the DUT
  4498. *
  4499. * RETURN:
  4500. * 0 = success
  4501. * !0 = failure
  4502. *
  4503. * PARAMETERS:
  4504. * *cd - pointer to core data
  4505. * test_id - enumerated ID against which to initialize the baseline
  4506. * *status - pointer to where the command response status is stored
  4507. ******************************************************************************/
  4508. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4509. u8 test_id, u8 *status)
  4510. {
  4511. int rc;
  4512. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4513. if (rc < 0) {
  4514. pt_debug(cd->dev, DL_ERROR,
  4515. "%s: fail get exclusive ex=%p own=%p\n",
  4516. __func__, cd->exclusive_dev, cd->dev);
  4517. return rc;
  4518. }
  4519. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4520. if (release_exclusive(cd, cd->dev) < 0)
  4521. pt_debug(cd->dev, DL_ERROR,
  4522. "%s: fail to release exclusive\n", __func__);
  4523. return rc;
  4524. }
  4525. /*******************************************************************************
  4526. * FUNCTION: _pt_request_pip_initialize_baselines
  4527. *
  4528. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4529. * calls to the protected or unprotected call to
  4530. * pt_pip_initialize_baselines
  4531. *
  4532. * RETURN:
  4533. * 0 = success
  4534. * !0 = failure
  4535. *
  4536. * PARAMETERS:
  4537. * *dev - pointer to device structure
  4538. * protect - flag to call protected or non-protected
  4539. * test_id - enumerated ID against which to initialize the baseline
  4540. * *status - pointer to where the command response status is stored
  4541. ******************************************************************************/
  4542. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4543. int protect, u8 test_id, u8 *status)
  4544. {
  4545. struct pt_core_data *cd = dev_get_drvdata(dev);
  4546. if (protect)
  4547. return pt_hid_output_initialize_baselines(cd, test_id,
  4548. status);
  4549. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4550. }
  4551. /*******************************************************************************
  4552. * FUNCTION: pt_hid_output_exec_panel_scan_
  4553. *
  4554. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4555. * the response
  4556. *
  4557. * RETURN:
  4558. * 0 = success
  4559. * !0 = failure
  4560. *
  4561. * PARAMETERS:
  4562. * *cd - pointer to core data
  4563. ******************************************************************************/
  4564. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4565. {
  4566. struct pt_hid_output hid_output = {
  4567. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4568. };
  4569. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4570. }
  4571. /*******************************************************************************
  4572. * FUNCTION: pt_hid_output_exec_panel_scan
  4573. *
  4574. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4575. * access to the DUT.
  4576. *
  4577. * RETURN:
  4578. * 0 = success
  4579. * !0 = failure
  4580. *
  4581. * PARAMETERS:
  4582. * *cd - pointer to core data
  4583. ******************************************************************************/
  4584. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4585. {
  4586. int rc;
  4587. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4588. if (rc < 0) {
  4589. pt_debug(cd->dev, DL_ERROR,
  4590. "%s: fail get exclusive ex=%p own=%p\n",
  4591. __func__, cd->exclusive_dev, cd->dev);
  4592. return rc;
  4593. }
  4594. rc = pt_hid_output_exec_panel_scan_(cd);
  4595. if (release_exclusive(cd, cd->dev) < 0)
  4596. pt_debug(cd->dev, DL_ERROR,
  4597. "%s: fail to release exclusive\n", __func__);
  4598. return rc;
  4599. }
  4600. /*******************************************************************************
  4601. * FUNCTION: pt_pip2_exec_panel_scan_
  4602. *
  4603. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4604. * the response
  4605. *
  4606. * RETURN:
  4607. * 0 = success
  4608. * !0 = failure
  4609. *
  4610. * PARAMETERS:
  4611. * *cd - pointer to core data
  4612. * scan_type - type of panel scan to perform (PIP2 only)
  4613. ******************************************************************************/
  4614. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4615. {
  4616. int rc = 0;
  4617. u8 data[2];
  4618. u8 read_buf[10];
  4619. u16 actual_read_len;
  4620. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4621. __func__, scan_type);
  4622. data[0] = scan_type;
  4623. rc = _pt_request_pip2_send_cmd(cd->dev,
  4624. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4625. data, 1, read_buf, &actual_read_len);
  4626. if (rc) {
  4627. pt_debug(cd->dev, DL_ERROR,
  4628. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4629. __func__, scan_type, rc);
  4630. }
  4631. return rc;
  4632. }
  4633. /*******************************************************************************
  4634. * FUNCTION: pt_pip2_exec_panel_scan
  4635. *
  4636. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4637. * access to the DUT.
  4638. *
  4639. * RETURN:
  4640. * 0 = success
  4641. * !0 = failure
  4642. *
  4643. * PARAMETERS:
  4644. * *cd - pointer to core data
  4645. * scan_type - type of panel scan to perform (PIP2 only)
  4646. ******************************************************************************/
  4647. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4648. {
  4649. int rc;
  4650. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4651. if (rc < 0) {
  4652. pt_debug(cd->dev, DL_ERROR,
  4653. "%s: fail get exclusive ex=%p own=%p\n",
  4654. __func__, cd->exclusive_dev, cd->dev);
  4655. return rc;
  4656. }
  4657. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4658. if (release_exclusive(cd, cd->dev) < 0)
  4659. pt_debug(cd->dev, DL_ERROR,
  4660. "%s: fail to release exclusive\n", __func__);
  4661. return rc;
  4662. }
  4663. /*******************************************************************************
  4664. * FUNCTION: _pt_request_pip_exec_panel_scan
  4665. *
  4666. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4667. * calls to the protected or unprotected call to
  4668. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4669. *
  4670. * RETURN:
  4671. * 0 = success
  4672. * !0 = failure
  4673. *
  4674. * PARAMETERS:
  4675. * *dev - pointer to device structure
  4676. * protect - flag to call protected or non-protected
  4677. * scan_type - type of panel scan to perform (PIP2 only)
  4678. ******************************************************************************/
  4679. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4680. int protect, u8 scan_type)
  4681. {
  4682. struct pt_core_data *cd = dev_get_drvdata(dev);
  4683. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4684. if (protect)
  4685. return pt_pip2_exec_panel_scan(cd, scan_type);
  4686. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4687. }
  4688. if (protect)
  4689. return pt_hid_output_exec_panel_scan(cd);
  4690. return pt_hid_output_exec_panel_scan_(cd);
  4691. }
  4692. /*******************************************************************************
  4693. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4694. *
  4695. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4696. * to retrieve the specified data type for a the last successful Execute
  4697. * Panel Scan command.
  4698. *
  4699. * RETURN:
  4700. * 0 = success
  4701. * !0 = failure
  4702. *
  4703. * PARAMETERS:
  4704. * *dev - pointer to device structure
  4705. * protect - flag to call protected or non-protected
  4706. * read_offset - read pointer offset
  4707. * read_count - length of data to read
  4708. * data_id - enumerated test ID to read selftest results from
  4709. * *response - pointer to store the read response status
  4710. * *config - pointer to store config data
  4711. * *actual_read_len - pointer to store data length actually read
  4712. * *read_buf - pointer to the read buffer
  4713. ******************************************************************************/
  4714. static int pt_hid_output_retrieve_panel_scan_(
  4715. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4716. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4717. u8 *read_buf)
  4718. {
  4719. int status;
  4720. u8 read_data_id;
  4721. int rc = 0;
  4722. int write_length = 5;
  4723. u8 write_buf[5];
  4724. u8 cmd_offset = 0;
  4725. u8 data_elem_size;
  4726. int size;
  4727. int data_size;
  4728. struct pt_hid_output hid_output = {
  4729. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4730. .write_length = write_length,
  4731. .write_buf = write_buf,
  4732. };
  4733. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4734. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4735. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4736. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4737. write_buf[cmd_offset++] = data_id;
  4738. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4739. if (rc)
  4740. return rc;
  4741. status = cd->response_buf[5];
  4742. if (status)
  4743. return -EINVAL;
  4744. read_data_id = cd->response_buf[6];
  4745. if (read_data_id != data_id)
  4746. return -EPROTO;
  4747. size = get_unaligned_le16(&cd->response_buf[0]);
  4748. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4749. *config = cd->response_buf[9];
  4750. data_elem_size = *config & 0x07;
  4751. data_size = *actual_read_len * data_elem_size;
  4752. if (read_buf)
  4753. memcpy(read_buf, &cd->response_buf[10], data_size);
  4754. if (response)
  4755. memcpy(response, cd->response_buf, size);
  4756. return rc;
  4757. }
  4758. /*******************************************************************************
  4759. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4760. *
  4761. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4762. * access to the DUT.
  4763. *
  4764. * RETURN:
  4765. * 0 = success
  4766. * !0 = failure
  4767. *
  4768. * PARAMETERS:
  4769. * *dev - pointer to device structure
  4770. * protect - flag to call protected or non-protected
  4771. * read_offset - read pointer offset
  4772. * read_count - length of data to read
  4773. * data_id - enumerated test ID to read selftest results from
  4774. * *response - pointer to store the read response status
  4775. * *config - pointer to store config data
  4776. * *actual_read_len - pointer to store data length actually read
  4777. * *read_buf - pointer to the read buffer
  4778. ******************************************************************************/
  4779. static int pt_hid_output_retrieve_panel_scan(
  4780. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4781. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4782. u8 *read_buf)
  4783. {
  4784. int rc;
  4785. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4786. if (rc < 0) {
  4787. pt_debug(cd->dev, DL_ERROR,
  4788. "%s: fail get exclusive ex=%p own=%p\n",
  4789. __func__, cd->exclusive_dev, cd->dev);
  4790. return rc;
  4791. }
  4792. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4793. read_count, data_id, response, config,
  4794. actual_read_len, read_buf);
  4795. if (release_exclusive(cd, cd->dev) < 0)
  4796. pt_debug(cd->dev, DL_ERROR,
  4797. "%s: fail to release exclusive\n", __func__);
  4798. return rc;
  4799. }
  4800. /*******************************************************************************
  4801. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4802. *
  4803. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4804. * calls to the protected or unprotected call to
  4805. * pt_hid_output_retrieve_panel_scan
  4806. *
  4807. * RETURN:
  4808. * 0 = success
  4809. * !0 = failure
  4810. *
  4811. * PARAMETERS:
  4812. * *dev - pointer to device structure
  4813. * protect - flag to call protected or non-protected
  4814. * read_offset - read pointer offset
  4815. * read_count - length of data to read
  4816. * data_id - enumerated test ID to read selftest results from
  4817. * *response - pointer to store the read response status
  4818. * *config - pointer to store config data
  4819. * *actual_read_len - pointer to store data length actually read
  4820. * *read_buf - pointer to the read buffer
  4821. ******************************************************************************/
  4822. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4823. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4824. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4825. {
  4826. struct pt_core_data *cd = dev_get_drvdata(dev);
  4827. if (protect)
  4828. return pt_hid_output_retrieve_panel_scan(cd,
  4829. read_offset, read_count, data_id, response,
  4830. config, actual_read_len, read_buf);
  4831. return pt_hid_output_retrieve_panel_scan_(cd,
  4832. read_offset, read_count, data_id, response,
  4833. config, actual_read_len, read_buf);
  4834. }
  4835. /*******************************************************************************
  4836. * FUNCTION: _pt_request_pip_user_cmd
  4837. *
  4838. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4839. * calls to the protected or unprotected call to
  4840. * pt_hid_output_user_cmd
  4841. *
  4842. * RETURN:
  4843. * 0 = success
  4844. * !0 = failure
  4845. *
  4846. * PARAMETERS:
  4847. * *dev - pointer to device structure
  4848. * protect - flag to call protected or non-protected
  4849. * read_len - length of data to read
  4850. * *read_buf - pointer to store read data
  4851. * write_len - length of data to write
  4852. * *write_buf - pointer to buffer to write
  4853. * *actual_read_len - pointer to store data length actually read
  4854. ******************************************************************************/
  4855. static int _pt_request_pip_user_cmd(struct device *dev,
  4856. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4857. u8 *write_buf, u16 *actual_read_len)
  4858. {
  4859. struct pt_core_data *cd = dev_get_drvdata(dev);
  4860. if (protect)
  4861. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4862. write_len, write_buf, actual_read_len);
  4863. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4864. write_len, write_buf, actual_read_len);
  4865. }
  4866. /*******************************************************************************
  4867. * FUNCTION: pt_hid_output_bl_get_information_
  4868. *
  4869. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4870. * DUT to retrieve bootloader version and chip identification information.
  4871. *
  4872. * RETURN:
  4873. * 0 = success
  4874. * !0 = failure
  4875. *
  4876. * PARAMETERS:
  4877. * *cd - pointer to core data
  4878. * *return_data - pointer to store the return data
  4879. *****************************************************************************/
  4880. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4881. u8 *return_data)
  4882. {
  4883. int rc;
  4884. int data_len;
  4885. struct pt_hid_output hid_output = {
  4886. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4887. };
  4888. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4889. if (rc)
  4890. return rc;
  4891. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4892. if (!data_len)
  4893. return -EPROTO;
  4894. memcpy(return_data, &cd->response_buf[8], data_len);
  4895. return 0;
  4896. }
  4897. /*******************************************************************************
  4898. * FUNCTION: pt_hid_output_bl_get_information
  4899. *
  4900. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4901. * access to the DUT.
  4902. *
  4903. * RETURN:
  4904. * 0 = success
  4905. * !0 = failure
  4906. *
  4907. * PARAMETERS:
  4908. * *cd - pointer to core data
  4909. * *return_data - pointer to store the return data
  4910. ******************************************************************************/
  4911. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4912. u8 *return_data)
  4913. {
  4914. int rc;
  4915. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4916. if (rc < 0) {
  4917. pt_debug(cd->dev, DL_ERROR,
  4918. "%s: fail get exclusive ex=%p own=%p\n",
  4919. __func__, cd->exclusive_dev, cd->dev);
  4920. return rc;
  4921. }
  4922. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4923. if (release_exclusive(cd, cd->dev) < 0)
  4924. pt_debug(cd->dev, DL_ERROR,
  4925. "%s: fail to release exclusive\n", __func__);
  4926. return rc;
  4927. }
  4928. /*******************************************************************************
  4929. * FUNCTION: _pt_request_pip_bl_get_information
  4930. *
  4931. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4932. * calls to the protected or unprotected call to
  4933. * pt_hid_output_bl_get_information
  4934. *
  4935. * RETURN:
  4936. * 0 = success
  4937. * !0 = failure
  4938. *
  4939. * PARAMETERS:
  4940. * *dev - pointer to device structure
  4941. * protect - flag to call protected or non-protected
  4942. * *return_data - pointer to store bl data
  4943. ******************************************************************************/
  4944. static int _pt_request_pip_bl_get_information(struct device *dev,
  4945. int protect, u8 *return_data)
  4946. {
  4947. struct pt_core_data *cd = dev_get_drvdata(dev);
  4948. if (protect)
  4949. return pt_hid_output_bl_get_information(cd, return_data);
  4950. return pt_hid_output_bl_get_information_(cd, return_data);
  4951. }
  4952. /*******************************************************************************
  4953. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4954. *
  4955. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4956. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4957. * and Design Data block in flash and enables the host to execute the Program
  4958. * and Verify Row command to bootload the application image and data.
  4959. *
  4960. * RETURN:
  4961. * 0 = success
  4962. * !0 = failure
  4963. *
  4964. * PARAMETERS:
  4965. * *cd - pointer to core data
  4966. * protect - flag to call protected or non-protected
  4967. * key_size - size of key
  4968. * *key_buf - pointer to key data to allow operation
  4969. * row_size - size of the meta data row
  4970. * *metadata_row_buf - pointer to meta data to write
  4971. ******************************************************************************/
  4972. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4973. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4974. {
  4975. u16 write_length = key_size + row_size;
  4976. u8 *write_buf;
  4977. int rc = 0;
  4978. struct pt_hid_output hid_output = {
  4979. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4980. .write_length = write_length,
  4981. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4982. };
  4983. write_buf = kzalloc(write_length, GFP_KERNEL);
  4984. if (!write_buf)
  4985. return -ENOMEM;
  4986. hid_output.write_buf = write_buf;
  4987. if (key_size)
  4988. memcpy(write_buf, key_buf, key_size);
  4989. if (row_size)
  4990. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4991. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4992. kfree(write_buf);
  4993. return rc;
  4994. }
  4995. /*******************************************************************************
  4996. * FUNCTION: pt_hid_output_bl_initiate_bl
  4997. *
  4998. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4999. * access to the DUT.
  5000. *
  5001. * RETURN:
  5002. * 0 = success
  5003. * !0 = failure
  5004. *
  5005. * PARAMETERS:
  5006. * *cd - pointer to core data
  5007. * key_size - size of key
  5008. * *key_buf - pointer to key data to allow operation
  5009. * row_size - size of the meta data row
  5010. * *metadata_row_buf - pointer to meta data to write
  5011. ******************************************************************************/
  5012. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5013. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5014. {
  5015. int rc;
  5016. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5017. if (rc < 0) {
  5018. pt_debug(cd->dev, DL_ERROR,
  5019. "%s: fail get exclusive ex=%p own=%p\n",
  5020. __func__, cd->exclusive_dev, cd->dev);
  5021. return rc;
  5022. }
  5023. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5024. row_size, metadata_row_buf);
  5025. if (release_exclusive(cd, cd->dev) < 0)
  5026. pt_debug(cd->dev, DL_ERROR,
  5027. "%s: fail to release exclusive\n", __func__);
  5028. return rc;
  5029. }
  5030. /*******************************************************************************
  5031. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5032. *
  5033. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5034. * calls to the protected or unprotected call to
  5035. * pt_hid_output_bl_initiate_bl
  5036. *
  5037. * RETURN:
  5038. * 0 = success
  5039. * !0 = failure
  5040. *
  5041. * PARAMETERS:
  5042. * *dev - pointer to device structure
  5043. * protect - flag to call protected or non-protected
  5044. * key_size - size of key
  5045. * *key_buf - pointer to key data to allow operation
  5046. * row_size - size of the meta data row
  5047. * *metadata_row_buf - pointer to meta data to write
  5048. ******************************************************************************/
  5049. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5050. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5051. u8 *metadata_row_buf)
  5052. {
  5053. struct pt_core_data *cd = dev_get_drvdata(dev);
  5054. if (protect)
  5055. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5056. row_size, metadata_row_buf);
  5057. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5058. row_size, metadata_row_buf);
  5059. }
  5060. /*******************************************************************************
  5061. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5062. *
  5063. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5064. * and program a 128-byte row into the flash, and then verifies written data.
  5065. *
  5066. * RETURN:
  5067. * 0 = success
  5068. * !0 = failure
  5069. *
  5070. * PARAMETERS:
  5071. * *cd - pointer to core data
  5072. * data_len - length of data_buf
  5073. * *data_buf - firmware image to program
  5074. ******************************************************************************/
  5075. static int pt_hid_output_bl_program_and_verify_(
  5076. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5077. {
  5078. struct pt_hid_output hid_output = {
  5079. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5080. .write_length = data_len,
  5081. .write_buf = data_buf,
  5082. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5083. };
  5084. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5085. }
  5086. /*******************************************************************************
  5087. * FUNCTION: pt_hid_output_bl_program_and_verify
  5088. *
  5089. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5090. * access to the DUT.
  5091. *
  5092. * RETURN:
  5093. * 0 = success
  5094. * !0 = failure
  5095. *
  5096. * PARAMETERS:
  5097. * *cd - pointer to core data
  5098. * data_len - length of data_buf
  5099. * *data_buf - firmware image to program
  5100. ******************************************************************************/
  5101. static int pt_hid_output_bl_program_and_verify(
  5102. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5103. {
  5104. int rc;
  5105. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5106. if (rc < 0) {
  5107. pt_debug(cd->dev, DL_ERROR,
  5108. "%s: fail get exclusive ex=%p own=%p\n",
  5109. __func__, cd->exclusive_dev, cd->dev);
  5110. return rc;
  5111. }
  5112. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5113. if (release_exclusive(cd, cd->dev) < 0)
  5114. pt_debug(cd->dev, DL_ERROR,
  5115. "%s: fail to release exclusive\n", __func__);
  5116. return rc;
  5117. }
  5118. /*******************************************************************************
  5119. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5120. *
  5121. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5122. * to request to have the BL program and verify a FW image
  5123. *
  5124. * RETURN:
  5125. * 0 = success
  5126. * !0 = failure
  5127. *
  5128. * PARAMETERS:
  5129. * *dev - pointer to device structure
  5130. * protect - boolean to determine to call the protected function
  5131. * data_len - length of data_buf
  5132. * *data_buf - firmware image to program
  5133. ******************************************************************************/
  5134. static int _pt_request_pip_bl_program_and_verify(
  5135. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5136. {
  5137. struct pt_core_data *cd = dev_get_drvdata(dev);
  5138. if (protect)
  5139. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5140. data_buf);
  5141. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5142. data_buf);
  5143. }
  5144. /*******************************************************************************
  5145. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5146. *
  5147. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5148. * perform a full verification of the application integrity by calculating the
  5149. * CRC of the image in flash and compare it to the expected CRC stored in the
  5150. * Metadata row.
  5151. *
  5152. * RETURN:
  5153. * 0 = success
  5154. * !0 = failure
  5155. *
  5156. * PARAMETERS:
  5157. * *cd - pointer to core data
  5158. * *result - pointer to store result
  5159. ******************************************************************************/
  5160. static int pt_hid_output_bl_verify_app_integrity_(
  5161. struct pt_core_data *cd, u8 *result)
  5162. {
  5163. int rc;
  5164. struct pt_hid_output hid_output = {
  5165. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5166. };
  5167. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5168. if (rc) {
  5169. *result = 0;
  5170. return rc;
  5171. }
  5172. *result = cd->response_buf[8];
  5173. return 0;
  5174. }
  5175. /*******************************************************************************
  5176. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5177. *
  5178. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5179. * exclusive access to the DUT.
  5180. *
  5181. * RETURN:
  5182. * 0 = success
  5183. * !0 = failure
  5184. *
  5185. * PARAMETERS:
  5186. * *cd - pointer to core data
  5187. * *result - pointer to store result
  5188. ******************************************************************************/
  5189. static int pt_hid_output_bl_verify_app_integrity(
  5190. struct pt_core_data *cd, u8 *result)
  5191. {
  5192. int rc;
  5193. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5194. if (rc < 0) {
  5195. pt_debug(cd->dev, DL_ERROR,
  5196. "%s: fail get exclusive ex=%p own=%p\n",
  5197. __func__, cd->exclusive_dev, cd->dev);
  5198. return rc;
  5199. }
  5200. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5201. if (release_exclusive(cd, cd->dev) < 0)
  5202. pt_debug(cd->dev, DL_ERROR,
  5203. "%s: fail to release exclusive\n", __func__);
  5204. return rc;
  5205. }
  5206. /*******************************************************************************
  5207. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5208. *
  5209. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5210. * to request to have the BL verify the application integrity (PIP1.x only)
  5211. *
  5212. * RETURN:
  5213. * 0 = success
  5214. * !0 = failure
  5215. *
  5216. * PARAMETERS:
  5217. * *dev - pointer to device structure
  5218. * protect - boolean to determine to call the protected function
  5219. * *result - pointer to store result
  5220. ******************************************************************************/
  5221. static int _pt_request_pip_bl_verify_app_integrity(
  5222. struct device *dev, int protect, u8 *result)
  5223. {
  5224. struct pt_core_data *cd = dev_get_drvdata(dev);
  5225. if (protect)
  5226. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5227. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5228. }
  5229. /*******************************************************************************
  5230. * FUNCTION: pt_hid_output_bl_launch_app_
  5231. *
  5232. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5233. * application from bootloader (PIP1.x only).
  5234. *
  5235. * RETURN:
  5236. * 0 = success
  5237. * !0 = failure
  5238. *
  5239. * PARAMETERS:
  5240. * *cd - pointer to core data
  5241. ******************************************************************************/
  5242. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5243. {
  5244. struct pt_hid_output hid_output = {
  5245. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5246. .reset_expected = 1,
  5247. };
  5248. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5249. }
  5250. /*******************************************************************************
  5251. * FUNCTION: pt_hid_output_bl_launch_app
  5252. *
  5253. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5254. * to the DUT.
  5255. *
  5256. * RETURN:
  5257. * 0 = success
  5258. * !0 = failure
  5259. *
  5260. * PARAMETERS:
  5261. * *cd - pointer to core data
  5262. ******************************************************************************/
  5263. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5264. {
  5265. int rc;
  5266. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5267. if (rc < 0) {
  5268. pt_debug(cd->dev, DL_ERROR,
  5269. "%s: fail get exclusive ex=%p own=%p\n",
  5270. __func__, cd->exclusive_dev, cd->dev);
  5271. return rc;
  5272. }
  5273. rc = pt_hid_output_bl_launch_app_(cd);
  5274. if (release_exclusive(cd, cd->dev) < 0)
  5275. pt_debug(cd->dev, DL_ERROR,
  5276. "%s: fail to release exclusive\n", __func__);
  5277. return rc;
  5278. }
  5279. /*******************************************************************************
  5280. * FUNCTION: _pt_request_pip_launch_app
  5281. *
  5282. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5283. * to request to have the BL launch the application. (PIP1.x only)
  5284. *
  5285. * RETURN:
  5286. * 0 = success
  5287. * !0 = failure
  5288. *
  5289. * PARAMETERS:
  5290. * *dev - pointer to device structure
  5291. * protect - boolean to determine to call the protected function
  5292. ******************************************************************************/
  5293. static int _pt_request_pip_launch_app(struct device *dev,
  5294. int protect)
  5295. {
  5296. struct pt_core_data *cd = dev_get_drvdata(dev);
  5297. if (protect)
  5298. return pt_hid_output_bl_launch_app(cd);
  5299. return pt_hid_output_bl_launch_app_(cd);
  5300. }
  5301. /*******************************************************************************
  5302. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5303. *
  5304. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5305. * value store in the System Information.
  5306. *
  5307. * RETURN:
  5308. * 0 = success
  5309. * !0 = failure
  5310. *
  5311. * PARAMETERS:
  5312. * *cd - pointer to core data
  5313. * *panel_id - pointer to where the panel ID will be stored
  5314. ******************************************************************************/
  5315. static int pt_hid_output_bl_get_panel_id_(
  5316. struct pt_core_data *cd, u8 *panel_id)
  5317. {
  5318. int rc;
  5319. struct pt_hid_output hid_output = {
  5320. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5321. };
  5322. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5323. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5324. pt_debug(cd->dev, DL_ERROR,
  5325. "%s: Get Panel ID command not supported\n",
  5326. __func__);
  5327. *panel_id = PANEL_ID_NOT_ENABLED;
  5328. return 0;
  5329. } else if (rc < 0) {
  5330. pt_debug(cd->dev, DL_ERROR,
  5331. "%s: Error on Get Panel ID command\n", __func__);
  5332. return rc;
  5333. }
  5334. *panel_id = cd->response_buf[8];
  5335. return 0;
  5336. }
  5337. /*******************************************************************************
  5338. * FUNCTION: pt_hid_output_bl_get_panel_id
  5339. *
  5340. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5341. * to the DUT.
  5342. *
  5343. * RETURN:
  5344. * 0 = success
  5345. * !0 = failure
  5346. *
  5347. * PARAMETERS:
  5348. * *cd - pointer to core data
  5349. * *panel_id - pointer to where the panel ID will be stored
  5350. ******************************************************************************/
  5351. static int pt_hid_output_bl_get_panel_id(
  5352. struct pt_core_data *cd, u8 *panel_id)
  5353. {
  5354. int rc;
  5355. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5356. if (rc < 0) {
  5357. pt_debug(cd->dev, DL_ERROR,
  5358. "%s: fail get exclusive ex=%p own=%p\n",
  5359. __func__, cd->exclusive_dev, cd->dev);
  5360. return rc;
  5361. }
  5362. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5363. if (release_exclusive(cd, cd->dev) < 0)
  5364. pt_debug(cd->dev, DL_ERROR,
  5365. "%s: fail to release exclusive\n", __func__);
  5366. return rc;
  5367. }
  5368. /*******************************************************************************
  5369. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5370. *
  5371. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5372. * to have the BL retrieve the panel ID
  5373. *
  5374. * RETURN:
  5375. * 0 = success
  5376. * !0 = failure
  5377. *
  5378. * PARAMETERS:
  5379. * *dev - pointer to device structure
  5380. * protect - flag to run in protected mode
  5381. * *panel_id - pointer to where the panel ID will be stored
  5382. ******************************************************************************/
  5383. static int _pt_request_pip_bl_get_panel_id(
  5384. struct device *dev, int protect, u8 *panel_id)
  5385. {
  5386. struct pt_core_data *cd = dev_get_drvdata(dev);
  5387. if (protect)
  5388. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5389. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5390. }
  5391. /*******************************************************************************
  5392. * FUNCTION: pt_pip2_get_mode_sysmode_
  5393. *
  5394. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5395. * PIP2 STATUS command.
  5396. *
  5397. * RETURN:
  5398. * 0 = success
  5399. * !0 = failure
  5400. *
  5401. * PARAMETERS:
  5402. * *cd - pointer to core data structure
  5403. * *mode - pointer to store the retrieved mode
  5404. * *sys_mode - pointer to store the FW system mode
  5405. ******************************************************************************/
  5406. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5407. u8 *mode, u8 *sys_mode)
  5408. {
  5409. u16 actual_read_len;
  5410. u8 read_buf[12];
  5411. u8 status, boot;
  5412. int rc = 0;
  5413. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5414. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5415. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5416. __func__, rc);
  5417. if (!rc) {
  5418. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5419. "PIP2 STATUS");
  5420. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5421. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5422. if (sys_mode) {
  5423. if (status == PIP2_RSP_ERR_NONE &&
  5424. boot == PIP2_STATUS_APP_EXEC)
  5425. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5426. else
  5427. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5428. }
  5429. if (mode) {
  5430. if (status == PIP2_RSP_ERR_NONE &&
  5431. boot == PIP2_STATUS_BOOT_EXEC)
  5432. *mode = PT_MODE_BOOTLOADER;
  5433. else if (status == PIP2_RSP_ERR_NONE &&
  5434. boot == PIP2_STATUS_APP_EXEC)
  5435. *mode = PT_MODE_OPERATIONAL;
  5436. else
  5437. *mode = PT_MODE_UNKNOWN;
  5438. }
  5439. } else {
  5440. if (mode)
  5441. *mode = PT_MODE_UNKNOWN;
  5442. if (sys_mode)
  5443. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5444. pt_debug(cd->dev, DL_WARN,
  5445. "%s: Mode and sys_mode could not be determined\n",
  5446. __func__);
  5447. }
  5448. return rc;
  5449. }
  5450. /*******************************************************************************
  5451. * FUNCTION: pt_pip2_get_mode_sysmode
  5452. *
  5453. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5454. * access to the DUT.
  5455. *
  5456. * RETURN:
  5457. * 0 = success
  5458. * !0 = failure
  5459. *
  5460. * PARAMETERS:
  5461. * *cd - pointer to core data structure
  5462. * *mode - pointer to store the retrieved mode
  5463. * *sys_mode - pointer to store the FW system mode
  5464. ******************************************************************************/
  5465. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5466. u8 *mode, u8 *sys_mode)
  5467. {
  5468. int rc;
  5469. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5470. if (rc < 0) {
  5471. pt_debug(cd->dev, DL_ERROR,
  5472. "%s: fail get exclusive ex=%p own=%p\n",
  5473. __func__, cd->exclusive_dev, cd->dev);
  5474. return rc;
  5475. }
  5476. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5477. if (release_exclusive(cd, cd->dev) < 0)
  5478. pt_debug(cd->dev, DL_ERROR,
  5479. "%s: fail to release exclusive\n", __func__);
  5480. return rc;
  5481. }
  5482. /*******************************************************************************
  5483. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5484. *
  5485. * SUMMARY: Function pointer included in core_commands struct for external
  5486. * calls to the protected or unprotected call to
  5487. * pt_pip2_get_mode_sysmode
  5488. *
  5489. * RETURN:
  5490. * 0 = success
  5491. * !0 = failure
  5492. *
  5493. * PARAMETERS:
  5494. * *dev - pointer to device structure
  5495. * *mode - pointer to store the retrieved mode
  5496. * *sys_mode - pointer to store the FW system mode
  5497. ******************************************************************************/
  5498. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5499. int protect, u8 *mode, u8 *sys_mode)
  5500. {
  5501. struct pt_core_data *cd = dev_get_drvdata(dev);
  5502. if (protect)
  5503. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5504. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5505. }
  5506. /*******************************************************************************
  5507. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5508. *
  5509. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5510. * mode only the following PIP commands will be serviced, any other PIP
  5511. * command the FW will respond with an "Invalid PIP Command" response.
  5512. * - Get HID Descriptor (Register 0x0001, no command ID)
  5513. * - Reset (Register 0x0005, RESET HID request)
  5514. * - Ping (Register 0x0004, Command ID 0x00
  5515. * - Get System Information (Register 0x0004, Command ID 0x02)
  5516. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5517. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5518. * This function will loop on the results of the STATUS command until
  5519. * the FW reports it is out of BOOT mode.
  5520. *
  5521. * NOTE:
  5522. * - This function will update cd->fw_system_mode
  5523. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5524. *
  5525. * RETURN:
  5526. * 0 = success
  5527. * !0 = failure
  5528. *
  5529. * PARAMETERS:
  5530. * *cd - pointer to core data
  5531. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5532. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5533. ******************************************************************************/
  5534. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5535. int *actual_wait)
  5536. {
  5537. int loop = 0;
  5538. u8 sys_mode = cd->fw_system_mode;
  5539. u8 pause = 10; /* in ms */
  5540. int rc = 0;
  5541. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5542. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5543. /*
  5544. * For PIP <1.11, no support for polling wait so do a hard
  5545. * coded wait and assume the FW is out of BOOT. Added 1 to
  5546. * timeout to make it clear in kmsg if non polling was done.
  5547. */
  5548. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5549. pt_debug(cd->dev, DL_ERROR,
  5550. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5551. __func__,
  5552. cd->sysinfo.ttdata.pip_ver_major,
  5553. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5554. msleep(*actual_wait);
  5555. sys_mode = FW_SYS_MODE_SCANNING;
  5556. }
  5557. if (sys_mode == FW_SYS_MODE_BOOT) {
  5558. while (!rc && loop <= max_loop &&
  5559. (sys_mode == FW_SYS_MODE_BOOT)) {
  5560. loop++;
  5561. usleep_range(9000, pause * 1000);
  5562. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5563. pt_debug(cd->dev, DL_DEBUG,
  5564. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5565. __func__, loop * pause, sys_mode);
  5566. }
  5567. *actual_wait = (int)(loop * pause);
  5568. pt_debug(cd->dev, DL_WARN,
  5569. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5570. __func__, *actual_wait, sys_mode);
  5571. if (rc)
  5572. sys_mode = FW_SYS_MODE_UNDEFINED;
  5573. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5574. sys_mode == FW_SYS_MODE_UNDEFINED)
  5575. rc = -EBUSY;
  5576. }
  5577. mutex_lock(&cd->system_lock);
  5578. cd->fw_system_mode = sys_mode;
  5579. mutex_unlock(&cd->system_lock);
  5580. return rc;
  5581. }
  5582. /*******************************************************************************
  5583. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5584. *
  5585. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5586. * access to the DUT.
  5587. *
  5588. * RETURN:
  5589. * 0 = success
  5590. * !0 = failure
  5591. *
  5592. * PARAMETERS:
  5593. * *cd - pointer to core data
  5594. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5595. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5596. ******************************************************************************/
  5597. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5598. int *actual_wait)
  5599. {
  5600. int rc;
  5601. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5602. if (rc < 0) {
  5603. pt_debug(cd->dev, DL_ERROR,
  5604. "%s: fail get exclusive ex=%p own=%p\n",
  5605. __func__, cd->exclusive_dev, cd->dev);
  5606. return rc;
  5607. }
  5608. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5609. if (release_exclusive(cd, cd->dev) < 0)
  5610. pt_debug(cd->dev, DL_ERROR,
  5611. "%s: fail to release exclusive\n", __func__);
  5612. return rc;
  5613. }
  5614. /*******************************************************************************
  5615. * FUNCTION: _pt_get_fw_sys_mode
  5616. *
  5617. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5618. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5619. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5620. * information but any PIP command above 0x1F requires scanning to be disabled
  5621. * before it will be operational. If scanning was not disabled before sending
  5622. * these PIP commands the FW will respond with a 6 byte error response. So to
  5623. * safely determine the scanning state, a PIP message that will not affect the
  5624. * operation of the FW was chosen:
  5625. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5626. * received scanning is active.
  5627. *
  5628. * RETURN:
  5629. * 0 = success
  5630. * !0 = failure
  5631. *
  5632. * PARAMETERS:
  5633. * *cd - pointer to core data
  5634. * *sys_mode - pointer to FW System mode
  5635. * *mode - pointer to mode (BL/FW)
  5636. ******************************************************************************/
  5637. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5638. {
  5639. int write_length = 1;
  5640. int report_length;
  5641. int rc = 0;
  5642. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5643. u8 tmp_mode = PT_MODE_UNKNOWN;
  5644. u8 param[1] = { PT_TCH_PARM_EBID };
  5645. struct pt_hid_output hid_output = {
  5646. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5647. .write_length = write_length,
  5648. .write_buf = param,
  5649. .novalidate = true,
  5650. };
  5651. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5652. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5653. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5654. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5655. __func__, tmp_sys_mode, tmp_mode);
  5656. if (!rc) {
  5657. if (tmp_mode != PT_MODE_OPERATIONAL)
  5658. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5659. }
  5660. goto exit;
  5661. }
  5662. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5663. if (cd->mode != PT_MODE_OPERATIONAL) {
  5664. tmp_mode = cd->mode;
  5665. goto exit;
  5666. }
  5667. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5668. if (rc)
  5669. goto exit;
  5670. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5671. if ((report_length == 0x06) &&
  5672. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5673. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5674. tmp_mode = PIP2_STATUS_APP_EXEC;
  5675. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5676. } else if ((report_length == 0x0A) &&
  5677. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5678. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5679. tmp_mode = PIP2_STATUS_APP_EXEC;
  5680. tmp_sys_mode = FW_SYS_MODE_TEST;
  5681. }
  5682. exit:
  5683. if (mode)
  5684. *mode = tmp_mode;
  5685. if (sys_mode)
  5686. *sys_mode = tmp_sys_mode;
  5687. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5688. __func__, tmp_mode, tmp_sys_mode);
  5689. return rc;
  5690. }
  5691. /*******************************************************************************
  5692. * FUNCTION: pt_get_fw_sys_mode
  5693. *
  5694. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5695. * is active or not.
  5696. *
  5697. * RETURN:
  5698. * 0 = success
  5699. * !0 = failure
  5700. *
  5701. * PARAMETERS:
  5702. * *cd - pointer to core data
  5703. * *sys_mode - pointer to fw system mode
  5704. * *mode - pointer to mode
  5705. ******************************************************************************/
  5706. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5707. {
  5708. int rc;
  5709. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5710. if (rc < 0) {
  5711. pt_debug(cd->dev, DL_ERROR,
  5712. "%s: fail get exclusive ex=%p own=%p\n",
  5713. __func__, cd->exclusive_dev, cd->dev);
  5714. return rc;
  5715. }
  5716. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5717. if (release_exclusive(cd, cd->dev) < 0)
  5718. pt_debug(cd->dev, DL_ERROR,
  5719. "%s: fail to release exclusive\n", __func__);
  5720. return rc;
  5721. }
  5722. /*******************************************************************************
  5723. * FUNCTION: _pt_request_get_fw_sys_mode
  5724. *
  5725. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5726. * to request to get scan state
  5727. *
  5728. * RETURN:
  5729. * 0 = success
  5730. * !0 = failure
  5731. *
  5732. * PARAMETERS:
  5733. * *dev - pointer to device structure
  5734. * protect - flag to call protected or non-protected
  5735. * *sys_mode - pointer to FW system mode
  5736. * *mode - pointer to mode
  5737. ******************************************************************************/
  5738. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5739. u8 *sys_mode, u8 *mode)
  5740. {
  5741. struct pt_core_data *cd = dev_get_drvdata(dev);
  5742. if (protect)
  5743. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5744. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5745. }
  5746. /* Default hid descriptor to provide basic register map */
  5747. const struct pt_hid_desc hid_desc_default = {
  5748. 230, /* hid_desc_len */
  5749. HID_APP_REPORT_ID, /* packet_id */
  5750. 0x00, /* reserved_byte */
  5751. 0x0100, /* bcd_version */
  5752. 0x00EC, /* report_desc_len */
  5753. 0x0002, /* report_desc_register */
  5754. 0x0003, /* input_register */
  5755. 0x00FE, /* max_input_len */
  5756. 0x0004, /* output_register */
  5757. 0x00FE, /* max_output_len */
  5758. 0x0005, /* command_register */
  5759. 0x0006, /* data_register */
  5760. 0x04B4, /* vendor_id */
  5761. 0xC101, /* product_id */
  5762. 0x0100, /* version_id */
  5763. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5764. };
  5765. /*******************************************************************************
  5766. * FUNCTION: pt_init_hid_descriptor
  5767. *
  5768. * SUMMARY: Setup default values for HID descriptor structure
  5769. *
  5770. *
  5771. * PARAMETERS:
  5772. * *desc - pointer to the HID descriptor data read back from DUT
  5773. ******************************************************************************/
  5774. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5775. {
  5776. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5777. }
  5778. /*******************************************************************************
  5779. * FUNCTION: pt_get_hid_descriptor_
  5780. *
  5781. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5782. * into the HID descriptor structure
  5783. *
  5784. * RETURN:
  5785. * 0 = success
  5786. * !0 = failure
  5787. *
  5788. * PARAMETERS:
  5789. * *cd - pointer to core data
  5790. * *desc - pointer to the HID descriptor data read back from DUT
  5791. ******************************************************************************/
  5792. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5793. struct pt_hid_desc *desc)
  5794. {
  5795. struct device *dev = cd->dev;
  5796. int rc = 0;
  5797. int t;
  5798. u8 cmd[2];
  5799. /*
  5800. * During startup the HID descriptor is required for all future
  5801. * processing. If IRQ is already asserted due to an early touch report
  5802. * the report must be cleared before sending command.
  5803. */
  5804. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5805. /* Read HID descriptor length and version */
  5806. mutex_lock(&cd->system_lock);
  5807. cd->hid_cmd_state = 1;
  5808. mutex_unlock(&cd->system_lock);
  5809. /* Set HID descriptor register */
  5810. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5811. sizeof(cd->hid_core.hid_desc_register));
  5812. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5813. __func__, sizeof(cmd));
  5814. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5815. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5816. if (rc) {
  5817. pt_debug(dev, DL_ERROR,
  5818. "%s: failed to get HID descriptor, rc=%d\n",
  5819. __func__, rc);
  5820. goto error;
  5821. }
  5822. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5823. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5824. if (IS_TMO(t)) {
  5825. #ifdef TTDL_DIAGNOSTICS
  5826. cd->bus_transmit_error_count++;
  5827. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5828. #endif /* TTDL_DIAGNOSTICS */
  5829. pt_debug(cd->dev, DL_ERROR,
  5830. "%s: HID get descriptor timed out\n", __func__);
  5831. rc = -ETIME;
  5832. goto error;
  5833. } else {
  5834. cd->hw_detected = true;
  5835. }
  5836. /* Load the HID descriptor including all registers */
  5837. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5838. /* Check HID descriptor length and version */
  5839. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5840. le16_to_cpu(desc->hid_desc_len),
  5841. le16_to_cpu(desc->bcd_version));
  5842. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5843. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5844. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5845. __func__);
  5846. return -ENODEV;
  5847. }
  5848. goto exit;
  5849. error:
  5850. mutex_lock(&cd->system_lock);
  5851. cd->hid_cmd_state = 0;
  5852. mutex_unlock(&cd->system_lock);
  5853. exit:
  5854. return rc;
  5855. }
  5856. /*******************************************************************************
  5857. * FUNCTION: pt_get_hid_descriptor
  5858. *
  5859. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5860. *
  5861. * RETURN:
  5862. * 0 = success
  5863. * !0 = failure
  5864. *
  5865. * PARAMETERS:
  5866. * *cd - pointer to core data
  5867. * *desc - pointer to the HID descriptor data read back from DUT
  5868. ******************************************************************************/
  5869. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5870. struct pt_hid_desc *desc)
  5871. {
  5872. int rc;
  5873. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5874. if (rc < 0) {
  5875. pt_debug(cd->dev, DL_ERROR,
  5876. "%s: fail get exclusive ex=%p own=%p\n",
  5877. __func__, cd->exclusive_dev, cd->dev);
  5878. return rc;
  5879. }
  5880. rc = pt_get_hid_descriptor_(cd, desc);
  5881. if (release_exclusive(cd, cd->dev) < 0)
  5882. pt_debug(cd->dev, DL_ERROR,
  5883. "%s: fail to release exclusive\n", __func__);
  5884. return rc;
  5885. }
  5886. /*******************************************************************************
  5887. * FUNCTION: pt_pip2_get_version_
  5888. *
  5889. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5890. * cd-ttdata
  5891. *
  5892. * RETURN:
  5893. * 0 = success
  5894. * !0 = failure
  5895. *
  5896. * PARAMETERS:
  5897. * *cd - pointer to core data
  5898. ******************************************************************************/
  5899. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5900. {
  5901. int rc = 0;
  5902. int status;
  5903. u8 read_buf[64];
  5904. u16 actual_read_len;
  5905. rc = _pt_request_pip2_send_cmd(cd->dev,
  5906. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5907. NULL, 0, read_buf, &actual_read_len);
  5908. if (rc) {
  5909. pt_debug(cd->dev, DL_ERROR,
  5910. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5911. __func__, rc);
  5912. return rc;
  5913. }
  5914. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5915. if (status == 0) {
  5916. /* Parse the PIP2 VERSION response into ttdata */
  5917. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5918. } else {
  5919. pt_debug(cd->dev, DL_ERROR,
  5920. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5921. __func__, status);
  5922. return status;
  5923. }
  5924. return rc;
  5925. }
  5926. /*******************************************************************************
  5927. * FUNCTION: pt_pip2_get_version
  5928. *
  5929. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5930. * access to the DUT.
  5931. *
  5932. * RETURN:
  5933. * 0 = success
  5934. * !0 = failure
  5935. *
  5936. * PARAMETERS:
  5937. * *cd - pointer to core data structure
  5938. ******************************************************************************/
  5939. static int pt_pip2_get_version(struct pt_core_data *cd)
  5940. {
  5941. int rc;
  5942. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5943. if (rc < 0) {
  5944. pt_debug(cd->dev, DL_ERROR,
  5945. "%s: fail get exclusive ex=%p own=%p\n",
  5946. __func__, cd->exclusive_dev, cd->dev);
  5947. return rc;
  5948. }
  5949. rc = pt_pip2_get_version_(cd);
  5950. if (release_exclusive(cd, cd->dev) < 0)
  5951. pt_debug(cd->dev, DL_ERROR,
  5952. "%s: fail to release exclusive\n", __func__);
  5953. return rc;
  5954. }
  5955. /*******************************************************************************
  5956. * FUNCTION: _pt_request_active_pip_protocol
  5957. *
  5958. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5959. * Function will return PIP version of BL or application based on
  5960. * when it's called.
  5961. *
  5962. * RETURN:
  5963. * 0 = success
  5964. * !0 = failure
  5965. *
  5966. * PARAMETERS:
  5967. * *dev - pointer to device structure
  5968. * protect - flag to run in protected mode
  5969. * *pip_version_major - pointer to store PIP major version
  5970. * *pip_version_minor - pointer to store PIP minor version
  5971. ******************************************************************************/
  5972. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5973. u8 *pip_version_major, u8 *pip_version_minor)
  5974. {
  5975. struct pt_core_data *cd = dev_get_drvdata(dev);
  5976. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5977. int rc = 0;
  5978. struct pt_hid_output sys_info = {
  5979. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5980. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5981. };
  5982. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5983. /* Skip PIP2 command if DUT generation is confirmed */
  5984. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5985. goto skip_pip2_command;
  5986. rc = pt_pip2_get_version_(cd);
  5987. if (!rc) {
  5988. *pip_version_major = ttdata->pip_ver_major;
  5989. *pip_version_minor = ttdata->pip_ver_minor;
  5990. pt_debug(dev, DL_INFO,
  5991. "%s: pip_version = %d.%d\n", __func__,
  5992. *pip_version_major, *pip_version_minor);
  5993. } else {
  5994. /*
  5995. * Legacy products do not support the pip2 protocol to get
  5996. * pip version. However, they do support the "get sysinfo"
  5997. * command to get pip version from FW, but the bootloader
  5998. * does not support it. This function will try "get sysinfo"
  5999. * command if the pip2 command failed but this cmd could also
  6000. * fail if DUT is stuck in bootloader mode.
  6001. */
  6002. pt_debug(dev, DL_INFO,
  6003. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  6004. __func__, rc);
  6005. skip_pip2_command:
  6006. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  6007. if (!rc) {
  6008. *pip_version_minor =
  6009. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6010. *pip_version_major =
  6011. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6012. pt_debug(dev, DL_INFO,
  6013. "%s: pip_version = %d.%d\n", __func__,
  6014. *pip_version_major, *pip_version_minor);
  6015. } else {
  6016. *pip_version_major = 0;
  6017. *pip_version_minor = 0;
  6018. pt_debug(dev, DL_ERROR,
  6019. "%s: pip_version Not Detected\n", __func__);
  6020. }
  6021. }
  6022. return rc;
  6023. }
  6024. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6025. /*******************************************************************************
  6026. * FUNCTION: _pt_detect_dut_generation
  6027. *
  6028. * SUMMARY: Determine the generation of device that we are communicating with:
  6029. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6030. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6031. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6032. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6033. * coming form the BL or the FW:
  6034. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6035. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6036. * This function will return a modified status if it detects the DUT
  6037. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6038. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6039. *
  6040. * RETURN:
  6041. * 0 = success
  6042. * !0 = failure
  6043. *
  6044. * PARAMETERS:
  6045. * *dev - pointer to device structure
  6046. * *status - pointer to status bitmask
  6047. * *dut_gen - pointer to store the dut_generation
  6048. * *mode - pointer to store the PT_MODE
  6049. ******************************************************************************/
  6050. static int _pt_detect_dut_generation(struct device *dev,
  6051. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6052. {
  6053. int rc = 0;
  6054. u8 dut_gen_tmp = DUT_UNKNOWN;
  6055. u8 mode_tmp = PT_MODE_UNKNOWN;
  6056. u8 attempt = 1;
  6057. u32 status_tmp = STARTUP_STATUS_START;
  6058. struct pt_core_data *cd = dev_get_drvdata(dev);
  6059. struct pt_hid_desc hid_desc;
  6060. memset(&hid_desc, 0, sizeof(hid_desc));
  6061. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6062. while (rc && attempt < 3) {
  6063. attempt++;
  6064. usleep_range(2000, 5000);
  6065. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6066. }
  6067. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6068. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6069. mode_tmp = PT_MODE_BOOTLOADER;
  6070. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6071. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6072. rc = pt_pip2_get_version_(cd);
  6073. if (!rc)
  6074. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6075. else
  6076. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6077. mode_tmp = PT_MODE_OPERATIONAL;
  6078. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6079. rc = 0; /* To return success instead of error code */
  6080. } else if (rc) {
  6081. rc = pt_pip2_get_version_(cd);
  6082. if (!rc) {
  6083. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6084. mode_tmp = PT_MODE_BOOTLOADER;
  6085. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6086. }
  6087. }
  6088. mutex_lock(&cd->system_lock);
  6089. if (dut_gen)
  6090. *dut_gen = dut_gen_tmp;
  6091. if (mode)
  6092. *mode = mode_tmp;
  6093. if (status)
  6094. *status = status_tmp;
  6095. mutex_unlock(&cd->system_lock);
  6096. #ifdef TTDL_DIAGNOSTICS
  6097. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6098. __func__, dut_gen_tmp, mode_tmp);
  6099. #endif /* TTDL_DIAGNOSTICS */
  6100. return rc;
  6101. }
  6102. /*******************************************************************************
  6103. * FUNCTION: _pt_request_dut_generation
  6104. *
  6105. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6106. * to get current dut generation.
  6107. *
  6108. * NOTE: This function WILL NOT try to determine dut generation.
  6109. *
  6110. * RETURN:
  6111. * The current dut generation.
  6112. *
  6113. * PARAMETERS:
  6114. * *dev - pointer to device structure
  6115. ******************************************************************************/
  6116. static int _pt_request_dut_generation(struct device *dev)
  6117. {
  6118. struct pt_core_data *cd = dev_get_drvdata(dev);
  6119. return cd->active_dut_generation;
  6120. }
  6121. #define HW_VERSION_LEN_MAX 13
  6122. /*******************************************************************************
  6123. * FUNCTION: _legacy_generate_hw_version
  6124. *
  6125. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6126. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6127. * command) to the hw_version.
  6128. *
  6129. * RETURN:
  6130. * 0 = success
  6131. * !0 = failure
  6132. *
  6133. * PARAMETERS:
  6134. * *cd - pointer to core data
  6135. * *hw_version - pointer to store the hardware version
  6136. ******************************************************************************/
  6137. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6138. char *hw_version)
  6139. {
  6140. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6141. if (cd->sysinfo.ready) {
  6142. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6143. ttdata->jtag_id_h, cd->pid_for_loader);
  6144. return 0;
  6145. } else if (cd->bl_info.ready) {
  6146. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6147. cd->bl_info.chip_id, cd->pid_for_loader);
  6148. return 0;
  6149. } else {
  6150. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6151. pt_debug(cd->dev, DL_ERROR,
  6152. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6153. return -ENODATA;
  6154. }
  6155. }
  6156. /*******************************************************************************
  6157. * FUNCTION: _pip2_generate_hw_version
  6158. *
  6159. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6160. * VERSION command) to the hw_version.
  6161. *
  6162. * RETURN:
  6163. * 0 = success
  6164. * !0 = failure
  6165. *
  6166. * PARAMETERS:
  6167. * *cd - pointer to core data
  6168. * *hw_version - pointer to store the hardware version
  6169. ******************************************************************************/
  6170. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6171. {
  6172. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6173. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6174. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6175. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6176. return 0;
  6177. } else {
  6178. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6179. pt_debug(cd->dev, DL_ERROR,
  6180. "%s: PIP Version are not ready\n", __func__);
  6181. return -ENODATA;
  6182. }
  6183. }
  6184. /*******************************************************************************
  6185. * FUNCTION: pt_generate_hw_version
  6186. *
  6187. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6188. * hw_version from static data.
  6189. *
  6190. * RETURN:
  6191. * 0 = success
  6192. * !0 = failure
  6193. *
  6194. * PARAMETERS:
  6195. * *cd - pointer to core data
  6196. * *hw_version - pointer to store the hardware version
  6197. ******************************************************************************/
  6198. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6199. {
  6200. int rc = 0;
  6201. if (!hw_version)
  6202. return -ENOMEM;
  6203. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6204. rc = _legacy_generate_hw_version(cd, hw_version);
  6205. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6206. rc = _pip2_generate_hw_version(cd, hw_version);
  6207. else {
  6208. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6209. rc = -ENODATA;
  6210. }
  6211. return rc;
  6212. }
  6213. /*******************************************************************************
  6214. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6215. *
  6216. * NOTE: The calling function must ensure to free *hw_version
  6217. *
  6218. * RETURN:
  6219. * 0 = success
  6220. * !0 = Failure
  6221. *
  6222. * PARAMETERS:
  6223. * *dev - pointer to device structure
  6224. * *hw_version - pointer to where the hw_version string will be stored
  6225. ******************************************************************************/
  6226. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6227. {
  6228. int rc = 0;
  6229. u16 actual_read_len;
  6230. u16 pip_ver;
  6231. u8 rd_buf[256];
  6232. u8 status;
  6233. u8 index = PIP2_RESP_STATUS_OFFSET;
  6234. u8 return_data[8];
  6235. u8 panel_id;
  6236. struct pt_core_data *cd = dev_get_drvdata(dev);
  6237. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6238. if (!hw_version)
  6239. return -ENOMEM;
  6240. if (!cd->hw_detected) {
  6241. /* No HW detected */
  6242. rc = -ENODEV;
  6243. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6244. __func__);
  6245. goto exit_error;
  6246. }
  6247. /* For Parade TC or TT parts */
  6248. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6249. rc = _pt_request_pip2_send_cmd(dev,
  6250. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6251. NULL, 0, rd_buf, &actual_read_len);
  6252. if (rc) {
  6253. pt_debug(dev, DL_ERROR,
  6254. "%s: Failed to send PIP2 VERSION cmd\n",
  6255. __func__);
  6256. goto exit_error;
  6257. }
  6258. status = rd_buf[index];
  6259. if (status == 0) {
  6260. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6261. /*
  6262. * BL PIP 2.02 and greater the version fields are
  6263. * swapped
  6264. */
  6265. if (pip_ver >= 0x0202) {
  6266. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6267. "%02X%02X.%02X%02X.FF",
  6268. rd_buf[index + 10], rd_buf[index + 9],
  6269. rd_buf[index + 8], rd_buf[index + 7]);
  6270. } else {
  6271. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6272. "%02X%02X.%02X%02X.FF",
  6273. rd_buf[index + 8], rd_buf[index + 7],
  6274. rd_buf[index + 10], rd_buf[index + 9]);
  6275. }
  6276. return STATUS_SUCCESS;
  6277. } else {
  6278. rc = status;
  6279. pt_debug(dev, DL_WARN,
  6280. "%s: PIP2 VERSION cmd response error\n",
  6281. __func__);
  6282. }
  6283. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6284. /*
  6285. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6286. * hard coded to FFFF
  6287. */
  6288. if (cd->mode == PT_MODE_OPERATIONAL) {
  6289. rc = pt_hid_output_get_sysinfo(cd);
  6290. if (!rc) {
  6291. panel_id =
  6292. cd->sysinfo.sensing_conf_data.panel_id;
  6293. } else {
  6294. panel_id = PANEL_ID_NOT_ENABLED;
  6295. }
  6296. /* In FW - simply retrieve from ttdata struct */
  6297. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6298. "%04X.FFFF.%02X",
  6299. ttdata->jtag_id_h,
  6300. panel_id);
  6301. return STATUS_SUCCESS;
  6302. } else {
  6303. /*
  6304. * Return the stored value if PT_PANEL_ID_BY_BL
  6305. * is not supported while other feature is.
  6306. */
  6307. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6308. rc = pt_hid_output_bl_get_information(
  6309. cd, return_data);
  6310. if (!rc) {
  6311. rc = pt_hid_output_bl_get_panel_id(
  6312. cd, &panel_id);
  6313. }
  6314. } else
  6315. panel_id = cd->pid_for_loader;
  6316. if (!rc) {
  6317. snprintf(hw_version,
  6318. HW_VERSION_LEN_MAX,
  6319. "%02X%02X.FFFF.%02X",
  6320. return_data[3], return_data[2],
  6321. panel_id);
  6322. return STATUS_SUCCESS;
  6323. }
  6324. }
  6325. } else {
  6326. /* Unknown generation */
  6327. rc = -ENODEV;
  6328. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6329. __func__);
  6330. }
  6331. exit_error:
  6332. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6333. return rc;
  6334. }
  6335. /*******************************************************************************
  6336. * FUNCTION: pt_start_wd_timer
  6337. *
  6338. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6339. *
  6340. * RETURN: void
  6341. *
  6342. * PARAMETERS:
  6343. * *cd - pointer to core data
  6344. ******************************************************************************/
  6345. static void pt_start_wd_timer(struct pt_core_data *cd)
  6346. {
  6347. if (cd->watchdog_interval < 100) {
  6348. pt_debug(cd->dev, DL_ERROR,
  6349. "%s: WARNING: Invalid watchdog interval: %d\n",
  6350. __func__, cd->watchdog_interval);
  6351. return;
  6352. }
  6353. if (cd->watchdog_force_stop) {
  6354. pt_debug(cd->dev, DL_INFO,
  6355. "%s: TTDL WD Forced stop\n", __func__);
  6356. return;
  6357. }
  6358. mod_timer(&cd->watchdog_timer, jiffies +
  6359. msecs_to_jiffies(cd->watchdog_interval));
  6360. cd->watchdog_enabled = 1;
  6361. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6362. }
  6363. /*******************************************************************************
  6364. * FUNCTION: pt_stop_wd_timer
  6365. *
  6366. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6367. *
  6368. * RETURN: void
  6369. *
  6370. * PARAMETERS:
  6371. * *cd - pointer to core data
  6372. ******************************************************************************/
  6373. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6374. {
  6375. if (!cd->watchdog_interval)
  6376. return;
  6377. /*
  6378. * Ensure we wait until the watchdog timer
  6379. * running on a different CPU finishes
  6380. */
  6381. del_timer_sync(&cd->watchdog_timer);
  6382. cancel_work_sync(&cd->watchdog_work);
  6383. del_timer_sync(&cd->watchdog_timer);
  6384. cd->watchdog_enabled = 0;
  6385. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6386. }
  6387. /*******************************************************************************
  6388. * FUNCTION: pt_hw_soft_reset
  6389. *
  6390. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6391. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6392. * schedule an enum if it fires when the DUT is being reset.
  6393. * This can cause a double reset.
  6394. *
  6395. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6396. * the WD active could cause this function to fail!
  6397. *
  6398. * RETURN:
  6399. * 0 = success
  6400. * !0 = failure
  6401. *
  6402. * PARAMETERS:
  6403. * *cd - pointer to core data struct
  6404. * protect - flag to call protected or non-protected
  6405. ******************************************************************************/
  6406. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6407. {
  6408. int rc = 0;
  6409. mutex_lock(&cd->system_lock);
  6410. cd->startup_status = STARTUP_STATUS_START;
  6411. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6412. mutex_unlock(&cd->system_lock);
  6413. if (protect)
  6414. rc = pt_hid_cmd_reset(cd);
  6415. else
  6416. rc = pt_hid_cmd_reset_(cd);
  6417. if (rc < 0) {
  6418. pt_debug(cd->dev, DL_ERROR,
  6419. "%s: FAILED to execute SOFT reset\n", __func__);
  6420. return rc;
  6421. }
  6422. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6423. __func__);
  6424. return 0;
  6425. }
  6426. /*******************************************************************************
  6427. * FUNCTION: pt_hw_hard_reset
  6428. *
  6429. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6430. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6431. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6432. * schedule an enum if it fires when the DUT is being reset.
  6433. * This can cause a double reset.
  6434. *
  6435. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6436. * the WD active could cause this function to fail!
  6437. *
  6438. * RETURN:
  6439. * 0 = success
  6440. * !0 = failure
  6441. *
  6442. * PARAMETERS:
  6443. * *cd - pointer to core data struct
  6444. ******************************************************************************/
  6445. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6446. {
  6447. if (cd->cpdata->xres) {
  6448. cd->startup_status = STARTUP_STATUS_START;
  6449. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6450. __func__);
  6451. cd->cpdata->xres(cd->cpdata, cd->dev);
  6452. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6453. __func__);
  6454. return 0;
  6455. }
  6456. pt_debug(cd->dev, DL_ERROR,
  6457. "%s: FAILED to execute HARD reset\n", __func__);
  6458. return -ENODEV;
  6459. }
  6460. /*******************************************************************************
  6461. * FUNCTION: pt_dut_reset
  6462. *
  6463. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6464. * soft reset.
  6465. *
  6466. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6467. * the WD active could cause this function to fail!
  6468. * NOTE: "protect" flag is only used for soft reset.
  6469. *
  6470. * RETURN:
  6471. * 0 = success
  6472. * !0 = failure
  6473. *
  6474. * PARAMETERS:
  6475. * *cd - pointer to core data structure
  6476. * protect - flag to call protected or non-protected
  6477. ******************************************************************************/
  6478. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6479. {
  6480. int rc = 0;
  6481. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6482. mutex_lock(&cd->system_lock);
  6483. cd->hid_reset_cmd_state = 1;
  6484. rc = pt_hw_hard_reset(cd);
  6485. mutex_unlock(&cd->system_lock);
  6486. if (rc == -ENODEV) {
  6487. mutex_lock(&cd->system_lock);
  6488. cd->hid_reset_cmd_state = 0;
  6489. mutex_unlock(&cd->system_lock);
  6490. pt_debug(cd->dev, DL_ERROR,
  6491. "%s: Hard reset failed, try soft reset\n", __func__);
  6492. rc = pt_hw_soft_reset(cd, protect);
  6493. }
  6494. if (rc)
  6495. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6496. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6497. return rc;
  6498. }
  6499. /*******************************************************************************
  6500. * FUNCTION: pt_dut_reset_and_wait
  6501. *
  6502. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6503. * complete
  6504. *
  6505. * RETURN:
  6506. * 0 = success
  6507. * !0 = failure
  6508. *
  6509. * PARAMETERS:
  6510. * *cd - pointer to core data structure
  6511. ******************************************************************************/
  6512. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6513. {
  6514. int rc = 0;
  6515. int t;
  6516. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6517. if (rc < 0)
  6518. goto exit;
  6519. t = wait_event_timeout(cd->wait_q,
  6520. (cd->hid_reset_cmd_state == 0),
  6521. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6522. if (IS_TMO(t)) {
  6523. #ifdef TTDL_DIAGNOSTICS
  6524. cd->bus_transmit_error_count++;
  6525. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6526. #endif /* TTDL_DIAGNOSTICS */
  6527. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6528. __func__);
  6529. rc = -ETIME;
  6530. goto exit;
  6531. }
  6532. exit:
  6533. return rc;
  6534. }
  6535. /*
  6536. * touch default parameters (from report descriptor) to resolve protocol for
  6537. * touch report
  6538. */
  6539. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6540. /* byte offset, size, min, max, bit offset, report */
  6541. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6542. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6543. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6544. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6545. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6546. };
  6547. /*
  6548. * button default parameters (from report descriptor) to resolve protocol for
  6549. * button report
  6550. */
  6551. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6552. /* byte offset, size, min, max, bit offset, report */
  6553. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6554. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6555. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6556. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6557. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6558. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6559. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6560. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6561. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6562. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6563. };
  6564. /*******************************************************************************
  6565. * FUNCTION: pt_init_pip_report_fields
  6566. *
  6567. * SUMMARY: Setup default values for touch/button report parsing.
  6568. *
  6569. * PARAMETERS:
  6570. * *cd - pointer to core data structure
  6571. ******************************************************************************/
  6572. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6573. {
  6574. struct pt_sysinfo *si = &cd->sysinfo;
  6575. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6576. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6577. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6578. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6579. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6580. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6581. cd->features.easywake = 1;
  6582. cd->features.noise_metric = 1;
  6583. cd->features.tracking_heatmap = 1;
  6584. cd->features.sensor_data = 1;
  6585. }
  6586. /*******************************************************************************
  6587. * FUNCTION: pt_get_mode
  6588. *
  6589. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6590. * message
  6591. *
  6592. * RETURN: Enum of the current mode
  6593. *
  6594. * PARAMETERS:
  6595. * *cd - pointer to the Core Data structure
  6596. * protect - run command in protected mode
  6597. * *mode - pointer to store the retrieved mode
  6598. ******************************************************************************/
  6599. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6600. {
  6601. if (desc->packet_id == HID_APP_REPORT_ID)
  6602. return PT_MODE_OPERATIONAL;
  6603. else if (desc->packet_id == HID_BL_REPORT_ID)
  6604. return PT_MODE_BOOTLOADER;
  6605. return PT_MODE_UNKNOWN;
  6606. }
  6607. /*******************************************************************************
  6608. * FUNCTION: _pt_request_get_mode
  6609. *
  6610. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6611. * to determine the current mode of the DUT by use of the Get HID
  6612. * Descriptor command.
  6613. *
  6614. * RETURN:
  6615. * 0 = success
  6616. * !0 = failure
  6617. *
  6618. * PARAMETERS:
  6619. * *dev - pointer to device structure
  6620. * protect - run command in protected mode
  6621. * *mode - pointer to store the retrieved mode
  6622. ******************************************************************************/
  6623. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6624. {
  6625. struct pt_core_data *cd = dev_get_drvdata(dev);
  6626. struct pt_hid_desc hid_desc;
  6627. int rc = 0;
  6628. memset(&hid_desc, 0, sizeof(hid_desc));
  6629. if (protect)
  6630. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6631. else
  6632. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6633. if (rc)
  6634. *mode = PT_MODE_UNKNOWN;
  6635. else
  6636. *mode = pt_get_mode(cd, &hid_desc);
  6637. return rc;
  6638. }
  6639. /*******************************************************************************
  6640. * FUNCTION: pt_queue_enum_
  6641. *
  6642. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6643. * function. It won't try to add/delete sysfs node or modules.
  6644. *
  6645. * RETURN: void
  6646. *
  6647. * PARAMETERS:
  6648. * *cd - pointer to core data
  6649. ******************************************************************************/
  6650. static void pt_queue_enum_(struct pt_core_data *cd)
  6651. {
  6652. if (cd->startup_state == STARTUP_NONE) {
  6653. cd->startup_state = STARTUP_QUEUED;
  6654. #ifdef TTDL_DIAGNOSTICS
  6655. if (!cd->bridge_mode)
  6656. schedule_work(&cd->enum_work);
  6657. else
  6658. cd->startup_state = STARTUP_NONE;
  6659. #else
  6660. schedule_work(&cd->enum_work);
  6661. #endif
  6662. pt_debug(cd->dev, DL_INFO,
  6663. "%s: enum_work queued\n", __func__);
  6664. } else {
  6665. pt_debug(cd->dev, DL_WARN,
  6666. "%s: Enum not queued - startup_state = %d\n",
  6667. __func__, cd->startup_state);
  6668. }
  6669. }
  6670. /*******************************************************************************
  6671. * FUNCTION: pt_queue_enum
  6672. *
  6673. * SUMMARY: Queues a TTDL enum within a mutex lock
  6674. *
  6675. * RETURN: void
  6676. *
  6677. * PARAMETERS:
  6678. * *cd - pointer to core data
  6679. ******************************************************************************/
  6680. static void pt_queue_enum(struct pt_core_data *cd)
  6681. {
  6682. mutex_lock(&cd->system_lock);
  6683. pt_queue_enum_(cd);
  6684. mutex_unlock(&cd->system_lock);
  6685. }
  6686. static void remove_sysfs_and_modules(struct device *dev);
  6687. /*******************************************************************************
  6688. * FUNCTION: pt_queue_restart
  6689. *
  6690. * SUMMARY: Queues a TTDL restart within a mutex lock
  6691. *
  6692. * RETURN: void
  6693. *
  6694. * PARAMETERS:
  6695. * *cd - pointer to core data
  6696. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6697. * False: will not perform remove action
  6698. ******************************************************************************/
  6699. static void pt_queue_restart(struct pt_core_data *cd)
  6700. {
  6701. mutex_lock(&cd->system_lock);
  6702. if (cd->startup_state == STARTUP_NONE) {
  6703. cd->startup_state = STARTUP_QUEUED;
  6704. schedule_work(&cd->ttdl_restart_work);
  6705. pt_debug(cd->dev, DL_INFO,
  6706. "%s: pt_ttdl_restart queued\n", __func__);
  6707. } else {
  6708. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6709. __func__, cd->startup_state);
  6710. }
  6711. mutex_unlock(&cd->system_lock);
  6712. }
  6713. /*******************************************************************************
  6714. * FUNCTION: call_atten_cb
  6715. *
  6716. * SUMMARY: Iterate over attention list call the function that registered.
  6717. *
  6718. * RETURN: void
  6719. *
  6720. * PARAMETERS:
  6721. * *cd - pointer to core data
  6722. * type - type of attention list
  6723. * mode - condition for execution
  6724. ******************************************************************************/
  6725. static void call_atten_cb(struct pt_core_data *cd,
  6726. enum pt_atten_type type, int mode)
  6727. {
  6728. struct atten_node *atten, *atten_n;
  6729. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6730. __func__, type, mode);
  6731. spin_lock(&cd->spinlock);
  6732. list_for_each_entry_safe(atten, atten_n,
  6733. &cd->atten_list[type], node) {
  6734. if (!mode || atten->mode & mode) {
  6735. spin_unlock(&cd->spinlock);
  6736. pt_debug(cd->dev, DL_DEBUG,
  6737. "%s: attention for '%s'",
  6738. __func__, dev_name(atten->dev));
  6739. atten->func(atten->dev);
  6740. spin_lock(&cd->spinlock);
  6741. }
  6742. }
  6743. spin_unlock(&cd->spinlock);
  6744. }
  6745. /*******************************************************************************
  6746. * FUNCTION: start_fw_upgrade
  6747. *
  6748. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6749. * start firmware upgrade.
  6750. *
  6751. * RETURN:
  6752. * 0 = success
  6753. *
  6754. * PARAMETERS:
  6755. * *data - pointer to core data
  6756. ******************************************************************************/
  6757. static int start_fw_upgrade(void *data)
  6758. {
  6759. struct pt_core_data *cd = (struct pt_core_data *)data;
  6760. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6761. return 0;
  6762. }
  6763. /*******************************************************************************
  6764. * FUNCTION: pt_put_device_into_easy_wakeup_
  6765. *
  6766. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6767. * wake up state.
  6768. *
  6769. * RETURN:
  6770. * 0 = success
  6771. * !0 = failure
  6772. *
  6773. * PARAMETERS:
  6774. * *cd - pointer to core data
  6775. ******************************************************************************/
  6776. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6777. {
  6778. int rc = 0;
  6779. u8 status = 0;
  6780. mutex_lock(&cd->system_lock);
  6781. cd->wait_until_wake = 0;
  6782. mutex_unlock(&cd->system_lock);
  6783. rc = pt_hid_output_enter_easywake_state_(cd,
  6784. cd->easy_wakeup_gesture, &status);
  6785. if (rc || status == 0)
  6786. return -EBUSY;
  6787. return rc;
  6788. }
  6789. /*******************************************************************************
  6790. * FUNCTION: pt_put_device_into_deep_sleep_
  6791. *
  6792. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6793. *
  6794. * RETURN:
  6795. * 0 = success
  6796. * !0 = error
  6797. *
  6798. * PARAMETERS:
  6799. * *cd - pointer to core data
  6800. ******************************************************************************/
  6801. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6802. {
  6803. int rc = 0;
  6804. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6805. if (rc)
  6806. rc = -EBUSY;
  6807. return rc;
  6808. }
  6809. /*******************************************************************************
  6810. * FUNCTION: pt_put_device_into_deep_standby_
  6811. *
  6812. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6813. *
  6814. * RETURN:
  6815. * 0 = success
  6816. * !0 = error
  6817. *
  6818. * PARAMETERS:
  6819. * *cd - pointer to core data
  6820. ******************************************************************************/
  6821. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6822. {
  6823. int rc = 0;
  6824. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6825. if (rc)
  6826. rc = -EBUSY;
  6827. return rc;
  6828. }
  6829. /*******************************************************************************
  6830. * FUNCTION: pt_core_poweroff_device_
  6831. *
  6832. * SUMMARY: Disable IRQ and HW power down the device.
  6833. *
  6834. * RETURN:
  6835. * 0 = success
  6836. * !0 = failure
  6837. *
  6838. * PARAMETERS:
  6839. * *cd - pointer to core data
  6840. ******************************************************************************/
  6841. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6842. {
  6843. int rc;
  6844. if (cd->irq_enabled) {
  6845. cd->irq_enabled = false;
  6846. disable_irq_nosync(cd->irq);
  6847. }
  6848. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6849. if (rc < 0)
  6850. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6851. __func__, rc);
  6852. return rc;
  6853. }
  6854. /*******************************************************************************
  6855. * FUNCTION: pt_core_sleep_
  6856. *
  6857. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6858. * configuration in the core platform data structure.
  6859. *
  6860. * RETURN:
  6861. * 0 = success
  6862. * !0 = failure
  6863. *
  6864. * PARAMETERS:
  6865. * *cd - pointer to core data
  6866. ******************************************************************************/
  6867. static int pt_core_sleep_(struct pt_core_data *cd)
  6868. {
  6869. int rc = 0;
  6870. mutex_lock(&cd->system_lock);
  6871. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6872. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEPING) {
  6873. mutex_unlock(&cd->system_lock);
  6874. pt_debug(cd->dev, DL_INFO,
  6875. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6876. return 0;
  6877. } else {
  6878. cd->sleep_state = SS_SLEEPING;
  6879. }
  6880. mutex_unlock(&cd->system_lock);
  6881. /* Ensure watchdog and startup works stopped */
  6882. pt_stop_wd_timer(cd);
  6883. cancel_work_sync(&cd->enum_work);
  6884. pt_stop_wd_timer(cd);
  6885. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6886. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6887. rc = pt_core_poweroff_device_(cd);
  6888. if (rc)
  6889. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6890. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6891. pt_debug(cd->dev, DL_INFO,
  6892. "%s: Entering into deep standby mode:\n", __func__);
  6893. rc = pt_put_device_into_deep_standby_(cd);
  6894. if (rc)
  6895. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6896. } else {
  6897. pt_debug(cd->dev, DL_INFO,
  6898. "%s: Entering into deep sleep mode:\n", __func__);
  6899. rc = pt_put_device_into_deep_sleep_(cd);
  6900. if (rc)
  6901. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6902. }
  6903. mutex_lock(&cd->system_lock);
  6904. cd->sleep_state = SS_SLEEP_ON;
  6905. mutex_unlock(&cd->system_lock);
  6906. return rc;
  6907. }
  6908. /*******************************************************************************
  6909. * FUNCTION: pt_core_easywake_on_
  6910. *
  6911. * SUMMARY: Suspend the device with easy wake on the
  6912. * configuration in the core platform data structure.
  6913. *
  6914. * RETURN:
  6915. * 0 = success
  6916. * !0 = failure
  6917. *
  6918. * PARAMETERS:
  6919. * *cd - pointer to core data
  6920. ******************************************************************************/
  6921. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6922. {
  6923. int rc = 0;
  6924. mutex_lock(&cd->system_lock);
  6925. if (cd->sleep_state == SS_EASY_WAKING_ON) {
  6926. mutex_unlock(&cd->system_lock);
  6927. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6928. __func__, cd->sleep_state);
  6929. return 0;
  6930. }
  6931. mutex_unlock(&cd->system_lock);
  6932. /* Ensure watchdog and startup works stopped */
  6933. pt_stop_wd_timer(cd);
  6934. cancel_work_sync(&cd->enum_work);
  6935. pt_stop_wd_timer(cd);
  6936. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6937. rc = pt_put_device_into_easy_wakeup_(cd);
  6938. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6939. if (rc)
  6940. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6941. }
  6942. mutex_lock(&cd->system_lock);
  6943. cd->sleep_state = SS_EASY_WAKING_ON;
  6944. mutex_unlock(&cd->system_lock);
  6945. return rc;
  6946. }
  6947. /*******************************************************************************
  6948. * FUNCTION: pt_core_easywake_on
  6949. *
  6950. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6951. *
  6952. * RETURN:
  6953. * 0 = success
  6954. * !0 = failure
  6955. *
  6956. * PARAMETERS:
  6957. * *cd - pointer to core data
  6958. ******************************************************************************/
  6959. static int pt_core_easywake_on(struct pt_core_data *cd)
  6960. {
  6961. int rc = 0;
  6962. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6963. if (rc < 0) {
  6964. pt_debug(cd->dev, DL_ERROR,
  6965. "%s: fail get exclusive ex=%p own=%p\n",
  6966. __func__, cd->exclusive_dev, cd->dev);
  6967. return rc;
  6968. }
  6969. rc = pt_core_easywake_on_(cd);
  6970. if (release_exclusive(cd, cd->dev) < 0)
  6971. pt_debug(cd->dev, DL_ERROR,
  6972. "%s: fail to release exclusive\n", __func__);
  6973. else
  6974. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6975. __func__);
  6976. return rc;
  6977. }
  6978. /*******************************************************************************
  6979. * FUNCTION: pt_core_sleep
  6980. *
  6981. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6982. *
  6983. * RETURN:
  6984. * 0 = success
  6985. * !0 = failure
  6986. *
  6987. * PARAMETERS:
  6988. * *cd - pointer to core data
  6989. ******************************************************************************/
  6990. static int pt_core_sleep(struct pt_core_data *cd)
  6991. {
  6992. int rc = 0;
  6993. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6994. if (rc < 0) {
  6995. pt_debug(cd->dev, DL_ERROR,
  6996. "%s: fail get exclusive ex=%p own=%p\n",
  6997. __func__, cd->exclusive_dev, cd->dev);
  6998. return rc;
  6999. }
  7000. rc = pt_core_sleep_(cd);
  7001. if (release_exclusive(cd, cd->dev) < 0)
  7002. pt_debug(cd->dev, DL_ERROR,
  7003. "%s: fail to release exclusive\n", __func__);
  7004. else
  7005. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  7006. __func__);
  7007. return rc;
  7008. }
  7009. /*******************************************************************************
  7010. * FUNCTION: pt_wakeup_host
  7011. *
  7012. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7013. *
  7014. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7015. *
  7016. * RETURN:
  7017. * 0 = success
  7018. * !0 = failure
  7019. *
  7020. * PARAMETERS:
  7021. * *cd - pointer to core data
  7022. ******************************************************************************/
  7023. static int pt_wakeup_host(struct pt_core_data *cd)
  7024. {
  7025. #ifndef EASYWAKE_TSG6
  7026. /* TSG5 EasyWake */
  7027. int rc = 0;
  7028. int event_id;
  7029. int size = get_unaligned_le16(&cd->input_buf[0]);
  7030. /* Validate report */
  7031. if (size != 4 || cd->input_buf[2] != 4)
  7032. rc = -EINVAL;
  7033. event_id = cd->input_buf[3];
  7034. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7035. __func__, event_id, rc);
  7036. if (rc) {
  7037. pt_core_sleep_(cd);
  7038. goto exit;
  7039. }
  7040. /* attention WAKE */
  7041. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7042. exit:
  7043. return rc;
  7044. #else
  7045. /* TSG6 FW1.3 EasyWake */
  7046. int rc = 0;
  7047. int i = 0;
  7048. int report_length;
  7049. /* Validate report */
  7050. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7051. rc = -EINVAL;
  7052. pt_core_sleep_(cd);
  7053. goto exit;
  7054. }
  7055. /* Get gesture id and gesture data length */
  7056. cd->gesture_id = cd->input_buf[3];
  7057. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7058. cd->gesture_data_length = report_length - 4;
  7059. pt_debug(cd->dev, DL_INFO,
  7060. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7061. __func__, cd->gesture_id, cd->gesture_data_length);
  7062. for (i = 0; i < cd->gesture_data_length; i++)
  7063. cd->gesture_data[i] = cd->input_buf[4 + i];
  7064. /* attention WAKE */
  7065. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7066. exit:
  7067. return rc;
  7068. #endif
  7069. }
  7070. /*******************************************************************************
  7071. * FUNCTION: pt_get_touch_axis
  7072. *
  7073. * SUMMARY: Function to calculate touch axis
  7074. *
  7075. * PARAMETERS:
  7076. * *cd - pointer to core data structure
  7077. * *axis - pointer to axis calculation result
  7078. * size - size in bytes
  7079. * max - max value of result
  7080. * *xy_data - pointer to input data to be parsed
  7081. * bofs - bit offset
  7082. ******************************************************************************/
  7083. static void pt_get_touch_axis(struct pt_core_data *cd,
  7084. int *axis, int size, int max, u8 *data, int bofs)
  7085. {
  7086. int nbyte;
  7087. int next;
  7088. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7089. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7090. next++;
  7091. }
  7092. *axis &= max - 1;
  7093. }
  7094. /*******************************************************************************
  7095. * FUNCTION: move_tracking_heatmap_data
  7096. *
  7097. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7098. * system information structure, xy_mode and xy_data.
  7099. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7100. * the tthe_tuner sysfs node under the label "THM"
  7101. *
  7102. * RETURN:
  7103. * 0 = success
  7104. * !0 = failure
  7105. *
  7106. * PARAMETERS:
  7107. * *cd - pointer to core data
  7108. * *si - pointer to the system information structure
  7109. ******************************************************************************/
  7110. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7111. struct pt_sysinfo *si)
  7112. {
  7113. #ifdef TTHE_TUNER_SUPPORT
  7114. int size = get_unaligned_le16(&cd->input_buf[0]);
  7115. if (size)
  7116. tthe_print(cd, cd->input_buf, size, "THM=");
  7117. #endif
  7118. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7119. return 0;
  7120. }
  7121. /*******************************************************************************
  7122. * FUNCTION: move_sensor_data
  7123. *
  7124. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7125. * information structure, xy_mode and xy_data.
  7126. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7127. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7128. *
  7129. * RETURN:
  7130. * 0 = success
  7131. * !0 = failure
  7132. *
  7133. * PARAMETERS:
  7134. * *cd - pointer to core data
  7135. * *si - pointer to the system information structure
  7136. ******************************************************************************/
  7137. static int move_sensor_data(struct pt_core_data *cd,
  7138. struct pt_sysinfo *si)
  7139. {
  7140. #ifdef TTHE_TUNER_SUPPORT
  7141. int size = get_unaligned_le16(&cd->input_buf[0]);
  7142. if (size)
  7143. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7144. #endif
  7145. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7146. return 0;
  7147. }
  7148. /*******************************************************************************
  7149. * FUNCTION: move_button_data
  7150. *
  7151. * SUMMARY: Move the valid button data from the input buffer into the system
  7152. * information structure, xy_mode and xy_data.
  7153. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7154. * the tthe_tuner sysfs node under the label "OpModeData"
  7155. *
  7156. * RETURN:
  7157. * 0 = success
  7158. * !0 = failure
  7159. *
  7160. * PARAMETERS:
  7161. * *cd - pointer to core data
  7162. * *si - pointer to the system information structure
  7163. ******************************************************************************/
  7164. static int move_button_data(struct pt_core_data *cd,
  7165. struct pt_sysinfo *si)
  7166. {
  7167. #ifdef TTHE_TUNER_SUPPORT
  7168. int size = get_unaligned_le16(&cd->input_buf[0]);
  7169. if (size)
  7170. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7171. #endif
  7172. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7173. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7174. "xy_mode");
  7175. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7176. BTN_REPORT_SIZE);
  7177. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7178. "xy_data");
  7179. return 0;
  7180. }
  7181. /*******************************************************************************
  7182. * FUNCTION: move_touch_data
  7183. *
  7184. * SUMMARY: Move the valid touch data from the input buffer into the system
  7185. * information structure, xy_mode and xy_data.
  7186. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7187. * the tthe_tuner sysfs node under the label "OpModeData"
  7188. *
  7189. * RETURN:
  7190. * 0 = success
  7191. * !0 = failure
  7192. *
  7193. * PARAMETERS:
  7194. * *cd - pointer to core data
  7195. * *si - pointer to the system information structure
  7196. ******************************************************************************/
  7197. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7198. {
  7199. int max_tch = si->sensing_conf_data.max_tch;
  7200. int num_cur_tch;
  7201. int length;
  7202. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7203. #ifdef TTHE_TUNER_SUPPORT
  7204. int size = get_unaligned_le16(&cd->input_buf[0]);
  7205. if (size)
  7206. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7207. #endif
  7208. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7209. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7210. si->desc.tch_header_size, "xy_mode");
  7211. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7212. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7213. if (unlikely(num_cur_tch > max_tch))
  7214. num_cur_tch = max_tch;
  7215. length = num_cur_tch * si->desc.tch_record_size;
  7216. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7217. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7218. return 0;
  7219. }
  7220. /*******************************************************************************
  7221. * FUNCTION: move_hid_pen_data
  7222. *
  7223. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7224. * information structure, xy_mode and xy_data.
  7225. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7226. * the tthe_tuner sysfs node under the label "HID" starting with the
  7227. * report ID.
  7228. *
  7229. * RETURN:
  7230. * 0 = success
  7231. * !0 = failure
  7232. *
  7233. * PARAMETERS:
  7234. * *cd - pointer to core data
  7235. * *si - pointer to the system information structure
  7236. ******************************************************************************/
  7237. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7238. {
  7239. #ifdef TTHE_TUNER_SUPPORT
  7240. int size = get_unaligned_le16(&cd->input_buf[0]);
  7241. if (size) {
  7242. /*
  7243. * HID over USB does not require the two byte length field, so
  7244. * this should print from input_buf[2] but to keep both finger
  7245. * and pen reports the same the length is included
  7246. */
  7247. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7248. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7249. "HID-USB=");
  7250. else
  7251. tthe_print(cd, &(cd->input_buf[0]), size,
  7252. "HID-I2C=");
  7253. }
  7254. #endif
  7255. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7256. return 0;
  7257. }
  7258. /*******************************************************************************
  7259. * FUNCTION: parse_touch_input
  7260. *
  7261. * SUMMARY: Parse the touch report and take action based on the touch
  7262. * report_id.
  7263. *
  7264. * RETURN:
  7265. * 0 = success
  7266. * !0 = failure
  7267. *
  7268. * PARAMETERS:
  7269. * *cd - pointer to core data
  7270. * size - size of touch record
  7271. ******************************************************************************/
  7272. static int parse_touch_input(struct pt_core_data *cd, int size)
  7273. {
  7274. struct pt_sysinfo *si = &cd->sysinfo;
  7275. int report_id = cd->input_buf[2];
  7276. int rc = -EINVAL;
  7277. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7278. __func__);
  7279. if (!si->ready) {
  7280. pt_debug(cd->dev, DL_ERROR,
  7281. "%s: Need system information to parse touches\n",
  7282. __func__);
  7283. return 0;
  7284. }
  7285. if (!si->xy_mode || !si->xy_data)
  7286. return rc;
  7287. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7288. rc = move_touch_data(cd, si);
  7289. else if (report_id == PT_HID_PEN_REPORT_ID)
  7290. rc = move_hid_pen_data(cd, si);
  7291. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7292. rc = move_button_data(cd, si);
  7293. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7294. rc = move_sensor_data(cd, si);
  7295. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7296. rc = move_tracking_heatmap_data(cd, si);
  7297. if (rc)
  7298. return rc;
  7299. /* attention IRQ */
  7300. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7301. return 0;
  7302. }
  7303. /*******************************************************************************
  7304. * FUNCTION: parse_command_input
  7305. *
  7306. * SUMMARY: Move the response data from the input buffer to the response buffer
  7307. *
  7308. * RETURN:
  7309. * 0 = success
  7310. * !0 = failure
  7311. *
  7312. * PARAMETERS:
  7313. * *cd - pointer to core data
  7314. * size - size of response data
  7315. ******************************************************************************/
  7316. static int parse_command_input(struct pt_core_data *cd, int size)
  7317. {
  7318. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7319. __func__);
  7320. memcpy(cd->response_buf, cd->input_buf, size);
  7321. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7322. if (size && cd->show_tt_data) {
  7323. if (cd->pip2_prot_active)
  7324. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7325. else
  7326. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7327. }
  7328. #endif
  7329. mutex_lock(&cd->system_lock);
  7330. cd->hid_cmd_state = 0;
  7331. mutex_unlock(&cd->system_lock);
  7332. wake_up(&cd->wait_q);
  7333. return 0;
  7334. }
  7335. /*******************************************************************************
  7336. * FUNCTION: pt_allow_enumeration
  7337. *
  7338. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7339. * FW sentinel is seen.
  7340. *
  7341. * RETURN:
  7342. * true = allow enumeration or fully re-probe
  7343. * false = skip enumeration and fully re-probe
  7344. *
  7345. * PARAMETERS:
  7346. * *cd - pointer to core data
  7347. ******************************************************************************/
  7348. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7349. {
  7350. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7351. (!cd->hid_reset_cmd_state) &&
  7352. (cd->core_probe_complete) &&
  7353. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7354. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7355. (cd->mode == PT_MODE_OPERATIONAL)) {
  7356. return true;
  7357. }
  7358. if ((!cd->hid_reset_cmd_state) &&
  7359. (cd->core_probe_complete) &&
  7360. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7361. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7362. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7363. return true;
  7364. }
  7365. pt_debug(cd->dev, DL_INFO,
  7366. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7367. __func__,
  7368. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7369. "core_probe_complete(1)", cd->core_probe_complete,
  7370. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7371. "active_dut_gen(0,2)", cd->active_dut_generation);
  7372. return false;
  7373. }
  7374. /*******************************************************************************
  7375. * FUNCTION: pt_is_touch_report
  7376. *
  7377. * SUMMARY: Determine if a report ID should be treated as a touch report
  7378. *
  7379. * RETURN:
  7380. * true = report ID is a touch report
  7381. * false = report ID is not a touch report
  7382. *
  7383. * PARAMETERS:
  7384. * id - Report ID
  7385. ******************************************************************************/
  7386. static bool pt_is_touch_report(int id)
  7387. {
  7388. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7389. id == PT_HID_PEN_REPORT_ID ||
  7390. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7391. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7392. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7393. return true;
  7394. else
  7395. return false;
  7396. }
  7397. /*******************************************************************************
  7398. * FUNCTION: pt_parse_input
  7399. *
  7400. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7401. * on if its a response to a command or asynchronous touch data / reset
  7402. * sentinel. PIP2.x messages have additional error checking that is
  7403. * parsed (SEQ match from cmd to rsp, CRC valid).
  7404. * Look for special packets based on unique lengths:
  7405. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7406. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7407. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7408. * 0xFFXX - Empty buffer (PIP 1.7+)
  7409. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7410. * the initial probe has not yet been done.
  7411. *
  7412. * RETURN:
  7413. * 0 = success
  7414. * !0 = failure
  7415. *
  7416. * PARAMETERS:
  7417. * *cd - pointer to core data
  7418. ******************************************************************************/
  7419. static int pt_parse_input(struct pt_core_data *cd)
  7420. {
  7421. int report_id;
  7422. int cmd_id;
  7423. int is_command = 0;
  7424. int size;
  7425. int print_size;
  7426. bool touch_report = true;
  7427. unsigned short calc_crc;
  7428. unsigned short resp_crc;
  7429. cd->fw_sys_mode_in_standby_state = false;
  7430. size = get_unaligned_le16(&cd->input_buf[0]);
  7431. print_size = size;
  7432. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7433. __func__, print_size);
  7434. if (print_size <= PT_MAX_INPUT)
  7435. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7436. "<<< Read buf");
  7437. if (size == 0 ||
  7438. (size == 11 &&
  7439. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7440. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7441. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7442. PIP2_CMD_COMMAND_ID_MASK) ==
  7443. PIP2_CMD_ID_STATUS)) {
  7444. touch_report = false;
  7445. cd->hw_detected = true;
  7446. cd->bl_pip_ver_ready = false;
  7447. cd->app_pip_ver_ready = false;
  7448. if (size == 0) {
  7449. mutex_lock(&cd->system_lock);
  7450. cd->pip2_prot_active = false;
  7451. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7452. /*
  7453. * For Gen5/6 this sentinel could be from
  7454. * the BL or FW. Attempt to set the correct
  7455. * mode based on the previous PIP command.
  7456. */
  7457. if (cd->hid_cmd_state ==
  7458. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7459. cd->mode = PT_MODE_OPERATIONAL;
  7460. cd->startup_status =
  7461. STARTUP_STATUS_FW_RESET_SENTINEL;
  7462. } else if (cd->hid_cmd_state ==
  7463. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7464. cd->hid_reset_cmd_state) {
  7465. cd->mode = PT_MODE_BOOTLOADER;
  7466. cd->startup_status =
  7467. STARTUP_STATUS_BL_RESET_SENTINEL;
  7468. } else {
  7469. cd->mode = PT_MODE_UNKNOWN;
  7470. cd->startup_status =
  7471. STARTUP_STATUS_START;
  7472. }
  7473. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7474. pt_debug(cd->dev, DL_INFO,
  7475. "%s: ATM Gen5/6 %s sentinel received\n",
  7476. __func__,
  7477. (cd->mode == PT_MODE_OPERATIONAL ?
  7478. "FW" :
  7479. (cd->mode == PT_MODE_BOOTLOADER ?
  7480. "BL" : "Unknown")));
  7481. } else {
  7482. cd->mode = PT_MODE_OPERATIONAL;
  7483. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7484. cd->startup_status =
  7485. STARTUP_STATUS_FW_RESET_SENTINEL;
  7486. pt_debug(cd->dev, DL_INFO,
  7487. "%s: ATM PT/TT FW sentinel received\n",
  7488. __func__);
  7489. }
  7490. mutex_unlock(&cd->system_lock);
  7491. if (pt_allow_enumeration(cd)) {
  7492. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7493. pt_debug(cd->dev, DL_INFO,
  7494. "%s: Queue Restart\n", __func__);
  7495. pt_queue_restart(cd);
  7496. } else {
  7497. pt_debug(cd->dev, DL_INFO,
  7498. "%s: Queue Enum\n", __func__);
  7499. pt_queue_enum(cd);
  7500. }
  7501. } else {
  7502. pt_debug(cd->dev, DL_INFO,
  7503. "%s: Sentinel - No Queued Action\n",
  7504. __func__);
  7505. }
  7506. } else {
  7507. /* Sentinel must be from TT/TC BL */
  7508. mutex_lock(&cd->system_lock);
  7509. cd->pip2_prot_active = true;
  7510. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7511. cd->mode = PT_MODE_BOOTLOADER;
  7512. cd->sysinfo.ready = false;
  7513. mutex_unlock(&cd->system_lock);
  7514. pt_debug(cd->dev, DL_INFO,
  7515. "%s: BL Reset sentinel received\n", __func__);
  7516. if (cd->flashless_dut &&
  7517. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7518. pt_debug(cd->dev, DL_INFO,
  7519. "%s: BL to RAM for flashless DUT\n",
  7520. __func__);
  7521. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7522. }
  7523. }
  7524. mutex_lock(&cd->system_lock);
  7525. memcpy(cd->response_buf, cd->input_buf, 2);
  7526. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7527. mutex_unlock(&cd->system_lock);
  7528. pt_debug(cd->dev, DL_WARN,
  7529. "%s: Device Initiated Reset\n", __func__);
  7530. wake_up(&cd->wait_q);
  7531. return 0;
  7532. }
  7533. cd->hid_reset_cmd_state = 0;
  7534. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7535. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7536. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7537. cd->hid_cmd_state = 0;
  7538. wake_up(&cd->wait_q);
  7539. mutex_unlock(&cd->system_lock);
  7540. return 0;
  7541. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7542. /*
  7543. * This debug message below is used by PBATS to calculate the
  7544. * time from the last lift off IRQ to when FW exits LFT mode.
  7545. */
  7546. touch_report = false;
  7547. pt_debug(cd->dev, DL_WARN,
  7548. "%s: DUT - Empty buffer detected\n", __func__);
  7549. return 0;
  7550. } else if (size > PT_MAX_INPUT) {
  7551. pt_debug(cd->dev, DL_ERROR,
  7552. "%s: DUT - Unexpected len field in active bus data!\n",
  7553. __func__);
  7554. return -EINVAL;
  7555. }
  7556. if (cd->pip2_prot_active) {
  7557. pt_debug(cd->dev, DL_DEBUG,
  7558. "%s: Decode PIP2.x Response\n", __func__);
  7559. /* PIP2 does not have a report id, hard code it */
  7560. report_id = 0x00;
  7561. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7562. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7563. resp_crc = cd->input_buf[size - 2] << 8;
  7564. resp_crc |= cd->input_buf[size - 1];
  7565. if ((cd->pip2_cmd_tag_seq !=
  7566. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7567. (resp_crc != calc_crc) &&
  7568. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7569. == PT_PIP_TOUCH_REPORT_ID) ||
  7570. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7571. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7572. pt_debug(cd->dev, DL_WARN,
  7573. "%s: %s %d %s\n",
  7574. __func__,
  7575. "Received PIP1 report id =",
  7576. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7577. "when expecting a PIP2 report - IGNORE report");
  7578. return 0;
  7579. }
  7580. is_command = 1;
  7581. touch_report = false;
  7582. } else {
  7583. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7584. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7585. }
  7586. #ifdef TTDL_DIAGNOSTICS
  7587. pt_debug(cd->dev, DL_DEBUG,
  7588. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7589. __func__, cd->pip2_prot_active, report_id,
  7590. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7591. #endif /* TTDL_DIAGNOSTICS */
  7592. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7593. pt_wakeup_host(cd);
  7594. #ifdef TTHE_TUNER_SUPPORT
  7595. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7596. #endif
  7597. return 0;
  7598. }
  7599. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7600. msecs_to_jiffies(cd->watchdog_interval));
  7601. /*
  7602. * If it is PIP2 response, the report_id has been set to 0,
  7603. * so it will not be parsed as a touch packet.
  7604. */
  7605. if (!pt_is_touch_report(report_id)) {
  7606. is_command = 1;
  7607. touch_report = false;
  7608. }
  7609. if (unlikely(is_command)) {
  7610. parse_command_input(cd, size);
  7611. return 0;
  7612. }
  7613. if (touch_report)
  7614. parse_touch_input(cd, size);
  7615. return 0;
  7616. }
  7617. /*******************************************************************************
  7618. * FUNCTION: pt_read_input
  7619. *
  7620. * SUMMARY: Reads incoming data off of the active bus
  7621. *
  7622. * RETURN:
  7623. * 0 = success
  7624. * !0 = failure
  7625. *
  7626. * PARAMETERS:
  7627. * *cd - pointer to core data
  7628. ******************************************************************************/
  7629. static int pt_read_input(struct pt_core_data *cd)
  7630. {
  7631. struct device *dev = cd->dev;
  7632. int rc = 0;
  7633. int t;
  7634. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7635. /*
  7636. * Workaround for easywake failure
  7637. * Interrupt for easywake, wait for bus controller to wake
  7638. */
  7639. mutex_lock(&cd->system_lock);
  7640. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7641. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_EASY_WAKING_ON) {
  7642. mutex_unlock(&cd->system_lock);
  7643. if (!dev->power.is_suspended)
  7644. goto read;
  7645. t = wait_event_timeout(cd->wait_q,
  7646. (cd->wait_until_wake == 1),
  7647. msecs_to_jiffies(2000));
  7648. #ifdef TTDL_DIAGNOSTICS
  7649. if (IS_TMO(t)) {
  7650. cd->bus_transmit_error_count++;
  7651. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7652. pt_debug(dev, DL_ERROR,
  7653. "%s: !!!I2C Transmission Error %d\n",
  7654. __func__,
  7655. cd->bus_transmit_error_count);
  7656. }
  7657. #else
  7658. if (IS_TMO(t))
  7659. pt_queue_enum(cd);
  7660. #endif /* TTDL_DIAGNOSTICS */
  7661. goto read;
  7662. }
  7663. }
  7664. mutex_unlock(&cd->system_lock);
  7665. read:
  7666. /* Try reading up to 'retry' times */
  7667. while (retry-- != 0) {
  7668. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7669. PT_MAX_INPUT);
  7670. if (!rc) {
  7671. pt_debug(dev, DL_DEBUG,
  7672. "%s: Read input successfully\n", __func__);
  7673. goto read_exit;
  7674. }
  7675. usleep_range(1000, 2000);
  7676. }
  7677. pt_debug(dev, DL_ERROR,
  7678. "%s: Error getting report, rc=%d\n", __func__, rc);
  7679. read_exit:
  7680. return rc;
  7681. }
  7682. /*******************************************************************************
  7683. * FUNCTION: pt_irq
  7684. *
  7685. * SUMMARY: Process all detected interrupts
  7686. *
  7687. * RETURN:
  7688. * IRQ_HANDLED - Finished processing the interrupt
  7689. *
  7690. * PARAMETERS:
  7691. * irq - IRQ number
  7692. * *handle - pointer to core data struct
  7693. ******************************************************************************/
  7694. irqreturn_t pt_irq(int irq, void *handle)
  7695. {
  7696. struct pt_core_data *cd = handle;
  7697. int rc = 0;
  7698. if (!pt_check_irq_asserted(cd))
  7699. return IRQ_HANDLED;
  7700. rc = pt_read_input(cd);
  7701. #ifdef TTDL_DIAGNOSTICS
  7702. cd->irq_count++;
  7703. /* Used to calculate T-Refresh */
  7704. if (cd->t_refresh_active) {
  7705. if (cd->t_refresh_count == 0) {
  7706. cd->t_refresh_time = jiffies;
  7707. cd->t_refresh_count++;
  7708. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7709. cd->t_refresh_count++;
  7710. } else {
  7711. cd->t_refresh_active = 0;
  7712. cd->t_refresh_time =
  7713. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7714. }
  7715. }
  7716. #endif /* TTDL_DIAGNOSTICS */
  7717. if (!rc)
  7718. pt_parse_input(cd);
  7719. return IRQ_HANDLED;
  7720. }
  7721. /*******************************************************************************
  7722. * FUNCTION: _pt_subscribe_attention
  7723. *
  7724. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7725. * to subscribe themselves into the TTDL attention list
  7726. *
  7727. * RETURN:
  7728. * 0 = success
  7729. * !0 = failure
  7730. *
  7731. * PARAMETERS:
  7732. * *dev - pointer to device structure
  7733. * type - attention type enum
  7734. * *id - ID of the module calling this function
  7735. * *func - callback function pointer to be called when notified
  7736. * mode - attention mode
  7737. ******************************************************************************/
  7738. int _pt_subscribe_attention(struct device *dev,
  7739. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7740. int mode)
  7741. {
  7742. struct pt_core_data *cd = dev_get_drvdata(dev);
  7743. struct atten_node *atten, *atten_new;
  7744. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7745. if (!atten_new)
  7746. return -ENOMEM;
  7747. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7748. dev_name(cd->dev));
  7749. spin_lock(&cd->spinlock);
  7750. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7751. if (atten->id == id && atten->mode == mode) {
  7752. spin_unlock(&cd->spinlock);
  7753. kfree(atten_new);
  7754. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7755. __func__,
  7756. "already subscribed attention",
  7757. dev, "mode", mode);
  7758. return 0;
  7759. }
  7760. }
  7761. atten_new->id = id;
  7762. atten_new->dev = dev;
  7763. atten_new->mode = mode;
  7764. atten_new->func = func;
  7765. list_add(&atten_new->node, &cd->atten_list[type]);
  7766. spin_unlock(&cd->spinlock);
  7767. return 0;
  7768. }
  7769. /*******************************************************************************
  7770. * FUNCTION: _pt_unsubscribe_attention
  7771. *
  7772. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7773. * to unsubscribe themselves from the TTDL attention list
  7774. *
  7775. * RETURN:
  7776. * 0 = success
  7777. * !0 = failure
  7778. *
  7779. * PARAMETERS:
  7780. * *dev - pointer to device structure
  7781. * type - attention type enum
  7782. * *id - ID of the module calling this function
  7783. * *func - function pointer
  7784. * mode - attention mode
  7785. ******************************************************************************/
  7786. int _pt_unsubscribe_attention(struct device *dev,
  7787. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7788. int mode)
  7789. {
  7790. struct pt_core_data *cd = dev_get_drvdata(dev);
  7791. struct atten_node *atten, *atten_n;
  7792. spin_lock(&cd->spinlock);
  7793. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7794. if (atten->id == id && atten->mode == mode) {
  7795. list_del(&atten->node);
  7796. spin_unlock(&cd->spinlock);
  7797. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7798. __func__,
  7799. "unsub for atten->dev", atten->dev,
  7800. "atten->mode", atten->mode);
  7801. kfree(atten);
  7802. return 0;
  7803. }
  7804. }
  7805. spin_unlock(&cd->spinlock);
  7806. return -ENODEV;
  7807. }
  7808. /*******************************************************************************
  7809. * FUNCTION: _pt_request_exclusive
  7810. *
  7811. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7812. * to request exclusive access
  7813. *
  7814. * RETURN:
  7815. * 0 = success
  7816. * !0 = failure
  7817. *
  7818. * PARAMETERS:
  7819. * *dev - pointer to device structure
  7820. * timeout_ms - timeout to wait for exclusive access
  7821. ******************************************************************************/
  7822. static int _pt_request_exclusive(struct device *dev,
  7823. int timeout_ms)
  7824. {
  7825. struct pt_core_data *cd = dev_get_drvdata(dev);
  7826. return request_exclusive(cd, (void *)dev, timeout_ms);
  7827. }
  7828. /*******************************************************************************
  7829. * FUNCTION: _pt_release_exclusive
  7830. *
  7831. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7832. * to release exclusive access
  7833. *
  7834. * RETURN:
  7835. * 0 = success
  7836. * !0 = failure
  7837. *
  7838. * PARAMETERS:
  7839. * *dev - pointer to device structure
  7840. ******************************************************************************/
  7841. static int _pt_release_exclusive(struct device *dev)
  7842. {
  7843. struct pt_core_data *cd = dev_get_drvdata(dev);
  7844. return release_exclusive(cd, (void *)dev);
  7845. }
  7846. /*******************************************************************************
  7847. * FUNCTION: _pt_request_reset
  7848. *
  7849. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7850. * to request the DUT to be reset. Function returns err if refused or
  7851. * timeout occurs (Note: core uses fixed timeout period).
  7852. *
  7853. * NOTE: Function blocks until ISR occurs.
  7854. *
  7855. * RETURN:
  7856. * 0 = success
  7857. * !0 = failure
  7858. *
  7859. * PARAMETERS:
  7860. * *dev - pointer to device structure
  7861. * protect - flag to call protected or non-protected
  7862. ******************************************************************************/
  7863. static int _pt_request_reset(struct device *dev, int protect)
  7864. {
  7865. struct pt_core_data *cd = dev_get_drvdata(dev);
  7866. int rc;
  7867. rc = pt_dut_reset(cd, protect);
  7868. if (rc < 0) {
  7869. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7870. __func__, rc);
  7871. }
  7872. return rc;
  7873. }
  7874. /*******************************************************************************
  7875. * FUNCTION: _pt_request_enum
  7876. *
  7877. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7878. * to request TTDL to queue an enum. This function will return err
  7879. * if refused; if no error then enum has completed and system is in
  7880. * normal operation mode.
  7881. *
  7882. * RETURN:
  7883. * 0 = success
  7884. * !0 = failure
  7885. *
  7886. * PARAMETERS:
  7887. * *dev - pointer to device structure
  7888. * wait - boolean to determine if to wait for startup event
  7889. ******************************************************************************/
  7890. static int _pt_request_enum(struct device *dev, bool wait)
  7891. {
  7892. struct pt_core_data *cd = dev_get_drvdata(dev);
  7893. pt_queue_enum(cd);
  7894. if (wait)
  7895. wait_event_timeout(cd->wait_q,
  7896. cd->startup_state == STARTUP_NONE,
  7897. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7898. return 0;
  7899. }
  7900. /*******************************************************************************
  7901. * FUNCTION: _pt_request_sysinfo
  7902. *
  7903. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7904. * to request the pointer to the system information structure. This
  7905. * function will return NULL if sysinfo has not been acquired from the
  7906. * DUT yet.
  7907. *
  7908. * RETURN: Pointer to the system information struct
  7909. *
  7910. * PARAMETERS:
  7911. * *dev - pointer to device structure
  7912. ******************************************************************************/
  7913. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7914. {
  7915. struct pt_core_data *cd = dev_get_drvdata(dev);
  7916. int rc = 0;
  7917. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7918. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7919. !cd->sysinfo.ready) {
  7920. rc = pt_hid_output_get_sysinfo_(cd);
  7921. if (rc) {
  7922. pt_debug(cd->dev, DL_ERROR,
  7923. "%s: Error getting sysinfo rc=%d\n",
  7924. __func__, rc);
  7925. }
  7926. }
  7927. if (cd->sysinfo.ready)
  7928. return &cd->sysinfo;
  7929. return NULL;
  7930. }
  7931. /*******************************************************************************
  7932. * FUNCTION: _pt_request_loader_pdata
  7933. *
  7934. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7935. * to request the pointer to the loader platform data
  7936. *
  7937. * RETURN: Pointer to the loader platform data struct
  7938. *
  7939. * PARAMETERS:
  7940. * *dev - pointer to device structure
  7941. ******************************************************************************/
  7942. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7943. struct device *dev)
  7944. {
  7945. struct pt_core_data *cd = dev_get_drvdata(dev);
  7946. return cd->pdata->loader_pdata;
  7947. }
  7948. /*******************************************************************************
  7949. * FUNCTION: _pt_request_start_wd
  7950. *
  7951. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7952. * to request to start the TTDL watchdog
  7953. *
  7954. * RETURN:
  7955. * 0 = success
  7956. * !0 = failure
  7957. *
  7958. * PARAMETERS:
  7959. * *dev - pointer to device structure
  7960. ******************************************************************************/
  7961. static int _pt_request_start_wd(struct device *dev)
  7962. {
  7963. struct pt_core_data *cd = dev_get_drvdata(dev);
  7964. pt_start_wd_timer(cd);
  7965. return 0;
  7966. }
  7967. /*******************************************************************************
  7968. * FUNCTION: _pt_request_stop_wd
  7969. *
  7970. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7971. * to request to stop the TTDL watchdog
  7972. *
  7973. * RETURN:
  7974. * 0 = success
  7975. * !0 = failure
  7976. *
  7977. * PARAMETERS:
  7978. * *dev - pointer to device structure
  7979. ******************************************************************************/
  7980. static int _pt_request_stop_wd(struct device *dev)
  7981. {
  7982. struct pt_core_data *cd = dev_get_drvdata(dev);
  7983. pt_stop_wd_timer(cd);
  7984. return 0;
  7985. }
  7986. /*******************************************************************************
  7987. * FUNCTION: pt_pip2_launch_app
  7988. *
  7989. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7990. * the FW reset sentinel to indicate the function succeeded.
  7991. *
  7992. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7993. * in a timeout delay and with the DUT being reset with an XRES.
  7994. *
  7995. * RETURN:
  7996. * 0 = success
  7997. * !0 = failure
  7998. *
  7999. * PARAMETERS:
  8000. * *cd - pointer to core data structure
  8001. * protect - flag to call protected or non-protected
  8002. ******************************************************************************/
  8003. static int pt_pip2_launch_app(struct device *dev, int protect)
  8004. {
  8005. struct pt_core_data *cd = dev_get_drvdata(dev);
  8006. u16 actual_read_len;
  8007. u16 tmp_startup_status = cd->startup_status;
  8008. u8 read_buf[12];
  8009. u8 status;
  8010. int time = 0;
  8011. int rc = 0;
  8012. mutex_lock(&cd->system_lock);
  8013. cd->startup_status = STARTUP_STATUS_START;
  8014. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8015. mutex_unlock(&cd->system_lock);
  8016. rc = _pt_request_pip2_send_cmd(dev, protect,
  8017. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8018. &actual_read_len);
  8019. if (rc) {
  8020. pt_debug(dev, DL_ERROR,
  8021. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8022. __func__, rc);
  8023. } else {
  8024. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8025. /* Test for no or invalid image in FLASH, no point to reset */
  8026. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8027. rc = status;
  8028. goto exit;
  8029. }
  8030. /* Any other boot failure */
  8031. if (status != 0) {
  8032. pt_debug(dev, DL_ERROR,
  8033. "%s: FW did not EXECUTE, status = %d\n",
  8034. __func__, status);
  8035. rc = status;
  8036. }
  8037. }
  8038. if (rc) {
  8039. pt_debug(dev, DL_ERROR,
  8040. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8041. __func__, rc);
  8042. goto exit;
  8043. }
  8044. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8045. msleep(20);
  8046. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8047. __func__, time, cd->startup_status);
  8048. time += 20;
  8049. }
  8050. if (cd->startup_status == STARTUP_STATUS_START) {
  8051. pt_debug(cd->dev, DL_WARN,
  8052. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8053. rc = -ETIME;
  8054. }
  8055. exit:
  8056. if (cd->startup_status == STARTUP_STATUS_START) {
  8057. /* Reset to original state because we could be stuck in BL */
  8058. mutex_lock(&cd->system_lock);
  8059. cd->startup_status = tmp_startup_status;
  8060. mutex_unlock(&cd->system_lock);
  8061. }
  8062. return rc;
  8063. }
  8064. /*******************************************************************************
  8065. * FUNCTION: _pt_request_pip2_launch_app
  8066. *
  8067. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8068. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8069. * consumed.
  8070. *
  8071. * RETURN:
  8072. * 0 = success
  8073. * !0 = failure
  8074. *
  8075. * PARAMETERS:
  8076. * *cd - pointer to core data structure
  8077. ******************************************************************************/
  8078. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8079. {
  8080. return pt_pip2_launch_app(dev, protect);
  8081. }
  8082. /*******************************************************************************
  8083. * FUNCTION: _pt_request_wait_for_enum_state
  8084. *
  8085. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8086. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8087. *
  8088. * RETURN:
  8089. * 0 = success
  8090. * !0 = failure
  8091. *
  8092. * PARAMETERS:
  8093. * *cd - pointer to core data structure
  8094. * timeout - timeout for how long to wait
  8095. * state - enum state to wait for
  8096. ******************************************************************************/
  8097. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8098. int state)
  8099. {
  8100. int rc = 0;
  8101. int t;
  8102. struct pt_core_data *cd = dev_get_drvdata(dev);
  8103. t = wait_event_timeout(cd->wait_q,
  8104. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8105. msecs_to_jiffies(timeout));
  8106. if (IS_TMO(t)) {
  8107. pt_debug(cd->dev, DL_ERROR,
  8108. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8109. __func__, state, timeout);
  8110. pt_debug(cd->dev, DL_WARN,
  8111. "%s: enum state reached 0x%04X\n",
  8112. __func__, cd->startup_status);
  8113. rc = -ETIME;
  8114. } else if (cd->startup_status & state) {
  8115. pt_debug(cd->dev, DL_INFO,
  8116. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8117. __func__, cd->startup_status,
  8118. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8119. } else {
  8120. if (t == 1) {
  8121. pt_debug(
  8122. cd->dev, DL_ERROR,
  8123. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8124. __func__, state, timeout);
  8125. rc = -ETIME;
  8126. } else {
  8127. pt_debug(
  8128. cd->dev, DL_ERROR,
  8129. "%s: Enum state 0x%04X not reached in %dms\n",
  8130. __func__, state, timeout - jiffies_to_msecs(t));
  8131. rc = -EINVAL;
  8132. }
  8133. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8134. __func__, cd->startup_status);
  8135. }
  8136. return rc;
  8137. }
  8138. /*******************************************************************************
  8139. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8140. *
  8141. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8142. * deep sleep.
  8143. *
  8144. * RETURN:
  8145. * 0 = success
  8146. * !0 = error
  8147. *
  8148. * PARAMETERS:
  8149. * *cd - pointer to core data
  8150. ******************************************************************************/
  8151. static int pt_core_wake_device_from_deep_sleep_(
  8152. struct pt_core_data *cd)
  8153. {
  8154. int rc;
  8155. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8156. if (rc)
  8157. rc = -EAGAIN;
  8158. /* Prevent failure on sequential wake/sleep requests from OS */
  8159. msleep(20);
  8160. return rc;
  8161. }
  8162. /*******************************************************************************
  8163. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8164. *
  8165. * SUMMARY: Wake up device from Easy-Wake state.
  8166. *
  8167. * RETURN:
  8168. * 0 = success
  8169. * !0 = failure
  8170. *
  8171. * PARAMETERS:
  8172. * *cd - pointer to core data
  8173. ******************************************************************************/
  8174. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8175. {
  8176. int rc = 0;
  8177. mutex_lock(&cd->system_lock);
  8178. cd->wait_until_wake = 1;
  8179. mutex_unlock(&cd->system_lock);
  8180. wake_up(&cd->wait_q);
  8181. msleep(20);
  8182. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8183. return rc;
  8184. }
  8185. /*******************************************************************************
  8186. * FUNCTION: pt_restore_parameters_
  8187. *
  8188. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8189. * back to the DUT
  8190. *
  8191. * RETURN:
  8192. * 0 = success
  8193. * !0 = failure
  8194. *
  8195. * PARAMETERS:
  8196. * *cd - pointer the core data structure
  8197. ******************************************************************************/
  8198. static int pt_restore_parameters_(struct pt_core_data *cd)
  8199. {
  8200. struct param_node *param;
  8201. int rc = 0;
  8202. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8203. goto exit;
  8204. spin_lock(&cd->spinlock);
  8205. list_for_each_entry(param, &cd->param_list, node) {
  8206. spin_unlock(&cd->spinlock);
  8207. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8208. __func__, param->id, param->value);
  8209. rc = pt_pip_set_param_(cd, param->id,
  8210. param->value, param->size);
  8211. if (rc)
  8212. goto exit;
  8213. spin_lock(&cd->spinlock);
  8214. }
  8215. spin_unlock(&cd->spinlock);
  8216. exit:
  8217. return rc;
  8218. }
  8219. /*******************************************************************************
  8220. * FUNCTION: pt_pip2_exit_bl_
  8221. *
  8222. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8223. * a DUT that may not have flash and will need FW to be loaded into RAM
  8224. *
  8225. * RETURN:
  8226. * 0 = success
  8227. * !0 = failure
  8228. *
  8229. * PARAMETERS:
  8230. * *cd - pointer the core data structure
  8231. * *status_str - pointer to optional status string buffer
  8232. * buf_size - size of status_str buffer
  8233. ******************************************************************************/
  8234. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8235. {
  8236. int rc;
  8237. int wait_time = 0;
  8238. u8 mode = PT_MODE_UNKNOWN;
  8239. bool load_status_str = false;
  8240. /*
  8241. * Below function has protective call to ensure no enum is still on
  8242. * going, while this kind of protection should be applied widely in
  8243. * future (TODO).
  8244. */
  8245. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8246. if (status_str && buf_size <= 50)
  8247. load_status_str = true;
  8248. if (mode == PT_MODE_BOOTLOADER) {
  8249. if (cd->flashless_dut == 1) {
  8250. rc = pt_hw_hard_reset(cd);
  8251. } else {
  8252. rc = pt_pip2_launch_app(cd->dev,
  8253. PT_CORE_CMD_UNPROTECTED);
  8254. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8255. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8256. __func__, "Invalid image in FLASH rc", rc);
  8257. } else if (rc) {
  8258. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8259. __func__, "Failed to launch app rc", rc);
  8260. }
  8261. }
  8262. if (!rc) {
  8263. if (cd->flashless_dut == 1) {
  8264. /* Wait for BL to complete before enum */
  8265. rc = _pt_request_wait_for_enum_state(cd->dev,
  8266. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8267. if (rc && load_status_str) {
  8268. strlcpy(status_str, "No FW sentinel after BL",
  8269. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8270. goto exit;
  8271. }
  8272. }
  8273. /*
  8274. * If the host wants to interact with the FW or do a
  8275. * forced calibration, the FW must be out of BOOT mode
  8276. * and the system information must have been retrieved.
  8277. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8278. * If, however, the enumeration does not reach this
  8279. * point, the DUT may still be in APP mode so test
  8280. * for all conditions.
  8281. */
  8282. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8283. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8284. if (!rc || cd->startup_status >=
  8285. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8286. mutex_lock(&cd->system_lock);
  8287. cd->mode = PT_MODE_OPERATIONAL;
  8288. mutex_unlock(&cd->system_lock);
  8289. }
  8290. if (rc) {
  8291. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8292. __func__, "Failed to enum with DUT",
  8293. cd->startup_status);
  8294. if (load_status_str && !(cd->startup_status &
  8295. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8296. strlcpy(status_str, "FW Stuck in Boot mode",
  8297. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8298. goto exit;
  8299. }
  8300. }
  8301. /*
  8302. * The coming FW sentinel could wake up the event
  8303. * queue, which has chance to be taken by next command
  8304. * wrongly. Following delay is a workaround to avoid
  8305. * this issue for most situations.
  8306. */
  8307. msleep(20);
  8308. pt_start_wd_timer(cd);
  8309. }
  8310. if (load_status_str) {
  8311. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8312. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8313. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8314. else if (!rc)
  8315. strlcpy(status_str, "Entered APP from BL mode",
  8316. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8317. else
  8318. strlcpy(status_str, "Failed to enter APP from BL mode",
  8319. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8320. }
  8321. } else if (mode == PT_MODE_OPERATIONAL) {
  8322. mutex_lock(&cd->system_lock);
  8323. cd->mode = mode;
  8324. mutex_unlock(&cd->system_lock);
  8325. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8326. if (load_status_str) {
  8327. if (!rc)
  8328. strlcpy(status_str, "Already in APP mode",
  8329. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8330. else
  8331. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8332. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8333. }
  8334. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8335. mutex_lock(&cd->system_lock);
  8336. cd->mode = mode;
  8337. mutex_unlock(&cd->system_lock);
  8338. if (load_status_str)
  8339. strlcpy(status_str, "Failed to determine active mode",
  8340. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8341. }
  8342. exit:
  8343. if (!rc)
  8344. pt_start_wd_timer(cd);
  8345. return rc;
  8346. }
  8347. /*******************************************************************************
  8348. * FUNCTION: pt_pip2_exit_bl
  8349. *
  8350. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8351. * access.
  8352. *
  8353. * RETURN:
  8354. * 0 = success
  8355. * !0 = failure
  8356. *
  8357. * PARAMETERS:
  8358. * *cd - pointer the core data structure
  8359. * *status_str - pointer to optional status string buffer
  8360. * buf_size - size of status_str buffer
  8361. ******************************************************************************/
  8362. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8363. {
  8364. int rc;
  8365. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8366. if (rc < 0) {
  8367. pt_debug(cd->dev, DL_ERROR,
  8368. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8369. cd->exclusive_dev, cd->dev);
  8370. return rc;
  8371. }
  8372. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8373. if (release_exclusive(cd, cd->dev) < 0)
  8374. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8375. __func__);
  8376. return rc;
  8377. }
  8378. /*******************************************************************************
  8379. * FUNCTION: _fast_startup
  8380. *
  8381. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8382. *
  8383. * RETURN:
  8384. * 0 = success
  8385. * !0 = failure
  8386. *
  8387. * PARAMETERS:
  8388. * *cd - pointer the core data structure
  8389. ******************************************************************************/
  8390. static int _fast_startup(struct pt_core_data *cd)
  8391. {
  8392. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8393. int rc = 0;
  8394. u8 mode = PT_MODE_UNKNOWN;
  8395. struct pt_hid_desc hid_desc;
  8396. int wait_time = 0;
  8397. memset(&hid_desc, 0, sizeof(hid_desc));
  8398. reset:
  8399. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8400. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8401. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8402. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8403. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8404. __func__);
  8405. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8406. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8407. if (rc < 0) {
  8408. pt_debug(cd->dev, DL_ERROR,
  8409. "%s: Error on getting HID descriptor r=%d\n",
  8410. __func__, rc);
  8411. if (retry--)
  8412. goto reset;
  8413. goto exit;
  8414. }
  8415. cd->mode = pt_get_mode(cd, &hid_desc);
  8416. if (cd->mode == PT_MODE_BOOTLOADER) {
  8417. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8418. __func__);
  8419. rc = pt_hid_output_bl_launch_app_(cd);
  8420. if (rc < 0) {
  8421. pt_debug(cd->dev, DL_ERROR,
  8422. "%s: Error on launch app r=%d\n",
  8423. __func__, rc);
  8424. if (retry--)
  8425. goto reset;
  8426. goto exit;
  8427. }
  8428. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8429. if (rc < 0) {
  8430. pt_debug(cd->dev, DL_ERROR,
  8431. "%s: Error on getting HID descriptor r=%d\n",
  8432. __func__, rc);
  8433. if (retry--)
  8434. goto reset;
  8435. goto exit;
  8436. }
  8437. cd->mode = pt_get_mode(cd, &hid_desc);
  8438. if (cd->mode == PT_MODE_BOOTLOADER) {
  8439. if (retry--)
  8440. goto reset;
  8441. goto exit;
  8442. }
  8443. }
  8444. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8445. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8446. } else {
  8447. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8448. __func__);
  8449. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8450. /* Wait for any sentinel before first try */
  8451. rc = _pt_request_wait_for_enum_state(
  8452. cd->dev, 150,
  8453. STARTUP_STATUS_BL_RESET_SENTINEL |
  8454. STARTUP_STATUS_FW_RESET_SENTINEL);
  8455. if (rc)
  8456. pt_debug(cd->dev, DL_ERROR,
  8457. "%s: No Sentinel detected rc = %d\n",
  8458. __func__, rc);
  8459. } else
  8460. pt_flush_bus_if_irq_asserted(cd,
  8461. PT_FLUSH_BUS_BASED_ON_LEN);
  8462. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8463. if (rc) {
  8464. pt_debug(cd->dev, DL_ERROR,
  8465. "%s: Get mode failed, mode unknown\n",
  8466. __func__);
  8467. mode = PT_MODE_UNKNOWN;
  8468. }
  8469. cd->mode = mode;
  8470. if (cd->mode == PT_MODE_BOOTLOADER) {
  8471. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8472. __func__);
  8473. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8474. if (rc) {
  8475. pt_debug(cd->dev, DL_ERROR,
  8476. "%s Failed to exit bootloader\n",
  8477. __func__);
  8478. msleep(50);
  8479. rc = -ENODEV;
  8480. if (retry--)
  8481. goto reset;
  8482. goto exit;
  8483. } else {
  8484. pt_debug(cd->dev, DL_INFO,
  8485. "%s: Exit bootloader successfully\n",
  8486. __func__);
  8487. }
  8488. if (cd->mode != PT_MODE_OPERATIONAL) {
  8489. pt_debug(cd->dev, DL_WARN,
  8490. "%s: restore mode failure mode = %d\n",
  8491. __func__, cd->mode);
  8492. if (retry--)
  8493. goto reset;
  8494. goto exit;
  8495. }
  8496. }
  8497. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8498. }
  8499. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8500. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8501. if (!rc) {
  8502. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8503. pt_debug(cd->dev, DL_WARN,
  8504. "%s: Exit FW BOOT Mode after %dms\n",
  8505. __func__, wait_time);
  8506. } else {
  8507. pt_debug(cd->dev, DL_WARN,
  8508. "%s: FW stuck in BOOT Mode after %dms\n",
  8509. __func__, wait_time);
  8510. goto exit;
  8511. }
  8512. if (!cd->sysinfo.ready) {
  8513. rc = pt_hid_output_get_sysinfo_(cd);
  8514. if (rc) {
  8515. pt_debug(cd->dev, DL_ERROR,
  8516. "%s: Error on getting sysinfo r=%d\n",
  8517. __func__, rc);
  8518. if (retry--)
  8519. goto reset;
  8520. goto exit;
  8521. }
  8522. }
  8523. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8524. rc = pt_restore_parameters_(cd);
  8525. if (rc)
  8526. pt_debug(cd->dev, DL_ERROR,
  8527. "%s: failed to restore parameters rc=%d\n",
  8528. __func__, rc);
  8529. else
  8530. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8531. exit:
  8532. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8533. return rc;
  8534. }
  8535. /*******************************************************************************
  8536. * FUNCTION: pt_core_poweron_device_
  8537. *
  8538. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8539. *
  8540. * RETURN:
  8541. * 0 = success
  8542. * !0 = failure
  8543. *
  8544. * PARAMETERS:
  8545. * *cd - pointer to core data
  8546. ******************************************************************************/
  8547. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8548. {
  8549. struct device *dev = cd->dev;
  8550. int rc = 0;
  8551. /*
  8552. * After power on action, the chip can general FW sentinel. It can
  8553. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8554. * _fast_startup() can perform a quick enumeration too, here doesn't
  8555. * need another enumeration.
  8556. */
  8557. mutex_lock(&cd->system_lock);
  8558. cd->startup_status = STARTUP_STATUS_START;
  8559. cd->hid_reset_cmd_state = 1;
  8560. mutex_unlock(&cd->system_lock);
  8561. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8562. if (rc < 0) {
  8563. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8564. __func__, rc);
  8565. goto exit;
  8566. }
  8567. if (!cd->irq_enabled) {
  8568. cd->irq_enabled = true;
  8569. enable_irq(cd->irq);
  8570. }
  8571. /* TBD: following function doesn't update startup_status */
  8572. rc = _fast_startup(cd);
  8573. exit:
  8574. return rc;
  8575. }
  8576. /*******************************************************************************
  8577. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8578. *
  8579. * SUMMARY: Reset device, and then trigger a full enumeration.
  8580. *
  8581. * RETURN:
  8582. * 0 = success
  8583. * !0 = failure
  8584. *
  8585. * PARAMETERS:
  8586. * *cd - pointer to core data
  8587. ******************************************************************************/
  8588. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8589. {
  8590. int rc;
  8591. rc = pt_dut_reset_and_wait(cd);
  8592. if (rc < 0) {
  8593. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8594. __func__, rc);
  8595. goto exit;
  8596. }
  8597. rc = _fast_startup(cd);
  8598. exit:
  8599. return rc;
  8600. }
  8601. /*******************************************************************************
  8602. * FUNCTION: pt_core_easywake_off_
  8603. *
  8604. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8605. * the configuration in the core platform data structure.
  8606. *
  8607. * RETURN:
  8608. * 0 = success
  8609. * !0 = failure
  8610. *
  8611. * PARAMETERS:
  8612. * *cd - pointer to core data
  8613. ******************************************************************************/
  8614. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8615. {
  8616. int rc = 0;
  8617. mutex_lock(&cd->system_lock);
  8618. if (cd->sleep_state == SS_EASY_WAKING_OFF) {
  8619. mutex_unlock(&cd->system_lock);
  8620. pt_debug(cd->dev, DL_INFO,
  8621. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8622. return 0;
  8623. }
  8624. mutex_unlock(&cd->system_lock);
  8625. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8626. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8627. rc = pt_core_wake_device_from_easy_wake_(cd);
  8628. if (rc < 0)
  8629. pt_debug(cd->dev, DL_ERROR,
  8630. "%s - %d failed %d\n", __func__, rc);
  8631. }
  8632. mutex_lock(&cd->system_lock);
  8633. cd->sleep_state = SS_EASY_WAKING_OFF;
  8634. mutex_unlock(&cd->system_lock);
  8635. pt_start_wd_timer(cd);
  8636. return rc;
  8637. }
  8638. /*******************************************************************************
  8639. * FUNCTION: pt_core_easywake_off
  8640. *
  8641. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8642. *
  8643. * RETURN:
  8644. * 0 = success
  8645. * !0 = failure
  8646. *
  8647. * PARAMETERS:
  8648. * *cd - pointer to core data
  8649. ******************************************************************************/
  8650. static int pt_core_easywake_off(struct pt_core_data *cd)
  8651. {
  8652. int rc;
  8653. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8654. if (rc < 0) {
  8655. pt_debug(cd->dev, DL_ERROR,
  8656. "%s: fail get exclusive ex=%p own=%p\n",
  8657. __func__, cd->exclusive_dev, cd->dev);
  8658. return rc;
  8659. }
  8660. rc = pt_core_easywake_off_(cd);
  8661. if (release_exclusive(cd, cd->dev) < 0)
  8662. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8663. __func__);
  8664. else
  8665. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8666. __func__);
  8667. return rc;
  8668. }
  8669. /*******************************************************************************
  8670. * FUNCTION: pt_core_wake_
  8671. *
  8672. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8673. * the configuration in the core platform data structure.
  8674. *
  8675. * RETURN:
  8676. * 0 = success
  8677. * !0 = failure
  8678. *
  8679. * PARAMETERS:
  8680. * *cd - pointer to core data
  8681. ******************************************************************************/
  8682. static int pt_core_wake_(struct pt_core_data *cd)
  8683. {
  8684. int rc = 0;
  8685. mutex_lock(&cd->system_lock);
  8686. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_WAKING) {
  8687. mutex_unlock(&cd->system_lock);
  8688. pt_debug(cd->dev, DL_INFO,
  8689. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8690. return 0;
  8691. } else {
  8692. cd->sleep_state = SS_WAKING;
  8693. }
  8694. mutex_unlock(&cd->system_lock);
  8695. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8696. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8697. pt_debug(cd->dev, DL_INFO,
  8698. "%s: Entering into poweron mode:\n", __func__);
  8699. rc = pt_core_poweron_device_(cd);
  8700. if (rc < 0)
  8701. pr_err("%s: Poweron error detected: rc=%d\n",
  8702. __func__, rc);
  8703. }
  8704. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8705. rc = pt_core_wake_device_from_deep_standby_(cd);
  8706. else /* Default action to exit DeepSleep */
  8707. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8708. }
  8709. mutex_lock(&cd->system_lock);
  8710. cd->sleep_state = SS_SLEEP_OFF;
  8711. mutex_unlock(&cd->system_lock);
  8712. pt_start_wd_timer(cd);
  8713. return rc;
  8714. }
  8715. /*******************************************************************************
  8716. * FUNCTION: pt_core_wake_
  8717. *
  8718. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8719. *
  8720. * RETURN:
  8721. * 0 = success
  8722. * !0 = failure
  8723. *
  8724. * PARAMETERS:
  8725. * *cd - pointer to core data
  8726. ******************************************************************************/
  8727. static int pt_core_wake(struct pt_core_data *cd)
  8728. {
  8729. int rc = 0;
  8730. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8731. if (rc < 0) {
  8732. pt_debug(cd->dev, DL_ERROR,
  8733. "%s: fail get exclusive ex=%p own=%p\n",
  8734. __func__, cd->exclusive_dev, cd->dev);
  8735. return rc;
  8736. }
  8737. rc = pt_core_wake_(cd);
  8738. if (release_exclusive(cd, cd->dev) < 0)
  8739. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8740. __func__);
  8741. else
  8742. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8743. __func__);
  8744. return rc;
  8745. }
  8746. /*******************************************************************************
  8747. * FUNCTION: pt_get_ic_crc_
  8748. *
  8749. * SUMMARY: This function retrieves the config block CRC
  8750. *
  8751. * NOTE: The post condition of calling this function will be that the DUT will
  8752. * be in SCANNINING mode if no failures occur
  8753. *
  8754. * RETURN:
  8755. * 0 = success
  8756. * !0 = failure
  8757. *
  8758. * PARAMETERS:
  8759. * *cd - pointer the core data structure
  8760. * ebid - enumerated block ID
  8761. ******************************************************************************/
  8762. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8763. {
  8764. struct pt_sysinfo *si = &cd->sysinfo;
  8765. int rc = 0;
  8766. u8 status;
  8767. u16 calculated_crc = 0;
  8768. u16 stored_crc = 0;
  8769. rc = pt_pip_suspend_scanning_(cd);
  8770. if (rc)
  8771. goto error;
  8772. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8773. &calculated_crc, &stored_crc);
  8774. if (rc)
  8775. goto exit;
  8776. if (status) {
  8777. rc = -EINVAL;
  8778. goto exit;
  8779. }
  8780. si->ttconfig.crc = stored_crc;
  8781. exit:
  8782. pt_pip_resume_scanning_(cd);
  8783. error:
  8784. pt_debug(cd->dev, DL_INFO,
  8785. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8786. __func__, ebid, calculated_crc, stored_crc, rc);
  8787. return rc;
  8788. }
  8789. /*******************************************************************************
  8790. * FUNCTION: pt_pip2_read_gpio
  8791. *
  8792. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8793. * bits into the passed in variable
  8794. *
  8795. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8796. *
  8797. * RETURN:
  8798. * 0 = success
  8799. * !0 = failure
  8800. *
  8801. * PARAMETERS:
  8802. * *dev - pointer to device structure
  8803. * *status - pointer to where the command response status is stored
  8804. * *gpio - pointer to device attributes structure
  8805. ******************************************************************************/
  8806. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8807. {
  8808. int rc = 0;
  8809. u16 actual_read_len;
  8810. u8 read_buf[12];
  8811. u8 tmp_status = 0;
  8812. u8 index = PIP2_RESP_STATUS_OFFSET;
  8813. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8814. rc = _pt_request_pip2_send_cmd(dev,
  8815. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8816. NULL, 0, read_buf, &actual_read_len);
  8817. if (rc) {
  8818. pt_debug(dev, DL_ERROR,
  8819. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8820. rc = -ECOMM;
  8821. } else {
  8822. tmp_status = read_buf[index];
  8823. }
  8824. if (status)
  8825. *status = tmp_status;
  8826. if (!rc && gpio && (tmp_status == 0)) {
  8827. *gpio = ((read_buf[index + 4] << 24) |
  8828. (read_buf[index + 3] << 16) |
  8829. (read_buf[index + 2] << 8) |
  8830. (read_buf[index + 1]));
  8831. }
  8832. return rc;
  8833. }
  8834. /*******************************************************************************
  8835. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8836. *
  8837. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8838. *
  8839. * RETURN:
  8840. * 0 = success
  8841. * !0 = failure
  8842. *
  8843. * PARAMETERS:
  8844. * *cd - pointer the core data structure
  8845. * *pid - pointer to store panel ID
  8846. ******************************************************************************/
  8847. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8848. {
  8849. u32 gpio_value = 0;
  8850. u8 status = 0;
  8851. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8852. int rc = 0;
  8853. if (!pid)
  8854. return -ENOMEM;
  8855. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8856. if (!rc) {
  8857. if (status == 0) {
  8858. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8859. PT_PANEL_ID_SHIFT;
  8860. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8861. __func__,
  8862. "BL mode PID", panel_id, "gpio", gpio_value);
  8863. *pid = panel_id;
  8864. } else {
  8865. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8866. __func__,
  8867. "BL read gpio failed status", status);
  8868. }
  8869. } else {
  8870. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8871. __func__,
  8872. "BL read gpio failed status", status);
  8873. }
  8874. return rc;
  8875. }
  8876. /*******************************************************************************
  8877. * FUNCTION: pt_enum_with_dut_
  8878. *
  8879. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8880. * The core data (cd) startup_status will store, as a bitmask, each
  8881. * state of the enumeration process. The startup will be attempted
  8882. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8883. *
  8884. * RETURN:
  8885. * 0 = success
  8886. * !0 = failure
  8887. *
  8888. * PARAMETERS:
  8889. * *cd - pointer the core data structure
  8890. * reset - Flag to reset the DUT before attempting to enumerate
  8891. * *status - poionter to store the enum status bitmask flags
  8892. ******************************************************************************/
  8893. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8894. u32 *enum_status)
  8895. {
  8896. int try = 1;
  8897. int rc = 0;
  8898. int wait_time = 0;
  8899. bool detected = false;
  8900. u8 return_data[8];
  8901. u8 mode = PT_MODE_UNKNOWN;
  8902. u8 pid = PANEL_ID_NOT_ENABLED;
  8903. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8904. struct pt_hid_desc hid_desc;
  8905. memset(&hid_desc, 0, sizeof(hid_desc));
  8906. #ifdef TTHE_TUNER_SUPPORT
  8907. tthe_print(cd, NULL, 0, "enter startup");
  8908. #endif
  8909. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8910. __func__, cd->startup_status, reset);
  8911. pt_stop_wd_timer(cd);
  8912. reset:
  8913. if (try > 1)
  8914. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8915. __func__, try);
  8916. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8917. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8918. pt_debug(cd->dev, DL_INFO,
  8919. "%s: PIP1 Enumeration start\n", __func__);
  8920. /* Only reset the DUT after the first try */
  8921. if (reset || (try > 1)) {
  8922. /*
  8923. * Reset hardware only for Legacy parts. Skip for TT/TC
  8924. * parts because if the FW image was loaded directly
  8925. * to SRAM issueing a reset ill wipe out what was just
  8926. * loaded.
  8927. */
  8928. rc = pt_dut_reset_and_wait(cd);
  8929. if (rc < 0) {
  8930. pt_debug(cd->dev, DL_ERROR,
  8931. "%s: Error on h/w reset r=%d\n",
  8932. __func__, rc);
  8933. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8934. goto reset;
  8935. goto exit;
  8936. }
  8937. /* sleep to allow FW to be launched if available */
  8938. msleep(120);
  8939. }
  8940. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8941. if (rc < 0) {
  8942. pt_debug(cd->dev, DL_ERROR,
  8943. "%s: Error getting HID Descriptor r=%d\n",
  8944. __func__, rc);
  8945. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8946. goto reset;
  8947. rc = -EIO;
  8948. goto exit;
  8949. }
  8950. detected = true;
  8951. cd->mode = pt_get_mode(cd, &hid_desc);
  8952. /*
  8953. * Most systems do not use an XY pin as the panel_id and so
  8954. * the BL is used to retrieve the panel_id, however for
  8955. * systems that do use an XY pin, the panel_id MUST be
  8956. * retrieved from the system information when running FW
  8957. * (done below) and so this section of code is skipped.
  8958. * Entering the BL here is only needed on XY_PIN systems.
  8959. */
  8960. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8961. if (cd->mode == PT_MODE_OPERATIONAL) {
  8962. rc = pt_pip_start_bootloader_(cd);
  8963. if (rc < 0) {
  8964. pt_debug(cd->dev, DL_ERROR,
  8965. "%s: Error on start bootloader r=%d\n",
  8966. __func__, rc);
  8967. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8968. goto reset;
  8969. goto exit;
  8970. }
  8971. cd->mode = PT_MODE_BOOTLOADER;
  8972. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8973. __func__);
  8974. }
  8975. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8976. if (!rc) {
  8977. cd->bl_info.ready = true;
  8978. cd->bl_info.chip_id =
  8979. get_unaligned_le16(&return_data[2]);
  8980. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8981. __func__, cd->bl_info.chip_id);
  8982. } else {
  8983. pt_debug(cd->dev, DL_ERROR,
  8984. "%s: failed to get chip ID, r=%d\n",
  8985. __func__, rc);
  8986. }
  8987. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8988. mutex_lock(&cd->system_lock);
  8989. if (!rc) {
  8990. cd->pid_for_loader = pid;
  8991. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8992. __func__, cd->pid_for_loader);
  8993. } else {
  8994. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8995. pt_debug(cd->dev, DL_WARN,
  8996. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8997. __func__, cd->pid_for_loader);
  8998. }
  8999. mutex_unlock(&cd->system_lock);
  9000. }
  9001. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  9002. if (cd->mode == PT_MODE_BOOTLOADER) {
  9003. rc = pt_hid_output_bl_launch_app_(cd);
  9004. if (rc < 0) {
  9005. pt_debug(cd->dev, DL_ERROR,
  9006. "%s: Error on launch app r=%d\n",
  9007. __func__, rc);
  9008. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9009. goto reset;
  9010. rc = -ENODEV;
  9011. goto exit;
  9012. }
  9013. /* sleep to allow FW to be launched if available */
  9014. msleep(120);
  9015. /* Ensure the DUT is now in Application mode */
  9016. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9017. if (rc < 0) {
  9018. pt_debug(cd->dev, DL_ERROR,
  9019. "%s: Error getting HID Desc r=%d\n",
  9020. __func__, rc);
  9021. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9022. goto reset;
  9023. rc = -EIO;
  9024. goto exit;
  9025. }
  9026. cd->mode = pt_get_mode(cd, &hid_desc);
  9027. if (cd->mode == PT_MODE_BOOTLOADER) {
  9028. pt_debug(cd->dev, DL_WARN,
  9029. "%s: Error confiming exit BL\n",
  9030. __func__);
  9031. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9032. goto reset;
  9033. rc = -ENODEV;
  9034. goto exit;
  9035. }
  9036. }
  9037. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9038. cd->mode = PT_MODE_OPERATIONAL;
  9039. *enum_status |= STARTUP_STATUS_GET_DESC;
  9040. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9041. } else {
  9042. /* Generation is PIP2 Capable */
  9043. pt_debug(cd->dev, DL_INFO,
  9044. "%s: PIP2 Enumeration start\n", __func__);
  9045. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9046. if (rc) {
  9047. pt_debug(cd->dev, DL_ERROR,
  9048. "%s: Get mode failed, mode unknown\n",
  9049. __func__);
  9050. mode = PT_MODE_UNKNOWN;
  9051. } else
  9052. detected = true;
  9053. cd->mode = mode;
  9054. switch (cd->mode) {
  9055. case PT_MODE_OPERATIONAL:
  9056. pt_debug(cd->dev, DL_INFO,
  9057. "%s: Operational mode\n", __func__);
  9058. if (cd->app_pip_ver_ready == false) {
  9059. rc = pt_pip2_get_version_(cd);
  9060. if (!rc)
  9061. cd->app_pip_ver_ready = true;
  9062. else {
  9063. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9064. goto reset;
  9065. goto exit;
  9066. }
  9067. }
  9068. break;
  9069. case PT_MODE_BOOTLOADER:
  9070. pt_debug(cd->dev, DL_INFO,
  9071. "%s: Bootloader mode\n", __func__);
  9072. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9073. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9074. mutex_lock(&cd->system_lock);
  9075. if (!rc) {
  9076. cd->pid_for_loader = pid;
  9077. pt_debug(cd->dev, DL_INFO,
  9078. "%s: Panel ID: 0x%02X\n",
  9079. __func__, cd->pid_for_loader);
  9080. } else {
  9081. cd->pid_for_loader =
  9082. PANEL_ID_NOT_ENABLED;
  9083. pt_debug(cd->dev, DL_WARN,
  9084. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9085. __func__, cd->pid_for_loader);
  9086. }
  9087. mutex_unlock(&cd->system_lock);
  9088. }
  9089. if (cd->bl_pip_ver_ready == false) {
  9090. rc = pt_pip2_get_version_(cd);
  9091. if (!rc)
  9092. cd->bl_pip_ver_ready = true;
  9093. else {
  9094. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9095. goto reset;
  9096. goto exit;
  9097. }
  9098. }
  9099. /*
  9100. * Launch app command will fail in flashless mode.
  9101. * Skip launch app command here to save time for
  9102. * enumeration flow.
  9103. */
  9104. if (cd->flashless_dut)
  9105. goto exit;
  9106. /*
  9107. * pt_pip2_launch_app() is needed here instead of
  9108. * pt_pip2_exit_bl() because exit_bl will cause several
  9109. * hw_resets to occur and the auto BL on a flashless
  9110. * DUT will fail.
  9111. */
  9112. rc = pt_pip2_launch_app(cd->dev,
  9113. PT_CORE_CMD_UNPROTECTED);
  9114. if (rc) {
  9115. pt_debug(cd->dev, DL_ERROR,
  9116. "%s: Error on launch app r=%d\n",
  9117. __func__, rc);
  9118. msleep(50);
  9119. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9120. goto reset;
  9121. goto exit;
  9122. }
  9123. /*
  9124. * IRQ thread can be delayed if the serial log print is
  9125. * enabled. It causes next command to get wrong response
  9126. * Here the delay is to ensure pt_parse_input() to be
  9127. * finished.
  9128. */
  9129. msleep(60);
  9130. /* Check and update the mode */
  9131. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9132. if (rc) {
  9133. pt_debug(cd->dev, DL_ERROR,
  9134. "%s: Get mode failed, mode unknown\n",
  9135. __func__);
  9136. mode = PT_MODE_UNKNOWN;
  9137. }
  9138. cd->mode = mode;
  9139. if (cd->mode == PT_MODE_OPERATIONAL) {
  9140. pt_debug(cd->dev, DL_INFO,
  9141. "%s: Launched to Operational mode\n",
  9142. __func__);
  9143. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9144. pt_debug(cd->dev, DL_ERROR,
  9145. "%s: Launch failed, Bootloader mode\n",
  9146. __func__);
  9147. goto exit;
  9148. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9149. pt_debug(cd->dev, DL_ERROR,
  9150. "%s: Launch failed, Unknown mode\n",
  9151. __func__);
  9152. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9153. goto reset;
  9154. goto exit;
  9155. }
  9156. if (cd->app_pip_ver_ready == false) {
  9157. rc = pt_pip2_get_version_(cd);
  9158. if (!rc)
  9159. cd->app_pip_ver_ready = true;
  9160. else {
  9161. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9162. goto reset;
  9163. goto exit;
  9164. }
  9165. }
  9166. break;
  9167. default:
  9168. pt_debug(cd->dev, DL_ERROR,
  9169. "%s: Unknown mode\n", __func__);
  9170. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9171. goto reset;
  9172. goto exit;
  9173. }
  9174. *enum_status |= STARTUP_STATUS_GET_DESC;
  9175. }
  9176. pt_init_pip_report_fields(cd);
  9177. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9178. if (!cd->features.easywake)
  9179. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9180. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9181. rc = pt_hid_output_get_sysinfo_(cd);
  9182. if (rc) {
  9183. pt_debug(cd->dev, DL_ERROR,
  9184. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9185. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9186. goto reset;
  9187. goto exit;
  9188. }
  9189. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9190. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9191. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9192. if (!rc) {
  9193. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9194. pt_debug(cd->dev, DL_WARN,
  9195. "%s: Exit FW BOOT Mode after %dms\n",
  9196. __func__, wait_time);
  9197. } else {
  9198. pt_debug(cd->dev, DL_WARN,
  9199. "%s: FW stuck in BOOT Mode after %dms\n",
  9200. __func__, wait_time);
  9201. goto exit;
  9202. }
  9203. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9204. __func__,
  9205. cd->sysinfo.ttdata.pip_ver_major,
  9206. cd->sysinfo.ttdata.pip_ver_minor);
  9207. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9208. if (rc) {
  9209. pt_debug(cd->dev, DL_ERROR,
  9210. "%s: DUT Config block CRC failure rc=%d\n",
  9211. __func__, rc);
  9212. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9213. goto reset;
  9214. } else {
  9215. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9216. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9217. pt_debug(cd->dev, DL_ERROR,
  9218. "%s: scan state: %d, retry: %d\n",
  9219. __func__, sys_mode, try);
  9220. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9221. goto reset;
  9222. } else
  9223. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9224. }
  9225. rc = pt_restore_parameters_(cd);
  9226. if (rc) {
  9227. pt_debug(cd->dev, DL_ERROR,
  9228. "%s: Failed to restore parameters rc=%d\n",
  9229. __func__, rc);
  9230. } else
  9231. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9232. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9233. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9234. cd->startup_retry_count = 0;
  9235. exit:
  9236. /* Generate the HW Version of the connected DUT and store in cd */
  9237. pt_generate_hw_version(cd, cd->hw_version);
  9238. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9239. cd->hw_version);
  9240. pt_start_wd_timer(cd);
  9241. if (!detected)
  9242. rc = -ENODEV;
  9243. #ifdef TTHE_TUNER_SUPPORT
  9244. tthe_print(cd, NULL, 0, "exit startup");
  9245. #endif
  9246. pt_debug(cd->dev, DL_WARN,
  9247. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9248. __func__, rc, try);
  9249. return rc;
  9250. }
  9251. /*******************************************************************************
  9252. * FUNCTION: pt_enum_with_dut
  9253. *
  9254. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9255. * requesting exclusive access around the call.
  9256. *
  9257. * RETURN:
  9258. * 0 = success
  9259. * !0 = failure
  9260. *
  9261. * PARAMETERS:
  9262. * *cd - pointer the core data structure
  9263. * reset - Flag to reset the DUT before attempting to enumerate
  9264. * *status - pointer to store the enum status bitmask flags
  9265. ******************************************************************************/
  9266. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9267. {
  9268. int rc = 0;
  9269. mutex_lock(&cd->system_lock);
  9270. cd->startup_state = STARTUP_RUNNING;
  9271. mutex_unlock(&cd->system_lock);
  9272. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9273. if (rc) {
  9274. pt_debug(cd->dev, DL_ERROR,
  9275. "%s: fail get exclusive ex=%p own=%p\n",
  9276. __func__, cd->exclusive_dev, cd->dev);
  9277. goto exit;
  9278. }
  9279. rc = pt_enum_with_dut_(cd, reset, status);
  9280. if (release_exclusive(cd, cd->dev) < 0)
  9281. /* Don't return fail code, mode is already changed. */
  9282. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9283. __func__);
  9284. else
  9285. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9286. __func__);
  9287. exit:
  9288. mutex_lock(&cd->system_lock);
  9289. /* Clear startup state for any tasks waiting for startup completion */
  9290. cd->startup_state = STARTUP_NONE;
  9291. mutex_unlock(&cd->system_lock);
  9292. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9293. *status |= STARTUP_STATUS_COMPLETE;
  9294. /* Wake the waiters for end of startup */
  9295. wake_up(&cd->wait_q);
  9296. return rc;
  9297. }
  9298. static int add_sysfs_interfaces(struct device *dev);
  9299. static void remove_sysfs_interfaces(struct device *dev);
  9300. static void remove_sysfs_and_modules(struct device *dev);
  9301. static void pt_release_modules(struct pt_core_data *cd);
  9302. static void pt_probe_modules(struct pt_core_data *cd);
  9303. /*******************************************************************************
  9304. * FUNCTION: _pt_ttdl_restart
  9305. *
  9306. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9307. *
  9308. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9309. * existing sysfs nodes will produce an error.
  9310. *
  9311. * RETURN:
  9312. * 0 = success
  9313. * !0 = failure
  9314. *
  9315. * PARAMETERS:
  9316. * *dev - pointer to core device
  9317. ******************************************************************************/
  9318. static int _pt_ttdl_restart(struct device *dev)
  9319. {
  9320. int rc = 0;
  9321. struct pt_core_data *cd = dev_get_drvdata(dev);
  9322. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9323. struct i2c_client *client =
  9324. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9325. #endif
  9326. /*
  9327. * Make sure the device is awake, pt_mt_release function will
  9328. * cause pm sleep function and lead to deadlock.
  9329. */
  9330. pm_runtime_get_sync(dev);
  9331. /* Use ttdl_restart_lock to avoid reentry */
  9332. mutex_lock(&cd->ttdl_restart_lock);
  9333. remove_sysfs_and_modules(cd->dev);
  9334. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9335. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9336. pt_debug(dev, DL_ERROR,
  9337. "%s I2C functionality not Supported\n", __func__);
  9338. rc = -EIO;
  9339. goto ttdl_no_error;
  9340. }
  9341. #endif
  9342. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9343. rc = _pt_detect_dut_generation(cd->dev,
  9344. &cd->startup_status, &cd->active_dut_generation,
  9345. &cd->mode);
  9346. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9347. pt_debug(dev, DL_ERROR,
  9348. "%s: Error, Unknown DUT Generation rc=%d\n",
  9349. __func__, rc);
  9350. }
  9351. }
  9352. rc = add_sysfs_interfaces(cd->dev);
  9353. if (rc < 0)
  9354. pt_debug(cd->dev, DL_ERROR,
  9355. "%s: Error, failed sysfs nodes rc=%d\n",
  9356. __func__, rc);
  9357. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9358. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9359. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9360. if (rc < 0)
  9361. pt_debug(dev, DL_ERROR,
  9362. "%s: Error, Failed to Enumerate\n", __func__);
  9363. }
  9364. rc = pt_mt_probe(dev);
  9365. if (rc < 0) {
  9366. pt_debug(dev, DL_ERROR,
  9367. "%s: Error, fail mt probe\n", __func__);
  9368. }
  9369. rc = pt_btn_probe(dev);
  9370. if (rc < 0) {
  9371. pt_debug(dev, DL_ERROR,
  9372. "%s: Error, fail btn probe\n", __func__);
  9373. }
  9374. pt_probe_modules(cd);
  9375. pt_debug(cd->dev, DL_WARN,
  9376. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9377. rc = 0;
  9378. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9379. ttdl_no_error:
  9380. #endif
  9381. mutex_unlock(&cd->ttdl_restart_lock);
  9382. mutex_lock(&cd->system_lock);
  9383. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9384. cd->startup_state = STARTUP_NONE;
  9385. mutex_unlock(&cd->system_lock);
  9386. pm_runtime_put(dev);
  9387. return rc;
  9388. }
  9389. /*******************************************************************************
  9390. * FUNCTION: pt_restart_work_function
  9391. *
  9392. * SUMMARY: This is the wrapper function placed in a work queue to call
  9393. * _pt_ttdl_restart()
  9394. *
  9395. * RETURN: none
  9396. *
  9397. * PARAMETERS:
  9398. * *work - pointer to the work_struct
  9399. ******************************************************************************/
  9400. static void pt_restart_work_function(struct work_struct *work)
  9401. {
  9402. struct pt_core_data *cd = container_of(work,
  9403. struct pt_core_data, ttdl_restart_work);
  9404. int rc = 0;
  9405. rc = _pt_ttdl_restart(cd->dev);
  9406. if (rc < 0)
  9407. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9408. __func__, rc);
  9409. }
  9410. /*******************************************************************************
  9411. * FUNCTION: pt_enum_work_function
  9412. *
  9413. * SUMMARY: This is the wrapper function placed in a work queue to call
  9414. * pt_enum_with_dut()
  9415. *
  9416. * RETURN: none
  9417. *
  9418. * PARAMETERS:
  9419. * *work - pointer to the work_struct
  9420. ******************************************************************************/
  9421. static void pt_enum_work_function(struct work_struct *work)
  9422. {
  9423. struct pt_core_data *cd = container_of(work,
  9424. struct pt_core_data, enum_work);
  9425. int rc;
  9426. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9427. if (rc < 0)
  9428. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9429. __func__, rc);
  9430. }
  9431. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9432. {
  9433. int rc;
  9434. if (!get) {
  9435. rc = 0;
  9436. goto regulator_put;
  9437. }
  9438. cd->vdd = regulator_get(cd->dev, "vdd");
  9439. if (IS_ERR(cd->vdd)) {
  9440. rc = PTR_ERR(cd->vdd);
  9441. dev_err(cd->dev,
  9442. "Regulator get failed vdd rc=%d\n", rc);
  9443. goto regulator_put;
  9444. }
  9445. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9446. if (IS_ERR(cd->vcc_i2c)) {
  9447. rc = PTR_ERR(cd->vcc_i2c);
  9448. dev_err(cd->dev,
  9449. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9450. goto regulator_put;
  9451. }
  9452. return 0;
  9453. regulator_put:
  9454. if (cd->vdd) {
  9455. regulator_put(cd->vdd);
  9456. cd->vdd = NULL;
  9457. }
  9458. if (cd->vcc_i2c) {
  9459. regulator_put(cd->vcc_i2c);
  9460. cd->vcc_i2c = NULL;
  9461. }
  9462. return rc;
  9463. }
  9464. #ifdef ENABLE_I2C_REG_ONLY
  9465. static int pt_enable_i2c_regulator(struct pt_core_data *cd, bool en)
  9466. {
  9467. int rc = 0;
  9468. pt_debug(cd->dev, DL_INFO, "%s: Enter flag = %d\n", __func__, en);
  9469. if (!en) {
  9470. rc = 0;
  9471. goto disable_vcc_i2c_reg_only;
  9472. }
  9473. if (cd->vcc_i2c) {
  9474. rc = regulator_set_load(cd->vcc_i2c, I2C_ACTIVE_LOAD_MA);
  9475. if (rc < 0)
  9476. pt_debug(cd->dev, DL_INFO,
  9477. "%s: I2c unable to set active current rc = %d\n", __func__, rc);
  9478. pt_debug(cd->dev, DL_INFO, "%s: i2c set I2C_ACTIVE_LOAD_MA rc = %d\n",
  9479. __func__, rc);
  9480. }
  9481. return 0;
  9482. disable_vcc_i2c_reg_only:
  9483. if (cd->vcc_i2c) {
  9484. rc = regulator_set_load(cd->vcc_i2c, I2C_SUSPEND_LOAD_UA);
  9485. if (rc < 0)
  9486. pt_debug(cd->dev, DL_INFO, "%s: i2c unable to set 0 uAm rc = %d\n",
  9487. __func__, rc);
  9488. }
  9489. pt_debug(cd->dev, DL_INFO, "%s: Exit rc = %d I2C_SUSPEND_LOAD_UA\n", __func__, rc);
  9490. return rc;
  9491. }
  9492. #endif
  9493. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9494. {
  9495. int rc;
  9496. if (!en) {
  9497. rc = 0;
  9498. goto disable_vcc_i2c_reg;
  9499. }
  9500. if (cd->vdd) {
  9501. if (regulator_count_voltages(cd->vdd) > 0) {
  9502. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9503. FT_VTG_MAX_UV);
  9504. if (rc) {
  9505. dev_err(cd->dev,
  9506. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9507. goto exit;
  9508. }
  9509. }
  9510. rc = regulator_enable(cd->vdd);
  9511. if (rc) {
  9512. dev_err(cd->dev,
  9513. "Regulator vdd enable failed rc=%d\n", rc);
  9514. goto exit;
  9515. }
  9516. }
  9517. if (cd->vcc_i2c) {
  9518. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9519. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9520. FT_I2C_VTG_MAX_UV);
  9521. if (rc) {
  9522. dev_err(cd->dev,
  9523. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9524. goto disable_vdd_reg;
  9525. }
  9526. }
  9527. rc = regulator_enable(cd->vcc_i2c);
  9528. if (rc) {
  9529. dev_err(cd->dev,
  9530. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9531. goto disable_vdd_reg;
  9532. }
  9533. }
  9534. return 0;
  9535. disable_vcc_i2c_reg:
  9536. if (cd->vcc_i2c) {
  9537. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9538. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9539. FT_I2C_VTG_MAX_UV);
  9540. regulator_disable(cd->vcc_i2c);
  9541. }
  9542. disable_vdd_reg:
  9543. if (cd->vdd) {
  9544. if (regulator_count_voltages(cd->vdd) > 0)
  9545. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9546. FT_VTG_MAX_UV);
  9547. regulator_disable(cd->vdd);
  9548. }
  9549. exit:
  9550. return rc;
  9551. }
  9552. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9553. #define KERNEL_VER_GT_3_19
  9554. #endif
  9555. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9556. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9557. #if defined(CONFIG_PM_SLEEP)
  9558. /*******************************************************************************
  9559. * FUNCTION: pt_core_rt_suspend
  9560. *
  9561. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9562. *
  9563. * RETURN:
  9564. * 0 = success
  9565. * !0 = failure
  9566. *
  9567. * PARAMETERS:
  9568. * *dev - pointer to core device
  9569. ******************************************************************************/
  9570. static int pt_core_rt_suspend(struct device *dev)
  9571. {
  9572. struct pt_core_data *cd = dev_get_drvdata(dev);
  9573. pt_debug(dev, DL_DEBUG, "%s Skip - probe state %d\n",
  9574. __func__, cd->core_probe_complete);
  9575. return 0;
  9576. }
  9577. /*******************************************************************************
  9578. * FUNCTION: pt_core_rt_resume
  9579. *
  9580. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  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_resume(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. #endif /* CONFIG_PM_SLEEP */
  9597. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9598. #if defined(CONFIG_PM_SLEEP)
  9599. /*******************************************************************************
  9600. * FUNCTION: pt_core_suspend_
  9601. *
  9602. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9603. * pt_core_sleep. This function may disable IRQ during sleep state.
  9604. *
  9605. * RETURN:
  9606. * 0 = success
  9607. * !0 = failure
  9608. *
  9609. * PARAMETERS:
  9610. * *dev - pointer to core device
  9611. ******************************************************************************/
  9612. static int pt_core_suspend_(struct device *dev)
  9613. {
  9614. int rc;
  9615. struct pt_core_data *cd = dev_get_drvdata(dev);
  9616. pt_debug(dev, DL_INFO, "%s: Enter\n", __func__);
  9617. rc = pt_core_sleep(cd);
  9618. if (rc) {
  9619. pt_debug(dev, DL_ERROR, "%s: Error on sleep rc =%d\n",
  9620. __func__, rc);
  9621. return -EAGAIN;
  9622. }
  9623. rc = pt_enable_regulator(cd, false);
  9624. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9625. return 0;
  9626. /* Required to prevent interrupts before bus awake */
  9627. disable_irq(cd->irq);
  9628. cd->irq_disabled = 1;
  9629. if (device_may_wakeup(dev)) {
  9630. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9631. __func__);
  9632. if (!enable_irq_wake(cd->irq))
  9633. cd->irq_wake = 1;
  9634. } else {
  9635. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9636. __func__);
  9637. }
  9638. pt_debug(dev, DL_INFO, "%s: Exit :\n", __func__);
  9639. return rc;
  9640. }
  9641. /*******************************************************************************
  9642. * FUNCTION: pt_core_suspend
  9643. *
  9644. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9645. * woke up or put to sleep based on Kernel power state even when the display
  9646. * is off based on the check of TTDL core platform flag.
  9647. *
  9648. * RETURN:
  9649. * 0 = success
  9650. * !0 = failure
  9651. *
  9652. * PARAMETERS:
  9653. * *dev - pointer to core device
  9654. ******************************************************************************/
  9655. static int pt_core_suspend(struct device *dev)
  9656. {
  9657. struct pt_core_data *cd = dev_get_drvdata(dev);
  9658. int rc = 0, status = 0;
  9659. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9660. return 0;
  9661. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9662. cancel_work_sync(&cd->resume_work);
  9663. cancel_work_sync(&cd->suspend_work);
  9664. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9665. rc = pt_core_suspend_(cd->dev);
  9666. cd->quick_boot = true;
  9667. } else {
  9668. rc = pt_core_easywake_on(cd);
  9669. if (rc < 0)
  9670. pt_debug(cd->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9671. cd->fb_state = FB_OFF;
  9672. status = pt_enable_i2c_regulator(cd, false);
  9673. pt_debug(cd->dev, DL_INFO, "%s Exit - rc = %d\n", __func__, status);
  9674. }
  9675. return rc;
  9676. }
  9677. /*******************************************************************************
  9678. * FUNCTION: pt_core_resume_
  9679. *
  9680. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9681. * pt_core_wake. This function may enable IRQ before wake up.
  9682. *
  9683. * RETURN:
  9684. * 0 = success
  9685. * !0 = failure
  9686. *
  9687. * PARAMETERS:
  9688. * *dev - pointer to core device
  9689. ******************************************************************************/
  9690. static int pt_core_resume_(struct device *dev)
  9691. {
  9692. int rc = 0;
  9693. struct pt_core_data *cd = dev_get_drvdata(dev);
  9694. dev_info(dev, "%s: Entering into resume mode:\n",
  9695. __func__);
  9696. rc = pt_enable_regulator(cd, true);
  9697. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9698. __func__, rc);
  9699. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9700. goto exit;
  9701. /*
  9702. * Bus pm does not call suspend if device runtime suspended
  9703. * This flag is covers that case
  9704. */
  9705. if (cd->irq_disabled) {
  9706. enable_irq(cd->irq);
  9707. cd->irq_disabled = 0;
  9708. }
  9709. if (device_may_wakeup(dev)) {
  9710. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9711. __func__);
  9712. if (cd->irq_wake) {
  9713. disable_irq_wake(cd->irq);
  9714. cd->irq_wake = 0;
  9715. }
  9716. } else {
  9717. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9718. __func__);
  9719. }
  9720. exit:
  9721. rc = pt_core_wake(cd);
  9722. if (rc) {
  9723. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9724. __func__, rc);
  9725. return -EAGAIN;
  9726. }
  9727. return rc;
  9728. }
  9729. /*******************************************************************************
  9730. * FUNCTION: pt_core_restore
  9731. *
  9732. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9733. * pt_core_wake. This function may enable IRQ before wake up.
  9734. *
  9735. * RETURN:
  9736. * 0 = success
  9737. * !0 = failure
  9738. *
  9739. * PARAMETERS:
  9740. * *dev - pointer to core device
  9741. ******************************************************************************/
  9742. static int pt_core_restore(struct device *dev)
  9743. {
  9744. int rc = 0;
  9745. struct pt_core_data *cd = dev_get_drvdata(dev);
  9746. dev_info(dev, "%s: Entering into resume mode:\n",
  9747. __func__);
  9748. queue_work(cd->pt_workqueue, &cd->resume_offload_work);
  9749. return rc;
  9750. }
  9751. /*******************************************************************************
  9752. * FUNCTION: suspend_offload_work
  9753. *
  9754. * SUMMARY: Wrapper function of pt_core_suspend() to avoid TP to be waken/slept
  9755. * along with kernel power state even the display is off based on the check of
  9756. * TTDL core platform flag.
  9757. *
  9758. * RETURN:
  9759. * 0 = success
  9760. * !0 = failure
  9761. *
  9762. * PARAMETERS:
  9763. * *dev - pointer to core device
  9764. ******************************************************************************/
  9765. static void pt_suspend_offload_work(struct work_struct *work)
  9766. {
  9767. int rc = 0;
  9768. struct pt_core_data *cd = container_of(work, struct pt_core_data,
  9769. suspend_offload_work);
  9770. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9771. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9772. return;
  9773. rc = pt_core_suspend_(cd->dev);
  9774. pt_debug(cd->dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9775. }
  9776. /*******************************************************************************
  9777. * FUNCTION: resume_offload_work
  9778. *
  9779. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9780. * along with kernel power state even the display is off based on the check of
  9781. * TTDL core platform flag.
  9782. *
  9783. * RETURN:
  9784. * 0 = success
  9785. * !0 = failure
  9786. *
  9787. * PARAMETERS:
  9788. * *dev - pointer to core device
  9789. ******************************************************************************/
  9790. static void pt_resume_offload_work(struct work_struct *work)
  9791. {
  9792. int rc = 0;
  9793. int retry_count = 10;
  9794. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  9795. resume_offload_work);
  9796. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  9797. do {
  9798. retry_count--;
  9799. rc = pt_core_resume_(pt_data->dev);
  9800. if (rc < 0)
  9801. pt_debug(pt_data->dev, DL_ERROR,
  9802. "%s: Error on wake\n", __func__);
  9803. } while (retry_count && rc < 0);
  9804. #ifdef TOUCH_TO_WAKE_POWER_FEATURE_WORK_AROUND
  9805. rc = pt_core_easywake_on(pt_data);
  9806. if (rc < 0) {
  9807. pt_debug(pt_data->dev, DL_ERROR,
  9808. "%s: Error on enable touch to wake key\n",
  9809. __func__);
  9810. return;
  9811. }
  9812. pt_data->fb_state = FB_OFF;
  9813. pt_debug(pt_data->dev, DL_INFO, "%s End\n", __func__);
  9814. #endif
  9815. pt_data->quick_boot = false;
  9816. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  9817. }
  9818. /*******************************************************************************
  9819. * FUNCTION: pt_core_resume
  9820. *
  9821. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9822. * along with kernel power state even the display is off based on the check of
  9823. * TTDL core platform flag.
  9824. *
  9825. * RETURN:
  9826. * 0 = success
  9827. * !0 = failure
  9828. *
  9829. * PARAMETERS:
  9830. * *dev - pointer to core device
  9831. ******************************************************************************/
  9832. static int pt_core_resume(struct device *dev)
  9833. {
  9834. struct pt_core_data *cd = dev_get_drvdata(dev);
  9835. int rc = 0;
  9836. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9837. return 0;
  9838. if (mem_sleep_current == PM_SUSPEND_MEM) {
  9839. rc = pt_core_restore(cd->dev);
  9840. } else {
  9841. pt_debug(cd->dev, DL_INFO, "%s start\n", __func__);
  9842. rc = pt_enable_i2c_regulator(cd, true);
  9843. pt_debug(cd->dev, DL_INFO, "%s i2c regulator rc %d\n", __func__, rc);
  9844. pt_debug(cd->dev, DL_INFO, "%s End\n", __func__);
  9845. }
  9846. pt_debug(cd->dev, DL_INFO, "%s End rc = %d\n", __func__, rc);
  9847. return rc;
  9848. }
  9849. #endif
  9850. #ifdef NEED_SUSPEND_NOTIFIER
  9851. /*******************************************************************************
  9852. * FUNCTION: pt_pm_notifier
  9853. *
  9854. * SUMMARY: This function is registered to notifier chain and will perform
  9855. * suspend operation if match event PM_SUSPEND_PREPARE.
  9856. *
  9857. * RETURN:
  9858. * 0 = success
  9859. * !0 = failure
  9860. *
  9861. * PARAMETERS:
  9862. * *nb - pointer to notifier_block structure
  9863. * action - notifier event type
  9864. * *data - void pointer
  9865. ******************************************************************************/
  9866. static int pt_pm_notifier(struct notifier_block *nb,
  9867. unsigned long action, void *data)
  9868. {
  9869. struct pt_core_data *cd = container_of(nb,
  9870. struct pt_core_data, pm_notifier);
  9871. if (action == PM_SUSPEND_PREPARE) {
  9872. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9873. __func__);
  9874. /*
  9875. * If PM runtime is not suspended, either call runtime
  9876. * PM suspend callback or wait until it finishes
  9877. */
  9878. if (!pm_runtime_suspended(cd->dev))
  9879. pm_runtime_suspend(cd->dev);
  9880. (void) pt_core_suspend(cd->dev);
  9881. }
  9882. return NOTIFY_DONE;
  9883. }
  9884. #endif
  9885. const struct dev_pm_ops pt_pm_ops = {
  9886. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9887. .freeze = pt_core_suspend,
  9888. .restore = pt_core_restore,
  9889. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9890. NULL)
  9891. };
  9892. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9893. /*******************************************************************************
  9894. * FUNCTION: _pt_request_pip2_enter_bl
  9895. *
  9896. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9897. * XRES pin or a soft reset.
  9898. *
  9899. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9900. * the WD active could cause this function to fail!
  9901. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9902. * will not try to determine the current mode but will proceed with
  9903. * resetting the DUT and entering the BL.
  9904. *
  9905. * NOTE: The definition of result code:
  9906. * PT_ENTER_BL_PASS (0)
  9907. * PT_ENTER_BL_ERROR (1)
  9908. * PT_ENTER_BL_RESET_FAIL (2)
  9909. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9910. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9911. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9912. *
  9913. * RETURNS:
  9914. * 0 = success
  9915. * !0 = failure
  9916. *
  9917. *
  9918. * PARAMETERS:
  9919. * *dev - pointer to device structure
  9920. * *start_mode - pointer to the mode the DUT was in when this function
  9921. * starts
  9922. * *result - pointer to store the result when to enter BL
  9923. ******************************************************************************/
  9924. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9925. {
  9926. int rc = 0;
  9927. int t;
  9928. int tmp_result = PT_ENTER_BL_ERROR;
  9929. int flash_info_retry = 2;
  9930. u8 mode = PT_MODE_UNKNOWN;
  9931. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9932. u8 read_buf[32];
  9933. u16 actual_read_len;
  9934. struct pt_core_data *cd = dev_get_drvdata(dev);
  9935. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9936. u8 time = 0;
  9937. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9938. if (cd->watchdog_enabled) {
  9939. pt_debug(dev, DL_WARN,
  9940. "%s: Watchdog must be stopped before entering BL\n",
  9941. __func__);
  9942. goto exit;
  9943. }
  9944. cancel_work_sync(&cd->enum_work);
  9945. cancel_work_sync(&cd->watchdog_work);
  9946. /* if undefined assume operational/test to bypass all checks */
  9947. if (*start_mode == PT_MODE_IGNORE) {
  9948. mode = PT_MODE_OPERATIONAL;
  9949. sys_mode = FW_SYS_MODE_TEST;
  9950. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9951. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9952. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9953. if (rc) {
  9954. pt_debug(dev, DL_ERROR,
  9955. "%s: Get mode failed, mode unknown\n",
  9956. __func__);
  9957. }
  9958. *start_mode = mode;
  9959. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9960. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9961. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9962. sys_mode = FW_SYS_MODE_SCANNING;
  9963. }
  9964. _retry:
  9965. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9966. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9967. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9968. switch (mode) {
  9969. case PT_MODE_UNKNOWN:
  9970. /*
  9971. * When the mode could not be determined the DUT could be
  9972. * in App mode running corrupted FW or FW that is not
  9973. * responding to the mode request, assume no communication
  9974. * and do a hard reset
  9975. */
  9976. mutex_lock(&cd->system_lock);
  9977. cd->startup_status = STARTUP_STATUS_START;
  9978. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9979. mutex_unlock(&cd->system_lock);
  9980. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9981. if (rc) {
  9982. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9983. goto exit;
  9984. }
  9985. break;
  9986. case PT_MODE_OPERATIONAL:
  9987. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9988. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9989. __func__);
  9990. rc = pt_pip_suspend_scanning_(cd);
  9991. if (rc) {
  9992. /*
  9993. * Print to log but don't exit, the FW could be
  9994. * running but be hung or fail to respond to
  9995. * this request
  9996. */
  9997. pt_debug(dev, DL_ERROR,
  9998. "%s Suspend Scan Failed\n", __func__);
  9999. }
  10000. /* sleep to allow the suspend scan to be processed */
  10001. usleep_range(1000, 2000);
  10002. }
  10003. mutex_lock(&cd->system_lock);
  10004. cd->startup_status = STARTUP_STATUS_START;
  10005. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  10006. mutex_unlock(&cd->system_lock);
  10007. /* Reset device to enter the BL */
  10008. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  10009. if (rc) {
  10010. tmp_result = PT_ENTER_BL_RESET_FAIL;
  10011. goto exit;
  10012. }
  10013. break;
  10014. case PT_MODE_BOOTLOADER:
  10015. /* Do nothing as we are already in the BL */
  10016. tmp_result = PT_ENTER_BL_PASS;
  10017. goto exit;
  10018. default:
  10019. /* Should NEVER get here */
  10020. tmp_result = PT_ENTER_BL_ERROR;
  10021. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  10022. goto exit;
  10023. }
  10024. if (!cd->flashless_dut &&
  10025. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  10026. /*
  10027. * Sending the special "Host Mode" command will instruct the
  10028. * BL to not execute the FW it has loaded into RAM.
  10029. * The command must be sent within a 40ms window from releasing
  10030. * the XRES pin. If the messages is sent too early it will NAK,
  10031. * so keep sending it every 2ms until it is accepted by the BL.
  10032. * A no-flash DUT does not require this command as there is no
  10033. * FW for the BL to load and execute.
  10034. */
  10035. usleep_range(4000, 6000);
  10036. pt_debug(cd->dev, DL_INFO,
  10037. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  10038. __func__, (int)sizeof(host_mode_cmd));
  10039. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  10040. (int)sizeof(host_mode_cmd), ">>> User CMD");
  10041. rc = 1;
  10042. while (rc && time < 34) {
  10043. rc = pt_adap_write_read_specific(cd, 4,
  10044. host_mode_cmd, NULL);
  10045. usleep_range(1800, 2000);
  10046. time += 2;
  10047. }
  10048. /* Sleep to allow the BL to come up */
  10049. usleep_range(1000, 2000);
  10050. }
  10051. /*
  10052. * To avoid the case that next PIP command can be confused by BL/FW
  10053. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  10054. * followed by "wakeup event" function can lower the failure rate.
  10055. */
  10056. t = wait_event_timeout(cd->wait_q,
  10057. ((cd->startup_status != STARTUP_STATUS_START)
  10058. && (cd->hid_reset_cmd_state == 0)),
  10059. msecs_to_jiffies(300));
  10060. if (IS_TMO(t)) {
  10061. pt_debug(cd->dev, DL_ERROR,
  10062. "%s: TMO waiting for BL sentinel\n", __func__);
  10063. }
  10064. /* Check if device is now in BL mode */
  10065. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  10066. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  10067. cd->startup_status);
  10068. if (!rc && mode == PT_MODE_BOOTLOADER) {
  10069. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  10070. mutex_lock(&cd->system_lock);
  10071. cd->pip2_prot_active = true;
  10072. cd->mode = PT_MODE_BOOTLOADER;
  10073. mutex_unlock(&cd->system_lock);
  10074. tmp_result = PT_ENTER_BL_PASS;
  10075. } else {
  10076. /*
  10077. * If the device doesn't enter BL mode as expected and rc is
  10078. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  10079. * the function should return an error code to indicate this
  10080. * failure PT_ENTER_BL_CONFIRM_FAIL.
  10081. */
  10082. if (!rc)
  10083. rc = -EINVAL;
  10084. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  10085. mutex_lock(&cd->system_lock);
  10086. cd->mode = mode;
  10087. mutex_unlock(&cd->system_lock);
  10088. pt_debug(dev, DL_ERROR,
  10089. "%s ERROR: Not in BL as expected", __func__);
  10090. }
  10091. exit:
  10092. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  10093. /* Check to get (buffered) flash information */
  10094. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10095. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  10096. read_buf, &actual_read_len);
  10097. if (!rc) {
  10098. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  10099. pt_debug(
  10100. dev, DL_WARN,
  10101. "%s Unavailable Manufacturer ID: 0x%02x\n",
  10102. __func__,
  10103. read_buf[PIP2_RESP_BODY_OFFSET]);
  10104. /*
  10105. * If the BL was unable to cache the correct
  10106. * values when entering the first time due to
  10107. * the Flash part not having been powered up
  10108. * long enough, re-enter the BL to trigger the
  10109. * BL to re-attempt to cache the values.
  10110. */
  10111. if (flash_info_retry-- > 0) {
  10112. mode = PT_MODE_UNKNOWN;
  10113. pt_debug(dev, DL_WARN,
  10114. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  10115. __func__, flash_info_retry);
  10116. goto _retry;
  10117. } else {
  10118. pt_debug(dev, DL_WARN,
  10119. "%s Manufacturer ID Unknown\n",
  10120. __func__);
  10121. tmp_result = PT_ENTER_BL_PASS;
  10122. }
  10123. }
  10124. } else {
  10125. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  10126. pt_debug(
  10127. dev, DL_ERROR,
  10128. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  10129. __func__);
  10130. }
  10131. }
  10132. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10133. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10134. "SUPPRESS");
  10135. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10136. if (result)
  10137. *result = tmp_result;
  10138. return rc;
  10139. }
  10140. /*******************************************************************************
  10141. * FUNCTION: _pt_pip2_file_open
  10142. *
  10143. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10144. *
  10145. * NOTE: The DUT must be in BL mode for this command to work
  10146. *
  10147. * RETURNS:
  10148. * <0 = Error
  10149. * >0 = file handle opened
  10150. *
  10151. * PARAMETERS:
  10152. * *dev - pointer to device structure
  10153. * file_no - PIP2 file number to open
  10154. ******************************************************************************/
  10155. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10156. {
  10157. int ret = 0;
  10158. u16 status;
  10159. u16 actual_read_len;
  10160. u8 file_handle;
  10161. u8 data[2];
  10162. u8 read_buf[10];
  10163. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10164. data[0] = file_no;
  10165. ret = _pt_request_pip2_send_cmd(dev,
  10166. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10167. data, 1, read_buf, &actual_read_len);
  10168. if (ret) {
  10169. pt_debug(dev, DL_ERROR,
  10170. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10171. __func__, file_no);
  10172. return -PIP2_RSP_ERR_NOT_OPEN;
  10173. }
  10174. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10175. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10176. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10177. (file_handle != file_no)) {
  10178. pt_debug(dev, DL_ERROR,
  10179. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10180. __func__, status, file_handle);
  10181. return -status;
  10182. }
  10183. return file_handle;
  10184. }
  10185. /*******************************************************************************
  10186. * FUNCTION: _pt_pip2_file_close
  10187. *
  10188. * SUMMARY: Using the BL PIP2 commands close a file
  10189. *
  10190. * NOTE: The DUT must be in BL mode for this command to work
  10191. *
  10192. * RETURNS:
  10193. * <0 = Error
  10194. * >0 = file handle closed
  10195. *
  10196. * PARAMETERS:
  10197. * *dev - pointer to device structure
  10198. * file_handle - handle to the file to be closed
  10199. ******************************************************************************/
  10200. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10201. {
  10202. int ret = 0;
  10203. u16 status;
  10204. u16 actual_read_len;
  10205. u8 data[2];
  10206. u8 read_buf[32];
  10207. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10208. data[0] = file_handle;
  10209. ret = _pt_request_pip2_send_cmd(dev,
  10210. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10211. data, 1, read_buf, &actual_read_len);
  10212. if (ret) {
  10213. pt_debug(dev, DL_ERROR,
  10214. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10215. __func__, file_handle);
  10216. return -ETIME;
  10217. }
  10218. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10219. if (status != 0x00) {
  10220. pt_debug(dev, DL_ERROR,
  10221. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10222. __func__, status, file_handle);
  10223. return -status;
  10224. }
  10225. return file_handle;
  10226. }
  10227. /*******************************************************************************
  10228. * FUNCTION: _pt_pip2_file_erase
  10229. *
  10230. * SUMMARY: Using the BL PIP2 commands erase a file
  10231. *
  10232. * NOTE: The DUT must be in BL mode for this command to work
  10233. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10234. * one retry is attempted for each sector in a file.
  10235. *
  10236. * RETURNS:
  10237. * <0 = Error
  10238. * >0 = file handle closed
  10239. *
  10240. * PARAMETERS:
  10241. * *dev - pointer to device structure
  10242. * file_handle - handle to the file to be erased
  10243. * *status - PIP2 erase status code
  10244. ******************************************************************************/
  10245. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10246. {
  10247. int ret = 0;
  10248. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10249. int retry = 1;
  10250. u16 actual_read_len;
  10251. u8 data[2];
  10252. u8 read_buf[10];
  10253. struct pt_core_data *cd = dev_get_drvdata(dev);
  10254. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10255. data[0] = file_handle;
  10256. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10257. *status = PIP2_RSP_ERR_TIMEOUT;
  10258. /* Increase waiting time for large file erase */
  10259. mutex_lock(&cd->system_lock);
  10260. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10261. mutex_unlock(&cd->system_lock);
  10262. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10263. ret = _pt_request_pip2_send_cmd(dev,
  10264. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10265. data, 2, read_buf, &actual_read_len);
  10266. if (ret)
  10267. break;
  10268. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10269. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10270. #ifdef TTDL_DIAGNOSTICS
  10271. cd->file_erase_timeout_count++;
  10272. #endif
  10273. pt_debug(dev, DL_WARN,
  10274. "%s: ERASE timeout %d for file = %d\n",
  10275. __func__, retry, file_handle);
  10276. }
  10277. retry++;
  10278. }
  10279. mutex_lock(&cd->system_lock);
  10280. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10281. mutex_unlock(&cd->system_lock);
  10282. if (ret) {
  10283. pt_debug(dev, DL_ERROR,
  10284. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10285. __func__, ret, file_handle);
  10286. return -EIO;
  10287. }
  10288. if (*status != 0x00) {
  10289. pt_debug(dev, DL_ERROR,
  10290. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10291. __func__, *status, file_handle);
  10292. return -EIO;
  10293. }
  10294. return file_handle;
  10295. }
  10296. /*******************************************************************************
  10297. * FUNCTION: _pt_pip2_file_read
  10298. *
  10299. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10300. *
  10301. * NOTE: The DUT must be in BL mode for this command to work
  10302. *
  10303. * RETURNS:
  10304. * <0 = Error
  10305. * >0 = number of bytes read
  10306. *
  10307. * PARAMETERS:
  10308. * *dev - pointer to device structure
  10309. * file_handle - File handle to read from
  10310. * num_bytes - number of bytes to read
  10311. ******************************************************************************/
  10312. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10313. u8 *read_buf)
  10314. {
  10315. int ret = 0;
  10316. u16 status;
  10317. u16 actual_read_len;
  10318. u8 data[3];
  10319. data[0] = file_handle;
  10320. data[1] = (num_bytes & 0x00FF);
  10321. data[2] = (num_bytes & 0xFF00) >> 8;
  10322. ret = _pt_request_pip2_send_cmd(dev,
  10323. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10324. data, 3, read_buf, &actual_read_len);
  10325. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10326. if (ret || ((status != 0x00) && (status != 0x03))) {
  10327. pt_debug(dev, DL_ERROR,
  10328. "%s File open failure with error code = %d\n",
  10329. __func__, status);
  10330. return -EPERM;
  10331. }
  10332. ret = num_bytes;
  10333. return ret;
  10334. }
  10335. /*******************************************************************************
  10336. * FUNCTION: _pt_read_us_file
  10337. *
  10338. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10339. * then read the entire file.
  10340. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10341. *
  10342. * RETURN:
  10343. * 0 = success
  10344. * !0 = failure
  10345. *
  10346. * PARAMETERS:
  10347. * *file_path - pointer to the file path
  10348. * *buf - pointer to the buffer to store the file contents
  10349. * *size - pointer to the size of the file
  10350. ******************************************************************************/
  10351. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10352. {
  10353. struct file *filp = NULL;
  10354. struct inode *inode = NULL;
  10355. unsigned int file_len = 0;
  10356. unsigned int read_len = 0;
  10357. mm_segment_t oldfs;
  10358. int rc = 0;
  10359. if (file_path == NULL || buf == NULL) {
  10360. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10361. return -EINVAL;
  10362. }
  10363. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10364. oldfs = get_fs();
  10365. set_fs(KERNEL_DS);
  10366. filp = filp_open(file_path, O_RDONLY, 0400);
  10367. if (IS_ERR(filp)) {
  10368. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10369. file_path);
  10370. rc = -ENOENT;
  10371. goto err;
  10372. }
  10373. if (filp->f_op == NULL) {
  10374. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10375. __func__);
  10376. rc = -EINVAL;
  10377. goto exit;
  10378. }
  10379. inode = filp->f_path.dentry->d_inode;
  10380. if (inode == NULL) {
  10381. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10382. __func__);
  10383. rc = -EINVAL;
  10384. goto exit;
  10385. }
  10386. file_len = i_size_read(inode->i_mapping->host);
  10387. if (file_len == 0) {
  10388. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10389. __func__, file_len);
  10390. rc = -EINVAL;
  10391. goto exit;
  10392. }
  10393. if (*size == 0)
  10394. read_len = file_len;
  10395. else
  10396. read_len = *size;
  10397. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10398. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10399. __func__, read_len);
  10400. rc = -EINVAL;
  10401. goto exit;
  10402. }
  10403. filp->private_data = inode->i_private;
  10404. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10405. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10406. rc = -EINVAL;
  10407. goto exit;
  10408. }
  10409. *size = read_len;
  10410. exit:
  10411. if (filp_close(filp, NULL) != 0)
  10412. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10413. err:
  10414. set_fs(oldfs);
  10415. return rc;
  10416. }
  10417. /*******************************************************************************
  10418. * FUNCTION: _pt_request_pip2_bin_hdr
  10419. *
  10420. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10421. * in the case of a flashless DUT, and parse the contents
  10422. *
  10423. * RETURNS:
  10424. * 0 = Success
  10425. * !0 = Error condition
  10426. *
  10427. * PARAMETERS:
  10428. * *dev - pointer to device structure
  10429. ******************************************************************************/
  10430. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10431. {
  10432. struct pt_core_data *cd = dev_get_drvdata(dev);
  10433. u8 file_handle;
  10434. u8 read_buf[255];
  10435. u8 hdr_len = 0;
  10436. u8 i;
  10437. int bytes_read;
  10438. int read_size;
  10439. int ret = 0;
  10440. int rc = 0;
  10441. bool load_hdr_struct = false;
  10442. if (cd->flashless_dut) {
  10443. read_size = sizeof(read_buf);
  10444. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10445. read_buf, &read_size);
  10446. if (rc) {
  10447. ret = rc;
  10448. pt_debug(dev, DL_ERROR,
  10449. "%s Failed to read fw image from US, rc=%d\n",
  10450. __func__, rc);
  10451. goto exit;
  10452. }
  10453. load_hdr_struct = true;
  10454. hdr_len = read_buf[0];
  10455. i = 0;
  10456. } else {
  10457. if (cd->mode != PT_MODE_BOOTLOADER) {
  10458. ret = -EPERM;
  10459. goto exit;
  10460. }
  10461. /* Open the bin file in Flash */
  10462. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10463. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10464. if (file_handle != PIP2_FW_FILE) {
  10465. ret = -ENOENT;
  10466. pt_debug(dev, DL_ERROR,
  10467. "%s Failed to open bin file\n", __func__);
  10468. goto exit;
  10469. }
  10470. /* Read the header length from the file */
  10471. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10472. read_size = 1;
  10473. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10474. read_buf);
  10475. if (bytes_read != read_size) {
  10476. ret = -EX_ERR_FREAD;
  10477. pt_debug(dev, DL_ERROR,
  10478. "%s Failed to bin file header len\n", __func__);
  10479. goto exit_close_file;
  10480. }
  10481. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10482. if (hdr_len == 0xFF) {
  10483. ret = -EX_ERR_FLEN;
  10484. pt_debug(dev, DL_ERROR,
  10485. "%s Bin header len is invalid\n", __func__);
  10486. goto exit_close_file;
  10487. }
  10488. /* Read the rest of the header from the bin file */
  10489. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10490. memset(read_buf, 0, sizeof(read_buf));
  10491. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10492. read_buf);
  10493. if (bytes_read != hdr_len) {
  10494. ret = -EX_ERR_FREAD;
  10495. pt_debug(dev, DL_ERROR,
  10496. "%s Failed to read bin file\n", __func__);
  10497. goto exit_close_file;
  10498. }
  10499. load_hdr_struct = true;
  10500. exit_close_file:
  10501. /* Close the file */
  10502. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10503. ret = -EX_ERR_FCLOSE;
  10504. pt_debug(dev, DL_ERROR,
  10505. "%s Failed to close bin file\n", __func__);
  10506. }
  10507. /*
  10508. * The length was already read so subtract 1 to make the rest of
  10509. * the offsets match the spec
  10510. */
  10511. i = PIP2_RESP_BODY_OFFSET - 1;
  10512. }
  10513. if (load_hdr_struct) {
  10514. hdr->length = hdr_len;
  10515. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10516. hdr->fw_major = (read_buf[i+3]);
  10517. hdr->fw_minor = (read_buf[i+4]);
  10518. hdr->fw_crc = (read_buf[i+5] << 24) |
  10519. (read_buf[i+6] << 16) |
  10520. (read_buf[i+7] << 8) |
  10521. (read_buf[i+8]);
  10522. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10523. (read_buf[i+10] << 16) |
  10524. (read_buf[i+11] << 8) |
  10525. (read_buf[i+12]);
  10526. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10527. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10528. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10529. if (hdr_len >= 22) {
  10530. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10531. (read_buf[i+20] << 16) |
  10532. (read_buf[i+21] << 8) |
  10533. (read_buf[i+22]);
  10534. } else {
  10535. hdr->hex_file_size = 0;
  10536. }
  10537. }
  10538. exit:
  10539. return ret;
  10540. }
  10541. /*******************************************************************************
  10542. * FUNCTION: _pt_pip2_file_get_stats
  10543. *
  10544. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10545. * opened file
  10546. *
  10547. * NOTE: The DUT must be in BL mode for this command to work
  10548. *
  10549. * RETURNS:
  10550. * !0 = Error
  10551. * 0 = Success
  10552. *
  10553. * PARAMETERS:
  10554. * *dev - pointer to device structure
  10555. * file_handle - File handle to read from
  10556. * *address - pointer to store address of file
  10557. * *file_size _ pointer to store size of file
  10558. ******************************************************************************/
  10559. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10560. u32 *file_size)
  10561. {
  10562. int ret = 1;
  10563. u16 status;
  10564. u16 actual_read_len;
  10565. u8 data[2];
  10566. u8 read_buf[16];
  10567. data[0] = file_handle;
  10568. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10569. ret = _pt_request_pip2_send_cmd(dev,
  10570. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10571. data, 2, read_buf, &actual_read_len);
  10572. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10573. if (ret || (status != 0x00)) {
  10574. pt_debug(dev, DL_ERROR,
  10575. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10576. __func__, status, file_handle, ret);
  10577. ret = -EIO;
  10578. goto exit;
  10579. }
  10580. pt_debug(dev, DL_DEBUG,
  10581. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10582. if (address) {
  10583. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10584. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10585. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10586. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10587. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10588. __func__, *address);
  10589. }
  10590. if (file_size) {
  10591. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10592. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10593. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10594. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10595. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10596. __func__, *file_size);
  10597. }
  10598. exit:
  10599. return ret;
  10600. }
  10601. /*******************************************************************************
  10602. * FUNCTION: _pt_pip2_file_seek_offset
  10603. *
  10604. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10605. * opened file
  10606. *
  10607. * NOTE: The DUT must be in BL mode for this command to work
  10608. * NOTE: File open/erase command can reset the offset
  10609. *
  10610. * RETURNS:
  10611. * !0 = Error
  10612. * 0 = Success
  10613. *
  10614. * PARAMETERS:
  10615. * *dev - pointer to device structure
  10616. * file_handle - File handle to read from
  10617. * read_offset - read offset of file
  10618. * write_offset - write offset of file
  10619. ******************************************************************************/
  10620. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10621. u32 read_offset, u32 write_offset)
  10622. {
  10623. int ret = 1;
  10624. u16 status;
  10625. u16 actual_read_len;
  10626. u8 data[10];
  10627. u8 read_buf[16];
  10628. data[0] = file_handle;
  10629. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10630. data[2] = 0xff & read_offset;
  10631. data[3] = 0xff & (read_offset >> 8);
  10632. data[4] = 0xff & (read_offset >> 16);
  10633. data[5] = 0xff & (read_offset >> 24);
  10634. data[6] = 0xff & write_offset;
  10635. data[7] = 0xff & (write_offset >> 8);
  10636. data[8] = 0xff & (write_offset >> 16);
  10637. data[9] = 0xff & (write_offset >> 24);
  10638. ret = _pt_request_pip2_send_cmd(dev,
  10639. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10640. data, 10, read_buf, &actual_read_len);
  10641. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10642. if (ret || (status != 0x00)) {
  10643. pt_debug(dev, DL_ERROR,
  10644. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10645. __func__, status, ret, file_handle);
  10646. ret = -EIO;
  10647. }
  10648. return ret;
  10649. }
  10650. /*******************************************************************************
  10651. * FUNCTION: _pt_pip2_file_crc
  10652. *
  10653. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10654. * the file.
  10655. *
  10656. * NOTE: The DUT must be in BL mode for this command to work
  10657. * NOTE: This command only can be used for BL version 1.8 or greater.
  10658. * BL version 1.8 added this change according to PGV-173.
  10659. *
  10660. * RETURNS:
  10661. * !0 = Error
  10662. * 0 = Success
  10663. *
  10664. * PARAMETERS:
  10665. * *dev - pointer to device structure
  10666. * file_handle - File handle to read from
  10667. * offset - start offset for CRC calculation
  10668. * length - number of bytes to calculate CRC over
  10669. * read_buf - pointer to the read buffer
  10670. ******************************************************************************/
  10671. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10672. u32 offset, u32 length, u8 *read_buf)
  10673. {
  10674. int rc = 1;
  10675. u16 actual_read_len;
  10676. u8 data[10];
  10677. data[0] = file_handle;
  10678. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10679. data[2] = 0xff & offset;
  10680. data[3] = 0xff & (offset >> 8);
  10681. data[4] = 0xff & (offset >> 16);
  10682. data[5] = 0xff & (offset >> 24);
  10683. data[6] = 0xff & length;
  10684. data[7] = 0xff & (length >> 8);
  10685. data[8] = 0xff & (length >> 16);
  10686. data[9] = 0xff & (length >> 24);
  10687. rc = _pt_request_pip2_send_cmd(dev,
  10688. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10689. data, 10, read_buf, &actual_read_len);
  10690. if (rc)
  10691. pt_debug(dev, DL_ERROR,
  10692. "%s Return FILE_CRC failure, rc = %d\n",
  10693. __func__, rc);
  10694. return rc;
  10695. }
  10696. /*******************************************************************************
  10697. * FUNCTION: pt_pip2_ping_test
  10698. *
  10699. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10700. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10701. * response payload matches what was sent.
  10702. * The max payload size is 247:
  10703. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10704. *
  10705. * RETURN:
  10706. * 0 = success
  10707. * !0 = failure
  10708. *
  10709. * PARAMETERS:
  10710. * *dev - pointer to device structure
  10711. * *attr - pointer to device attributes
  10712. * *buf - pointer to output buffer
  10713. ******************************************************************************/
  10714. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10715. {
  10716. u16 actual_read_len;
  10717. u8 *read_buf = NULL;
  10718. u8 *data = NULL;
  10719. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10720. int i = 1;
  10721. int j = 0;
  10722. int rc = 0;
  10723. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10724. if (!read_buf)
  10725. goto ping_test_exit;
  10726. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10727. if (!data)
  10728. goto ping_test_exit;
  10729. /* Load data payload with an array of walking 1's */
  10730. for (i = 0; i < 255; i++)
  10731. data[i] = 0x01 << (i % 8);
  10732. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10733. for (i = 0; i <= max_bytes; i++) {
  10734. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10735. PIP2_CMD_ID_PING, data, i, read_buf,
  10736. &actual_read_len);
  10737. if (rc) {
  10738. pt_debug(dev, DL_ERROR,
  10739. "%s: PING failed with %d byte payload\n",
  10740. __func__, i);
  10741. break;
  10742. }
  10743. /* Verify data returned matches data sent */
  10744. for (j = 0; j < i; j++) {
  10745. if (read_buf[data_offset + j] != data[j]) {
  10746. pt_debug(dev, DL_DEBUG,
  10747. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10748. __func__, i, j, data[j], j,
  10749. read_buf[data_offset + j]);
  10750. goto ping_test_exit;
  10751. }
  10752. }
  10753. }
  10754. ping_test_exit:
  10755. *last_packet_size = i - 1;
  10756. kfree(read_buf);
  10757. kfree(data);
  10758. return rc;
  10759. }
  10760. /*******************************************************************************
  10761. * FUNCTION: _pt_ic_parse_input_hex
  10762. *
  10763. * SUMMARY: Parse a char data array as space delimited hex values into
  10764. * an int array.
  10765. *
  10766. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10767. * buffer to store data is "u32". This function is still needed by the
  10768. * "command" sysfs node because the buffer type to store data is "u8".
  10769. *
  10770. * RETURN: Length of parsed data
  10771. *
  10772. * PARAMETERS:
  10773. * *dev - pointer to device structure
  10774. * *buf - pointer to buffer that holds the input array to parse
  10775. * buf_size - size of buf
  10776. * *ic_buf - pointer to array to store parsed data
  10777. * ic_buf_size - max size of ic_buf
  10778. ******************************************************************************/
  10779. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10780. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10781. {
  10782. const char *pbuf = buf;
  10783. unsigned long value;
  10784. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10785. u32 i = 0;
  10786. u32 j;
  10787. int last = 0;
  10788. int ret;
  10789. pt_debug(dev, DL_DEBUG,
  10790. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10791. __func__, pbuf, buf, buf_size, "scan buf size",
  10792. PT_MAX_PIP2_MSG_SIZE, buf);
  10793. while (pbuf <= (buf + buf_size)) {
  10794. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10795. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10796. __func__, "Max cmd size exceeded", i,
  10797. PT_MAX_PIP2_MSG_SIZE);
  10798. return -EINVAL;
  10799. }
  10800. if (i >= ic_buf_size) {
  10801. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10802. __func__, "Buffer size exceeded", i,
  10803. ic_buf_size);
  10804. return -EINVAL;
  10805. }
  10806. while (((*pbuf == ' ') || (*pbuf == ','))
  10807. && (pbuf < (buf + buf_size))) {
  10808. last = *pbuf;
  10809. pbuf++;
  10810. }
  10811. if (pbuf >= (buf + buf_size))
  10812. break;
  10813. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10814. if ((last == ',') && (*pbuf == ',')) {
  10815. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10816. __func__, "Invalid data format.");
  10817. return -EINVAL;
  10818. }
  10819. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10820. && (pbuf < (buf + buf_size))
  10821. && (*pbuf != ' ')
  10822. && (*pbuf != ','); j++) {
  10823. last = *pbuf;
  10824. scan_buf[j] = *pbuf++;
  10825. }
  10826. ret = kstrtoul(scan_buf, 16, &value);
  10827. if (ret < 0) {
  10828. pt_debug(dev, DL_ERROR,
  10829. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10830. "Invalid data format. ", scan_buf,
  10831. "Use \"0xHH,...,0xHH\"", " instead.",
  10832. i, ret);
  10833. return ret;
  10834. }
  10835. ic_buf[i] = value;
  10836. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10837. __func__, i, value);
  10838. i++;
  10839. }
  10840. return i;
  10841. }
  10842. /*******************************************************************************
  10843. * FUNCTION: _pt_ic_parse_input
  10844. *
  10845. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10846. * hex values or dec values into an int array with the following rules:
  10847. * 1) Hex values must have a "0x" prefix for each element or the first element
  10848. * only
  10849. * 2) Dec values do not have any prefix
  10850. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10851. * string
  10852. *
  10853. * RETURN: Number of values parsed
  10854. *
  10855. * PARAMETERS:
  10856. * *dev - pointer to device structure
  10857. * *buf - pointer to buffer that holds the input array to parse
  10858. * buf_size - size of buf
  10859. * *out_buf - pointer to array to store parsed data
  10860. * out_buf_size - max size of buffer to be stored
  10861. ******************************************************************************/
  10862. static int _pt_ic_parse_input(struct device *dev,
  10863. const char *buf, size_t buf_size,
  10864. u32 *out_buf, size_t out_buf_size)
  10865. {
  10866. const char *pbuf = buf;
  10867. unsigned long value;
  10868. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10869. u32 i = 0;
  10870. u32 j;
  10871. int last = 0;
  10872. int ret = 0;
  10873. u8 str_base = 0;
  10874. pt_debug(dev, DL_DEBUG,
  10875. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10876. __func__, buf_size, out_buf_size, "scan buf size",
  10877. PT_MAX_PIP2_MSG_SIZE, buf);
  10878. while (pbuf <= (buf + buf_size)) {
  10879. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10880. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10881. __func__, "Max cmd size exceeded", i,
  10882. PT_MAX_PIP2_MSG_SIZE);
  10883. ret = -EINVAL;
  10884. goto error;
  10885. }
  10886. if (i >= out_buf_size) {
  10887. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10888. __func__, "Buffer size exceeded", i,
  10889. out_buf_size);
  10890. ret = -EINVAL;
  10891. goto error;
  10892. }
  10893. while (((*pbuf == ' ') || (*pbuf == ','))
  10894. && (pbuf < (buf + buf_size))) {
  10895. last = *pbuf;
  10896. pbuf++;
  10897. }
  10898. if (pbuf >= (buf + buf_size))
  10899. break;
  10900. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10901. if ((last == ',') && (*pbuf == ',')) {
  10902. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10903. __func__, "Invalid data format.");
  10904. ret = -EINVAL;
  10905. goto error;
  10906. }
  10907. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10908. && (pbuf < (buf + buf_size))
  10909. && (*pbuf != ' ')
  10910. && (*pbuf != ','); j++) {
  10911. last = *pbuf;
  10912. scan_buf[j] = *pbuf++;
  10913. }
  10914. if (i == 0) {
  10915. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10916. (strncmp(scan_buf, "0X", 2) == 0))
  10917. str_base = 16;
  10918. else
  10919. str_base = 10;
  10920. } else {
  10921. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10922. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10923. (str_base == 10)) {
  10924. pt_debug(dev, DL_ERROR,
  10925. "%s: Decimal and Heximal data mixed\n",
  10926. __func__);
  10927. ret = -EINVAL;
  10928. goto error;
  10929. }
  10930. }
  10931. ret = kstrtoul(scan_buf, str_base, &value);
  10932. if (ret < 0) {
  10933. pt_debug(dev, DL_ERROR,
  10934. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10935. "Invalid data format. ", scan_buf,
  10936. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10937. " instead.", i, ret);
  10938. goto error;
  10939. }
  10940. out_buf[i] = value;
  10941. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10942. __func__, i, value, value);
  10943. i++;
  10944. }
  10945. ret = i;
  10946. error:
  10947. return ret;
  10948. }
  10949. #ifdef TTHE_TUNER_SUPPORT
  10950. /*******************************************************************************
  10951. * FUNCTION: tthe_debugfs_open
  10952. *
  10953. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10954. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10955. * printing a large number of fingers and sensor data without overflowing
  10956. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10957. * even multiple of PAGE_SIZE
  10958. *
  10959. * RETURN:
  10960. * 0 = success
  10961. * !0 = failure
  10962. *
  10963. * PARAMETERS:
  10964. * *inode - file inode number
  10965. * *filp - file pointer to debugfs file
  10966. ******************************************************************************/
  10967. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10968. {
  10969. struct pt_core_data *cd = inode->i_private;
  10970. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10971. filp->private_data = inode->i_private;
  10972. if (cd->tthe_buf)
  10973. return -EBUSY;
  10974. while (buf_size < 4096)
  10975. buf_size = buf_size << 1;
  10976. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10977. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10978. cd->tthe_buf_size = buf_size;
  10979. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10980. if (!cd->tthe_buf)
  10981. return -ENOMEM;
  10982. return 0;
  10983. }
  10984. /*******************************************************************************
  10985. * FUNCTION: tthe_debugfs_close
  10986. *
  10987. * SUMMARY: Close method for tthe_tuner debugfs node.
  10988. *
  10989. * RETURN:
  10990. * 0 = success
  10991. * !0 = failure
  10992. *
  10993. * PARAMETERS:
  10994. * *inode - file inode number
  10995. * *filp - file pointer to debugfs file
  10996. ******************************************************************************/
  10997. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10998. {
  10999. struct pt_core_data *cd = filp->private_data;
  11000. filp->private_data = NULL;
  11001. kfree(cd->tthe_buf);
  11002. cd->tthe_buf = NULL;
  11003. return 0;
  11004. }
  11005. /*******************************************************************************
  11006. * FUNCTION: tthe_debugfs_read
  11007. *
  11008. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  11009. * tthe_buf to user buffer.
  11010. *
  11011. * RETURN: Size of debugfs data print
  11012. *
  11013. * PARAMETERS:
  11014. * *filp - file pointer to debugfs file
  11015. * *buf - the user space buffer to read to
  11016. * count - the maximum number of bytes to read
  11017. * *ppos - the current position in the buffer
  11018. ******************************************************************************/
  11019. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  11020. size_t count, loff_t *ppos)
  11021. {
  11022. struct pt_core_data *cd = filp->private_data;
  11023. int size;
  11024. int ret;
  11025. static int partial_read;
  11026. wait_event_interruptible(cd->wait_q,
  11027. cd->tthe_buf_len != 0 || cd->tthe_exit);
  11028. mutex_lock(&cd->tthe_lock);
  11029. if (cd->tthe_exit) {
  11030. mutex_unlock(&cd->tthe_lock);
  11031. return 0;
  11032. }
  11033. if (count > cd->tthe_buf_len)
  11034. size = cd->tthe_buf_len;
  11035. else
  11036. size = count;
  11037. if (!size) {
  11038. mutex_unlock(&cd->tthe_lock);
  11039. return 0;
  11040. }
  11041. if (partial_read) {
  11042. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  11043. partial_read = 0;
  11044. } else {
  11045. ret = copy_to_user(buf, cd->tthe_buf, size);
  11046. }
  11047. if (size == count)
  11048. partial_read = count;
  11049. if (ret == size)
  11050. return -EFAULT;
  11051. size -= ret;
  11052. cd->tthe_buf_len -= size;
  11053. mutex_unlock(&cd->tthe_lock);
  11054. *ppos += size;
  11055. return size;
  11056. }
  11057. static const struct file_operations tthe_debugfs_fops = {
  11058. .open = tthe_debugfs_open,
  11059. .release = tthe_debugfs_close,
  11060. .read = tthe_debugfs_read,
  11061. };
  11062. #endif
  11063. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  11064. .start_bl = _pt_request_pip_start_bl,
  11065. .suspend_scanning = _pt_request_pip_suspend_scanning,
  11066. .resume_scanning = _pt_request_pip_resume_scanning,
  11067. .get_param = _pt_request_pip_get_param,
  11068. .set_param = _pt_request_pip_set_param,
  11069. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  11070. .get_config_row_size = _pt_request_pip_get_config_row_size,
  11071. .get_data_structure = _pt_request_pip_get_data_structure,
  11072. .run_selftest = _pt_request_pip_run_selftest,
  11073. .get_selftest_result = _pt_request_pip_get_selftest_result,
  11074. .load_self_test_param = _pt_request_pip_load_self_test_param,
  11075. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  11076. .calibrate_ext = _pt_request_pip_calibrate_ext,
  11077. .initialize_baselines = _pt_request_pip_initialize_baselines,
  11078. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  11079. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  11080. .read_data_block = _pt_request_pip_read_data_block,
  11081. .write_data_block = _pt_request_pip_write_data_block,
  11082. .user_cmd = _pt_request_pip_user_cmd,
  11083. .get_bl_info = _pt_request_pip_bl_get_information,
  11084. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  11085. .launch_app = _pt_request_pip_launch_app,
  11086. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  11087. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  11088. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  11089. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  11090. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  11091. .pip2_file_open = _pt_pip2_file_open,
  11092. .pip2_file_close = _pt_pip2_file_close,
  11093. .pip2_file_erase = _pt_pip2_file_erase,
  11094. .read_us_file = _pt_read_us_file,
  11095. .manage_cal_data = _pt_manage_local_cal_data,
  11096. .calc_crc = crc_ccitt_calculate,
  11097. #ifdef TTDL_DIAGNOSTICS
  11098. .pip2_file_read = _pt_pip2_file_read,
  11099. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  11100. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  11101. .pip2_file_crc = _pt_pip2_file_crc,
  11102. #endif
  11103. };
  11104. static struct pt_core_commands _pt_core_commands = {
  11105. .subscribe_attention = _pt_subscribe_attention,
  11106. .unsubscribe_attention = _pt_unsubscribe_attention,
  11107. .request_exclusive = _pt_request_exclusive,
  11108. .release_exclusive = _pt_release_exclusive,
  11109. .request_reset = _pt_request_reset,
  11110. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  11111. .request_enum = _pt_request_enum,
  11112. .request_sysinfo = _pt_request_sysinfo,
  11113. .request_loader_pdata = _pt_request_loader_pdata,
  11114. .request_stop_wd = _pt_request_stop_wd,
  11115. .request_start_wd = _pt_request_start_wd,
  11116. .request_get_mode = _pt_request_get_mode,
  11117. .request_active_pip_prot = _pt_request_active_pip_protocol,
  11118. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  11119. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  11120. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  11121. .request_dut_generation = _pt_request_dut_generation,
  11122. .request_hw_version = _pt_request_hw_version,
  11123. .parse_sysfs_input = _pt_ic_parse_input,
  11124. #ifdef TTHE_TUNER_SUPPORT
  11125. .request_tthe_print = _pt_request_tthe_print,
  11126. #endif
  11127. #ifdef TTDL_DIAGNOSTICS
  11128. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  11129. #endif
  11130. .nonhid_cmd = &_pt_core_nonhid_cmd,
  11131. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11132. };
  11133. struct pt_core_commands *pt_get_commands(void)
  11134. {
  11135. return &_pt_core_commands;
  11136. }
  11137. EXPORT_SYMBOL_GPL(pt_get_commands);
  11138. static DEFINE_MUTEX(core_list_lock);
  11139. static LIST_HEAD(core_list);
  11140. static DEFINE_MUTEX(module_list_lock);
  11141. static LIST_HEAD(module_list);
  11142. static int core_number;
  11143. /*******************************************************************************
  11144. * FUNCTION: pt_probe_module
  11145. *
  11146. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11147. *
  11148. * RETURN:
  11149. * 0 = success
  11150. * !0 = failure
  11151. *
  11152. * PARAMETERS:
  11153. * *cd - pointer to core data
  11154. * *module - pointer to module structure
  11155. ******************************************************************************/
  11156. static int pt_probe_module(struct pt_core_data *cd,
  11157. struct pt_module *module)
  11158. {
  11159. struct module_node *module_node;
  11160. int rc = 0;
  11161. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11162. if (!module_node)
  11163. return -ENOMEM;
  11164. module_node->module = module;
  11165. mutex_lock(&cd->module_list_lock);
  11166. list_add(&module_node->node, &cd->module_list);
  11167. mutex_unlock(&cd->module_list_lock);
  11168. rc = module->probe(cd->dev, &module_node->data);
  11169. if (rc) {
  11170. /*
  11171. * Remove from the list when probe fails
  11172. * in order not to call release
  11173. */
  11174. mutex_lock(&cd->module_list_lock);
  11175. list_del(&module_node->node);
  11176. mutex_unlock(&cd->module_list_lock);
  11177. kfree(module_node);
  11178. goto exit;
  11179. }
  11180. exit:
  11181. return rc;
  11182. }
  11183. /*******************************************************************************
  11184. * FUNCTION: pt_release_module
  11185. *
  11186. * SUMMARY: Call the release pointer and remove the module pointer from
  11187. * module_list.
  11188. *
  11189. * PARAMETERS:
  11190. * *cd - pointer to core data
  11191. * *module - pointer to module structure
  11192. ******************************************************************************/
  11193. static void pt_release_module(struct pt_core_data *cd,
  11194. struct pt_module *module)
  11195. {
  11196. struct module_node *m, *m_n;
  11197. mutex_lock(&cd->module_list_lock);
  11198. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11199. if (m->module == module) {
  11200. module->release(cd->dev, m->data);
  11201. list_del(&m->node);
  11202. kfree(m);
  11203. break;
  11204. }
  11205. mutex_unlock(&cd->module_list_lock);
  11206. }
  11207. /*******************************************************************************
  11208. * FUNCTION: pt_probe_modules
  11209. *
  11210. * SUMMARY: Iterate module_list and probe each module.
  11211. *
  11212. * PARAMETERS:
  11213. * *cd - pointer to core data
  11214. ******************************************************************************/
  11215. static void pt_probe_modules(struct pt_core_data *cd)
  11216. {
  11217. struct pt_module *m;
  11218. int rc = 0;
  11219. mutex_lock(&module_list_lock);
  11220. list_for_each_entry(m, &module_list, node) {
  11221. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11222. __func__, m->name);
  11223. rc = pt_probe_module(cd, m);
  11224. if (rc)
  11225. pt_debug(cd->dev, DL_ERROR,
  11226. "%s: Probe fails for module %s\n",
  11227. __func__, m->name);
  11228. }
  11229. mutex_unlock(&module_list_lock);
  11230. }
  11231. /*******************************************************************************
  11232. * FUNCTION: pt_release_modules
  11233. *
  11234. * SUMMARY: Iterate module_list and remove each module.
  11235. *
  11236. * PARAMETERS:
  11237. * *cd - pointer to core data
  11238. ******************************************************************************/
  11239. static void pt_release_modules(struct pt_core_data *cd)
  11240. {
  11241. struct pt_module *m;
  11242. mutex_lock(&module_list_lock);
  11243. list_for_each_entry(m, &module_list, node)
  11244. pt_release_module(cd, m);
  11245. mutex_unlock(&module_list_lock);
  11246. }
  11247. /*******************************************************************************
  11248. * FUNCTION: pt_get_core_data
  11249. *
  11250. * SUMMARY: Iterate core_list and get core data.
  11251. *
  11252. * RETURN:
  11253. * pointer to core data or null pointer if fail
  11254. *
  11255. * PARAMETERS:
  11256. * *id - pointer to core id
  11257. ******************************************************************************/
  11258. struct pt_core_data *pt_get_core_data(char *id)
  11259. {
  11260. struct pt_core_data *d;
  11261. list_for_each_entry(d, &core_list, node)
  11262. if (!strncmp(d->core_id, id, 20))
  11263. return d;
  11264. return NULL;
  11265. }
  11266. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11267. /*******************************************************************************
  11268. * FUNCTION: pt_add_core
  11269. *
  11270. * SUMMARY: Add core data to the core_list.
  11271. *
  11272. * PARAMETERS:
  11273. * *dev - pointer to device structure
  11274. ******************************************************************************/
  11275. static void pt_add_core(struct device *dev)
  11276. {
  11277. struct pt_core_data *d;
  11278. struct pt_core_data *cd = dev_get_drvdata(dev);
  11279. mutex_lock(&core_list_lock);
  11280. list_for_each_entry(d, &core_list, node)
  11281. if (d->dev == dev)
  11282. goto unlock;
  11283. list_add(&cd->node, &core_list);
  11284. unlock:
  11285. mutex_unlock(&core_list_lock);
  11286. }
  11287. /*******************************************************************************
  11288. * FUNCTION: pt_del_core
  11289. *
  11290. * SUMMARY: Remove core data from the core_list.
  11291. *
  11292. * PARAMETERS:
  11293. * *dev - pointer to device structure
  11294. ******************************************************************************/
  11295. static void pt_del_core(struct device *dev)
  11296. {
  11297. struct pt_core_data *d, *d_n;
  11298. mutex_lock(&core_list_lock);
  11299. list_for_each_entry_safe(d, d_n, &core_list, node)
  11300. if (d->dev == dev) {
  11301. list_del(&d->node);
  11302. goto unlock;
  11303. }
  11304. unlock:
  11305. mutex_unlock(&core_list_lock);
  11306. }
  11307. /*******************************************************************************
  11308. * FUNCTION: pt_register_module
  11309. *
  11310. * SUMMARY: Register the module to module_list and probe the module for each
  11311. * core.
  11312. *
  11313. * RETURN:
  11314. * 0 = success
  11315. * !0 = failure
  11316. *
  11317. * PARAMETERS:
  11318. * *module - pointer to module structure
  11319. ******************************************************************************/
  11320. int pt_register_module(struct pt_module *module)
  11321. {
  11322. struct pt_module *m;
  11323. struct pt_core_data *cd;
  11324. int rc = 0;
  11325. if (!module || !module->probe || !module->release)
  11326. return -EINVAL;
  11327. mutex_lock(&module_list_lock);
  11328. list_for_each_entry(m, &module_list, node)
  11329. if (m == module) {
  11330. rc = -EEXIST;
  11331. goto unlock;
  11332. }
  11333. list_add(&module->node, &module_list);
  11334. /* Probe the module for each core */
  11335. mutex_lock(&core_list_lock);
  11336. list_for_each_entry(cd, &core_list, node)
  11337. pt_probe_module(cd, module);
  11338. mutex_unlock(&core_list_lock);
  11339. unlock:
  11340. mutex_unlock(&module_list_lock);
  11341. return rc;
  11342. }
  11343. EXPORT_SYMBOL_GPL(pt_register_module);
  11344. /*******************************************************************************
  11345. * FUNCTION: pt_unregister_module
  11346. *
  11347. * SUMMARY: Release the module for each core and remove the module from
  11348. * module_list.
  11349. *
  11350. * RETURN:
  11351. * 0 = success
  11352. * !0 = failure
  11353. *
  11354. * PARAMETERS:
  11355. * *module - pointer to module structure
  11356. ******************************************************************************/
  11357. void pt_unregister_module(struct pt_module *module)
  11358. {
  11359. struct pt_module *m, *m_n;
  11360. struct pt_core_data *cd;
  11361. if (!module)
  11362. return;
  11363. mutex_lock(&module_list_lock);
  11364. /* Release the module for each core */
  11365. mutex_lock(&core_list_lock);
  11366. list_for_each_entry(cd, &core_list, node)
  11367. pt_release_module(cd, module);
  11368. mutex_unlock(&core_list_lock);
  11369. list_for_each_entry_safe(m, m_n, &module_list, node)
  11370. if (m == module) {
  11371. list_del(&m->node);
  11372. break;
  11373. }
  11374. mutex_unlock(&module_list_lock);
  11375. }
  11376. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11377. /*******************************************************************************
  11378. * FUNCTION: pt_get_module_data
  11379. *
  11380. * SUMMARY: Get module data from module_node by module_list.
  11381. *
  11382. * RETURN:
  11383. * pointer to module data
  11384. *
  11385. * PARAMETERS:
  11386. * *dev - pointer to device structure
  11387. * *module - pointer to module structure
  11388. ******************************************************************************/
  11389. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11390. {
  11391. struct pt_core_data *cd = dev_get_drvdata(dev);
  11392. struct module_node *m;
  11393. void *data = NULL;
  11394. mutex_lock(&cd->module_list_lock);
  11395. list_for_each_entry(m, &cd->module_list, node)
  11396. if (m->module == module) {
  11397. data = m->data;
  11398. break;
  11399. }
  11400. mutex_unlock(&cd->module_list_lock);
  11401. return data;
  11402. }
  11403. EXPORT_SYMBOL(pt_get_module_data);
  11404. #ifdef CONFIG_HAS_EARLYSUSPEND
  11405. /*******************************************************************************
  11406. * FUNCTION: pt_early_suspend
  11407. *
  11408. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11409. * attention list.
  11410. *
  11411. * PARAMETERS:
  11412. * *h - pointer to early_suspend structure
  11413. ******************************************************************************/
  11414. static void pt_early_suspend(struct early_suspend *h)
  11415. {
  11416. struct pt_core_data *cd =
  11417. container_of(h, struct pt_core_data, es);
  11418. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11419. }
  11420. /*******************************************************************************
  11421. * FUNCTION: pt_late_resume
  11422. *
  11423. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11424. * attention list.
  11425. *
  11426. * PARAMETERS:
  11427. * *h - pointer to early_suspend structure
  11428. ******************************************************************************/
  11429. static void pt_late_resume(struct early_suspend *h)
  11430. {
  11431. struct pt_core_data *cd =
  11432. container_of(h, struct pt_core_data, es);
  11433. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11434. }
  11435. /*******************************************************************************
  11436. * FUNCTION: pt_setup_early_suspend
  11437. *
  11438. * SUMMARY: Register early/suspend function to the system.
  11439. *
  11440. * PARAMETERS:
  11441. * *cd - pointer to core data
  11442. ******************************************************************************/
  11443. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11444. {
  11445. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11446. cd->es.suspend = pt_early_suspend;
  11447. cd->es.resume = pt_late_resume;
  11448. register_early_suspend(&cd->es);
  11449. }
  11450. #elif defined(CONFIG_DRM)
  11451. static void pt_resume_work(struct work_struct *work)
  11452. {
  11453. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11454. resume_work);
  11455. int rc = 0;
  11456. pt_debug(pt_data->dev, DL_INFO, "%s start ", __func__);
  11457. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11458. return;
  11459. rc = pt_core_easywake_off(pt_data);
  11460. if (rc < 0) {
  11461. pt_debug(pt_data->dev, DL_ERROR,
  11462. "%s: Error on wake\n", __func__);
  11463. }
  11464. pt_debug(pt_data->dev, DL_INFO, "%s touch to wake disabled ", __func__);
  11465. return;
  11466. }
  11467. static void pt_suspend_work(struct work_struct *work)
  11468. {
  11469. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11470. suspend_work);
  11471. int rc = 0;
  11472. pt_debug(pt_data->dev, DL_INFO, "%s start\n", __func__);
  11473. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11474. return;
  11475. rc = pt_core_easywake_on(pt_data);
  11476. if (rc < 0) {
  11477. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11478. return;
  11479. }
  11480. pt_debug(pt_data->dev, DL_INFO, "%s Exit touch to wake enabled\n", __func__);
  11481. return;
  11482. }
  11483. /*******************************************************************************
  11484. * FUNCTION: drm_notifier_callback
  11485. *
  11486. * SUMMARY: Call back function for DRM notifier to allow to call
  11487. * resume/suspend attention list.
  11488. *
  11489. * RETURN:
  11490. * 0 = success
  11491. *
  11492. * PARAMETERS:
  11493. * *self - pointer to notifier_block structure
  11494. * event - event type of fb notifier
  11495. * *data - pointer to fb_event structure
  11496. ******************************************************************************/
  11497. static int drm_notifier_callback(struct notifier_block *self,
  11498. unsigned long event, void *data)
  11499. {
  11500. struct pt_core_data *cd =
  11501. container_of(self, struct pt_core_data, fb_notifier);
  11502. struct drm_panel_notifier *evdata = data;
  11503. int *blank;
  11504. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11505. if (!evdata)
  11506. goto exit;
  11507. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11508. event == DRM_PANEL_EVENT_BLANK)) {
  11509. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11510. __func__, event);
  11511. goto exit;
  11512. }
  11513. if (cd->quick_boot)
  11514. goto exit;
  11515. blank = evdata->data;
  11516. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11517. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11518. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11519. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11520. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11521. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11522. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11523. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11524. __func__, event);
  11525. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11526. if (cd->fb_state != FB_ON) {
  11527. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11528. __func__);
  11529. cancel_work_sync(&cd->resume_offload_work);
  11530. cancel_work_sync(&cd->suspend_offload_work);
  11531. cancel_work_sync(&cd->resume_work);
  11532. cancel_work_sync(&cd->suspend_work);
  11533. queue_work(cd->pt_workqueue, &cd->resume_work);
  11534. #if defined(CONFIG_PM_SLEEP)
  11535. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11536. __func__);
  11537. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11538. pt_core_resume_(cd->dev);
  11539. #endif
  11540. cd->fb_state = FB_ON;
  11541. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11542. }
  11543. }
  11544. } else if (*blank == DRM_PANEL_BLANK_LP || *blank == DRM_PANEL_BLANK_POWERDOWN) {
  11545. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11546. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11547. if (cd->fb_state != FB_OFF) {
  11548. #if defined(CONFIG_PM_SLEEP)
  11549. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11550. __func__);
  11551. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11552. pt_core_suspend_(cd->dev);
  11553. #endif
  11554. cancel_work_sync(&cd->resume_offload_work);
  11555. cancel_work_sync(&cd->suspend_offload_work);
  11556. cancel_work_sync(&cd->resume_work);
  11557. cancel_work_sync(&cd->suspend_work);
  11558. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11559. cd->fb_state = FB_OFF;
  11560. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11561. }
  11562. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11563. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11564. __func__, event);
  11565. }
  11566. } else {
  11567. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11568. __func__, *blank);
  11569. }
  11570. exit:
  11571. return 0;
  11572. }
  11573. /*******************************************************************************
  11574. * FUNCTION: pt_setup_drm_notifier
  11575. *
  11576. * SUMMARY: Set up call back function into drm notifier.
  11577. *
  11578. * PARAMETERS:
  11579. * *cd - pointer to core data
  11580. ******************************************************************************/
  11581. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11582. {
  11583. cd->fb_state = FB_NONE;
  11584. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11585. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11586. if (!active_panel)
  11587. pt_debug(cd->dev, DL_ERROR,
  11588. "%s: Active panel not registered!\n", __func__);
  11589. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11590. if (!cd->pt_workqueue) {
  11591. pt_debug(cd->dev, DL_ERROR,
  11592. "%s: worker thread creation failed !\n", __func__);
  11593. }
  11594. if (cd->pt_workqueue) {
  11595. INIT_WORK(&cd->resume_work, pt_resume_work);
  11596. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11597. }
  11598. if (active_panel &&
  11599. drm_panel_notifier_register(active_panel,
  11600. &cd->fb_notifier) < 0)
  11601. pt_debug(cd->dev, DL_ERROR,
  11602. "%s: Register notifier failed!\n", __func__);
  11603. }
  11604. #elif defined(CONFIG_FB)
  11605. /*******************************************************************************
  11606. * FUNCTION: fb_notifier_callback
  11607. *
  11608. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11609. * resume/suspend attention list.
  11610. *
  11611. * RETURN:
  11612. * 0 = success
  11613. *
  11614. * PARAMETERS:
  11615. * *self - pointer to notifier_block structure
  11616. * event - event type of fb notifier
  11617. * *data - pointer to fb_event structure
  11618. ******************************************************************************/
  11619. static int fb_notifier_callback(struct notifier_block *self,
  11620. unsigned long event, void *data)
  11621. {
  11622. struct pt_core_data *cd =
  11623. container_of(self, struct pt_core_data, fb_notifier);
  11624. struct fb_event *evdata = data;
  11625. int *blank;
  11626. if (event != FB_EVENT_BLANK || !evdata)
  11627. goto exit;
  11628. blank = evdata->data;
  11629. if (*blank == FB_BLANK_UNBLANK) {
  11630. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11631. if (cd->fb_state != FB_ON) {
  11632. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11633. #if defined(CONFIG_PM_SLEEP)
  11634. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11635. pt_core_resume_(cd->dev);
  11636. #endif
  11637. cd->fb_state = FB_ON;
  11638. }
  11639. } else if (*blank == FB_BLANK_POWERDOWN) {
  11640. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11641. if (cd->fb_state != FB_OFF) {
  11642. #if defined(CONFIG_PM_SLEEP)
  11643. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11644. pt_core_suspend_(cd->dev);
  11645. #endif
  11646. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11647. cd->fb_state = FB_OFF;
  11648. }
  11649. }
  11650. exit:
  11651. return 0;
  11652. }
  11653. /*******************************************************************************
  11654. * FUNCTION: pt_setup_fb_notifier
  11655. *
  11656. * SUMMARY: Set up call back function into fb notifier.
  11657. *
  11658. * PARAMETERS:
  11659. * *cd - pointer to core data
  11660. ******************************************************************************/
  11661. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11662. {
  11663. int rc = 0;
  11664. cd->fb_state = FB_ON;
  11665. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11666. rc = fb_register_client(&cd->fb_notifier);
  11667. if (rc)
  11668. pt_debug(cd->dev, DL_ERROR,
  11669. "Unable to register fb_notifier: %d\n", rc);
  11670. }
  11671. #endif
  11672. /*******************************************************************************
  11673. * FUNCTION: pt_watchdog_work
  11674. *
  11675. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11676. * sleeping then this function simply returns. If the DUT is awake the
  11677. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11678. * somehow a response is waiting on the DUT that has not been read. If
  11679. * this occurs the message is simply consumed. If or once the IRQ is
  11680. * cleared, a PIP PING message is sent to the DUT and if the response
  11681. * is received the watchdog succeeds and exits, if no response is seen
  11682. * a startup is queued unless the maximum number of startups have already
  11683. * been attempted, in that case a BL is attempted.
  11684. *
  11685. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11686. * work thread
  11687. *
  11688. * RETURN: void
  11689. *
  11690. * PARAMETERS:
  11691. * *work - pointer to a work structure for the watchdog work queue
  11692. ******************************************************************************/
  11693. static void pt_watchdog_work(struct work_struct *work)
  11694. {
  11695. int rc = 0;
  11696. struct pt_core_data *cd = container_of(work,
  11697. struct pt_core_data, watchdog_work);
  11698. /*
  11699. * if found the current sleep_state is SS_SLEEPING
  11700. * then no need to request_exclusive, directly return
  11701. */
  11702. if (cd->sleep_state == SS_SLEEPING)
  11703. return;
  11704. #ifdef TTDL_DIAGNOSTICS
  11705. cd->watchdog_count++;
  11706. #endif /* TTDL_DIAGNOSTICS */
  11707. /*
  11708. * The first WD interval was extended to allow DDI to come up.
  11709. * If the WD interval is not the default then adjust timer to the
  11710. * current setting. The user can override value with drv_debug sysfs.
  11711. */
  11712. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11713. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11714. msecs_to_jiffies(cd->watchdog_interval));
  11715. }
  11716. if (pt_check_irq_asserted(cd)) {
  11717. #ifdef TTDL_DIAGNOSTICS
  11718. cd->watchdog_irq_stuck_count++;
  11719. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11720. #endif /* TTDL_DIAGNOSTICS */
  11721. pt_debug(cd->dev, DL_ERROR,
  11722. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11723. __func__);
  11724. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11725. }
  11726. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11727. if (rc < 0) {
  11728. pt_debug(cd->dev, DL_ERROR,
  11729. "%s: fail get exclusive ex=%p own=%p\n",
  11730. __func__, cd->exclusive_dev, cd->dev);
  11731. goto queue_startup;
  11732. }
  11733. rc = pt_pip_null_(cd);
  11734. if (release_exclusive(cd, cd->dev) < 0)
  11735. pt_debug(cd->dev, DL_ERROR,
  11736. "%s: fail to release exclusive\n", __func__);
  11737. queue_startup:
  11738. if (rc) {
  11739. #ifdef TTDL_DIAGNOSTICS
  11740. cd->watchdog_failed_access_count++;
  11741. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11742. #endif /* TTDL_DIAGNOSTICS */
  11743. pt_debug(cd->dev, DL_ERROR,
  11744. "%s: failed to access device in WD, retry count=%d\n",
  11745. __func__, cd->startup_retry_count);
  11746. /* Already tried FW upgrade because of watchdog but failed */
  11747. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11748. return;
  11749. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11750. /*
  11751. * Any wrapper function that trys to disable the
  11752. * WD killing this worker cannot be called here.
  11753. */
  11754. rc = request_exclusive(cd, cd->dev,
  11755. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11756. if (rc < 0) {
  11757. pt_debug(cd->dev, DL_ERROR,
  11758. "%s: fail get exclusive ex=%p own=%p\n",
  11759. __func__, cd->exclusive_dev, cd->dev);
  11760. goto exit;
  11761. }
  11762. cd->hw_detected = false;
  11763. cd->startup_status = STARTUP_STATUS_START;
  11764. pt_debug(cd->dev, DL_DEBUG,
  11765. "%s: Startup Status Reset\n", __func__);
  11766. rc = pt_dut_reset_and_wait(cd);
  11767. if (release_exclusive(cd, cd->dev) < 0)
  11768. pt_debug(cd->dev, DL_ERROR,
  11769. "%s: fail to release exclusive\n",
  11770. __func__);
  11771. if (!rc) {
  11772. cd->hw_detected = true;
  11773. if (!cd->flashless_dut)
  11774. pt_queue_enum(cd);
  11775. }
  11776. #ifdef TTDL_DIAGNOSTICS
  11777. cd->wd_xres_count++;
  11778. pt_debug(cd->dev, DL_ERROR,
  11779. "%s: Comm Failed - DUT reset [#%d]\n",
  11780. __func__, cd->wd_xres_count);
  11781. #endif /* TTDL_DIAGNOSTICS */
  11782. } else {
  11783. /*
  11784. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11785. * reset the part to regain communications, try to BL
  11786. */
  11787. pt_debug(cd->dev, DL_ERROR,
  11788. "%s: WD DUT access failure, Start FW Upgrade\n",
  11789. __func__);
  11790. #ifdef TTDL_DIAGNOSTICS
  11791. /*
  11792. * When diagnostics is enabled allow TTDL to keep
  11793. * trying to find the DUT. This allows the DUT to be
  11794. * hot swap-able while the host stays running. In
  11795. * production this may not be wanted as a customer
  11796. * may have several touch drivers and any driver
  11797. * that doesn't match the current DUT should give
  11798. * up trying and give up using the bus.
  11799. */
  11800. pt_debug(cd->dev, DL_INFO,
  11801. "%s: Resetting startup_retry_count\n",
  11802. __func__);
  11803. cd->startup_retry_count = 0;
  11804. #endif /* TTDL_DIAGNOSTICS */
  11805. /*
  11806. * Since fw may be broken,reset sysinfo ready flag
  11807. * to let upgrade function work.
  11808. */
  11809. mutex_lock(&cd->system_lock);
  11810. cd->sysinfo.ready = false;
  11811. mutex_unlock(&cd->system_lock);
  11812. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11813. pt_debug(cd->dev, DL_ERROR,
  11814. "%s: Queue Restart\n", __func__);
  11815. pt_queue_restart(cd);
  11816. } else
  11817. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11818. }
  11819. } else {
  11820. cd->hw_detected = true;
  11821. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11822. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11823. pt_debug(cd->dev, DL_ERROR,
  11824. "%s: HW detected but not enumerated\n",
  11825. __func__);
  11826. pt_queue_enum(cd);
  11827. }
  11828. }
  11829. exit:
  11830. pt_start_wd_timer(cd);
  11831. }
  11832. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11833. /*******************************************************************************
  11834. * FUNCTION: pt_watchdog_timer
  11835. *
  11836. * SUMMARY: The function that is called when the WD timer expires. If the
  11837. * watchdog work is not already busy schedule the watchdog work queue.
  11838. *
  11839. * RETURN: void
  11840. *
  11841. * PARAMETERS:
  11842. * handle - Handle to the watchdog timer
  11843. ******************************************************************************/
  11844. static void pt_watchdog_timer(unsigned long handle)
  11845. {
  11846. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11847. if (!cd)
  11848. return;
  11849. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11850. __func__);
  11851. if (!work_pending(&cd->watchdog_work))
  11852. schedule_work(&cd->watchdog_work);
  11853. }
  11854. #else
  11855. /*******************************************************************************
  11856. * FUNCTION: pt_watchdog_timer
  11857. *
  11858. * SUMMARY: The function that is called when the WD timer expires. If the
  11859. * watchdog work is not already busy schedule the watchdog work queue.
  11860. *
  11861. * RETURN: void
  11862. *
  11863. * PARAMETERS:
  11864. * *t - Pointer to timer list
  11865. ******************************************************************************/
  11866. static void pt_watchdog_timer(struct timer_list *t)
  11867. {
  11868. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11869. if (!cd)
  11870. return;
  11871. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11872. __func__);
  11873. if (!work_pending(&cd->watchdog_work))
  11874. schedule_work(&cd->watchdog_work);
  11875. }
  11876. #endif
  11877. /*******************************************************************************
  11878. * Core sysfs show and store functions
  11879. ******************************************************************************/
  11880. /*******************************************************************************
  11881. * FUNCTION: pt_hw_version_show
  11882. *
  11883. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11884. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11885. *
  11886. * RETURN: size of data written to sysfs node
  11887. *
  11888. * PARAMETERS:
  11889. * *dev - pointer to device structure
  11890. * *attr - pointer to device attributes structure
  11891. * *buf - pointer to print output buffer
  11892. ******************************************************************************/
  11893. static ssize_t pt_hw_version_show(struct device *dev,
  11894. struct device_attribute *attr, char *buf)
  11895. {
  11896. struct pt_core_data *cd = dev_get_drvdata(dev);
  11897. _pt_request_hw_version(dev, cd->hw_version);
  11898. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11899. }
  11900. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11901. /*******************************************************************************
  11902. * FUNCTION: pt_drv_version_show
  11903. *
  11904. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11905. * TTDL version information
  11906. *
  11907. * RETURN: Char buffer with printed TTDL version information
  11908. *
  11909. * PARAMETERS:
  11910. * *dev - pointer to device structure
  11911. * *attr - pointer to device attributes
  11912. * *buf - pointer to output buffer
  11913. ******************************************************************************/
  11914. static ssize_t pt_drv_version_show(struct device *dev,
  11915. struct device_attribute *attr, char *buf)
  11916. {
  11917. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11918. "Driver: %s\nVersion: %s\nDate: %s\n",
  11919. pt_driver_core_name, pt_driver_core_version,
  11920. pt_driver_core_date);
  11921. }
  11922. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11923. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11924. /*******************************************************************************
  11925. * FUNCTION: pt_fw_version_show
  11926. *
  11927. * SUMMARY: Show method for the fw_version sysfs node that will
  11928. * show the firmware, bootloader and PIP version information
  11929. *
  11930. * RETURN: Size of printed buffer
  11931. *
  11932. * PARAMETERS:
  11933. * *dev - pointer to device structure
  11934. * *attr - pointer to device attributes
  11935. * *buf - pointer to output buffer
  11936. ******************************************************************************/
  11937. static ssize_t pt_fw_version_show(struct device *dev,
  11938. struct device_attribute *attr, char *buf)
  11939. {
  11940. struct pt_core_data *cd = dev_get_drvdata(dev);
  11941. struct pt_ttdata *ttdata;
  11942. int rc = 0;
  11943. if (cd->mode == PT_MODE_OPERATIONAL)
  11944. rc = pt_hid_output_get_sysinfo_(cd);
  11945. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11946. __func__, cd->mode, cd->sysinfo.ready);
  11947. if (cd->sysinfo.ready)
  11948. ttdata = &cd->sysinfo.ttdata;
  11949. else
  11950. rc = -ENODATA;
  11951. if (!rc) {
  11952. return scnprintf(buf, strlen(buf),
  11953. "Status: %d\n"
  11954. "FW : %d.%d.%d\n"
  11955. "Config: %d\n"
  11956. "BL : %d.%d\n"
  11957. "PIP : %d.%d\n",
  11958. rc,
  11959. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11960. ttdata->revctrl,
  11961. ttdata->fw_ver_conf,
  11962. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11963. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11964. } else {
  11965. return scnprintf(buf, strlen(buf),
  11966. "Status: %d\n"
  11967. "FW : n/a\n"
  11968. "Config: n/a\n"
  11969. "BL : n/a\n"
  11970. "PIP : n/a\n",
  11971. rc);
  11972. }
  11973. }
  11974. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11975. /*******************************************************************************
  11976. * FUNCTION: pt_sysinfo_show
  11977. *
  11978. * SUMMARY: Show method for the sysinfo sysfs node that will
  11979. * show all the information from get system information command.
  11980. *
  11981. * RETURN: Size of printed buffer
  11982. *
  11983. * PARAMETERS:
  11984. * *dev - pointer to device structure
  11985. * *attr - pointer to device attributes
  11986. * *buf - pointer to output buffer
  11987. ******************************************************************************/
  11988. static ssize_t pt_sysinfo_show(struct device *dev,
  11989. struct device_attribute *attr, char *buf)
  11990. {
  11991. struct pt_core_data *cd = dev_get_drvdata(dev);
  11992. struct pt_sysinfo *si;
  11993. struct pt_ttdata *ttdata = NULL;
  11994. struct pt_sensing_conf_data *scd = NULL;
  11995. int rc = 0;
  11996. if (cd->mode == PT_MODE_OPERATIONAL) {
  11997. rc = pt_hid_output_get_sysinfo_(cd);
  11998. if (cd->sysinfo.ready) {
  11999. si = &cd->sysinfo;
  12000. ttdata = &si->ttdata;
  12001. scd = &si->sensing_conf_data;
  12002. } else
  12003. rc = -ENODATA;
  12004. } else
  12005. rc = -EPERM;
  12006. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  12007. __func__, cd->mode, cd->sysinfo.ready);
  12008. if (!rc && ttdata && scd) {
  12009. return scnprintf(buf, strlen(buf),
  12010. "Status: %d\n"
  12011. "pip_ver_major: 0x%02X\n"
  12012. "pip_ver_minor: 0x%02X\n"
  12013. "fw_pid : 0x%04X\n"
  12014. "fw_ver_major : 0x%02X\n"
  12015. "fw_ver_minor : 0x%02X\n"
  12016. "revctrl : 0x%08X\n"
  12017. "fw_ver_conf : 0x%04X\n"
  12018. "bl_ver_major : 0x%02X\n"
  12019. "bl_ver_minor : 0x%02X\n"
  12020. "jtag_id_h : 0x%04X\n"
  12021. "jtag_id_l : 0x%04X\n"
  12022. "mfg_id[0] : 0x%02X\n"
  12023. "mfg_id[1] : 0x%02X\n"
  12024. "mfg_id[2] : 0x%02X\n"
  12025. "mfg_id[3] : 0x%02X\n"
  12026. "mfg_id[4] : 0x%02X\n"
  12027. "mfg_id[5] : 0x%02X\n"
  12028. "mfg_id[6] : 0x%02X\n"
  12029. "mfg_id[7] : 0x%02X\n"
  12030. "post_code : 0x%04X\n"
  12031. "electrodes_x : 0x%02X\n"
  12032. "electrodes_y : 0x%02X\n"
  12033. "len_x : 0x%04X\n"
  12034. "len_y : 0x%04X\n"
  12035. "res_x : 0x%04X\n"
  12036. "res_y : 0x%04X\n"
  12037. "max_z : 0x%04X\n"
  12038. "origin_x : 0x%02X\n"
  12039. "origin_y : 0x%02X\n"
  12040. "panel_id : 0x%02X\n"
  12041. "btn : 0x%02X\n"
  12042. "scan_mode : 0x%02X\n"
  12043. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  12044. rc,
  12045. ttdata->pip_ver_major,
  12046. ttdata->pip_ver_minor,
  12047. ttdata->fw_pid,
  12048. ttdata->fw_ver_major,
  12049. ttdata->fw_ver_minor,
  12050. ttdata->revctrl,
  12051. ttdata->fw_ver_conf,
  12052. ttdata->bl_ver_major,
  12053. ttdata->bl_ver_minor,
  12054. ttdata->jtag_id_h,
  12055. ttdata->jtag_id_l,
  12056. ttdata->mfg_id[0],
  12057. ttdata->mfg_id[1],
  12058. ttdata->mfg_id[2],
  12059. ttdata->mfg_id[3],
  12060. ttdata->mfg_id[4],
  12061. ttdata->mfg_id[5],
  12062. ttdata->mfg_id[6],
  12063. ttdata->mfg_id[7],
  12064. ttdata->post_code,
  12065. scd->electrodes_x,
  12066. scd->electrodes_y,
  12067. scd->len_x,
  12068. scd->len_y,
  12069. scd->res_x,
  12070. scd->res_y,
  12071. scd->max_z,
  12072. scd->origin_x,
  12073. scd->origin_y,
  12074. scd->panel_id,
  12075. scd->btn,
  12076. scd->scan_mode,
  12077. scd->max_tch);
  12078. } else {
  12079. return scnprintf(buf, strlen(buf),
  12080. "Status: %d\n",
  12081. rc);
  12082. }
  12083. }
  12084. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  12085. /*******************************************************************************
  12086. * FUNCTION: pt_hw_reset_show
  12087. *
  12088. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  12089. * by toggling the XRES line and then calls the startup function to
  12090. * allow TTDL to re-enumerate the DUT.
  12091. * The printed value reflects the status of the full reset/enum.
  12092. *
  12093. * PARAMETERS:
  12094. * *dev - pointer to Device structure
  12095. * *attr - pointer to the device attribute structure
  12096. * *buf - pointer to buffer to print
  12097. ******************************************************************************/
  12098. static ssize_t pt_hw_reset_show(struct device *dev,
  12099. struct device_attribute *attr, char *buf)
  12100. {
  12101. struct pt_core_data *cd = dev_get_drvdata(dev);
  12102. int rc = 0;
  12103. int time = 0;
  12104. u8 reset_status = 0;
  12105. int t;
  12106. struct pt_hid_desc hid_desc;
  12107. memset(&hid_desc, 0, sizeof(hid_desc));
  12108. /* Only allow DUT reset if no active BL in progress */
  12109. mutex_lock(&cd->firmware_class_lock);
  12110. mutex_lock(&cd->system_lock);
  12111. cd->startup_state = STARTUP_NONE;
  12112. mutex_unlock(&(cd->system_lock));
  12113. pt_stop_wd_timer(cd);
  12114. /* ensure no left over exclusive access is still locked */
  12115. release_exclusive(cd, cd->dev);
  12116. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  12117. if (rc) {
  12118. mutex_unlock(&cd->firmware_class_lock);
  12119. pt_debug(cd->dev, DL_ERROR,
  12120. "%s: HW reset failed rc = %d\n", __func__, rc);
  12121. goto exit_hw_reset;
  12122. }
  12123. reset_status |= 0x01 << 0;
  12124. if (cd->flashless_dut) {
  12125. mutex_unlock(&cd->firmware_class_lock);
  12126. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  12127. msecs_to_jiffies(200));
  12128. if (IS_TMO(t)) {
  12129. pt_debug(dev, DL_ERROR,
  12130. "%s: Timeout waiting for FW update",
  12131. __func__);
  12132. rc = -ETIME;
  12133. goto exit_hw_reset;
  12134. } else {
  12135. pt_debug(dev, DL_INFO,
  12136. "%s: ----- Wait FW Loading ----",
  12137. __func__);
  12138. rc = _pt_request_wait_for_enum_state(
  12139. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  12140. if (rc) {
  12141. pt_debug(cd->dev, DL_ERROR,
  12142. "%s: No FW Sentinel detected rc = %d\n",
  12143. __func__, rc);
  12144. goto exit_hw_reset;
  12145. }
  12146. reset_status |= 0x01 << 1;
  12147. }
  12148. } else {
  12149. /* Wait for any sentinel */
  12150. rc = _pt_request_wait_for_enum_state(dev, 150,
  12151. STARTUP_STATUS_BL_RESET_SENTINEL |
  12152. STARTUP_STATUS_FW_RESET_SENTINEL);
  12153. if (rc) {
  12154. mutex_unlock(&cd->firmware_class_lock);
  12155. pt_debug(cd->dev, DL_ERROR,
  12156. "%s: No Sentinel detected rc = %d\n",
  12157. __func__, rc);
  12158. goto exit_hw_reset;
  12159. }
  12160. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12161. msleep(30);
  12162. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12163. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12164. if (rc < 0) {
  12165. pt_debug(cd->dev, DL_ERROR,
  12166. "%s: Error on getting HID descriptor r=%d\n",
  12167. __func__, rc);
  12168. goto exit_hw_reset;
  12169. }
  12170. cd->mode = pt_get_mode(cd, &hid_desc);
  12171. if (cd->mode == PT_MODE_BOOTLOADER)
  12172. rc = pt_hid_output_bl_launch_app_(cd);
  12173. } else {
  12174. if (cd->mode == PT_MODE_BOOTLOADER)
  12175. rc = pt_pip2_launch_app(dev,
  12176. PT_CORE_CMD_UNPROTECTED);
  12177. }
  12178. if (rc) {
  12179. mutex_unlock(&cd->firmware_class_lock);
  12180. pt_debug(cd->dev, DL_ERROR,
  12181. "%s: PIP launch app failed rc = %d\n",
  12182. __func__, rc);
  12183. goto exit_hw_reset;
  12184. }
  12185. mutex_unlock(&cd->firmware_class_lock);
  12186. reset_status |= 0x01 << 1;
  12187. msleep(20);
  12188. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12189. (cd->mode != PT_MODE_OPERATIONAL))
  12190. pt_queue_restart(cd);
  12191. else
  12192. pt_queue_enum(cd);
  12193. }
  12194. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12195. msleep(50);
  12196. pt_debug(cd->dev, DL_INFO,
  12197. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12198. __func__, time, STARTUP_STATUS_COMPLETE,
  12199. cd->startup_status);
  12200. time += 50;
  12201. }
  12202. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12203. rc = -ETIME;
  12204. goto exit_hw_reset;
  12205. }
  12206. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12207. __func__, cd->startup_status);
  12208. reset_status |= 0x01 << 2;
  12209. pt_start_wd_timer(cd);
  12210. exit_hw_reset:
  12211. return scnprintf(buf, strlen(buf),
  12212. "Status: %d\n"
  12213. "Reset Status: 0x%02X\n", rc, reset_status);
  12214. }
  12215. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12216. /*******************************************************************************
  12217. * FUNCTION: pt_pip2_cmd_rsp_store
  12218. *
  12219. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12220. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12221. * Command byte order:
  12222. * Byte [0] - PIP2 command ID
  12223. * Byte [1-n] - PIP2 command payload
  12224. *
  12225. * RETURN: Size of passed in buffer
  12226. *
  12227. * PARAMETERS:
  12228. * *dev - pointer to device structure
  12229. * *attr - pointer to device attributes
  12230. * *buf - pointer to buffer that hold the command parameters
  12231. * size - size of buf
  12232. ******************************************************************************/
  12233. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12234. struct device_attribute *attr, const char *buf, size_t size)
  12235. {
  12236. struct pt_core_data *cd = dev_get_drvdata(dev);
  12237. u16 actual_read_len;
  12238. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12239. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12240. u8 pip2_cmd_id = 0x00;
  12241. u8 *pip2_cmd_data = NULL;
  12242. int data_len = 0;
  12243. int length;
  12244. int rc = 0;
  12245. /* clear shared data */
  12246. mutex_lock(&cd->sysfs_lock);
  12247. cd->raw_cmd_status = 0;
  12248. cd->cmd_rsp_buf_len = 0;
  12249. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12250. mutex_unlock(&cd->sysfs_lock);
  12251. length = _pt_ic_parse_input_hex(dev, buf, size,
  12252. input_data, PT_MAX_PIP2_MSG_SIZE);
  12253. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12254. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12255. __func__);
  12256. rc = -EINVAL;
  12257. goto exit;
  12258. }
  12259. /* Send PIP2 command if enough data was provided */
  12260. if (length >= 1) {
  12261. pip2_cmd_id = input_data[0];
  12262. pip2_cmd_data = &input_data[1];
  12263. data_len = length - 1;
  12264. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12265. pip2_cmd_id, pip2_cmd_data, data_len,
  12266. read_buf, &actual_read_len);
  12267. cd->raw_cmd_status = rc;
  12268. if (rc) {
  12269. pt_debug(dev, DL_ERROR,
  12270. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12271. __func__, pip2_cmd_id, rc);
  12272. goto exit;
  12273. } else {
  12274. cd->cmd_rsp_buf_len = actual_read_len;
  12275. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12276. pt_debug(dev, DL_ERROR,
  12277. "%s: PIP2 actual_read_len = %d\n",
  12278. __func__, actual_read_len);
  12279. }
  12280. } else {
  12281. rc = -EINVAL;
  12282. pt_debug(dev, DL_ERROR,
  12283. "%s: Insufficient data provided for PIP2 cmd\n",
  12284. __func__);
  12285. }
  12286. exit:
  12287. if (rc)
  12288. return rc;
  12289. return size;
  12290. }
  12291. /*******************************************************************************
  12292. * FUNCTION: pt_pip2_cmd_rsp_show
  12293. *
  12294. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12295. * response generated after using the store method of the pip2_cmd_rsp
  12296. * sysfs node, are available to be read here.
  12297. *
  12298. * PARAMETERS:
  12299. * *dev - pointer to Device structure
  12300. * *attr - pointer to the device attribute structure
  12301. * *buf - pointer to buffer to print
  12302. ******************************************************************************/
  12303. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12304. struct device_attribute *attr, char *buf)
  12305. {
  12306. struct pt_core_data *cd = dev_get_drvdata(dev);
  12307. int i;
  12308. ssize_t data_len;
  12309. int index;
  12310. mutex_lock(&cd->sysfs_lock);
  12311. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12312. "Status: %d\n", cd->raw_cmd_status);
  12313. if (cd->raw_cmd_status)
  12314. goto error;
  12315. /* Remove the CRC from the length of the response */
  12316. data_len = cd->cmd_rsp_buf_len - 2;
  12317. /* Start printing from the data payload */
  12318. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12319. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12320. "%02X ", cd->cmd_rsp_buf[i]);
  12321. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12322. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12323. "\n(%zd bytes)\n",
  12324. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12325. } else {
  12326. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12327. "\n(%zd bytes)\n", 0);
  12328. }
  12329. error:
  12330. mutex_unlock(&cd->sysfs_lock);
  12331. return index;
  12332. }
  12333. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12334. pt_pip2_cmd_rsp_store);
  12335. /*******************************************************************************
  12336. * FUNCTION: pt_command_store
  12337. *
  12338. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12339. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12340. * TTDL will not parse the command.
  12341. *
  12342. * RETURN: Size of passed in buffer
  12343. *
  12344. * PARAMETERS:
  12345. * *dev - pointer to device structure
  12346. * *attr - pointer to device attributes
  12347. * *buf - pointer to buffer that hold the command parameters
  12348. * size - size of buf
  12349. ******************************************************************************/
  12350. static ssize_t pt_command_store(struct device *dev,
  12351. struct device_attribute *attr, const char *buf, size_t size)
  12352. {
  12353. struct pt_core_data *cd = dev_get_drvdata(dev);
  12354. unsigned short crc;
  12355. u16 actual_read_len;
  12356. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12357. int length;
  12358. int len_field;
  12359. int rc = 0;
  12360. mutex_lock(&cd->sysfs_lock);
  12361. cd->cmd_rsp_buf_len = 0;
  12362. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12363. mutex_unlock(&cd->sysfs_lock);
  12364. length = _pt_ic_parse_input_hex(dev, buf, size,
  12365. input_data, PT_MAX_PIP2_MSG_SIZE);
  12366. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12367. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12368. __func__);
  12369. rc = -EINVAL;
  12370. goto pt_command_store_exit;
  12371. }
  12372. /* PIP2 messages begin with 01 01 */
  12373. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12374. cd->pip2_prot_active = 1;
  12375. /* Override next seq tag with what was sent */
  12376. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12377. /* For PIP2 cmd if length does not include crc, add it */
  12378. len_field = (input_data[3] << 8) | input_data[2];
  12379. if (len_field == length && length <= 254) {
  12380. crc = crc_ccitt_calculate(&input_data[2],
  12381. length - 2);
  12382. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12383. __func__, length, crc);
  12384. input_data[length] = (crc & 0xFF00) >> 8;
  12385. input_data[length + 1] = crc & 0x00FF;
  12386. length = length + 2;
  12387. }
  12388. }
  12389. /* write PIP command to log */
  12390. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12391. pm_runtime_get_sync(dev);
  12392. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12393. length, input_data, &actual_read_len);
  12394. pm_runtime_put(dev);
  12395. mutex_lock(&cd->sysfs_lock);
  12396. cd->raw_cmd_status = rc;
  12397. if (rc) {
  12398. cd->cmd_rsp_buf_len = 0;
  12399. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12400. __func__, buf);
  12401. } else {
  12402. cd->cmd_rsp_buf_len = actual_read_len;
  12403. }
  12404. cd->pip2_prot_active = 0;
  12405. mutex_unlock(&cd->sysfs_lock);
  12406. pt_command_store_exit:
  12407. if (rc)
  12408. return rc;
  12409. return size;
  12410. }
  12411. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12412. /*******************************************************************************
  12413. * FUNCTION: pt_response_show
  12414. *
  12415. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12416. * response generated after using the pt_command_store sysfs node, are
  12417. * available to be read here.
  12418. *
  12419. * PARAMETERS:
  12420. * *dev - pointer to Device structure
  12421. * *attr - pointer to the device attribute structure
  12422. * *buf - pointer to buffer to print
  12423. ******************************************************************************/
  12424. static ssize_t pt_response_show(struct device *dev,
  12425. struct device_attribute *attr, char *buf)
  12426. {
  12427. struct pt_core_data *cd = dev_get_drvdata(dev);
  12428. int i;
  12429. ssize_t num_read;
  12430. int index;
  12431. mutex_lock(&cd->sysfs_lock);
  12432. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12433. "Status: %d\n", cd->raw_cmd_status);
  12434. if (cd->raw_cmd_status)
  12435. goto error;
  12436. num_read = cd->cmd_rsp_buf_len;
  12437. for (i = 0; i < num_read; i++)
  12438. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12439. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12440. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12441. "(%zd bytes)\n", num_read);
  12442. error:
  12443. mutex_unlock(&cd->sysfs_lock);
  12444. return index;
  12445. }
  12446. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12447. /*******************************************************************************
  12448. * FUNCTION: pt_dut_debug_show
  12449. *
  12450. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12451. * are available for the store method.
  12452. *
  12453. * RETURN:
  12454. * 0 = success
  12455. * !0 = failure
  12456. *
  12457. * PARAMETERS:
  12458. * *dev - pointer to device structure
  12459. * *attr - pointer to device attributes
  12460. * *buf - pointer to output buffer
  12461. ******************************************************************************/
  12462. static ssize_t pt_dut_debug_show(struct device *dev,
  12463. struct device_attribute *attr, char *buf)
  12464. {
  12465. ssize_t ret;
  12466. ret = scnprintf(buf, strlen(buf),
  12467. "Status: 0\n"
  12468. "dut_debug sends the following commands to the DUT:\n"
  12469. "%d %s \t- %s\n"
  12470. "%d %s \t- %s\n"
  12471. "%d %s \t- %s\n"
  12472. "%d %s \t- %s\n"
  12473. "%d %s \t- %s\n"
  12474. "%d %s \t- %s\n"
  12475. "%d %s \t- %s\n"
  12476. "%d %s \t- %s\n"
  12477. "%d %s \t- %s\n"
  12478. "%d %s \t- %s\n"
  12479. "%d %s \t- %s\n"
  12480. "%d %s \t- %s\n"
  12481. "%d %s \t- %s\n"
  12482. "%d %s \t- %s\n"
  12483. "%d %s \t- %s\n"
  12484. "%d %s \t- %s\n"
  12485. "%d %s \t- %s\n"
  12486. ,
  12487. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12488. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12489. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12490. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12491. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12492. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12493. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12494. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12495. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12496. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12497. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12498. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12499. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12500. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12501. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12502. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12503. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12504. );
  12505. return ret;
  12506. }
  12507. /*******************************************************************************
  12508. * FUNCTION: pt_drv_debug_show
  12509. *
  12510. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12511. * are available for the store method.
  12512. *
  12513. * RETURN:
  12514. * 0 = success
  12515. * !0 = failure
  12516. *
  12517. * PARAMETERS:
  12518. * *dev - pointer to device structure
  12519. * *attr - pointer to device attributes
  12520. * *buf - pointer to output buffer
  12521. ******************************************************************************/
  12522. static ssize_t pt_drv_debug_show(struct device *dev,
  12523. struct device_attribute *attr, char *buf)
  12524. {
  12525. ssize_t ret;
  12526. ret = scnprintf(buf, strlen(buf),
  12527. "Status: 0\n"
  12528. "drv_debug supports the following values:\n"
  12529. "%d %s \t- %s\n"
  12530. "%d %s \t- %s\n"
  12531. "%d %s \t- %s\n"
  12532. "%d %s \t- %s\n"
  12533. "%d %s \t- %s\n"
  12534. "%d %s \t- %s\n"
  12535. "%d %s \t- %s\n"
  12536. "%d %s \t- %s\n"
  12537. "%d %s \t- %s\n"
  12538. "%d %s - %s\n"
  12539. "%d %s \t- %s\n"
  12540. "%d %s \t- %s\n"
  12541. #ifdef TTDL_DIAGNOSTICS
  12542. "%d %s \t- %s\n"
  12543. "%d %s \t- %s\n"
  12544. "%d %s \t- %s\n"
  12545. #endif /* TTDL_DIAGNOSTICS */
  12546. "%d %s \t- %s\n"
  12547. #ifdef TTDL_DIAGNOSTICS
  12548. "%d %s \t- %s\n"
  12549. "%d %s \t- %s\n"
  12550. "%d %s \t- %s\n"
  12551. "%d %s \t- %s\n"
  12552. "%d %s \t- %s\n"
  12553. "%d %s \t- %s\n"
  12554. "%d %s \t- %s\n"
  12555. "%d %s \t- %s\n"
  12556. "%d %s \t- %s\n"
  12557. #endif /* TTDL_DIAGNOSTICS */
  12558. ,
  12559. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12560. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12561. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12562. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12563. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12564. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12565. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12566. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12567. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12568. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12569. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12570. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12571. #ifdef TTDL_DIAGNOSTICS
  12572. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12573. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12574. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12575. #endif /* TTDL_DIAGNOSTICS */
  12576. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12577. #ifdef TTDL_DIAGNOSTICS
  12578. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12579. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12580. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12581. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12582. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12583. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12584. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12585. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12586. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12587. "TTHE_TUNER HID USB Format"
  12588. #endif /* TTDL_DIAGNOSTICS */
  12589. );
  12590. return ret;
  12591. }
  12592. /*******************************************************************************
  12593. * FUNCTION: pt_drv_debug_store
  12594. *
  12595. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12596. * dut_debug. Drv_debug will contain all functionality that can be run
  12597. * without a DUT preset and is available anytime TTDL is running.
  12598. * Dut_debug requires a DUT to be available and will only be created after
  12599. * a DUT has been detected.
  12600. * This function will eventually be split into two but until the overlap
  12601. * has been depricated this function contains all commands that can be
  12602. * used for TTDL/DUT debugging status and control.
  12603. * All commands require at least one value to be passed in *buf with some
  12604. * requiring two.
  12605. *
  12606. * RETURN: Size of passed in buffer
  12607. *
  12608. * PARAMETERS:
  12609. * *dev - pointer to device structure
  12610. * *attr - pointer to device attributes
  12611. * *buf - pointer to buffer that hold the command parameters
  12612. * size - size of buf
  12613. ******************************************************************************/
  12614. static ssize_t pt_drv_debug_store(struct device *dev,
  12615. struct device_attribute *attr, const char *buf, size_t size)
  12616. {
  12617. struct pt_core_data *cd = dev_get_drvdata(dev);
  12618. unsigned long value;
  12619. int rc = 0;
  12620. u8 return_data[8];
  12621. static u8 wd_disabled;
  12622. u32 input_data[3];
  12623. int length;
  12624. #ifdef TTDL_DIAGNOSTICS
  12625. struct i2c_client *client = to_i2c_client(dev);
  12626. unsigned short crc = 0;
  12627. u16 cal_size;
  12628. #endif
  12629. input_data[0] = 0;
  12630. input_data[1] = 0;
  12631. /* Maximmum input is two elements */
  12632. length = _pt_ic_parse_input(dev, buf, size,
  12633. input_data, ARRAY_SIZE(input_data));
  12634. if (length < 1 || length > 2) {
  12635. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12636. __func__);
  12637. rc = -EINVAL;
  12638. goto pt_drv_debug_store_exit;
  12639. }
  12640. value = input_data[0];
  12641. if (length == 1) {
  12642. pt_debug(dev, DL_DEBUG,
  12643. "%s: Debug Cmd Received (id=%d)\n",
  12644. __func__, input_data[0]);
  12645. } else if (length == 2) {
  12646. pt_debug(dev, DL_DEBUG,
  12647. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12648. __func__, input_data[0], input_data[1]);
  12649. } else {
  12650. pt_debug(dev, DL_DEBUG,
  12651. "%s: Invalid arguments received\n", __func__);
  12652. rc = -EINVAL;
  12653. goto pt_drv_debug_store_exit;
  12654. }
  12655. /* Start watchdog timer command */
  12656. if (value == PT_DRV_DBG_START_WD) {
  12657. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12658. wd_disabled = 0;
  12659. cd->watchdog_force_stop = false;
  12660. pt_start_wd_timer(cd);
  12661. goto pt_drv_debug_store_exit;
  12662. }
  12663. /* Stop watchdog timer temporarily */
  12664. pt_stop_wd_timer(cd);
  12665. if (value == PT_DRV_DBG_STOP_WD) {
  12666. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12667. wd_disabled = 1;
  12668. cd->watchdog_force_stop = true;
  12669. goto pt_drv_debug_store_exit;
  12670. }
  12671. switch (value) {
  12672. case PT_DRV_DBG_SUSPEND: /* 4 */
  12673. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12674. wd_disabled = 1;
  12675. rc = pt_core_sleep(cd);
  12676. if (rc)
  12677. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12678. __func__, rc);
  12679. else
  12680. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12681. __func__);
  12682. break;
  12683. case PT_DRV_DBG_RESUME: /* 5 */
  12684. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12685. rc = pt_core_wake(cd);
  12686. if (rc)
  12687. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12688. __func__, rc);
  12689. else
  12690. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12691. __func__);
  12692. break;
  12693. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12694. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12695. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12696. break;
  12697. case PT_DUT_DBG_HID_RESET: /* 50 */
  12698. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12699. pt_hid_cmd_reset(cd);
  12700. break;
  12701. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12702. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12703. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12704. wd_disabled = 0;
  12705. break;
  12706. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12707. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12708. __func__);
  12709. wd_disabled = 1;
  12710. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12711. break;
  12712. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12713. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12714. __func__);
  12715. wd_disabled = 1;
  12716. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12717. break;
  12718. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12719. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12720. pt_hid_output_bl_get_information(cd, return_data);
  12721. break;
  12722. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12723. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12724. __func__);
  12725. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12726. break;
  12727. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12728. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12729. pt_hid_output_bl_launch_app(cd);
  12730. break;
  12731. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12732. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12733. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12734. break;
  12735. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12736. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12737. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12738. break;
  12739. case PT_DUT_DBG_RESET: /* 98 */
  12740. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12741. rc = pt_hw_hard_reset(cd);
  12742. break;
  12743. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12744. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12745. pt_pip_null(cd);
  12746. break;
  12747. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12748. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12749. rc = pt_pip_start_bootloader(cd);
  12750. if (!rc) {
  12751. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12752. cd->mode = PT_MODE_BOOTLOADER;
  12753. }
  12754. break;
  12755. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12756. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12757. pt_hid_output_get_sysinfo(cd);
  12758. break;
  12759. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12760. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12761. pt_pip_suspend_scanning(cd);
  12762. break;
  12763. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12764. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12765. pt_pip_resume_scanning(cd);
  12766. break;
  12767. #ifdef TTHE_TUNER_SUPPORT
  12768. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12769. cd->tthe_exit = 1;
  12770. wake_up(&cd->wait_q);
  12771. kfree(cd->tthe_buf);
  12772. cd->tthe_buf = NULL;
  12773. cd->tthe_exit = 0;
  12774. break;
  12775. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12776. if (cd->tthe_buf)
  12777. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12778. else
  12779. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12780. __func__);
  12781. break;
  12782. #endif
  12783. #ifdef TTDL_DIAGNOSTICS
  12784. case PT_DUT_DBG_HID_DESC: /* 109 */
  12785. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12786. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12787. break;
  12788. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12789. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12790. __func__);
  12791. pt_erase_parameter_list(cd);
  12792. break;
  12793. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12794. rc = pt_read_input(cd);
  12795. if (!rc)
  12796. pt_parse_input(cd);
  12797. break;
  12798. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12799. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12800. &cal_size, &crc);
  12801. if (rc)
  12802. pt_debug(dev, DL_ERROR,
  12803. "%s: CAL Data clear failed rc=%d\n",
  12804. __func__, rc);
  12805. else
  12806. pt_debug(dev, DL_INFO,
  12807. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12808. __func__, crc, size);
  12809. break;
  12810. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12811. mutex_lock(&cd->system_lock);
  12812. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12813. cd->debug_level = input_data[1];
  12814. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12815. __func__, cd->debug_level);
  12816. } else {
  12817. rc = -EINVAL;
  12818. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12819. __func__, input_data[1]);
  12820. }
  12821. mutex_unlock(&(cd->system_lock));
  12822. break;
  12823. #endif
  12824. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12825. mutex_lock(&cd->system_lock);
  12826. if (input_data[1] > 100) {
  12827. cd->watchdog_interval = input_data[1];
  12828. pt_debug(dev, DL_INFO,
  12829. "%s: Set watchdog_ interval to: %d\n",
  12830. __func__, cd->watchdog_interval);
  12831. pt_start_wd_timer(cd);
  12832. } else {
  12833. rc = -EINVAL;
  12834. pt_debug(dev, DL_ERROR,
  12835. "%s: Invalid watchdog interval: %d\n",
  12836. __func__, input_data[1]);
  12837. }
  12838. mutex_unlock(&(cd->system_lock));
  12839. break;
  12840. #ifdef TTDL_DIAGNOSTICS
  12841. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12842. mutex_lock(&cd->system_lock);
  12843. if (input_data[1] == 0) {
  12844. cd->show_timestamp = 0;
  12845. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12846. __func__);
  12847. } else if (input_data[1] == 1) {
  12848. cd->show_timestamp = 1;
  12849. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12850. __func__);
  12851. } else {
  12852. rc = -EINVAL;
  12853. pt_debug(dev, DL_ERROR,
  12854. "%s: Invalid parameter: %d\n",
  12855. __func__, input_data[1]);
  12856. }
  12857. mutex_unlock(&(cd->system_lock));
  12858. break;
  12859. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12860. if (input_data[1] == 0) {
  12861. cd->cpdata->setup_power(cd->cpdata,
  12862. PT_MT_POWER_OFF, cd->dev);
  12863. pt_debug(dev, DL_INFO,
  12864. "%s: Initiate Power Off\n", __func__);
  12865. } else if (input_data[1] == 1) {
  12866. cd->cpdata->setup_power(cd->cpdata,
  12867. PT_MT_POWER_ON, cd->dev);
  12868. pt_debug(dev, DL_INFO,
  12869. "%s: Initiate Power On\n", __func__);
  12870. } else {
  12871. rc = -EINVAL;
  12872. pt_debug(dev, DL_ERROR,
  12873. "%s: Invalid parameter: %d\n",
  12874. __func__, input_data[1]);
  12875. }
  12876. break;
  12877. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12878. if (input_data[1] == 0) {
  12879. pm_runtime_put(dev);
  12880. pt_debug(dev, DL_ERROR,
  12881. "%s: Force call pm_runtime_put()\n", __func__);
  12882. } else if (input_data[1] == 1) {
  12883. pm_runtime_get_sync(dev);
  12884. pt_debug(dev, DL_ERROR,
  12885. "%s: Force call pm_runtime_get_sync()\n",
  12886. __func__);
  12887. } else {
  12888. rc = -EINVAL;
  12889. pt_debug(dev, DL_ERROR,
  12890. "%s: WARNING: Invalid parameter: %d\n",
  12891. __func__, input_data[1]);
  12892. }
  12893. break;
  12894. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12895. mutex_lock(&cd->system_lock);
  12896. if (input_data[1] == 0) {
  12897. cd->show_tt_data = false;
  12898. pt_debug(dev, DL_INFO,
  12899. "%s: Disable TT_DATA\n", __func__);
  12900. } else if (input_data[1] == 1) {
  12901. cd->show_tt_data = true;
  12902. pt_debug(dev, DL_INFO,
  12903. "%s: Enable TT_DATA\n", __func__);
  12904. } else {
  12905. rc = -EINVAL;
  12906. pt_debug(dev, DL_ERROR,
  12907. "%s: Invalid parameter: %d\n",
  12908. __func__, input_data[1]);
  12909. }
  12910. mutex_unlock(&(cd->system_lock));
  12911. break;
  12912. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12913. if (input_data[1] == cd->active_dut_generation) {
  12914. mutex_lock(&cd->system_lock);
  12915. cd->set_dut_generation = true;
  12916. mutex_unlock(&(cd->system_lock));
  12917. } else {
  12918. mutex_lock(&cd->system_lock);
  12919. if (input_data[1] == 0) {
  12920. cd->active_dut_generation = DUT_UNKNOWN;
  12921. cd->set_dut_generation = false;
  12922. } else if (input_data[1] == 1) {
  12923. cd->active_dut_generation = DUT_PIP1_ONLY;
  12924. cd->set_dut_generation = true;
  12925. } else if (input_data[1] == 2) {
  12926. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12927. cd->set_dut_generation = true;
  12928. } else {
  12929. rc = -EINVAL;
  12930. pt_debug(dev, DL_ERROR,
  12931. "%s: Invalid parameter: %d\n",
  12932. __func__, input_data[1]);
  12933. break;
  12934. }
  12935. cd->startup_status = STARTUP_STATUS_START;
  12936. pt_debug(cd->dev, DL_DEBUG,
  12937. "%s: Startup Status Reset\n", __func__);
  12938. mutex_unlock(&(cd->system_lock));
  12939. pt_debug(dev, DL_INFO,
  12940. "%s: Active DUT Generation Set to: %d\n",
  12941. __func__, cd->active_dut_generation);
  12942. /* Changing DUT generations full restart needed */
  12943. pt_queue_restart(cd);
  12944. }
  12945. break;
  12946. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12947. mutex_lock(&cd->system_lock);
  12948. if (input_data[1] == 0) {
  12949. cd->bridge_mode = false;
  12950. pt_debug(dev, DL_INFO,
  12951. "%s: Disable Bridge Mode\n", __func__);
  12952. } else if (input_data[1] == 1) {
  12953. cd->bridge_mode = true;
  12954. pt_debug(dev, DL_INFO,
  12955. "%s: Enable Bridge Mode\n", __func__);
  12956. } else {
  12957. rc = -EINVAL;
  12958. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12959. __func__, input_data[1]);
  12960. }
  12961. mutex_unlock(&(cd->system_lock));
  12962. break;
  12963. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12964. mutex_lock(&cd->system_lock);
  12965. /* Only a 7bit address is valid */
  12966. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12967. client->addr = input_data[1];
  12968. pt_debug(dev, DL_INFO,
  12969. "%s: Set I2C Address: 0x%2X\n",
  12970. __func__, client->addr);
  12971. } else {
  12972. rc = -EINVAL;
  12973. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12974. __func__, input_data[1]);
  12975. client->addr = 0x24;
  12976. }
  12977. mutex_unlock(&(cd->system_lock));
  12978. break;
  12979. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12980. mutex_lock(&cd->system_lock);
  12981. if (input_data[1] == 0) {
  12982. cd->flashless_dut = 0;
  12983. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12984. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12985. __func__);
  12986. } else if (input_data[1] == 1) {
  12987. cd->flashless_dut = 1;
  12988. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12989. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12990. __func__);
  12991. }
  12992. mutex_unlock(&(cd->system_lock));
  12993. break;
  12994. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12995. mutex_lock(&cd->system_lock);
  12996. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12997. cd->force_pip2_seq = input_data[1];
  12998. cd->pip2_cmd_tag_seq = input_data[1];
  12999. pt_debug(dev, DL_INFO,
  13000. "%s: Force PIP2 Seq to: 0x%02X\n",
  13001. __func__, input_data[1]);
  13002. } else {
  13003. cd->force_pip2_seq = 0;
  13004. pt_debug(dev, DL_INFO,
  13005. "%s: Clear Forced PIP2 Seq\n", __func__);
  13006. }
  13007. mutex_unlock(&(cd->system_lock));
  13008. break;
  13009. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  13010. mutex_lock(&cd->system_lock);
  13011. if (input_data[1] == 0) {
  13012. cd->bl_with_no_int = 0;
  13013. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  13014. } else if (input_data[1] == 1) {
  13015. cd->bl_with_no_int = 1;
  13016. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  13017. __func__);
  13018. } else {
  13019. rc = -EINVAL;
  13020. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13021. __func__, input_data[1]);
  13022. }
  13023. mutex_unlock(&(cd->system_lock));
  13024. break;
  13025. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  13026. mutex_lock(&cd->system_lock);
  13027. if (input_data[1] == 0) {
  13028. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  13029. pt_debug(dev, DL_INFO,
  13030. "%s: Disable Calibration cache in host\n",
  13031. __func__);
  13032. } else if (input_data[1] == 1) {
  13033. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  13034. pt_debug(dev, DL_INFO,
  13035. "%s: Enable Calibration cache in host\n",
  13036. __func__);
  13037. } else {
  13038. rc = -EINVAL;
  13039. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13040. __func__, input_data[1]);
  13041. }
  13042. mutex_unlock(&(cd->system_lock));
  13043. break;
  13044. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  13045. mutex_lock(&cd->system_lock);
  13046. if (input_data[1] == 0) {
  13047. cd->multi_chip = PT_FEATURE_DISABLE;
  13048. cd->ttdl_bist_select = 0x07;
  13049. pt_debug(dev, DL_INFO,
  13050. "%s: Disable Multi-chip support\n", __func__);
  13051. } else if (input_data[1] == 1) {
  13052. cd->multi_chip = PT_FEATURE_ENABLE;
  13053. cd->ttdl_bist_select = 0x3F;
  13054. pt_debug(dev, DL_INFO,
  13055. "%s: Enable Multi-chip support\n", __func__);
  13056. } else {
  13057. rc = -EINVAL;
  13058. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13059. __func__, input_data[1]);
  13060. }
  13061. mutex_unlock(&(cd->system_lock));
  13062. break;
  13063. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  13064. mutex_lock(&cd->system_lock);
  13065. if (input_data[1] <= 0x07) {
  13066. cd->panel_id_support = input_data[1];
  13067. pt_debug(dev, DL_INFO,
  13068. "%s: Set panel_id_support to %d\n",
  13069. __func__, cd->panel_id_support);
  13070. } else {
  13071. rc = -EINVAL;
  13072. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13073. __func__, input_data[1]);
  13074. }
  13075. mutex_unlock(&(cd->system_lock));
  13076. break;
  13077. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  13078. mutex_lock(&cd->system_lock);
  13079. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  13080. /*
  13081. * The timeout is changed for some cases so the
  13082. * pip_cmd_timeout_default is used to retore back to
  13083. * what the user requested as the new timeout.
  13084. */
  13085. cd->pip_cmd_timeout_default = input_data[1];
  13086. cd->pip_cmd_timeout = input_data[1];
  13087. pt_debug(dev, DL_INFO,
  13088. "%s: PIP Timeout = %d\n", __func__,
  13089. cd->pip_cmd_timeout_default);
  13090. } else {
  13091. rc = -EINVAL;
  13092. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13093. __func__, input_data[1]);
  13094. }
  13095. mutex_unlock(&(cd->system_lock));
  13096. break;
  13097. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  13098. mutex_lock(&cd->system_lock);
  13099. if (input_data[1] == 0) {
  13100. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  13101. pt_debug(dev, DL_INFO,
  13102. "%s: Disable tthe_tuner HID-USB format\n",
  13103. __func__);
  13104. } else if (input_data[1] == 1) {
  13105. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  13106. pt_debug(dev, DL_INFO,
  13107. "%s: Enable tthe_tuner HID-USB format\n",
  13108. __func__);
  13109. } else {
  13110. rc = -EINVAL;
  13111. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  13112. __func__, input_data[1]);
  13113. }
  13114. mutex_unlock(&(cd->system_lock));
  13115. break;
  13116. #endif /* TTDL_DIAGNOSTICS */
  13117. default:
  13118. rc = -EINVAL;
  13119. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13120. }
  13121. /* Enable watchdog timer */
  13122. if (!wd_disabled)
  13123. pt_start_wd_timer(cd);
  13124. pt_drv_debug_store_exit:
  13125. if (rc)
  13126. return rc;
  13127. return size;
  13128. }
  13129. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  13130. pt_drv_debug_store);
  13131. /*******************************************************************************
  13132. * FUNCTION: pt_sleep_status_show
  13133. *
  13134. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  13135. * sleep status as either ON or OFF
  13136. *
  13137. * RETURN:
  13138. * 0 = success
  13139. * !0 = failure
  13140. *
  13141. * PARAMETERS:
  13142. * *dev - pointer to device structure
  13143. * *attr - pointer to device attributes
  13144. * *buf - pointer to output buffer
  13145. ******************************************************************************/
  13146. static ssize_t pt_sleep_status_show(struct device *dev,
  13147. struct device_attribute *attr, char *buf)
  13148. {
  13149. struct pt_core_data *cd = dev_get_drvdata(dev);
  13150. ssize_t ret;
  13151. mutex_lock(&cd->system_lock);
  13152. if (cd->sleep_state == SS_SLEEP_ON)
  13153. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13154. else
  13155. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13156. mutex_unlock(&cd->system_lock);
  13157. return ret;
  13158. }
  13159. /*******************************************************************************
  13160. * FUNCTION: pt_panel_id_show
  13161. *
  13162. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13163. * detected panel ID from the DUT
  13164. *
  13165. * RETURN:
  13166. * 0 = success
  13167. * !0 = failure
  13168. *
  13169. * PARAMETERS:
  13170. * *dev - pointer to device structure
  13171. * *attr - pointer to device attributes
  13172. * *buf - pointer to output buffer
  13173. ******************************************************************************/
  13174. static ssize_t pt_panel_id_show(struct device *dev,
  13175. struct device_attribute *attr, char *buf)
  13176. {
  13177. struct pt_core_data *cd = dev_get_drvdata(dev);
  13178. ssize_t ret;
  13179. u8 pid = PANEL_ID_NOT_ENABLED;
  13180. int rc = 0;
  13181. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13182. /*
  13183. * The DUT should report the same panel ID from both the BL and
  13184. * the FW unless the panel_id feature is set to only
  13185. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13186. * to retrieve the panel_id.
  13187. */
  13188. if (cd->mode == PT_MODE_BOOTLOADER) {
  13189. /*
  13190. * Return the stored value if PT_PANEL_ID_BY_BL
  13191. * is not supported while other feature exits.
  13192. */
  13193. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13194. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13195. if (rc) {
  13196. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13197. "Failed to retrieve Panel ID. ",
  13198. "Using cached value\n",
  13199. __func__);
  13200. }
  13201. }
  13202. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13203. if (cd->panel_id_support &
  13204. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13205. /* For all systems sysinfo has the panel_id */
  13206. rc = pt_hid_output_get_sysinfo(cd);
  13207. if (!rc)
  13208. pid =
  13209. cd->sysinfo.sensing_conf_data.panel_id;
  13210. pt_debug(dev, DL_ERROR,
  13211. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13212. __func__, rc, pid);
  13213. }
  13214. } else {
  13215. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13216. __func__);
  13217. rc = -EPERM;
  13218. }
  13219. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13220. if (cd->mode == PT_MODE_BOOTLOADER) {
  13221. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13222. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13223. if (rc) {
  13224. pt_debug(dev, DL_ERROR,
  13225. "%s: BL get panel ID failed rc=%d\n",
  13226. __func__, rc);
  13227. }
  13228. }
  13229. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13230. if (cd->panel_id_support &
  13231. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13232. rc = pt_hid_output_get_sysinfo(cd);
  13233. if (!rc)
  13234. pid =
  13235. cd->sysinfo.sensing_conf_data.panel_id;
  13236. pt_debug(dev, DL_ERROR,
  13237. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13238. __func__, rc, pid);
  13239. }
  13240. } else {
  13241. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13242. __func__);
  13243. rc = -EPERM;
  13244. }
  13245. } else {
  13246. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13247. __func__);
  13248. rc = -EPERM;
  13249. }
  13250. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13251. rc, pid);
  13252. return ret;
  13253. }
  13254. /*******************************************************************************
  13255. * FUNCTION: pt_get_param_store
  13256. *
  13257. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13258. * ID to retrieve with the show method.
  13259. *
  13260. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13261. *
  13262. * RETURN: Size of passed in buffer if successful
  13263. *
  13264. * PARAMETERS:
  13265. * *dev - pointer to device structure
  13266. * *attr - pointer to device attributes
  13267. * *buf - pointer to output buffer
  13268. * size - size of buf
  13269. ******************************************************************************/
  13270. static ssize_t pt_get_param_store(struct device *dev,
  13271. struct device_attribute *attr, const char *buf, size_t size)
  13272. {
  13273. struct pt_core_data *cd = dev_get_drvdata(dev);
  13274. u32 input_data[2];
  13275. int length;
  13276. int rc = 0;
  13277. /* Maximum input of one value */
  13278. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13279. ARRAY_SIZE(input_data));
  13280. if (length != 1) {
  13281. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13282. __func__);
  13283. rc = -EINVAL;
  13284. goto exit;
  13285. }
  13286. mutex_lock(&cd->system_lock);
  13287. cd->get_param_id = input_data[0];
  13288. mutex_unlock(&(cd->system_lock));
  13289. exit:
  13290. if (rc)
  13291. return rc;
  13292. return size;
  13293. }
  13294. /*******************************************************************************
  13295. * FUNCTION: pt_get_param_show
  13296. *
  13297. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13298. * parameter data from the DUT based on the ID stored in the core
  13299. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13300. * communicate or some other error occures, an error status is returned
  13301. * with no value following.
  13302. * Output is in the form:
  13303. * Status: x
  13304. * 0xyyyyyyyy
  13305. * The 32bit data will only follow the status code if the status == 0
  13306. *
  13307. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13308. *
  13309. * RETURN:
  13310. * 0 = success
  13311. * !0 = failure
  13312. *
  13313. * PARAMETERS:
  13314. * *dev - pointer to device structure
  13315. * *attr - pointer to device attributes
  13316. * *buf - pointer to output buffer
  13317. ******************************************************************************/
  13318. static ssize_t pt_get_param_show(struct device *dev,
  13319. struct device_attribute *attr, char *buf)
  13320. {
  13321. struct pt_core_data *cd = dev_get_drvdata(dev);
  13322. ssize_t ret = 0;
  13323. int status;
  13324. u32 value = 0;
  13325. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13326. if (status) {
  13327. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13328. __func__, "pt_get_param", status);
  13329. ret = scnprintf(buf, strlen(buf),
  13330. "%s %d\n",
  13331. "Status:", status);
  13332. } else {
  13333. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13334. __func__, cd->get_param_id, value);
  13335. ret = scnprintf(buf, strlen(buf),
  13336. "Status: %d\n"
  13337. "0x%04X\n",
  13338. status, value);
  13339. }
  13340. return ret;
  13341. }
  13342. /*******************************************************************************
  13343. * FUNCTION: pt_ttdl_restart_show
  13344. *
  13345. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13346. * probed modules, calls startup() and then re-probes modules.
  13347. *
  13348. * RETURN: size of data written to sysfs node
  13349. *
  13350. * PARAMETERS:
  13351. * *dev - pointer to device structure
  13352. * *attr - pointer to device attributes structure
  13353. * *buf - pointer to print output buffer
  13354. ******************************************************************************/
  13355. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13356. struct device_attribute *attr, char *buf)
  13357. {
  13358. struct pt_core_data *cd = dev_get_drvdata(dev);
  13359. int t;
  13360. int rc = 0;
  13361. mutex_lock(&cd->system_lock);
  13362. cd->startup_state = STARTUP_NONE;
  13363. mutex_unlock(&(cd->system_lock));
  13364. /* ensure no left over exclusive access is still locked */
  13365. release_exclusive(cd, cd->dev);
  13366. pt_queue_restart(cd);
  13367. t = wait_event_timeout(cd->wait_q,
  13368. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13369. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13370. if (IS_TMO(t)) {
  13371. pt_debug(cd->dev, DL_ERROR,
  13372. "%s: TMO waiting for FW sentinel\n", __func__);
  13373. rc = -ETIME;
  13374. }
  13375. return scnprintf(buf, strlen(buf),
  13376. "Status: %d\n"
  13377. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13378. }
  13379. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13380. /*******************************************************************************
  13381. * FUNCTION: pt_pip2_gpio_read_show
  13382. *
  13383. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13384. * contents of the response to the passed in output buffer.
  13385. *
  13386. * RETURN: size of data written to sysfs node
  13387. *
  13388. * PARAMETERS:
  13389. * *dev - pointer to device structure
  13390. * *attr - pointer to device attributes structure
  13391. * *buf - pointer to print output buffer
  13392. ******************************************************************************/
  13393. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13394. struct device_attribute *attr, char *buf)
  13395. {
  13396. struct pt_core_data *cd = dev_get_drvdata(dev);
  13397. u8 status = 0;
  13398. u32 gpio_value = 0;
  13399. int rc = 0;
  13400. /* This functionality is only available in the BL */
  13401. if (cd->mode == PT_MODE_BOOTLOADER)
  13402. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13403. else
  13404. rc = -EPERM;
  13405. if (!rc) {
  13406. if (status == 0)
  13407. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13408. "Status: %d\n"
  13409. "DUT GPIO Reg: 0x%08X\n",
  13410. rc, gpio_value);
  13411. else
  13412. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13413. "Status: %d\n"
  13414. "DUT GPIO Reg: n/a\n",
  13415. status);
  13416. } else
  13417. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13418. "Status: %d\n"
  13419. "DUT GPIO Reg: n/a\n",
  13420. rc);
  13421. }
  13422. /*******************************************************************************
  13423. * FUNCTION: pt_pip2_version_show
  13424. *
  13425. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13426. * contents of the response to the passed in output buffer.
  13427. *
  13428. * RETURN: size of data written to sysfs node
  13429. *
  13430. * PARAMETERS:
  13431. * *dev - pointer to device structure
  13432. * *attr - pointer to device attributes structure
  13433. * *buf - pointer to print output buffer
  13434. ******************************************************************************/
  13435. static ssize_t pt_pip2_version_show(struct device *dev,
  13436. struct device_attribute *attr, char *buf)
  13437. {
  13438. int rc = 0;
  13439. struct pt_core_data *cd = dev_get_drvdata(dev);
  13440. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13441. rc = pt_pip2_get_version(cd);
  13442. if (!rc) {
  13443. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13444. "PIP VERSION : %02X.%02X\n"
  13445. "BL VERSION : %02X.%02X\n"
  13446. "FW VERSION : %02X.%02X\n"
  13447. "SILICON ID : %04X.%04X\n"
  13448. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13449. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13450. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13451. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13452. ttdata->chip_id, ttdata->chip_rev,
  13453. ttdata->uid[0], ttdata->uid[1],
  13454. ttdata->uid[2], ttdata->uid[3],
  13455. ttdata->uid[4], ttdata->uid[5],
  13456. ttdata->uid[6], ttdata->uid[7],
  13457. ttdata->uid[8], ttdata->uid[9],
  13458. ttdata->uid[10], ttdata->uid[11]);
  13459. } else {
  13460. pt_debug(dev, DL_ERROR,
  13461. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13462. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13463. "PIP VERSION : -\n"
  13464. "BL VERSION : -\n"
  13465. "FW VERSION : -\n"
  13466. "SILICON ID : -\n"
  13467. "UID : -\n");
  13468. }
  13469. }
  13470. #ifdef TTDL_DIAGNOSTICS
  13471. /*******************************************************************************
  13472. * FUNCTION: pt_ttdl_status_show
  13473. *
  13474. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13475. * variable states and GPIO levels. Additional information printed when
  13476. * TTDL_DIAGNOSTICS is enabled.
  13477. *
  13478. * NOTE: All counters will be reset to 0 when this function is called.
  13479. *
  13480. * RETURN:
  13481. * 0 = success
  13482. * !0 = failure
  13483. *
  13484. * PARAMETERS:
  13485. * *dev - pointer to device structure
  13486. * *attr - pointer to device attributes
  13487. * *buf - pointer to output buffer
  13488. ******************************************************************************/
  13489. static ssize_t pt_ttdl_status_show(struct device *dev,
  13490. struct device_attribute *attr, char *buf)
  13491. {
  13492. struct pt_core_data *cd = dev_get_drvdata(dev);
  13493. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13494. struct i2c_client *client = to_i2c_client(dev);
  13495. ssize_t ret;
  13496. u16 cal_size = 0;
  13497. unsigned short crc = 0;
  13498. if (cd->cal_cache_in_host)
  13499. _pt_manage_local_cal_data(dev,
  13500. PT_CAL_DATA_INFO, &cal_size, &crc);
  13501. ret = scnprintf(buf, strlen(buf),
  13502. "%s: 0x%04X\n"
  13503. "%s: %d\n"
  13504. "%s: %s\n"
  13505. "%s: %s %s\n"
  13506. "%s: %s\n"
  13507. "%s: 0x%02X\n"
  13508. "%s: %s\n"
  13509. "%s: %s\n"
  13510. "%s: %s\n"
  13511. "%s: %s\n"
  13512. "%s: %d\n"
  13513. "%s: %d\n"
  13514. "%s: %s\n"
  13515. "%s: %s\n"
  13516. "%s: %d\n"
  13517. #ifdef TTDL_DIAGNOSTICS
  13518. "%s: %d\n"
  13519. "%s: %d\n"
  13520. "%s: %d\n"
  13521. "%s: %d\n"
  13522. "%s: %d\n"
  13523. "%s: %d\n"
  13524. "%s: %d\n"
  13525. "%s: %d\n"
  13526. "%s: %d\n"
  13527. "%s: %d\n"
  13528. "%s: %s\n"
  13529. "%s: %s\n"
  13530. "%s: %s\n"
  13531. "%s: %d\n"
  13532. "%s: 0x%04X\n"
  13533. "%s: %s\n"
  13534. #endif /* TTDL_DIAGNOSTICS */
  13535. ,
  13536. "Startup Status ", cd->startup_status,
  13537. "TTDL Debug Level ", cd->debug_level,
  13538. "Mode ",
  13539. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13540. "Operational" : "BL") : "Unknown",
  13541. "DUT Generation ",
  13542. cd->active_dut_generation ?
  13543. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13544. "PT TC/TT" : "Gen5/6") : "Unknown",
  13545. cd->active_dut_generation ?
  13546. (cd->set_dut_generation == true ?
  13547. "(Set)" : "(Detected)") : "",
  13548. "HW Detected ",
  13549. cd->hw_detected ? "True" : "False",
  13550. "I2C Address ",
  13551. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13552. "Active Bus Module ",
  13553. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13554. "Flashless Mode ",
  13555. cd->flashless_dut == 1 ? "Yes" : "No",
  13556. "GPIO state - IRQ ",
  13557. cd->cpdata->irq_stat ?
  13558. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13559. "High" : "Low") : "not defined",
  13560. "GPIO state - TP_XRES ",
  13561. pdata->core_pdata->rst_gpio ?
  13562. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13563. "High" : "Low") : "not defined",
  13564. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13565. "Startup Retry Count ", cd->startup_retry_count,
  13566. "WD - Manual Force Stop ",
  13567. cd->watchdog_force_stop ? "True" : "False",
  13568. "WD - Enabled ",
  13569. cd->watchdog_enabled ? "True" : "False",
  13570. "WD - Interval (ms) ", cd->watchdog_interval
  13571. #ifdef TTDL_DIAGNOSTICS
  13572. , "WD - Triggered Count ", cd->watchdog_count,
  13573. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13574. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13575. "WD - XRES Count ", cd->wd_xres_count,
  13576. "IRQ Triggered Count ", cd->irq_count,
  13577. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13578. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13579. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13580. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13581. "Error GPIO trigger type ", cd->err_gpio_type,
  13582. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13583. "Suppress No-Flash Auto BL ",
  13584. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13585. "Yes" : "No",
  13586. "Calibration Cache on host ",
  13587. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13588. "Yes" : "No",
  13589. "Calibration Cache size ", cal_size,
  13590. "Calibration Cache chip ID ", crc,
  13591. "Multi-Chip Support ",
  13592. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13593. #endif /* TTDL_DIAGNOSTICS */
  13594. );
  13595. #ifdef TTDL_DIAGNOSTICS
  13596. /* Reset all diagnostic counters */
  13597. cd->watchdog_count = 0;
  13598. cd->watchdog_irq_stuck_count = 0;
  13599. cd->watchdog_failed_access_count = 0;
  13600. cd->wd_xres_count = 0;
  13601. cd->irq_count = 0;
  13602. cd->bl_retry_packet_count = 0;
  13603. cd->pip2_crc_error_count = 0;
  13604. cd->bus_transmit_error_count = 0;
  13605. #endif
  13606. return ret;
  13607. }
  13608. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13609. /*******************************************************************************
  13610. * FUNCTION: pt_pip2_enter_bl_show
  13611. *
  13612. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13613. * the DUT into the BL and show the success or failure of entering the BL
  13614. *
  13615. * RETURN:
  13616. * 0 = success
  13617. * !0 = failure
  13618. *
  13619. * PARAMETERS:
  13620. * *dev - pointer to device structure
  13621. * *attr - pointer to device attributes
  13622. * *buf - pointer to output buffer
  13623. ******************************************************************************/
  13624. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13625. struct device_attribute *attr, char *buf)
  13626. {
  13627. ssize_t ret = 0;
  13628. int rc = 0;
  13629. int result = 0;
  13630. u8 mode = PT_MODE_UNKNOWN;
  13631. struct pt_core_data *cd = dev_get_drvdata(dev);
  13632. /* Turn off the TTDL WD before enter bootloader */
  13633. pt_stop_wd_timer(cd);
  13634. /* Ensure NO enumeration work is queued or will be queued */
  13635. cancel_work_sync(&cd->enum_work);
  13636. mutex_lock(&cd->system_lock);
  13637. cd->bridge_mode = true;
  13638. mutex_unlock(&cd->system_lock);
  13639. /* set mode to operational to avoid any extra PIP traffic */
  13640. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13641. switch (result) {
  13642. case PT_ENTER_BL_PASS:
  13643. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13644. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13645. break;
  13646. case PT_ENTER_BL_ERROR:
  13647. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13648. rc,
  13649. " Unknown Error");
  13650. break;
  13651. case PT_ENTER_BL_RESET_FAIL:
  13652. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13653. rc,
  13654. " Soft Reset Failed");
  13655. break;
  13656. case PT_ENTER_BL_HID_START_BL_FAIL:
  13657. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13658. rc,
  13659. " PIP Start BL Cmd Failed");
  13660. break;
  13661. case PT_ENTER_BL_CONFIRM_FAIL:
  13662. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13663. rc,
  13664. " Error confirming DUT entered BL");
  13665. break;
  13666. default:
  13667. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13668. rc, " Unknown Error");
  13669. break;
  13670. };
  13671. /* Allow enumeration work to be queued again */
  13672. cd->bridge_mode = false;
  13673. return ret;
  13674. }
  13675. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13676. /*******************************************************************************
  13677. * FUNCTION: pt_pip2_exit_bl_show
  13678. *
  13679. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13680. * launch the APP and put the DUT Application mode
  13681. *
  13682. * RETURN:
  13683. * 0 = success
  13684. * !0 = failure
  13685. *
  13686. * PARAMETERS:
  13687. * *dev - pointer to device structure
  13688. * *attr - pointer to device attributes
  13689. * *buf - pointer to output buffer
  13690. ******************************************************************************/
  13691. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13692. struct device_attribute *attr, char *buf)
  13693. {
  13694. struct pt_core_data *cd = dev_get_drvdata(dev);
  13695. ssize_t ret = 0;
  13696. int rc = 0;
  13697. u8 status_str[PT_STATUS_STR_LEN];
  13698. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13699. /*
  13700. * Perform enum if startup_status doesn't reach to
  13701. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13702. */
  13703. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13704. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13705. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13706. strlcpy(status_str,
  13707. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13708. }
  13709. }
  13710. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13711. rc, status_str);
  13712. return ret;
  13713. }
  13714. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13715. #endif
  13716. #ifdef EASYWAKE_TSG6
  13717. /*******************************************************************************
  13718. * FUNCTION: pt_easy_wakeup_gesture_show
  13719. *
  13720. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13721. * current easy wakeup gesture
  13722. *
  13723. * RETURN:
  13724. * 0 = success
  13725. * !0 = failure
  13726. *
  13727. * PARAMETERS:
  13728. * *dev - pointer to device structure
  13729. * *attr - pointer to device attributes
  13730. * *buf - pointer to output buffer
  13731. ******************************************************************************/
  13732. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13733. struct device_attribute *attr, char *buf)
  13734. {
  13735. struct pt_core_data *cd = dev_get_drvdata(dev);
  13736. ssize_t ret;
  13737. mutex_lock(&cd->system_lock);
  13738. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13739. cd->easy_wakeup_gesture);
  13740. mutex_unlock(&cd->system_lock);
  13741. return ret;
  13742. }
  13743. /*******************************************************************************
  13744. * FUNCTION: pt_easy_wakeup_gesture_store
  13745. *
  13746. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13747. * allows the wake gesture to be set to a custom value.
  13748. *
  13749. * RETURN: Size of passed in buffer is success
  13750. *
  13751. * PARAMETERS:
  13752. * *dev - pointer to device structure
  13753. * *attr - pointer to device attributes
  13754. * *buf - pointer to buffer that hold the command parameters
  13755. * size - size of buf
  13756. ******************************************************************************/
  13757. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13758. struct device_attribute *attr, const char *buf, size_t size)
  13759. {
  13760. struct pt_core_data *cd = dev_get_drvdata(dev);
  13761. u32 input_data[2];
  13762. int length;
  13763. int rc = 0;
  13764. /* Maximum input of one value */
  13765. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13766. ARRAY_SIZE(input_data));
  13767. if (length != 1) {
  13768. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13769. __func__);
  13770. rc = -EINVAL;
  13771. goto exit;
  13772. }
  13773. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13774. __func__, cd->features.easywake);
  13775. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13776. rc = -EINVAL;
  13777. goto exit;
  13778. }
  13779. pm_runtime_get_sync(dev);
  13780. mutex_lock(&cd->system_lock);
  13781. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13782. cd->easy_wakeup_gesture = (u8)input_data[0];
  13783. pt_debug(dev, DL_INFO,
  13784. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13785. __func__, cd->easy_wakeup_gesture);
  13786. } else
  13787. rc = -ENODEV;
  13788. mutex_unlock(&cd->system_lock);
  13789. pm_runtime_put(dev);
  13790. exit:
  13791. if (rc)
  13792. return rc;
  13793. return size;
  13794. }
  13795. /*******************************************************************************
  13796. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13797. *
  13798. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13799. * show the TSG6 easywake gesture ID
  13800. *
  13801. * RETURN:
  13802. * 0 = success
  13803. * !0 = failure
  13804. *
  13805. * PARAMETERS:
  13806. * *dev - pointer to device structure
  13807. * *attr - pointer to device attributes
  13808. * *buf - pointer to output buffer
  13809. ******************************************************************************/
  13810. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13811. struct device_attribute *attr, char *buf)
  13812. {
  13813. struct pt_core_data *cd = dev_get_drvdata(dev);
  13814. ssize_t ret;
  13815. mutex_lock(&cd->system_lock);
  13816. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13817. cd->gesture_id);
  13818. mutex_unlock(&cd->system_lock);
  13819. return ret;
  13820. }
  13821. /*******************************************************************************
  13822. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13823. *
  13824. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13825. * show the TSG6 easywake gesture data in the following format:
  13826. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13827. *
  13828. * RETURN:
  13829. * 0 = success
  13830. * !0 = failure
  13831. *
  13832. * PARAMETERS:
  13833. * *dev - pointer to device structure
  13834. * *attr - pointer to device attributes
  13835. * *buf - pointer to output buffer
  13836. ******************************************************************************/
  13837. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13838. struct device_attribute *attr, char *buf)
  13839. {
  13840. struct pt_core_data *cd = dev_get_drvdata(dev);
  13841. ssize_t ret = 0;
  13842. int i;
  13843. mutex_lock(&cd->system_lock);
  13844. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13845. for (i = 0; i < cd->gesture_data_length; i++)
  13846. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13847. "0x%02X\n", cd->gesture_data[i]);
  13848. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13849. "(%d bytes)\n", cd->gesture_data_length);
  13850. mutex_unlock(&cd->system_lock);
  13851. return ret;
  13852. }
  13853. #endif /* EASYWAKE_TSG6 */
  13854. #ifdef TTDL_DIAGNOSTICS
  13855. /*******************************************************************************
  13856. * FUNCTION: pt_err_gpio_show
  13857. *
  13858. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13859. * setting up the gpio was successful
  13860. *
  13861. * RETURN: Char buffer with printed GPIO creation state
  13862. *
  13863. * PARAMETERS:
  13864. * *dev - pointer to device structure
  13865. * *attr - pointer to device attributes
  13866. * *buf - pointer to output buffer
  13867. ******************************************************************************/
  13868. static ssize_t pt_err_gpio_show(struct device *dev,
  13869. struct device_attribute *attr, char *buf)
  13870. {
  13871. struct pt_core_data *cd = dev_get_drvdata(dev);
  13872. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13873. "Err GPIO (%d) : %s\n"
  13874. "Err GPIO trig type: %d\n",
  13875. cd->err_gpio,
  13876. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13877. "HIGH" : "low") : "not defined"),
  13878. cd->err_gpio_type);
  13879. }
  13880. /*******************************************************************************
  13881. * FUNCTION: pt_err_gpio_store
  13882. *
  13883. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13884. * available host GPIO to be used to trigger when TTDL detects a PIP
  13885. * command/response timeout.
  13886. *
  13887. * RETURN:
  13888. * 0 = success
  13889. * !0 = failure
  13890. *
  13891. * PARAMETERS:
  13892. * *dev - pointer to device structure
  13893. * *attr - pointer to device attributes
  13894. * *buf - pointer to buffer that hold the command parameters
  13895. * size - size of buf
  13896. ******************************************************************************/
  13897. static ssize_t pt_err_gpio_store(struct device *dev,
  13898. struct device_attribute *attr, const char *buf, size_t size)
  13899. {
  13900. struct pt_core_data *cd = dev_get_drvdata(dev);
  13901. unsigned long gpio;
  13902. int rc = 0;
  13903. u32 input_data[3];
  13904. int length;
  13905. u8 err_type;
  13906. input_data[0] = 0;
  13907. input_data[1] = 0;
  13908. /* Maximmum input is two elements */
  13909. length = _pt_ic_parse_input(dev, buf, size,
  13910. input_data, ARRAY_SIZE(input_data));
  13911. if (length < 1 || length > 2) {
  13912. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13913. __func__);
  13914. rc = -EINVAL;
  13915. goto exit;
  13916. }
  13917. gpio = input_data[0];
  13918. err_type = (u8)input_data[1];
  13919. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13920. rc = -EINVAL;
  13921. goto exit;
  13922. }
  13923. mutex_lock(&cd->system_lock);
  13924. gpio_free(gpio);
  13925. rc = gpio_request(gpio, NULL);
  13926. if (rc) {
  13927. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13928. rc = -ENODEV;
  13929. } else {
  13930. cd->err_gpio = gpio;
  13931. cd->err_gpio_type = err_type;
  13932. gpio_direction_output(gpio, 0);
  13933. }
  13934. mutex_unlock(&cd->system_lock);
  13935. exit:
  13936. if (rc)
  13937. return rc;
  13938. return size;
  13939. }
  13940. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13941. pt_err_gpio_store);
  13942. /*******************************************************************************
  13943. * FUNCTION: pt_drv_irq_show
  13944. *
  13945. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13946. * TTDL interrupt is enabled/disabled
  13947. *
  13948. * RETURN:
  13949. * 0 = success
  13950. * !0 = failure
  13951. *
  13952. * PARAMETERS:
  13953. * *dev - pointer to device structure
  13954. * *attr - pointer to device attributes
  13955. * *buf - pointer to output buffer
  13956. ******************************************************************************/
  13957. static ssize_t pt_drv_irq_show(struct device *dev,
  13958. struct device_attribute *attr, char *buf)
  13959. {
  13960. struct pt_core_data *cd = dev_get_drvdata(dev);
  13961. ssize_t ret = 0;
  13962. mutex_lock(&cd->system_lock);
  13963. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13964. if (cd->irq_enabled)
  13965. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13966. "Driver interrupt: ENABLED\n");
  13967. else
  13968. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13969. "Driver interrupt: DISABLED\n");
  13970. mutex_unlock(&cd->system_lock);
  13971. return ret;
  13972. }
  13973. /*******************************************************************************
  13974. * FUNCTION: pt_drv_irq_store
  13975. *
  13976. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13977. * IRQ to be enabled/disabled.
  13978. *
  13979. * RETURN: Size of passed in buffer
  13980. *
  13981. * PARAMETERS:
  13982. * *dev - pointer to device structure
  13983. * *attr - pointer to device attributes
  13984. * *buf - pointer to buffer that hold the command parameters
  13985. * size - size of buf
  13986. ******************************************************************************/
  13987. static ssize_t pt_drv_irq_store(struct device *dev,
  13988. struct device_attribute *attr, const char *buf, size_t size)
  13989. {
  13990. struct pt_core_data *cd = dev_get_drvdata(dev);
  13991. u32 input_data[2];
  13992. int length;
  13993. int rc = 0;
  13994. /* Maximum input of one value */
  13995. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13996. ARRAY_SIZE(input_data));
  13997. if (length != 1) {
  13998. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13999. __func__);
  14000. rc = -EINVAL;
  14001. goto exit;
  14002. }
  14003. mutex_lock(&cd->system_lock);
  14004. switch (input_data[0]) {
  14005. case 0:
  14006. if (cd->irq_enabled) {
  14007. cd->irq_enabled = false;
  14008. /* Disable IRQ has no return value to check */
  14009. disable_irq_nosync(cd->irq);
  14010. pt_debug(dev, DL_INFO,
  14011. "%s: Driver IRQ now disabled\n",
  14012. __func__);
  14013. } else
  14014. pt_debug(dev, DL_INFO,
  14015. "%s: Driver IRQ already disabled\n",
  14016. __func__);
  14017. break;
  14018. case 1:
  14019. if (cd->irq_enabled == false) {
  14020. cd->irq_enabled = true;
  14021. enable_irq(cd->irq);
  14022. pt_debug(dev, DL_INFO,
  14023. "%s: Driver IRQ now enabled\n",
  14024. __func__);
  14025. } else
  14026. pt_debug(dev, DL_INFO,
  14027. "%s: Driver IRQ already enabled\n",
  14028. __func__);
  14029. break;
  14030. default:
  14031. rc = -EINVAL;
  14032. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14033. }
  14034. mutex_unlock(&(cd->system_lock));
  14035. exit:
  14036. if (rc)
  14037. return rc;
  14038. return size;
  14039. }
  14040. /*******************************************************************************
  14041. * FUNCTION: pt_pip2_bin_hdr_show
  14042. *
  14043. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  14044. * the bin file header from flash and show each field
  14045. *
  14046. * RETURN:
  14047. * 0 = success
  14048. * !0 = failure
  14049. *
  14050. * PARAMETERS:
  14051. * *dev - pointer to device structure
  14052. * *attr - pointer to device attributes
  14053. * *buf - pointer to output buffer
  14054. ******************************************************************************/
  14055. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  14056. struct device_attribute *attr, char *buf)
  14057. {
  14058. ssize_t ret = 0;
  14059. struct pt_bin_file_hdr hdr = {0};
  14060. int rc;
  14061. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  14062. ret = scnprintf(buf, strlen(buf),
  14063. "%s: %d\n"
  14064. "%s: %d\n"
  14065. "%s: 0x%04X\n"
  14066. "%s: %d\n"
  14067. "%s: %d\n"
  14068. "%s: %d\n"
  14069. "%s: 0x%08X\n"
  14070. "%s: 0x%04X\n"
  14071. "%s: 0x%04X\n"
  14072. "%s: %d\n"
  14073. "%s: %d\n",
  14074. "Status", rc,
  14075. "Header Length ", hdr.length,
  14076. "TTPID ", hdr.ttpid,
  14077. "FW Major Ver ", hdr.fw_major,
  14078. "FW Minor Ver ", hdr.fw_minor,
  14079. "FW Rev Control ", hdr.fw_rev_ctrl,
  14080. "FW CRC ", hdr.fw_crc,
  14081. "Silicon Rev ", hdr.si_rev,
  14082. "Silicon ID ", hdr.si_id,
  14083. "Config Ver ", hdr.config_ver,
  14084. "HEX File Size ", hdr.hex_file_size
  14085. );
  14086. return ret;
  14087. }
  14088. /*******************************************************************************
  14089. * FUNCTION: pt_platform_data_show
  14090. *
  14091. * SUMMARY: Show method for the platform_data sysfs node that will show the
  14092. * active platform data including: GPIOs, Vendor and Product IDs,
  14093. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  14094. * HID registers, and Easy wake gesture
  14095. *
  14096. * RETURN: Size of printed data
  14097. *
  14098. * PARAMETERS:
  14099. * *dev - pointer to device structure
  14100. * *attr - pointer to device attributes
  14101. * *buf - pointer to output buffer
  14102. ******************************************************************************/
  14103. static ssize_t pt_platform_data_show(struct device *dev,
  14104. struct device_attribute *attr, char *buf)
  14105. {
  14106. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14107. struct pt_core_data *cd = dev_get_drvdata(dev);
  14108. ssize_t ret;
  14109. ret = scnprintf(buf, strlen(buf),
  14110. "%s: %d\n"
  14111. "%s: %d\n"
  14112. "%s: %d\n"
  14113. "%s: %d\n"
  14114. "%s: %d\n"
  14115. "%s: %d\n"
  14116. "%s: %d\n"
  14117. "%s: %d\n"
  14118. "%s: %d\n"
  14119. "%s: %d\n"
  14120. "%s: %d\n"
  14121. "%s: %d\n"
  14122. "%s: %d\n"
  14123. "%s: %d\n"
  14124. "%s: %d\n"
  14125. "%s: %d\n"
  14126. "%s: %d\n"
  14127. "%s: %d\n"
  14128. "%s: %d\n"
  14129. "%s: %d\n"
  14130. "%s: %d\n"
  14131. "%s: %d\n"
  14132. "%s: %d\n"
  14133. "%s: %d\n"
  14134. "%s: %d\n"
  14135. "%s: %d\n"
  14136. "%s: %d\n"
  14137. "%s: %s\n"
  14138. "%s: %s\n"
  14139. "%s: %d\n",
  14140. "Status", 0,
  14141. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  14142. "Interrupt GPIO Value ",
  14143. pdata->core_pdata->irq_gpio ?
  14144. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14145. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14146. "Reset GPIO Value ",
  14147. pdata->core_pdata->rst_gpio ?
  14148. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14149. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14150. "DDI Reset GPIO Value ",
  14151. pdata->core_pdata->ddi_rst_gpio ?
  14152. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14153. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14154. "VDDI GPIO Value ",
  14155. pdata->core_pdata->vddi_gpio ?
  14156. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14157. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14158. "VCC GPIO Value ",
  14159. pdata->core_pdata->vcc_gpio ?
  14160. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14161. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14162. "AVDD GPIO Value ",
  14163. pdata->core_pdata->avdd_gpio ?
  14164. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14165. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14166. "AVEE GPIO Value ",
  14167. pdata->core_pdata->avee_gpio ?
  14168. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14169. "Vendor ID ", pdata->core_pdata->vendor_id,
  14170. "Product ID ", pdata->core_pdata->product_id,
  14171. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14172. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14173. "Core data flags ", pdata->core_pdata->flags,
  14174. "MT data flags ", pdata->mt_pdata->flags,
  14175. "Loader data flags ", pdata->loader_pdata->flags,
  14176. "Level trigger delay (us) ",
  14177. pdata->core_pdata->level_irq_udelay,
  14178. "HID Descriptor Register ",
  14179. pdata->core_pdata->hid_desc_register,
  14180. "HID Output Register ",
  14181. cd->hid_desc.output_register,
  14182. "HID Command Register ",
  14183. cd->hid_desc.command_register,
  14184. "Easy wakeup gesture ",
  14185. pdata->core_pdata->easy_wakeup_gesture,
  14186. "Config DUT generation ",
  14187. pdata->core_pdata->config_dut_generation ?
  14188. (pdata->core_pdata->config_dut_generation ==
  14189. CONFIG_DUT_PIP2_CAPABLE ?
  14190. "PT TC/TT" : "Gen5/6") : "Auto",
  14191. "Watchdog Force Stop ",
  14192. pdata->core_pdata->watchdog_force_stop ?
  14193. "True" : "False",
  14194. "Panel ID Support ",
  14195. pdata->core_pdata->panel_id_support);
  14196. return ret;
  14197. }
  14198. #define PT_ERR_STR_SIZE 64
  14199. /*******************************************************************************
  14200. * FUNCTION: pt_bist_bus_test
  14201. *
  14202. * SUMMARY: Tests the connectivity of the active bus pins:
  14203. * I2C - SDA and SCL
  14204. * SPI - MOSI, MISO, CLK
  14205. *
  14206. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14207. * bus. If any data is read we know the I2C/SPI pins are connected
  14208. *
  14209. * RETURN:
  14210. * 0 = Success
  14211. * !0 = Failure
  14212. *
  14213. * PARAMETERS:
  14214. * *dev - pointer to device structure
  14215. * *net_toggled - pointer to where to store if bus toggled
  14216. * *err_str - pointer to error string buffer
  14217. ******************************************************************************/
  14218. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14219. {
  14220. struct pt_core_data *cd = dev_get_drvdata(dev);
  14221. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14222. u8 *read_buf = NULL;
  14223. int bytes_read = 0;
  14224. int rc = 0;
  14225. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14226. if (read_buf == NULL) {
  14227. rc = -ENOMEM;
  14228. goto exit;
  14229. }
  14230. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14231. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14232. disable_irq(cd->irq);
  14233. mutex_lock(&cd->system_lock);
  14234. cd->irq_disabled = true;
  14235. mutex_unlock(&cd->system_lock);
  14236. /*
  14237. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14238. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14239. */
  14240. usleep_range(5000, 6000);
  14241. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14242. __func__, (int)sizeof(ver_cmd));
  14243. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14244. ">>> User CMD");
  14245. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14246. if (rc) {
  14247. pt_debug(dev, DL_ERROR,
  14248. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14249. *net_toggled = 0;
  14250. strlcpy(err_str,
  14251. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14252. goto exit_enable_irq;
  14253. }
  14254. usleep_range(4000, 5000);
  14255. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14256. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14257. __func__, bytes_read);
  14258. if (bytes_read == 0) {
  14259. *net_toggled = 0;
  14260. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14261. __func__);
  14262. strlcpy(err_str,
  14263. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14264. rc = -EIO;
  14265. goto exit_enable_irq;
  14266. } else {
  14267. if (cd->bus_ops->bustype == BUS_I2C)
  14268. *net_toggled = 1;
  14269. else {
  14270. if ((bytes_read > 3) &&
  14271. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14272. PIP2_CMD_ID_VERSION)
  14273. *net_toggled = 1;
  14274. else {
  14275. *net_toggled = 0;
  14276. pt_debug(dev, DL_INFO,
  14277. "%s: BUS Read Failed, %d bytes read\n",
  14278. __func__, bytes_read);
  14279. strlcpy(err_str,
  14280. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14281. }
  14282. }
  14283. }
  14284. exit_enable_irq:
  14285. enable_irq(cd->irq);
  14286. usleep_range(5000, 6000);
  14287. mutex_lock(&cd->system_lock);
  14288. cd->irq_disabled = false;
  14289. mutex_unlock(&cd->system_lock);
  14290. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14291. exit:
  14292. kfree(read_buf);
  14293. return rc;
  14294. }
  14295. /*******************************************************************************
  14296. * FUNCTION: pt_bist_irq_test
  14297. *
  14298. * SUMMARY: Tests the connectivity of the IRQ net
  14299. *
  14300. * This test will ensure there is a good connection between the host
  14301. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14302. * asserted and if so keep reading messages off of the bus until
  14303. * it de-asserts. Possible outcomes:
  14304. * - IRQ was already de-asserted: Send a PIP command and if an
  14305. * interrupt is generated the test passes.
  14306. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14307. * test passes.
  14308. * - IRQ stays asserted: After reading the bus multiple times
  14309. * the IRQ stays asserted. Likely open or shorted to GND
  14310. *
  14311. * RETURN:
  14312. * 0 = Success
  14313. * !0 = Failure
  14314. *
  14315. * PARAMETERS:
  14316. * *dev - pointer to device structure
  14317. * *bus_toggled - pointer to where to store if bus toggled
  14318. * *irq_toggled - pointer to where to store if IRQ toggled
  14319. * *xres_toggled - pointer to where to store if XRES toggled
  14320. * *err_str - pointer to error string buffer
  14321. ******************************************************************************/
  14322. static int pt_bist_irq_test(struct device *dev,
  14323. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14324. {
  14325. struct pt_core_data *cd = dev_get_drvdata(dev);
  14326. u8 *read_buf = NULL;
  14327. u8 mode = PT_MODE_UNKNOWN;
  14328. u16 actual_read_len;
  14329. int bytes_read = 0;
  14330. int count = 0;
  14331. int rc = 0;
  14332. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14333. if (read_buf == NULL) {
  14334. rc = -ENOMEM;
  14335. goto exit;
  14336. }
  14337. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14338. cd->irq_count = 0;
  14339. /*
  14340. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14341. * the bus based on the 2 byte initial length read. Try up to 5x.
  14342. */
  14343. while (pt_check_irq_asserted(cd) && count < 5) {
  14344. count++;
  14345. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14346. }
  14347. if (count > 1 && count < 5 && bytes_read > 0) {
  14348. /*
  14349. * IRQ was stuck but data was successfully read from the
  14350. * bus releasing the IRQ line.
  14351. */
  14352. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14353. __func__, count, bytes_read);
  14354. *bus_toggled = 1;
  14355. *irq_toggled = 1;
  14356. goto exit;
  14357. }
  14358. if (count == 5 && bytes_read == 0) {
  14359. /*
  14360. * Looped 5x and read nothing off the bus yet the IRQ is still
  14361. * asserted. Possible conditions:
  14362. * - IRQ open circuit
  14363. * - IRQ shorted to GND
  14364. * - I2C/SPI bus is disconnected
  14365. * - FW holding the pin low
  14366. * Try entering the BL to see if communication works there.
  14367. */
  14368. mode = PT_MODE_IGNORE;
  14369. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14370. if (rc) {
  14371. pt_debug(dev, DL_ERROR,
  14372. "%s Failed to enter BL\n", __func__);
  14373. strlcpy(err_str,
  14374. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14375. *irq_toggled = 0;
  14376. goto exit;
  14377. }
  14378. /*
  14379. * If original mode was operational and we successfully
  14380. * entered the BL, then the XRES net must have toggled
  14381. */
  14382. if (mode == PT_MODE_OPERATIONAL)
  14383. *xres_toggled = 1;
  14384. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14385. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14386. &actual_read_len);
  14387. if (rc) {
  14388. /*
  14389. * Could not communicate to DUT in BL mode. Save the
  14390. * error string, slim chance but the XRES test below may
  14391. * show the IRQ is actually working.
  14392. */
  14393. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14394. pt_debug(dev, DL_ERROR,
  14395. "%s: %s, count=%d bytes_read=%d\n",
  14396. __func__, err_str, count, bytes_read);
  14397. *irq_toggled = 0;
  14398. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14399. goto exit;
  14400. } else {
  14401. *bus_toggled = 1;
  14402. *irq_toggled = 1;
  14403. goto exit;
  14404. }
  14405. }
  14406. if (pt_check_irq_asserted(cd)) {
  14407. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14408. rc = -EIO;
  14409. *irq_toggled = 0;
  14410. goto exit;
  14411. }
  14412. /* Try sending a PIP command to see if we get a response */
  14413. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14414. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14415. if (rc) {
  14416. /*
  14417. * Potential IRQ issue, no communication in App mode, attempt
  14418. * the same command in the BL
  14419. */
  14420. mode = PT_MODE_IGNORE;
  14421. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14422. if (rc) {
  14423. pt_debug(dev, DL_ERROR,
  14424. "%s Failed to enter BL\n", __func__);
  14425. *irq_toggled = 0;
  14426. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14427. goto exit;
  14428. }
  14429. /*
  14430. * If original mode was operational and we successfully
  14431. * entered the BL, this will be useful info for the tp_xres
  14432. * test below.
  14433. */
  14434. if (mode == PT_MODE_OPERATIONAL)
  14435. *xres_toggled = 1;
  14436. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14437. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14438. &actual_read_len);
  14439. if (rc) {
  14440. /*
  14441. * Could not communicate in FW mode or BL mode. Save the
  14442. * error string, slim chance but the XRES test below may
  14443. * show the IRQ is actually working.
  14444. */
  14445. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14446. pt_debug(dev, DL_ERROR,
  14447. "%s: request_active_pip_prot failed\n",
  14448. __func__);
  14449. *irq_toggled = 0;
  14450. goto exit;
  14451. }
  14452. }
  14453. if (cd->irq_count > 0) {
  14454. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14455. cd->irq_count);
  14456. *bus_toggled = 1;
  14457. *irq_toggled = 1;
  14458. goto exit;
  14459. }
  14460. exit:
  14461. kfree(read_buf);
  14462. return rc;
  14463. }
  14464. /*******************************************************************************
  14465. * FUNCTION: pt_bist_xres_test
  14466. *
  14467. * SUMMARY: Tests the connectivity of the TP_XRES net
  14468. *
  14469. * This test will ensure there is a good connection between the host
  14470. * and the DUT on the tp_xres pin. The pin will be toggled to
  14471. * generate a TP reset which will cause the DUT to output a reset
  14472. * sentinel. If the reset sentinel is seen the test passes. If it is
  14473. * not seen the test will attempt to send a soft reset to simply gain
  14474. * some additional information on the failure:
  14475. * - soft reset fails to send: XRES and IRQ likely open
  14476. * - soft reset passes: XRES likely open or stuck de-asserted
  14477. * - soft reset fails: XRES likely stuck asserted
  14478. *
  14479. * RETURN:
  14480. * 0 = Success
  14481. * !0 = Failure
  14482. *
  14483. * PARAMETERS:
  14484. * *dev - pointer to device structure
  14485. * *bus_toggled - pointer to where to store if bus toggled
  14486. * *irq_toggled - pointer to where to store if IRQ toggled
  14487. * *xres_toggled - pointer to where to store if XRES toggled
  14488. * *err_str - pointer to error string buffer
  14489. ******************************************************************************/
  14490. static int pt_bist_xres_test(struct device *dev,
  14491. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14492. {
  14493. struct pt_core_data *cd = dev_get_drvdata(dev);
  14494. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14495. u8 *read_buf = NULL;
  14496. u8 mode = PT_MODE_UNKNOWN;
  14497. int rc = 0;
  14498. int t = 0;
  14499. int timeout = 300;
  14500. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14501. if (read_buf == NULL) {
  14502. rc = -ENOMEM;
  14503. goto exit;
  14504. }
  14505. /* Clear the startup bit mask, reset and enum will re-populate it */
  14506. cd->startup_status = STARTUP_STATUS_START;
  14507. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14508. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14509. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14510. rc = -ENODEV;
  14511. goto exit;
  14512. }
  14513. /* Ensure we have nothing pending on active bus */
  14514. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14515. /* Perform a hard XRES toggle and wait for reset sentinel */
  14516. mutex_lock(&cd->system_lock);
  14517. cd->hid_reset_cmd_state = 1;
  14518. mutex_unlock(&cd->system_lock);
  14519. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14520. rc = pt_hw_hard_reset(cd);
  14521. /* Set timeout to 1s for the flashless case where a BL could be done */
  14522. if (cd->flashless_dut)
  14523. timeout = 1000;
  14524. /*
  14525. * To avoid the case that next PIP command can be confused by BL/FW
  14526. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14527. * followed by "wakeup event" function can lower the failure rate.
  14528. */
  14529. t = wait_event_timeout(cd->wait_q,
  14530. ((cd->startup_status != STARTUP_STATUS_START)
  14531. && (cd->hid_reset_cmd_state == 0)),
  14532. msecs_to_jiffies(timeout));
  14533. if (IS_TMO(t)) {
  14534. pt_debug(cd->dev, DL_ERROR,
  14535. "%s: TMO waiting for sentinel\n", __func__);
  14536. *xres_toggled = 0;
  14537. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14538. /*
  14539. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14540. * To enter the BL we need to generate an XRES so first try to
  14541. * launch the applicaiton
  14542. */
  14543. if (cd->mode == PT_MODE_BOOTLOADER)
  14544. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14545. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14546. __func__);
  14547. mode = PT_MODE_IGNORE;
  14548. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14549. if (rc) {
  14550. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14551. __func__);
  14552. *xres_toggled = 0;
  14553. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14554. goto exit;
  14555. } else {
  14556. /* Wait for the BL sentinel */
  14557. t = wait_event_timeout(cd->wait_q,
  14558. (cd->startup_status != STARTUP_STATUS_START),
  14559. msecs_to_jiffies(500));
  14560. if (IS_TMO(t)) {
  14561. pt_debug(cd->dev, DL_ERROR,
  14562. "%s: TMO waiting for BL sentinel\n",
  14563. __func__);
  14564. *xres_toggled = 0;
  14565. strlcpy(err_str,
  14566. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14567. rc = -ETIME;
  14568. goto exit;
  14569. }
  14570. }
  14571. }
  14572. mutex_lock(&cd->system_lock);
  14573. cd->hid_reset_cmd_state = 0;
  14574. mutex_unlock(&cd->system_lock);
  14575. /* Look for BL or FW reset sentinels */
  14576. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14577. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14578. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14579. /* If a sentinel was seen, all nets are working */
  14580. *xres_toggled = 1;
  14581. *irq_toggled = 1;
  14582. /*
  14583. * For SPI test, bus read result can be confused as FW sentinel
  14584. * if MISO(slave) is connected to MISO(host).
  14585. */
  14586. if (cd->bus_ops->bustype == BUS_I2C)
  14587. *bus_toggled = 1;
  14588. } else {
  14589. /*
  14590. * Hard reset failed, however some additional information
  14591. * could be determined. Try a soft reset to see if DUT resets
  14592. * with the possible outcomes:
  14593. * - if it resets the line is not stuck asserted
  14594. * - if it does not reset the line could be stuck asserted
  14595. */
  14596. *xres_toggled = 0;
  14597. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14598. msleep(30);
  14599. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14600. __func__, rc);
  14601. if (rc) {
  14602. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14603. pt_debug(dev, DL_ERROR,
  14604. "%s: Hard reset failed, soft reset failed %s\n",
  14605. __func__, err_str);
  14606. goto exit;
  14607. }
  14608. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14609. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14610. strlcpy(err_str,
  14611. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14612. pt_debug(dev, DL_ERROR,
  14613. "%s: Hard reset failed, soft reset passed-%s\n",
  14614. __func__, err_str);
  14615. } else if (cd->startup_status == 0) {
  14616. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14617. pt_debug(dev, DL_ERROR,
  14618. "%s: Hard reset failed, soft reset failed-%s\n",
  14619. __func__, err_str);
  14620. } else {
  14621. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14622. pt_debug(dev, DL_ERROR,
  14623. "%s: Hard and Soft reset failed - %s\n",
  14624. __func__, err_str);
  14625. }
  14626. }
  14627. exit:
  14628. kfree(read_buf);
  14629. return rc;
  14630. }
  14631. /*******************************************************************************
  14632. * FUNCTION: pt_bist_slave_irq_test
  14633. *
  14634. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14635. *
  14636. * This test will ensure there is a good IRQ connection between the master
  14637. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14638. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14639. * saw each slave trigger the IRQ with it's reset sentinel.
  14640. *
  14641. * RETURN:
  14642. * 0 = Success
  14643. * !0 = Failure
  14644. *
  14645. * PARAMETERS:
  14646. * *dev - pointer to device structure
  14647. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14648. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14649. * *err_str - pointer to error string buffer
  14650. * *slave_detect - pointer to slave detect buffer
  14651. * *boot_err - pointer to boot_err buffer
  14652. ******************************************************************************/
  14653. static int pt_bist_slave_irq_test(struct device *dev,
  14654. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14655. u8 *slave_detect, u8 *boot_err)
  14656. {
  14657. struct pt_core_data *cd = dev_get_drvdata(dev);
  14658. u8 mode = PT_MODE_UNKNOWN;
  14659. u8 status;
  14660. u8 boot;
  14661. u8 read_buf[12];
  14662. u8 detected = 0;
  14663. u8 last_err = -1;
  14664. u16 actual_read_len;
  14665. int result = 0;
  14666. int rc = 0;
  14667. /*
  14668. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14669. * detect bits. If the DUT was in FW, entering the BL will cause an
  14670. * XRES signal which will inadvertently test the XRES net as well
  14671. */
  14672. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14673. if (rc) {
  14674. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14675. __func__, rc);
  14676. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14677. goto exit;
  14678. }
  14679. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14680. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14681. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14682. &actual_read_len);
  14683. if (!rc) {
  14684. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14685. "PIP2 STATUS");
  14686. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14687. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14688. /* Slave detect is only valid if status ok and in boot exec */
  14689. if (status == PIP2_RSP_ERR_NONE &&
  14690. boot == PIP2_STATUS_BOOT_EXEC) {
  14691. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14692. SLAVE_DETECT_MASK;
  14693. } else {
  14694. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14695. rc = -EPERM;
  14696. }
  14697. } else {
  14698. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14699. __func__);
  14700. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14701. goto exit;
  14702. }
  14703. /*
  14704. * Retrieve boot error regardless of the state of the slave detect
  14705. * bit because the IRQ could have been stuck high or low.
  14706. */
  14707. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14708. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14709. read_buf, &actual_read_len);
  14710. if (!rc) {
  14711. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14712. "PIP2 GET_LAST_ERRNO");
  14713. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14714. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14715. if (last_err) {
  14716. pt_debug(cd->dev, DL_ERROR,
  14717. "%s: Master Boot Last Err = 0x%02X\n",
  14718. __func__, last_err);
  14719. }
  14720. } else {
  14721. pt_debug(cd->dev, DL_ERROR,
  14722. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14723. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14724. }
  14725. exit:
  14726. pt_debug(cd->dev, DL_INFO,
  14727. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14728. __func__, rc, detected, last_err);
  14729. if (err_str && last_err) {
  14730. if (detected)
  14731. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14732. "- Likely stuck low. Boot Error:",
  14733. last_err);
  14734. else
  14735. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14736. "- Likely stuck high. Boot Error:",
  14737. last_err);
  14738. }
  14739. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14740. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14741. last_err = PIP2_RSP_ERR_NONE;
  14742. if (slave_irq_toggled)
  14743. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14744. if (slave_bus_toggled) {
  14745. /* Leave as UNTEST if slave not detected */
  14746. if (detected)
  14747. *slave_bus_toggled = !last_err ? true : false;
  14748. }
  14749. if (slave_detect)
  14750. *slave_detect = detected;
  14751. if (boot_err)
  14752. *boot_err = last_err;
  14753. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14754. __func__,
  14755. "Detected", detected,
  14756. "slave_irq_toggled", *slave_irq_toggled,
  14757. "slave_bus_toggled", *slave_bus_toggled);
  14758. return rc;
  14759. }
  14760. /*******************************************************************************
  14761. * FUNCTION: pt_bist_slave_xres_test
  14762. *
  14763. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14764. *
  14765. * This test will ensure there is a good TP_XRES connection between the
  14766. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14767. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14768. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14769. * it's reset sentinel.
  14770. *
  14771. * RETURN:
  14772. * 0 = Success
  14773. * !0 = Failure
  14774. *
  14775. * PARAMETERS:
  14776. * *dev - pointer to device structure
  14777. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14778. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14779. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14780. * *err_str - pointer to error string buffer
  14781. ******************************************************************************/
  14782. static int pt_bist_slave_xres_test(struct device *dev,
  14783. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14784. u8 *err_str)
  14785. {
  14786. struct pt_core_data *cd = dev_get_drvdata(dev);
  14787. u8 slave_detect = 0;
  14788. u8 boot_err = 0;
  14789. int rc = 0;
  14790. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  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. pt_hw_hard_reset(cd);
  14796. msleep(100);
  14797. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14798. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14799. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14800. if (!rc && *slave_irq_toggled == false) {
  14801. /*
  14802. * If the slave IRQ did not toggle, either the slave_detect
  14803. * bit was not set or we had a boot error. If the slave
  14804. * detect was not set the slave did not reset causing a boot
  14805. * error.
  14806. */
  14807. if (!slave_detect)
  14808. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14809. else
  14810. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14811. "- likely open or an IRQ issue. Boot Error:",
  14812. boot_err);
  14813. }
  14814. if (slave_xres_toggled) {
  14815. if (!rc)
  14816. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14817. else
  14818. *slave_xres_toggled = false;
  14819. }
  14820. return rc;
  14821. }
  14822. /*******************************************************************************
  14823. * FUNCTION: pt_bist_slave_bus_test
  14824. *
  14825. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14826. *
  14827. * This test will ensure a good SPI bus connection between the
  14828. * master DUT and the slave DUT(s). This bus connection is ensured by
  14829. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14830. * then there is a master/slave communication issue. Opening file 0 on
  14831. * the master will open it on the slave as well if the slave was detected.
  14832. *
  14833. * RETURN:
  14834. * 0 = Success
  14835. * !0 = Failure
  14836. *
  14837. * PARAMETERS:
  14838. * *dev - pointer to device structure
  14839. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14840. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14841. * *err_str - pointer to error string buffer
  14842. ******************************************************************************/
  14843. static int pt_bist_slave_bus_test(struct device *dev,
  14844. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14845. {
  14846. struct pt_core_data *cd = dev_get_drvdata(dev);
  14847. u8 mode = PT_MODE_UNKNOWN;
  14848. u8 bus_toggled = false;
  14849. u8 file_handle;
  14850. int result = 0;
  14851. int rc = 0;
  14852. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14853. if (rc) {
  14854. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14855. __func__, rc);
  14856. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14857. goto exit;
  14858. }
  14859. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14860. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14861. if (file_handle != PIP2_RAM_FILE) {
  14862. rc = -ENOENT;
  14863. bus_toggled = false;
  14864. pt_debug(dev, DL_ERROR,
  14865. "%s Failed to open bin file\n", __func__);
  14866. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14867. goto exit;
  14868. } else {
  14869. bus_toggled = true;
  14870. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14871. pt_debug(dev, DL_ERROR,
  14872. "%s: File Close failed, file_handle=%d\n",
  14873. __func__, file_handle);
  14874. }
  14875. }
  14876. exit:
  14877. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14878. if (slave_irq_toggled)
  14879. *slave_irq_toggled = bus_toggled;
  14880. if (slave_bus_toggled)
  14881. *slave_bus_toggled = bus_toggled;
  14882. return rc;
  14883. }
  14884. /*******************************************************************************
  14885. * FUNCTION: pt_ttdl_bist_show
  14886. *
  14887. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14888. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14889. *
  14890. * NOTE: This function will reset the DUT and the startup_status bit
  14891. * mask. A pt_enum will be queued after completion.
  14892. *
  14893. * NOTE: The order of the net tests is done to optimize the time it takes
  14894. * to run. The first test is capable of verifying all nets, each subsequent
  14895. * test is only run if the previous was not able to see all nets toggle.
  14896. *
  14897. * RETURN:
  14898. * 0 = success
  14899. * !0 = failure
  14900. *
  14901. * PARAMETERS:
  14902. * *dev - pointer to device structure
  14903. * *attr - pointer to device attributes
  14904. * *buf - pointer to output buffer
  14905. ******************************************************************************/
  14906. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14907. struct device_attribute *attr, char *buf)
  14908. {
  14909. struct pt_core_data *cd = dev_get_drvdata(dev);
  14910. ssize_t ret;
  14911. char *bus_err_str = NULL;
  14912. char *irq_err_str = NULL;
  14913. char *xres_err_str = NULL;
  14914. char *slave_bus_err_str = NULL;
  14915. char *slave_irq_err_str = NULL;
  14916. char *slave_xres_err_str = NULL;
  14917. u8 tests;
  14918. int rc = 0;
  14919. int num_tests = 0;
  14920. int status = 1; /* 0 = Pass, !0 = fail */
  14921. u8 bus_toggled = 0x0F; /* default to untested */
  14922. u8 i2c_toggled = 0x0F; /* default to untested */
  14923. u8 spi_toggled = 0x0F; /* default to untested */
  14924. u8 irq_toggled = 0x0F; /* default to untested */
  14925. u8 xres_toggled = 0x0F; /* default to untested */
  14926. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14927. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14928. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14929. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14930. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14931. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14932. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14933. goto print_results;
  14934. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14935. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14936. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14937. if (cd->multi_chip) {
  14938. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14939. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14940. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14941. if (!slave_bus_err_str ||
  14942. !slave_irq_err_str ||
  14943. !slave_xres_err_str)
  14944. goto print_results;
  14945. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14946. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14947. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14948. }
  14949. /* Turn off the TTDL WD during the test */
  14950. pt_stop_wd_timer(cd);
  14951. /* Shorten default PIP cmd timeout while running BIST */
  14952. cd->pip_cmd_timeout = 200;
  14953. /* Count the number of tests to run */
  14954. tests = cd->ttdl_bist_select;
  14955. while (tests) {
  14956. num_tests += tests & 1;
  14957. tests >>= 1;
  14958. }
  14959. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14960. __func__, cd->ttdl_bist_select, num_tests);
  14961. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14962. if (cd->flashless_dut) {
  14963. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14964. __func__);
  14965. mutex_lock(&cd->system_lock);
  14966. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14967. mutex_unlock(&cd->system_lock);
  14968. }
  14969. /* --------------- TP_XRES BIST TEST --------------- */
  14970. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14971. pt_debug(dev, DL_INFO,
  14972. "%s: ----- Start TP_XRES BIST -----", __func__);
  14973. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14974. &xres_toggled, xres_err_str);
  14975. /* Done if the rest of all nets toggled */
  14976. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14977. goto host_nets_complete;
  14978. }
  14979. /* Flush bus in case a PIP response is waiting from previous test */
  14980. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14981. /* --------------- IRQ BIST TEST --------------- */
  14982. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14983. pt_debug(dev, DL_INFO,
  14984. "%s: ----- Start IRQ BIST -----", __func__);
  14985. bus_toggled = 0xFF;
  14986. irq_toggled = 0xFF;
  14987. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14988. &xres_toggled, irq_err_str);
  14989. /* If this net failed clear results from previous net */
  14990. if (irq_toggled != 1) {
  14991. xres_toggled = 0x0F;
  14992. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14993. }
  14994. if (bus_toggled == 1 && irq_toggled == 1)
  14995. goto host_nets_complete;
  14996. }
  14997. /* Flush bus in case a PIP response is waiting from previous test */
  14998. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14999. /* --------------- BUS BIST TEST --------------- */
  15000. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  15001. pt_debug(dev, DL_INFO,
  15002. "%s: ----- Start BUS BIST -----", __func__);
  15003. bus_toggled = 0xFF;
  15004. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  15005. /* If this net failed clear results from previous net */
  15006. if (bus_toggled == 0) {
  15007. irq_toggled = 0x0F;
  15008. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15009. }
  15010. }
  15011. host_nets_complete:
  15012. /* --------------- SLAVE XRES BIST TEST --------------- */
  15013. if (cd->multi_chip &&
  15014. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  15015. pt_debug(dev, DL_INFO,
  15016. "%s: ----- Start Slave XRES BIST -----", __func__);
  15017. slave_xres_toggled = 0xFF;
  15018. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  15019. &slave_bus_toggled, &slave_xres_toggled,
  15020. slave_xres_err_str);
  15021. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  15022. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  15023. goto print_results;
  15024. }
  15025. /* --------------- SLAVE IRQ BIST TEST --------------- */
  15026. if (cd->multi_chip &&
  15027. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  15028. pt_debug(dev, DL_INFO,
  15029. "%s: ----- Start Slave IRQ BIST -----", __func__);
  15030. slave_irq_toggled = 0xFF;
  15031. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  15032. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  15033. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  15034. __func__, slave_irq_toggled);
  15035. if (slave_irq_toggled == 1) {
  15036. slave_bus_toggled = 1;
  15037. goto print_results;
  15038. }
  15039. }
  15040. /* --------------- SLAVE BUS BIST TEST --------------- */
  15041. if (cd->multi_chip &&
  15042. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  15043. pt_debug(dev, DL_INFO,
  15044. "%s: ----- Start Slave BUS BIST -----", __func__);
  15045. slave_bus_toggled = 0xFF;
  15046. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  15047. &slave_bus_toggled, slave_bus_err_str);
  15048. }
  15049. print_results:
  15050. /* Restore PIP command timeout */
  15051. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  15052. /*
  15053. * We're done! - Perform a hard XRES toggle, allowing BL
  15054. * to load FW if there is any in Flash
  15055. */
  15056. mutex_lock(&cd->system_lock);
  15057. cd->hid_reset_cmd_state = 0;
  15058. mutex_unlock(&cd->system_lock);
  15059. pt_debug(dev, DL_INFO,
  15060. "%s: TTDL BIST Complete - Final reset\n", __func__);
  15061. if (cd->flashless_dut) {
  15062. /*
  15063. * For Flashless solution, FW update is triggered after BL is
  15064. * seen that several miliseconds delay is needed.
  15065. */
  15066. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  15067. __func__);
  15068. mutex_lock(&cd->system_lock);
  15069. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  15070. mutex_unlock(&cd->system_lock);
  15071. /* Reset DUT and wait 100ms to see if loader started */
  15072. pt_hw_hard_reset(cd);
  15073. msleep(100);
  15074. if (cd->fw_updating) {
  15075. pt_debug(dev, DL_INFO,
  15076. "%s: ----- BIST Wait FW Loading ----",
  15077. __func__);
  15078. rc = _pt_request_wait_for_enum_state(
  15079. dev, 4000, STARTUP_STATUS_COMPLETE);
  15080. }
  15081. } else {
  15082. if (cd->mode == PT_MODE_BOOTLOADER) {
  15083. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  15084. if (rc) {
  15085. pt_debug(dev, DL_ERROR,
  15086. "%s Failed to launch app\n", __func__);
  15087. rc = pt_hw_hard_reset(cd);
  15088. }
  15089. }
  15090. /*
  15091. * If FW exists the BL may have just started or will start soon,
  15092. * so the FW sentinel may be on it's way but with no FW it will
  15093. * not arrive, wait for it before deciding if we need to queue
  15094. * an enum.
  15095. */
  15096. rc = _pt_request_wait_for_enum_state(
  15097. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  15098. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  15099. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  15100. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  15101. __func__);
  15102. pt_queue_enum(cd);
  15103. rc = _pt_request_wait_for_enum_state(
  15104. dev, 2000, STARTUP_STATUS_COMPLETE);
  15105. }
  15106. }
  15107. msleep(20);
  15108. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  15109. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  15110. pt_start_wd_timer(cd);
  15111. /* Canned print if any memory allocation issues */
  15112. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  15113. ret = scnprintf(buf, strlen(buf),
  15114. "Status: %d\n"
  15115. "I2C (SDA,SCL): [UNTEST]\n"
  15116. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15117. "IRQ connection: [UNTEST]\n"
  15118. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  15119. if (cd->multi_chip) {
  15120. ret += scnprintf(buf + ret, strlen(buf),
  15121. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  15122. "I/P IRQ connection: [UNTEST]\n"
  15123. "I/P TP_XRES connection: [UNTEST]\n");
  15124. }
  15125. } else {
  15126. status = 0;
  15127. if (bus_toggled == 1)
  15128. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  15129. if (irq_toggled == 1)
  15130. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  15131. if (xres_toggled == 1)
  15132. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  15133. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  15134. status += bus_toggled;
  15135. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  15136. status += irq_toggled;
  15137. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  15138. status += xres_toggled;
  15139. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  15140. __func__, status, bus_toggled, irq_toggled,
  15141. xres_toggled);
  15142. if (cd->multi_chip) {
  15143. if (slave_irq_toggled == 1)
  15144. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15145. if (slave_xres_toggled == 1)
  15146. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15147. if (slave_bus_toggled == 1)
  15148. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15149. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15150. status += slave_bus_toggled;
  15151. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15152. status += slave_irq_toggled;
  15153. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15154. status += slave_xres_toggled;
  15155. pt_debug(dev, DL_ERROR,
  15156. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15157. __func__, status, bus_toggled, irq_toggled,
  15158. xres_toggled, slave_bus_toggled,
  15159. slave_irq_toggled, slave_xres_toggled);
  15160. }
  15161. if (cd->bus_ops->bustype == BUS_I2C)
  15162. i2c_toggled = bus_toggled;
  15163. else
  15164. spi_toggled = bus_toggled;
  15165. ret = scnprintf(buf, strlen(buf),
  15166. "Status: %d\n"
  15167. "I2C (SDA,SCL): %s %s\n"
  15168. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15169. "IRQ connection: %s %s\n"
  15170. "TP_XRES connection: %s %s\n",
  15171. status == num_tests ? 0 : 1,
  15172. i2c_toggled == 0x0F ? "[UNTEST]" :
  15173. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15174. i2c_toggled == 0x0F ? "" : bus_err_str,
  15175. spi_toggled == 0x0F ? "[UNTEST]" :
  15176. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15177. spi_toggled == 0x0F ? "" : bus_err_str,
  15178. irq_toggled == 0x0F ? "[UNTEST]" :
  15179. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15180. irq_err_str,
  15181. xres_toggled == 0x0F ? "[UNTEST]" :
  15182. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15183. xres_err_str);
  15184. if (cd->multi_chip) {
  15185. ret += scnprintf(buf + ret, strlen(buf),
  15186. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15187. "I/P IRQ connection: %s %s\n"
  15188. "I/P TP_XRES connection: %s %s\n",
  15189. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15190. slave_bus_toggled == 1 ? "[ OK ]" :
  15191. "[FAILED]", slave_bus_err_str,
  15192. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15193. slave_irq_toggled == 1 ? "[ OK ]" :
  15194. "[FAILED]", slave_irq_err_str,
  15195. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15196. slave_xres_toggled == 1 ? "[ OK ]" :
  15197. "[FAILED]", slave_xres_err_str);
  15198. }
  15199. }
  15200. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15201. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15202. __func__, cd->startup_status);
  15203. kfree(bus_err_str);
  15204. kfree(irq_err_str);
  15205. kfree(xres_err_str);
  15206. return ret;
  15207. }
  15208. /*******************************************************************************
  15209. * FUNCTION: pt_ttdl_bist_store
  15210. *
  15211. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15212. *
  15213. * RETURN: Size of passed in buffer if successful
  15214. *
  15215. * PARAMETERS:
  15216. * *dev - pointer to device structure
  15217. * *attr - pointer to device attributes
  15218. * *buf - pointer to command buffer
  15219. * size - size of buf
  15220. ******************************************************************************/
  15221. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15222. struct device_attribute *attr, const char *buf, size_t size)
  15223. {
  15224. struct pt_core_data *cd = dev_get_drvdata(dev);
  15225. u32 input_data[2] = {0};
  15226. int length;
  15227. int rc = 0;
  15228. /* Maximum input of one value */
  15229. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15230. ARRAY_SIZE(input_data));
  15231. if (length != 1) {
  15232. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15233. __func__);
  15234. rc = -EINVAL;
  15235. goto exit;
  15236. } else {
  15237. mutex_lock(&cd->system_lock);
  15238. cd->ttdl_bist_select = input_data[0];
  15239. mutex_unlock(&cd->system_lock);
  15240. }
  15241. exit:
  15242. if (rc)
  15243. return rc;
  15244. return size;
  15245. }
  15246. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15247. pt_ttdl_bist_store);
  15248. /*******************************************************************************
  15249. * FUNCTION: pt_flush_bus_store
  15250. *
  15251. * SUMMARY: Store method for the flush_bus sysfs node.
  15252. *
  15253. * RETURN: Size of passed in buffer if successful
  15254. *
  15255. * PARAMETERS:
  15256. * *dev - pointer to device structure
  15257. * *attr - pointer to device attributes
  15258. * *buf - pointer to command buffer
  15259. * size - size of buf
  15260. ******************************************************************************/
  15261. static ssize_t pt_flush_bus_store(struct device *dev,
  15262. struct device_attribute *attr, const char *buf, size_t size)
  15263. {
  15264. struct pt_core_data *cd = dev_get_drvdata(dev);
  15265. u32 input_data[2] = {0};
  15266. int length;
  15267. int rc = 0;
  15268. /* Maximum input of one value */
  15269. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15270. ARRAY_SIZE(input_data));
  15271. if (length != 1) {
  15272. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15273. __func__);
  15274. rc = -EINVAL;
  15275. goto exit;
  15276. }
  15277. mutex_lock(&cd->system_lock);
  15278. if (input_data[0] == 0)
  15279. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15280. else if (input_data[0] == 1)
  15281. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15282. else
  15283. rc = -EINVAL;
  15284. mutex_unlock(&cd->system_lock);
  15285. exit:
  15286. if (rc)
  15287. return rc;
  15288. return size;
  15289. }
  15290. /*******************************************************************************
  15291. * FUNCTION: pt_flush_bus_show
  15292. *
  15293. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15294. * based on either the size of the first two bytes or a blind 256 bytes.
  15295. *
  15296. * RETURN:
  15297. * 0 = success
  15298. * !0 = failure
  15299. *
  15300. * PARAMETERS:
  15301. * *dev - pointer to device structure
  15302. * *attr - pointer to device attributes
  15303. * *buf - pointer to output buffer
  15304. ******************************************************************************/
  15305. static ssize_t pt_flush_bus_show(struct device *dev,
  15306. struct device_attribute *attr, char *buf)
  15307. {
  15308. ssize_t ret = 0;
  15309. ssize_t bytes = 0;
  15310. struct pt_core_data *cd = dev_get_drvdata(dev);
  15311. mutex_lock(&cd->system_lock);
  15312. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15313. ret = scnprintf(buf, strlen(buf),
  15314. "Status: 0\n"
  15315. "%s: %zd\n",
  15316. "Bytes flushed", bytes);
  15317. mutex_unlock(&cd->system_lock);
  15318. return ret;
  15319. }
  15320. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15321. pt_flush_bus_store);
  15322. /*******************************************************************************
  15323. * FUNCTION: pt_pip2_ping_test_store
  15324. *
  15325. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15326. *
  15327. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15328. * someone may want to test sending invalid packet lengths so any values
  15329. * up to 255 are allowed.
  15330. *
  15331. * RETURN: Size of passed in buffer if successful
  15332. *
  15333. * PARAMETERS:
  15334. * *dev - pointer to device structure
  15335. * *attr - pointer to device attributes
  15336. * *buf - pointer to command buffer
  15337. * size - size of buf
  15338. ******************************************************************************/
  15339. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15340. struct device_attribute *attr, const char *buf, size_t size)
  15341. {
  15342. struct pt_core_data *cd = dev_get_drvdata(dev);
  15343. u32 input_data[2];
  15344. int length;
  15345. int rc = 0;
  15346. /* Maximum input of one value */
  15347. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15348. ARRAY_SIZE(input_data));
  15349. if (length != 1) {
  15350. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15351. __func__);
  15352. rc = -EINVAL;
  15353. goto exit;
  15354. }
  15355. mutex_lock(&cd->system_lock);
  15356. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15357. cd->ping_test_size = input_data[0];
  15358. else
  15359. rc = -EINVAL;
  15360. mutex_unlock(&cd->system_lock);
  15361. exit:
  15362. if (rc)
  15363. return rc;
  15364. return size;
  15365. }
  15366. /*******************************************************************************
  15367. * FUNCTION: pt_ping_test_show
  15368. *
  15369. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15370. * PING command and ramps up the optional payload from 0 to
  15371. * ping_test_size.
  15372. * The max payload size is 247:
  15373. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15374. *
  15375. * RETURN:
  15376. * 0 = success
  15377. * !0 = failure
  15378. *
  15379. * PARAMETERS:
  15380. * *dev - pointer to device structure
  15381. * *attr - pointer to device attributes
  15382. * *buf - pointer to output buffer
  15383. ******************************************************************************/
  15384. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15385. struct device_attribute *attr, char *buf)
  15386. {
  15387. struct pt_core_data *cd = dev_get_drvdata(dev);
  15388. ssize_t ret;
  15389. int last_packet_size;
  15390. int rc = 0;
  15391. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15392. if (rc) {
  15393. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15394. return ret;
  15395. }
  15396. ret = scnprintf(buf, strlen(buf),
  15397. "Status: %d\n"
  15398. "PING payload test passed with packet sizes 0 - %d\n",
  15399. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15400. last_packet_size);
  15401. return ret;
  15402. }
  15403. /*******************************************************************************
  15404. * FUNCTION: pt_t_refresh_store
  15405. *
  15406. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15407. * in integer as the number of interrupts to count. A timer is started to
  15408. * calculate the total time it takes to see that number of interrupts.
  15409. *
  15410. * RETURN: Size of passed in buffer if successful
  15411. *
  15412. * PARAMETERS:
  15413. * *dev - pointer to device structure
  15414. * *attr - pointer to device attributes
  15415. * *buf - pointer to buffer that hold the command parameters
  15416. * size - size of buf
  15417. ******************************************************************************/
  15418. static ssize_t pt_t_refresh_store(struct device *dev,
  15419. struct device_attribute *attr, const char *buf, size_t size)
  15420. {
  15421. struct pt_core_data *cd = dev_get_drvdata(dev);
  15422. u32 input_data[2];
  15423. int length;
  15424. int rc = 0;
  15425. /* Maximum input of one value */
  15426. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15427. ARRAY_SIZE(input_data));
  15428. if (length != 1) {
  15429. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15430. __func__);
  15431. rc = -EINVAL;
  15432. goto exit;
  15433. }
  15434. mutex_lock(&cd->system_lock);
  15435. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15436. input_data[0]);
  15437. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15438. cd->t_refresh_total = input_data[0];
  15439. cd->t_refresh_count = 0;
  15440. cd->t_refresh_active = 1;
  15441. } else {
  15442. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15443. rc = -EINVAL;
  15444. }
  15445. mutex_unlock(&cd->system_lock);
  15446. exit:
  15447. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15448. if (rc)
  15449. return rc;
  15450. return size;
  15451. }
  15452. /*******************************************************************************
  15453. * FUNCTION: pt_t_refresh_show
  15454. *
  15455. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15456. * of the T-Refresh timer counting the time it takes to see a user defined
  15457. * number of interrupts.
  15458. *
  15459. * RETURN:
  15460. * 0 = success
  15461. * !0 = failure
  15462. *
  15463. * PARAMETERS:
  15464. * *dev - pointer to device structure
  15465. * *attr - pointer to device attributes
  15466. * *buf - pointer to output buffer
  15467. ******************************************************************************/
  15468. static ssize_t pt_t_refresh_show(struct device *dev,
  15469. struct device_attribute *attr, char *buf)
  15470. {
  15471. ssize_t ret = 0;
  15472. struct pt_core_data *cd = dev_get_drvdata(dev);
  15473. u32 whole;
  15474. u16 fraction;
  15475. mutex_lock(&cd->system_lock);
  15476. /* Check if we have counted the number requested */
  15477. if (cd->t_refresh_count != cd->t_refresh_total) {
  15478. ret = scnprintf(buf, strlen(buf),
  15479. "Status: 0\n"
  15480. "%s: %d\n",
  15481. "Still counting... current IRQ count",
  15482. cd->t_refresh_count);
  15483. } else {
  15484. /* Ensure T-Refresh is de-activated */
  15485. cd->t_refresh_active = 0;
  15486. whole = cd->t_refresh_time / cd->t_refresh_count;
  15487. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15488. fraction = fraction * 1000 / cd->t_refresh_count;
  15489. ret = scnprintf(buf, strlen(buf),
  15490. "Status: 0\n"
  15491. "%s: %d\n"
  15492. "%s: %d\n"
  15493. "%s: %d\n"
  15494. "%s: %d.%02d\n",
  15495. "Requested IRQ Count ", cd->t_refresh_total,
  15496. "IRQ Counted ", cd->t_refresh_count,
  15497. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15498. "Average T-Refresh (ms) ", whole, fraction);
  15499. }
  15500. mutex_unlock(&cd->system_lock);
  15501. return ret;
  15502. }
  15503. /*******************************************************************************
  15504. * FUNCTION: pt_dut_status_show
  15505. *
  15506. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15507. * more items such as operation mode,easywake state are added in the future.
  15508. *
  15509. * RETURN: Char buffer with printed scan status information
  15510. *
  15511. * PARAMETERS:
  15512. * *dev - pointer to device structure
  15513. * *attr - pointer to device attributes
  15514. * *buf - pointer to output buffer
  15515. ******************************************************************************/
  15516. static ssize_t pt_dut_status_show(struct device *dev,
  15517. struct device_attribute *attr, char *buf)
  15518. {
  15519. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15520. u8 mode = PT_MODE_UNKNOWN;
  15521. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15522. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15523. struct pt_core_data *cd = dev_get_drvdata(dev);
  15524. ssize_t ret;
  15525. u16 calculated_crc = 0;
  15526. u16 stored_crc = 0;
  15527. u8 status;
  15528. int rc = 0;
  15529. /* In STANDBY the DUT will not repond to any PIP cmd */
  15530. if (cd->fw_sys_mode_in_standby_state) {
  15531. mode = PT_MODE_OPERATIONAL;
  15532. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15533. goto print_limited_results;
  15534. }
  15535. /* Retrieve mode and FW system mode which can only be 0-4 */
  15536. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15537. if (rc || mode == PT_MODE_UNKNOWN) {
  15538. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15539. "%s: %d\n"
  15540. "%s: n/a\n"
  15541. "%s: n/a\n"
  15542. "%s: n/a\n"
  15543. "%s: n/a\n",
  15544. "Status", rc,
  15545. "Active Exec ",
  15546. "FW System Mode ",
  15547. "Stored CRC ",
  15548. "Calculated CRC ");
  15549. return ret;
  15550. } else {
  15551. if (mode == PT_MODE_OPERATIONAL) {
  15552. if (sys_mode > FW_SYS_MODE_MAX)
  15553. sys_mode = FW_SYS_MODE_UNDEFINED;
  15554. if (sys_mode != FW_SYS_MODE_TEST)
  15555. goto print_limited_results;
  15556. rc = pt_pip_verify_config_block_crc_(cd,
  15557. PT_TCH_PARM_EBID, &status,
  15558. &calculated_crc, &stored_crc);
  15559. if (rc)
  15560. goto print_limited_results;
  15561. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15562. "%s: %d\n"
  15563. "%s: %s\n"
  15564. "%s: %s\n"
  15565. "%s: 0x%04X\n"
  15566. "%s: 0x%04X\n",
  15567. "Status", rc,
  15568. "Active Exec ", "FW",
  15569. "FW System Mode ", outputstring[sys_mode],
  15570. "Stored CRC ", stored_crc,
  15571. "Calculated CRC ", calculated_crc);
  15572. return ret;
  15573. } else {
  15574. /* When in BL or unknon mode Active Exec is "n/a" */
  15575. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15576. }
  15577. }
  15578. print_limited_results:
  15579. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15580. "%s: %d\n"
  15581. "%s: %s\n"
  15582. "%s: %s\n"
  15583. "%s: n/a\n"
  15584. "%s: n/a\n",
  15585. "Status", rc,
  15586. "Active Exec ",
  15587. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15588. "FW System Mode ", outputstring[sys_mode],
  15589. "Stored CRC ",
  15590. "Calculated CRC ");
  15591. return ret;
  15592. }
  15593. #endif /* TTDL_DIAGNOSTICS */
  15594. /*******************************************************************************
  15595. * Structures of sysfs attributes for all DUT dependent sysfs node
  15596. ******************************************************************************/
  15597. static struct attribute *early_attrs[] = {
  15598. &dev_attr_hw_version.attr,
  15599. &dev_attr_drv_version.attr,
  15600. &dev_attr_drv_ver.attr,
  15601. &dev_attr_fw_version.attr,
  15602. &dev_attr_sysinfo.attr,
  15603. &dev_attr_pip2_cmd_rsp.attr,
  15604. &dev_attr_command.attr,
  15605. &dev_attr_drv_debug.attr,
  15606. &dev_attr_hw_reset.attr,
  15607. &dev_attr_response.attr,
  15608. &dev_attr_ttdl_restart.attr,
  15609. #ifdef TTDL_DIAGNOSTICS
  15610. &dev_attr_ttdl_status.attr,
  15611. &dev_attr_pip2_enter_bl.attr,
  15612. &dev_attr_pip2_exit_bl.attr,
  15613. &dev_attr_err_gpio.attr,
  15614. &dev_attr_flush_bus.attr,
  15615. &dev_attr_ttdl_bist.attr,
  15616. #endif /* TTDL_DIAGNOSTICS */
  15617. NULL,
  15618. };
  15619. static struct attribute_group early_attr_group = {
  15620. .attrs = early_attrs,
  15621. };
  15622. static struct device_attribute pip2_attributes[] = {
  15623. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15624. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15625. #ifdef TTDL_DIAGNOSTICS
  15626. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15627. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15628. pt_pip2_ping_test_store),
  15629. #endif
  15630. };
  15631. static struct device_attribute attributes[] = {
  15632. __ATTR(dut_debug, 0644,
  15633. pt_dut_debug_show, pt_drv_debug_store),
  15634. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15635. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15636. __ATTR(get_param, 0644,
  15637. pt_get_param_show, pt_get_param_store),
  15638. #ifdef EASYWAKE_TSG6
  15639. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15640. pt_easy_wakeup_gesture_store),
  15641. __ATTR(easy_wakeup_gesture_id, 0444,
  15642. pt_easy_wakeup_gesture_id_show, NULL),
  15643. __ATTR(easy_wakeup_gesture_data, 0444,
  15644. pt_easy_wakeup_gesture_data_show, NULL),
  15645. #endif
  15646. #ifdef TTDL_DIAGNOSTICS
  15647. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15648. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15649. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15650. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15651. #endif /* TTDL_DIAGNOSTICS */
  15652. };
  15653. /*******************************************************************************
  15654. * FUNCTION: add_sysfs_interfaces
  15655. *
  15656. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15657. *
  15658. * RETURN:
  15659. * 0 = success
  15660. * !0 = failure
  15661. *
  15662. * PARAMETERS:
  15663. * *dev - pointer to device structure
  15664. ******************************************************************************/
  15665. static int add_sysfs_interfaces(struct device *dev)
  15666. {
  15667. int i;
  15668. int j = 0;
  15669. struct pt_core_data *cd = dev_get_drvdata(dev);
  15670. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15671. if (device_create_file(dev, attributes + i))
  15672. goto undo;
  15673. }
  15674. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15675. __func__, cd->active_dut_generation);
  15676. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15677. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15678. if (device_create_file(dev, pip2_attributes + j))
  15679. goto undo;
  15680. }
  15681. }
  15682. return 0;
  15683. undo:
  15684. for (i--; i >= 0; i--)
  15685. device_remove_file(dev, attributes + i);
  15686. for (j--; j >= 0; j--)
  15687. device_remove_file(dev, pip2_attributes + j);
  15688. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15689. __func__);
  15690. return -ENODEV;
  15691. }
  15692. /*******************************************************************************
  15693. * FUNCTION: remove_sysfs_interfaces
  15694. *
  15695. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15696. *
  15697. * RETURN: void
  15698. *
  15699. * PARAMETERS:
  15700. * *dev - pointer to device structure
  15701. ******************************************************************************/
  15702. static void remove_sysfs_interfaces(struct device *dev)
  15703. {
  15704. int i;
  15705. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15706. device_remove_file(dev, attributes + i);
  15707. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15708. device_remove_file(dev, pip2_attributes + i);
  15709. }
  15710. /*******************************************************************************
  15711. * FUNCTION: remove_sysfs_and_modules
  15712. *
  15713. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15714. *
  15715. * RETURN: void
  15716. *
  15717. * PARAMETERS:
  15718. * *dev - pointer to device structure
  15719. ******************************************************************************/
  15720. static void remove_sysfs_and_modules(struct device *dev)
  15721. {
  15722. struct pt_core_data *cd = dev_get_drvdata(dev);
  15723. /* Queued work should be removed before to release loader module */
  15724. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15725. pt_release_modules(cd);
  15726. pt_btn_release(dev);
  15727. pt_mt_release(dev);
  15728. remove_sysfs_interfaces(dev);
  15729. }
  15730. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15731. {
  15732. int retval;
  15733. /* Get pinctrl if target uses pinctrl */
  15734. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15735. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15736. retval = PTR_ERR(cd->ts_pinctrl);
  15737. dev_dbg(cd->dev,
  15738. "Target does not use pinctrl %d\n", retval);
  15739. goto err_pinctrl_get;
  15740. }
  15741. cd->pinctrl_state_active
  15742. = pinctrl_lookup_state(cd->ts_pinctrl,
  15743. PINCTRL_STATE_ACTIVE);
  15744. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15745. retval = PTR_ERR(cd->pinctrl_state_active);
  15746. dev_err(cd->dev,
  15747. "Can not lookup %s pinstate %d\n",
  15748. PINCTRL_STATE_ACTIVE, retval);
  15749. goto err_pinctrl_lookup;
  15750. }
  15751. cd->pinctrl_state_suspend
  15752. = pinctrl_lookup_state(cd->ts_pinctrl,
  15753. PINCTRL_STATE_SUSPEND);
  15754. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15755. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15756. dev_err(cd->dev,
  15757. "Can not lookup %s pinstate %d\n",
  15758. PINCTRL_STATE_SUSPEND, retval);
  15759. goto err_pinctrl_lookup;
  15760. }
  15761. cd->pinctrl_state_release
  15762. = pinctrl_lookup_state(cd->ts_pinctrl,
  15763. PINCTRL_STATE_RELEASE);
  15764. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15765. retval = PTR_ERR(cd->pinctrl_state_release);
  15766. dev_dbg(cd->dev,
  15767. "Can not lookup %s pinstate %d\n",
  15768. PINCTRL_STATE_RELEASE, retval);
  15769. }
  15770. return 0;
  15771. err_pinctrl_lookup:
  15772. devm_pinctrl_put(cd->ts_pinctrl);
  15773. err_pinctrl_get:
  15774. cd->ts_pinctrl = NULL;
  15775. return retval;
  15776. }
  15777. /*******************************************************************************
  15778. *******************************************************************************
  15779. * FUNCTION: pt_probe
  15780. *
  15781. * SUMMARY: Probe of the core module.
  15782. *
  15783. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15784. * probe functionality is split into two functions; pt_probe() and
  15785. * pt_probe_complete(). the initial setup is done in this function which
  15786. * then creates a WORK task which runs after the probe timer expires. This
  15787. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15788. *
  15789. * RETURN:
  15790. * 0 = success
  15791. * !0 = failure
  15792. *
  15793. * PARAMETERS:
  15794. * *ops - pointer to the bus
  15795. * *dev - pointer to the device structure
  15796. * irq - IRQ
  15797. * xfer_buf_size - size of the buffer
  15798. ******************************************************************************/
  15799. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15800. u16 irq, size_t xfer_buf_size)
  15801. {
  15802. struct pt_core_data *cd;
  15803. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15804. enum pt_atten_type type;
  15805. struct i2c_client *client = to_i2c_client(dev);
  15806. int rc = 0;
  15807. u8 pip_ver_major;
  15808. u8 pip_ver_minor;
  15809. u32 status = STARTUP_STATUS_START;
  15810. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15811. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15812. __func__);
  15813. rc = -ENODEV;
  15814. goto error_no_pdata;
  15815. }
  15816. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15817. if (!pdata->core_pdata->power) {
  15818. pt_debug(dev, DL_ERROR,
  15819. "%s: Missing platform data function\n",
  15820. __func__);
  15821. rc = -ENODEV;
  15822. goto error_no_pdata;
  15823. }
  15824. }
  15825. /* get context and debug print buffers */
  15826. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15827. if (!cd) {
  15828. rc = -ENOMEM;
  15829. goto error_no_pdata;
  15830. }
  15831. /* Initialize device info */
  15832. cd->dev = dev;
  15833. cd->pdata = pdata;
  15834. cd->cpdata = pdata->core_pdata;
  15835. cd->bus_ops = ops;
  15836. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15837. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15838. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15839. cd->hw_detected = false;
  15840. cd->pip2_prot_active = false;
  15841. cd->pip2_send_user_cmd = false;
  15842. cd->bl_pip_ver_ready = false;
  15843. cd->app_pip_ver_ready = false;
  15844. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15845. cd->get_param_id = 0;
  15846. cd->watchdog_enabled = 0;
  15847. cd->startup_retry_count = 0;
  15848. cd->core_probe_complete = 0;
  15849. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15850. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15851. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15852. cd->flashless_dut = 0;
  15853. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15854. cd->bl_with_no_int = 0;
  15855. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15856. cd->multi_chip = PT_FEATURE_DISABLE;
  15857. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15858. cd->sleep_state = SS_SLEEP_NONE;
  15859. cd->quick_boot = false;
  15860. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15861. cd->set_dut_generation = true;
  15862. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15863. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15864. cd->set_dut_generation = true;
  15865. cd->active_dut_generation = DUT_PIP1_ONLY;
  15866. } else {
  15867. cd->set_dut_generation = false;
  15868. cd->active_dut_generation = DUT_UNKNOWN;
  15869. }
  15870. /* Initialize with platform data */
  15871. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15872. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15873. cd->hid_cmd_state = 1;
  15874. cd->fw_updating = false;
  15875. cd->multi_chip = 0;
  15876. #ifdef TTDL_DIAGNOSTICS
  15877. cd->t_refresh_active = 0;
  15878. cd->t_refresh_count = 0;
  15879. cd->pip2_crc_error_count = 0;
  15880. cd->wd_xres_count = 0;
  15881. cd->bl_retry_packet_count = 0;
  15882. cd->file_erase_timeout_count = 0;
  15883. cd->show_tt_data = false;
  15884. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15885. cd->err_gpio = 0;
  15886. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15887. cd->ttdl_bist_select = 0x07;
  15888. cd->force_pip2_seq = 0;
  15889. #endif /* TTDL_DIAGNOSTICS */
  15890. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15891. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15892. sizeof(PT_PIP2_BIN_FILE_PATH));
  15893. pt_init_hid_descriptor(&cd->hid_desc);
  15894. /* Read and store the descriptor lengths */
  15895. cd->hid_core.hid_report_desc_len =
  15896. le16_to_cpu(cd->hid_desc.report_desc_len);
  15897. cd->hid_core.hid_max_input_len =
  15898. le16_to_cpu(cd->hid_desc.max_input_len);
  15899. cd->hid_core.hid_max_output_len =
  15900. le16_to_cpu(cd->hid_desc.max_output_len);
  15901. /* Initialize mutexes and spinlocks */
  15902. mutex_init(&cd->module_list_lock);
  15903. mutex_init(&cd->system_lock);
  15904. mutex_init(&cd->sysfs_lock);
  15905. mutex_init(&cd->ttdl_restart_lock);
  15906. mutex_init(&cd->firmware_class_lock);
  15907. spin_lock_init(&cd->spinlock);
  15908. /* Initialize module list */
  15909. INIT_LIST_HEAD(&cd->module_list);
  15910. /* Initialize attention lists */
  15911. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15912. INIT_LIST_HEAD(&cd->atten_list[type]);
  15913. /* Initialize parameter list */
  15914. INIT_LIST_HEAD(&cd->param_list);
  15915. /* Initialize wait queue */
  15916. init_waitqueue_head(&cd->wait_q);
  15917. rc = pt_ts_pinctrl_init(cd);
  15918. if (!rc && cd->ts_pinctrl) {
  15919. /*
  15920. * Pinctrl handle is optional. If pinctrl handle is found
  15921. * let pins to be configured in active state. If not
  15922. * found continue further without error.
  15923. */
  15924. rc = pinctrl_select_state(cd->ts_pinctrl,
  15925. cd->pinctrl_state_active);
  15926. if (rc < 0)
  15927. dev_err(&client->dev, "failed to select pin to active state\n");
  15928. }
  15929. rc = pt_get_regulator(cd, true);
  15930. if (rc) {
  15931. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15932. goto error_alloc_data;
  15933. }
  15934. rc = pt_enable_regulator(cd, true);
  15935. if (rc) {
  15936. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15937. goto error_get_regulator;
  15938. }
  15939. /* Initialize works */
  15940. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15941. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15942. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15943. /* Initialize HID specific data */
  15944. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15945. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15946. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15947. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15948. cd->hid_core.hid_desc_register =
  15949. cpu_to_le16(cd->cpdata->hid_desc_register);
  15950. /* Set platform easywake value */
  15951. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15952. #ifdef CONFIG_DRM
  15953. /* Setup active dsi panel */
  15954. active_panel = cd->cpdata->active_panel;
  15955. #endif
  15956. /* Set platform panel_id value */
  15957. cd->panel_id_support = cd->cpdata->panel_id_support;
  15958. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15959. /* Set Panel ID to default to 0 */
  15960. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15961. else
  15962. /* Set Panel ID to Not Enabled */
  15963. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15964. /* Initialize hw_version default to FFFF.FFFF.FF */
  15965. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15966. dev_set_drvdata(dev, cd);
  15967. /* PtSBC builds will call this function in pt_probe_complete() */
  15968. pt_add_core(dev);
  15969. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15970. if (rc) {
  15971. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15972. __func__);
  15973. goto error_enable_regulator;
  15974. }
  15975. /*
  15976. * Save the pointer to a global value, which will be used
  15977. * in ttdl_restart function
  15978. */
  15979. cd->bus_ops = ops;
  15980. /*
  15981. * When the IRQ GPIO is not direclty accessible and no function is
  15982. * defined to get the IRQ status, the IRQ passed in must be assigned
  15983. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15984. */
  15985. if (!cd->cpdata->irq_stat) {
  15986. cd->irq = irq;
  15987. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15988. __func__, cd->irq);
  15989. }
  15990. /* Call platform init function before setting up the GPIO's */
  15991. if (cd->cpdata->init) {
  15992. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15993. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15994. } else {
  15995. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15996. __func__);
  15997. rc = 0;
  15998. }
  15999. if (rc < 0) {
  16000. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  16001. __func__, rc);
  16002. }
  16003. /* Power on any needed regulator(s) */
  16004. if (cd->cpdata->setup_power) {
  16005. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  16006. rc = cd->cpdata->setup_power(cd->cpdata,
  16007. PT_MT_POWER_ON, cd->dev);
  16008. } else {
  16009. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  16010. __func__);
  16011. rc = 0;
  16012. }
  16013. if (rc < 0)
  16014. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  16015. __func__, rc);
  16016. #ifdef TTDL_DIAGNOSTICS
  16017. cd->watchdog_irq_stuck_count = 0;
  16018. cd->bus_transmit_error_count = 0;
  16019. #endif /* TTDL_DIAGNOSTICS */
  16020. if (cd->cpdata->detect) {
  16021. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  16022. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  16023. pt_platform_detect_read);
  16024. if (!rc) {
  16025. cd->hw_detected = true;
  16026. pt_debug(cd->dev, DL_INFO,
  16027. "%s: HW detected\n", __func__);
  16028. } else {
  16029. cd->hw_detected = false;
  16030. pt_debug(cd->dev, DL_INFO,
  16031. "%s: No HW detected\n", __func__);
  16032. rc = -ENODEV;
  16033. goto error_detect;
  16034. }
  16035. } else {
  16036. pt_debug(dev, DL_ERROR,
  16037. "%s: PARADE No HW detect function pointer\n",
  16038. __func__);
  16039. /*
  16040. * "hw_reset" is not needed in the "if" statement,
  16041. * because "hw_reset" is already included in "hw_detect"
  16042. * function.
  16043. */
  16044. rc = pt_hw_hard_reset(cd);
  16045. if (rc)
  16046. pt_debug(cd->dev, DL_ERROR,
  16047. "%s: FAILED to execute HARD reset\n",
  16048. __func__);
  16049. }
  16050. if (cd->cpdata->setup_irq) {
  16051. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  16052. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  16053. if (rc) {
  16054. pt_debug(dev, DL_ERROR,
  16055. "%s: Error, couldn't setup IRQ\n", __func__);
  16056. goto error_setup_irq;
  16057. }
  16058. } else {
  16059. pt_debug(dev, DL_ERROR,
  16060. "%s: IRQ function pointer not setup\n",
  16061. __func__);
  16062. goto error_setup_irq;
  16063. }
  16064. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  16065. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  16066. (unsigned long)cd);
  16067. #else
  16068. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  16069. #endif
  16070. pt_stop_wd_timer(cd);
  16071. #ifdef TTHE_TUNER_SUPPORT
  16072. mutex_init(&cd->tthe_lock);
  16073. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  16074. 0644, NULL, cd, &tthe_debugfs_fops);
  16075. #endif
  16076. rc = device_init_wakeup(dev, 1);
  16077. if (rc < 0)
  16078. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  16079. __func__, rc);
  16080. pm_runtime_get_noresume(dev);
  16081. pm_runtime_set_active(dev);
  16082. pm_runtime_enable(dev);
  16083. /* If IRQ asserted, read out all from buffer to release INT pin */
  16084. if (cd->cpdata->irq_stat) {
  16085. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  16086. } else {
  16087. /* Force a read in case the reset sentinel already arrived */
  16088. rc = pt_read_input(cd);
  16089. if (!rc)
  16090. pt_parse_input(cd);
  16091. }
  16092. /* Without sleep DUT is not ready and will NAK the first write */
  16093. msleep(150);
  16094. /* Attempt to set the DUT generation if not yet set */
  16095. if (cd->active_dut_generation == DUT_UNKNOWN) {
  16096. if (cd->bl_pip_ver_ready ||
  16097. (cd->app_pip_ver_ready &&
  16098. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  16099. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  16100. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  16101. __func__, cd->active_dut_generation);
  16102. } else {
  16103. rc = _pt_detect_dut_generation(cd->dev,
  16104. &status, &cd->active_dut_generation,
  16105. &cd->mode);
  16106. if ((cd->active_dut_generation == DUT_UNKNOWN)
  16107. || rc) {
  16108. pt_debug(cd->dev, DL_ERROR,
  16109. " === DUT Gen unknown, Skip Enum ===\n");
  16110. goto skip_enum;
  16111. }
  16112. }
  16113. }
  16114. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  16115. &pip_ver_major, &pip_ver_minor);
  16116. if (pip_ver_major == 2) {
  16117. cd->bl_pip_ver_ready = true;
  16118. pt_debug(dev, DL_ERROR,
  16119. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  16120. pip_ver_minor);
  16121. cd->hw_detected = true;
  16122. } else if (pip_ver_major == 1) {
  16123. cd->app_pip_ver_ready = true;
  16124. pt_debug(dev, DL_ERROR,
  16125. " === PIP1.%d Detected ===\n", pip_ver_minor);
  16126. cd->hw_detected = true;
  16127. } else {
  16128. cd->sysinfo.ttdata.pip_ver_major = 0;
  16129. cd->sysinfo.ttdata.pip_ver_minor = 0;
  16130. cd->app_pip_ver_ready = false;
  16131. cd->hw_detected = false;
  16132. pt_debug(dev, DL_ERROR,
  16133. " === PIP Version Not Detected, Skip Enum ===\n");
  16134. /* For legacy DUTS proceed, enum will attempt to launch app */
  16135. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  16136. goto skip_enum;
  16137. }
  16138. rc = pt_enum_with_dut(cd, false, &status);
  16139. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  16140. __func__, cd->startup_status, status);
  16141. if (rc == -ENODEV) {
  16142. pt_debug(cd->dev, DL_ERROR,
  16143. "%s: Enumeration Failed r=%d\n", __func__, rc);
  16144. /* For PtSBC don't error out, allow TTDL to stay up */
  16145. rc = -EPROBE_DEFER;
  16146. goto error_after_startup;
  16147. }
  16148. /* Suspend scanning until probe is complete to avoid asyc touches */
  16149. pt_pip_suspend_scanning_(cd);
  16150. if (cd->hw_detected) {
  16151. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16152. __func__);
  16153. rc = add_sysfs_interfaces(dev);
  16154. if (rc < 0) {
  16155. pt_debug(dev, DL_ERROR,
  16156. "%s: Error, fail sysfs init\n", __func__);
  16157. goto error_after_startup;
  16158. }
  16159. } else {
  16160. pt_debug(dev, DL_ERROR,
  16161. "%s: No HW detected, sysfs interfaces not added\n",
  16162. __func__);
  16163. }
  16164. skip_enum:
  16165. pm_runtime_put_sync(dev);
  16166. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16167. rc = pt_mt_probe(dev);
  16168. if (rc < 0) {
  16169. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16170. __func__);
  16171. goto error_after_sysfs_create;
  16172. }
  16173. rc = pt_btn_probe(dev);
  16174. if (rc < 0) {
  16175. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16176. __func__);
  16177. goto error_after_startup_mt;
  16178. }
  16179. pt_probe_modules(cd);
  16180. #ifdef CONFIG_HAS_EARLYSUSPEND
  16181. pt_setup_early_suspend(cd);
  16182. #elif defined(CONFIG_DRM)
  16183. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16184. pt_setup_drm_notifier(cd);
  16185. INIT_WORK(&cd->resume_offload_work, pt_resume_offload_work);
  16186. INIT_WORK(&cd->suspend_offload_work, pt_suspend_offload_work);
  16187. #elif defined(CONFIG_FB)
  16188. pt_setup_fb_notifier(cd);
  16189. #endif
  16190. #ifdef NEED_SUSPEND_NOTIFIER
  16191. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16192. register_pm_notifier(&cd->pm_notifier);
  16193. #endif
  16194. pt_pip_resume_scanning_(cd);
  16195. mutex_lock(&cd->system_lock);
  16196. cd->startup_status |= status;
  16197. cd->core_probe_complete = 1;
  16198. mutex_unlock(&cd->system_lock);
  16199. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16200. __func__);
  16201. return 0;
  16202. error_after_startup_mt:
  16203. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16204. pt_mt_release(dev);
  16205. error_after_sysfs_create:
  16206. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16207. pm_runtime_disable(dev);
  16208. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16209. device_wakeup_disable(dev);
  16210. #endif
  16211. device_init_wakeup(dev, 0);
  16212. cancel_work_sync(&cd->enum_work);
  16213. pt_stop_wd_timer(cd);
  16214. pt_free_si_ptrs(cd);
  16215. remove_sysfs_interfaces(dev);
  16216. error_after_startup:
  16217. pr_err("%s PARADE error_after_startup\n", __func__);
  16218. del_timer(&cd->watchdog_timer);
  16219. if (cd->cpdata->setup_irq)
  16220. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16221. error_setup_irq:
  16222. error_detect:
  16223. if (cd->cpdata->init)
  16224. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16225. if (cd->cpdata->setup_power)
  16226. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16227. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16228. error_enable_regulator:
  16229. pt_del_core(dev);
  16230. dev_set_drvdata(dev, NULL);
  16231. pt_enable_regulator(cd, false);
  16232. error_get_regulator:
  16233. pt_get_regulator(cd, false);
  16234. error_alloc_data:
  16235. kfree(cd);
  16236. error_no_pdata:
  16237. pr_err("%s failed.\n", __func__);
  16238. return rc;
  16239. }
  16240. EXPORT_SYMBOL_GPL(pt_probe);
  16241. /*******************************************************************************
  16242. * FUNCTION: pt_release
  16243. *
  16244. * SUMMARY: This function does the following cleanup:
  16245. * - Releases all probed modules
  16246. * - Stops the watchdog
  16247. * - Cancels all pending work tasks
  16248. * - Removes all created sysfs nodes
  16249. * - Removes all created debugfs nodes
  16250. * - Frees the IRQ
  16251. * - Deletes the core
  16252. * - Frees all pointers and HID reports
  16253. *
  16254. * RETURN:
  16255. * 0 = success
  16256. * !0 = failure
  16257. *
  16258. * PARAMETERS:
  16259. * *cd - pointer to the core data structure
  16260. ******************************************************************************/
  16261. int pt_release(struct pt_core_data *cd)
  16262. {
  16263. struct device *dev = cd->dev;
  16264. /*
  16265. * Suspend the device before freeing the startup_work and stopping
  16266. * the watchdog since sleep function restarts watchdog on failure
  16267. */
  16268. pm_runtime_suspend(dev);
  16269. pm_runtime_disable(dev);
  16270. /*
  16271. * Any 'work' that can trigger a new thread should be canceled first.
  16272. * The watchdog is also stopped again because another thread could have
  16273. * restarted it. The irq_work is cancelled last because it is used for
  16274. * all I2C/SPI communication.
  16275. */
  16276. pt_stop_wd_timer(cd);
  16277. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16278. cancel_work_sync(&cd->ttdl_restart_work);
  16279. cancel_work_sync(&cd->enum_work);
  16280. cancel_work_sync(&cd->resume_offload_work);
  16281. cancel_work_sync(&cd->suspend_offload_work);
  16282. cancel_work_sync(&cd->resume_work);
  16283. cancel_work_sync(&cd->suspend_work);
  16284. destroy_workqueue(cd->pt_workqueue);
  16285. pt_stop_wd_timer(cd);
  16286. pt_release_modules(cd);
  16287. pt_proximity_release(dev);
  16288. pt_btn_release(dev);
  16289. pt_mt_release(dev);
  16290. #ifdef CONFIG_HAS_EARLYSUSPEND
  16291. unregister_early_suspend(&cd->es);
  16292. #elif defined(CONFIG_DRM)
  16293. if (active_panel)
  16294. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16295. #elif defined(CONFIG_FB)
  16296. fb_unregister_client(&cd->fb_notifier);
  16297. #endif
  16298. #ifdef NEED_SUSPEND_NOTIFIER
  16299. unregister_pm_notifier(&cd->pm_notifier);
  16300. #endif
  16301. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16302. device_wakeup_disable(dev);
  16303. #endif
  16304. device_init_wakeup(dev, 0);
  16305. #ifdef TTHE_TUNER_SUPPORT
  16306. mutex_lock(&cd->tthe_lock);
  16307. cd->tthe_exit = 1;
  16308. wake_up(&cd->wait_q);
  16309. mutex_unlock(&cd->tthe_lock);
  16310. debugfs_remove(cd->tthe_debugfs);
  16311. #endif
  16312. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16313. remove_sysfs_interfaces(dev);
  16314. disable_irq_nosync(cd->irq);
  16315. if (cd->cpdata->setup_irq)
  16316. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16317. if (cd->cpdata->init)
  16318. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16319. if (cd->cpdata->setup_power)
  16320. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16321. dev_set_drvdata(dev, NULL);
  16322. pt_del_core(dev);
  16323. pt_enable_regulator(cd, false);
  16324. pt_get_regulator(cd, false);
  16325. pt_free_si_ptrs(cd);
  16326. kfree(cd);
  16327. return 0;
  16328. }
  16329. EXPORT_SYMBOL_GPL(pt_release);
  16330. MODULE_LICENSE("GPL");
  16331. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16332. MODULE_AUTHOR("Parade Technologies <[email protected]>");