pt_core.c 500 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410
  1. /*
  2. * pt_core.c
  3. * Parade TrueTouch(TM) Standard Product Core Module.
  4. * For use with Parade touchscreen controllers.
  5. * Supported parts include:
  6. * TMA5XX
  7. * TMA448
  8. * TMA445A
  9. * TT21XXX
  10. * TT31XXX
  11. * TT4XXXX
  12. * TT7XXX
  13. * TC3XXX
  14. *
  15. * Copyright (C) 2015-2020 Parade Technologies
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * version 2, and only version 2, as published by the
  20. * Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * Contact Parade Technologies at www.paradetech.com <[email protected]>
  28. */
  29. #include <linux/i2c.h>
  30. #include <linux/gpio.h>
  31. #include <linux/kthread.h>
  32. #include "pt_regs.h"
  33. #define PINCTRL_STATE_ACTIVE "pmx_ts_active"
  34. #define PINCTRL_STATE_SUSPEND "pmx_ts_suspend"
  35. #define PINCTRL_STATE_RELEASE "pmx_ts_release"
  36. #define FT_VTG_MIN_UV 2800000
  37. #define FT_VTG_MAX_UV 2800000
  38. #define FT_I2C_VTG_MIN_UV 1800000
  39. #define FT_I2C_VTG_MAX_UV 1800000
  40. #define PT_CORE_STARTUP_RETRY_COUNT 3
  41. #define PT_STATUS_STR_LEN (50)
  42. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  43. static const char *pt_driver_core_name = PT_CORE_NAME;
  44. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  45. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  46. struct pt_hid_field {
  47. int report_count;
  48. int report_size;
  49. int size; /* report_count * report_size */
  50. int offset;
  51. int data_type;
  52. int logical_min;
  53. int logical_max;
  54. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  55. u32 usage_page;
  56. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  57. struct pt_hid_report *report;
  58. bool record_field;
  59. };
  60. struct pt_hid_report {
  61. u8 id;
  62. u8 type;
  63. int size;
  64. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  65. int num_fields;
  66. int record_field_index;
  67. int header_size;
  68. int record_size;
  69. u32 usage_page;
  70. };
  71. struct atten_node {
  72. struct list_head node;
  73. char *id;
  74. struct device *dev;
  75. int (*func)(struct device *dev);
  76. int mode;
  77. };
  78. struct param_node {
  79. struct list_head node;
  80. u8 id;
  81. u32 value;
  82. u8 size;
  83. };
  84. struct module_node {
  85. struct list_head node;
  86. struct pt_module *module;
  87. void *data;
  88. };
  89. struct pt_hid_cmd {
  90. u8 opcode;
  91. u8 report_type;
  92. union {
  93. u8 report_id;
  94. u8 power_state;
  95. };
  96. u8 has_data_register;
  97. size_t write_length;
  98. u8 *write_buf;
  99. u8 *read_buf;
  100. u8 wait_interrupt;
  101. u8 reset_cmd;
  102. u16 timeout_ms;
  103. };
  104. struct pt_hid_output {
  105. u8 cmd_type;
  106. u16 length;
  107. u8 command_code;
  108. size_t write_length;
  109. u8 *write_buf;
  110. u8 novalidate;
  111. u8 reset_expected;
  112. u16 timeout_ms;
  113. };
  114. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  115. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  116. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  117. #define CREATE_PIP1_FW_CMD(command) \
  118. .cmd_type = PIP1_CMD_TYPE_FW, \
  119. .command_code = command
  120. #define CREATE_PIP1_BL_CMD(command) \
  121. .cmd_type = PIP1_CMD_TYPE_BL, \
  122. .command_code = command
  123. #define PT_MAX_PR_BUF_SIZE 2048
  124. /*******************************************************************************
  125. * FUNCTION: pt_pr_buf
  126. *
  127. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  128. *
  129. * RETURN: Void
  130. *
  131. * PARAMETERS:
  132. * *dev - pointer to Device structure
  133. * debug_level - requested debug level to print at
  134. * *buf - pointer to buffer to print
  135. * buf_len - size of buf
  136. * *data_name - Descriptive name of data prefixed to data
  137. ******************************************************************************/
  138. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  139. u16 buf_len, const char *data_name)
  140. {
  141. struct pt_core_data *cd = dev_get_drvdata(dev);
  142. int i;
  143. ssize_t pr_buf_index = 0;
  144. int max_size;
  145. /* only proceed if valid debug level and there is data to print */
  146. if (debug_level <= cd->debug_level && buf_len > 0) {
  147. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  148. if (!pr_buf)
  149. return;
  150. /*
  151. * With a space each printed char takes 3 bytes, subtract
  152. * the length of the data_name prefix as well as 11 bytes
  153. * for the " [0..xxx]: " printed before the data.
  154. */
  155. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  156. /* Ensure pr_buf_index stays within the 1018 size */
  157. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  158. data_name);
  159. for (i = 0; i < buf_len && i < max_size; i++)
  160. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  161. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  162. pt_debug(dev, debug_level, "%s\n", pr_buf);
  163. kfree(pr_buf);
  164. }
  165. }
  166. EXPORT_SYMBOL_GPL(pt_pr_buf);
  167. #ifdef TTHE_TUNER_SUPPORT
  168. /*******************************************************************************
  169. * FUNCTION: tthe_print
  170. *
  171. * SUMMARY: Format data name and time stamp as the header and format the
  172. * content of input buffer with hex base to "tthe_buf". And then wake up event
  173. * semaphore for tthe debugfs node.
  174. *
  175. * RETURN:
  176. * 0 = success
  177. * !0 = failure
  178. *
  179. * PARAMETERS:
  180. * *cd - pointer to core data
  181. * *buf - pointer to input buffer
  182. * buf_len - size of input buffer
  183. * *data_name - pointer to data name
  184. ******************************************************************************/
  185. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  186. const u8 *data_name)
  187. {
  188. int name_len = strlen(data_name);
  189. int i, n;
  190. u8 *p;
  191. int remain;
  192. u8 data_name_with_time_stamp[100];
  193. /* Prepend timestamp, if requested, to data_name */
  194. if (cd->show_timestamp) {
  195. scnprintf(data_name_with_time_stamp,
  196. sizeof(data_name_with_time_stamp),
  197. "[%u] %s", pt_get_time_stamp(), data_name);
  198. data_name = data_name_with_time_stamp;
  199. name_len = strlen(data_name);
  200. }
  201. mutex_lock(&cd->tthe_lock);
  202. if (!cd->tthe_buf)
  203. goto exit;
  204. /* Add 1 due to the '\n' that is appended at the end */
  205. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  206. goto exit;
  207. if (name_len + buf_len == 0)
  208. goto exit;
  209. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  210. if (remain < name_len)
  211. name_len = remain;
  212. p = cd->tthe_buf + cd->tthe_buf_len;
  213. memcpy(p, data_name, name_len);
  214. cd->tthe_buf_len += name_len;
  215. p += name_len;
  216. remain -= name_len;
  217. *p = 0;
  218. for (i = 0; i < buf_len; i++) {
  219. n = scnprintf(p, remain, "%02X ", buf[i]);
  220. if (n <= 0)
  221. break;
  222. p += n;
  223. remain -= n;
  224. cd->tthe_buf_len += n;
  225. }
  226. n = scnprintf(p, remain, "\n");
  227. cd->tthe_buf_len += n;
  228. exit:
  229. wake_up(&cd->wait_q);
  230. mutex_unlock(&cd->tthe_lock);
  231. return 0;
  232. }
  233. /*******************************************************************************
  234. * FUNCTION: _pt_request_tthe_print
  235. *
  236. * SUMMARY: Function pointer included in core_cmds to allow other modules
  237. * to request to print data to the "tthe_buffer".
  238. *
  239. * RETURN:
  240. * 0 = success
  241. * !0 = failure
  242. *
  243. * PARAMETERS:
  244. * *dev - pointer to device structure
  245. ******************************************************************************/
  246. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  247. int buf_len, const u8 *data_name)
  248. {
  249. struct pt_core_data *cd = dev_get_drvdata(dev);
  250. return tthe_print(cd, buf, buf_len, data_name);
  251. }
  252. #endif
  253. /*******************************************************************************
  254. * FUNCTION: pt_platform_detect_read
  255. *
  256. * SUMMARY: To be passed to platform dectect function to perform a read
  257. * operation.
  258. *
  259. * RETURN:
  260. * 0 = success
  261. * !0 = failure
  262. *
  263. * PARAMETERS:
  264. * *dev - pointer to Device structure
  265. * *buf - pointer to buffer where the data read will be stored
  266. * size - size to be read
  267. ******************************************************************************/
  268. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  269. {
  270. struct pt_core_data *cd = dev_get_drvdata(dev);
  271. return pt_adap_read_default(cd, buf, size);
  272. }
  273. /*******************************************************************************
  274. * FUNCTION: pt_add_parameter
  275. *
  276. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  277. * On every reset of the DUT this linked list is traversed and all
  278. * parameters in it are restored to the DUT.
  279. *
  280. * RETURN:
  281. * 0 = success
  282. * !0 = failure
  283. *
  284. * PARAMETERS:
  285. * *cd - pointer to core data
  286. * param_id - parameter ID to add
  287. * param_value - Value corresponding to the ID
  288. * param_size - Size of param_value
  289. ******************************************************************************/
  290. static int pt_add_parameter(struct pt_core_data *cd,
  291. u8 param_id, u32 param_value, u8 param_size)
  292. {
  293. struct param_node *param, *param_new;
  294. /* Check if parameter already exists in the list */
  295. spin_lock(&cd->spinlock);
  296. list_for_each_entry(param, &cd->param_list, node) {
  297. if (param->id == param_id) {
  298. /* Update parameter */
  299. param->value = param_value;
  300. pt_debug(cd->dev, DL_INFO,
  301. "%s: Update parameter id:%d value:%d size:%d\n",
  302. __func__, param_id, param_value, param_size);
  303. goto exit_unlock;
  304. }
  305. }
  306. spin_unlock(&cd->spinlock);
  307. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  308. if (!param_new)
  309. return -ENOMEM;
  310. param_new->id = param_id;
  311. param_new->value = param_value;
  312. param_new->size = param_size;
  313. pt_debug(cd->dev, DL_INFO,
  314. "%s: Add parameter id:%d value:%d size:%d\n",
  315. __func__, param_id, param_value, param_size);
  316. spin_lock(&cd->spinlock);
  317. list_add(&param_new->node, &cd->param_list);
  318. exit_unlock:
  319. spin_unlock(&cd->spinlock);
  320. return 0;
  321. }
  322. #ifdef TTDL_DIAGNOSTICS
  323. /*******************************************************************************
  324. * FUNCTION: pt_erase_parameter_list
  325. *
  326. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  327. * pairs. In some test cases this functionality is needed to ensure DUT
  328. * returns to a virgin state after a reset and no parameters are restored.
  329. *
  330. * RETURN:
  331. * 0 = success
  332. * !0 = failure
  333. *
  334. * PARAMETERS:
  335. * *cd - pointer to core data
  336. ******************************************************************************/
  337. static int pt_erase_parameter_list(struct pt_core_data *cd)
  338. {
  339. struct param_node *pos, *temp;
  340. spin_lock(&cd->spinlock);
  341. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  342. pt_debug(cd->dev, DL_INFO,
  343. "%s: Parameter Restore List - remove 0x%02x\n",
  344. __func__, pos->id);
  345. list_del(&pos->node);
  346. kfree(pos);
  347. }
  348. spin_unlock(&cd->spinlock);
  349. return 0;
  350. }
  351. /*******************************************************************************
  352. * FUNCTION: pt_count_parameter_list
  353. *
  354. * SUMMARY: Count the items in the RAM parameter restor list
  355. *
  356. * RETURN:
  357. * 0 = success
  358. * !0 = failure
  359. *
  360. * PARAMETERS:
  361. * *cd - pointer to core data
  362. ******************************************************************************/
  363. static int pt_count_parameter_list(struct pt_core_data *cd)
  364. {
  365. struct param_node *pos, *temp;
  366. int entries = 0;
  367. spin_lock(&cd->spinlock);
  368. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  369. entries++;
  370. spin_unlock(&cd->spinlock);
  371. return entries;
  372. }
  373. #endif /* TTDL_DIAGNOSTICS */
  374. /*******************************************************************************
  375. * FUNCTION: request_exclusive
  376. *
  377. * SUMMARY: Request exclusive access to the DUT
  378. *
  379. * RETURN:
  380. * 0 = success
  381. * !0 = failure
  382. *
  383. * PARAMETERS:
  384. * *cd - pointer to core data
  385. * *ownptr - pointer to device
  386. * timeout_ms - Timeout value
  387. ******************************************************************************/
  388. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  389. int timeout_ms)
  390. {
  391. int t = msecs_to_jiffies(timeout_ms);
  392. bool with_timeout = (timeout_ms != 0);
  393. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  394. __func__, timeout_ms);
  395. mutex_lock(&cd->system_lock);
  396. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  397. cd->exclusive_dev = ownptr;
  398. goto exit;
  399. }
  400. cd->exclusive_waits++;
  401. wait:
  402. mutex_unlock(&cd->system_lock);
  403. if (with_timeout) {
  404. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  405. if (IS_TMO(t)) {
  406. pt_debug(cd->dev, DL_ERROR,
  407. "%s: tmo waiting exclusive access\n", __func__);
  408. return -ETIME;
  409. }
  410. } else {
  411. wait_event(cd->wait_q, !cd->exclusive_dev);
  412. }
  413. mutex_lock(&cd->system_lock);
  414. if (cd->exclusive_dev)
  415. goto wait;
  416. cd->exclusive_dev = ownptr;
  417. cd->exclusive_waits--;
  418. exit:
  419. mutex_unlock(&cd->system_lock);
  420. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  421. __func__, ownptr);
  422. return 0;
  423. }
  424. /*******************************************************************************
  425. * FUNCTION: release_exclusive_
  426. *
  427. * SUMMARY: Release exclusive access to the DUT
  428. *
  429. * RETURN:
  430. * 0 = success
  431. *
  432. * PARAMETERS:
  433. * *cd - pointer to core data
  434. * *ownptr - pointer to device
  435. ******************************************************************************/
  436. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  437. {
  438. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  439. __func__);
  440. if (cd->exclusive_dev != ownptr)
  441. return -EINVAL;
  442. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  443. __func__, cd->exclusive_dev);
  444. cd->exclusive_dev = NULL;
  445. wake_up(&cd->wait_q);
  446. return 0;
  447. }
  448. /*******************************************************************************
  449. * FUNCTION: release_exclusive
  450. *
  451. * SUMMARY: Protected wrapper to release_exclusive_()
  452. *
  453. * RETURN:
  454. * 0 = success
  455. * !0 = failure
  456. *
  457. * PARAMETERS:
  458. * *cd - pointer to core data
  459. * *ownptr - pointer to device
  460. ******************************************************************************/
  461. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  462. {
  463. int rc;
  464. mutex_lock(&cd->system_lock);
  465. rc = release_exclusive_(cd, ownptr);
  466. mutex_unlock(&cd->system_lock);
  467. return rc;
  468. }
  469. /*******************************************************************************
  470. * FUNCTION: pt_hid_exec_cmd_
  471. *
  472. * SUMMARY: Send the HID command to the DUT
  473. *
  474. * RETURN:
  475. * 0 = success
  476. * !0 = failure
  477. *
  478. * PARAMETERS:
  479. * *cd - pointer to core data
  480. * *hid_cmd - pointer to the HID command to send
  481. ******************************************************************************/
  482. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  483. struct pt_hid_cmd *hid_cmd)
  484. {
  485. int rc = 0;
  486. u8 *cmd;
  487. u16 cmd_length;
  488. u8 cmd_offset = 0;
  489. cmd_length = 2 /* command register */
  490. + 2 /* command */
  491. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  492. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  493. + hid_cmd->write_length; /* Data length */
  494. cmd = kzalloc(cmd_length, GFP_KERNEL);
  495. if (!cmd)
  496. return -ENOMEM;
  497. /* Set Command register */
  498. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  499. sizeof(cd->hid_desc.command_register));
  500. cmd_offset += sizeof(cd->hid_desc.command_register);
  501. /* Set Command */
  502. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  503. if (hid_cmd->report_id >= 0XF)
  504. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  505. else
  506. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  507. cmd_offset++;
  508. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  509. cmd_offset++;
  510. if (hid_cmd->report_id >= 0XF) {
  511. cmd[cmd_offset] = hid_cmd->report_id;
  512. cmd_offset++;
  513. }
  514. /* Set Data register */
  515. if (hid_cmd->has_data_register) {
  516. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  517. sizeof(cd->hid_desc.data_register));
  518. cmd_offset += sizeof(cd->hid_desc.data_register);
  519. }
  520. /* Set Data */
  521. if (hid_cmd->write_length && hid_cmd->write_buf) {
  522. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  523. hid_cmd->write_length);
  524. cmd_offset += hid_cmd->write_length;
  525. }
  526. pt_debug(cd->dev, DL_INFO,
  527. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  528. __func__, cmd_length, hid_cmd->report_id);
  529. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  530. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  531. hid_cmd->read_buf);
  532. if (rc)
  533. pt_debug(cd->dev, DL_ERROR,
  534. "%s: Fail pt_adap_transfer\n", __func__);
  535. kfree(cmd);
  536. return rc;
  537. }
  538. #ifdef TTDL_DIAGNOSTICS
  539. /*******************************************************************************
  540. * FUNCTION: pt_toggle_err_gpio
  541. *
  542. * SUMMARY: Toggles the pre-defined error GPIO
  543. *
  544. * RETURN: n/a
  545. *
  546. * PARAMETERS:
  547. * *cd - pointer to core data
  548. * type - type of err that occurred
  549. ******************************************************************************/
  550. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  551. {
  552. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  553. __func__, type);
  554. if (cd->err_gpio && type == cd->err_gpio_type) {
  555. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  556. gpio_direction_output(cd->err_gpio,
  557. !gpio_get_value(cd->err_gpio));
  558. }
  559. }
  560. /*******************************************************************************
  561. * FUNCTION: _pt_request_toggle_err_gpio
  562. *
  563. * SUMMARY: Function pointer included in core_cmds to allow other modules
  564. * to request to toggle the err_gpio
  565. *
  566. * RETURN: n/a
  567. *
  568. * PARAMETERS:
  569. * *cd - pointer to core data
  570. * type - type of err that occurred
  571. ******************************************************************************/
  572. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  573. {
  574. struct pt_core_data *cd = dev_get_drvdata(dev);
  575. pt_toggle_err_gpio(cd, type);
  576. }
  577. #endif /* TTDL_DIAGNOSTICS */
  578. /*******************************************************************************
  579. * FUNCTION: pt_hid_exec_cmd_and_wait_
  580. *
  581. * SUMMARY: Send the HID command to the DUT and wait for the response
  582. *
  583. * RETURN:
  584. * 0 = success
  585. * !0 = failure
  586. *
  587. * PARAMETERS:
  588. * *cd - pointer to core data
  589. * *hid_cmd - pointer to the HID command to send
  590. ******************************************************************************/
  591. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  592. struct pt_hid_cmd *hid_cmd)
  593. {
  594. int rc = 0;
  595. int t;
  596. u16 timeout_ms;
  597. int *cmd_state;
  598. if (hid_cmd->reset_cmd)
  599. cmd_state = &cd->hid_reset_cmd_state;
  600. else
  601. cmd_state = &cd->hid_cmd_state;
  602. if (hid_cmd->wait_interrupt) {
  603. mutex_lock(&cd->system_lock);
  604. *cmd_state = 1;
  605. mutex_unlock(&cd->system_lock);
  606. }
  607. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  608. if (rc) {
  609. if (hid_cmd->wait_interrupt)
  610. goto error;
  611. goto exit;
  612. }
  613. if (!hid_cmd->wait_interrupt)
  614. goto exit;
  615. if (hid_cmd->timeout_ms)
  616. timeout_ms = hid_cmd->timeout_ms;
  617. else
  618. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  619. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  620. msecs_to_jiffies(timeout_ms));
  621. if (IS_TMO(t)) {
  622. #ifdef TTDL_DIAGNOSTICS
  623. cd->bus_transmit_error_count++;
  624. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  625. #endif /* TTDL_DIAGNOSTICS */
  626. pt_debug(cd->dev, DL_ERROR,
  627. "%s: HID output cmd execution timed out\n",
  628. __func__);
  629. rc = -ETIME;
  630. goto error;
  631. }
  632. goto exit;
  633. error:
  634. mutex_lock(&cd->system_lock);
  635. *cmd_state = 0;
  636. mutex_unlock(&cd->system_lock);
  637. exit:
  638. return rc;
  639. }
  640. /*******************************************************************************
  641. * FUNCTION: pt_hid_cmd_reset_
  642. *
  643. * SUMMARY: Send the HID RESET command to the DUT
  644. *
  645. * RETURN:
  646. * 0 = success
  647. * !0 = failure
  648. *
  649. * PARAMETERS:
  650. * *cd - pointer to core data
  651. ******************************************************************************/
  652. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  653. {
  654. struct pt_hid_cmd hid_cmd = {
  655. .opcode = HID_CMD_RESET,
  656. .wait_interrupt = 1,
  657. .reset_cmd = 1,
  658. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  659. };
  660. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  661. }
  662. /*******************************************************************************
  663. * FUNCTION: pt_hid_cmd_reset
  664. *
  665. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  666. * access.
  667. *
  668. * RETURN:
  669. * 0 = success
  670. * !0 = failure
  671. *
  672. * PARAMETERS:
  673. * *cd - pointer to core data
  674. ******************************************************************************/
  675. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  676. {
  677. int rc = 0;
  678. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  679. if (rc < 0) {
  680. pt_debug(cd->dev, DL_ERROR,
  681. "%s: fail get exclusive ex=%p own=%p\n",
  682. __func__, cd->exclusive_dev, cd->dev);
  683. return rc;
  684. }
  685. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  686. rc = pt_hid_cmd_reset_(cd);
  687. if (release_exclusive(cd, cd->dev) < 0)
  688. pt_debug(cd->dev, DL_ERROR,
  689. "%s: fail to release exclusive\n", __func__);
  690. return rc;
  691. }
  692. /*******************************************************************************
  693. * FUNCTION: pt_hid_cmd_set_power_
  694. *
  695. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  696. *
  697. * RETURN:
  698. * 0 = success
  699. * !0 = failure
  700. *
  701. * PARAMETERS:
  702. * *cd - pointer to core data
  703. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  704. ******************************************************************************/
  705. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  706. u8 power_state)
  707. {
  708. int rc = 0;
  709. struct pt_hid_cmd hid_cmd = {
  710. .opcode = HID_CMD_SET_POWER,
  711. .wait_interrupt = 1,
  712. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  713. };
  714. hid_cmd.power_state = power_state;
  715. /* The chip won't give response if goes to Deep Standby */
  716. if (power_state == HID_POWER_STANDBY) {
  717. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  718. if (rc)
  719. pt_debug(cd->dev, DL_ERROR,
  720. "%s: Failed to set power to state:%d\n",
  721. __func__, power_state);
  722. else
  723. cd->fw_sys_mode_in_standby_state = true;
  724. return rc;
  725. }
  726. cd->fw_sys_mode_in_standby_state = false;
  727. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  728. if (rc) {
  729. pt_debug(cd->dev, DL_ERROR,
  730. "%s: Failed to set power to state:%d\n",
  731. __func__, power_state);
  732. return rc;
  733. }
  734. /* validate */
  735. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  736. || ((cd->response_buf[3] & 0x3) != power_state)
  737. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  738. rc = -EINVAL;
  739. return rc;
  740. }
  741. /*******************************************************************************
  742. * FUNCTION: pt_hid_cmd_set_power
  743. *
  744. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  745. * exclusive access.
  746. *
  747. * RETURN:
  748. * 0 = success
  749. * !0 = failure
  750. *
  751. * PARAMETERS:
  752. * *cd - pointer to core data
  753. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  754. ******************************************************************************/
  755. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  756. u8 power_state)
  757. {
  758. int rc;
  759. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  760. if (rc < 0) {
  761. pt_debug(cd->dev, DL_ERROR,
  762. "%s: fail get exclusive ex=%p own=%p\n",
  763. __func__, cd->exclusive_dev, cd->dev);
  764. return rc;
  765. }
  766. rc = pt_hid_cmd_set_power_(cd, power_state);
  767. if (release_exclusive(cd, cd->dev) < 0)
  768. pt_debug(cd->dev, DL_ERROR,
  769. "%s: fail to release exclusive\n", __func__);
  770. return rc;
  771. }
  772. static const u16 crc_table[16] = {
  773. 0x0000, 0x1021, 0x2042, 0x3063,
  774. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  775. 0x8108, 0x9129, 0xa14a, 0xb16b,
  776. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  777. };
  778. /*******************************************************************************
  779. * FUNCTION: _pt_compute_crc
  780. *
  781. * SUMMARY: Calculate CRC by CRC table.
  782. *
  783. * RETURN:
  784. * CRC calculation result
  785. *
  786. * PARAMETERS:
  787. * *buf - pointer to the data array to be calculated
  788. * size - size of data array
  789. ******************************************************************************/
  790. static u16 _pt_compute_crc(u8 *buf, u32 size)
  791. {
  792. u16 remainder = 0xFFFF;
  793. u16 xor_mask = 0x0000;
  794. u32 index;
  795. u32 byte_value;
  796. u32 table_index;
  797. u32 crc_bit_width = sizeof(u16) * 8;
  798. /* Divide the message by polynomial, via the table. */
  799. for (index = 0; index < size; index++) {
  800. byte_value = buf[index];
  801. table_index = ((byte_value >> 4) & 0x0F)
  802. ^ (remainder >> (crc_bit_width - 4));
  803. remainder = crc_table[table_index] ^ (remainder << 4);
  804. table_index = (byte_value & 0x0F)
  805. ^ (remainder >> (crc_bit_width - 4));
  806. remainder = crc_table[table_index] ^ (remainder << 4);
  807. }
  808. /* Perform the final remainder CRC. */
  809. return remainder ^ xor_mask;
  810. }
  811. u16 ccitt_Table[] = {
  812. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  813. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  814. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  815. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  816. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  817. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  818. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  819. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  820. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  821. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  822. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  823. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  824. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  825. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  826. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  827. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  828. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  829. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  830. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  831. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  832. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  833. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  834. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  835. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  836. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  837. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  838. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  839. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  840. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  841. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  842. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  843. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  844. };
  845. /*******************************************************************************
  846. * FUNCTION: crc_ccitt_calculate
  847. *
  848. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  849. *
  850. * RETURN:
  851. * CRC calculation result
  852. *
  853. * PARAMETERS:
  854. * *q - pointer to the data array to be calculated
  855. * len - size of data array
  856. ******************************************************************************/
  857. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  858. {
  859. unsigned short crc = 0xffff;
  860. while (len-- > 0)
  861. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  862. return crc;
  863. }
  864. /*******************************************************************************
  865. * FUNCTION: pt_pip2_cmd_calculate_crc
  866. *
  867. * SUMMARY: Calculate the CRC of a command packet
  868. *
  869. * RETURN: void
  870. *
  871. * PARAMETERS:
  872. * *cmd - pointer to command data
  873. * extra_bytes - Extra bytes included in command length
  874. ******************************************************************************/
  875. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  876. u8 extra_bytes)
  877. {
  878. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  879. unsigned short crc;
  880. buf[0] = cmd->len & 0xff;
  881. buf[1] = (cmd->len & 0xff00) >> 8;
  882. buf[2] = cmd->seq;
  883. buf[3] = cmd->id;
  884. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  885. /* Calculate the CRC for the first 4 bytes above and the data payload */
  886. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  887. cmd->crc[0] = (crc & 0xff00) >> 8;
  888. cmd->crc[1] = (crc & 0xff);
  889. }
  890. /*******************************************************************************
  891. * FUNCTION: pt_pip2_get_next_cmd_seq
  892. *
  893. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  894. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  895. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  896. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  897. * increment, just use the forced value.
  898. *
  899. * RETURN: Next command sequence number [0x08-0x0F]
  900. *
  901. * PARAMETERS:
  902. * *cd - pointer to core data
  903. ******************************************************************************/
  904. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  905. {
  906. #ifdef TTDL_DIAGNOSTICS
  907. if (cd->force_pip2_seq <= 0x07) {
  908. cd->pip2_cmd_tag_seq++;
  909. if (cd->pip2_cmd_tag_seq > 0x0F)
  910. cd->pip2_cmd_tag_seq = 0x08;
  911. } else {
  912. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  913. }
  914. #else
  915. cd->pip2_cmd_tag_seq++;
  916. if (cd->pip2_cmd_tag_seq > 0x0F)
  917. cd->pip2_cmd_tag_seq = 0x08;
  918. #endif
  919. return cd->pip2_cmd_tag_seq;
  920. }
  921. /*
  922. * Following macros are to define the response time (the interval between PIP2
  923. * command finishes sending and INT pin falls). The unit is in microsecond.
  924. * It has different time settings between the solution GPIO polling and Bus
  925. * polling due to the considration for system load.
  926. */
  927. #ifdef PT_POLL_RESP_BY_BUS
  928. #define POLL_RETRY_DEFAULT_INTERVAL 50
  929. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  930. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  931. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  932. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  933. #else
  934. #define POLL_RETRY_DEFAULT_INTERVAL 20
  935. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  936. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  937. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  938. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  939. #endif
  940. /*
  941. * id: the command id defined in PIP2
  942. * response_len: the (maximum) length of response.
  943. * response_time_min: minimum response time in microsecond
  944. * response_time_max: maximum response time in microsecond
  945. */
  946. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  947. {.id = PIP2_CMD_ID_PING,
  948. .response_len = 255,
  949. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  950. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  951. {.id = PIP2_CMD_ID_STATUS,
  952. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  953. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  954. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  955. {.id = PIP2_CMD_ID_CTRL,
  956. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  957. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  958. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  959. {.id = PIP2_CMD_ID_CONFIG,
  960. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  961. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  962. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  963. {.id = PIP2_CMD_ID_CLEAR,
  964. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  965. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  966. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  967. {.id = PIP2_CMD_ID_RESET,
  968. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  969. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  970. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  971. {.id = PIP2_CMD_ID_VERSION,
  972. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  973. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  974. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  975. {.id = PIP2_CMD_ID_FILE_OPEN,
  976. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  977. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  978. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  979. {.id = PIP2_CMD_ID_FILE_CLOSE,
  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_FILE_READ,
  984. .response_len = 255,
  985. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  986. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  987. {.id = PIP2_CMD_ID_FILE_WRITE,
  988. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  989. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  990. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  991. {.id = PIP2_CMD_ID_FILE_IOCTL,
  992. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  993. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  994. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  995. {.id = PIP2_CMD_ID_FLASH_INFO,
  996. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  997. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  998. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  999. {.id = PIP2_CMD_ID_EXECUTE,
  1000. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1001. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1002. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1003. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1004. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1005. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1006. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1007. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1008. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1009. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1010. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1011. {.id = PIP2_CMD_ID_READ_GPIO,
  1012. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1013. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1014. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1015. {.id = PIP2_CMD_EXECUTE_SCAN,
  1016. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1017. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1018. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1019. {.id = PIP2_CMD_SET_PARAMETER,
  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_GET_PARAMETER,
  1024. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1025. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1026. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1027. {.id = PIP2_CMD_SET_DDI_REG,
  1028. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1029. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1030. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1031. {.id = PIP2_CMD_GET_DDI_REG,
  1032. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1033. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1034. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1035. {.id = PIP2_CMD_ID_END,
  1036. .response_len = 255,
  1037. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1038. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1039. };
  1040. /*******************************************************************************
  1041. * FUNCTION: pt_pip2_get_cmd_response_len
  1042. *
  1043. * SUMMARY: Gets the expected response length based on the command ID
  1044. *
  1045. * RETURN: Expected response length
  1046. *
  1047. * PARAMETERS:
  1048. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1049. ******************************************************************************/
  1050. static int pt_pip2_get_cmd_response_len(u8 id)
  1051. {
  1052. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1053. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1054. p++;
  1055. if (p->id != PIP2_CMD_ID_END)
  1056. return p->response_len;
  1057. else
  1058. return -EPERM;
  1059. }
  1060. /*******************************************************************************
  1061. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1062. *
  1063. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1064. * finishes sending and INT pin falls) based on the command ID
  1065. *
  1066. * RETURN: Estimated minimum response time in microsecond
  1067. *
  1068. * PARAMETERS:
  1069. * id - Command ID
  1070. ******************************************************************************/
  1071. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1072. {
  1073. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1074. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1075. p++;
  1076. if (p->id != PIP2_CMD_ID_END)
  1077. return p->response_time_min;
  1078. else
  1079. return PIP2_RESP_DEFAULT_TIME_MIN;
  1080. }
  1081. /*******************************************************************************
  1082. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1083. *
  1084. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1085. * finishes sending and INT pin falls) based on the command ID
  1086. *
  1087. * RETURN: Estimated maximum response time in microsecond
  1088. *
  1089. * PARAMETERS:
  1090. * id - Command ID
  1091. ******************************************************************************/
  1092. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1093. {
  1094. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1095. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1096. p++;
  1097. if (p->id != PIP2_CMD_ID_END)
  1098. return p->response_time_max;
  1099. else
  1100. return PIP2_RESP_DEFAULT_TIME_MAX;
  1101. }
  1102. /*******************************************************************************
  1103. * FUNCTION: pt_pip2_validate_response
  1104. *
  1105. * SUMMARY: Validate the response of PIP2 command.
  1106. *
  1107. * RETURN:
  1108. * 0 = success
  1109. * !0 = failure
  1110. *
  1111. * PARAMETERS:
  1112. * *cd - pointer to core data
  1113. * *pip2_cmd - pointer to PIP2 command to send
  1114. * *read_buf - pointer to response buffer
  1115. * actual_read_len - actual read length of the response
  1116. ******************************************************************************/
  1117. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1118. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1119. u16 actual_read_len)
  1120. {
  1121. int rc = 0;
  1122. u8 response_seq = 0;
  1123. u8 reserved_bits = 0;
  1124. u8 cmd_id = 0;
  1125. u8 response_bit = 0;
  1126. unsigned short calc_crc = 0;
  1127. unsigned short resp_crc = 0;
  1128. /* Verify the length of response buffer */
  1129. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1130. pt_debug(cd->dev, DL_ERROR,
  1131. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1132. __func__, pip2_cmd->id, actual_read_len);
  1133. rc = -EINVAL;
  1134. goto exit;
  1135. }
  1136. /* Verify the CRC */
  1137. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1138. resp_crc = read_buf[actual_read_len - 2] << 8;
  1139. resp_crc |= read_buf[actual_read_len - 1];
  1140. if (resp_crc != calc_crc) {
  1141. pt_debug(cd->dev, DL_ERROR,
  1142. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1143. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1144. #ifdef TTDL_DIAGNOSTICS
  1145. cd->pip2_crc_error_count++;
  1146. #endif /* TTDL_DIAGNOSTICS */
  1147. rc = -EINVAL;
  1148. goto exit;
  1149. }
  1150. /* Verify the response bit is set */
  1151. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1152. if (!response_bit) {
  1153. pt_debug(cd->dev, DL_ERROR,
  1154. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1155. __func__, pip2_cmd->id, response_bit);
  1156. rc = -EINVAL;
  1157. goto exit;
  1158. }
  1159. /* Verify the command ID matches from command to response */
  1160. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1161. if (cmd_id != pip2_cmd->id) {
  1162. pt_debug(cd->dev, DL_ERROR,
  1163. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1164. __func__, pip2_cmd->id, cmd_id);
  1165. rc = -EINVAL;
  1166. goto exit;
  1167. }
  1168. /* Verify the SEQ number matches from command to response */
  1169. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1170. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1171. pt_debug(cd->dev, DL_ERROR,
  1172. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1173. __func__, pip2_cmd->id,
  1174. pip2_cmd->seq, response_seq);
  1175. rc = -EINVAL;
  1176. goto exit;
  1177. }
  1178. /* Verify the reserved bits are 0 */
  1179. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1180. if (reserved_bits)
  1181. pt_debug(cd->dev, DL_WARN,
  1182. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1183. __func__, pip2_cmd->id, reserved_bits);
  1184. exit:
  1185. if (rc)
  1186. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1187. "PIP RSP:");
  1188. return rc;
  1189. }
  1190. /*******************************************************************************
  1191. * FUNCTION: pt_hid_output_validate_bl_response
  1192. *
  1193. * SUMMARY: Validate the response of bootloader command.
  1194. *
  1195. * RETURN:
  1196. * 0 = success
  1197. * !0 = failure
  1198. *
  1199. * PARAMETERS:
  1200. * *cd - pointer to core data
  1201. * *hid_output - pointer to hid output data structure
  1202. ******************************************************************************/
  1203. static int pt_hid_output_validate_bl_response(
  1204. struct pt_core_data *cd,
  1205. struct pt_hid_output *hid_output)
  1206. {
  1207. u16 size;
  1208. u16 crc;
  1209. u8 status;
  1210. size = get_unaligned_le16(&cd->response_buf[0]);
  1211. if (hid_output->reset_expected && !size)
  1212. return 0;
  1213. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1214. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1215. pt_debug(cd->dev, DL_ERROR,
  1216. "%s: BL output response, wrong report_id\n", __func__);
  1217. return -EPROTO;
  1218. }
  1219. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1220. pt_debug(cd->dev, DL_ERROR,
  1221. "%s: BL output response, wrong SOP\n", __func__);
  1222. return -EPROTO;
  1223. }
  1224. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1225. pt_debug(cd->dev, DL_ERROR,
  1226. "%s: BL output response, wrong EOP\n", __func__);
  1227. return -EPROTO;
  1228. }
  1229. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1230. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1231. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1232. pt_debug(cd->dev, DL_ERROR,
  1233. "%s: BL output response, wrong CRC 0x%X\n",
  1234. __func__, crc);
  1235. return -EPROTO;
  1236. }
  1237. status = cd->response_buf[5];
  1238. if (status) {
  1239. pt_debug(cd->dev, DL_ERROR,
  1240. "%s: BL output response, ERROR:%d\n",
  1241. __func__, status);
  1242. return -EPROTO;
  1243. }
  1244. return 0;
  1245. }
  1246. /*******************************************************************************
  1247. * FUNCTION: pt_hid_output_validate_app_response
  1248. *
  1249. * SUMMARY: Validate the response of application command.
  1250. *
  1251. * RETURN:
  1252. * 0 = success
  1253. * !0 = failure
  1254. *
  1255. * PARAMETERS:
  1256. * *cd - pointer to core data
  1257. * *hid_output - pointer to hid output data structure
  1258. ******************************************************************************/
  1259. static int pt_hid_output_validate_app_response(
  1260. struct pt_core_data *cd,
  1261. struct pt_hid_output *hid_output)
  1262. {
  1263. int command_code;
  1264. u16 size;
  1265. size = get_unaligned_le16(&cd->response_buf[0]);
  1266. if (hid_output->reset_expected && !size)
  1267. return 0;
  1268. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1269. != PT_PIP_NON_HID_RESPONSE_ID) {
  1270. pt_debug(cd->dev, DL_ERROR,
  1271. "%s: APP output response, wrong report_id\n", __func__);
  1272. return -EPROTO;
  1273. }
  1274. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1275. & PIP1_RESP_COMMAND_ID_MASK;
  1276. if (command_code != hid_output->command_code) {
  1277. pt_debug(cd->dev, DL_ERROR,
  1278. "%s: APP output response, wrong command_code:%X\n",
  1279. __func__, command_code);
  1280. return -EPROTO;
  1281. }
  1282. return 0;
  1283. }
  1284. /*******************************************************************************
  1285. * FUNCTION: pt_check_set_parameter
  1286. *
  1287. * SUMMARY: Check command input and response for Set Parameter command.And
  1288. * store the parameter to the list for resume work if pass the check.
  1289. *
  1290. * PARAMETERS:
  1291. * *cd - pointer to core data
  1292. * *hid_output - pointer to hid output data structure
  1293. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1294. ******************************************************************************/
  1295. static void pt_check_set_parameter(struct pt_core_data *cd,
  1296. struct pt_hid_output *hid_output, bool raw)
  1297. {
  1298. u8 *param_buf;
  1299. u32 param_value = 0;
  1300. u8 param_size;
  1301. u8 param_id;
  1302. int i = 0;
  1303. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1304. return;
  1305. /* Check command input for Set Parameter command */
  1306. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1307. && !memcmp(&hid_output->write_buf[0],
  1308. &cd->hid_desc.output_register,
  1309. sizeof(cd->hid_desc.output_register))
  1310. && hid_output->write_buf[4] ==
  1311. PT_PIP_NON_HID_COMMAND_ID
  1312. && hid_output->write_buf[6] ==
  1313. PIP1_CMD_ID_SET_PARAM)
  1314. param_buf = &hid_output->write_buf[7];
  1315. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1316. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1317. && hid_output->write_length >= 3
  1318. && hid_output->write_length <= 6)
  1319. param_buf = &hid_output->write_buf[0];
  1320. else
  1321. return;
  1322. /* Get parameter ID, size and value */
  1323. param_id = param_buf[0];
  1324. param_size = param_buf[1];
  1325. if (param_size > 4) {
  1326. pt_debug(cd->dev, DL_ERROR,
  1327. "%s: Invalid parameter size\n", __func__);
  1328. return;
  1329. }
  1330. param_buf = &param_buf[2];
  1331. while (i < param_size)
  1332. param_value += *(param_buf++) << (8 * i++);
  1333. /* Check command response for Set Parameter command */
  1334. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1335. || (cd->response_buf[4] &
  1336. PIP1_RESP_COMMAND_ID_MASK) !=
  1337. PIP1_CMD_ID_SET_PARAM
  1338. || cd->response_buf[5] != param_id
  1339. || cd->response_buf[6] != param_size) {
  1340. pt_debug(cd->dev, DL_ERROR,
  1341. "%s: Set Parameter command not successful\n",
  1342. __func__);
  1343. return;
  1344. }
  1345. pt_add_parameter(cd, param_id, param_value, param_size);
  1346. }
  1347. /*******************************************************************************
  1348. * FUNCTION: pt_check_command
  1349. *
  1350. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1351. * called here to check output command and store parameter to the list.
  1352. *
  1353. * PARAMETERS:
  1354. * *cd - pointer to core data
  1355. * *hid_output - pointer to hid output data structure
  1356. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1357. ******************************************************************************/
  1358. static void pt_check_command(struct pt_core_data *cd,
  1359. struct pt_hid_output *hid_output, bool raw)
  1360. {
  1361. pt_check_set_parameter(cd, hid_output, raw);
  1362. }
  1363. /*******************************************************************************
  1364. * FUNCTION: pt_hid_output_validate_response
  1365. *
  1366. * SUMMARY: Validate the response of application or bootloader command.
  1367. *
  1368. * RETURN:
  1369. * 0 = success
  1370. * !0 = failure
  1371. *
  1372. * PARAMETERS:
  1373. * *cd - pointer to core data
  1374. * *hid_output - pointer to hid output data structure
  1375. ******************************************************************************/
  1376. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1377. struct pt_hid_output *hid_output)
  1378. {
  1379. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1380. return pt_hid_output_validate_bl_response(cd, hid_output);
  1381. return pt_hid_output_validate_app_response(cd, hid_output);
  1382. }
  1383. /*******************************************************************************
  1384. * FUNCTION: pt_hid_send_output_user_
  1385. *
  1386. * SUMMARY: Blindly send user data to the DUT.
  1387. *
  1388. * RETURN:
  1389. * 0 = success
  1390. * !0 = failure
  1391. *
  1392. * PARAMETERS:
  1393. * *cd - pointer to core data
  1394. * *hid_output - pointer to the command to send
  1395. ******************************************************************************/
  1396. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1397. struct pt_hid_output *hid_output)
  1398. {
  1399. int rc = 0;
  1400. int cmd;
  1401. if (!hid_output->length || !hid_output->write_buf)
  1402. return -EINVAL;
  1403. if (cd->pip2_prot_active) {
  1404. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1405. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1406. } else
  1407. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1408. pt_debug(cd->dev, DL_INFO,
  1409. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1410. __func__, hid_output->length, cmd);
  1411. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1412. hid_output->length, ">>> User CMD");
  1413. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1414. hid_output->write_buf, NULL);
  1415. if (rc)
  1416. pt_debug(cd->dev, DL_ERROR,
  1417. "%s: Fail pt_adap_transfer\n", __func__);
  1418. return rc;
  1419. }
  1420. /*******************************************************************************
  1421. * FUNCTION: pt_hid_send_output_user_and_wait_
  1422. *
  1423. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1424. *
  1425. * RETURN:
  1426. * 0 = success
  1427. * !0 = failure
  1428. *
  1429. * PARAMETERS:
  1430. * *cd - pointer to core data
  1431. * *hid_output - pointer to the command to send
  1432. ******************************************************************************/
  1433. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1434. struct pt_hid_output *hid_output)
  1435. {
  1436. int rc = 0;
  1437. int t;
  1438. mutex_lock(&cd->system_lock);
  1439. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1440. mutex_unlock(&cd->system_lock);
  1441. rc = pt_hid_send_output_user_(cd, hid_output);
  1442. if (rc)
  1443. goto error;
  1444. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1445. msecs_to_jiffies(cd->pip_cmd_timeout));
  1446. if (IS_TMO(t)) {
  1447. #ifdef TTDL_DIAGNOSTICS
  1448. cd->bus_transmit_error_count++;
  1449. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1450. #endif /* TTDL_DIAGNOSTICS */
  1451. pt_debug(cd->dev, DL_ERROR,
  1452. "%s: HID output cmd execution timed out\n",
  1453. __func__);
  1454. rc = -ETIME;
  1455. goto error;
  1456. }
  1457. pt_check_command(cd, hid_output, true);
  1458. goto exit;
  1459. error:
  1460. mutex_lock(&cd->system_lock);
  1461. cd->hid_cmd_state = 0;
  1462. mutex_unlock(&cd->system_lock);
  1463. exit:
  1464. return rc;
  1465. }
  1466. /*******************************************************************************
  1467. * FUNCTION: pt_check_irq_asserted
  1468. *
  1469. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1470. * the FW can hold the INT line low ~150us after the read is complete.
  1471. * NOTE: if irq_stat is not defined this function will return false
  1472. *
  1473. * RETURN:
  1474. * true = IRQ asserted
  1475. * false = IRQ not asserted
  1476. *
  1477. * PARAMETERS:
  1478. * *cd - pointer to core data
  1479. ******************************************************************************/
  1480. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1481. {
  1482. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1483. /*
  1484. * Workaround for FW defect, CDT165308
  1485. * bl_launch app creates a glitch in IRQ line
  1486. */
  1487. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1488. && cd->cpdata->irq_stat) {
  1489. /*
  1490. * in X1S panel and GC1546 panel, the width for the INT
  1491. * glitch is about 4us,the normal INT width of response
  1492. * will last more than 200us, so use 10us delay
  1493. * for distinguish the glitch the normal INT is enough.
  1494. */
  1495. udelay(10);
  1496. }
  1497. #endif
  1498. if (cd->cpdata->irq_stat) {
  1499. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1500. == PT_IRQ_ASSERTED_VALUE) {
  1501. /* Debounce to allow FW to release INT */
  1502. usleep_range(100, 200);
  1503. }
  1504. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1505. == PT_IRQ_ASSERTED_VALUE)
  1506. return true;
  1507. else
  1508. return false;
  1509. }
  1510. return true;
  1511. }
  1512. /*******************************************************************************
  1513. * FUNCTION: pt_flush_bus
  1514. *
  1515. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1516. * Used if IRQ is found to be stuck low
  1517. *
  1518. * RETURN: Length of bytes read from bus
  1519. *
  1520. * PARAMETERS:
  1521. * *cd - pointer to core data
  1522. * flush_type - type of flush
  1523. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1524. * - PT_FLUSH_BUS_FULL_256_READ
  1525. * *read_buf - pointer to store read data
  1526. ******************************************************************************/
  1527. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1528. u8 flush_type, u8 *read_buf)
  1529. {
  1530. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1531. u16 pip_len;
  1532. int bytes_read;
  1533. int rc = 0;
  1534. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1535. rc = pt_adap_read_default(cd, buf, 2);
  1536. if (rc) {
  1537. bytes_read = 0;
  1538. goto exit;
  1539. }
  1540. pip_len = get_unaligned_le16(&buf[0]);
  1541. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1542. #ifdef TTDL_DIAGNOSTICS
  1543. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1544. #endif
  1545. bytes_read = 2;
  1546. pt_debug(cd->dev, DL_INFO,
  1547. "%s: Empty buf detected - len=0x%04X\n",
  1548. __func__, pip_len);
  1549. } else if (pip_len == 0) {
  1550. bytes_read = 0;
  1551. pt_debug(cd->dev, DL_INFO,
  1552. "%s: Sentinel detected\n", __func__);
  1553. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1554. pt_debug(cd->dev, DL_ERROR,
  1555. "%s: Illegal len=0x%04x, force %d byte read\n",
  1556. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1557. rc = pt_adap_read_default(cd, buf,
  1558. PT_MAX_PIP2_MSG_SIZE);
  1559. if (!rc)
  1560. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1561. else
  1562. bytes_read = 0;
  1563. } else {
  1564. pt_debug(cd->dev, DL_INFO,
  1565. "%s: Flush read of %d bytes...\n",
  1566. __func__, pip_len);
  1567. rc = pt_adap_read_default(cd, buf, pip_len);
  1568. if (!rc)
  1569. bytes_read = pip_len;
  1570. else
  1571. bytes_read = 0;
  1572. }
  1573. } else {
  1574. pt_debug(cd->dev, DL_INFO,
  1575. "%s: Forced flush of max %d bytes...\n",
  1576. __func__, PT_MAX_PIP2_MSG_SIZE);
  1577. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1578. if (!rc)
  1579. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1580. else
  1581. bytes_read = 0;
  1582. }
  1583. if (read_buf && (bytes_read > 3))
  1584. memcpy(read_buf, buf, bytes_read);
  1585. exit:
  1586. return bytes_read;
  1587. }
  1588. /*******************************************************************************
  1589. * FUNCTION: pt_flush_bus_if_irq_asserted
  1590. *
  1591. * SUMMARY: This function will flush the active bus if the INT is found to be
  1592. * asserted.
  1593. *
  1594. * RETURN: bytes cleared from bus
  1595. *
  1596. * PARAMETERS:
  1597. * *cd - pointer the core data structure
  1598. * flush_type - type of flush
  1599. * - PT_FLUSH_BUS_BASED_ON_LEN
  1600. * - PT_FLUSH_BUS_FULL_256_READ
  1601. ******************************************************************************/
  1602. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1603. {
  1604. int count = 0;
  1605. int bytes_read = 0;
  1606. while (pt_check_irq_asserted(cd) && count < 5) {
  1607. count++;
  1608. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1609. if (bytes_read) {
  1610. pt_debug(cd->dev, DL_WARN,
  1611. "%s: Cleared %d bytes off bus\n",
  1612. __func__, bytes_read);
  1613. }
  1614. }
  1615. if (pt_check_irq_asserted(cd)) {
  1616. pt_debug(cd->dev, DL_ERROR,
  1617. "%s: IRQ still asserted, %d bytes read\n",
  1618. __func__, bytes_read);
  1619. } else {
  1620. pt_debug(cd->dev, DL_INFO,
  1621. "%s: IRQ cleared, %d bytes read\n",
  1622. __func__, bytes_read);
  1623. }
  1624. return bytes_read;
  1625. }
  1626. /*******************************************************************************
  1627. * FUNCTION: pt_hid_send_output_
  1628. *
  1629. * SUMMARY: Send a touch application command to the DUT
  1630. *
  1631. * RETURN:
  1632. * 0 = success
  1633. * !0 = failure
  1634. *
  1635. * PARAMETERS:
  1636. * *cd - pointer to core data
  1637. * *hid_output - pointer to the command to send
  1638. ******************************************************************************/
  1639. static int pt_hid_send_output_(struct pt_core_data *cd,
  1640. struct pt_hid_output *hid_output)
  1641. {
  1642. int rc = 0;
  1643. u8 *cmd;
  1644. u16 length;
  1645. u16 crc;
  1646. u8 report_id;
  1647. u8 cmd_offset = 0;
  1648. u8 cmd_allocated = 0;
  1649. switch (hid_output->cmd_type) {
  1650. case PIP1_CMD_TYPE_FW:
  1651. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1652. length = 5;
  1653. break;
  1654. case PIP1_CMD_TYPE_BL:
  1655. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1656. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1657. break;
  1658. default:
  1659. return -EINVAL;
  1660. }
  1661. length += hid_output->write_length;
  1662. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1663. cmd = kzalloc(length + 2, GFP_KERNEL);
  1664. if (!cmd)
  1665. return -ENOMEM;
  1666. cmd_allocated = 1;
  1667. } else {
  1668. cmd = cd->cmd_buf;
  1669. }
  1670. /* Set Output register */
  1671. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1672. sizeof(cd->hid_desc.output_register));
  1673. cmd_offset += sizeof(cd->hid_desc.output_register);
  1674. cmd[cmd_offset++] = LOW_BYTE(length);
  1675. cmd[cmd_offset++] = HI_BYTE(length);
  1676. cmd[cmd_offset++] = report_id;
  1677. cmd[cmd_offset++] = 0x0; /* reserved */
  1678. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1679. cmd[cmd_offset++] = PIP1_BL_SOP;
  1680. cmd[cmd_offset++] = hid_output->command_code;
  1681. /* Set Data Length for bootloader */
  1682. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1683. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1684. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1685. }
  1686. /* Set Data */
  1687. if (hid_output->write_length && hid_output->write_buf) {
  1688. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1689. hid_output->write_length);
  1690. cmd_offset += hid_output->write_length;
  1691. }
  1692. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1693. crc = _pt_compute_crc(&cmd[6],
  1694. hid_output->write_length + 4);
  1695. cmd[cmd_offset++] = LOW_BYTE(crc);
  1696. cmd[cmd_offset++] = HI_BYTE(crc);
  1697. cmd[cmd_offset++] = PIP1_BL_EOP;
  1698. }
  1699. pt_debug(cd->dev, DL_INFO,
  1700. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1701. __func__, length + 2, hid_output->command_code);
  1702. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1703. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1704. if (rc)
  1705. pt_debug(cd->dev, DL_ERROR,
  1706. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1707. if (cmd_allocated)
  1708. kfree(cmd);
  1709. return rc;
  1710. }
  1711. /*******************************************************************************
  1712. * FUNCTION: pt_pip1_send_output_and_wait_
  1713. *
  1714. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1715. *
  1716. * RETURN:
  1717. * 0 = success
  1718. * !0 = failure
  1719. *
  1720. * PARAMETERS:
  1721. * *cd - pointer to core data
  1722. * *hid_output - pointer to the command to send
  1723. ******************************************************************************/
  1724. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1725. struct pt_hid_output *hid_output)
  1726. {
  1727. int rc = 0;
  1728. int t;
  1729. u16 timeout_ms;
  1730. mutex_lock(&cd->system_lock);
  1731. cd->hid_cmd_state = hid_output->command_code + 1;
  1732. mutex_unlock(&cd->system_lock);
  1733. if (hid_output->timeout_ms)
  1734. timeout_ms = hid_output->timeout_ms;
  1735. else
  1736. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1737. rc = pt_hid_send_output_(cd, hid_output);
  1738. if (rc)
  1739. goto error;
  1740. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1741. msecs_to_jiffies(timeout_ms));
  1742. if (IS_TMO(t)) {
  1743. #ifdef TTDL_DIAGNOSTICS
  1744. cd->bus_transmit_error_count++;
  1745. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1746. #endif /* TTDL_DIAGNOSTICS */
  1747. pt_debug(cd->dev, DL_ERROR,
  1748. "%s: HID output cmd execution timed out (%dms)\n",
  1749. __func__, timeout_ms);
  1750. rc = -ETIME;
  1751. goto error;
  1752. }
  1753. if (!hid_output->novalidate)
  1754. rc = pt_hid_output_validate_response(cd, hid_output);
  1755. pt_check_command(cd, hid_output, false);
  1756. goto exit;
  1757. error:
  1758. mutex_lock(&cd->system_lock);
  1759. cd->hid_cmd_state = 0;
  1760. mutex_unlock(&cd->system_lock);
  1761. exit:
  1762. return rc;
  1763. }
  1764. /*******************************************************************************
  1765. * FUNCTION: pt_hid_output_user_cmd_
  1766. *
  1767. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1768. * to the DUT waiting for the response and loading it into the read buffer
  1769. *
  1770. * RETURN:
  1771. * 0 = success
  1772. * !0 = failure
  1773. *
  1774. * PARAMETERS:
  1775. * *cd - pointer to core data
  1776. * read_len - expected read length of the response
  1777. * *read_buf - pointer to where the response will be loaded
  1778. * write_len - length of the write buffer
  1779. * *write_buf - pointer to the write buffer
  1780. * *actual_read_len - pointer to the actual amount of data read back
  1781. ******************************************************************************/
  1782. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1783. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1784. u16 *actual_read_len)
  1785. {
  1786. int rc = 0;
  1787. u16 size;
  1788. struct pt_hid_output hid_output = {
  1789. .length = write_len,
  1790. .write_buf = write_buf,
  1791. };
  1792. #ifdef TTHE_TUNER_SUPPORT
  1793. if (!cd->pip2_send_user_cmd) {
  1794. int command_code = 0;
  1795. int len;
  1796. /* Print up to cmd ID */
  1797. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1798. if (write_len < len)
  1799. len = write_len;
  1800. else
  1801. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1802. & PIP1_CMD_COMMAND_ID_MASK;
  1803. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1804. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1805. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1806. tthe_print(cd, write_buf, len, "CMD=");
  1807. }
  1808. #endif
  1809. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1810. if (rc)
  1811. return rc;
  1812. /* Get the response size from the first 2 bytes in the response */
  1813. size = get_unaligned_le16(&cd->response_buf[0]);
  1814. /* Ensure size is not greater than max buffer size */
  1815. if (size > PT_MAX_PIP2_MSG_SIZE)
  1816. size = PT_MAX_PIP2_MSG_SIZE;
  1817. /* Minimum size to read is the 2 byte len field */
  1818. if (size == 0)
  1819. size = 2;
  1820. if (size > read_len) {
  1821. pt_debug(cd->dev, DL_ERROR,
  1822. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1823. __func__, size, read_len);
  1824. *actual_read_len = 0;
  1825. return -EIO;
  1826. }
  1827. memcpy(read_buf, cd->response_buf, size);
  1828. *actual_read_len = size;
  1829. return 0;
  1830. }
  1831. /*******************************************************************************
  1832. * FUNCTION: pt_hid_output_user_cmd
  1833. *
  1834. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1835. * the DUT.
  1836. *
  1837. * RETURN:
  1838. * 0 = success
  1839. * !0 = failure
  1840. *
  1841. * PARAMETERS:
  1842. * *cd - pointer to core data
  1843. * read_len - length of data to read
  1844. * *read_buf - pointer to store read data
  1845. * write_len - length of data to write
  1846. * *write_buf - pointer to buffer to write
  1847. * *actual_read_len - pointer to store data length actually read
  1848. ******************************************************************************/
  1849. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1850. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1851. u16 *actual_read_len)
  1852. {
  1853. int rc;
  1854. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1855. if (rc < 0) {
  1856. pt_debug(cd->dev, DL_ERROR,
  1857. "%s: fail get exclusive ex=%p own=%p\n",
  1858. __func__, cd->exclusive_dev, cd->dev);
  1859. return rc;
  1860. }
  1861. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1862. write_len, write_buf, actual_read_len);
  1863. if (release_exclusive(cd, cd->dev) < 0)
  1864. pt_debug(cd->dev, DL_ERROR,
  1865. "%s: fail to release exclusive\n", __func__);
  1866. return rc;
  1867. }
  1868. /*******************************************************************************
  1869. * FUNCTION: _pt_request_pip2_send_cmd
  1870. *
  1871. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1872. * interrupt and reads response data to read_buf
  1873. *
  1874. * RETURN:
  1875. * 0 = success
  1876. * !0 = failure
  1877. *
  1878. * PARAMETERS:
  1879. * *dev - pointer to device structure
  1880. * protect - flag to run in protected mode
  1881. * id - ID of PIP command
  1882. * *data - pointer to PIP data payload
  1883. * report_body_len - report length
  1884. * *read_buf - pointer to response buffer
  1885. * *actual_read_len - pointer to response buffer length
  1886. ******************************************************************************/
  1887. static int _pt_request_pip2_send_cmd(struct device *dev,
  1888. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1889. u16 *actual_read_len)
  1890. {
  1891. struct pt_core_data *cd = dev_get_drvdata(dev);
  1892. struct pip2_cmd_structure pip2_cmd;
  1893. int rc = 0;
  1894. int i = 0;
  1895. int j = 0;
  1896. u16 write_len;
  1897. u8 *write_buf = NULL;
  1898. u16 read_len;
  1899. u8 extra_bytes;
  1900. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1901. /* Hard coded register for PIP2.x */
  1902. pip2_cmd.reg[0] = 0x01;
  1903. pip2_cmd.reg[1] = 0x01;
  1904. /*
  1905. * For PIP2.1+ the length field value includes itself:
  1906. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1907. *
  1908. * The overall write length must include only the register:
  1909. * ADD 2: 2 (Register)
  1910. */
  1911. extra_bytes = 6;
  1912. write_len = 2;
  1913. /* PIP2 the CMD ID is a 7bit field */
  1914. if (id > PIP2_CMD_ID_END) {
  1915. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1916. __func__, id);
  1917. rc = -EINVAL;
  1918. goto exit;
  1919. }
  1920. pip2_cmd.len = report_body_len + extra_bytes;
  1921. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1922. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1923. pip2_cmd.data = data;
  1924. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1925. /* Add the command length to the extra bytes based on PIP version */
  1926. write_len += pip2_cmd.len;
  1927. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1928. __func__, pip2_cmd.len, write_len);
  1929. write_buf = kzalloc(write_len, GFP_KERNEL);
  1930. if (write_buf == NULL) {
  1931. rc = -ENOMEM;
  1932. goto exit;
  1933. }
  1934. write_buf[i++] = pip2_cmd.reg[0];
  1935. write_buf[i++] = pip2_cmd.reg[1];
  1936. write_buf[i++] = pip2_cmd.len & 0xff;
  1937. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1938. write_buf[i++] = pip2_cmd.seq;
  1939. write_buf[i++] = pip2_cmd.id;
  1940. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1941. write_buf[j] = pip2_cmd.data[j-i];
  1942. write_buf[j++] = pip2_cmd.crc[0];
  1943. write_buf[j++] = pip2_cmd.crc[1];
  1944. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1945. if (read_len < 0)
  1946. read_len = 255;
  1947. pt_debug(dev, DL_INFO,
  1948. "%s cmd_id[0x%02X] expected response length:%d ",
  1949. __func__, pip2_cmd.id, read_len);
  1950. /*
  1951. * All PIP2 commands come through this function.
  1952. * Set flag for PIP2.x interface to allow response parsing to know
  1953. * how to decode the protocol header.
  1954. */
  1955. mutex_lock(&cd->system_lock);
  1956. cd->pip2_prot_active = true;
  1957. cd->pip2_send_user_cmd = true;
  1958. mutex_unlock(&cd->system_lock);
  1959. if (protect == PT_CORE_CMD_PROTECTED)
  1960. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1961. write_len, write_buf, actual_read_len);
  1962. else {
  1963. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1964. write_len, write_buf, actual_read_len);
  1965. }
  1966. if (rc) {
  1967. pt_debug(dev, DL_ERROR,
  1968. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1969. __func__, rc);
  1970. goto exit;
  1971. }
  1972. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1973. *actual_read_len);
  1974. exit:
  1975. mutex_lock(&cd->system_lock);
  1976. cd->pip2_prot_active = false;
  1977. cd->pip2_send_user_cmd = false;
  1978. mutex_unlock(&cd->system_lock);
  1979. kfree(write_buf);
  1980. return rc;
  1981. }
  1982. /*******************************************************************************
  1983. * FUNCTION: _pt_pip2_send_cmd_no_int
  1984. *
  1985. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1986. * reads response data to read_buf if response is available.
  1987. *
  1988. * NOTE:
  1989. * Interrupt MUST be disabled before to call this function.
  1990. *
  1991. * RETURN:
  1992. * 0 = success
  1993. * !0 = failure
  1994. *
  1995. * PARAMETERS:
  1996. * *dev - pointer to device structure
  1997. * protect - flag to run in protected mode
  1998. * id - ID of PIP command
  1999. * *data - pointer to PIP data payload
  2000. * report_body_len - report length
  2001. * *read_buf - pointer to response buffer
  2002. * *actual_read_len - pointer to response buffer length
  2003. ******************************************************************************/
  2004. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2005. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2006. u16 *actual_read_len)
  2007. {
  2008. int max_retry = 0;
  2009. int retry = 0;
  2010. int rc = 0;
  2011. int i = 0;
  2012. int j = 0;
  2013. u16 write_len;
  2014. u8 *write_buf = NULL;
  2015. u16 read_len;
  2016. u16 size = 0;
  2017. u8 response_seq = 0;
  2018. u8 extra_bytes;
  2019. u32 retry_interval = 0;
  2020. u32 retry_total_time = 0;
  2021. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2022. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2023. struct pt_core_data *cd = dev_get_drvdata(dev);
  2024. struct pip2_cmd_structure pip2_cmd;
  2025. if (protect == PT_CORE_CMD_PROTECTED) {
  2026. rc = request_exclusive(cd,
  2027. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2028. if (rc < 0) {
  2029. pt_debug(cd->dev, DL_ERROR,
  2030. "%s: fail get exclusive ex=%p own=%p\n",
  2031. __func__, cd->exclusive_dev, cd->dev);
  2032. return rc;
  2033. }
  2034. }
  2035. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2036. /* Hard coded register for PIP2.x */
  2037. pip2_cmd.reg[0] = 0x01;
  2038. pip2_cmd.reg[1] = 0x01;
  2039. /*
  2040. * For PIP2.1+ the length field value includes itself:
  2041. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2042. *
  2043. * The overall write length must include only the register:
  2044. * ADD 2: 2 (Register)
  2045. */
  2046. extra_bytes = 6;
  2047. write_len = 2;
  2048. pip2_cmd.len = report_body_len + extra_bytes;
  2049. pip2_cmd.id = id;
  2050. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2051. pip2_cmd.data = data;
  2052. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2053. /* Add the command length to the extra bytes based on PIP version */
  2054. write_len += pip2_cmd.len;
  2055. write_buf = kzalloc(write_len, GFP_KERNEL);
  2056. if (write_buf == NULL) {
  2057. rc = -ENOMEM;
  2058. goto exit;
  2059. }
  2060. write_buf[i++] = pip2_cmd.reg[0];
  2061. write_buf[i++] = pip2_cmd.reg[1];
  2062. write_buf[i++] = pip2_cmd.len & 0xff;
  2063. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2064. write_buf[i++] = pip2_cmd.seq;
  2065. write_buf[i++] = pip2_cmd.id;
  2066. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2067. write_buf[j] = pip2_cmd.data[j-i];
  2068. write_buf[j++] = pip2_cmd.crc[0];
  2069. write_buf[j++] = pip2_cmd.crc[1];
  2070. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2071. if (read_len < 0)
  2072. read_len = 255;
  2073. pt_debug(dev, DL_INFO,
  2074. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2075. __func__, pip2_cmd.id, read_len);
  2076. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2077. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2078. if (rc) {
  2079. pt_debug(dev, DL_ERROR,
  2080. "%s: SPI write Error = %d\n",
  2081. __func__, rc);
  2082. goto exit;
  2083. }
  2084. #ifdef PT_POLL_RESP_BY_BUS
  2085. /*
  2086. * Frequent bus read can increase system load obviously. The expected
  2087. * first bus read should be valid and timely. The tollerance for
  2088. * usleep_range should be limited. The minimum response delay (between
  2089. * command finishes sending and INT pin falls) is less than 50
  2090. * microseconds. So the 10 microseconds should be maximum tollerance
  2091. * with the consideration that the unit to calculate the response delay
  2092. * is 10 microseconds and more precise is not necessary. Every
  2093. * additional 10 microseconds only contribute less than 3 milliseconds
  2094. * for whole BL.
  2095. */
  2096. usleep_range(resp_time_min, resp_time_min+10);
  2097. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2098. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2099. rc = pt_adap_read_default(cd, read_buf, read_len);
  2100. if (rc) {
  2101. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2102. __func__, rc);
  2103. break;
  2104. }
  2105. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2106. size = get_unaligned_le16(&read_buf[0]);
  2107. if ((size <= read_len) &&
  2108. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2109. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2110. break;
  2111. }
  2112. /*
  2113. * To reduce the bus and system load, increase the sleep
  2114. * step gradually:
  2115. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2116. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2117. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2118. */
  2119. retry++;
  2120. if (retry < 20) {
  2121. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2122. usleep_range(retry_interval,
  2123. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2124. } else if (retry < 40) {
  2125. retry_interval += 1000;
  2126. usleep_range(retry_interval, retry_interval + 1000);
  2127. } else {
  2128. retry_interval += 50000;
  2129. msleep(retry_interval/1000);
  2130. }
  2131. retry_total_time += retry_interval;
  2132. }
  2133. #else
  2134. /*
  2135. * Frequent GPIO read will not increase CPU/system load heavily if the
  2136. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2137. * fixed interval: 20 us.
  2138. */
  2139. usleep_range(resp_time_min, resp_time_min+10);
  2140. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2141. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2142. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2143. rc = pt_adap_read_default(cd, read_buf, read_len);
  2144. size = get_unaligned_le16(&read_buf[0]);
  2145. if (rc)
  2146. pt_debug(dev, DL_ERROR,
  2147. "%s: SPI read Error = %d\n",
  2148. __func__, rc);
  2149. else if (size > read_len) {
  2150. pt_debug(cd->dev, DL_ERROR,
  2151. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2152. __func__, size, read_len);
  2153. rc = -EIO;
  2154. }
  2155. break;
  2156. }
  2157. /*
  2158. * Poll GPIO with fixed interval 20 us, and tollerance is
  2159. * limited to 10 us to speed up the process.
  2160. */
  2161. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2162. usleep_range(retry_interval, retry_interval+10);
  2163. retry_total_time += retry_interval;
  2164. }
  2165. #endif
  2166. *actual_read_len = size;
  2167. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2168. pt_debug(dev, DL_ERROR,
  2169. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2170. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2171. *actual_read_len = 0;
  2172. rc = -EINVAL;
  2173. }
  2174. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2175. "<<< NO_INT Read");
  2176. exit:
  2177. kfree(write_buf);
  2178. if (protect == PT_CORE_CMD_PROTECTED) {
  2179. if (release_exclusive(cd, cd->dev) < 0)
  2180. pt_debug(cd->dev, DL_ERROR,
  2181. "%s: fail to release exclusive\n", __func__);
  2182. }
  2183. return rc;
  2184. }
  2185. /*******************************************************************************
  2186. * FUNCTION: pt_pip_null_
  2187. *
  2188. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2189. * This function is used by watchdog to check if the fw corrupts.
  2190. *
  2191. * RETURN:
  2192. * 0 = success
  2193. * !0 = failure
  2194. *
  2195. * PARAMETERS:
  2196. * *cd - pointer to core data
  2197. ******************************************************************************/
  2198. static int pt_pip_null_(struct pt_core_data *cd)
  2199. {
  2200. struct pt_hid_output hid_output = {
  2201. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2202. };
  2203. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2204. }
  2205. /*******************************************************************************
  2206. * FUNCTION: pt_pip_null
  2207. *
  2208. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2209. *
  2210. * RETURN:
  2211. * 0 = success
  2212. * !0 = failure
  2213. *
  2214. * PARAMETERS:
  2215. * *cd - pointer to core data
  2216. ******************************************************************************/
  2217. static int pt_pip_null(struct pt_core_data *cd)
  2218. {
  2219. int rc;
  2220. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2221. if (rc < 0) {
  2222. pt_debug(cd->dev, DL_ERROR,
  2223. "%s: fail get exclusive ex=%p own=%p\n",
  2224. __func__, cd->exclusive_dev, cd->dev);
  2225. return rc;
  2226. }
  2227. rc = pt_pip_null_(cd);
  2228. if (release_exclusive(cd, cd->dev) < 0)
  2229. pt_debug(cd->dev, DL_ERROR,
  2230. "%s: fail to release exclusive\n", __func__);
  2231. return rc;
  2232. }
  2233. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2234. /*******************************************************************************
  2235. * FUNCTION: pt_pip_start_bootloader_
  2236. *
  2237. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2238. *
  2239. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2240. * the WD active could cause this function to fail!
  2241. *
  2242. * RETURN:
  2243. * 0 = success
  2244. * !0 = failure
  2245. *
  2246. * PARAMETERS:
  2247. * *cd - pointer to core data
  2248. ******************************************************************************/
  2249. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2250. {
  2251. int rc = 0;
  2252. struct pt_hid_output hid_output = {
  2253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2254. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2255. .reset_expected = 1,
  2256. };
  2257. if (cd->watchdog_enabled) {
  2258. pt_debug(cd->dev, DL_WARN,
  2259. "%s: watchdog isn't stopped before enter bl\n",
  2260. __func__);
  2261. goto exit;
  2262. }
  2263. /* Reset startup status after entering BL, new DUT enum required */
  2264. cd->startup_status = STARTUP_STATUS_START;
  2265. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2266. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2267. if (rc) {
  2268. pt_debug(cd->dev, DL_ERROR,
  2269. "%s: Start BL PIP cmd failed. rc = %d\n",
  2270. __func__, rc);
  2271. }
  2272. exit:
  2273. return rc;
  2274. }
  2275. /*******************************************************************************
  2276. * FUNCTION: pt_pip_start_bootloader
  2277. *
  2278. * SUMMARY: Protected function to force DUT to enter the BL
  2279. *
  2280. * RETURN:
  2281. * 0 = success
  2282. * !0 = failure
  2283. *
  2284. * PARAMETERS:
  2285. * *cd - pointer to core data structure
  2286. ******************************************************************************/
  2287. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2288. {
  2289. int rc;
  2290. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2291. if (rc < 0) {
  2292. pt_debug(cd->dev, DL_ERROR,
  2293. "%s: fail get exclusive ex=%p own=%p\n",
  2294. __func__, cd->exclusive_dev, cd->dev);
  2295. return rc;
  2296. }
  2297. rc = pt_pip_start_bootloader_(cd);
  2298. if (release_exclusive(cd, cd->dev) < 0)
  2299. pt_debug(cd->dev, DL_ERROR,
  2300. "%s: fail to release exclusive\n", __func__);
  2301. return rc;
  2302. }
  2303. /*******************************************************************************
  2304. * FUNCTION: _pt_request_pip_start_bl
  2305. *
  2306. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2307. * modules to request the DUT to enter the BL
  2308. *
  2309. * RETURN:
  2310. * 0 = success
  2311. * !0 = failure
  2312. *
  2313. * PARAMETERS:
  2314. * *dev - pointer to device structure
  2315. * protect - flag to run in protected mode
  2316. ******************************************************************************/
  2317. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2318. {
  2319. struct pt_core_data *cd = dev_get_drvdata(dev);
  2320. if (protect)
  2321. return pt_pip_start_bootloader(cd);
  2322. return pt_pip_start_bootloader_(cd);
  2323. }
  2324. /*******************************************************************************
  2325. * FUNCTION: pt_pip2_ver_load_ttdata
  2326. *
  2327. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2328. * command into the core data struct.
  2329. *
  2330. * RETURN: n/a
  2331. *
  2332. * PARAMETERS:
  2333. * *cd - pointer to core data structure
  2334. * len - Length of data in response_buf
  2335. ******************************************************************************/
  2336. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2337. {
  2338. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2339. struct pt_pip2_version_full *full_ver;
  2340. struct pt_pip2_version *ver;
  2341. /*
  2342. * The PIP2 VERSION command can return different lengths of data.
  2343. * The additional LOT fields are included when the packet
  2344. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2345. * NOTE:
  2346. * - The FW would swap the BL and FW versions when reporting
  2347. * the small packet.
  2348. * - Sub Lot bytes 16 and 17 are reserved.
  2349. */
  2350. if (len >= 0x1D) {
  2351. full_ver = (struct pt_pip2_version_full *)
  2352. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2353. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2354. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2355. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2356. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2357. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2358. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2359. /*
  2360. * BL PIP 2.02 and greater the version fields are
  2361. * swapped
  2362. */
  2363. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2364. ttdata->chip_rev =
  2365. get_unaligned_le16(&full_ver->chip_rev);
  2366. ttdata->chip_id =
  2367. get_unaligned_le16(&full_ver->chip_id);
  2368. } else {
  2369. ttdata->chip_rev =
  2370. get_unaligned_le16(&full_ver->chip_id);
  2371. ttdata->chip_id =
  2372. get_unaligned_le16(&full_ver->chip_rev);
  2373. }
  2374. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2375. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2376. sizeof(struct pt_pip2_version_full),
  2377. "PIP2 VERSION FULL");
  2378. } else {
  2379. ver = (struct pt_pip2_version *)
  2380. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2381. ttdata->pip_ver_major = ver->pip2_version_msb;
  2382. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2383. ttdata->bl_ver_major = ver->bl_version_msb;
  2384. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2385. ttdata->fw_ver_major = ver->fw_version_msb;
  2386. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2387. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2388. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2389. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2390. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2391. }
  2392. }
  2393. /*******************************************************************************
  2394. * FUNCTION: pt_si_get_ttdata
  2395. *
  2396. * SUMMARY: Function to load the version information from the system information
  2397. * PIP command into the core data struct.
  2398. *
  2399. * RETURN: n/a
  2400. *
  2401. * PARAMETERS:
  2402. * *cd - pointer to core data structure
  2403. ******************************************************************************/
  2404. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2405. {
  2406. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2407. struct pt_ttdata_dev *ttdata_dev =
  2408. (struct pt_ttdata_dev *)
  2409. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2410. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2411. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2412. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2413. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2414. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2415. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2416. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2417. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2418. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2419. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2420. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2421. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2422. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2423. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2424. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2425. }
  2426. /*******************************************************************************
  2427. * FUNCTION: pt_si_get_sensing_conf_data
  2428. *
  2429. * SUMMARY: Function to load the sensing information from the system information
  2430. * PIP command into the core data struct.
  2431. *
  2432. * RETURN: n/a
  2433. *
  2434. * PARAMETERS:
  2435. * *cd - pointer to core data structure
  2436. ******************************************************************************/
  2437. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2438. {
  2439. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2440. struct pt_sensing_conf_data_dev *scd_dev =
  2441. (struct pt_sensing_conf_data_dev *)
  2442. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2443. scd->electrodes_x = scd_dev->electrodes_x;
  2444. scd->electrodes_y = scd_dev->electrodes_y;
  2445. scd->origin_x = scd_dev->origin_x;
  2446. scd->origin_y = scd_dev->origin_y;
  2447. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2448. if (scd->origin_x & 0x02) {
  2449. scd->tx_num = scd->electrodes_x;
  2450. scd->rx_num = scd->electrodes_y;
  2451. } else {
  2452. scd->tx_num = scd->electrodes_y;
  2453. scd->rx_num = scd->electrodes_x;
  2454. }
  2455. /*
  2456. * When the Panel ID is coming from an XY pin and not a dedicated
  2457. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2458. * other DUTs as the loader will use cd->pid_for_loader to generate
  2459. * the bin file name but will ignore it if pid_for_loader is still
  2460. * set to PANEL_ID_NOT_ENABLED
  2461. */
  2462. if (cd->panel_id_support &
  2463. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2464. mutex_lock(&cd->system_lock);
  2465. cd->pid_for_loader = scd_dev->panel_id;
  2466. mutex_unlock(&cd->system_lock);
  2467. }
  2468. scd->panel_id = scd_dev->panel_id;
  2469. scd->btn = scd_dev->btn;
  2470. scd->scan_mode = scd_dev->scan_mode;
  2471. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2472. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2473. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2474. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2475. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2476. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2477. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2478. sizeof(struct pt_sensing_conf_data_dev),
  2479. "sensing_conf_data");
  2480. }
  2481. /*******************************************************************************
  2482. * FUNCTION: pt_si_setup
  2483. *
  2484. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2485. *
  2486. * RETURN:
  2487. * 0 = success
  2488. * !0 = failure
  2489. *
  2490. * PARAMETERS:
  2491. * *cd - pointer to core data structure
  2492. ******************************************************************************/
  2493. static int pt_si_setup(struct pt_core_data *cd)
  2494. {
  2495. struct pt_sysinfo *si = &cd->sysinfo;
  2496. int max_tch = si->sensing_conf_data.max_tch;
  2497. if (!si->xy_data)
  2498. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2499. GFP_KERNEL);
  2500. if (!si->xy_data)
  2501. return -ENOMEM;
  2502. if (!si->xy_mode)
  2503. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2504. if (!si->xy_mode) {
  2505. kfree(si->xy_data);
  2506. return -ENOMEM;
  2507. }
  2508. return 0;
  2509. }
  2510. /*******************************************************************************
  2511. * FUNCTION: pt_si_get_btn_data
  2512. *
  2513. * SUMMARY: Setup the core data button information based on the response of the
  2514. * System Information PIP command.
  2515. *
  2516. * RETURN:
  2517. * 0 = success
  2518. * !0 = failure
  2519. *
  2520. * PARAMETERS:
  2521. * *cd - pointer to core data structure
  2522. ******************************************************************************/
  2523. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2524. {
  2525. struct pt_sysinfo *si = &cd->sysinfo;
  2526. int num_btns = 0;
  2527. int num_defined_keys;
  2528. u16 *key_table;
  2529. int btn;
  2530. int i;
  2531. int rc = 0;
  2532. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2533. & PIP1_SYSINFO_BTN_MASK;
  2534. size_t btn_keys_size;
  2535. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2536. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2537. if (btns & (1 << i))
  2538. num_btns++;
  2539. }
  2540. si->num_btns = num_btns;
  2541. if (num_btns) {
  2542. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2543. if (!si->btn)
  2544. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2545. if (!si->btn)
  2546. return -ENOMEM;
  2547. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2548. num_defined_keys = 0;
  2549. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2550. num_defined_keys = 0;
  2551. else
  2552. num_defined_keys = cd->cpdata->sett
  2553. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2554. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2555. key_table = (u16 *)cd->cpdata->sett
  2556. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2557. si->btn[btn].key_code = key_table[btn];
  2558. si->btn[btn].enabled = true;
  2559. }
  2560. for (; btn < num_btns; btn++) {
  2561. si->btn[btn].key_code = KEY_RESERVED;
  2562. si->btn[btn].enabled = true;
  2563. }
  2564. return rc;
  2565. }
  2566. kfree(si->btn);
  2567. si->btn = NULL;
  2568. return rc;
  2569. }
  2570. /*******************************************************************************
  2571. * FUNCTION: pt_si_put_log_data
  2572. *
  2573. * SUMMARY: Prints all sys info data to kmsg log
  2574. *
  2575. * RETURN: n/a
  2576. *
  2577. * PARAMETERS:
  2578. * *cd - pointer to core data structure
  2579. ******************************************************************************/
  2580. static void pt_si_put_log_data(struct pt_core_data *cd)
  2581. {
  2582. struct pt_sysinfo *si = &cd->sysinfo;
  2583. struct pt_ttdata *ttdata = &si->ttdata;
  2584. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2585. int i;
  2586. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2587. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2588. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2589. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2590. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2591. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2592. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2593. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2594. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2595. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2596. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2597. __func__, ttdata->revctrl, ttdata->revctrl);
  2598. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2599. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2600. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2601. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2602. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2603. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2604. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2605. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2606. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2607. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2608. for (i = 0; i < PT_NUM_MFGID; i++)
  2609. pt_debug(cd->dev, DL_DEBUG,
  2610. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2611. __func__, i, ttdata->mfg_id[i],
  2612. ttdata->mfg_id[i]);
  2613. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2614. __func__, ttdata->post_code, ttdata->post_code);
  2615. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2616. __func__, scd->electrodes_x, scd->electrodes_x);
  2617. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2618. __func__, scd->electrodes_y, scd->electrodes_y);
  2619. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2620. __func__, scd->len_x, scd->len_x);
  2621. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2622. __func__, scd->len_y, scd->len_y);
  2623. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2624. __func__, scd->res_x, scd->res_x);
  2625. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2626. __func__, scd->res_y, scd->res_y);
  2627. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2628. __func__, scd->max_z, scd->max_z);
  2629. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2630. __func__, scd->origin_x, scd->origin_x);
  2631. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2632. __func__, scd->origin_y, scd->origin_y);
  2633. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2634. __func__, scd->panel_id, scd->panel_id);
  2635. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2636. __func__, scd->btn, scd->btn);
  2637. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2638. __func__, scd->scan_mode, scd->scan_mode);
  2639. pt_debug(cd->dev, DL_DEBUG,
  2640. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2641. __func__, scd->max_tch, scd->max_tch);
  2642. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2643. __func__, si->xy_mode);
  2644. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2645. __func__, si->xy_data);
  2646. }
  2647. /*******************************************************************************
  2648. * FUNCTION: pt_get_sysinfo_regs
  2649. *
  2650. * SUMMARY: Setup all the core data System information based on the response
  2651. * of the System Information PIP command.
  2652. *
  2653. * RETURN:
  2654. * 0 = success
  2655. * !0 = failure
  2656. *
  2657. * PARAMETERS:
  2658. * *cd - pointer to core data structure
  2659. ******************************************************************************/
  2660. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2661. {
  2662. struct pt_sysinfo *si = &cd->sysinfo;
  2663. int rc;
  2664. rc = pt_si_get_btn_data(cd);
  2665. if (rc < 0)
  2666. return rc;
  2667. pt_si_get_ttdata(cd);
  2668. pt_si_get_sensing_conf_data(cd);
  2669. pt_si_setup(cd);
  2670. pt_si_put_log_data(cd);
  2671. si->ready = true;
  2672. return rc;
  2673. }
  2674. /*******************************************************************************
  2675. * FUNCTION: pt_free_si_ptrs
  2676. *
  2677. * SUMMARY: Frees all memory associated with the System Information within
  2678. * core data
  2679. *
  2680. * RETURN: n/a
  2681. *
  2682. * PARAMETERS:
  2683. * *cd - pointer to core data structure
  2684. ******************************************************************************/
  2685. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2686. {
  2687. struct pt_sysinfo *si = &cd->sysinfo;
  2688. kfree(si->btn);
  2689. kfree(si->xy_mode);
  2690. kfree(si->xy_data);
  2691. }
  2692. /*******************************************************************************
  2693. * FUNCTION: pt_hid_output_get_sysinfo_
  2694. *
  2695. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2696. * response.
  2697. *
  2698. * RETURN::
  2699. * 0 = success
  2700. * !0 = failure
  2701. *
  2702. * PARAMETERS:
  2703. * *cd - pointer to core data structure
  2704. ******************************************************************************/
  2705. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2706. {
  2707. int rc = 0;
  2708. struct pt_hid_output hid_output = {
  2709. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2710. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2711. };
  2712. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2713. if (rc)
  2714. return rc;
  2715. /* Parse the sysinfo data */
  2716. rc = pt_get_sysinfo_regs(cd);
  2717. if (rc)
  2718. pt_free_si_ptrs(cd);
  2719. return rc;
  2720. }
  2721. /*******************************************************************************
  2722. * FUNCTION: pt_hid_output_get_sysinfo
  2723. *
  2724. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2725. *
  2726. * RETURN::
  2727. * 0 = success
  2728. * !0 = failure
  2729. *
  2730. * PARAMETERS:
  2731. * *cd - pointer to core data structure
  2732. ******************************************************************************/
  2733. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2734. {
  2735. int rc;
  2736. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2737. if (rc < 0) {
  2738. pt_debug(cd->dev, DL_ERROR,
  2739. "%s: fail get exclusive ex=%p own=%p\n",
  2740. __func__, cd->exclusive_dev, cd->dev);
  2741. return rc;
  2742. }
  2743. rc = pt_hid_output_get_sysinfo_(cd);
  2744. if (release_exclusive(cd, cd->dev) < 0)
  2745. pt_debug(cd->dev, DL_ERROR,
  2746. "%s: fail to release exclusive\n", __func__);
  2747. return rc;
  2748. }
  2749. /*******************************************************************************
  2750. * FUNCTION: pt_pip_suspend_scanning_
  2751. *
  2752. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2753. *
  2754. * RETURN::
  2755. * 0 = success
  2756. * !0 = failure
  2757. *
  2758. * PARAMETERS:
  2759. * *cd - pointer to core data structure
  2760. ******************************************************************************/
  2761. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2762. {
  2763. int rc = 0;
  2764. struct pt_hid_output hid_output = {
  2765. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2766. };
  2767. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2768. if (rc) {
  2769. pt_debug(cd->dev, DL_ERROR,
  2770. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2771. __func__, rc);
  2772. }
  2773. return rc;
  2774. }
  2775. /*******************************************************************************
  2776. * FUNCTION: pt_pip_suspend_scanning
  2777. *
  2778. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2779. *
  2780. * RETURN::
  2781. * 0 = success
  2782. * !0 = failure
  2783. *
  2784. * PARAMETERS:
  2785. * *cd - pointer to core data structure
  2786. ******************************************************************************/
  2787. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2788. {
  2789. int rc;
  2790. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2791. if (rc < 0) {
  2792. pt_debug(cd->dev, DL_ERROR,
  2793. "%s: fail get exclusive ex=%p own=%p\n",
  2794. __func__, cd->exclusive_dev, cd->dev);
  2795. return rc;
  2796. }
  2797. rc = pt_pip_suspend_scanning_(cd);
  2798. if (release_exclusive(cd, cd->dev) < 0)
  2799. pt_debug(cd->dev, DL_ERROR,
  2800. "%s: fail to release exclusive\n", __func__);
  2801. return rc;
  2802. }
  2803. /*******************************************************************************
  2804. * FUNCTION: _pt_request_pip_suspend_scanning
  2805. *
  2806. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2807. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2808. *
  2809. * RETURN:
  2810. * 0 = success
  2811. * !0 = failure
  2812. *
  2813. * PARAMETERS:
  2814. * *dev - pointer to device structure
  2815. * protect - 0 = call non-protected function
  2816. * 1 = call protected function
  2817. ******************************************************************************/
  2818. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2819. int protect)
  2820. {
  2821. struct pt_core_data *cd = dev_get_drvdata(dev);
  2822. if (protect)
  2823. return pt_pip_suspend_scanning(cd);
  2824. return pt_pip_suspend_scanning_(cd);
  2825. }
  2826. /*******************************************************************************
  2827. * FUNCTION: pt_pip_resume_scanning_
  2828. *
  2829. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2830. *
  2831. * RETURN::
  2832. * 0 = success
  2833. * !0 = failure
  2834. *
  2835. * PARAMETERS:
  2836. * *cd - pointer to core data structure
  2837. ******************************************************************************/
  2838. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2839. {
  2840. int rc = 0;
  2841. struct pt_hid_output hid_output = {
  2842. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2843. };
  2844. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2845. if (rc) {
  2846. pt_debug(cd->dev, DL_ERROR,
  2847. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2848. __func__, rc);
  2849. }
  2850. return rc;
  2851. }
  2852. /*******************************************************************************
  2853. * FUNCTION: pt_pip_resume_scanning
  2854. *
  2855. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2856. *
  2857. * RETURN::
  2858. * 0 = success
  2859. * !0 = failure
  2860. *
  2861. * PARAMETERS:
  2862. * *cd - pointer to core data structure
  2863. ******************************************************************************/
  2864. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2865. {
  2866. int rc;
  2867. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2868. if (rc < 0) {
  2869. pt_debug(cd->dev, DL_ERROR,
  2870. "%s: fail get exclusive ex=%p own=%p\n",
  2871. __func__, cd->exclusive_dev, cd->dev);
  2872. return rc;
  2873. }
  2874. rc = pt_pip_resume_scanning_(cd);
  2875. if (release_exclusive(cd, cd->dev) < 0)
  2876. pt_debug(cd->dev, DL_ERROR,
  2877. "%s: fail to release exclusive\n", __func__);
  2878. return rc;
  2879. }
  2880. /*******************************************************************************
  2881. * FUNCTION: _pt_request_pip_resume_scanning
  2882. *
  2883. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2884. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2885. *
  2886. * RETURN::
  2887. * 0 = success
  2888. * !0 = failure
  2889. *
  2890. * PARAMETERS:
  2891. * *dev - pointer to device structure
  2892. * protect - 0 = call non-protected function
  2893. * 1 = call protected function
  2894. ******************************************************************************/
  2895. static int _pt_request_pip_resume_scanning(struct device *dev,
  2896. int protect)
  2897. {
  2898. struct pt_core_data *cd = dev_get_drvdata(dev);
  2899. if (protect)
  2900. return pt_pip_resume_scanning(cd);
  2901. return pt_pip_resume_scanning_(cd);
  2902. }
  2903. /*******************************************************************************
  2904. * FUNCTION: pt_pip_get_param_
  2905. *
  2906. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2907. * the 32bit parameter value
  2908. *
  2909. * RETURN:
  2910. * 0 = success
  2911. * !0 = failure
  2912. *
  2913. * PARAMETERS:
  2914. * *cd - pointer to core data
  2915. * param_id - parameter ID to retrieve
  2916. * *value - value of DUT parameter
  2917. ******************************************************************************/
  2918. static int pt_pip_get_param_(struct pt_core_data *cd,
  2919. u8 param_id, u32 *value)
  2920. {
  2921. int write_length = 1;
  2922. u8 param[1] = { param_id };
  2923. u8 read_param_id;
  2924. int param_size;
  2925. u8 *ptr;
  2926. int rc = 0;
  2927. int i;
  2928. struct pt_hid_output hid_output = {
  2929. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2930. .write_length = write_length,
  2931. .write_buf = param,
  2932. };
  2933. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2934. if (rc)
  2935. return rc;
  2936. read_param_id = cd->response_buf[5];
  2937. if (read_param_id != param_id)
  2938. return -EPROTO;
  2939. param_size = cd->response_buf[6];
  2940. ptr = &cd->response_buf[7];
  2941. *value = 0;
  2942. for (i = 0; i < param_size; i++)
  2943. *value += ptr[i] << (i * 8);
  2944. return 0;
  2945. }
  2946. /*******************************************************************************
  2947. * FUNCTION: pt_pip_get_param
  2948. *
  2949. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2950. *
  2951. * RETURN:
  2952. * 0 = success
  2953. * !0 = failure
  2954. *
  2955. * PARAMETERS:
  2956. * *cd - pointer to core data
  2957. * param_id - parameter ID to retrieve
  2958. * *value - value of DUT parameter
  2959. ******************************************************************************/
  2960. static int pt_pip_get_param(struct pt_core_data *cd,
  2961. u8 param_id, u32 *value)
  2962. {
  2963. int rc;
  2964. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2965. if (rc < 0) {
  2966. pt_debug(cd->dev, DL_ERROR,
  2967. "%s: fail get exclusive ex=%p own=%p\n",
  2968. __func__, cd->exclusive_dev, cd->dev);
  2969. return rc;
  2970. }
  2971. rc = pt_pip_get_param_(cd, param_id, value);
  2972. if (release_exclusive(cd, cd->dev) < 0)
  2973. pt_debug(cd->dev, DL_ERROR,
  2974. "%s: fail to release exclusive\n", __func__);
  2975. return rc;
  2976. }
  2977. /*******************************************************************************
  2978. * FUNCTION: _pt_request_pip_get_param
  2979. *
  2980. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2981. * calls to the protected or unprotected call to pt_pip_get_param
  2982. *
  2983. * RETURN:
  2984. * 0 = success
  2985. * !0 = failure
  2986. *
  2987. * PARAMETERS:
  2988. * *dev - pointer to device structure
  2989. * protect - flag to call protected or non protected function
  2990. * param_id - parameter ID to retrieve
  2991. * *value - value of DUT parameter
  2992. ******************************************************************************/
  2993. int _pt_request_pip_get_param(struct device *dev,
  2994. int protect, u8 param_id, u32 *value)
  2995. {
  2996. struct pt_core_data *cd = dev_get_drvdata(dev);
  2997. if (protect)
  2998. return pt_pip_get_param(cd, param_id, value);
  2999. return pt_pip_get_param_(cd, param_id, value);
  3000. }
  3001. /*******************************************************************************
  3002. * FUNCTION: pt_pip_set_param_
  3003. *
  3004. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3005. * passed in value to flash
  3006. *
  3007. * RETURN:
  3008. * 0 = success
  3009. * !0 = failure
  3010. *
  3011. * PARAMETERS:
  3012. * *cd - pointer to core data
  3013. * param_id - parameter ID to set
  3014. * value - value to write
  3015. * size - size to write
  3016. ******************************************************************************/
  3017. static int pt_pip_set_param_(struct pt_core_data *cd,
  3018. u8 param_id, u32 value, u8 size)
  3019. {
  3020. u8 write_buf[6];
  3021. u8 *ptr = &write_buf[2];
  3022. int rc = 0;
  3023. int i;
  3024. struct pt_hid_output hid_output = {
  3025. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3026. .write_buf = write_buf,
  3027. };
  3028. write_buf[0] = param_id;
  3029. write_buf[1] = size;
  3030. for (i = 0; i < size; i++) {
  3031. ptr[i] = value & 0xFF;
  3032. value = value >> 8;
  3033. }
  3034. hid_output.write_length = 2 + size;
  3035. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3036. if (rc)
  3037. return rc;
  3038. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3039. return -EPROTO;
  3040. return 0;
  3041. }
  3042. /*******************************************************************************
  3043. * FUNCTION: pt_pip_set_param
  3044. *
  3045. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3046. *
  3047. * RETURN:
  3048. * 0 = success
  3049. * !0 = failure
  3050. *
  3051. * PARAMETERS:
  3052. * *cd - pointer to core data
  3053. * param_id - parameter ID to set
  3054. * value - value to write
  3055. * size - size to write
  3056. ******************************************************************************/
  3057. static int pt_pip_set_param(struct pt_core_data *cd,
  3058. u8 param_id, u32 value, u8 size)
  3059. {
  3060. int rc;
  3061. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3062. if (rc < 0) {
  3063. pt_debug(cd->dev, DL_ERROR,
  3064. "%s: fail get exclusive ex=%p own=%p\n",
  3065. __func__, cd->exclusive_dev, cd->dev);
  3066. return rc;
  3067. }
  3068. rc = pt_pip_set_param_(cd, param_id, value, size);
  3069. if (release_exclusive(cd, cd->dev) < 0)
  3070. pt_debug(cd->dev, DL_ERROR,
  3071. "%s: fail to release exclusive\n", __func__);
  3072. return rc;
  3073. }
  3074. /*******************************************************************************
  3075. * FUNCTION: _pt_request_pip_set_param
  3076. *
  3077. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3078. * calls to the protected or unprotected call to pt_pip_set_param
  3079. *
  3080. * RETURN:
  3081. * 0 = success
  3082. * !0 = failure
  3083. *
  3084. * PARAMETERS:
  3085. * *dev - pointer to device structure
  3086. * protect - flag to call protected or non-protected
  3087. * param_id - parameter ID to set
  3088. * value - value to write
  3089. * size - size to write
  3090. ******************************************************************************/
  3091. int _pt_request_pip_set_param(struct device *dev, int protect,
  3092. u8 param_id, u32 value, u8 size)
  3093. {
  3094. struct pt_core_data *cd = dev_get_drvdata(dev);
  3095. if (protect)
  3096. return pt_pip_set_param(cd, param_id, value, size);
  3097. return pt_pip_set_param_(cd, param_id, value, size);
  3098. }
  3099. /*******************************************************************************
  3100. * FUNCTION: _pt_pip_enter_easywake_state_
  3101. *
  3102. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3103. *
  3104. * RETURN:
  3105. * 0 = success
  3106. * !0 = failure
  3107. *
  3108. * PARAMETERS:
  3109. * *dev - pointer to device structure
  3110. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3111. * *return_data - return status if easywake was entered
  3112. ******************************************************************************/
  3113. static int pt_hid_output_enter_easywake_state_(
  3114. struct pt_core_data *cd, u8 data, u8 *return_data)
  3115. {
  3116. int write_length = 1;
  3117. u8 param[1] = { data };
  3118. int rc = 0;
  3119. struct pt_hid_output hid_output = {
  3120. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3121. .write_length = write_length,
  3122. .write_buf = param,
  3123. };
  3124. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3125. if (rc)
  3126. return rc;
  3127. *return_data = cd->response_buf[5];
  3128. return rc;
  3129. }
  3130. /*******************************************************************************
  3131. * FUNCTION: pt_pip_verify_config_block_crc_
  3132. *
  3133. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3134. *
  3135. * RETURN:
  3136. * 0 = success
  3137. * !0 = failure
  3138. *
  3139. * PARAMETERS:
  3140. * *cd - pointer the core data structure
  3141. * ebid - enumerated block ID
  3142. * *status - PIP command status
  3143. * calculated_crc - calculated CRC
  3144. * stored_crc - stored CRC in config area
  3145. ******************************************************************************/
  3146. static int pt_pip_verify_config_block_crc_(
  3147. struct pt_core_data *cd, u8 ebid, u8 *status,
  3148. u16 *calculated_crc, u16 *stored_crc)
  3149. {
  3150. int write_length = 1;
  3151. u8 param[1] = { ebid };
  3152. u8 *ptr;
  3153. int rc = 0;
  3154. struct pt_hid_output hid_output = {
  3155. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3156. .write_length = write_length,
  3157. .write_buf = param,
  3158. };
  3159. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3160. if (rc)
  3161. return rc;
  3162. ptr = &cd->response_buf[5];
  3163. *status = ptr[0];
  3164. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3165. *stored_crc = get_unaligned_le16(&ptr[3]);
  3166. return 0;
  3167. }
  3168. /*******************************************************************************
  3169. * FUNCTION: pt_pip_verify_config_block_crc
  3170. *
  3171. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3172. * an exclusive access to the DUT.
  3173. *
  3174. * RETURN:
  3175. * 0 = success
  3176. * !0 = failure
  3177. *
  3178. * PARAMETERS:
  3179. * *cd - pointer the core data structure
  3180. * ebid - enumerated block ID
  3181. * *status - PIP command status
  3182. * calculated_crc - calculated CRC
  3183. * stored_crc - stored CRC in config area
  3184. ******************************************************************************/
  3185. static int pt_pip_verify_config_block_crc(
  3186. struct pt_core_data *cd, u8 ebid, u8 *status,
  3187. u16 *calculated_crc, u16 *stored_crc)
  3188. {
  3189. int rc;
  3190. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3191. if (rc < 0) {
  3192. pt_debug(cd->dev, DL_ERROR,
  3193. "%s: fail get exclusive ex=%p own=%p\n",
  3194. __func__, cd->exclusive_dev, cd->dev);
  3195. return rc;
  3196. }
  3197. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3198. calculated_crc, stored_crc);
  3199. if (release_exclusive(cd, cd->dev) < 0)
  3200. pt_debug(cd->dev, DL_ERROR,
  3201. "%s: fail to release exclusive\n", __func__);
  3202. return rc;
  3203. }
  3204. /*******************************************************************************
  3205. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3206. *
  3207. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3208. * calls to the protected or unprotected call to
  3209. * pt_pip_verify_config_block_crc_
  3210. *
  3211. * RETURN:
  3212. * 0 = success
  3213. * !0 = failure
  3214. *
  3215. * PARAMETERS:
  3216. * *dev - pointer to device structure
  3217. * protect - flag to call protected or non-protected
  3218. * ebid - enumerated block ID
  3219. * *status - PIP command status
  3220. * calculated_crc - calculated CRC
  3221. * stored_crc - stored CRC in config area
  3222. ******************************************************************************/
  3223. static int _pt_request_pip_verify_config_block_crc(
  3224. struct device *dev, int protect, u8 ebid, u8 *status,
  3225. u16 *calculated_crc, u16 *stored_crc)
  3226. {
  3227. struct pt_core_data *cd = dev_get_drvdata(dev);
  3228. if (protect)
  3229. return pt_pip_verify_config_block_crc(cd, ebid,
  3230. status, calculated_crc, stored_crc);
  3231. return pt_pip_verify_config_block_crc_(cd, ebid,
  3232. status, calculated_crc, stored_crc);
  3233. }
  3234. /*******************************************************************************
  3235. * FUNCTION: pt_pip_get_config_row_size_
  3236. *
  3237. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3238. *
  3239. * RETURN:
  3240. * 0 = success
  3241. * !0 = failure
  3242. *
  3243. * PARAMETERS:
  3244. * *cd - pointer to core data
  3245. * protect - flag to call protected or non-protected
  3246. * *row_size - pointer to store the retrieved row size
  3247. ******************************************************************************/
  3248. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3249. u16 *row_size)
  3250. {
  3251. int rc = 0;
  3252. struct pt_hid_output hid_output = {
  3253. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3254. };
  3255. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3256. if (rc)
  3257. return rc;
  3258. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3259. return 0;
  3260. }
  3261. /*******************************************************************************
  3262. * FUNCTION: pt_pip_get_config_row_size
  3263. *
  3264. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3265. * an exclusive access to the DUT.
  3266. *
  3267. * RETURN:
  3268. * 0 = success
  3269. * !0 = failure
  3270. *
  3271. * PARAMETERS:
  3272. * *cd - pointer to core data
  3273. * protect - flag to call protected or non-protected
  3274. * *row_size - pointer to store the retrieved row size
  3275. ******************************************************************************/
  3276. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3277. u16 *row_size)
  3278. {
  3279. int rc;
  3280. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3281. if (rc < 0) {
  3282. pt_debug(cd->dev, DL_ERROR,
  3283. "%s: fail get exclusive ex=%p own=%p\n",
  3284. __func__, cd->exclusive_dev, cd->dev);
  3285. return rc;
  3286. }
  3287. rc = pt_pip_get_config_row_size_(cd, row_size);
  3288. if (release_exclusive(cd, cd->dev) < 0)
  3289. pt_debug(cd->dev, DL_ERROR,
  3290. "%s: fail to release exclusive\n", __func__);
  3291. return rc;
  3292. }
  3293. /*******************************************************************************
  3294. * FUNCTION: _pt_request_pip_get_config_row_size
  3295. *
  3296. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3297. * calls to the protected or unprotected call to
  3298. * pt_pip_get_config_row_size_
  3299. *
  3300. * RETURN:
  3301. * 0 = success
  3302. * !0 = failure
  3303. *
  3304. * PARAMETERS:
  3305. * *dev - pointer to device structure
  3306. * protect - flag to call protected or non-protected
  3307. * *row_size - pointer to store the retrieved row size
  3308. ******************************************************************************/
  3309. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3310. int protect, u16 *row_size)
  3311. {
  3312. struct pt_core_data *cd = dev_get_drvdata(dev);
  3313. if (protect)
  3314. return pt_pip_get_config_row_size(cd, row_size);
  3315. return pt_pip_get_config_row_size_(cd, row_size);
  3316. }
  3317. /*******************************************************************************
  3318. * FUNCTION: pt_pip1_read_data_block_
  3319. *
  3320. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3321. * output data to the "read_buf" and update "crc".
  3322. *
  3323. * RETURN:
  3324. * 0 = success
  3325. * !0 = failure
  3326. *
  3327. * PARAMETERS:
  3328. * *cd - pointer to core data
  3329. * row_number - row number
  3330. * length - length of data to read
  3331. * ebid - block id
  3332. * *actual_read_len - Actual data length read
  3333. * *read_buf - pointer to the buffer to store read data
  3334. * read_buf_size - size of read_buf
  3335. * *crc - pointer to store CRC of row data
  3336. ******************************************************************************/
  3337. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3338. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3339. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3340. {
  3341. int read_ebid;
  3342. int status;
  3343. int rc = 0;
  3344. int write_length = 5;
  3345. u8 write_buf[5];
  3346. u8 cmd_offset = 0;
  3347. u16 calc_crc;
  3348. struct pt_hid_output hid_output = {
  3349. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3350. .write_length = write_length,
  3351. .write_buf = write_buf,
  3352. };
  3353. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3354. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3355. write_buf[cmd_offset++] = LOW_BYTE(length);
  3356. write_buf[cmd_offset++] = HI_BYTE(length);
  3357. write_buf[cmd_offset++] = ebid;
  3358. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3359. if (rc)
  3360. return rc;
  3361. status = cd->response_buf[5];
  3362. if (status)
  3363. return status;
  3364. read_ebid = cd->response_buf[6];
  3365. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3366. return -EPROTO;
  3367. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3368. if (length == 0 || *actual_read_len == 0)
  3369. return 0;
  3370. if (read_buf_size >= *actual_read_len)
  3371. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3372. else
  3373. return -EPROTO;
  3374. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3375. /* Validate Row Data CRC */
  3376. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3377. if (*crc == calc_crc) {
  3378. return 0;
  3379. } else {
  3380. pt_debug(cd->dev, DL_ERROR,
  3381. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3382. __func__, *crc, calc_crc);
  3383. return -EPROTO;
  3384. }
  3385. }
  3386. /*******************************************************************************
  3387. * FUNCTION: _pt_request_pip_read_data_block
  3388. *
  3389. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3390. * calls to pt_pip1_read_data_block_
  3391. *
  3392. * RETURN:
  3393. * 0 = success
  3394. * !0 = failure
  3395. *
  3396. * PARAMETERS:
  3397. * *dev - pointer to device structure
  3398. * row_number - row number
  3399. * length - length of data to read
  3400. * ebid - block id
  3401. * *actual_read_len - Actual data length read
  3402. * *read_buf - pointer to the buffer to store read data
  3403. * *crc - pointer to store CRC of row data
  3404. ******************************************************************************/
  3405. static int _pt_request_pip_read_data_block(struct device *dev,
  3406. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3407. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3408. {
  3409. struct pt_core_data *cd = dev_get_drvdata(dev);
  3410. return pt_pip1_read_data_block_(cd, row_number, length,
  3411. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3412. }
  3413. /*******************************************************************************
  3414. * FUNCTION: pt_pip1_write_data_block_
  3415. *
  3416. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3417. * write data to the data block.
  3418. *
  3419. * RETURN:
  3420. * 0 = success
  3421. * !0 = failure
  3422. *
  3423. * PARAMETERS:
  3424. * *cd - pointer to core data
  3425. * row_number - row in config block to write to
  3426. * write_length - length of data to write
  3427. * ebid - enumerated block ID
  3428. * *write_buf - pointer to buffer to write
  3429. * *security_key - pointer to security key to allow write
  3430. * *actual_write_len - pointer to store data length actually written
  3431. ******************************************************************************/
  3432. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3433. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3434. u8 *security_key, u16 *actual_write_len)
  3435. {
  3436. /* row_number + write_len + ebid + security_key + crc */
  3437. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3438. u8 *full_write_buf;
  3439. u8 cmd_offset = 0;
  3440. u16 crc;
  3441. int status;
  3442. int rc = 0;
  3443. int read_ebid;
  3444. u8 *data;
  3445. struct pt_hid_output hid_output = {
  3446. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3447. .write_length = full_write_length,
  3448. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3449. };
  3450. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3451. if (!full_write_buf)
  3452. return -ENOMEM;
  3453. hid_output.write_buf = full_write_buf;
  3454. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3455. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3456. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3457. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3458. full_write_buf[cmd_offset++] = ebid;
  3459. data = &full_write_buf[cmd_offset];
  3460. memcpy(data, write_buf, write_length);
  3461. cmd_offset += write_length;
  3462. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3463. cmd_offset += 8;
  3464. crc = _pt_compute_crc(data, write_length);
  3465. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3466. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3467. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3468. if (rc)
  3469. goto exit;
  3470. status = cd->response_buf[5];
  3471. if (status) {
  3472. rc = -EINVAL;
  3473. goto exit;
  3474. }
  3475. read_ebid = cd->response_buf[6];
  3476. if (read_ebid != ebid) {
  3477. rc = -EPROTO;
  3478. goto exit;
  3479. }
  3480. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3481. exit:
  3482. kfree(full_write_buf);
  3483. return rc;
  3484. }
  3485. /*******************************************************************************
  3486. * FUNCTION: _pt_request_pip_write_data_block
  3487. *
  3488. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3489. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3490. *
  3491. * RETURN:
  3492. * 0 = success
  3493. * !0 = failure
  3494. *
  3495. * PARAMETERS:
  3496. * *dev - pointer to device structure
  3497. * protect - flag to call protected or non-protected
  3498. * row_number - row in config block to write to
  3499. * write_length - length of data to write
  3500. * ebid - enumerated block ID
  3501. * *write_buf - pointer to buffer to write
  3502. * *security_key - pointer to security key to allow write
  3503. * *actual_write_len - pointer to store data length actually written
  3504. ******************************************************************************/
  3505. static int _pt_request_pip_write_data_block(struct device *dev,
  3506. u16 row_number, u16 write_length, u8 ebid,
  3507. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3508. {
  3509. struct pt_core_data *cd = dev_get_drvdata(dev);
  3510. return pt_pip1_write_data_block_(cd, row_number,
  3511. write_length, ebid, write_buf, security_key,
  3512. actual_write_len);
  3513. }
  3514. /*******************************************************************************
  3515. * FUNCTION: pt_pip_get_data_structure_
  3516. *
  3517. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3518. * returning a structure of data defined by data_id
  3519. *
  3520. * RETURN:
  3521. * 0 = success
  3522. * !0 = failure
  3523. *
  3524. * PARAMETERS:
  3525. * *cd - pointer to core data
  3526. * read_offset - read pointer offset
  3527. * read_length - length of data to read
  3528. * data_id - data ID to read
  3529. * *status - pointer to store the read response status
  3530. * *data_format - pointer to store format of data read
  3531. * *actual_read_len - pointer to store data length actually read
  3532. * *data - pointer to store data read
  3533. ******************************************************************************/
  3534. static int pt_pip_get_data_structure_(
  3535. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3536. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3537. u8 *data)
  3538. {
  3539. int rc = 0;
  3540. u16 total_read_len = 0;
  3541. u16 read_len;
  3542. u16 off_buf = 0;
  3543. u8 write_buf[5];
  3544. u8 read_data_id;
  3545. struct pt_hid_output hid_output = {
  3546. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3547. .write_length = 5,
  3548. .write_buf = write_buf,
  3549. };
  3550. again:
  3551. write_buf[0] = LOW_BYTE(read_offset);
  3552. write_buf[1] = HI_BYTE(read_offset);
  3553. write_buf[2] = LOW_BYTE(read_length);
  3554. write_buf[3] = HI_BYTE(read_length);
  3555. write_buf[4] = data_id;
  3556. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3557. if (rc)
  3558. return rc;
  3559. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3560. goto set_status;
  3561. read_data_id = cd->response_buf[6];
  3562. if (read_data_id != data_id)
  3563. return -EPROTO;
  3564. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3565. if (read_len && data) {
  3566. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3567. total_read_len += read_len;
  3568. if (read_len < read_length) {
  3569. read_offset += read_len;
  3570. off_buf += read_len;
  3571. read_length -= read_len;
  3572. goto again;
  3573. }
  3574. }
  3575. if (data_format)
  3576. *data_format = cd->response_buf[9];
  3577. if (actual_read_len)
  3578. *actual_read_len = total_read_len;
  3579. set_status:
  3580. if (status)
  3581. *status = cd->response_buf[5];
  3582. return rc;
  3583. }
  3584. /*******************************************************************************
  3585. * FUNCTION: pt_pip_get_data_structure
  3586. *
  3587. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3588. * an exclusive access to the DUT.
  3589. *
  3590. * RETURN:
  3591. * 0 = success
  3592. * !0 = failure
  3593. *
  3594. * PARAMETERS:
  3595. * *cd - pointer to core data
  3596. * read_offset - read pointer offset
  3597. * read_length - length of data to read
  3598. * data_id - data ID to read
  3599. * *status - pointer to store the read response status
  3600. * *data_format - pointer to store format of data read
  3601. * *actual_read_len - pointer to store data length actually read
  3602. * *data - pointer to store data read
  3603. ******************************************************************************/
  3604. static int pt_pip_get_data_structure(
  3605. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3606. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3607. u8 *data)
  3608. {
  3609. int rc;
  3610. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3611. if (rc < 0) {
  3612. pt_debug(cd->dev, DL_ERROR,
  3613. "%s: fail get exclusive ex=%p own=%p\n",
  3614. __func__, cd->exclusive_dev, cd->dev);
  3615. return rc;
  3616. }
  3617. rc = pt_pip_get_data_structure_(cd, read_offset,
  3618. read_length, data_id, status, data_format,
  3619. actual_read_len, data);
  3620. if (release_exclusive(cd, cd->dev) < 0)
  3621. pt_debug(cd->dev, DL_ERROR,
  3622. "%s: fail to release exclusive\n", __func__);
  3623. return rc;
  3624. }
  3625. /*******************************************************************************
  3626. * FUNCTION: _pt_request_pip_get_data_structure
  3627. *
  3628. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3629. * calls to the protected or unprotected call to
  3630. * pt_pip_get_data_structure
  3631. *
  3632. * RETURN:
  3633. * 0 = success
  3634. * !0 = failure
  3635. *
  3636. * PARAMETERS:
  3637. * *dev - pointer to device structure
  3638. * protect - flag to call protected or non-protected
  3639. * read_offset - read pointer offset
  3640. * read_length - length of data to read
  3641. * data_id - data ID to read
  3642. * *status - pointer to store the read response status
  3643. * *data_format - pointer to store format of data read
  3644. * *actual_read_len - pointer to store data length actually read
  3645. * *data - pointer to store data read
  3646. ******************************************************************************/
  3647. static int _pt_request_pip_get_data_structure(struct device *dev,
  3648. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3649. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3650. {
  3651. struct pt_core_data *cd = dev_get_drvdata(dev);
  3652. if (protect)
  3653. return pt_pip_get_data_structure(cd,
  3654. read_offset, read_length, data_id, status,
  3655. data_format, actual_read_len, data);
  3656. return pt_pip_get_data_structure_(cd,
  3657. read_offset, read_length, data_id, status,
  3658. data_format, actual_read_len, data);
  3659. }
  3660. /*******************************************************************************
  3661. * FUNCTION: _pt_manage_local_cal_data
  3662. *
  3663. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3664. * CALIBRATION data. It stores it in a local static array and can be
  3665. * cleared, loaded or used to restore the CAL data back to the running FW.
  3666. * The CAL data is read or restored by use of the PIP1 commands:
  3667. * - READ_DATA_BLOCK (0x22)
  3668. * - WRITE_DATA_BLOCK (0x23)
  3669. *
  3670. * RETURN:
  3671. * 0 = success
  3672. * !0 = failure
  3673. *
  3674. * PARAMETERS:
  3675. * *dev - pointer to device structure
  3676. * action - One of the following actions:
  3677. * - PT_CAL_DATA_SAVE
  3678. * - PT_CAL_DATA_RESTORE
  3679. * - PT_CAL_DATA_CLEAR
  3680. * - PT_CAL_DATA_SIZE
  3681. * *size - pointer to the number of bytes transferred
  3682. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3683. ******************************************************************************/
  3684. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3685. unsigned short *crc)
  3686. {
  3687. struct pt_core_data *cd = dev_get_drvdata(dev);
  3688. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3689. unsigned short calc_id_crc = 0;
  3690. static u8 *cal_cache_data;
  3691. static u16 cal_cache_len;
  3692. static unsigned short cal_cache_chip_id;
  3693. int rc = 0;
  3694. u8 *tmp_data = NULL;
  3695. u8 row_number = 0;
  3696. u8 prefix[20];
  3697. u16 cal_size = 0;
  3698. u16 transfer_size;
  3699. u16 act_trans_len = 0;
  3700. u16 byte_offset = 0;
  3701. u16 cal_blk_size;
  3702. u16 total_rows;
  3703. u16 remain_bytes;
  3704. u16 data_block_crc;
  3705. u16 buf_size = 12;
  3706. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3707. __func__, action);
  3708. switch (action) {
  3709. case PT_CAL_DATA_SAVE:
  3710. /* Read the size of the CAL block and calculate # rows */
  3711. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3712. if (!tmp_data) {
  3713. rc = -ENOMEM;
  3714. goto exit;
  3715. }
  3716. /*
  3717. * Don't check rc as doing a read size will give a false
  3718. * error on the CRC check.
  3719. */
  3720. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3721. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3722. cal_blk_size = act_trans_len;
  3723. kfree(tmp_data);
  3724. pt_debug(dev, DL_INFO,
  3725. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3726. __func__, cal_cache_len, cal_blk_size);
  3727. /* Safety net to ensure we didn't read incorrect size */
  3728. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3729. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3730. __func__);
  3731. rc = 1;
  3732. goto exit;
  3733. }
  3734. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3735. if (cal_blk_size != cal_cache_len) {
  3736. kfree(cal_cache_data);
  3737. cal_cache_data = kzalloc(cal_blk_size + 2,
  3738. GFP_KERNEL);
  3739. if (!cal_cache_data) {
  3740. rc = -ENOMEM;
  3741. goto exit;
  3742. }
  3743. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3744. __func__);
  3745. }
  3746. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3747. /* Calculate how many rows [0-n] (PIP Transactions) */
  3748. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3749. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3750. /* Add row if we have a last partial row */
  3751. if (remain_bytes > 0)
  3752. total_rows++;
  3753. pt_debug(dev, DL_INFO,
  3754. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3755. __func__, cal_blk_size, total_rows, remain_bytes);
  3756. /* Read all rows unless an error occurs */
  3757. rc = 0;
  3758. while (rc == 0 && row_number <= total_rows) {
  3759. act_trans_len = 0;
  3760. if (remain_bytes > 0 && row_number == total_rows)
  3761. transfer_size = remain_bytes;
  3762. else
  3763. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3764. rc = pt_pip1_read_data_block_(cd, row_number,
  3765. transfer_size, PT_CAL_EBID,
  3766. &act_trans_len,
  3767. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3768. &data_block_crc);
  3769. if (rc) {
  3770. /* Error occurred, exit loop */
  3771. cal_size = 0;
  3772. break;
  3773. }
  3774. pt_debug(dev, DL_INFO,
  3775. "%s: CAL read rc=%d actual read len=%d\n",
  3776. __func__, rc, act_trans_len);
  3777. byte_offset += act_trans_len;
  3778. cal_size = byte_offset;
  3779. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3780. pt_pr_buf(dev, DL_INFO,
  3781. &cal_cache_data[byte_offset - act_trans_len],
  3782. act_trans_len, prefix);
  3783. row_number++;
  3784. }
  3785. if (cal_size > 0) {
  3786. /* Save a CRC of the chip info the CAL was saved from */
  3787. calc_id_crc = crc_ccitt_calculate(
  3788. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3789. cal_cache_chip_id = calc_id_crc;
  3790. cal_cache_len = cal_size;
  3791. pt_debug(dev, DL_INFO,
  3792. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3793. __func__, calc_id_crc, cal_size);
  3794. }
  3795. *size = cal_size;
  3796. *crc = calc_id_crc;
  3797. break;
  3798. case PT_CAL_DATA_RESTORE:
  3799. cal_size = cal_cache_len;
  3800. while ((rc == 0) && (byte_offset < cal_size)) {
  3801. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3802. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3803. else
  3804. transfer_size = cal_size - byte_offset;
  3805. rc = pt_pip1_write_data_block_(cd, row_number,
  3806. transfer_size, PT_CAL_EBID,
  3807. &cal_cache_data[byte_offset],
  3808. (u8 *)pt_data_block_security_key,
  3809. &act_trans_len);
  3810. byte_offset += act_trans_len;
  3811. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3812. __func__, byte_offset);
  3813. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3814. pt_pr_buf(dev, DL_INFO,
  3815. &cal_cache_data[byte_offset - act_trans_len],
  3816. act_trans_len, prefix);
  3817. if ((byte_offset > cal_size) ||
  3818. (act_trans_len != transfer_size))
  3819. rc = -EIO;
  3820. row_number++;
  3821. }
  3822. *size = byte_offset;
  3823. *crc = cal_cache_chip_id;
  3824. break;
  3825. case PT_CAL_DATA_CLEAR:
  3826. if (cal_cache_data)
  3827. memset(&cal_cache_data[0], 0, cal_cache_len);
  3828. cal_cache_len = 0;
  3829. cal_cache_chip_id = 0;
  3830. *size = 0;
  3831. *crc = 0;
  3832. break;
  3833. case PT_CAL_DATA_INFO:
  3834. default:
  3835. *size = cal_cache_len;
  3836. *crc = cal_cache_chip_id;
  3837. pt_debug(dev, DL_INFO,
  3838. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3839. __func__, cal_cache_chip_id,
  3840. cal_cache_len);
  3841. break;
  3842. }
  3843. exit:
  3844. pt_debug(dev, DL_INFO,
  3845. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3846. __func__, rc, *crc, *size);
  3847. return rc;
  3848. }
  3849. /*******************************************************************************
  3850. * FUNCTION: pt_pip_run_selftest_
  3851. *
  3852. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3853. * to execute a FW built in self test
  3854. *
  3855. * RETURN:
  3856. * 0 = success
  3857. * !0 = failure
  3858. *
  3859. * PARAMETERS:
  3860. * *cd - pointer to core data
  3861. * test_id - enumerated test ID to run
  3862. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3863. * *status - pointer to store the read response status
  3864. * *summary_results - pointer to store the results summary
  3865. * *results_available - pointer to store if results are available
  3866. *****************************************************************************/
  3867. static int pt_pip_run_selftest_(
  3868. struct pt_core_data *cd, u8 test_id,
  3869. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3870. u8 *results_available)
  3871. {
  3872. int rc = 0;
  3873. u8 write_buf[2];
  3874. struct pt_hid_output hid_output = {
  3875. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3876. .write_length = 2,
  3877. .write_buf = write_buf,
  3878. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3879. };
  3880. write_buf[0] = test_id;
  3881. write_buf[1] = write_idacs_to_flash;
  3882. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3883. hid_output.write_length = 1;
  3884. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3885. if (rc)
  3886. return rc;
  3887. if (status)
  3888. *status = cd->response_buf[5];
  3889. if (summary_result)
  3890. *summary_result = cd->response_buf[6];
  3891. /* results_available only available before PIP 1.03 */
  3892. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3893. if (results_available)
  3894. *results_available = cd->response_buf[7];
  3895. }
  3896. return rc;
  3897. }
  3898. /*******************************************************************************
  3899. * FUNCTION: pt_pip_run_selftest
  3900. *
  3901. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3902. * an exclusive access to the DUT.
  3903. *
  3904. * RETURN:
  3905. * 0 = success
  3906. * !0 = failure
  3907. *
  3908. * PARAMETERS:
  3909. * *cd - pointer to core data
  3910. * test_id - enumerated test ID to run
  3911. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3912. * *status - pointer to store the read response status
  3913. * *summary_results - pointer to store the results summary
  3914. * *results_available - pointer to store if results are available
  3915. ******************************************************************************/
  3916. static int pt_pip_run_selftest(
  3917. struct pt_core_data *cd, u8 test_id,
  3918. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3919. u8 *results_available)
  3920. {
  3921. int rc;
  3922. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3923. if (rc < 0) {
  3924. pt_debug(cd->dev, DL_ERROR,
  3925. "%s: fail get exclusive ex=%p own=%p\n",
  3926. __func__, cd->exclusive_dev, cd->dev);
  3927. return rc;
  3928. }
  3929. rc = pt_pip_run_selftest_(cd, test_id,
  3930. write_idacs_to_flash, status, summary_result,
  3931. results_available);
  3932. if (release_exclusive(cd, cd->dev) < 0)
  3933. pt_debug(cd->dev, DL_ERROR,
  3934. "%s: fail to release exclusive\n", __func__);
  3935. return rc;
  3936. }
  3937. /*******************************************************************************
  3938. * FUNCTION: _pt_request_pip_run_selftest
  3939. *
  3940. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3941. * calls to the protected or unprotected call to pt_pip_run_selftest
  3942. *
  3943. * RETURN:
  3944. * 0 = success
  3945. * !0 = failure
  3946. *
  3947. * PARAMETERS:
  3948. * *dev - pointer to device structure
  3949. * protect - flag to call protected or non-protected
  3950. * test_id - enumerated test ID to run
  3951. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3952. * *status - pointer to store the read response status
  3953. * *summary_results - pointer to store the results summary
  3954. * *results_available - pointer to store if results are available
  3955. ******************************************************************************/
  3956. static int _pt_request_pip_run_selftest(struct device *dev,
  3957. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3958. u8 *summary_result, u8 *results_available)
  3959. {
  3960. struct pt_core_data *cd = dev_get_drvdata(dev);
  3961. if (protect)
  3962. return pt_pip_run_selftest(cd, test_id,
  3963. write_idacs_to_flash, status, summary_result,
  3964. results_available);
  3965. return pt_pip_run_selftest_(cd, test_id,
  3966. write_idacs_to_flash, status, summary_result,
  3967. results_available);
  3968. }
  3969. /*******************************************************************************
  3970. * FUNCTION: _pt_pip_get_selftest_result_
  3971. *
  3972. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3973. * to retrieve the self test results from the self test already executed
  3974. *
  3975. * RETURN:
  3976. * 0 = success
  3977. * !0 = failure
  3978. *
  3979. * PARAMETERS:
  3980. * *cd - pointer to core data
  3981. * read_offset - read pointer offset
  3982. * read_length - length of data to read
  3983. * test_id - enumerated test ID to read selftest results from
  3984. * *status - pointer to store the read response status
  3985. * *actual_read_len - pointer to store data length actually read
  3986. * *status - pointer to where the cmd response statas is stored
  3987. ******************************************************************************/
  3988. static int pt_pip_get_selftest_result_(
  3989. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3990. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3991. {
  3992. int rc = 0;
  3993. u16 total_read_len = 0;
  3994. u16 read_len;
  3995. u16 off_buf = 0;
  3996. u8 write_buf[5];
  3997. u8 read_test_id;
  3998. bool repeat;
  3999. struct pt_hid_output hid_output = {
  4000. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4001. .write_length = 5,
  4002. .write_buf = write_buf,
  4003. };
  4004. /*
  4005. * Do not repeat reading for Auto Shorts test
  4006. * when PIP version < 1.3
  4007. */
  4008. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4009. || test_id != PT_ST_ID_AUTOSHORTS;
  4010. again:
  4011. write_buf[0] = LOW_BYTE(read_offset);
  4012. write_buf[1] = HI_BYTE(read_offset);
  4013. write_buf[2] = LOW_BYTE(read_length);
  4014. write_buf[3] = HI_BYTE(read_length);
  4015. write_buf[4] = test_id;
  4016. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4017. if (rc)
  4018. return rc;
  4019. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4020. goto set_status;
  4021. read_test_id = cd->response_buf[6];
  4022. if (read_test_id != test_id)
  4023. return -EPROTO;
  4024. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4025. if (read_len && data) {
  4026. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4027. total_read_len += read_len;
  4028. if (repeat && read_len < read_length) {
  4029. read_offset += read_len;
  4030. off_buf += read_len;
  4031. read_length -= read_len;
  4032. goto again;
  4033. }
  4034. }
  4035. if (actual_read_len)
  4036. *actual_read_len = total_read_len;
  4037. set_status:
  4038. if (status)
  4039. *status = cd->response_buf[5];
  4040. return rc;
  4041. }
  4042. /*******************************************************************************
  4043. * FUNCTION: _pt_pip_get_selftest_result
  4044. *
  4045. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4046. * access to the DUT
  4047. *
  4048. * RETURN:
  4049. * 0 = success
  4050. * !0 = failure
  4051. *
  4052. * PARAMETERS:
  4053. * *cd - pointer to core data
  4054. * read_offset - read pointer offset
  4055. * read_length - length of data to read
  4056. * test_id - enumerated test ID to read selftest results from
  4057. * *status - pointer to store the read response status
  4058. * *actual_read_len - pointer to store data length actually read
  4059. * *status - pointer to where the cmd response statas is stored
  4060. ******************************************************************************/
  4061. static int pt_pip_get_selftest_result(
  4062. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4063. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4064. {
  4065. int rc;
  4066. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4067. if (rc < 0) {
  4068. pt_debug(cd->dev, DL_ERROR,
  4069. "%s: fail get exclusive ex=%p own=%p\n",
  4070. __func__, cd->exclusive_dev, cd->dev);
  4071. return rc;
  4072. }
  4073. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4074. read_length, test_id, status, actual_read_len, data);
  4075. if (release_exclusive(cd, cd->dev) < 0)
  4076. pt_debug(cd->dev, DL_ERROR,
  4077. "%s: fail to release exclusive\n", __func__);
  4078. return rc;
  4079. }
  4080. /*******************************************************************************
  4081. * FUNCTION: _pt_request_pip_get_selftest_result
  4082. *
  4083. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4084. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4085. *
  4086. * RETURN:
  4087. * 0 = success
  4088. * !0 = failure
  4089. *
  4090. * PARAMETERS:
  4091. * *dev - pointer to device structure
  4092. * protect - flag to call protected or non-protected
  4093. * read_offset - read pointer offset
  4094. * read_length - length of data to read
  4095. * test_id - enumerated test ID to read selftest results from
  4096. * *status - pointer to store the read response status
  4097. * *actual_read_len - pointer to store data length actually read
  4098. * *data - pointer to where the data read is stored
  4099. ******************************************************************************/
  4100. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4101. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4102. u8 *status, u16 *actual_read_len, u8 *data)
  4103. {
  4104. struct pt_core_data *cd = dev_get_drvdata(dev);
  4105. if (protect)
  4106. return pt_pip_get_selftest_result(cd, read_offset,
  4107. read_length, test_id, status, actual_read_len,
  4108. data);
  4109. return pt_pip_get_selftest_result_(cd, read_offset,
  4110. read_length, test_id, status, actual_read_len,
  4111. data);
  4112. }
  4113. /*******************************************************************************
  4114. * FUNCTION: _pt_pip_load_self_test_param
  4115. *
  4116. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4117. * to load parameters needed by a self test
  4118. *
  4119. * RETURN:
  4120. * 0 = success
  4121. * !0 = failure
  4122. *
  4123. * PARAMETERS:
  4124. * *cd - pointer to core data
  4125. * self_test_id - enumerated test ID for which the parmeters belong
  4126. * load_offset - mem offset to where to load parameters
  4127. * load_length - length of parameter data to load
  4128. * *parameters - pointer to list of parameter data
  4129. * *status - pointer to store the response status
  4130. * *ret_test_id - pointer to returned test id the parameters were stored
  4131. * *act_load_len - pointer to store the actual load length that was writen
  4132. ******************************************************************************/
  4133. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4134. u8 self_test_id, u16 load_offset, u16 load_length,
  4135. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4136. {
  4137. int rc = 0;
  4138. int i;
  4139. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4140. struct pt_hid_output hid_output = {
  4141. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4142. .write_length = 5 + load_length,
  4143. .write_buf = write_buf,
  4144. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4145. };
  4146. write_buf[0] = LOW_BYTE(load_offset);
  4147. write_buf[1] = HI_BYTE(load_offset);
  4148. write_buf[2] = LOW_BYTE(load_length);
  4149. write_buf[3] = HI_BYTE(load_length);
  4150. write_buf[4] = self_test_id;
  4151. for (i = 0; i < load_length; i++)
  4152. write_buf[i + 5] = parameters[i];
  4153. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4154. if (rc)
  4155. return rc;
  4156. if (status)
  4157. *status = cd->response_buf[5];
  4158. if (ret_test_id)
  4159. *ret_test_id = cd->response_buf[6];
  4160. if (act_load_len)
  4161. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4162. return rc;
  4163. }
  4164. /*******************************************************************************
  4165. * FUNCTION: pt_pip_load_self_test_param
  4166. *
  4167. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4168. * access to the DUT.
  4169. *
  4170. * RETURN:
  4171. * 0 = success
  4172. * !0 = failure
  4173. *
  4174. * PARAMETERS:
  4175. * *cd - pointer to core data
  4176. * self_test_id - enumerated test ID for which the parmeters belong
  4177. * load_offset - mem offset to where to load parameters
  4178. * load_length - length of parameter data to load
  4179. * *parameters - pointer to list of parameter data
  4180. * *status - pointer to store the response status
  4181. * *ret_test_id - pointer to returned test id the parameters were stored
  4182. * *act_load_len - pointer to store the actual load length that was writen
  4183. ******************************************************************************/
  4184. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4185. u8 self_test_id, u16 load_offset, u16 load_length,
  4186. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4187. {
  4188. int rc;
  4189. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4190. if (rc < 0) {
  4191. pt_debug(cd->dev, DL_ERROR,
  4192. "%s: fail get exclusive ex=%p own=%p\n",
  4193. __func__, cd->exclusive_dev, cd->dev);
  4194. return rc;
  4195. }
  4196. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4197. load_length, parameters, status, ret_test_id, act_load_len);
  4198. if (release_exclusive(cd, cd->dev) < 0)
  4199. pt_debug(cd->dev, DL_ERROR,
  4200. "%s: fail to release exclusive\n", __func__);
  4201. return rc;
  4202. }
  4203. /*******************************************************************************
  4204. * FUNCTION: _pt_request_pip_load_self_test_param
  4205. *
  4206. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4207. * calls to the protected or unprotected call to
  4208. * pt_pip_load_self_test_param
  4209. *
  4210. * RETURN:
  4211. * 0 = success
  4212. * !0 = failure
  4213. *
  4214. * PARAMETERS:
  4215. * *dev - pointer to device structure
  4216. * protect - flag to call protected or non-protected
  4217. * self_test_id - enumerated test ID for which the parmeters belong
  4218. * load_offset - mem offset to where to load parameters
  4219. * load_length - length of parameter data to load
  4220. * *parameters - pointer to list of parameter data
  4221. * *status - pointer to store the response status
  4222. * *ret_test_id - pointer to returned test id the parameters were stored
  4223. * *act_load_len - pointer to store the actual load length that was writen
  4224. ******************************************************************************/
  4225. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4226. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4227. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4228. {
  4229. struct pt_core_data *cd = dev_get_drvdata(dev);
  4230. if (protect)
  4231. return pt_pip_load_self_test_param(cd, self_test_id,
  4232. load_offset, load_length, parameters, status, ret_test_id,
  4233. act_load_len);
  4234. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4235. load_length, parameters, status, ret_test_id, act_load_len);
  4236. }
  4237. /*******************************************************************************
  4238. * FUNCTION: pt_pip_calibrate_ext_
  4239. *
  4240. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4241. * for the response
  4242. *
  4243. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4244. *
  4245. * RETURN:
  4246. * 0 = success
  4247. * !0 = failure
  4248. *
  4249. * PARAMETERS:
  4250. * *cd - pointer to core data
  4251. * *cal_data - pointer to extended calibration data structure
  4252. * *status - pointer to where the command response status is stored
  4253. ******************************************************************************/
  4254. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4255. struct pt_cal_ext_data *cal_data, u8 *status)
  4256. {
  4257. int rc = 0;
  4258. int write_length = 4;
  4259. u8 write_buf[4];
  4260. u16 size = 0;
  4261. unsigned short crc = 0;
  4262. struct pt_hid_output hid_output = {
  4263. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4264. .write_length = write_length,
  4265. .write_buf = write_buf,
  4266. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4267. };
  4268. if (cal_data == NULL)
  4269. return -EINVAL;
  4270. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4271. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4272. if (rc)
  4273. return rc;
  4274. if (status)
  4275. *status = cd->response_buf[5];
  4276. /*
  4277. * When doing a calibration on a flashless DUT, save CAL data in
  4278. * the TTDL cache on any successful calibration
  4279. */
  4280. if (*status == 0 && cd->cal_cache_in_host) {
  4281. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4282. __func__);
  4283. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4284. &size, &crc);
  4285. if (rc)
  4286. pt_debug(cd->dev, DL_ERROR,
  4287. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4288. else
  4289. pt_debug(cd->dev, DL_INFO,
  4290. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4291. __func__, crc, size);
  4292. }
  4293. return 0;
  4294. }
  4295. /*******************************************************************************
  4296. * FUNCTION: pt_pip_calibrate_ext
  4297. *
  4298. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4299. * DUT
  4300. *
  4301. * RETURN:
  4302. * 0 = success
  4303. * !0 = failure
  4304. *
  4305. * PARAMETERS:
  4306. * *cd - pointer to core data
  4307. * *cal_data - pointer to extended calibration data structure
  4308. * *status - pointer to where the command response status is stored
  4309. ******************************************************************************/
  4310. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4311. struct pt_cal_ext_data *cal_data, u8 *status)
  4312. {
  4313. int rc;
  4314. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4315. if (rc < 0) {
  4316. pt_debug(cd->dev, DL_ERROR,
  4317. "%s: fail get exclusive ex=%p own=%p\n",
  4318. __func__, cd->exclusive_dev, cd->dev);
  4319. return rc;
  4320. }
  4321. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4322. if (release_exclusive(cd, cd->dev) < 0)
  4323. pt_debug(cd->dev, DL_ERROR,
  4324. "%s: fail to release exclusive\n", __func__);
  4325. return rc;
  4326. }
  4327. /*******************************************************************************
  4328. * FUNCTION: _pt_request_pip_calibrate_ext
  4329. *
  4330. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4331. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4332. *
  4333. * RETURN:
  4334. * 0 = success
  4335. * !0 = failure
  4336. *
  4337. * PARAMETERS:
  4338. * *dev - pointer to device structure
  4339. * protect - flag to call protected or non-protected
  4340. * *cal_data - pointer to extended calibration data structure
  4341. * *status - pointer to where the command response status is stored
  4342. ******************************************************************************/
  4343. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4344. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4345. {
  4346. struct pt_core_data *cd = dev_get_drvdata(dev);
  4347. if (protect)
  4348. return pt_pip_calibrate_ext(cd, cal_data, status);
  4349. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4350. }
  4351. /*******************************************************************************
  4352. * FUNCTION: pt_pip_calibrate_idacs_
  4353. *
  4354. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4355. * for the response
  4356. *
  4357. * RETURN:
  4358. * 0 = success
  4359. * !0 = failure
  4360. *
  4361. * PARAMETERS:
  4362. * *cd - pointer to core data
  4363. * mode - sense mode to calibrate (0-5)
  4364. * *status - pointer to where the command response status is stored
  4365. ******************************************************************************/
  4366. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4367. u8 mode, u8 *status)
  4368. {
  4369. int rc = 0;
  4370. int write_length = 1;
  4371. u8 write_buf[1];
  4372. u8 cmd_offset = 0;
  4373. struct pt_hid_output hid_output = {
  4374. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4375. .write_length = write_length,
  4376. .write_buf = write_buf,
  4377. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4378. };
  4379. write_buf[cmd_offset++] = mode;
  4380. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4381. if (rc)
  4382. return rc;
  4383. *status = cd->response_buf[5];
  4384. if (*status)
  4385. return -EINVAL;
  4386. return 0;
  4387. }
  4388. /*******************************************************************************
  4389. * FUNCTION: pt_pip_calibrate_idacs
  4390. *
  4391. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4392. * access to the DUT
  4393. *
  4394. * RETURN:
  4395. * 0 = success
  4396. * !0 = failure
  4397. *
  4398. * PARAMETERS:
  4399. * *cd - pointer to core data
  4400. * mode - sense mode to calibrate (0-5)
  4401. * *status - pointer to where the command response status is stored
  4402. ******************************************************************************/
  4403. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4404. u8 mode, u8 *status)
  4405. {
  4406. int rc;
  4407. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4408. if (rc < 0) {
  4409. pt_debug(cd->dev, DL_ERROR,
  4410. "%s: fail get exclusive ex=%p own=%p\n",
  4411. __func__, cd->exclusive_dev, cd->dev);
  4412. return rc;
  4413. }
  4414. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4415. if (release_exclusive(cd, cd->dev) < 0)
  4416. pt_debug(cd->dev, DL_ERROR,
  4417. "%s: fail to release exclusive\n", __func__);
  4418. return rc;
  4419. }
  4420. /*******************************************************************************
  4421. * FUNCTION: _pt_request_pip_calibrate_idacs
  4422. *
  4423. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4424. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4425. *
  4426. * RETURN:
  4427. * 0 = success
  4428. * !0 = failure
  4429. *
  4430. * PARAMETERS:
  4431. * *dev - pointer to device structure
  4432. * protect - flag to call protected or non-protected
  4433. * mode - sense mode to calibrate (0-5)
  4434. * *status - pointer to where the command response status is stored
  4435. ******************************************************************************/
  4436. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4437. int protect, u8 mode, u8 *status)
  4438. {
  4439. struct pt_core_data *cd = dev_get_drvdata(dev);
  4440. if (protect)
  4441. return pt_pip_calibrate_idacs(cd, mode, status);
  4442. return pt_pip_calibrate_idacs_(cd, mode, status);
  4443. }
  4444. /*******************************************************************************
  4445. * FUNCTION: pt_hid_output_initialize_baselines_
  4446. *
  4447. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4448. * waiting for the response.
  4449. *
  4450. * RETURN:
  4451. * 0 = success
  4452. * !0 = failure
  4453. *
  4454. * PARAMETERS:
  4455. * *cd - pointer to core data
  4456. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4457. * each or together with a single command.
  4458. * *status - pointer to where the command response status is stored
  4459. ******************************************************************************/
  4460. static int pt_hid_output_initialize_baselines_(
  4461. struct pt_core_data *cd, u8 test_id, u8 *status)
  4462. {
  4463. int rc = 0;
  4464. int write_length = 1;
  4465. u8 write_buf[1];
  4466. u8 cmd_offset = 0;
  4467. struct pt_hid_output hid_output = {
  4468. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4469. .write_length = write_length,
  4470. .write_buf = write_buf,
  4471. };
  4472. write_buf[cmd_offset++] = test_id;
  4473. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4474. if (rc)
  4475. return rc;
  4476. *status = cd->response_buf[5];
  4477. if (*status)
  4478. return -EINVAL;
  4479. return rc;
  4480. }
  4481. /*******************************************************************************
  4482. * FUNCTION: pt_hid_output_initialize_baselines
  4483. *
  4484. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4485. * access to the DUT
  4486. *
  4487. * RETURN:
  4488. * 0 = success
  4489. * !0 = failure
  4490. *
  4491. * PARAMETERS:
  4492. * *cd - pointer to core data
  4493. * test_id - enumerated ID against which to initialize the baseline
  4494. * *status - pointer to where the command response status is stored
  4495. ******************************************************************************/
  4496. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4497. u8 test_id, u8 *status)
  4498. {
  4499. int rc;
  4500. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4501. if (rc < 0) {
  4502. pt_debug(cd->dev, DL_ERROR,
  4503. "%s: fail get exclusive ex=%p own=%p\n",
  4504. __func__, cd->exclusive_dev, cd->dev);
  4505. return rc;
  4506. }
  4507. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4508. if (release_exclusive(cd, cd->dev) < 0)
  4509. pt_debug(cd->dev, DL_ERROR,
  4510. "%s: fail to release exclusive\n", __func__);
  4511. return rc;
  4512. }
  4513. /*******************************************************************************
  4514. * FUNCTION: _pt_request_pip_initialize_baselines
  4515. *
  4516. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4517. * calls to the protected or unprotected call to
  4518. * pt_pip_initialize_baselines
  4519. *
  4520. * RETURN:
  4521. * 0 = success
  4522. * !0 = failure
  4523. *
  4524. * PARAMETERS:
  4525. * *dev - pointer to device structure
  4526. * protect - flag to call protected or non-protected
  4527. * test_id - enumerated ID against which to initialize the baseline
  4528. * *status - pointer to where the command response status is stored
  4529. ******************************************************************************/
  4530. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4531. int protect, u8 test_id, u8 *status)
  4532. {
  4533. struct pt_core_data *cd = dev_get_drvdata(dev);
  4534. if (protect)
  4535. return pt_hid_output_initialize_baselines(cd, test_id,
  4536. status);
  4537. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4538. }
  4539. /*******************************************************************************
  4540. * FUNCTION: pt_hid_output_exec_panel_scan_
  4541. *
  4542. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4543. * the response
  4544. *
  4545. * RETURN:
  4546. * 0 = success
  4547. * !0 = failure
  4548. *
  4549. * PARAMETERS:
  4550. * *cd - pointer to core data
  4551. ******************************************************************************/
  4552. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4553. {
  4554. struct pt_hid_output hid_output = {
  4555. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4556. };
  4557. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4558. }
  4559. /*******************************************************************************
  4560. * FUNCTION: pt_hid_output_exec_panel_scan
  4561. *
  4562. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4563. * access to the DUT.
  4564. *
  4565. * RETURN:
  4566. * 0 = success
  4567. * !0 = failure
  4568. *
  4569. * PARAMETERS:
  4570. * *cd - pointer to core data
  4571. ******************************************************************************/
  4572. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4573. {
  4574. int rc;
  4575. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4576. if (rc < 0) {
  4577. pt_debug(cd->dev, DL_ERROR,
  4578. "%s: fail get exclusive ex=%p own=%p\n",
  4579. __func__, cd->exclusive_dev, cd->dev);
  4580. return rc;
  4581. }
  4582. rc = pt_hid_output_exec_panel_scan_(cd);
  4583. if (release_exclusive(cd, cd->dev) < 0)
  4584. pt_debug(cd->dev, DL_ERROR,
  4585. "%s: fail to release exclusive\n", __func__);
  4586. return rc;
  4587. }
  4588. /*******************************************************************************
  4589. * FUNCTION: pt_pip2_exec_panel_scan_
  4590. *
  4591. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4592. * the response
  4593. *
  4594. * RETURN:
  4595. * 0 = success
  4596. * !0 = failure
  4597. *
  4598. * PARAMETERS:
  4599. * *cd - pointer to core data
  4600. * scan_type - type of panel scan to perform (PIP2 only)
  4601. ******************************************************************************/
  4602. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4603. {
  4604. int rc = 0;
  4605. u8 data[2];
  4606. u8 read_buf[10];
  4607. u16 actual_read_len;
  4608. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4609. __func__, scan_type);
  4610. data[0] = scan_type;
  4611. rc = _pt_request_pip2_send_cmd(cd->dev,
  4612. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4613. data, 1, read_buf, &actual_read_len);
  4614. if (rc) {
  4615. pt_debug(cd->dev, DL_ERROR,
  4616. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4617. __func__, scan_type, rc);
  4618. }
  4619. return rc;
  4620. }
  4621. /*******************************************************************************
  4622. * FUNCTION: pt_pip2_exec_panel_scan
  4623. *
  4624. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4625. * access to the DUT.
  4626. *
  4627. * RETURN:
  4628. * 0 = success
  4629. * !0 = failure
  4630. *
  4631. * PARAMETERS:
  4632. * *cd - pointer to core data
  4633. * scan_type - type of panel scan to perform (PIP2 only)
  4634. ******************************************************************************/
  4635. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4636. {
  4637. int rc;
  4638. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4639. if (rc < 0) {
  4640. pt_debug(cd->dev, DL_ERROR,
  4641. "%s: fail get exclusive ex=%p own=%p\n",
  4642. __func__, cd->exclusive_dev, cd->dev);
  4643. return rc;
  4644. }
  4645. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4646. if (release_exclusive(cd, cd->dev) < 0)
  4647. pt_debug(cd->dev, DL_ERROR,
  4648. "%s: fail to release exclusive\n", __func__);
  4649. return rc;
  4650. }
  4651. /*******************************************************************************
  4652. * FUNCTION: _pt_request_pip_exec_panel_scan
  4653. *
  4654. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4655. * calls to the protected or unprotected call to
  4656. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4657. *
  4658. * RETURN:
  4659. * 0 = success
  4660. * !0 = failure
  4661. *
  4662. * PARAMETERS:
  4663. * *dev - pointer to device structure
  4664. * protect - flag to call protected or non-protected
  4665. * scan_type - type of panel scan to perform (PIP2 only)
  4666. ******************************************************************************/
  4667. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4668. int protect, u8 scan_type)
  4669. {
  4670. struct pt_core_data *cd = dev_get_drvdata(dev);
  4671. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4672. if (protect)
  4673. return pt_pip2_exec_panel_scan(cd, scan_type);
  4674. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4675. }
  4676. if (protect)
  4677. return pt_hid_output_exec_panel_scan(cd);
  4678. return pt_hid_output_exec_panel_scan_(cd);
  4679. }
  4680. /*******************************************************************************
  4681. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4682. *
  4683. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4684. * to retrieve the specified data type for a the last successful Execute
  4685. * Panel Scan command.
  4686. *
  4687. * RETURN:
  4688. * 0 = success
  4689. * !0 = failure
  4690. *
  4691. * PARAMETERS:
  4692. * *dev - pointer to device structure
  4693. * protect - flag to call protected or non-protected
  4694. * read_offset - read pointer offset
  4695. * read_count - length of data to read
  4696. * data_id - enumerated test ID to read selftest results from
  4697. * *response - pointer to store the read response status
  4698. * *config - pointer to store config data
  4699. * *actual_read_len - pointer to store data length actually read
  4700. * *read_buf - pointer to the read buffer
  4701. ******************************************************************************/
  4702. static int pt_hid_output_retrieve_panel_scan_(
  4703. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4704. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4705. u8 *read_buf)
  4706. {
  4707. int status;
  4708. u8 read_data_id;
  4709. int rc = 0;
  4710. int write_length = 5;
  4711. u8 write_buf[5];
  4712. u8 cmd_offset = 0;
  4713. u8 data_elem_size;
  4714. int size;
  4715. int data_size;
  4716. struct pt_hid_output hid_output = {
  4717. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4718. .write_length = write_length,
  4719. .write_buf = write_buf,
  4720. };
  4721. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4722. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4723. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4724. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4725. write_buf[cmd_offset++] = data_id;
  4726. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4727. if (rc)
  4728. return rc;
  4729. status = cd->response_buf[5];
  4730. if (status)
  4731. return -EINVAL;
  4732. read_data_id = cd->response_buf[6];
  4733. if (read_data_id != data_id)
  4734. return -EPROTO;
  4735. size = get_unaligned_le16(&cd->response_buf[0]);
  4736. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4737. *config = cd->response_buf[9];
  4738. data_elem_size = *config & 0x07;
  4739. data_size = *actual_read_len * data_elem_size;
  4740. if (read_buf)
  4741. memcpy(read_buf, &cd->response_buf[10], data_size);
  4742. if (response)
  4743. memcpy(response, cd->response_buf, size);
  4744. return rc;
  4745. }
  4746. /*******************************************************************************
  4747. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4748. *
  4749. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4750. * access to the DUT.
  4751. *
  4752. * RETURN:
  4753. * 0 = success
  4754. * !0 = failure
  4755. *
  4756. * PARAMETERS:
  4757. * *dev - pointer to device structure
  4758. * protect - flag to call protected or non-protected
  4759. * read_offset - read pointer offset
  4760. * read_count - length of data to read
  4761. * data_id - enumerated test ID to read selftest results from
  4762. * *response - pointer to store the read response status
  4763. * *config - pointer to store config data
  4764. * *actual_read_len - pointer to store data length actually read
  4765. * *read_buf - pointer to the read buffer
  4766. ******************************************************************************/
  4767. static int pt_hid_output_retrieve_panel_scan(
  4768. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4769. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4770. u8 *read_buf)
  4771. {
  4772. int rc;
  4773. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4774. if (rc < 0) {
  4775. pt_debug(cd->dev, DL_ERROR,
  4776. "%s: fail get exclusive ex=%p own=%p\n",
  4777. __func__, cd->exclusive_dev, cd->dev);
  4778. return rc;
  4779. }
  4780. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4781. read_count, data_id, response, config,
  4782. actual_read_len, read_buf);
  4783. if (release_exclusive(cd, cd->dev) < 0)
  4784. pt_debug(cd->dev, DL_ERROR,
  4785. "%s: fail to release exclusive\n", __func__);
  4786. return rc;
  4787. }
  4788. /*******************************************************************************
  4789. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4790. *
  4791. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4792. * calls to the protected or unprotected call to
  4793. * pt_hid_output_retrieve_panel_scan
  4794. *
  4795. * RETURN:
  4796. * 0 = success
  4797. * !0 = failure
  4798. *
  4799. * PARAMETERS:
  4800. * *dev - pointer to device structure
  4801. * protect - flag to call protected or non-protected
  4802. * read_offset - read pointer offset
  4803. * read_count - length of data to read
  4804. * data_id - enumerated test ID to read selftest results from
  4805. * *response - pointer to store the read response status
  4806. * *config - pointer to store config data
  4807. * *actual_read_len - pointer to store data length actually read
  4808. * *read_buf - pointer to the read buffer
  4809. ******************************************************************************/
  4810. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4811. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4812. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4813. {
  4814. struct pt_core_data *cd = dev_get_drvdata(dev);
  4815. if (protect)
  4816. return pt_hid_output_retrieve_panel_scan(cd,
  4817. read_offset, read_count, data_id, response,
  4818. config, actual_read_len, read_buf);
  4819. return pt_hid_output_retrieve_panel_scan_(cd,
  4820. read_offset, read_count, data_id, response,
  4821. config, actual_read_len, read_buf);
  4822. }
  4823. /*******************************************************************************
  4824. * FUNCTION: _pt_request_pip_user_cmd
  4825. *
  4826. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4827. * calls to the protected or unprotected call to
  4828. * pt_hid_output_user_cmd
  4829. *
  4830. * RETURN:
  4831. * 0 = success
  4832. * !0 = failure
  4833. *
  4834. * PARAMETERS:
  4835. * *dev - pointer to device structure
  4836. * protect - flag to call protected or non-protected
  4837. * read_len - length of data to read
  4838. * *read_buf - pointer to store read data
  4839. * write_len - length of data to write
  4840. * *write_buf - pointer to buffer to write
  4841. * *actual_read_len - pointer to store data length actually read
  4842. ******************************************************************************/
  4843. static int _pt_request_pip_user_cmd(struct device *dev,
  4844. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4845. u8 *write_buf, u16 *actual_read_len)
  4846. {
  4847. struct pt_core_data *cd = dev_get_drvdata(dev);
  4848. if (protect)
  4849. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4850. write_len, write_buf, actual_read_len);
  4851. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4852. write_len, write_buf, actual_read_len);
  4853. }
  4854. /*******************************************************************************
  4855. * FUNCTION: pt_hid_output_bl_get_information_
  4856. *
  4857. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4858. * DUT to retrieve bootloader version and chip identification information.
  4859. *
  4860. * RETURN:
  4861. * 0 = success
  4862. * !0 = failure
  4863. *
  4864. * PARAMETERS:
  4865. * *cd - pointer to core data
  4866. * *return_data - pointer to store the return data
  4867. *****************************************************************************/
  4868. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4869. u8 *return_data)
  4870. {
  4871. int rc;
  4872. int data_len;
  4873. struct pt_hid_output hid_output = {
  4874. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4875. };
  4876. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4877. if (rc)
  4878. return rc;
  4879. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4880. if (!data_len)
  4881. return -EPROTO;
  4882. memcpy(return_data, &cd->response_buf[8], data_len);
  4883. return 0;
  4884. }
  4885. /*******************************************************************************
  4886. * FUNCTION: pt_hid_output_bl_get_information
  4887. *
  4888. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4889. * access to the DUT.
  4890. *
  4891. * RETURN:
  4892. * 0 = success
  4893. * !0 = failure
  4894. *
  4895. * PARAMETERS:
  4896. * *cd - pointer to core data
  4897. * *return_data - pointer to store the return data
  4898. ******************************************************************************/
  4899. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4900. u8 *return_data)
  4901. {
  4902. int rc;
  4903. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4904. if (rc < 0) {
  4905. pt_debug(cd->dev, DL_ERROR,
  4906. "%s: fail get exclusive ex=%p own=%p\n",
  4907. __func__, cd->exclusive_dev, cd->dev);
  4908. return rc;
  4909. }
  4910. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4911. if (release_exclusive(cd, cd->dev) < 0)
  4912. pt_debug(cd->dev, DL_ERROR,
  4913. "%s: fail to release exclusive\n", __func__);
  4914. return rc;
  4915. }
  4916. /*******************************************************************************
  4917. * FUNCTION: _pt_request_pip_bl_get_information
  4918. *
  4919. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4920. * calls to the protected or unprotected call to
  4921. * pt_hid_output_bl_get_information
  4922. *
  4923. * RETURN:
  4924. * 0 = success
  4925. * !0 = failure
  4926. *
  4927. * PARAMETERS:
  4928. * *dev - pointer to device structure
  4929. * protect - flag to call protected or non-protected
  4930. * *return_data - pointer to store bl data
  4931. ******************************************************************************/
  4932. static int _pt_request_pip_bl_get_information(struct device *dev,
  4933. int protect, u8 *return_data)
  4934. {
  4935. struct pt_core_data *cd = dev_get_drvdata(dev);
  4936. if (protect)
  4937. return pt_hid_output_bl_get_information(cd, return_data);
  4938. return pt_hid_output_bl_get_information_(cd, return_data);
  4939. }
  4940. /*******************************************************************************
  4941. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4942. *
  4943. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4944. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4945. * and Design Data block in flash and enables the host to execute the Program
  4946. * and Verify Row command to bootload the application image and data.
  4947. *
  4948. * RETURN:
  4949. * 0 = success
  4950. * !0 = failure
  4951. *
  4952. * PARAMETERS:
  4953. * *cd - pointer to core data
  4954. * protect - flag to call protected or non-protected
  4955. * key_size - size of key
  4956. * *key_buf - pointer to key data to allow operation
  4957. * row_size - size of the meta data row
  4958. * *metadata_row_buf - pointer to meta data to write
  4959. ******************************************************************************/
  4960. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4961. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4962. {
  4963. u16 write_length = key_size + row_size;
  4964. u8 *write_buf;
  4965. int rc = 0;
  4966. struct pt_hid_output hid_output = {
  4967. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4968. .write_length = write_length,
  4969. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4970. };
  4971. write_buf = kzalloc(write_length, GFP_KERNEL);
  4972. if (!write_buf)
  4973. return -ENOMEM;
  4974. hid_output.write_buf = write_buf;
  4975. if (key_size)
  4976. memcpy(write_buf, key_buf, key_size);
  4977. if (row_size)
  4978. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4979. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4980. kfree(write_buf);
  4981. return rc;
  4982. }
  4983. /*******************************************************************************
  4984. * FUNCTION: pt_hid_output_bl_initiate_bl
  4985. *
  4986. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4987. * access to the DUT.
  4988. *
  4989. * RETURN:
  4990. * 0 = success
  4991. * !0 = failure
  4992. *
  4993. * PARAMETERS:
  4994. * *cd - pointer to core data
  4995. * key_size - size of key
  4996. * *key_buf - pointer to key data to allow operation
  4997. * row_size - size of the meta data row
  4998. * *metadata_row_buf - pointer to meta data to write
  4999. ******************************************************************************/
  5000. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5001. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5002. {
  5003. int rc;
  5004. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5005. if (rc < 0) {
  5006. pt_debug(cd->dev, DL_ERROR,
  5007. "%s: fail get exclusive ex=%p own=%p\n",
  5008. __func__, cd->exclusive_dev, cd->dev);
  5009. return rc;
  5010. }
  5011. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5012. row_size, metadata_row_buf);
  5013. if (release_exclusive(cd, cd->dev) < 0)
  5014. pt_debug(cd->dev, DL_ERROR,
  5015. "%s: fail to release exclusive\n", __func__);
  5016. return rc;
  5017. }
  5018. /*******************************************************************************
  5019. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5020. *
  5021. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5022. * calls to the protected or unprotected call to
  5023. * pt_hid_output_bl_initiate_bl
  5024. *
  5025. * RETURN:
  5026. * 0 = success
  5027. * !0 = failure
  5028. *
  5029. * PARAMETERS:
  5030. * *dev - pointer to device structure
  5031. * protect - flag to call protected or non-protected
  5032. * key_size - size of key
  5033. * *key_buf - pointer to key data to allow operation
  5034. * row_size - size of the meta data row
  5035. * *metadata_row_buf - pointer to meta data to write
  5036. ******************************************************************************/
  5037. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5038. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5039. u8 *metadata_row_buf)
  5040. {
  5041. struct pt_core_data *cd = dev_get_drvdata(dev);
  5042. if (protect)
  5043. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5044. row_size, metadata_row_buf);
  5045. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5046. row_size, metadata_row_buf);
  5047. }
  5048. /*******************************************************************************
  5049. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5050. *
  5051. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5052. * and program a 128-byte row into the flash, and then verifies written data.
  5053. *
  5054. * RETURN:
  5055. * 0 = success
  5056. * !0 = failure
  5057. *
  5058. * PARAMETERS:
  5059. * *cd - pointer to core data
  5060. * data_len - length of data_buf
  5061. * *data_buf - firmware image to program
  5062. ******************************************************************************/
  5063. static int pt_hid_output_bl_program_and_verify_(
  5064. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5065. {
  5066. struct pt_hid_output hid_output = {
  5067. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5068. .write_length = data_len,
  5069. .write_buf = data_buf,
  5070. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5071. };
  5072. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5073. }
  5074. /*******************************************************************************
  5075. * FUNCTION: pt_hid_output_bl_program_and_verify
  5076. *
  5077. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5078. * access to the DUT.
  5079. *
  5080. * RETURN:
  5081. * 0 = success
  5082. * !0 = failure
  5083. *
  5084. * PARAMETERS:
  5085. * *cd - pointer to core data
  5086. * data_len - length of data_buf
  5087. * *data_buf - firmware image to program
  5088. ******************************************************************************/
  5089. static int pt_hid_output_bl_program_and_verify(
  5090. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5091. {
  5092. int rc;
  5093. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5094. if (rc < 0) {
  5095. pt_debug(cd->dev, DL_ERROR,
  5096. "%s: fail get exclusive ex=%p own=%p\n",
  5097. __func__, cd->exclusive_dev, cd->dev);
  5098. return rc;
  5099. }
  5100. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5101. if (release_exclusive(cd, cd->dev) < 0)
  5102. pt_debug(cd->dev, DL_ERROR,
  5103. "%s: fail to release exclusive\n", __func__);
  5104. return rc;
  5105. }
  5106. /*******************************************************************************
  5107. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5108. *
  5109. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5110. * to request to have the BL program and verify a FW image
  5111. *
  5112. * RETURN:
  5113. * 0 = success
  5114. * !0 = failure
  5115. *
  5116. * PARAMETERS:
  5117. * *dev - pointer to device structure
  5118. * protect - boolean to determine to call the protected function
  5119. * data_len - length of data_buf
  5120. * *data_buf - firmware image to program
  5121. ******************************************************************************/
  5122. static int _pt_request_pip_bl_program_and_verify(
  5123. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5124. {
  5125. struct pt_core_data *cd = dev_get_drvdata(dev);
  5126. if (protect)
  5127. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5128. data_buf);
  5129. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5130. data_buf);
  5131. }
  5132. /*******************************************************************************
  5133. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5134. *
  5135. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5136. * perform a full verification of the application integrity by calculating the
  5137. * CRC of the image in flash and compare it to the expected CRC stored in the
  5138. * Metadata row.
  5139. *
  5140. * RETURN:
  5141. * 0 = success
  5142. * !0 = failure
  5143. *
  5144. * PARAMETERS:
  5145. * *cd - pointer to core data
  5146. * *result - pointer to store result
  5147. ******************************************************************************/
  5148. static int pt_hid_output_bl_verify_app_integrity_(
  5149. struct pt_core_data *cd, u8 *result)
  5150. {
  5151. int rc;
  5152. struct pt_hid_output hid_output = {
  5153. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5154. };
  5155. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5156. if (rc) {
  5157. *result = 0;
  5158. return rc;
  5159. }
  5160. *result = cd->response_buf[8];
  5161. return 0;
  5162. }
  5163. /*******************************************************************************
  5164. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5165. *
  5166. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5167. * exclusive access to the DUT.
  5168. *
  5169. * RETURN:
  5170. * 0 = success
  5171. * !0 = failure
  5172. *
  5173. * PARAMETERS:
  5174. * *cd - pointer to core data
  5175. * *result - pointer to store result
  5176. ******************************************************************************/
  5177. static int pt_hid_output_bl_verify_app_integrity(
  5178. struct pt_core_data *cd, u8 *result)
  5179. {
  5180. int rc;
  5181. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5182. if (rc < 0) {
  5183. pt_debug(cd->dev, DL_ERROR,
  5184. "%s: fail get exclusive ex=%p own=%p\n",
  5185. __func__, cd->exclusive_dev, cd->dev);
  5186. return rc;
  5187. }
  5188. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5189. if (release_exclusive(cd, cd->dev) < 0)
  5190. pt_debug(cd->dev, DL_ERROR,
  5191. "%s: fail to release exclusive\n", __func__);
  5192. return rc;
  5193. }
  5194. /*******************************************************************************
  5195. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5196. *
  5197. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5198. * to request to have the BL verify the application integrity (PIP1.x only)
  5199. *
  5200. * RETURN:
  5201. * 0 = success
  5202. * !0 = failure
  5203. *
  5204. * PARAMETERS:
  5205. * *dev - pointer to device structure
  5206. * protect - boolean to determine to call the protected function
  5207. * *result - pointer to store result
  5208. ******************************************************************************/
  5209. static int _pt_request_pip_bl_verify_app_integrity(
  5210. struct device *dev, int protect, u8 *result)
  5211. {
  5212. struct pt_core_data *cd = dev_get_drvdata(dev);
  5213. if (protect)
  5214. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5215. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5216. }
  5217. /*******************************************************************************
  5218. * FUNCTION: pt_hid_output_bl_launch_app_
  5219. *
  5220. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5221. * application from bootloader (PIP1.x only).
  5222. *
  5223. * RETURN:
  5224. * 0 = success
  5225. * !0 = failure
  5226. *
  5227. * PARAMETERS:
  5228. * *cd - pointer to core data
  5229. ******************************************************************************/
  5230. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5231. {
  5232. struct pt_hid_output hid_output = {
  5233. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5234. .reset_expected = 1,
  5235. };
  5236. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5237. }
  5238. /*******************************************************************************
  5239. * FUNCTION: pt_hid_output_bl_launch_app
  5240. *
  5241. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5242. * to the DUT.
  5243. *
  5244. * RETURN:
  5245. * 0 = success
  5246. * !0 = failure
  5247. *
  5248. * PARAMETERS:
  5249. * *cd - pointer to core data
  5250. ******************************************************************************/
  5251. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5252. {
  5253. int rc;
  5254. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5255. if (rc < 0) {
  5256. pt_debug(cd->dev, DL_ERROR,
  5257. "%s: fail get exclusive ex=%p own=%p\n",
  5258. __func__, cd->exclusive_dev, cd->dev);
  5259. return rc;
  5260. }
  5261. rc = pt_hid_output_bl_launch_app_(cd);
  5262. if (release_exclusive(cd, cd->dev) < 0)
  5263. pt_debug(cd->dev, DL_ERROR,
  5264. "%s: fail to release exclusive\n", __func__);
  5265. return rc;
  5266. }
  5267. /*******************************************************************************
  5268. * FUNCTION: _pt_request_pip_launch_app
  5269. *
  5270. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5271. * to request to have the BL launch the application. (PIP1.x only)
  5272. *
  5273. * RETURN:
  5274. * 0 = success
  5275. * !0 = failure
  5276. *
  5277. * PARAMETERS:
  5278. * *dev - pointer to device structure
  5279. * protect - boolean to determine to call the protected function
  5280. ******************************************************************************/
  5281. static int _pt_request_pip_launch_app(struct device *dev,
  5282. int protect)
  5283. {
  5284. struct pt_core_data *cd = dev_get_drvdata(dev);
  5285. if (protect)
  5286. return pt_hid_output_bl_launch_app(cd);
  5287. return pt_hid_output_bl_launch_app_(cd);
  5288. }
  5289. /*******************************************************************************
  5290. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5291. *
  5292. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5293. * value store in the System Information.
  5294. *
  5295. * RETURN:
  5296. * 0 = success
  5297. * !0 = failure
  5298. *
  5299. * PARAMETERS:
  5300. * *cd - pointer to core data
  5301. * *panel_id - pointer to where the panel ID will be stored
  5302. ******************************************************************************/
  5303. static int pt_hid_output_bl_get_panel_id_(
  5304. struct pt_core_data *cd, u8 *panel_id)
  5305. {
  5306. int rc;
  5307. struct pt_hid_output hid_output = {
  5308. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5309. };
  5310. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5311. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5312. pt_debug(cd->dev, DL_ERROR,
  5313. "%s: Get Panel ID command not supported\n",
  5314. __func__);
  5315. *panel_id = PANEL_ID_NOT_ENABLED;
  5316. return 0;
  5317. } else if (rc < 0) {
  5318. pt_debug(cd->dev, DL_ERROR,
  5319. "%s: Error on Get Panel ID command\n", __func__);
  5320. return rc;
  5321. }
  5322. *panel_id = cd->response_buf[8];
  5323. return 0;
  5324. }
  5325. /*******************************************************************************
  5326. * FUNCTION: pt_hid_output_bl_get_panel_id
  5327. *
  5328. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5329. * to the DUT.
  5330. *
  5331. * RETURN:
  5332. * 0 = success
  5333. * !0 = failure
  5334. *
  5335. * PARAMETERS:
  5336. * *cd - pointer to core data
  5337. * *panel_id - pointer to where the panel ID will be stored
  5338. ******************************************************************************/
  5339. static int pt_hid_output_bl_get_panel_id(
  5340. struct pt_core_data *cd, u8 *panel_id)
  5341. {
  5342. int rc;
  5343. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5344. if (rc < 0) {
  5345. pt_debug(cd->dev, DL_ERROR,
  5346. "%s: fail get exclusive ex=%p own=%p\n",
  5347. __func__, cd->exclusive_dev, cd->dev);
  5348. return rc;
  5349. }
  5350. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5351. if (release_exclusive(cd, cd->dev) < 0)
  5352. pt_debug(cd->dev, DL_ERROR,
  5353. "%s: fail to release exclusive\n", __func__);
  5354. return rc;
  5355. }
  5356. /*******************************************************************************
  5357. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5358. *
  5359. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5360. * to have the BL retrieve the panel ID
  5361. *
  5362. * RETURN:
  5363. * 0 = success
  5364. * !0 = failure
  5365. *
  5366. * PARAMETERS:
  5367. * *dev - pointer to device structure
  5368. * protect - flag to run in protected mode
  5369. * *panel_id - pointer to where the panel ID will be stored
  5370. ******************************************************************************/
  5371. static int _pt_request_pip_bl_get_panel_id(
  5372. struct device *dev, int protect, u8 *panel_id)
  5373. {
  5374. struct pt_core_data *cd = dev_get_drvdata(dev);
  5375. if (protect)
  5376. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5377. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5378. }
  5379. /*******************************************************************************
  5380. * FUNCTION: pt_pip2_get_mode_sysmode_
  5381. *
  5382. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5383. * PIP2 STATUS command.
  5384. *
  5385. * RETURN:
  5386. * 0 = success
  5387. * !0 = failure
  5388. *
  5389. * PARAMETERS:
  5390. * *cd - pointer to core data structure
  5391. * *mode - pointer to store the retrieved mode
  5392. * *sys_mode - pointer to store the FW system mode
  5393. ******************************************************************************/
  5394. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5395. u8 *mode, u8 *sys_mode)
  5396. {
  5397. u16 actual_read_len;
  5398. u8 read_buf[12];
  5399. u8 status, boot;
  5400. int rc = 0;
  5401. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5402. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5403. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5404. __func__, rc);
  5405. if (!rc) {
  5406. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5407. "PIP2 STATUS");
  5408. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5409. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5410. if (sys_mode) {
  5411. if (status == PIP2_RSP_ERR_NONE &&
  5412. boot == PIP2_STATUS_APP_EXEC)
  5413. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5414. else
  5415. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5416. }
  5417. if (mode) {
  5418. if (status == PIP2_RSP_ERR_NONE &&
  5419. boot == PIP2_STATUS_BOOT_EXEC)
  5420. *mode = PT_MODE_BOOTLOADER;
  5421. else if (status == PIP2_RSP_ERR_NONE &&
  5422. boot == PIP2_STATUS_APP_EXEC)
  5423. *mode = PT_MODE_OPERATIONAL;
  5424. else
  5425. *mode = PT_MODE_UNKNOWN;
  5426. }
  5427. } else {
  5428. if (mode)
  5429. *mode = PT_MODE_UNKNOWN;
  5430. if (sys_mode)
  5431. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5432. pt_debug(cd->dev, DL_WARN,
  5433. "%s: Mode and sys_mode could not be determined\n",
  5434. __func__);
  5435. }
  5436. return rc;
  5437. }
  5438. /*******************************************************************************
  5439. * FUNCTION: pt_pip2_get_mode_sysmode
  5440. *
  5441. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5442. * access to the DUT.
  5443. *
  5444. * RETURN:
  5445. * 0 = success
  5446. * !0 = failure
  5447. *
  5448. * PARAMETERS:
  5449. * *cd - pointer to core data structure
  5450. * *mode - pointer to store the retrieved mode
  5451. * *sys_mode - pointer to store the FW system mode
  5452. ******************************************************************************/
  5453. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5454. u8 *mode, u8 *sys_mode)
  5455. {
  5456. int rc;
  5457. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5458. if (rc < 0) {
  5459. pt_debug(cd->dev, DL_ERROR,
  5460. "%s: fail get exclusive ex=%p own=%p\n",
  5461. __func__, cd->exclusive_dev, cd->dev);
  5462. return rc;
  5463. }
  5464. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5465. if (release_exclusive(cd, cd->dev) < 0)
  5466. pt_debug(cd->dev, DL_ERROR,
  5467. "%s: fail to release exclusive\n", __func__);
  5468. return rc;
  5469. }
  5470. /*******************************************************************************
  5471. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5472. *
  5473. * SUMMARY: Function pointer included in core_commands struct for external
  5474. * calls to the protected or unprotected call to
  5475. * pt_pip2_get_mode_sysmode
  5476. *
  5477. * RETURN:
  5478. * 0 = success
  5479. * !0 = failure
  5480. *
  5481. * PARAMETERS:
  5482. * *dev - pointer to device structure
  5483. * *mode - pointer to store the retrieved mode
  5484. * *sys_mode - pointer to store the FW system mode
  5485. ******************************************************************************/
  5486. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5487. int protect, u8 *mode, u8 *sys_mode)
  5488. {
  5489. struct pt_core_data *cd = dev_get_drvdata(dev);
  5490. if (protect)
  5491. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5492. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5493. }
  5494. /*******************************************************************************
  5495. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5496. *
  5497. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5498. * mode only the following PIP commands will be serviced, any other PIP
  5499. * command the FW will respond with an "Invalid PIP Command" response.
  5500. * - Get HID Descriptor (Register 0x0001, no command ID)
  5501. * - Reset (Register 0x0005, RESET HID request)
  5502. * - Ping (Register 0x0004, Command ID 0x00
  5503. * - Get System Information (Register 0x0004, Command ID 0x02)
  5504. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5505. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5506. * This function will loop on the results of the STATUS command until
  5507. * the FW reports it is out of BOOT mode.
  5508. *
  5509. * NOTE:
  5510. * - This function will update cd->fw_system_mode
  5511. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5512. *
  5513. * RETURN:
  5514. * 0 = success
  5515. * !0 = failure
  5516. *
  5517. * PARAMETERS:
  5518. * *cd - pointer to core data
  5519. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5520. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5521. ******************************************************************************/
  5522. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5523. int *actual_wait)
  5524. {
  5525. int loop = 0;
  5526. u8 sys_mode = cd->fw_system_mode;
  5527. u8 pause = 10; /* in ms */
  5528. int rc = 0;
  5529. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5530. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5531. /*
  5532. * For PIP <1.11, no support for polling wait so do a hard
  5533. * coded wait and assume the FW is out of BOOT. Added 1 to
  5534. * timeout to make it clear in kmsg if non polling was done.
  5535. */
  5536. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5537. pt_debug(cd->dev, DL_ERROR,
  5538. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5539. __func__,
  5540. cd->sysinfo.ttdata.pip_ver_major,
  5541. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5542. msleep(*actual_wait);
  5543. sys_mode = FW_SYS_MODE_SCANNING;
  5544. }
  5545. if (sys_mode == FW_SYS_MODE_BOOT) {
  5546. while (!rc && loop <= max_loop &&
  5547. (sys_mode == FW_SYS_MODE_BOOT)) {
  5548. loop++;
  5549. usleep_range(9000, pause * 1000);
  5550. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5551. pt_debug(cd->dev, DL_DEBUG,
  5552. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5553. __func__, loop * pause, sys_mode);
  5554. }
  5555. *actual_wait = (int)(loop * pause);
  5556. pt_debug(cd->dev, DL_WARN,
  5557. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5558. __func__, *actual_wait, sys_mode);
  5559. if (rc)
  5560. sys_mode = FW_SYS_MODE_UNDEFINED;
  5561. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5562. sys_mode == FW_SYS_MODE_UNDEFINED)
  5563. rc = -EBUSY;
  5564. }
  5565. mutex_lock(&cd->system_lock);
  5566. cd->fw_system_mode = sys_mode;
  5567. mutex_unlock(&cd->system_lock);
  5568. return rc;
  5569. }
  5570. /*******************************************************************************
  5571. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5572. *
  5573. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5574. * access to the DUT.
  5575. *
  5576. * RETURN:
  5577. * 0 = success
  5578. * !0 = failure
  5579. *
  5580. * PARAMETERS:
  5581. * *cd - pointer to core data
  5582. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5583. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5584. ******************************************************************************/
  5585. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5586. int *actual_wait)
  5587. {
  5588. int rc;
  5589. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5590. if (rc < 0) {
  5591. pt_debug(cd->dev, DL_ERROR,
  5592. "%s: fail get exclusive ex=%p own=%p\n",
  5593. __func__, cd->exclusive_dev, cd->dev);
  5594. return rc;
  5595. }
  5596. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5597. if (release_exclusive(cd, cd->dev) < 0)
  5598. pt_debug(cd->dev, DL_ERROR,
  5599. "%s: fail to release exclusive\n", __func__);
  5600. return rc;
  5601. }
  5602. /*******************************************************************************
  5603. * FUNCTION: _pt_get_fw_sys_mode
  5604. *
  5605. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5606. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5607. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5608. * information but any PIP command above 0x1F requires scanning to be disabled
  5609. * before it will be operational. If scanning was not disabled before sending
  5610. * these PIP commands the FW will respond with a 6 byte error response. So to
  5611. * safely determine the scanning state, a PIP message that will not affect the
  5612. * operation of the FW was chosen:
  5613. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5614. * received scanning is active.
  5615. *
  5616. * RETURN:
  5617. * 0 = success
  5618. * !0 = failure
  5619. *
  5620. * PARAMETERS:
  5621. * *cd - pointer to core data
  5622. * *sys_mode - pointer to FW System mode
  5623. * *mode - pointer to mode (BL/FW)
  5624. ******************************************************************************/
  5625. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5626. {
  5627. int write_length = 1;
  5628. int report_length;
  5629. int rc = 0;
  5630. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5631. u8 tmp_mode = PT_MODE_UNKNOWN;
  5632. u8 param[1] = { PT_TCH_PARM_EBID };
  5633. struct pt_hid_output hid_output = {
  5634. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5635. .write_length = write_length,
  5636. .write_buf = param,
  5637. .novalidate = true,
  5638. };
  5639. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5640. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5641. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5642. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5643. __func__, tmp_sys_mode, tmp_mode);
  5644. if (!rc) {
  5645. if (tmp_mode != PT_MODE_OPERATIONAL)
  5646. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5647. }
  5648. goto exit;
  5649. }
  5650. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5651. if (cd->mode != PT_MODE_OPERATIONAL) {
  5652. tmp_mode = cd->mode;
  5653. goto exit;
  5654. }
  5655. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5656. if (rc)
  5657. goto exit;
  5658. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5659. if ((report_length == 0x06) &&
  5660. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5661. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5662. tmp_mode = PIP2_STATUS_APP_EXEC;
  5663. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5664. } else if ((report_length == 0x0A) &&
  5665. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5666. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5667. tmp_mode = PIP2_STATUS_APP_EXEC;
  5668. tmp_sys_mode = FW_SYS_MODE_TEST;
  5669. }
  5670. exit:
  5671. if (mode)
  5672. *mode = tmp_mode;
  5673. if (sys_mode)
  5674. *sys_mode = tmp_sys_mode;
  5675. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5676. __func__, tmp_mode, tmp_sys_mode);
  5677. return rc;
  5678. }
  5679. /*******************************************************************************
  5680. * FUNCTION: pt_get_fw_sys_mode
  5681. *
  5682. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5683. * is active or not.
  5684. *
  5685. * RETURN:
  5686. * 0 = success
  5687. * !0 = failure
  5688. *
  5689. * PARAMETERS:
  5690. * *cd - pointer to core data
  5691. * *sys_mode - pointer to fw system mode
  5692. * *mode - pointer to mode
  5693. ******************************************************************************/
  5694. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5695. {
  5696. int rc;
  5697. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5698. if (rc < 0) {
  5699. pt_debug(cd->dev, DL_ERROR,
  5700. "%s: fail get exclusive ex=%p own=%p\n",
  5701. __func__, cd->exclusive_dev, cd->dev);
  5702. return rc;
  5703. }
  5704. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5705. if (release_exclusive(cd, cd->dev) < 0)
  5706. pt_debug(cd->dev, DL_ERROR,
  5707. "%s: fail to release exclusive\n", __func__);
  5708. return rc;
  5709. }
  5710. /*******************************************************************************
  5711. * FUNCTION: _pt_request_get_fw_sys_mode
  5712. *
  5713. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5714. * to request to get scan state
  5715. *
  5716. * RETURN:
  5717. * 0 = success
  5718. * !0 = failure
  5719. *
  5720. * PARAMETERS:
  5721. * *dev - pointer to device structure
  5722. * protect - flag to call protected or non-protected
  5723. * *sys_mode - pointer to FW system mode
  5724. * *mode - pointer to mode
  5725. ******************************************************************************/
  5726. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5727. u8 *sys_mode, u8 *mode)
  5728. {
  5729. struct pt_core_data *cd = dev_get_drvdata(dev);
  5730. if (protect)
  5731. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5732. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5733. }
  5734. /* Default hid descriptor to provide basic register map */
  5735. const struct pt_hid_desc hid_desc_default = {
  5736. 230, /* hid_desc_len */
  5737. HID_APP_REPORT_ID, /* packet_id */
  5738. 0x00, /* reserved_byte */
  5739. 0x0100, /* bcd_version */
  5740. 0x00EC, /* report_desc_len */
  5741. 0x0002, /* report_desc_register */
  5742. 0x0003, /* input_register */
  5743. 0x00FE, /* max_input_len */
  5744. 0x0004, /* output_register */
  5745. 0x00FE, /* max_output_len */
  5746. 0x0005, /* command_register */
  5747. 0x0006, /* data_register */
  5748. 0x04B4, /* vendor_id */
  5749. 0xC101, /* product_id */
  5750. 0x0100, /* version_id */
  5751. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5752. };
  5753. /*******************************************************************************
  5754. * FUNCTION: pt_init_hid_descriptor
  5755. *
  5756. * SUMMARY: Setup default values for HID descriptor structure
  5757. *
  5758. *
  5759. * PARAMETERS:
  5760. * *desc - pointer to the HID descriptor data read back from DUT
  5761. ******************************************************************************/
  5762. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5763. {
  5764. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5765. }
  5766. /*******************************************************************************
  5767. * FUNCTION: pt_get_hid_descriptor_
  5768. *
  5769. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5770. * into the HID descriptor structure
  5771. *
  5772. * RETURN:
  5773. * 0 = success
  5774. * !0 = failure
  5775. *
  5776. * PARAMETERS:
  5777. * *cd - pointer to core data
  5778. * *desc - pointer to the HID descriptor data read back from DUT
  5779. ******************************************************************************/
  5780. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5781. struct pt_hid_desc *desc)
  5782. {
  5783. struct device *dev = cd->dev;
  5784. int rc = 0;
  5785. int t;
  5786. u8 cmd[2];
  5787. /*
  5788. * During startup the HID descriptor is required for all future
  5789. * processing. If IRQ is already asserted due to an early touch report
  5790. * the report must be cleared before sending command.
  5791. */
  5792. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5793. /* Read HID descriptor length and version */
  5794. mutex_lock(&cd->system_lock);
  5795. cd->hid_cmd_state = 1;
  5796. mutex_unlock(&cd->system_lock);
  5797. /* Set HID descriptor register */
  5798. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5799. sizeof(cd->hid_core.hid_desc_register));
  5800. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5801. __func__, sizeof(cmd));
  5802. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5803. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5804. if (rc) {
  5805. pt_debug(dev, DL_ERROR,
  5806. "%s: failed to get HID descriptor, rc=%d\n",
  5807. __func__, rc);
  5808. goto error;
  5809. }
  5810. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5811. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5812. if (IS_TMO(t)) {
  5813. #ifdef TTDL_DIAGNOSTICS
  5814. cd->bus_transmit_error_count++;
  5815. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5816. #endif /* TTDL_DIAGNOSTICS */
  5817. pt_debug(cd->dev, DL_ERROR,
  5818. "%s: HID get descriptor timed out\n", __func__);
  5819. rc = -ETIME;
  5820. goto error;
  5821. } else {
  5822. cd->hw_detected = true;
  5823. }
  5824. /* Load the HID descriptor including all registers */
  5825. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5826. /* Check HID descriptor length and version */
  5827. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5828. le16_to_cpu(desc->hid_desc_len),
  5829. le16_to_cpu(desc->bcd_version));
  5830. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5831. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5832. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5833. __func__);
  5834. return -ENODEV;
  5835. }
  5836. goto exit;
  5837. error:
  5838. mutex_lock(&cd->system_lock);
  5839. cd->hid_cmd_state = 0;
  5840. mutex_unlock(&cd->system_lock);
  5841. exit:
  5842. return rc;
  5843. }
  5844. /*******************************************************************************
  5845. * FUNCTION: pt_get_hid_descriptor
  5846. *
  5847. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5848. *
  5849. * RETURN:
  5850. * 0 = success
  5851. * !0 = failure
  5852. *
  5853. * PARAMETERS:
  5854. * *cd - pointer to core data
  5855. * *desc - pointer to the HID descriptor data read back from DUT
  5856. ******************************************************************************/
  5857. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5858. struct pt_hid_desc *desc)
  5859. {
  5860. int rc;
  5861. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5862. if (rc < 0) {
  5863. pt_debug(cd->dev, DL_ERROR,
  5864. "%s: fail get exclusive ex=%p own=%p\n",
  5865. __func__, cd->exclusive_dev, cd->dev);
  5866. return rc;
  5867. }
  5868. rc = pt_get_hid_descriptor_(cd, desc);
  5869. if (release_exclusive(cd, cd->dev) < 0)
  5870. pt_debug(cd->dev, DL_ERROR,
  5871. "%s: fail to release exclusive\n", __func__);
  5872. return rc;
  5873. }
  5874. /*******************************************************************************
  5875. * FUNCTION: pt_pip2_get_version_
  5876. *
  5877. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5878. * cd-ttdata
  5879. *
  5880. * RETURN:
  5881. * 0 = success
  5882. * !0 = failure
  5883. *
  5884. * PARAMETERS:
  5885. * *cd - pointer to core data
  5886. ******************************************************************************/
  5887. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5888. {
  5889. int rc = 0;
  5890. int status;
  5891. u8 read_buf[64];
  5892. u16 actual_read_len;
  5893. rc = _pt_request_pip2_send_cmd(cd->dev,
  5894. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5895. NULL, 0, read_buf, &actual_read_len);
  5896. if (rc) {
  5897. pt_debug(cd->dev, DL_ERROR,
  5898. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5899. __func__, rc);
  5900. return rc;
  5901. }
  5902. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5903. if (status == 0) {
  5904. /* Parse the PIP2 VERSION response into ttdata */
  5905. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5906. } else {
  5907. pt_debug(cd->dev, DL_ERROR,
  5908. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5909. __func__, status);
  5910. return status;
  5911. }
  5912. return rc;
  5913. }
  5914. /*******************************************************************************
  5915. * FUNCTION: pt_pip2_get_version
  5916. *
  5917. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5918. * access to the DUT.
  5919. *
  5920. * RETURN:
  5921. * 0 = success
  5922. * !0 = failure
  5923. *
  5924. * PARAMETERS:
  5925. * *cd - pointer to core data structure
  5926. ******************************************************************************/
  5927. static int pt_pip2_get_version(struct pt_core_data *cd)
  5928. {
  5929. int rc;
  5930. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5931. if (rc < 0) {
  5932. pt_debug(cd->dev, DL_ERROR,
  5933. "%s: fail get exclusive ex=%p own=%p\n",
  5934. __func__, cd->exclusive_dev, cd->dev);
  5935. return rc;
  5936. }
  5937. rc = pt_pip2_get_version_(cd);
  5938. if (release_exclusive(cd, cd->dev) < 0)
  5939. pt_debug(cd->dev, DL_ERROR,
  5940. "%s: fail to release exclusive\n", __func__);
  5941. return rc;
  5942. }
  5943. /*******************************************************************************
  5944. * FUNCTION: _pt_request_active_pip_protocol
  5945. *
  5946. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5947. * Function will return PIP version of BL or application based on
  5948. * when it's called.
  5949. *
  5950. * RETURN:
  5951. * 0 = success
  5952. * !0 = failure
  5953. *
  5954. * PARAMETERS:
  5955. * *dev - pointer to device structure
  5956. * protect - flag to run in protected mode
  5957. * *pip_version_major - pointer to store PIP major version
  5958. * *pip_version_minor - pointer to store PIP minor version
  5959. ******************************************************************************/
  5960. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5961. u8 *pip_version_major, u8 *pip_version_minor)
  5962. {
  5963. struct pt_core_data *cd = dev_get_drvdata(dev);
  5964. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5965. int rc = 0;
  5966. struct pt_hid_output sys_info = {
  5967. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5968. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5969. };
  5970. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5971. /* Skip PIP2 command if DUT generation is confirmed */
  5972. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5973. goto skip_pip2_command;
  5974. rc = pt_pip2_get_version_(cd);
  5975. if (!rc) {
  5976. *pip_version_major = ttdata->pip_ver_major;
  5977. *pip_version_minor = ttdata->pip_ver_minor;
  5978. pt_debug(dev, DL_INFO,
  5979. "%s: pip_version = %d.%d\n", __func__,
  5980. *pip_version_major, *pip_version_minor);
  5981. } else {
  5982. /*
  5983. * Legacy products do not support the pip2 protocol to get
  5984. * pip version. However, they do support the "get sysinfo"
  5985. * command to get pip version from FW, but the bootloader
  5986. * does not support it. This function will try "get sysinfo"
  5987. * command if the pip2 command failed but this cmd could also
  5988. * fail if DUT is stuck in bootloader mode.
  5989. */
  5990. pt_debug(dev, DL_INFO,
  5991. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  5992. __func__, rc);
  5993. skip_pip2_command:
  5994. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  5995. if (!rc) {
  5996. *pip_version_minor =
  5997. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  5998. *pip_version_major =
  5999. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6000. pt_debug(dev, DL_INFO,
  6001. "%s: pip_version = %d.%d\n", __func__,
  6002. *pip_version_major, *pip_version_minor);
  6003. } else {
  6004. *pip_version_major = 0;
  6005. *pip_version_minor = 0;
  6006. pt_debug(dev, DL_ERROR,
  6007. "%s: pip_version Not Detected\n", __func__);
  6008. }
  6009. }
  6010. return rc;
  6011. }
  6012. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6013. /*******************************************************************************
  6014. * FUNCTION: _pt_detect_dut_generation
  6015. *
  6016. * SUMMARY: Determine the generation of device that we are communicating with:
  6017. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6018. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6019. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6020. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6021. * coming form the BL or the FW:
  6022. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6023. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6024. * This function will return a modified status if it detects the DUT
  6025. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6026. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6027. *
  6028. * RETURN:
  6029. * 0 = success
  6030. * !0 = failure
  6031. *
  6032. * PARAMETERS:
  6033. * *dev - pointer to device structure
  6034. * *status - pointer to status bitmask
  6035. * *dut_gen - pointer to store the dut_generation
  6036. * *mode - pointer to store the PT_MODE
  6037. ******************************************************************************/
  6038. static int _pt_detect_dut_generation(struct device *dev,
  6039. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6040. {
  6041. int rc = 0;
  6042. u8 dut_gen_tmp = DUT_UNKNOWN;
  6043. u8 mode_tmp = PT_MODE_UNKNOWN;
  6044. u8 attempt = 1;
  6045. u32 status_tmp = STARTUP_STATUS_START;
  6046. struct pt_core_data *cd = dev_get_drvdata(dev);
  6047. struct pt_hid_desc hid_desc;
  6048. memset(&hid_desc, 0, sizeof(hid_desc));
  6049. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6050. while (rc && attempt < 3) {
  6051. attempt++;
  6052. usleep_range(2000, 5000);
  6053. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6054. }
  6055. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6056. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6057. mode_tmp = PT_MODE_BOOTLOADER;
  6058. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6059. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6060. rc = pt_pip2_get_version_(cd);
  6061. if (!rc)
  6062. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6063. else
  6064. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6065. mode_tmp = PT_MODE_OPERATIONAL;
  6066. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6067. rc = 0; /* To return success instead of error code */
  6068. } else if (rc) {
  6069. rc = pt_pip2_get_version_(cd);
  6070. if (!rc) {
  6071. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6072. mode_tmp = PT_MODE_BOOTLOADER;
  6073. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6074. }
  6075. }
  6076. mutex_lock(&cd->system_lock);
  6077. if (dut_gen)
  6078. *dut_gen = dut_gen_tmp;
  6079. if (mode)
  6080. *mode = mode_tmp;
  6081. if (status)
  6082. *status = status_tmp;
  6083. mutex_unlock(&cd->system_lock);
  6084. #ifdef TTDL_DIAGNOSTICS
  6085. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6086. __func__, dut_gen_tmp, mode_tmp);
  6087. #endif /* TTDL_DIAGNOSTICS */
  6088. return rc;
  6089. }
  6090. /*******************************************************************************
  6091. * FUNCTION: _pt_request_dut_generation
  6092. *
  6093. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6094. * to get current dut generation.
  6095. *
  6096. * NOTE: This function WILL NOT try to determine dut generation.
  6097. *
  6098. * RETURN:
  6099. * The current dut generation.
  6100. *
  6101. * PARAMETERS:
  6102. * *dev - pointer to device structure
  6103. ******************************************************************************/
  6104. static int _pt_request_dut_generation(struct device *dev)
  6105. {
  6106. struct pt_core_data *cd = dev_get_drvdata(dev);
  6107. return cd->active_dut_generation;
  6108. }
  6109. #define HW_VERSION_LEN_MAX 13
  6110. /*******************************************************************************
  6111. * FUNCTION: _legacy_generate_hw_version
  6112. *
  6113. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6114. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6115. * command) to the hw_version.
  6116. *
  6117. * RETURN:
  6118. * 0 = success
  6119. * !0 = failure
  6120. *
  6121. * PARAMETERS:
  6122. * *cd - pointer to core data
  6123. * *hw_version - pointer to store the hardware version
  6124. ******************************************************************************/
  6125. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6126. char *hw_version)
  6127. {
  6128. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6129. if (cd->sysinfo.ready) {
  6130. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6131. ttdata->jtag_id_h, cd->pid_for_loader);
  6132. return 0;
  6133. } else if (cd->bl_info.ready) {
  6134. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6135. cd->bl_info.chip_id, cd->pid_for_loader);
  6136. return 0;
  6137. } else {
  6138. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6139. pt_debug(cd->dev, DL_ERROR,
  6140. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6141. return -ENODATA;
  6142. }
  6143. }
  6144. /*******************************************************************************
  6145. * FUNCTION: _pip2_generate_hw_version
  6146. *
  6147. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6148. * VERSION command) to the hw_version.
  6149. *
  6150. * RETURN:
  6151. * 0 = success
  6152. * !0 = failure
  6153. *
  6154. * PARAMETERS:
  6155. * *cd - pointer to core data
  6156. * *hw_version - pointer to store the hardware version
  6157. ******************************************************************************/
  6158. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6159. {
  6160. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6161. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6162. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6163. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6164. return 0;
  6165. } else {
  6166. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6167. pt_debug(cd->dev, DL_ERROR,
  6168. "%s: PIP Version are not ready\n", __func__);
  6169. return -ENODATA;
  6170. }
  6171. }
  6172. /*******************************************************************************
  6173. * FUNCTION: pt_generate_hw_version
  6174. *
  6175. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6176. * hw_version from static data.
  6177. *
  6178. * RETURN:
  6179. * 0 = success
  6180. * !0 = failure
  6181. *
  6182. * PARAMETERS:
  6183. * *cd - pointer to core data
  6184. * *hw_version - pointer to store the hardware version
  6185. ******************************************************************************/
  6186. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6187. {
  6188. int rc = 0;
  6189. if (!hw_version)
  6190. return -ENOMEM;
  6191. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6192. rc = _legacy_generate_hw_version(cd, hw_version);
  6193. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6194. rc = _pip2_generate_hw_version(cd, hw_version);
  6195. else {
  6196. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6197. rc = -ENODATA;
  6198. }
  6199. return rc;
  6200. }
  6201. /*******************************************************************************
  6202. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6203. *
  6204. * NOTE: The calling function must ensure to free *hw_version
  6205. *
  6206. * RETURN:
  6207. * 0 = success
  6208. * !0 = Failure
  6209. *
  6210. * PARAMETERS:
  6211. * *dev - pointer to device structure
  6212. * *hw_version - pointer to where the hw_version string will be stored
  6213. ******************************************************************************/
  6214. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6215. {
  6216. int rc = 0;
  6217. u16 actual_read_len;
  6218. u16 pip_ver;
  6219. u8 rd_buf[256];
  6220. u8 status;
  6221. u8 index = PIP2_RESP_STATUS_OFFSET;
  6222. u8 return_data[8];
  6223. u8 panel_id;
  6224. struct pt_core_data *cd = dev_get_drvdata(dev);
  6225. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6226. if (!hw_version)
  6227. return -ENOMEM;
  6228. if (!cd->hw_detected) {
  6229. /* No HW detected */
  6230. rc = -ENODEV;
  6231. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6232. __func__);
  6233. goto exit_error;
  6234. }
  6235. /* For Parade TC or TT parts */
  6236. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6237. rc = _pt_request_pip2_send_cmd(dev,
  6238. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6239. NULL, 0, rd_buf, &actual_read_len);
  6240. if (rc) {
  6241. pt_debug(dev, DL_ERROR,
  6242. "%s: Failed to send PIP2 VERSION cmd\n",
  6243. __func__);
  6244. goto exit_error;
  6245. }
  6246. status = rd_buf[index];
  6247. if (status == 0) {
  6248. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6249. /*
  6250. * BL PIP 2.02 and greater the version fields are
  6251. * swapped
  6252. */
  6253. if (pip_ver >= 0x0202) {
  6254. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6255. "%02X%02X.%02X%02X.FF",
  6256. rd_buf[index + 10], rd_buf[index + 9],
  6257. rd_buf[index + 8], rd_buf[index + 7]);
  6258. } else {
  6259. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6260. "%02X%02X.%02X%02X.FF",
  6261. rd_buf[index + 8], rd_buf[index + 7],
  6262. rd_buf[index + 10], rd_buf[index + 9]);
  6263. }
  6264. return STATUS_SUCCESS;
  6265. } else {
  6266. rc = status;
  6267. pt_debug(dev, DL_WARN,
  6268. "%s: PIP2 VERSION cmd response error\n",
  6269. __func__);
  6270. }
  6271. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6272. /*
  6273. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6274. * hard coded to FFFF
  6275. */
  6276. if (cd->mode == PT_MODE_OPERATIONAL) {
  6277. rc = pt_hid_output_get_sysinfo(cd);
  6278. if (!rc) {
  6279. panel_id =
  6280. cd->sysinfo.sensing_conf_data.panel_id;
  6281. } else {
  6282. panel_id = PANEL_ID_NOT_ENABLED;
  6283. }
  6284. /* In FW - simply retrieve from ttdata struct */
  6285. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6286. "%04X.FFFF.%02X",
  6287. ttdata->jtag_id_h,
  6288. panel_id);
  6289. return STATUS_SUCCESS;
  6290. } else {
  6291. /*
  6292. * Return the stored value if PT_PANEL_ID_BY_BL
  6293. * is not supported while other feature is.
  6294. */
  6295. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6296. rc = pt_hid_output_bl_get_information(
  6297. cd, return_data);
  6298. if (!rc) {
  6299. rc = pt_hid_output_bl_get_panel_id(
  6300. cd, &panel_id);
  6301. }
  6302. } else
  6303. panel_id = cd->pid_for_loader;
  6304. if (!rc) {
  6305. snprintf(hw_version,
  6306. HW_VERSION_LEN_MAX,
  6307. "%02X%02X.FFFF.%02X",
  6308. return_data[3], return_data[2],
  6309. panel_id);
  6310. return STATUS_SUCCESS;
  6311. }
  6312. }
  6313. } else {
  6314. /* Unknown generation */
  6315. rc = -ENODEV;
  6316. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6317. __func__);
  6318. }
  6319. exit_error:
  6320. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6321. return rc;
  6322. }
  6323. /*******************************************************************************
  6324. * FUNCTION: pt_start_wd_timer
  6325. *
  6326. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6327. *
  6328. * RETURN: void
  6329. *
  6330. * PARAMETERS:
  6331. * *cd - pointer to core data
  6332. ******************************************************************************/
  6333. static void pt_start_wd_timer(struct pt_core_data *cd)
  6334. {
  6335. if (cd->watchdog_interval < 100) {
  6336. pt_debug(cd->dev, DL_ERROR,
  6337. "%s: WARNING: Invalid watchdog interval: %d\n",
  6338. __func__, cd->watchdog_interval);
  6339. return;
  6340. }
  6341. if (cd->watchdog_force_stop) {
  6342. pt_debug(cd->dev, DL_INFO,
  6343. "%s: TTDL WD Forced stop\n", __func__);
  6344. return;
  6345. }
  6346. mod_timer(&cd->watchdog_timer, jiffies +
  6347. msecs_to_jiffies(cd->watchdog_interval));
  6348. cd->watchdog_enabled = 1;
  6349. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6350. }
  6351. /*******************************************************************************
  6352. * FUNCTION: pt_stop_wd_timer
  6353. *
  6354. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6355. *
  6356. * RETURN: void
  6357. *
  6358. * PARAMETERS:
  6359. * *cd - pointer to core data
  6360. ******************************************************************************/
  6361. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6362. {
  6363. if (!cd->watchdog_interval)
  6364. return;
  6365. /*
  6366. * Ensure we wait until the watchdog timer
  6367. * running on a different CPU finishes
  6368. */
  6369. del_timer_sync(&cd->watchdog_timer);
  6370. cancel_work_sync(&cd->watchdog_work);
  6371. del_timer_sync(&cd->watchdog_timer);
  6372. cd->watchdog_enabled = 0;
  6373. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6374. }
  6375. /*******************************************************************************
  6376. * FUNCTION: pt_hw_soft_reset
  6377. *
  6378. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6379. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6380. * schedule an enum if it fires when the DUT is being reset.
  6381. * This can cause a double reset.
  6382. *
  6383. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6384. * the WD active could cause this function to fail!
  6385. *
  6386. * RETURN:
  6387. * 0 = success
  6388. * !0 = failure
  6389. *
  6390. * PARAMETERS:
  6391. * *cd - pointer to core data struct
  6392. * protect - flag to call protected or non-protected
  6393. ******************************************************************************/
  6394. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6395. {
  6396. int rc = 0;
  6397. mutex_lock(&cd->system_lock);
  6398. cd->startup_status = STARTUP_STATUS_START;
  6399. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6400. mutex_unlock(&cd->system_lock);
  6401. if (protect)
  6402. rc = pt_hid_cmd_reset(cd);
  6403. else
  6404. rc = pt_hid_cmd_reset_(cd);
  6405. if (rc < 0) {
  6406. pt_debug(cd->dev, DL_ERROR,
  6407. "%s: FAILED to execute SOFT reset\n", __func__);
  6408. return rc;
  6409. }
  6410. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6411. __func__);
  6412. return 0;
  6413. }
  6414. /*******************************************************************************
  6415. * FUNCTION: pt_hw_hard_reset
  6416. *
  6417. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6418. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6419. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6420. * schedule an enum if it fires when the DUT is being reset.
  6421. * This can cause a double reset.
  6422. *
  6423. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6424. * the WD active could cause this function to fail!
  6425. *
  6426. * RETURN:
  6427. * 0 = success
  6428. * !0 = failure
  6429. *
  6430. * PARAMETERS:
  6431. * *cd - pointer to core data struct
  6432. ******************************************************************************/
  6433. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6434. {
  6435. if (cd->cpdata->xres) {
  6436. cd->startup_status = STARTUP_STATUS_START;
  6437. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6438. __func__);
  6439. cd->cpdata->xres(cd->cpdata, cd->dev);
  6440. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6441. __func__);
  6442. return 0;
  6443. }
  6444. pt_debug(cd->dev, DL_ERROR,
  6445. "%s: FAILED to execute HARD reset\n", __func__);
  6446. return -ENODEV;
  6447. }
  6448. /*******************************************************************************
  6449. * FUNCTION: pt_dut_reset
  6450. *
  6451. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6452. * soft reset.
  6453. *
  6454. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6455. * the WD active could cause this function to fail!
  6456. * NOTE: "protect" flag is only used for soft reset.
  6457. *
  6458. * RETURN:
  6459. * 0 = success
  6460. * !0 = failure
  6461. *
  6462. * PARAMETERS:
  6463. * *cd - pointer to core data structure
  6464. * protect - flag to call protected or non-protected
  6465. ******************************************************************************/
  6466. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6467. {
  6468. int rc = 0;
  6469. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6470. mutex_lock(&cd->system_lock);
  6471. cd->hid_reset_cmd_state = 1;
  6472. rc = pt_hw_hard_reset(cd);
  6473. mutex_unlock(&cd->system_lock);
  6474. if (rc == -ENODEV) {
  6475. mutex_lock(&cd->system_lock);
  6476. cd->hid_reset_cmd_state = 0;
  6477. mutex_unlock(&cd->system_lock);
  6478. pt_debug(cd->dev, DL_ERROR,
  6479. "%s: Hard reset failed, try soft reset\n", __func__);
  6480. rc = pt_hw_soft_reset(cd, protect);
  6481. }
  6482. if (rc)
  6483. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6484. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6485. return rc;
  6486. }
  6487. /*******************************************************************************
  6488. * FUNCTION: pt_dut_reset_and_wait
  6489. *
  6490. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6491. * complete
  6492. *
  6493. * RETURN:
  6494. * 0 = success
  6495. * !0 = failure
  6496. *
  6497. * PARAMETERS:
  6498. * *cd - pointer to core data structure
  6499. ******************************************************************************/
  6500. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6501. {
  6502. int rc = 0;
  6503. int t;
  6504. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6505. if (rc < 0)
  6506. goto exit;
  6507. t = wait_event_timeout(cd->wait_q,
  6508. (cd->hid_reset_cmd_state == 0),
  6509. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6510. if (IS_TMO(t)) {
  6511. #ifdef TTDL_DIAGNOSTICS
  6512. cd->bus_transmit_error_count++;
  6513. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6514. #endif /* TTDL_DIAGNOSTICS */
  6515. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6516. __func__);
  6517. rc = -ETIME;
  6518. goto exit;
  6519. }
  6520. exit:
  6521. return rc;
  6522. }
  6523. /*
  6524. * touch default parameters (from report descriptor) to resolve protocol for
  6525. * touch report
  6526. */
  6527. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6528. /* byte offset, size, min, max, bit offset, report */
  6529. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6530. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6531. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6532. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6533. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6534. };
  6535. /*
  6536. * button default parameters (from report descriptor) to resolve protocol for
  6537. * button report
  6538. */
  6539. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6540. /* byte offset, size, min, max, bit offset, report */
  6541. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6542. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6543. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6544. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6545. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6546. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6547. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6548. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6549. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6550. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6551. };
  6552. /*******************************************************************************
  6553. * FUNCTION: pt_init_pip_report_fields
  6554. *
  6555. * SUMMARY: Setup default values for touch/button report parsing.
  6556. *
  6557. * PARAMETERS:
  6558. * *cd - pointer to core data structure
  6559. ******************************************************************************/
  6560. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6561. {
  6562. struct pt_sysinfo *si = &cd->sysinfo;
  6563. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6564. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6565. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6566. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6567. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6568. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6569. cd->features.easywake = 1;
  6570. cd->features.noise_metric = 1;
  6571. cd->features.tracking_heatmap = 1;
  6572. cd->features.sensor_data = 1;
  6573. }
  6574. /*******************************************************************************
  6575. * FUNCTION: pt_get_mode
  6576. *
  6577. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6578. * message
  6579. *
  6580. * RETURN: Enum of the current mode
  6581. *
  6582. * PARAMETERS:
  6583. * *cd - pointer to the Core Data structure
  6584. * protect - run command in protected mode
  6585. * *mode - pointer to store the retrieved mode
  6586. ******************************************************************************/
  6587. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6588. {
  6589. if (desc->packet_id == HID_APP_REPORT_ID)
  6590. return PT_MODE_OPERATIONAL;
  6591. else if (desc->packet_id == HID_BL_REPORT_ID)
  6592. return PT_MODE_BOOTLOADER;
  6593. return PT_MODE_UNKNOWN;
  6594. }
  6595. /*******************************************************************************
  6596. * FUNCTION: _pt_request_get_mode
  6597. *
  6598. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6599. * to determine the current mode of the DUT by use of the Get HID
  6600. * Descriptor command.
  6601. *
  6602. * RETURN:
  6603. * 0 = success
  6604. * !0 = failure
  6605. *
  6606. * PARAMETERS:
  6607. * *dev - pointer to device structure
  6608. * protect - run command in protected mode
  6609. * *mode - pointer to store the retrieved mode
  6610. ******************************************************************************/
  6611. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6612. {
  6613. struct pt_core_data *cd = dev_get_drvdata(dev);
  6614. struct pt_hid_desc hid_desc;
  6615. int rc = 0;
  6616. memset(&hid_desc, 0, sizeof(hid_desc));
  6617. if (protect)
  6618. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6619. else
  6620. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6621. if (rc)
  6622. *mode = PT_MODE_UNKNOWN;
  6623. else
  6624. *mode = pt_get_mode(cd, &hid_desc);
  6625. return rc;
  6626. }
  6627. /*******************************************************************************
  6628. * FUNCTION: pt_queue_enum_
  6629. *
  6630. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6631. * function. It won't try to add/delete sysfs node or modules.
  6632. *
  6633. * RETURN: void
  6634. *
  6635. * PARAMETERS:
  6636. * *cd - pointer to core data
  6637. ******************************************************************************/
  6638. static void pt_queue_enum_(struct pt_core_data *cd)
  6639. {
  6640. if (cd->startup_state == STARTUP_NONE) {
  6641. cd->startup_state = STARTUP_QUEUED;
  6642. #ifdef TTDL_DIAGNOSTICS
  6643. if (!cd->bridge_mode)
  6644. schedule_work(&cd->enum_work);
  6645. else
  6646. cd->startup_state = STARTUP_NONE;
  6647. #else
  6648. schedule_work(&cd->enum_work);
  6649. #endif
  6650. pt_debug(cd->dev, DL_INFO,
  6651. "%s: enum_work queued\n", __func__);
  6652. } else {
  6653. pt_debug(cd->dev, DL_WARN,
  6654. "%s: Enum not queued - startup_state = %d\n",
  6655. __func__, cd->startup_state);
  6656. }
  6657. }
  6658. /*******************************************************************************
  6659. * FUNCTION: pt_queue_enum
  6660. *
  6661. * SUMMARY: Queues a TTDL enum within a mutex lock
  6662. *
  6663. * RETURN: void
  6664. *
  6665. * PARAMETERS:
  6666. * *cd - pointer to core data
  6667. ******************************************************************************/
  6668. static void pt_queue_enum(struct pt_core_data *cd)
  6669. {
  6670. mutex_lock(&cd->system_lock);
  6671. pt_queue_enum_(cd);
  6672. mutex_unlock(&cd->system_lock);
  6673. }
  6674. static void remove_sysfs_and_modules(struct device *dev);
  6675. /*******************************************************************************
  6676. * FUNCTION: pt_queue_restart
  6677. *
  6678. * SUMMARY: Queues a TTDL restart within a mutex lock
  6679. *
  6680. * RETURN: void
  6681. *
  6682. * PARAMETERS:
  6683. * *cd - pointer to core data
  6684. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6685. * False: will not perform remove action
  6686. ******************************************************************************/
  6687. static void pt_queue_restart(struct pt_core_data *cd)
  6688. {
  6689. mutex_lock(&cd->system_lock);
  6690. if (cd->startup_state == STARTUP_NONE) {
  6691. cd->startup_state = STARTUP_QUEUED;
  6692. schedule_work(&cd->ttdl_restart_work);
  6693. pt_debug(cd->dev, DL_INFO,
  6694. "%s: pt_ttdl_restart queued\n", __func__);
  6695. } else {
  6696. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6697. __func__, cd->startup_state);
  6698. }
  6699. mutex_unlock(&cd->system_lock);
  6700. }
  6701. /*******************************************************************************
  6702. * FUNCTION: call_atten_cb
  6703. *
  6704. * SUMMARY: Iterate over attention list call the function that registered.
  6705. *
  6706. * RETURN: void
  6707. *
  6708. * PARAMETERS:
  6709. * *cd - pointer to core data
  6710. * type - type of attention list
  6711. * mode - condition for execution
  6712. ******************************************************************************/
  6713. static void call_atten_cb(struct pt_core_data *cd,
  6714. enum pt_atten_type type, int mode)
  6715. {
  6716. struct atten_node *atten, *atten_n;
  6717. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6718. __func__, type, mode);
  6719. spin_lock(&cd->spinlock);
  6720. list_for_each_entry_safe(atten, atten_n,
  6721. &cd->atten_list[type], node) {
  6722. if (!mode || atten->mode & mode) {
  6723. spin_unlock(&cd->spinlock);
  6724. pt_debug(cd->dev, DL_DEBUG,
  6725. "%s: attention for '%s'",
  6726. __func__, dev_name(atten->dev));
  6727. atten->func(atten->dev);
  6728. spin_lock(&cd->spinlock);
  6729. }
  6730. }
  6731. spin_unlock(&cd->spinlock);
  6732. }
  6733. /*******************************************************************************
  6734. * FUNCTION: start_fw_upgrade
  6735. *
  6736. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6737. * start firmware upgrade.
  6738. *
  6739. * RETURN:
  6740. * 0 = success
  6741. *
  6742. * PARAMETERS:
  6743. * *data - pointer to core data
  6744. ******************************************************************************/
  6745. static int start_fw_upgrade(void *data)
  6746. {
  6747. struct pt_core_data *cd = (struct pt_core_data *)data;
  6748. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6749. return 0;
  6750. }
  6751. /*******************************************************************************
  6752. * FUNCTION: pt_put_device_into_easy_wakeup_
  6753. *
  6754. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6755. * wake up state.
  6756. *
  6757. * RETURN:
  6758. * 0 = success
  6759. * !0 = failure
  6760. *
  6761. * PARAMETERS:
  6762. * *cd - pointer to core data
  6763. ******************************************************************************/
  6764. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6765. {
  6766. int rc = 0;
  6767. u8 status = 0;
  6768. mutex_lock(&cd->system_lock);
  6769. cd->wait_until_wake = 0;
  6770. mutex_unlock(&cd->system_lock);
  6771. rc = pt_hid_output_enter_easywake_state_(cd,
  6772. cd->easy_wakeup_gesture, &status);
  6773. if (rc || status == 0)
  6774. return -EBUSY;
  6775. return rc;
  6776. }
  6777. /*******************************************************************************
  6778. * FUNCTION: pt_put_device_into_deep_sleep_
  6779. *
  6780. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6781. *
  6782. * RETURN:
  6783. * 0 = success
  6784. * !0 = error
  6785. *
  6786. * PARAMETERS:
  6787. * *cd - pointer to core data
  6788. ******************************************************************************/
  6789. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6790. {
  6791. int rc = 0;
  6792. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6793. if (rc)
  6794. rc = -EBUSY;
  6795. return rc;
  6796. }
  6797. /*******************************************************************************
  6798. * FUNCTION: pt_put_device_into_deep_standby_
  6799. *
  6800. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6801. *
  6802. * RETURN:
  6803. * 0 = success
  6804. * !0 = error
  6805. *
  6806. * PARAMETERS:
  6807. * *cd - pointer to core data
  6808. ******************************************************************************/
  6809. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6810. {
  6811. int rc = 0;
  6812. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6813. if (rc)
  6814. rc = -EBUSY;
  6815. return rc;
  6816. }
  6817. /*******************************************************************************
  6818. * FUNCTION: pt_core_poweroff_device_
  6819. *
  6820. * SUMMARY: Disable IRQ and HW power down the device.
  6821. *
  6822. * RETURN:
  6823. * 0 = success
  6824. * !0 = failure
  6825. *
  6826. * PARAMETERS:
  6827. * *cd - pointer to core data
  6828. ******************************************************************************/
  6829. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6830. {
  6831. int rc;
  6832. if (cd->irq_enabled) {
  6833. cd->irq_enabled = false;
  6834. disable_irq_nosync(cd->irq);
  6835. }
  6836. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6837. if (rc < 0)
  6838. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6839. __func__, rc);
  6840. return rc;
  6841. }
  6842. /*******************************************************************************
  6843. * FUNCTION: pt_core_sleep_
  6844. *
  6845. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6846. * configuration in the core platform data structure.
  6847. *
  6848. * RETURN:
  6849. * 0 = success
  6850. * !0 = failure
  6851. *
  6852. * PARAMETERS:
  6853. * *cd - pointer to core data
  6854. ******************************************************************************/
  6855. static int pt_core_sleep_(struct pt_core_data *cd)
  6856. {
  6857. int rc = 0;
  6858. mutex_lock(&cd->system_lock);
  6859. if (cd->sleep_state == SS_SLEEP_OFF) {
  6860. cd->sleep_state = SS_SLEEPING;
  6861. } else {
  6862. mutex_unlock(&cd->system_lock);
  6863. return 1;
  6864. }
  6865. mutex_unlock(&cd->system_lock);
  6866. /* Ensure watchdog and startup works stopped */
  6867. pt_stop_wd_timer(cd);
  6868. cancel_work_sync(&cd->enum_work);
  6869. pt_stop_wd_timer(cd);
  6870. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  6871. rc = pt_put_device_into_easy_wakeup_(cd);
  6872. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  6873. rc = pt_core_poweroff_device_(cd);
  6874. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  6875. rc = pt_put_device_into_deep_standby_(cd);
  6876. else
  6877. rc = pt_put_device_into_deep_sleep_(cd);
  6878. mutex_lock(&cd->system_lock);
  6879. cd->sleep_state = SS_SLEEP_ON;
  6880. mutex_unlock(&cd->system_lock);
  6881. return rc;
  6882. }
  6883. /*******************************************************************************
  6884. * FUNCTION: pt_core_sleep
  6885. *
  6886. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6887. *
  6888. * RETURN:
  6889. * 0 = success
  6890. * !0 = failure
  6891. *
  6892. * PARAMETERS:
  6893. * *cd - pointer to core data
  6894. ******************************************************************************/
  6895. static int pt_core_sleep(struct pt_core_data *cd)
  6896. {
  6897. int rc = 0;
  6898. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6899. if (rc < 0) {
  6900. pt_debug(cd->dev, DL_ERROR,
  6901. "%s: fail get exclusive ex=%p own=%p\n",
  6902. __func__, cd->exclusive_dev, cd->dev);
  6903. return rc;
  6904. }
  6905. rc = pt_core_sleep_(cd);
  6906. if (release_exclusive(cd, cd->dev) < 0)
  6907. pt_debug(cd->dev, DL_ERROR,
  6908. "%s: fail to release exclusive\n", __func__);
  6909. else
  6910. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6911. __func__);
  6912. return rc;
  6913. }
  6914. /*******************************************************************************
  6915. * FUNCTION: pt_wakeup_host
  6916. *
  6917. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  6918. *
  6919. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  6920. *
  6921. * RETURN:
  6922. * 0 = success
  6923. * !0 = failure
  6924. *
  6925. * PARAMETERS:
  6926. * *cd - pointer to core data
  6927. ******************************************************************************/
  6928. static int pt_wakeup_host(struct pt_core_data *cd)
  6929. {
  6930. #ifndef EASYWAKE_TSG6
  6931. /* TSG5 EasyWake */
  6932. int rc = 0;
  6933. int event_id;
  6934. int size = get_unaligned_le16(&cd->input_buf[0]);
  6935. /* Validate report */
  6936. if (size != 4 || cd->input_buf[2] != 4)
  6937. rc = -EINVAL;
  6938. event_id = cd->input_buf[3];
  6939. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  6940. __func__, event_id, rc);
  6941. if (rc) {
  6942. pt_core_sleep_(cd);
  6943. goto exit;
  6944. }
  6945. /* attention WAKE */
  6946. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6947. exit:
  6948. return rc;
  6949. #else
  6950. /* TSG6 FW1.3 EasyWake */
  6951. int rc = 0;
  6952. int i = 0;
  6953. int report_length;
  6954. /* Validate report */
  6955. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  6956. rc = -EINVAL;
  6957. pt_core_sleep_(cd);
  6958. goto exit;
  6959. }
  6960. /* Get gesture id and gesture data length */
  6961. cd->gesture_id = cd->input_buf[3];
  6962. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  6963. cd->gesture_data_length = report_length - 4;
  6964. pt_debug(cd->dev, DL_INFO,
  6965. "%s: gesture_id = %d, gesture_data_length = %d\n",
  6966. __func__, cd->gesture_id, cd->gesture_data_length);
  6967. for (i = 0; i < cd->gesture_data_length; i++)
  6968. cd->gesture_data[i] = cd->input_buf[4 + i];
  6969. /* attention WAKE */
  6970. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  6971. exit:
  6972. return rc;
  6973. #endif
  6974. }
  6975. /*******************************************************************************
  6976. * FUNCTION: pt_get_touch_axis
  6977. *
  6978. * SUMMARY: Function to calculate touch axis
  6979. *
  6980. * PARAMETERS:
  6981. * *cd - pointer to core data structure
  6982. * *axis - pointer to axis calculation result
  6983. * size - size in bytes
  6984. * max - max value of result
  6985. * *xy_data - pointer to input data to be parsed
  6986. * bofs - bit offset
  6987. ******************************************************************************/
  6988. static void pt_get_touch_axis(struct pt_core_data *cd,
  6989. int *axis, int size, int max, u8 *data, int bofs)
  6990. {
  6991. int nbyte;
  6992. int next;
  6993. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  6994. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  6995. next++;
  6996. }
  6997. *axis &= max - 1;
  6998. }
  6999. /*******************************************************************************
  7000. * FUNCTION: move_tracking_heatmap_data
  7001. *
  7002. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7003. * system information structure, xy_mode and xy_data.
  7004. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7005. * the tthe_tuner sysfs node under the label "THM"
  7006. *
  7007. * RETURN:
  7008. * 0 = success
  7009. * !0 = failure
  7010. *
  7011. * PARAMETERS:
  7012. * *cd - pointer to core data
  7013. * *si - pointer to the system information structure
  7014. ******************************************************************************/
  7015. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7016. struct pt_sysinfo *si)
  7017. {
  7018. #ifdef TTHE_TUNER_SUPPORT
  7019. int size = get_unaligned_le16(&cd->input_buf[0]);
  7020. if (size)
  7021. tthe_print(cd, cd->input_buf, size, "THM=");
  7022. #endif
  7023. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7024. return 0;
  7025. }
  7026. /*******************************************************************************
  7027. * FUNCTION: move_sensor_data
  7028. *
  7029. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7030. * information structure, xy_mode and xy_data.
  7031. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7032. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7033. *
  7034. * RETURN:
  7035. * 0 = success
  7036. * !0 = failure
  7037. *
  7038. * PARAMETERS:
  7039. * *cd - pointer to core data
  7040. * *si - pointer to the system information structure
  7041. ******************************************************************************/
  7042. static int move_sensor_data(struct pt_core_data *cd,
  7043. struct pt_sysinfo *si)
  7044. {
  7045. #ifdef TTHE_TUNER_SUPPORT
  7046. int size = get_unaligned_le16(&cd->input_buf[0]);
  7047. if (size)
  7048. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7049. #endif
  7050. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7051. return 0;
  7052. }
  7053. /*******************************************************************************
  7054. * FUNCTION: move_button_data
  7055. *
  7056. * SUMMARY: Move the valid button data from the input buffer into the system
  7057. * information structure, xy_mode and xy_data.
  7058. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7059. * the tthe_tuner sysfs node under the label "OpModeData"
  7060. *
  7061. * RETURN:
  7062. * 0 = success
  7063. * !0 = failure
  7064. *
  7065. * PARAMETERS:
  7066. * *cd - pointer to core data
  7067. * *si - pointer to the system information structure
  7068. ******************************************************************************/
  7069. static int move_button_data(struct pt_core_data *cd,
  7070. struct pt_sysinfo *si)
  7071. {
  7072. #ifdef TTHE_TUNER_SUPPORT
  7073. int size = get_unaligned_le16(&cd->input_buf[0]);
  7074. if (size)
  7075. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7076. #endif
  7077. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7078. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7079. "xy_mode");
  7080. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7081. BTN_REPORT_SIZE);
  7082. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7083. "xy_data");
  7084. return 0;
  7085. }
  7086. /*******************************************************************************
  7087. * FUNCTION: move_touch_data
  7088. *
  7089. * SUMMARY: Move the valid touch data from the input buffer into the system
  7090. * information structure, xy_mode and xy_data.
  7091. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7092. * the tthe_tuner sysfs node under the label "OpModeData"
  7093. *
  7094. * RETURN:
  7095. * 0 = success
  7096. * !0 = failure
  7097. *
  7098. * PARAMETERS:
  7099. * *cd - pointer to core data
  7100. * *si - pointer to the system information structure
  7101. ******************************************************************************/
  7102. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7103. {
  7104. int max_tch = si->sensing_conf_data.max_tch;
  7105. int num_cur_tch;
  7106. int length;
  7107. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7108. #ifdef TTHE_TUNER_SUPPORT
  7109. int size = get_unaligned_le16(&cd->input_buf[0]);
  7110. if (size)
  7111. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7112. #endif
  7113. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7114. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7115. si->desc.tch_header_size, "xy_mode");
  7116. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7117. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7118. if (unlikely(num_cur_tch > max_tch))
  7119. num_cur_tch = max_tch;
  7120. length = num_cur_tch * si->desc.tch_record_size;
  7121. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7122. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7123. return 0;
  7124. }
  7125. /*******************************************************************************
  7126. * FUNCTION: move_hid_pen_data
  7127. *
  7128. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7129. * information structure, xy_mode and xy_data.
  7130. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7131. * the tthe_tuner sysfs node under the label "HID" starting with the
  7132. * report ID.
  7133. *
  7134. * RETURN:
  7135. * 0 = success
  7136. * !0 = failure
  7137. *
  7138. * PARAMETERS:
  7139. * *cd - pointer to core data
  7140. * *si - pointer to the system information structure
  7141. ******************************************************************************/
  7142. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7143. {
  7144. #ifdef TTHE_TUNER_SUPPORT
  7145. int size = get_unaligned_le16(&cd->input_buf[0]);
  7146. if (size) {
  7147. /*
  7148. * HID over USB does not require the two byte length field, so
  7149. * this should print from input_buf[2] but to keep both finger
  7150. * and pen reports the same the length is included
  7151. */
  7152. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7153. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7154. "HID-USB=");
  7155. else
  7156. tthe_print(cd, &(cd->input_buf[0]), size,
  7157. "HID-I2C=");
  7158. }
  7159. #endif
  7160. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7161. return 0;
  7162. }
  7163. /*******************************************************************************
  7164. * FUNCTION: parse_touch_input
  7165. *
  7166. * SUMMARY: Parse the touch report and take action based on the touch
  7167. * report_id.
  7168. *
  7169. * RETURN:
  7170. * 0 = success
  7171. * !0 = failure
  7172. *
  7173. * PARAMETERS:
  7174. * *cd - pointer to core data
  7175. * size - size of touch record
  7176. ******************************************************************************/
  7177. static int parse_touch_input(struct pt_core_data *cd, int size)
  7178. {
  7179. struct pt_sysinfo *si = &cd->sysinfo;
  7180. int report_id = cd->input_buf[2];
  7181. int rc = -EINVAL;
  7182. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7183. __func__);
  7184. if (!si->ready) {
  7185. pt_debug(cd->dev, DL_ERROR,
  7186. "%s: Need system information to parse touches\n",
  7187. __func__);
  7188. return 0;
  7189. }
  7190. if (!si->xy_mode || !si->xy_data)
  7191. return rc;
  7192. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7193. rc = move_touch_data(cd, si);
  7194. else if (report_id == PT_HID_PEN_REPORT_ID)
  7195. rc = move_hid_pen_data(cd, si);
  7196. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7197. rc = move_button_data(cd, si);
  7198. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7199. rc = move_sensor_data(cd, si);
  7200. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7201. rc = move_tracking_heatmap_data(cd, si);
  7202. if (rc)
  7203. return rc;
  7204. /* attention IRQ */
  7205. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7206. return 0;
  7207. }
  7208. /*******************************************************************************
  7209. * FUNCTION: parse_command_input
  7210. *
  7211. * SUMMARY: Move the response data from the input buffer to the response buffer
  7212. *
  7213. * RETURN:
  7214. * 0 = success
  7215. * !0 = failure
  7216. *
  7217. * PARAMETERS:
  7218. * *cd - pointer to core data
  7219. * size - size of response data
  7220. ******************************************************************************/
  7221. static int parse_command_input(struct pt_core_data *cd, int size)
  7222. {
  7223. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7224. __func__);
  7225. memcpy(cd->response_buf, cd->input_buf, size);
  7226. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7227. if (size && cd->show_tt_data) {
  7228. if (cd->pip2_prot_active)
  7229. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7230. else
  7231. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7232. }
  7233. #endif
  7234. mutex_lock(&cd->system_lock);
  7235. cd->hid_cmd_state = 0;
  7236. mutex_unlock(&cd->system_lock);
  7237. wake_up(&cd->wait_q);
  7238. return 0;
  7239. }
  7240. /*******************************************************************************
  7241. * FUNCTION: pt_allow_enumeration
  7242. *
  7243. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7244. * FW sentinel is seen.
  7245. *
  7246. * RETURN:
  7247. * true = allow enumeration or fully re-probe
  7248. * false = skip enumeration and fully re-probe
  7249. *
  7250. * PARAMETERS:
  7251. * *cd - pointer to core data
  7252. ******************************************************************************/
  7253. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7254. {
  7255. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7256. (!cd->hid_reset_cmd_state) &&
  7257. (cd->core_probe_complete) &&
  7258. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7259. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7260. (cd->mode == PT_MODE_OPERATIONAL)) {
  7261. return true;
  7262. }
  7263. if ((!cd->hid_reset_cmd_state) &&
  7264. (cd->core_probe_complete) &&
  7265. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7266. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7267. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7268. return true;
  7269. }
  7270. pt_debug(cd->dev, DL_INFO,
  7271. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7272. __func__,
  7273. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7274. "core_probe_complete(1)", cd->core_probe_complete,
  7275. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7276. "active_dut_gen(0,2)", cd->active_dut_generation);
  7277. return false;
  7278. }
  7279. /*******************************************************************************
  7280. * FUNCTION: pt_is_touch_report
  7281. *
  7282. * SUMMARY: Determine if a report ID should be treated as a touch report
  7283. *
  7284. * RETURN:
  7285. * true = report ID is a touch report
  7286. * false = report ID is not a touch report
  7287. *
  7288. * PARAMETERS:
  7289. * id - Report ID
  7290. ******************************************************************************/
  7291. static bool pt_is_touch_report(int id)
  7292. {
  7293. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7294. id == PT_HID_PEN_REPORT_ID ||
  7295. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7296. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7297. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7298. return true;
  7299. else
  7300. return false;
  7301. }
  7302. /*******************************************************************************
  7303. * FUNCTION: pt_parse_input
  7304. *
  7305. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7306. * on if its a response to a command or asynchronous touch data / reset
  7307. * sentinel. PIP2.x messages have additional error checking that is
  7308. * parsed (SEQ match from cmd to rsp, CRC valid).
  7309. * Look for special packets based on unique lengths:
  7310. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7311. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7312. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7313. * 0xFFXX - Empty buffer (PIP 1.7+)
  7314. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7315. * the initial probe has not yet been done.
  7316. *
  7317. * RETURN:
  7318. * 0 = success
  7319. * !0 = failure
  7320. *
  7321. * PARAMETERS:
  7322. * *cd - pointer to core data
  7323. ******************************************************************************/
  7324. static int pt_parse_input(struct pt_core_data *cd)
  7325. {
  7326. int report_id;
  7327. int cmd_id;
  7328. int is_command = 0;
  7329. int size;
  7330. int print_size;
  7331. bool touch_report = true;
  7332. unsigned short calc_crc;
  7333. unsigned short resp_crc;
  7334. cd->fw_sys_mode_in_standby_state = false;
  7335. size = get_unaligned_le16(&cd->input_buf[0]);
  7336. print_size = size;
  7337. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7338. __func__, print_size);
  7339. if (print_size <= PT_MAX_INPUT)
  7340. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7341. "<<< Read buf");
  7342. if (size == 0 ||
  7343. (size == 11 &&
  7344. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7345. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7346. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7347. PIP2_CMD_COMMAND_ID_MASK) ==
  7348. PIP2_CMD_ID_STATUS)) {
  7349. touch_report = false;
  7350. cd->hw_detected = true;
  7351. cd->bl_pip_ver_ready = false;
  7352. cd->app_pip_ver_ready = false;
  7353. if (size == 0) {
  7354. mutex_lock(&cd->system_lock);
  7355. cd->pip2_prot_active = false;
  7356. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7357. /*
  7358. * For Gen5/6 this sentinel could be from
  7359. * the BL or FW. Attempt to set the correct
  7360. * mode based on the previous PIP command.
  7361. */
  7362. if (cd->hid_cmd_state ==
  7363. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7364. cd->mode = PT_MODE_OPERATIONAL;
  7365. cd->startup_status =
  7366. STARTUP_STATUS_FW_RESET_SENTINEL;
  7367. } else if (cd->hid_cmd_state ==
  7368. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7369. cd->hid_reset_cmd_state) {
  7370. cd->mode = PT_MODE_BOOTLOADER;
  7371. cd->startup_status =
  7372. STARTUP_STATUS_BL_RESET_SENTINEL;
  7373. } else {
  7374. cd->mode = PT_MODE_UNKNOWN;
  7375. cd->startup_status =
  7376. STARTUP_STATUS_START;
  7377. }
  7378. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7379. pt_debug(cd->dev, DL_INFO,
  7380. "%s: ATM Gen5/6 %s sentinel received\n",
  7381. __func__,
  7382. (cd->mode == PT_MODE_OPERATIONAL ?
  7383. "FW" :
  7384. (cd->mode == PT_MODE_BOOTLOADER ?
  7385. "BL" : "Unknown")));
  7386. } else {
  7387. cd->mode = PT_MODE_OPERATIONAL;
  7388. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7389. cd->startup_status =
  7390. STARTUP_STATUS_FW_RESET_SENTINEL;
  7391. pt_debug(cd->dev, DL_INFO,
  7392. "%s: ATM PT/TT FW sentinel received\n",
  7393. __func__);
  7394. }
  7395. mutex_unlock(&cd->system_lock);
  7396. if (pt_allow_enumeration(cd)) {
  7397. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7398. pt_debug(cd->dev, DL_INFO,
  7399. "%s: Queue Restart\n", __func__);
  7400. pt_queue_restart(cd);
  7401. } else {
  7402. pt_debug(cd->dev, DL_INFO,
  7403. "%s: Queue Enum\n", __func__);
  7404. pt_queue_enum(cd);
  7405. }
  7406. } else {
  7407. pt_debug(cd->dev, DL_INFO,
  7408. "%s: Sentinel - No Queued Action\n",
  7409. __func__);
  7410. }
  7411. } else {
  7412. /* Sentinel must be from TT/TC BL */
  7413. mutex_lock(&cd->system_lock);
  7414. cd->pip2_prot_active = true;
  7415. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7416. cd->mode = PT_MODE_BOOTLOADER;
  7417. cd->sysinfo.ready = false;
  7418. mutex_unlock(&cd->system_lock);
  7419. pt_debug(cd->dev, DL_INFO,
  7420. "%s: BL Reset sentinel received\n", __func__);
  7421. if (cd->flashless_dut &&
  7422. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7423. pt_debug(cd->dev, DL_INFO,
  7424. "%s: BL to RAM for flashless DUT\n",
  7425. __func__);
  7426. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7427. }
  7428. }
  7429. mutex_lock(&cd->system_lock);
  7430. memcpy(cd->response_buf, cd->input_buf, 2);
  7431. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7432. mutex_unlock(&cd->system_lock);
  7433. pt_debug(cd->dev, DL_WARN,
  7434. "%s: Device Initiated Reset\n", __func__);
  7435. wake_up(&cd->wait_q);
  7436. return 0;
  7437. }
  7438. cd->hid_reset_cmd_state = 0;
  7439. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7440. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7441. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7442. cd->hid_cmd_state = 0;
  7443. wake_up(&cd->wait_q);
  7444. mutex_unlock(&cd->system_lock);
  7445. return 0;
  7446. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7447. /*
  7448. * This debug message below is used by PBATS to calculate the
  7449. * time from the last lift off IRQ to when FW exits LFT mode.
  7450. */
  7451. touch_report = false;
  7452. pt_debug(cd->dev, DL_WARN,
  7453. "%s: DUT - Empty buffer detected\n", __func__);
  7454. return 0;
  7455. } else if (size > PT_MAX_INPUT) {
  7456. pt_debug(cd->dev, DL_ERROR,
  7457. "%s: DUT - Unexpected len field in active bus data!\n",
  7458. __func__);
  7459. return -EINVAL;
  7460. }
  7461. if (cd->pip2_prot_active) {
  7462. pt_debug(cd->dev, DL_DEBUG,
  7463. "%s: Decode PIP2.x Response\n", __func__);
  7464. /* PIP2 does not have a report id, hard code it */
  7465. report_id = 0x00;
  7466. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7467. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7468. resp_crc = cd->input_buf[size - 2] << 8;
  7469. resp_crc |= cd->input_buf[size - 1];
  7470. if ((cd->pip2_cmd_tag_seq !=
  7471. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7472. (resp_crc != calc_crc) &&
  7473. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7474. == PT_PIP_TOUCH_REPORT_ID) ||
  7475. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7476. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7477. pt_debug(cd->dev, DL_WARN,
  7478. "%s: %s %d %s\n",
  7479. __func__,
  7480. "Received PIP1 report id =",
  7481. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7482. "when expecting a PIP2 report - IGNORE report");
  7483. return 0;
  7484. }
  7485. is_command = 1;
  7486. touch_report = false;
  7487. } else {
  7488. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7489. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7490. }
  7491. #ifdef TTDL_DIAGNOSTICS
  7492. pt_debug(cd->dev, DL_DEBUG,
  7493. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7494. __func__, cd->pip2_prot_active, report_id,
  7495. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7496. #endif /* TTDL_DIAGNOSTICS */
  7497. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7498. pt_wakeup_host(cd);
  7499. #ifdef TTHE_TUNER_SUPPORT
  7500. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7501. #endif
  7502. return 0;
  7503. }
  7504. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7505. msecs_to_jiffies(cd->watchdog_interval));
  7506. /*
  7507. * If it is PIP2 response, the report_id has been set to 0,
  7508. * so it will not be parsed as a touch packet.
  7509. */
  7510. if (!pt_is_touch_report(report_id)) {
  7511. is_command = 1;
  7512. touch_report = false;
  7513. }
  7514. if (unlikely(is_command)) {
  7515. parse_command_input(cd, size);
  7516. return 0;
  7517. }
  7518. if (touch_report)
  7519. parse_touch_input(cd, size);
  7520. return 0;
  7521. }
  7522. /*******************************************************************************
  7523. * FUNCTION: pt_read_input
  7524. *
  7525. * SUMMARY: Reads incoming data off of the active bus
  7526. *
  7527. * RETURN:
  7528. * 0 = success
  7529. * !0 = failure
  7530. *
  7531. * PARAMETERS:
  7532. * *cd - pointer to core data
  7533. ******************************************************************************/
  7534. static int pt_read_input(struct pt_core_data *cd)
  7535. {
  7536. struct device *dev = cd->dev;
  7537. int rc = 0;
  7538. int t;
  7539. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7540. /*
  7541. * Workaround for easywake failure
  7542. * Interrupt for easywake, wait for bus controller to wake
  7543. */
  7544. mutex_lock(&cd->system_lock);
  7545. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7546. if (cd->sleep_state == SS_SLEEP_ON) {
  7547. mutex_unlock(&cd->system_lock);
  7548. if (!dev->power.is_suspended)
  7549. goto read;
  7550. t = wait_event_timeout(cd->wait_q,
  7551. (cd->wait_until_wake == 1),
  7552. msecs_to_jiffies(2000));
  7553. #ifdef TTDL_DIAGNOSTICS
  7554. if (IS_TMO(t)) {
  7555. cd->bus_transmit_error_count++;
  7556. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7557. pt_debug(dev, DL_ERROR,
  7558. "%s: !!!I2C Transmission Error %d\n",
  7559. __func__,
  7560. cd->bus_transmit_error_count);
  7561. }
  7562. #else
  7563. if (IS_TMO(t))
  7564. pt_queue_enum(cd);
  7565. #endif /* TTDL_DIAGNOSTICS */
  7566. goto read;
  7567. }
  7568. }
  7569. mutex_unlock(&cd->system_lock);
  7570. read:
  7571. /* Try reading up to 'retry' times */
  7572. while (retry-- != 0) {
  7573. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7574. PT_MAX_INPUT);
  7575. if (!rc) {
  7576. pt_debug(dev, DL_DEBUG,
  7577. "%s: Read input successfully\n", __func__);
  7578. goto read_exit;
  7579. }
  7580. usleep_range(1000, 2000);
  7581. }
  7582. pt_debug(dev, DL_ERROR,
  7583. "%s: Error getting report, rc=%d\n", __func__, rc);
  7584. read_exit:
  7585. return rc;
  7586. }
  7587. /*******************************************************************************
  7588. * FUNCTION: pt_irq
  7589. *
  7590. * SUMMARY: Process all detected interrupts
  7591. *
  7592. * RETURN:
  7593. * IRQ_HANDLED - Finished processing the interrupt
  7594. *
  7595. * PARAMETERS:
  7596. * irq - IRQ number
  7597. * *handle - pointer to core data struct
  7598. ******************************************************************************/
  7599. irqreturn_t pt_irq(int irq, void *handle)
  7600. {
  7601. struct pt_core_data *cd = handle;
  7602. int rc = 0;
  7603. if (!pt_check_irq_asserted(cd))
  7604. return IRQ_HANDLED;
  7605. rc = pt_read_input(cd);
  7606. #ifdef TTDL_DIAGNOSTICS
  7607. cd->irq_count++;
  7608. /* Used to calculate T-Refresh */
  7609. if (cd->t_refresh_active) {
  7610. if (cd->t_refresh_count == 0) {
  7611. cd->t_refresh_time = jiffies;
  7612. cd->t_refresh_count++;
  7613. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7614. cd->t_refresh_count++;
  7615. } else {
  7616. cd->t_refresh_active = 0;
  7617. cd->t_refresh_time =
  7618. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7619. }
  7620. }
  7621. #endif /* TTDL_DIAGNOSTICS */
  7622. if (!rc)
  7623. pt_parse_input(cd);
  7624. return IRQ_HANDLED;
  7625. }
  7626. /*******************************************************************************
  7627. * FUNCTION: _pt_subscribe_attention
  7628. *
  7629. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7630. * to subscribe themselves into the TTDL attention list
  7631. *
  7632. * RETURN:
  7633. * 0 = success
  7634. * !0 = failure
  7635. *
  7636. * PARAMETERS:
  7637. * *dev - pointer to device structure
  7638. * type - attention type enum
  7639. * *id - ID of the module calling this function
  7640. * *func - callback function pointer to be called when notified
  7641. * mode - attention mode
  7642. ******************************************************************************/
  7643. int _pt_subscribe_attention(struct device *dev,
  7644. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7645. int mode)
  7646. {
  7647. struct pt_core_data *cd = dev_get_drvdata(dev);
  7648. struct atten_node *atten, *atten_new;
  7649. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7650. if (!atten_new)
  7651. return -ENOMEM;
  7652. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7653. dev_name(cd->dev));
  7654. spin_lock(&cd->spinlock);
  7655. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7656. if (atten->id == id && atten->mode == mode) {
  7657. spin_unlock(&cd->spinlock);
  7658. kfree(atten_new);
  7659. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7660. __func__,
  7661. "already subscribed attention",
  7662. dev, "mode", mode);
  7663. return 0;
  7664. }
  7665. }
  7666. atten_new->id = id;
  7667. atten_new->dev = dev;
  7668. atten_new->mode = mode;
  7669. atten_new->func = func;
  7670. list_add(&atten_new->node, &cd->atten_list[type]);
  7671. spin_unlock(&cd->spinlock);
  7672. return 0;
  7673. }
  7674. /*******************************************************************************
  7675. * FUNCTION: _pt_unsubscribe_attention
  7676. *
  7677. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7678. * to unsubscribe themselves from the TTDL attention list
  7679. *
  7680. * RETURN:
  7681. * 0 = success
  7682. * !0 = failure
  7683. *
  7684. * PARAMETERS:
  7685. * *dev - pointer to device structure
  7686. * type - attention type enum
  7687. * *id - ID of the module calling this function
  7688. * *func - function pointer
  7689. * mode - attention mode
  7690. ******************************************************************************/
  7691. int _pt_unsubscribe_attention(struct device *dev,
  7692. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7693. int mode)
  7694. {
  7695. struct pt_core_data *cd = dev_get_drvdata(dev);
  7696. struct atten_node *atten, *atten_n;
  7697. spin_lock(&cd->spinlock);
  7698. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7699. if (atten->id == id && atten->mode == mode) {
  7700. list_del(&atten->node);
  7701. spin_unlock(&cd->spinlock);
  7702. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7703. __func__,
  7704. "unsub for atten->dev", atten->dev,
  7705. "atten->mode", atten->mode);
  7706. kfree(atten);
  7707. return 0;
  7708. }
  7709. }
  7710. spin_unlock(&cd->spinlock);
  7711. return -ENODEV;
  7712. }
  7713. /*******************************************************************************
  7714. * FUNCTION: _pt_request_exclusive
  7715. *
  7716. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7717. * to request exclusive access
  7718. *
  7719. * RETURN:
  7720. * 0 = success
  7721. * !0 = failure
  7722. *
  7723. * PARAMETERS:
  7724. * *dev - pointer to device structure
  7725. * timeout_ms - timeout to wait for exclusive access
  7726. ******************************************************************************/
  7727. static int _pt_request_exclusive(struct device *dev,
  7728. int timeout_ms)
  7729. {
  7730. struct pt_core_data *cd = dev_get_drvdata(dev);
  7731. return request_exclusive(cd, (void *)dev, timeout_ms);
  7732. }
  7733. /*******************************************************************************
  7734. * FUNCTION: _pt_release_exclusive
  7735. *
  7736. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7737. * to release exclusive access
  7738. *
  7739. * RETURN:
  7740. * 0 = success
  7741. * !0 = failure
  7742. *
  7743. * PARAMETERS:
  7744. * *dev - pointer to device structure
  7745. ******************************************************************************/
  7746. static int _pt_release_exclusive(struct device *dev)
  7747. {
  7748. struct pt_core_data *cd = dev_get_drvdata(dev);
  7749. return release_exclusive(cd, (void *)dev);
  7750. }
  7751. /*******************************************************************************
  7752. * FUNCTION: _pt_request_reset
  7753. *
  7754. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7755. * to request the DUT to be reset. Function returns err if refused or
  7756. * timeout occurs (Note: core uses fixed timeout period).
  7757. *
  7758. * NOTE: Function blocks until ISR occurs.
  7759. *
  7760. * RETURN:
  7761. * 0 = success
  7762. * !0 = failure
  7763. *
  7764. * PARAMETERS:
  7765. * *dev - pointer to device structure
  7766. * protect - flag to call protected or non-protected
  7767. ******************************************************************************/
  7768. static int _pt_request_reset(struct device *dev, int protect)
  7769. {
  7770. struct pt_core_data *cd = dev_get_drvdata(dev);
  7771. int rc;
  7772. rc = pt_dut_reset(cd, protect);
  7773. if (rc < 0) {
  7774. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7775. __func__, rc);
  7776. }
  7777. return rc;
  7778. }
  7779. /*******************************************************************************
  7780. * FUNCTION: _pt_request_enum
  7781. *
  7782. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7783. * to request TTDL to queue an enum. This function will return err
  7784. * if refused; if no error then enum has completed and system is in
  7785. * normal operation mode.
  7786. *
  7787. * RETURN:
  7788. * 0 = success
  7789. * !0 = failure
  7790. *
  7791. * PARAMETERS:
  7792. * *dev - pointer to device structure
  7793. * wait - boolean to determine if to wait for startup event
  7794. ******************************************************************************/
  7795. static int _pt_request_enum(struct device *dev, bool wait)
  7796. {
  7797. struct pt_core_data *cd = dev_get_drvdata(dev);
  7798. pt_queue_enum(cd);
  7799. if (wait)
  7800. wait_event_timeout(cd->wait_q,
  7801. cd->startup_state == STARTUP_NONE,
  7802. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7803. return 0;
  7804. }
  7805. /*******************************************************************************
  7806. * FUNCTION: _pt_request_sysinfo
  7807. *
  7808. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7809. * to request the pointer to the system information structure. This
  7810. * function will return NULL if sysinfo has not been acquired from the
  7811. * DUT yet.
  7812. *
  7813. * RETURN: Pointer to the system information struct
  7814. *
  7815. * PARAMETERS:
  7816. * *dev - pointer to device structure
  7817. ******************************************************************************/
  7818. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7819. {
  7820. struct pt_core_data *cd = dev_get_drvdata(dev);
  7821. int rc = 0;
  7822. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7823. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7824. !cd->sysinfo.ready) {
  7825. rc = pt_hid_output_get_sysinfo_(cd);
  7826. if (rc) {
  7827. pt_debug(cd->dev, DL_ERROR,
  7828. "%s: Error getting sysinfo rc=%d\n",
  7829. __func__, rc);
  7830. }
  7831. }
  7832. if (cd->sysinfo.ready)
  7833. return &cd->sysinfo;
  7834. return NULL;
  7835. }
  7836. /*******************************************************************************
  7837. * FUNCTION: _pt_request_loader_pdata
  7838. *
  7839. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7840. * to request the pointer to the loader platform data
  7841. *
  7842. * RETURN: Pointer to the loader platform data struct
  7843. *
  7844. * PARAMETERS:
  7845. * *dev - pointer to device structure
  7846. ******************************************************************************/
  7847. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7848. struct device *dev)
  7849. {
  7850. struct pt_core_data *cd = dev_get_drvdata(dev);
  7851. return cd->pdata->loader_pdata;
  7852. }
  7853. /*******************************************************************************
  7854. * FUNCTION: _pt_request_start_wd
  7855. *
  7856. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7857. * to request to start the TTDL watchdog
  7858. *
  7859. * RETURN:
  7860. * 0 = success
  7861. * !0 = failure
  7862. *
  7863. * PARAMETERS:
  7864. * *dev - pointer to device structure
  7865. ******************************************************************************/
  7866. static int _pt_request_start_wd(struct device *dev)
  7867. {
  7868. struct pt_core_data *cd = dev_get_drvdata(dev);
  7869. pt_start_wd_timer(cd);
  7870. return 0;
  7871. }
  7872. /*******************************************************************************
  7873. * FUNCTION: _pt_request_stop_wd
  7874. *
  7875. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7876. * to request to stop the TTDL watchdog
  7877. *
  7878. * RETURN:
  7879. * 0 = success
  7880. * !0 = failure
  7881. *
  7882. * PARAMETERS:
  7883. * *dev - pointer to device structure
  7884. ******************************************************************************/
  7885. static int _pt_request_stop_wd(struct device *dev)
  7886. {
  7887. struct pt_core_data *cd = dev_get_drvdata(dev);
  7888. pt_stop_wd_timer(cd);
  7889. return 0;
  7890. }
  7891. /*******************************************************************************
  7892. * FUNCTION: pt_pip2_launch_app
  7893. *
  7894. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7895. * the FW reset sentinel to indicate the function succeeded.
  7896. *
  7897. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7898. * in a timeout delay and with the DUT being reset with an XRES.
  7899. *
  7900. * RETURN:
  7901. * 0 = success
  7902. * !0 = failure
  7903. *
  7904. * PARAMETERS:
  7905. * *cd - pointer to core data structure
  7906. * protect - flag to call protected or non-protected
  7907. ******************************************************************************/
  7908. static int pt_pip2_launch_app(struct device *dev, int protect)
  7909. {
  7910. struct pt_core_data *cd = dev_get_drvdata(dev);
  7911. u16 actual_read_len;
  7912. u16 tmp_startup_status = cd->startup_status;
  7913. u8 read_buf[12];
  7914. u8 status;
  7915. int time = 0;
  7916. int rc = 0;
  7917. mutex_lock(&cd->system_lock);
  7918. cd->startup_status = STARTUP_STATUS_START;
  7919. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  7920. mutex_unlock(&cd->system_lock);
  7921. rc = _pt_request_pip2_send_cmd(dev, protect,
  7922. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  7923. &actual_read_len);
  7924. if (rc) {
  7925. pt_debug(dev, DL_ERROR,
  7926. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  7927. __func__, rc);
  7928. } else {
  7929. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  7930. /* Test for no or invalid image in FLASH, no point to reset */
  7931. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  7932. rc = status;
  7933. goto exit;
  7934. }
  7935. /* Any other boot failure */
  7936. if (status != 0) {
  7937. pt_debug(dev, DL_ERROR,
  7938. "%s: FW did not EXECUTE, status = %d\n",
  7939. __func__, status);
  7940. rc = status;
  7941. }
  7942. }
  7943. if (rc) {
  7944. pt_debug(dev, DL_ERROR,
  7945. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  7946. __func__, rc);
  7947. goto exit;
  7948. }
  7949. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  7950. msleep(20);
  7951. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  7952. __func__, time, cd->startup_status);
  7953. time += 20;
  7954. }
  7955. if (cd->startup_status == STARTUP_STATUS_START) {
  7956. pt_debug(cd->dev, DL_WARN,
  7957. "%s: TMO waiting for FW reset sentinel\n", __func__);
  7958. rc = -ETIME;
  7959. }
  7960. exit:
  7961. if (cd->startup_status == STARTUP_STATUS_START) {
  7962. /* Reset to original state because we could be stuck in BL */
  7963. mutex_lock(&cd->system_lock);
  7964. cd->startup_status = tmp_startup_status;
  7965. mutex_unlock(&cd->system_lock);
  7966. }
  7967. return rc;
  7968. }
  7969. /*******************************************************************************
  7970. * FUNCTION: _pt_request_pip2_launch_app
  7971. *
  7972. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  7973. * inserted to ensure the reset has allowed the BL reset sentinel to be
  7974. * consumed.
  7975. *
  7976. * RETURN:
  7977. * 0 = success
  7978. * !0 = failure
  7979. *
  7980. * PARAMETERS:
  7981. * *cd - pointer to core data structure
  7982. ******************************************************************************/
  7983. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  7984. {
  7985. return pt_pip2_launch_app(dev, protect);
  7986. }
  7987. /*******************************************************************************
  7988. * FUNCTION: _pt_request_wait_for_enum_state
  7989. *
  7990. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  7991. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  7992. *
  7993. * RETURN:
  7994. * 0 = success
  7995. * !0 = failure
  7996. *
  7997. * PARAMETERS:
  7998. * *cd - pointer to core data structure
  7999. * timeout - timeout for how long to wait
  8000. * state - enum state to wait for
  8001. ******************************************************************************/
  8002. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8003. int state)
  8004. {
  8005. int rc = 0;
  8006. int t;
  8007. struct pt_core_data *cd = dev_get_drvdata(dev);
  8008. t = wait_event_timeout(cd->wait_q,
  8009. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8010. msecs_to_jiffies(timeout));
  8011. if (IS_TMO(t)) {
  8012. pt_debug(cd->dev, DL_ERROR,
  8013. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8014. __func__, state, timeout);
  8015. pt_debug(cd->dev, DL_WARN,
  8016. "%s: enum state reached 0x%04X\n",
  8017. __func__, cd->startup_status);
  8018. rc = -ETIME;
  8019. } else if (cd->startup_status & state) {
  8020. pt_debug(cd->dev, DL_INFO,
  8021. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8022. __func__, cd->startup_status,
  8023. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8024. } else {
  8025. if (t == 1) {
  8026. pt_debug(
  8027. cd->dev, DL_ERROR,
  8028. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8029. __func__, state, timeout);
  8030. rc = -ETIME;
  8031. } else {
  8032. pt_debug(
  8033. cd->dev, DL_ERROR,
  8034. "%s: Enum state 0x%04X not reached in %dms\n",
  8035. __func__, state, timeout - jiffies_to_msecs(t));
  8036. rc = -EINVAL;
  8037. }
  8038. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8039. __func__, cd->startup_status);
  8040. }
  8041. return rc;
  8042. }
  8043. /*******************************************************************************
  8044. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8045. *
  8046. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8047. * deep sleep.
  8048. *
  8049. * RETURN:
  8050. * 0 = success
  8051. * !0 = error
  8052. *
  8053. * PARAMETERS:
  8054. * *cd - pointer to core data
  8055. ******************************************************************************/
  8056. static int pt_core_wake_device_from_deep_sleep_(
  8057. struct pt_core_data *cd)
  8058. {
  8059. int rc;
  8060. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8061. if (rc)
  8062. rc = -EAGAIN;
  8063. /* Prevent failure on sequential wake/sleep requests from OS */
  8064. msleep(20);
  8065. return rc;
  8066. }
  8067. /*******************************************************************************
  8068. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8069. *
  8070. * SUMMARY: Wake up device from Easy-Wake state.
  8071. *
  8072. * RETURN:
  8073. * 0 = success
  8074. * !0 = failure
  8075. *
  8076. * PARAMETERS:
  8077. * *cd - pointer to core data
  8078. ******************************************************************************/
  8079. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8080. {
  8081. mutex_lock(&cd->system_lock);
  8082. cd->wait_until_wake = 1;
  8083. mutex_unlock(&cd->system_lock);
  8084. wake_up(&cd->wait_q);
  8085. msleep(20);
  8086. return pt_core_wake_device_from_deep_sleep_(cd);
  8087. }
  8088. /*******************************************************************************
  8089. * FUNCTION: pt_restore_parameters_
  8090. *
  8091. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8092. * back to the DUT
  8093. *
  8094. * RETURN:
  8095. * 0 = success
  8096. * !0 = failure
  8097. *
  8098. * PARAMETERS:
  8099. * *cd - pointer the core data structure
  8100. ******************************************************************************/
  8101. static int pt_restore_parameters_(struct pt_core_data *cd)
  8102. {
  8103. struct param_node *param;
  8104. int rc = 0;
  8105. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8106. goto exit;
  8107. spin_lock(&cd->spinlock);
  8108. list_for_each_entry(param, &cd->param_list, node) {
  8109. spin_unlock(&cd->spinlock);
  8110. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8111. __func__, param->id, param->value);
  8112. rc = pt_pip_set_param_(cd, param->id,
  8113. param->value, param->size);
  8114. if (rc)
  8115. goto exit;
  8116. spin_lock(&cd->spinlock);
  8117. }
  8118. spin_unlock(&cd->spinlock);
  8119. exit:
  8120. return rc;
  8121. }
  8122. /*******************************************************************************
  8123. * FUNCTION: pt_pip2_exit_bl_
  8124. *
  8125. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8126. * a DUT that may not have flash and will need FW to be loaded into RAM
  8127. *
  8128. * RETURN:
  8129. * 0 = success
  8130. * !0 = failure
  8131. *
  8132. * PARAMETERS:
  8133. * *cd - pointer the core data structure
  8134. * *status_str - pointer to optional status string buffer
  8135. * buf_size - size of status_str buffer
  8136. ******************************************************************************/
  8137. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8138. {
  8139. int rc;
  8140. int wait_time = 0;
  8141. u8 mode = PT_MODE_UNKNOWN;
  8142. bool load_status_str = false;
  8143. /*
  8144. * Below function has protective call to ensure no enum is still on
  8145. * going, while this kind of protection should be applied widely in
  8146. * future (TODO).
  8147. */
  8148. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8149. if (status_str && buf_size <= 50)
  8150. load_status_str = true;
  8151. if (mode == PT_MODE_BOOTLOADER) {
  8152. if (cd->flashless_dut == 1) {
  8153. rc = pt_hw_hard_reset(cd);
  8154. } else {
  8155. rc = pt_pip2_launch_app(cd->dev,
  8156. PT_CORE_CMD_UNPROTECTED);
  8157. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8158. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8159. __func__, "Invalid image in FLASH rc", rc);
  8160. } else if (rc) {
  8161. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8162. __func__, "Failed to launch app rc", rc);
  8163. }
  8164. }
  8165. if (!rc) {
  8166. if (cd->flashless_dut == 1) {
  8167. /* Wait for BL to complete before enum */
  8168. rc = _pt_request_wait_for_enum_state(cd->dev,
  8169. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8170. if (rc && load_status_str) {
  8171. strlcpy(status_str, "No FW sentinel after BL",
  8172. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8173. goto exit;
  8174. }
  8175. }
  8176. /*
  8177. * If the host wants to interact with the FW or do a
  8178. * forced calibration, the FW must be out of BOOT mode
  8179. * and the system information must have been retrieved.
  8180. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8181. * If, however, the enumeration does not reach this
  8182. * point, the DUT may still be in APP mode so test
  8183. * for all conditions.
  8184. */
  8185. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8186. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8187. if (!rc || cd->startup_status >=
  8188. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8189. mutex_lock(&cd->system_lock);
  8190. cd->mode = PT_MODE_OPERATIONAL;
  8191. mutex_unlock(&cd->system_lock);
  8192. }
  8193. if (rc) {
  8194. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8195. __func__, "Failed to enum with DUT",
  8196. cd->startup_status);
  8197. if (load_status_str && !(cd->startup_status &
  8198. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8199. strlcpy(status_str, "FW Stuck in Boot mode",
  8200. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8201. goto exit;
  8202. }
  8203. }
  8204. /*
  8205. * The coming FW sentinel could wake up the event
  8206. * queue, which has chance to be taken by next command
  8207. * wrongly. Following delay is a workaround to avoid
  8208. * this issue for most situations.
  8209. */
  8210. msleep(20);
  8211. pt_start_wd_timer(cd);
  8212. }
  8213. if (load_status_str) {
  8214. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8215. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8216. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8217. else if (!rc)
  8218. strlcpy(status_str, "Entered APP from BL mode",
  8219. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8220. else
  8221. strlcpy(status_str, "Failed to enter APP from BL mode",
  8222. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8223. }
  8224. } else if (mode == PT_MODE_OPERATIONAL) {
  8225. mutex_lock(&cd->system_lock);
  8226. cd->mode = mode;
  8227. mutex_unlock(&cd->system_lock);
  8228. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8229. if (load_status_str) {
  8230. if (!rc)
  8231. strlcpy(status_str, "Already in APP mode",
  8232. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8233. else
  8234. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8235. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8236. }
  8237. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8238. mutex_lock(&cd->system_lock);
  8239. cd->mode = mode;
  8240. mutex_unlock(&cd->system_lock);
  8241. if (load_status_str)
  8242. strlcpy(status_str, "Failed to determine active mode",
  8243. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8244. }
  8245. exit:
  8246. if (!rc)
  8247. pt_start_wd_timer(cd);
  8248. return rc;
  8249. }
  8250. /*******************************************************************************
  8251. * FUNCTION: pt_pip2_exit_bl
  8252. *
  8253. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8254. * access.
  8255. *
  8256. * RETURN:
  8257. * 0 = success
  8258. * !0 = failure
  8259. *
  8260. * PARAMETERS:
  8261. * *cd - pointer the core data structure
  8262. * *status_str - pointer to optional status string buffer
  8263. * buf_size - size of status_str buffer
  8264. ******************************************************************************/
  8265. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8266. {
  8267. int rc;
  8268. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8269. if (rc < 0) {
  8270. pt_debug(cd->dev, DL_ERROR,
  8271. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8272. cd->exclusive_dev, cd->dev);
  8273. return rc;
  8274. }
  8275. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8276. if (release_exclusive(cd, cd->dev) < 0)
  8277. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8278. __func__);
  8279. return rc;
  8280. }
  8281. /*******************************************************************************
  8282. * FUNCTION: _fast_startup
  8283. *
  8284. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8285. *
  8286. * RETURN:
  8287. * 0 = success
  8288. * !0 = failure
  8289. *
  8290. * PARAMETERS:
  8291. * *cd - pointer the core data structure
  8292. ******************************************************************************/
  8293. static int _fast_startup(struct pt_core_data *cd)
  8294. {
  8295. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8296. int rc = 0;
  8297. u8 mode = PT_MODE_UNKNOWN;
  8298. struct pt_hid_desc hid_desc;
  8299. int wait_time = 0;
  8300. memset(&hid_desc, 0, sizeof(hid_desc));
  8301. reset:
  8302. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8303. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8304. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8305. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8306. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8307. __func__);
  8308. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8309. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8310. if (rc < 0) {
  8311. pt_debug(cd->dev, DL_ERROR,
  8312. "%s: Error on getting HID descriptor r=%d\n",
  8313. __func__, rc);
  8314. if (retry--)
  8315. goto reset;
  8316. goto exit;
  8317. }
  8318. cd->mode = pt_get_mode(cd, &hid_desc);
  8319. if (cd->mode == PT_MODE_BOOTLOADER) {
  8320. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8321. __func__);
  8322. rc = pt_hid_output_bl_launch_app_(cd);
  8323. if (rc < 0) {
  8324. pt_debug(cd->dev, DL_ERROR,
  8325. "%s: Error on launch app r=%d\n",
  8326. __func__, rc);
  8327. if (retry--)
  8328. goto reset;
  8329. goto exit;
  8330. }
  8331. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8332. if (rc < 0) {
  8333. pt_debug(cd->dev, DL_ERROR,
  8334. "%s: Error on getting HID descriptor r=%d\n",
  8335. __func__, rc);
  8336. if (retry--)
  8337. goto reset;
  8338. goto exit;
  8339. }
  8340. cd->mode = pt_get_mode(cd, &hid_desc);
  8341. if (cd->mode == PT_MODE_BOOTLOADER) {
  8342. if (retry--)
  8343. goto reset;
  8344. goto exit;
  8345. }
  8346. }
  8347. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8348. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8349. } else {
  8350. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8351. __func__);
  8352. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8353. /* Wait for any sentinel before first try */
  8354. rc = _pt_request_wait_for_enum_state(
  8355. cd->dev, 150,
  8356. STARTUP_STATUS_BL_RESET_SENTINEL |
  8357. STARTUP_STATUS_FW_RESET_SENTINEL);
  8358. if (rc)
  8359. pt_debug(cd->dev, DL_ERROR,
  8360. "%s: No Sentinel detected rc = %d\n",
  8361. __func__, rc);
  8362. } else
  8363. pt_flush_bus_if_irq_asserted(cd,
  8364. PT_FLUSH_BUS_BASED_ON_LEN);
  8365. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8366. if (rc) {
  8367. pt_debug(cd->dev, DL_ERROR,
  8368. "%s: Get mode failed, mode unknown\n",
  8369. __func__);
  8370. mode = PT_MODE_UNKNOWN;
  8371. }
  8372. cd->mode = mode;
  8373. if (cd->mode == PT_MODE_BOOTLOADER) {
  8374. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8375. __func__);
  8376. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8377. if (rc) {
  8378. pt_debug(cd->dev, DL_ERROR,
  8379. "%s Failed to exit bootloader\n",
  8380. __func__);
  8381. msleep(50);
  8382. rc = -ENODEV;
  8383. if (retry--)
  8384. goto reset;
  8385. goto exit;
  8386. } else {
  8387. pt_debug(cd->dev, DL_INFO,
  8388. "%s: Exit bootloader successfully\n",
  8389. __func__);
  8390. }
  8391. if (cd->mode != PT_MODE_OPERATIONAL) {
  8392. pt_debug(cd->dev, DL_WARN,
  8393. "%s: restore mode failure mode = %d\n",
  8394. __func__, cd->mode);
  8395. if (retry--)
  8396. goto reset;
  8397. goto exit;
  8398. }
  8399. }
  8400. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8401. }
  8402. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8403. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8404. if (!rc) {
  8405. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8406. pt_debug(cd->dev, DL_WARN,
  8407. "%s: Exit FW BOOT Mode after %dms\n",
  8408. __func__, wait_time);
  8409. } else {
  8410. pt_debug(cd->dev, DL_WARN,
  8411. "%s: FW stuck in BOOT Mode after %dms\n",
  8412. __func__, wait_time);
  8413. goto exit;
  8414. }
  8415. if (!cd->sysinfo.ready) {
  8416. rc = pt_hid_output_get_sysinfo_(cd);
  8417. if (rc) {
  8418. pt_debug(cd->dev, DL_ERROR,
  8419. "%s: Error on getting sysinfo r=%d\n",
  8420. __func__, rc);
  8421. if (retry--)
  8422. goto reset;
  8423. goto exit;
  8424. }
  8425. }
  8426. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8427. rc = pt_restore_parameters_(cd);
  8428. if (rc)
  8429. pt_debug(cd->dev, DL_ERROR,
  8430. "%s: failed to restore parameters rc=%d\n",
  8431. __func__, rc);
  8432. else
  8433. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8434. exit:
  8435. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8436. return rc;
  8437. }
  8438. /*******************************************************************************
  8439. * FUNCTION: pt_core_poweron_device_
  8440. *
  8441. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8442. *
  8443. * RETURN:
  8444. * 0 = success
  8445. * !0 = failure
  8446. *
  8447. * PARAMETERS:
  8448. * *cd - pointer to core data
  8449. ******************************************************************************/
  8450. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8451. {
  8452. struct device *dev = cd->dev;
  8453. int rc = 0;
  8454. /*
  8455. * After power on action, the chip can general FW sentinel. It can
  8456. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8457. * _fast_startup() can perform a quick enumeration too, here doesn't
  8458. * need another enumeration.
  8459. */
  8460. mutex_lock(&cd->system_lock);
  8461. cd->startup_status = STARTUP_STATUS_START;
  8462. cd->hid_reset_cmd_state = 1;
  8463. mutex_unlock(&cd->system_lock);
  8464. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8465. if (rc < 0) {
  8466. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8467. __func__, rc);
  8468. goto exit;
  8469. }
  8470. if (!cd->irq_enabled) {
  8471. cd->irq_enabled = true;
  8472. enable_irq(cd->irq);
  8473. }
  8474. /* TBD: following function doesn't update startup_status */
  8475. rc = _fast_startup(cd);
  8476. exit:
  8477. return rc;
  8478. }
  8479. /*******************************************************************************
  8480. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8481. *
  8482. * SUMMARY: Reset device, and then trigger a full enumeration.
  8483. *
  8484. * RETURN:
  8485. * 0 = success
  8486. * !0 = failure
  8487. *
  8488. * PARAMETERS:
  8489. * *cd - pointer to core data
  8490. ******************************************************************************/
  8491. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8492. {
  8493. int rc;
  8494. rc = pt_dut_reset_and_wait(cd);
  8495. if (rc < 0) {
  8496. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8497. __func__, rc);
  8498. goto exit;
  8499. }
  8500. rc = _fast_startup(cd);
  8501. exit:
  8502. return rc;
  8503. }
  8504. /*******************************************************************************
  8505. * FUNCTION: pt_core_wake_
  8506. *
  8507. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8508. * the configuration in the core platform data structure.
  8509. *
  8510. * RETURN:
  8511. * 0 = success
  8512. * !0 = failure
  8513. *
  8514. * PARAMETERS:
  8515. * *cd - pointer to core data
  8516. ******************************************************************************/
  8517. static int pt_core_wake_(struct pt_core_data *cd)
  8518. {
  8519. int rc = 0;
  8520. mutex_lock(&cd->system_lock);
  8521. if (cd->sleep_state == SS_SLEEP_ON) {
  8522. cd->sleep_state = SS_WAKING;
  8523. } else {
  8524. mutex_unlock(&cd->system_lock);
  8525. return 1;
  8526. }
  8527. mutex_unlock(&cd->system_lock);
  8528. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8529. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8530. rc = pt_core_wake_device_from_easy_wake_(cd);
  8531. else if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP)
  8532. rc = pt_core_poweron_device_(cd);
  8533. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8534. rc = pt_core_wake_device_from_deep_standby_(cd);
  8535. else /* Default action to exit DeepSleep */
  8536. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8537. }
  8538. mutex_lock(&cd->system_lock);
  8539. cd->sleep_state = SS_SLEEP_OFF;
  8540. mutex_unlock(&cd->system_lock);
  8541. pt_start_wd_timer(cd);
  8542. return rc;
  8543. }
  8544. /*******************************************************************************
  8545. * FUNCTION: pt_core_wake_
  8546. *
  8547. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8548. *
  8549. * RETURN:
  8550. * 0 = success
  8551. * !0 = failure
  8552. *
  8553. * PARAMETERS:
  8554. * *cd - pointer to core data
  8555. ******************************************************************************/
  8556. static int pt_core_wake(struct pt_core_data *cd)
  8557. {
  8558. int rc;
  8559. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8560. if (rc < 0) {
  8561. pt_debug(cd->dev, DL_ERROR,
  8562. "%s: fail get exclusive ex=%p own=%p\n",
  8563. __func__, cd->exclusive_dev, cd->dev);
  8564. return rc;
  8565. }
  8566. rc = pt_core_wake_(cd);
  8567. if (release_exclusive(cd, cd->dev) < 0)
  8568. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8569. __func__);
  8570. else
  8571. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8572. __func__);
  8573. return rc;
  8574. }
  8575. /*******************************************************************************
  8576. * FUNCTION: pt_get_ic_crc_
  8577. *
  8578. * SUMMARY: This function retrieves the config block CRC
  8579. *
  8580. * NOTE: The post condition of calling this function will be that the DUT will
  8581. * be in SCANNINING mode if no failures occur
  8582. *
  8583. * RETURN:
  8584. * 0 = success
  8585. * !0 = failure
  8586. *
  8587. * PARAMETERS:
  8588. * *cd - pointer the core data structure
  8589. * ebid - enumerated block ID
  8590. ******************************************************************************/
  8591. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8592. {
  8593. struct pt_sysinfo *si = &cd->sysinfo;
  8594. int rc = 0;
  8595. u8 status;
  8596. u16 calculated_crc = 0;
  8597. u16 stored_crc = 0;
  8598. rc = pt_pip_suspend_scanning_(cd);
  8599. if (rc)
  8600. goto error;
  8601. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8602. &calculated_crc, &stored_crc);
  8603. if (rc)
  8604. goto exit;
  8605. if (status) {
  8606. rc = -EINVAL;
  8607. goto exit;
  8608. }
  8609. si->ttconfig.crc = stored_crc;
  8610. exit:
  8611. pt_pip_resume_scanning_(cd);
  8612. error:
  8613. pt_debug(cd->dev, DL_INFO,
  8614. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8615. __func__, ebid, calculated_crc, stored_crc, rc);
  8616. return rc;
  8617. }
  8618. /*******************************************************************************
  8619. * FUNCTION: pt_pip2_read_gpio
  8620. *
  8621. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8622. * bits into the passed in variable
  8623. *
  8624. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8625. *
  8626. * RETURN:
  8627. * 0 = success
  8628. * !0 = failure
  8629. *
  8630. * PARAMETERS:
  8631. * *dev - pointer to device structure
  8632. * *status - pointer to where the command response status is stored
  8633. * *gpio - pointer to device attributes structure
  8634. ******************************************************************************/
  8635. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8636. {
  8637. int rc = 0;
  8638. u16 actual_read_len;
  8639. u8 read_buf[12];
  8640. u8 tmp_status = 0;
  8641. u8 index = PIP2_RESP_STATUS_OFFSET;
  8642. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8643. rc = _pt_request_pip2_send_cmd(dev,
  8644. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8645. NULL, 0, read_buf, &actual_read_len);
  8646. if (rc) {
  8647. pt_debug(dev, DL_ERROR,
  8648. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8649. rc = -ECOMM;
  8650. } else {
  8651. tmp_status = read_buf[index];
  8652. }
  8653. if (status)
  8654. *status = tmp_status;
  8655. if (!rc && gpio && (tmp_status == 0)) {
  8656. *gpio = ((read_buf[index + 4] << 24) |
  8657. (read_buf[index + 3] << 16) |
  8658. (read_buf[index + 2] << 8) |
  8659. (read_buf[index + 1]));
  8660. }
  8661. return rc;
  8662. }
  8663. /*******************************************************************************
  8664. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8665. *
  8666. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8667. *
  8668. * RETURN:
  8669. * 0 = success
  8670. * !0 = failure
  8671. *
  8672. * PARAMETERS:
  8673. * *cd - pointer the core data structure
  8674. * *pid - pointer to store panel ID
  8675. ******************************************************************************/
  8676. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8677. {
  8678. u32 gpio_value = 0;
  8679. u8 status = 0;
  8680. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8681. int rc = 0;
  8682. if (!pid)
  8683. return -ENOMEM;
  8684. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8685. if (!rc) {
  8686. if (status == 0) {
  8687. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8688. PT_PANEL_ID_SHIFT;
  8689. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8690. __func__,
  8691. "BL mode PID", panel_id, "gpio", gpio_value);
  8692. *pid = panel_id;
  8693. } else {
  8694. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8695. __func__,
  8696. "BL read gpio failed status", status);
  8697. }
  8698. } else {
  8699. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8700. __func__,
  8701. "BL read gpio failed status", status);
  8702. }
  8703. return rc;
  8704. }
  8705. /*******************************************************************************
  8706. * FUNCTION: pt_enum_with_dut_
  8707. *
  8708. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8709. * The core data (cd) startup_status will store, as a bitmask, each
  8710. * state of the enumeration process. The startup will be attempted
  8711. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8712. *
  8713. * RETURN:
  8714. * 0 = success
  8715. * !0 = failure
  8716. *
  8717. * PARAMETERS:
  8718. * *cd - pointer the core data structure
  8719. * reset - Flag to reset the DUT before attempting to enumerate
  8720. * *status - poionter to store the enum status bitmask flags
  8721. ******************************************************************************/
  8722. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8723. u32 *enum_status)
  8724. {
  8725. int try = 1;
  8726. int rc = 0;
  8727. int wait_time = 0;
  8728. bool detected = false;
  8729. u8 return_data[8];
  8730. u8 mode = PT_MODE_UNKNOWN;
  8731. u8 pid = PANEL_ID_NOT_ENABLED;
  8732. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8733. struct pt_hid_desc hid_desc;
  8734. memset(&hid_desc, 0, sizeof(hid_desc));
  8735. #ifdef TTHE_TUNER_SUPPORT
  8736. tthe_print(cd, NULL, 0, "enter startup");
  8737. #endif
  8738. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8739. __func__, cd->startup_status, reset);
  8740. pt_stop_wd_timer(cd);
  8741. reset:
  8742. if (try > 1)
  8743. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8744. __func__, try);
  8745. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8746. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8747. pt_debug(cd->dev, DL_INFO,
  8748. "%s: PIP1 Enumeration start\n", __func__);
  8749. /* Only reset the DUT after the first try */
  8750. if (reset || (try > 1)) {
  8751. /*
  8752. * Reset hardware only for Legacy parts. Skip for TT/TC
  8753. * parts because if the FW image was loaded directly
  8754. * to SRAM issueing a reset ill wipe out what was just
  8755. * loaded.
  8756. */
  8757. rc = pt_dut_reset_and_wait(cd);
  8758. if (rc < 0) {
  8759. pt_debug(cd->dev, DL_ERROR,
  8760. "%s: Error on h/w reset r=%d\n",
  8761. __func__, rc);
  8762. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8763. goto reset;
  8764. goto exit;
  8765. }
  8766. /* sleep to allow FW to be launched if available */
  8767. msleep(120);
  8768. }
  8769. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8770. if (rc < 0) {
  8771. pt_debug(cd->dev, DL_ERROR,
  8772. "%s: Error getting HID Descriptor r=%d\n",
  8773. __func__, rc);
  8774. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8775. goto reset;
  8776. rc = -EIO;
  8777. goto exit;
  8778. }
  8779. detected = true;
  8780. cd->mode = pt_get_mode(cd, &hid_desc);
  8781. /*
  8782. * Most systems do not use an XY pin as the panel_id and so
  8783. * the BL is used to retrieve the panel_id, however for
  8784. * systems that do use an XY pin, the panel_id MUST be
  8785. * retrieved from the system information when running FW
  8786. * (done below) and so this section of code is skipped.
  8787. * Entering the BL here is only needed on XY_PIN systems.
  8788. */
  8789. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8790. if (cd->mode == PT_MODE_OPERATIONAL) {
  8791. rc = pt_pip_start_bootloader_(cd);
  8792. if (rc < 0) {
  8793. pt_debug(cd->dev, DL_ERROR,
  8794. "%s: Error on start bootloader r=%d\n",
  8795. __func__, rc);
  8796. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8797. goto reset;
  8798. goto exit;
  8799. }
  8800. cd->mode = PT_MODE_BOOTLOADER;
  8801. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8802. __func__);
  8803. }
  8804. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8805. if (!rc) {
  8806. cd->bl_info.ready = true;
  8807. cd->bl_info.chip_id =
  8808. get_unaligned_le16(&return_data[2]);
  8809. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8810. __func__, cd->bl_info.chip_id);
  8811. } else {
  8812. pt_debug(cd->dev, DL_ERROR,
  8813. "%s: failed to get chip ID, r=%d\n",
  8814. __func__, rc);
  8815. }
  8816. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8817. mutex_lock(&cd->system_lock);
  8818. if (!rc) {
  8819. cd->pid_for_loader = pid;
  8820. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8821. __func__, cd->pid_for_loader);
  8822. } else {
  8823. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8824. pt_debug(cd->dev, DL_WARN,
  8825. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8826. __func__, cd->pid_for_loader);
  8827. }
  8828. mutex_unlock(&cd->system_lock);
  8829. }
  8830. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8831. if (cd->mode == PT_MODE_BOOTLOADER) {
  8832. rc = pt_hid_output_bl_launch_app_(cd);
  8833. if (rc < 0) {
  8834. pt_debug(cd->dev, DL_ERROR,
  8835. "%s: Error on launch app r=%d\n",
  8836. __func__, rc);
  8837. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8838. goto reset;
  8839. rc = -ENODEV;
  8840. goto exit;
  8841. }
  8842. /* sleep to allow FW to be launched if available */
  8843. msleep(120);
  8844. /* Ensure the DUT is now in Application mode */
  8845. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8846. if (rc < 0) {
  8847. pt_debug(cd->dev, DL_ERROR,
  8848. "%s: Error getting HID Desc r=%d\n",
  8849. __func__, rc);
  8850. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8851. goto reset;
  8852. rc = -EIO;
  8853. goto exit;
  8854. }
  8855. cd->mode = pt_get_mode(cd, &hid_desc);
  8856. if (cd->mode == PT_MODE_BOOTLOADER) {
  8857. pt_debug(cd->dev, DL_WARN,
  8858. "%s: Error confiming exit BL\n",
  8859. __func__);
  8860. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8861. goto reset;
  8862. rc = -ENODEV;
  8863. goto exit;
  8864. }
  8865. }
  8866. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  8867. cd->mode = PT_MODE_OPERATIONAL;
  8868. *enum_status |= STARTUP_STATUS_GET_DESC;
  8869. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8870. } else {
  8871. /* Generation is PIP2 Capable */
  8872. pt_debug(cd->dev, DL_INFO,
  8873. "%s: PIP2 Enumeration start\n", __func__);
  8874. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8875. if (rc) {
  8876. pt_debug(cd->dev, DL_ERROR,
  8877. "%s: Get mode failed, mode unknown\n",
  8878. __func__);
  8879. mode = PT_MODE_UNKNOWN;
  8880. } else
  8881. detected = true;
  8882. cd->mode = mode;
  8883. switch (cd->mode) {
  8884. case PT_MODE_OPERATIONAL:
  8885. pt_debug(cd->dev, DL_INFO,
  8886. "%s: Operational mode\n", __func__);
  8887. if (cd->app_pip_ver_ready == false) {
  8888. rc = pt_pip2_get_version_(cd);
  8889. if (!rc)
  8890. cd->app_pip_ver_ready = true;
  8891. else {
  8892. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8893. goto reset;
  8894. goto exit;
  8895. }
  8896. }
  8897. break;
  8898. case PT_MODE_BOOTLOADER:
  8899. pt_debug(cd->dev, DL_INFO,
  8900. "%s: Bootloader mode\n", __func__);
  8901. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8902. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  8903. mutex_lock(&cd->system_lock);
  8904. if (!rc) {
  8905. cd->pid_for_loader = pid;
  8906. pt_debug(cd->dev, DL_INFO,
  8907. "%s: Panel ID: 0x%02X\n",
  8908. __func__, cd->pid_for_loader);
  8909. } else {
  8910. cd->pid_for_loader =
  8911. PANEL_ID_NOT_ENABLED;
  8912. pt_debug(cd->dev, DL_WARN,
  8913. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8914. __func__, cd->pid_for_loader);
  8915. }
  8916. mutex_unlock(&cd->system_lock);
  8917. }
  8918. if (cd->bl_pip_ver_ready == false) {
  8919. rc = pt_pip2_get_version_(cd);
  8920. if (!rc)
  8921. cd->bl_pip_ver_ready = true;
  8922. else {
  8923. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8924. goto reset;
  8925. goto exit;
  8926. }
  8927. }
  8928. /*
  8929. * Launch app command will fail in flashless mode.
  8930. * Skip launch app command here to save time for
  8931. * enumeration flow.
  8932. */
  8933. if (cd->flashless_dut)
  8934. goto exit;
  8935. /*
  8936. * pt_pip2_launch_app() is needed here instead of
  8937. * pt_pip2_exit_bl() because exit_bl will cause several
  8938. * hw_resets to occur and the auto BL on a flashless
  8939. * DUT will fail.
  8940. */
  8941. rc = pt_pip2_launch_app(cd->dev,
  8942. PT_CORE_CMD_UNPROTECTED);
  8943. if (rc) {
  8944. pt_debug(cd->dev, DL_ERROR,
  8945. "%s: Error on launch app r=%d\n",
  8946. __func__, rc);
  8947. msleep(50);
  8948. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8949. goto reset;
  8950. goto exit;
  8951. }
  8952. /*
  8953. * IRQ thread can be delayed if the serial log print is
  8954. * enabled. It causes next command to get wrong response
  8955. * Here the delay is to ensure pt_parse_input() to be
  8956. * finished.
  8957. */
  8958. msleep(60);
  8959. /* Check and update the mode */
  8960. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8961. if (rc) {
  8962. pt_debug(cd->dev, DL_ERROR,
  8963. "%s: Get mode failed, mode unknown\n",
  8964. __func__);
  8965. mode = PT_MODE_UNKNOWN;
  8966. }
  8967. cd->mode = mode;
  8968. if (cd->mode == PT_MODE_OPERATIONAL) {
  8969. pt_debug(cd->dev, DL_INFO,
  8970. "%s: Launched to Operational mode\n",
  8971. __func__);
  8972. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  8973. pt_debug(cd->dev, DL_ERROR,
  8974. "%s: Launch failed, Bootloader mode\n",
  8975. __func__);
  8976. goto exit;
  8977. } else if (cd->mode == PT_MODE_UNKNOWN) {
  8978. pt_debug(cd->dev, DL_ERROR,
  8979. "%s: Launch failed, Unknown mode\n",
  8980. __func__);
  8981. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8982. goto reset;
  8983. goto exit;
  8984. }
  8985. if (cd->app_pip_ver_ready == false) {
  8986. rc = pt_pip2_get_version_(cd);
  8987. if (!rc)
  8988. cd->app_pip_ver_ready = true;
  8989. else {
  8990. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8991. goto reset;
  8992. goto exit;
  8993. }
  8994. }
  8995. break;
  8996. default:
  8997. pt_debug(cd->dev, DL_ERROR,
  8998. "%s: Unknown mode\n", __func__);
  8999. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9000. goto reset;
  9001. goto exit;
  9002. }
  9003. *enum_status |= STARTUP_STATUS_GET_DESC;
  9004. }
  9005. pt_init_pip_report_fields(cd);
  9006. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9007. if (!cd->features.easywake)
  9008. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9009. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9010. rc = pt_hid_output_get_sysinfo_(cd);
  9011. if (rc) {
  9012. pt_debug(cd->dev, DL_ERROR,
  9013. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9014. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9015. goto reset;
  9016. goto exit;
  9017. }
  9018. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9019. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9020. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9021. if (!rc) {
  9022. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9023. pt_debug(cd->dev, DL_WARN,
  9024. "%s: Exit FW BOOT Mode after %dms\n",
  9025. __func__, wait_time);
  9026. } else {
  9027. pt_debug(cd->dev, DL_WARN,
  9028. "%s: FW stuck in BOOT Mode after %dms\n",
  9029. __func__, wait_time);
  9030. goto exit;
  9031. }
  9032. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9033. __func__,
  9034. cd->sysinfo.ttdata.pip_ver_major,
  9035. cd->sysinfo.ttdata.pip_ver_minor);
  9036. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9037. if (rc) {
  9038. pt_debug(cd->dev, DL_ERROR,
  9039. "%s: DUT Config block CRC failure rc=%d\n",
  9040. __func__, rc);
  9041. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9042. goto reset;
  9043. } else {
  9044. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9045. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9046. pt_debug(cd->dev, DL_ERROR,
  9047. "%s: scan state: %d, retry: %d\n",
  9048. __func__, sys_mode, try);
  9049. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9050. goto reset;
  9051. } else
  9052. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9053. }
  9054. rc = pt_restore_parameters_(cd);
  9055. if (rc) {
  9056. pt_debug(cd->dev, DL_ERROR,
  9057. "%s: Failed to restore parameters rc=%d\n",
  9058. __func__, rc);
  9059. } else
  9060. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9061. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9062. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9063. cd->startup_retry_count = 0;
  9064. exit:
  9065. /* Generate the HW Version of the connected DUT and store in cd */
  9066. pt_generate_hw_version(cd, cd->hw_version);
  9067. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9068. cd->hw_version);
  9069. pt_start_wd_timer(cd);
  9070. if (!detected)
  9071. rc = -ENODEV;
  9072. #ifdef TTHE_TUNER_SUPPORT
  9073. tthe_print(cd, NULL, 0, "exit startup");
  9074. #endif
  9075. pt_debug(cd->dev, DL_WARN,
  9076. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9077. __func__, rc, try);
  9078. return rc;
  9079. }
  9080. /*******************************************************************************
  9081. * FUNCTION: pt_enum_with_dut
  9082. *
  9083. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9084. * requesting exclusive access around the call.
  9085. *
  9086. * RETURN:
  9087. * 0 = success
  9088. * !0 = failure
  9089. *
  9090. * PARAMETERS:
  9091. * *cd - pointer the core data structure
  9092. * reset - Flag to reset the DUT before attempting to enumerate
  9093. * *status - pointer to store the enum status bitmask flags
  9094. ******************************************************************************/
  9095. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9096. {
  9097. int rc = 0;
  9098. mutex_lock(&cd->system_lock);
  9099. cd->startup_state = STARTUP_RUNNING;
  9100. mutex_unlock(&cd->system_lock);
  9101. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9102. if (rc) {
  9103. pt_debug(cd->dev, DL_ERROR,
  9104. "%s: fail get exclusive ex=%p own=%p\n",
  9105. __func__, cd->exclusive_dev, cd->dev);
  9106. goto exit;
  9107. }
  9108. rc = pt_enum_with_dut_(cd, reset, status);
  9109. if (release_exclusive(cd, cd->dev) < 0)
  9110. /* Don't return fail code, mode is already changed. */
  9111. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9112. __func__);
  9113. else
  9114. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9115. __func__);
  9116. exit:
  9117. mutex_lock(&cd->system_lock);
  9118. /* Clear startup state for any tasks waiting for startup completion */
  9119. cd->startup_state = STARTUP_NONE;
  9120. mutex_unlock(&cd->system_lock);
  9121. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9122. *status |= STARTUP_STATUS_COMPLETE;
  9123. /* Wake the waiters for end of startup */
  9124. wake_up(&cd->wait_q);
  9125. return rc;
  9126. }
  9127. static int add_sysfs_interfaces(struct device *dev);
  9128. static void remove_sysfs_interfaces(struct device *dev);
  9129. static void remove_sysfs_and_modules(struct device *dev);
  9130. static void pt_release_modules(struct pt_core_data *cd);
  9131. static void pt_probe_modules(struct pt_core_data *cd);
  9132. /*******************************************************************************
  9133. * FUNCTION: _pt_ttdl_restart
  9134. *
  9135. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9136. *
  9137. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9138. * existing sysfs nodes will produce an error.
  9139. *
  9140. * RETURN:
  9141. * 0 = success
  9142. * !0 = failure
  9143. *
  9144. * PARAMETERS:
  9145. * *dev - pointer to core device
  9146. ******************************************************************************/
  9147. static int _pt_ttdl_restart(struct device *dev)
  9148. {
  9149. int rc = 0;
  9150. struct pt_core_data *cd = dev_get_drvdata(dev);
  9151. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9152. struct i2c_client *client =
  9153. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9154. #endif
  9155. /*
  9156. * Make sure the device is awake, pt_mt_release function will
  9157. * cause pm sleep function and lead to deadlock.
  9158. */
  9159. pm_runtime_get_sync(dev);
  9160. /* Use ttdl_restart_lock to avoid reentry */
  9161. mutex_lock(&cd->ttdl_restart_lock);
  9162. remove_sysfs_and_modules(cd->dev);
  9163. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9164. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9165. pt_debug(dev, DL_ERROR,
  9166. "%s I2C functionality not Supported\n", __func__);
  9167. rc = -EIO;
  9168. goto ttdl_no_error;
  9169. }
  9170. #endif
  9171. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9172. rc = _pt_detect_dut_generation(cd->dev,
  9173. &cd->startup_status, &cd->active_dut_generation,
  9174. &cd->mode);
  9175. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9176. pt_debug(dev, DL_ERROR,
  9177. "%s: Error, Unknown DUT Generation rc=%d\n",
  9178. __func__, rc);
  9179. }
  9180. }
  9181. rc = add_sysfs_interfaces(cd->dev);
  9182. if (rc < 0)
  9183. pt_debug(cd->dev, DL_ERROR,
  9184. "%s: Error, failed sysfs nodes rc=%d\n",
  9185. __func__, rc);
  9186. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9187. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9188. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9189. if (rc < 0)
  9190. pt_debug(dev, DL_ERROR,
  9191. "%s: Error, Failed to Enumerate\n", __func__);
  9192. }
  9193. rc = pt_mt_probe(dev);
  9194. if (rc < 0) {
  9195. pt_debug(dev, DL_ERROR,
  9196. "%s: Error, fail mt probe\n", __func__);
  9197. }
  9198. rc = pt_btn_probe(dev);
  9199. if (rc < 0) {
  9200. pt_debug(dev, DL_ERROR,
  9201. "%s: Error, fail btn probe\n", __func__);
  9202. }
  9203. pt_probe_modules(cd);
  9204. pt_debug(cd->dev, DL_WARN,
  9205. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9206. rc = 0;
  9207. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9208. ttdl_no_error:
  9209. #endif
  9210. mutex_unlock(&cd->ttdl_restart_lock);
  9211. mutex_lock(&cd->system_lock);
  9212. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9213. cd->startup_state = STARTUP_NONE;
  9214. mutex_unlock(&cd->system_lock);
  9215. pm_runtime_put(dev);
  9216. return rc;
  9217. }
  9218. /*******************************************************************************
  9219. * FUNCTION: pt_restart_work_function
  9220. *
  9221. * SUMMARY: This is the wrapper function placed in a work queue to call
  9222. * _pt_ttdl_restart()
  9223. *
  9224. * RETURN: none
  9225. *
  9226. * PARAMETERS:
  9227. * *work - pointer to the work_struct
  9228. ******************************************************************************/
  9229. static void pt_restart_work_function(struct work_struct *work)
  9230. {
  9231. struct pt_core_data *cd = container_of(work,
  9232. struct pt_core_data, ttdl_restart_work);
  9233. int rc = 0;
  9234. rc = _pt_ttdl_restart(cd->dev);
  9235. if (rc < 0)
  9236. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9237. __func__, rc);
  9238. }
  9239. /*******************************************************************************
  9240. * FUNCTION: pt_enum_work_function
  9241. *
  9242. * SUMMARY: This is the wrapper function placed in a work queue to call
  9243. * pt_enum_with_dut()
  9244. *
  9245. * RETURN: none
  9246. *
  9247. * PARAMETERS:
  9248. * *work - pointer to the work_struct
  9249. ******************************************************************************/
  9250. static void pt_enum_work_function(struct work_struct *work)
  9251. {
  9252. struct pt_core_data *cd = container_of(work,
  9253. struct pt_core_data, enum_work);
  9254. int rc;
  9255. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9256. if (rc < 0)
  9257. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9258. __func__, rc);
  9259. }
  9260. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9261. {
  9262. int rc;
  9263. if (!get) {
  9264. rc = 0;
  9265. goto regulator_put;
  9266. }
  9267. cd->vdd = regulator_get(cd->dev, "vdd");
  9268. if (IS_ERR(cd->vdd)) {
  9269. rc = PTR_ERR(cd->vdd);
  9270. dev_err(cd->dev,
  9271. "Regulator get failed vdd rc=%d\n", rc);
  9272. goto regulator_put;
  9273. }
  9274. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9275. if (IS_ERR(cd->vcc_i2c)) {
  9276. rc = PTR_ERR(cd->vcc_i2c);
  9277. dev_err(cd->dev,
  9278. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9279. goto regulator_put;
  9280. }
  9281. return 0;
  9282. regulator_put:
  9283. if (cd->vdd) {
  9284. regulator_put(cd->vdd);
  9285. cd->vdd = NULL;
  9286. }
  9287. if (cd->vcc_i2c) {
  9288. regulator_put(cd->vcc_i2c);
  9289. cd->vcc_i2c = NULL;
  9290. }
  9291. return rc;
  9292. }
  9293. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9294. {
  9295. int rc;
  9296. if (!en) {
  9297. rc = 0;
  9298. goto disable_vcc_i2c_reg;
  9299. }
  9300. if (cd->vdd) {
  9301. if (regulator_count_voltages(cd->vdd) > 0) {
  9302. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9303. FT_VTG_MAX_UV);
  9304. if (rc) {
  9305. dev_err(cd->dev,
  9306. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9307. goto exit;
  9308. }
  9309. }
  9310. rc = regulator_enable(cd->vdd);
  9311. if (rc) {
  9312. dev_err(cd->dev,
  9313. "Regulator vdd enable failed rc=%d\n", rc);
  9314. goto exit;
  9315. }
  9316. }
  9317. if (cd->vcc_i2c) {
  9318. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9319. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9320. FT_I2C_VTG_MAX_UV);
  9321. if (rc) {
  9322. dev_err(cd->dev,
  9323. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9324. goto disable_vdd_reg;
  9325. }
  9326. }
  9327. rc = regulator_enable(cd->vcc_i2c);
  9328. if (rc) {
  9329. dev_err(cd->dev,
  9330. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9331. goto disable_vdd_reg;
  9332. }
  9333. }
  9334. return 0;
  9335. disable_vcc_i2c_reg:
  9336. if (cd->vcc_i2c) {
  9337. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9338. regulator_set_voltage(cd->vcc_i2c, 0, FT_I2C_VTG_MAX_UV);
  9339. regulator_disable(cd->vcc_i2c);
  9340. }
  9341. disable_vdd_reg:
  9342. if (cd->vdd) {
  9343. if (regulator_count_voltages(cd->vdd) > 0)
  9344. regulator_set_voltage(cd->vdd, 0, FT_VTG_MAX_UV);
  9345. regulator_disable(cd->vdd);
  9346. }
  9347. exit:
  9348. return rc;
  9349. }
  9350. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9351. #define KERNEL_VER_GT_3_19
  9352. #endif
  9353. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9354. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9355. #if defined(CONFIG_PM_SLEEP)
  9356. /*******************************************************************************
  9357. * FUNCTION: pt_core_rt_suspend
  9358. *
  9359. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9360. *
  9361. * RETURN:
  9362. * 0 = success
  9363. * !0 = failure
  9364. *
  9365. * PARAMETERS:
  9366. * *dev - pointer to core device
  9367. ******************************************************************************/
  9368. static int pt_core_rt_suspend(struct device *dev)
  9369. {
  9370. struct pt_core_data *cd = dev_get_drvdata(dev);
  9371. int rc = 0;
  9372. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9373. return 0;
  9374. rc = pt_core_sleep(cd);
  9375. if (rc < 0) {
  9376. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9377. return -EAGAIN;
  9378. }
  9379. return 0;
  9380. }
  9381. /*******************************************************************************
  9382. * FUNCTION: pt_core_rt_resume
  9383. *
  9384. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9385. *
  9386. * RETURN:
  9387. * 0 = success
  9388. * !0 = failure
  9389. *
  9390. * PARAMETERS:
  9391. * *dev - pointer to core device
  9392. ******************************************************************************/
  9393. static int pt_core_rt_resume(struct device *dev)
  9394. {
  9395. struct pt_core_data *cd = dev_get_drvdata(dev);
  9396. int rc = 0;
  9397. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9398. return 0;
  9399. rc = pt_core_wake(cd);
  9400. if (rc < 0) {
  9401. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9402. return -EAGAIN;
  9403. }
  9404. return 0;
  9405. }
  9406. #endif /* CONFIG_PM_SLEEP */
  9407. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9408. #if defined(CONFIG_PM_SLEEP)
  9409. /*******************************************************************************
  9410. * FUNCTION: pt_core_suspend_
  9411. *
  9412. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9413. * pt_core_sleep. This function may disable IRQ during sleep state.
  9414. *
  9415. * RETURN:
  9416. * 0 = success
  9417. * !0 = failure
  9418. *
  9419. * PARAMETERS:
  9420. * *dev - pointer to core device
  9421. ******************************************************************************/
  9422. static int pt_core_suspend_(struct device *dev)
  9423. {
  9424. struct pt_core_data *cd = dev_get_drvdata(dev);
  9425. pt_core_sleep(cd);
  9426. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9427. return 0;
  9428. /* Required to prevent interrupts before bus awake */
  9429. disable_irq(cd->irq);
  9430. cd->irq_disabled = 1;
  9431. if (device_may_wakeup(dev)) {
  9432. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9433. __func__);
  9434. if (!enable_irq_wake(cd->irq))
  9435. cd->irq_wake = 1;
  9436. } else {
  9437. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9438. __func__);
  9439. }
  9440. return 0;
  9441. }
  9442. /*******************************************************************************
  9443. * FUNCTION: pt_core_suspend
  9444. *
  9445. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9446. * woke up or put to sleep based on Kernel power state even when the display
  9447. * is off based on the check of TTDL core platform flag.
  9448. *
  9449. * RETURN:
  9450. * 0 = success
  9451. * !0 = failure
  9452. *
  9453. * PARAMETERS:
  9454. * *dev - pointer to core device
  9455. ******************************************************************************/
  9456. static int pt_core_suspend(struct device *dev)
  9457. {
  9458. struct pt_core_data *cd = dev_get_drvdata(dev);
  9459. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9460. return 0;
  9461. return pt_core_suspend_(dev);
  9462. }
  9463. /*******************************************************************************
  9464. * FUNCTION: pt_core_resume_
  9465. *
  9466. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9467. * pt_core_wake. This function may enable IRQ before wake up.
  9468. *
  9469. * RETURN:
  9470. * 0 = success
  9471. * !0 = failure
  9472. *
  9473. * PARAMETERS:
  9474. * *dev - pointer to core device
  9475. ******************************************************************************/
  9476. static int pt_core_resume_(struct device *dev)
  9477. {
  9478. struct pt_core_data *cd = dev_get_drvdata(dev);
  9479. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9480. goto exit;
  9481. /*
  9482. * Bus pm does not call suspend if device runtime suspended
  9483. * This flag is covers that case
  9484. */
  9485. if (cd->irq_disabled) {
  9486. enable_irq(cd->irq);
  9487. cd->irq_disabled = 0;
  9488. }
  9489. if (device_may_wakeup(dev)) {
  9490. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9491. __func__);
  9492. if (cd->irq_wake) {
  9493. disable_irq_wake(cd->irq);
  9494. cd->irq_wake = 0;
  9495. }
  9496. } else {
  9497. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9498. __func__);
  9499. }
  9500. exit:
  9501. pt_core_wake(cd);
  9502. return 0;
  9503. }
  9504. /*******************************************************************************
  9505. * FUNCTION: pt_core_resume
  9506. *
  9507. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9508. * along with kernel power state even the display is off based on the check of
  9509. * TTDL core platform flag.
  9510. *
  9511. * RETURN:
  9512. * 0 = success
  9513. * !0 = failure
  9514. *
  9515. * PARAMETERS:
  9516. * *dev - pointer to core device
  9517. ******************************************************************************/
  9518. static int pt_core_resume(struct device *dev)
  9519. {
  9520. struct pt_core_data *cd = dev_get_drvdata(dev);
  9521. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9522. return 0;
  9523. return pt_core_resume_(dev);
  9524. }
  9525. #endif
  9526. #ifdef NEED_SUSPEND_NOTIFIER
  9527. /*******************************************************************************
  9528. * FUNCTION: pt_pm_notifier
  9529. *
  9530. * SUMMARY: This function is registered to notifier chain and will perform
  9531. * suspend operation if match event PM_SUSPEND_PREPARE.
  9532. *
  9533. * RETURN:
  9534. * 0 = success
  9535. * !0 = failure
  9536. *
  9537. * PARAMETERS:
  9538. * *nb - pointer to notifier_block structure
  9539. * action - notifier event type
  9540. * *data - void pointer
  9541. ******************************************************************************/
  9542. static int pt_pm_notifier(struct notifier_block *nb,
  9543. unsigned long action, void *data)
  9544. {
  9545. struct pt_core_data *cd = container_of(nb,
  9546. struct pt_core_data, pm_notifier);
  9547. if (action == PM_SUSPEND_PREPARE) {
  9548. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9549. __func__);
  9550. /*
  9551. * If PM runtime is not suspended, either call runtime
  9552. * PM suspend callback or wait until it finishes
  9553. */
  9554. if (!pm_runtime_suspended(cd->dev))
  9555. pm_runtime_suspend(cd->dev);
  9556. (void) pt_core_suspend(cd->dev);
  9557. }
  9558. return NOTIFY_DONE;
  9559. }
  9560. #endif
  9561. const struct dev_pm_ops pt_pm_ops = {
  9562. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9563. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9564. NULL)
  9565. };
  9566. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9567. /*******************************************************************************
  9568. * FUNCTION: _pt_request_pip2_enter_bl
  9569. *
  9570. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9571. * XRES pin or a soft reset.
  9572. *
  9573. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9574. * the WD active could cause this function to fail!
  9575. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9576. * will not try to determine the current mode but will proceed with
  9577. * resetting the DUT and entering the BL.
  9578. *
  9579. * NOTE: The definition of result code:
  9580. * PT_ENTER_BL_PASS (0)
  9581. * PT_ENTER_BL_ERROR (1)
  9582. * PT_ENTER_BL_RESET_FAIL (2)
  9583. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9584. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9585. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9586. *
  9587. * RETURNS:
  9588. * 0 = success
  9589. * !0 = failure
  9590. *
  9591. *
  9592. * PARAMETERS:
  9593. * *dev - pointer to device structure
  9594. * *start_mode - pointer to the mode the DUT was in when this function
  9595. * starts
  9596. * *result - pointer to store the result when to enter BL
  9597. ******************************************************************************/
  9598. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9599. {
  9600. int rc = 0;
  9601. int t;
  9602. int tmp_result = PT_ENTER_BL_ERROR;
  9603. int flash_info_retry = 2;
  9604. u8 mode = PT_MODE_UNKNOWN;
  9605. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9606. u8 read_buf[32];
  9607. u16 actual_read_len;
  9608. struct pt_core_data *cd = dev_get_drvdata(dev);
  9609. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9610. u8 time = 0;
  9611. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9612. if (cd->watchdog_enabled) {
  9613. pt_debug(dev, DL_WARN,
  9614. "%s: Watchdog must be stopped before entering BL\n",
  9615. __func__);
  9616. goto exit;
  9617. }
  9618. cancel_work_sync(&cd->enum_work);
  9619. cancel_work_sync(&cd->watchdog_work);
  9620. /* if undefined assume operational/test to bypass all checks */
  9621. if (*start_mode == PT_MODE_IGNORE) {
  9622. mode = PT_MODE_OPERATIONAL;
  9623. sys_mode = FW_SYS_MODE_TEST;
  9624. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9625. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9626. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9627. if (rc) {
  9628. pt_debug(dev, DL_ERROR,
  9629. "%s: Get mode failed, mode unknown\n",
  9630. __func__);
  9631. }
  9632. *start_mode = mode;
  9633. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9634. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9635. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9636. sys_mode = FW_SYS_MODE_SCANNING;
  9637. }
  9638. _retry:
  9639. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9640. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9641. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9642. switch (mode) {
  9643. case PT_MODE_UNKNOWN:
  9644. /*
  9645. * When the mode could not be determined the DUT could be
  9646. * in App mode running corrupted FW or FW that is not
  9647. * responding to the mode request, assume no communication
  9648. * and do a hard reset
  9649. */
  9650. mutex_lock(&cd->system_lock);
  9651. cd->startup_status = STARTUP_STATUS_START;
  9652. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9653. mutex_unlock(&cd->system_lock);
  9654. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9655. if (rc) {
  9656. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9657. goto exit;
  9658. }
  9659. break;
  9660. case PT_MODE_OPERATIONAL:
  9661. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9662. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9663. __func__);
  9664. rc = pt_pip_suspend_scanning_(cd);
  9665. if (rc) {
  9666. /*
  9667. * Print to log but don't exit, the FW could be
  9668. * running but be hung or fail to respond to
  9669. * this request
  9670. */
  9671. pt_debug(dev, DL_ERROR,
  9672. "%s Suspend Scan Failed\n", __func__);
  9673. }
  9674. /* sleep to allow the suspend scan to be processed */
  9675. usleep_range(1000, 2000);
  9676. }
  9677. mutex_lock(&cd->system_lock);
  9678. cd->startup_status = STARTUP_STATUS_START;
  9679. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9680. mutex_unlock(&cd->system_lock);
  9681. /* Reset device to enter the BL */
  9682. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9683. if (rc) {
  9684. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9685. goto exit;
  9686. }
  9687. break;
  9688. case PT_MODE_BOOTLOADER:
  9689. /* Do nothing as we are already in the BL */
  9690. tmp_result = PT_ENTER_BL_PASS;
  9691. goto exit;
  9692. default:
  9693. /* Should NEVER get here */
  9694. tmp_result = PT_ENTER_BL_ERROR;
  9695. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9696. goto exit;
  9697. }
  9698. if (!cd->flashless_dut &&
  9699. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9700. /*
  9701. * Sending the special "Host Mode" command will instruct the
  9702. * BL to not execute the FW it has loaded into RAM.
  9703. * The command must be sent within a 40ms window from releasing
  9704. * the XRES pin. If the messages is sent too early it will NAK,
  9705. * so keep sending it every 2ms until it is accepted by the BL.
  9706. * A no-flash DUT does not require this command as there is no
  9707. * FW for the BL to load and execute.
  9708. */
  9709. usleep_range(4000, 6000);
  9710. pt_debug(cd->dev, DL_INFO,
  9711. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9712. __func__, (int)sizeof(host_mode_cmd));
  9713. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9714. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9715. rc = 1;
  9716. while (rc && time < 34) {
  9717. rc = pt_adap_write_read_specific(cd, 4,
  9718. host_mode_cmd, NULL);
  9719. usleep_range(1800, 2000);
  9720. time += 2;
  9721. }
  9722. /* Sleep to allow the BL to come up */
  9723. usleep_range(1000, 2000);
  9724. }
  9725. /*
  9726. * To avoid the case that next PIP command can be confused by BL/FW
  9727. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  9728. * followed by "wakeup event" function can lower the failure rate.
  9729. */
  9730. t = wait_event_timeout(cd->wait_q,
  9731. ((cd->startup_status != STARTUP_STATUS_START)
  9732. && (cd->hid_reset_cmd_state == 0)),
  9733. msecs_to_jiffies(300));
  9734. if (IS_TMO(t)) {
  9735. pt_debug(cd->dev, DL_ERROR,
  9736. "%s: TMO waiting for BL sentinel\n", __func__);
  9737. }
  9738. /* Check if device is now in BL mode */
  9739. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9740. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  9741. cd->startup_status);
  9742. if (!rc && mode == PT_MODE_BOOTLOADER) {
  9743. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  9744. mutex_lock(&cd->system_lock);
  9745. cd->pip2_prot_active = true;
  9746. cd->mode = PT_MODE_BOOTLOADER;
  9747. mutex_unlock(&cd->system_lock);
  9748. tmp_result = PT_ENTER_BL_PASS;
  9749. } else {
  9750. /*
  9751. * If the device doesn't enter BL mode as expected and rc is
  9752. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  9753. * the function should return an error code to indicate this
  9754. * failure PT_ENTER_BL_CONFIRM_FAIL.
  9755. */
  9756. if (!rc)
  9757. rc = -EINVAL;
  9758. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  9759. mutex_lock(&cd->system_lock);
  9760. cd->mode = mode;
  9761. mutex_unlock(&cd->system_lock);
  9762. pt_debug(dev, DL_ERROR,
  9763. "%s ERROR: Not in BL as expected", __func__);
  9764. }
  9765. exit:
  9766. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  9767. /* Check to get (buffered) flash information */
  9768. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  9769. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  9770. read_buf, &actual_read_len);
  9771. if (!rc) {
  9772. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  9773. pt_debug(
  9774. dev, DL_WARN,
  9775. "%s Unavailable Manufacturer ID: 0x%02x\n",
  9776. __func__,
  9777. read_buf[PIP2_RESP_BODY_OFFSET]);
  9778. /*
  9779. * If the BL was unable to cache the correct
  9780. * values when entering the first time due to
  9781. * the Flash part not having been powered up
  9782. * long enough, re-enter the BL to trigger the
  9783. * BL to re-attempt to cache the values.
  9784. */
  9785. if (flash_info_retry-- > 0) {
  9786. mode = PT_MODE_UNKNOWN;
  9787. pt_debug(dev, DL_WARN,
  9788. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  9789. __func__, flash_info_retry);
  9790. goto _retry;
  9791. } else {
  9792. pt_debug(dev, DL_WARN,
  9793. "%s Manufacturer ID Unknown\n",
  9794. __func__);
  9795. tmp_result = PT_ENTER_BL_PASS;
  9796. }
  9797. }
  9798. } else {
  9799. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  9800. pt_debug(
  9801. dev, DL_ERROR,
  9802. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  9803. __func__);
  9804. }
  9805. }
  9806. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  9807. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  9808. "SUPPRESS");
  9809. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  9810. if (result)
  9811. *result = tmp_result;
  9812. return rc;
  9813. }
  9814. /*******************************************************************************
  9815. * FUNCTION: _pt_pip2_file_open
  9816. *
  9817. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  9818. *
  9819. * NOTE: The DUT must be in BL mode for this command to work
  9820. *
  9821. * RETURNS:
  9822. * <0 = Error
  9823. * >0 = file handle opened
  9824. *
  9825. * PARAMETERS:
  9826. * *dev - pointer to device structure
  9827. * file_no - PIP2 file number to open
  9828. ******************************************************************************/
  9829. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  9830. {
  9831. int ret = 0;
  9832. u16 status;
  9833. u16 actual_read_len;
  9834. u8 file_handle;
  9835. u8 data[2];
  9836. u8 read_buf[10];
  9837. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  9838. data[0] = file_no;
  9839. ret = _pt_request_pip2_send_cmd(dev,
  9840. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  9841. data, 1, read_buf, &actual_read_len);
  9842. if (ret) {
  9843. pt_debug(dev, DL_ERROR,
  9844. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  9845. __func__, file_no);
  9846. return -PIP2_RSP_ERR_NOT_OPEN;
  9847. }
  9848. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9849. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  9850. if (ret || ((status != 0x00) && (status != 0x03)) ||
  9851. (file_handle != file_no)) {
  9852. pt_debug(dev, DL_ERROR,
  9853. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  9854. __func__, status, file_handle);
  9855. return -status;
  9856. }
  9857. return file_handle;
  9858. }
  9859. /*******************************************************************************
  9860. * FUNCTION: _pt_pip2_file_close
  9861. *
  9862. * SUMMARY: Using the BL PIP2 commands close a file
  9863. *
  9864. * NOTE: The DUT must be in BL mode for this command to work
  9865. *
  9866. * RETURNS:
  9867. * <0 = Error
  9868. * >0 = file handle closed
  9869. *
  9870. * PARAMETERS:
  9871. * *dev - pointer to device structure
  9872. * file_handle - handle to the file to be closed
  9873. ******************************************************************************/
  9874. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  9875. {
  9876. int ret = 0;
  9877. u16 status;
  9878. u16 actual_read_len;
  9879. u8 data[2];
  9880. u8 read_buf[32];
  9881. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  9882. data[0] = file_handle;
  9883. ret = _pt_request_pip2_send_cmd(dev,
  9884. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  9885. data, 1, read_buf, &actual_read_len);
  9886. if (ret) {
  9887. pt_debug(dev, DL_ERROR,
  9888. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  9889. __func__, file_handle);
  9890. return -ETIME;
  9891. }
  9892. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9893. if (status != 0x00) {
  9894. pt_debug(dev, DL_ERROR,
  9895. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  9896. __func__, status, file_handle);
  9897. return -status;
  9898. }
  9899. return file_handle;
  9900. }
  9901. /*******************************************************************************
  9902. * FUNCTION: _pt_pip2_file_erase
  9903. *
  9904. * SUMMARY: Using the BL PIP2 commands erase a file
  9905. *
  9906. * NOTE: The DUT must be in BL mode for this command to work
  9907. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  9908. * one retry is attempted for each sector in a file.
  9909. *
  9910. * RETURNS:
  9911. * <0 = Error
  9912. * >0 = file handle closed
  9913. *
  9914. * PARAMETERS:
  9915. * *dev - pointer to device structure
  9916. * file_handle - handle to the file to be erased
  9917. * *status - PIP2 erase status code
  9918. ******************************************************************************/
  9919. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  9920. {
  9921. int ret = 0;
  9922. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  9923. int retry = 1;
  9924. u16 actual_read_len;
  9925. u8 data[2];
  9926. u8 read_buf[10];
  9927. struct pt_core_data *cd = dev_get_drvdata(dev);
  9928. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  9929. data[0] = file_handle;
  9930. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  9931. *status = PIP2_RSP_ERR_TIMEOUT;
  9932. /* Increase waiting time for large file erase */
  9933. mutex_lock(&cd->system_lock);
  9934. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  9935. mutex_unlock(&cd->system_lock);
  9936. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  9937. ret = _pt_request_pip2_send_cmd(dev,
  9938. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  9939. data, 2, read_buf, &actual_read_len);
  9940. if (ret)
  9941. break;
  9942. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  9943. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  9944. #ifdef TTDL_DIAGNOSTICS
  9945. cd->file_erase_timeout_count++;
  9946. #endif
  9947. pt_debug(dev, DL_WARN,
  9948. "%s: ERASE timeout %d for file = %d\n",
  9949. __func__, retry, file_handle);
  9950. }
  9951. retry++;
  9952. }
  9953. mutex_lock(&cd->system_lock);
  9954. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  9955. mutex_unlock(&cd->system_lock);
  9956. if (ret) {
  9957. pt_debug(dev, DL_ERROR,
  9958. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  9959. __func__, ret, file_handle);
  9960. return -EIO;
  9961. }
  9962. if (*status != 0x00) {
  9963. pt_debug(dev, DL_ERROR,
  9964. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  9965. __func__, *status, file_handle);
  9966. return -EIO;
  9967. }
  9968. return file_handle;
  9969. }
  9970. /*******************************************************************************
  9971. * FUNCTION: _pt_pip2_file_read
  9972. *
  9973. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  9974. *
  9975. * NOTE: The DUT must be in BL mode for this command to work
  9976. *
  9977. * RETURNS:
  9978. * <0 = Error
  9979. * >0 = number of bytes read
  9980. *
  9981. * PARAMETERS:
  9982. * *dev - pointer to device structure
  9983. * file_handle - File handle to read from
  9984. * num_bytes - number of bytes to read
  9985. ******************************************************************************/
  9986. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  9987. u8 *read_buf)
  9988. {
  9989. int ret = 0;
  9990. u16 status;
  9991. u16 actual_read_len;
  9992. u8 data[3];
  9993. data[0] = file_handle;
  9994. data[1] = (num_bytes & 0x00FF);
  9995. data[2] = (num_bytes & 0xFF00) >> 8;
  9996. ret = _pt_request_pip2_send_cmd(dev,
  9997. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  9998. data, 3, read_buf, &actual_read_len);
  9999. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10000. if (ret || ((status != 0x00) && (status != 0x03))) {
  10001. pt_debug(dev, DL_ERROR,
  10002. "%s File open failure with error code = %d\n",
  10003. __func__, status);
  10004. return -EPERM;
  10005. }
  10006. ret = num_bytes;
  10007. return ret;
  10008. }
  10009. /*******************************************************************************
  10010. * FUNCTION: _pt_read_us_file
  10011. *
  10012. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10013. * then read the entire file.
  10014. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10015. *
  10016. * RETURN:
  10017. * 0 = success
  10018. * !0 = failure
  10019. *
  10020. * PARAMETERS:
  10021. * *file_path - pointer to the file path
  10022. * *buf - pointer to the buffer to store the file contents
  10023. * *size - pointer to the size of the file
  10024. ******************************************************************************/
  10025. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10026. {
  10027. struct file *filp = NULL;
  10028. struct inode *inode = NULL;
  10029. unsigned int file_len = 0;
  10030. unsigned int read_len = 0;
  10031. mm_segment_t oldfs;
  10032. int rc = 0;
  10033. if (file_path == NULL || buf == NULL) {
  10034. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10035. return -EINVAL;
  10036. }
  10037. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10038. oldfs = get_fs();
  10039. set_fs(KERNEL_DS);
  10040. filp = filp_open(file_path, O_RDONLY, 0400);
  10041. if (IS_ERR(filp)) {
  10042. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10043. file_path);
  10044. rc = -ENOENT;
  10045. goto err;
  10046. }
  10047. if (filp->f_op == NULL) {
  10048. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10049. __func__);
  10050. rc = -EINVAL;
  10051. goto exit;
  10052. }
  10053. inode = filp->f_path.dentry->d_inode;
  10054. if (inode == NULL) {
  10055. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10056. __func__);
  10057. rc = -EINVAL;
  10058. goto exit;
  10059. }
  10060. file_len = i_size_read(inode->i_mapping->host);
  10061. if (file_len == 0) {
  10062. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10063. __func__, file_len);
  10064. rc = -EINVAL;
  10065. goto exit;
  10066. }
  10067. if (*size == 0)
  10068. read_len = file_len;
  10069. else
  10070. read_len = *size;
  10071. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10072. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10073. __func__, read_len);
  10074. rc = -EINVAL;
  10075. goto exit;
  10076. }
  10077. filp->private_data = inode->i_private;
  10078. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10079. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10080. rc = -EINVAL;
  10081. goto exit;
  10082. }
  10083. *size = read_len;
  10084. exit:
  10085. if (filp_close(filp, NULL) != 0)
  10086. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10087. err:
  10088. set_fs(oldfs);
  10089. return rc;
  10090. }
  10091. /*******************************************************************************
  10092. * FUNCTION: _pt_request_pip2_bin_hdr
  10093. *
  10094. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10095. * in the case of a flashless DUT, and parse the contents
  10096. *
  10097. * RETURNS:
  10098. * 0 = Success
  10099. * !0 = Error condition
  10100. *
  10101. * PARAMETERS:
  10102. * *dev - pointer to device structure
  10103. ******************************************************************************/
  10104. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10105. {
  10106. struct pt_core_data *cd = dev_get_drvdata(dev);
  10107. u8 file_handle;
  10108. u8 read_buf[255];
  10109. u8 hdr_len = 0;
  10110. u8 i;
  10111. int bytes_read;
  10112. int read_size;
  10113. int ret = 0;
  10114. int rc = 0;
  10115. bool load_hdr_struct = false;
  10116. if (cd->flashless_dut) {
  10117. read_size = sizeof(read_buf);
  10118. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10119. read_buf, &read_size);
  10120. if (rc) {
  10121. ret = rc;
  10122. pt_debug(dev, DL_ERROR,
  10123. "%s Failed to read fw image from US, rc=%d\n",
  10124. __func__, rc);
  10125. goto exit;
  10126. }
  10127. load_hdr_struct = true;
  10128. hdr_len = read_buf[0];
  10129. i = 0;
  10130. } else {
  10131. if (cd->mode != PT_MODE_BOOTLOADER) {
  10132. ret = -EPERM;
  10133. goto exit;
  10134. }
  10135. /* Open the bin file in Flash */
  10136. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10137. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10138. if (file_handle != PIP2_FW_FILE) {
  10139. ret = -ENOENT;
  10140. pt_debug(dev, DL_ERROR,
  10141. "%s Failed to open bin file\n", __func__);
  10142. goto exit;
  10143. }
  10144. /* Read the header length from the file */
  10145. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10146. read_size = 1;
  10147. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10148. read_buf);
  10149. if (bytes_read != read_size) {
  10150. ret = -EX_ERR_FREAD;
  10151. pt_debug(dev, DL_ERROR,
  10152. "%s Failed to bin file header len\n", __func__);
  10153. goto exit_close_file;
  10154. }
  10155. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10156. if (hdr_len == 0xFF) {
  10157. ret = -EX_ERR_FLEN;
  10158. pt_debug(dev, DL_ERROR,
  10159. "%s Bin header len is invalid\n", __func__);
  10160. goto exit_close_file;
  10161. }
  10162. /* Read the rest of the header from the bin file */
  10163. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10164. memset(read_buf, 0, sizeof(read_buf));
  10165. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10166. read_buf);
  10167. if (bytes_read != hdr_len) {
  10168. ret = -EX_ERR_FREAD;
  10169. pt_debug(dev, DL_ERROR,
  10170. "%s Failed to read bin file\n", __func__);
  10171. goto exit_close_file;
  10172. }
  10173. load_hdr_struct = true;
  10174. exit_close_file:
  10175. /* Close the file */
  10176. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10177. ret = -EX_ERR_FCLOSE;
  10178. pt_debug(dev, DL_ERROR,
  10179. "%s Failed to close bin file\n", __func__);
  10180. }
  10181. /*
  10182. * The length was already read so subtract 1 to make the rest of
  10183. * the offsets match the spec
  10184. */
  10185. i = PIP2_RESP_BODY_OFFSET - 1;
  10186. }
  10187. if (load_hdr_struct) {
  10188. hdr->length = hdr_len;
  10189. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10190. hdr->fw_major = (read_buf[i+3]);
  10191. hdr->fw_minor = (read_buf[i+4]);
  10192. hdr->fw_crc = (read_buf[i+5] << 24) |
  10193. (read_buf[i+6] << 16) |
  10194. (read_buf[i+7] << 8) |
  10195. (read_buf[i+8]);
  10196. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10197. (read_buf[i+10] << 16) |
  10198. (read_buf[i+11] << 8) |
  10199. (read_buf[i+12]);
  10200. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10201. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10202. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10203. if (hdr_len >= 22) {
  10204. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10205. (read_buf[i+20] << 16) |
  10206. (read_buf[i+21] << 8) |
  10207. (read_buf[i+22]);
  10208. } else {
  10209. hdr->hex_file_size = 0;
  10210. }
  10211. }
  10212. exit:
  10213. return ret;
  10214. }
  10215. /*******************************************************************************
  10216. * FUNCTION: _pt_pip2_file_get_stats
  10217. *
  10218. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10219. * opened file
  10220. *
  10221. * NOTE: The DUT must be in BL mode for this command to work
  10222. *
  10223. * RETURNS:
  10224. * !0 = Error
  10225. * 0 = Success
  10226. *
  10227. * PARAMETERS:
  10228. * *dev - pointer to device structure
  10229. * file_handle - File handle to read from
  10230. * *address - pointer to store address of file
  10231. * *file_size _ pointer to store size of file
  10232. ******************************************************************************/
  10233. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10234. u32 *file_size)
  10235. {
  10236. int ret = 1;
  10237. u16 status;
  10238. u16 actual_read_len;
  10239. u8 data[2];
  10240. u8 read_buf[16];
  10241. data[0] = file_handle;
  10242. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10243. ret = _pt_request_pip2_send_cmd(dev,
  10244. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10245. data, 2, read_buf, &actual_read_len);
  10246. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10247. if (ret || (status != 0x00)) {
  10248. pt_debug(dev, DL_ERROR,
  10249. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10250. __func__, status, file_handle, ret);
  10251. ret = -EIO;
  10252. goto exit;
  10253. }
  10254. pt_debug(dev, DL_DEBUG,
  10255. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10256. if (address) {
  10257. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10258. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10259. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10260. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10261. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10262. __func__, *address);
  10263. }
  10264. if (file_size) {
  10265. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10266. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10267. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10268. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10269. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10270. __func__, *file_size);
  10271. }
  10272. exit:
  10273. return ret;
  10274. }
  10275. /*******************************************************************************
  10276. * FUNCTION: _pt_pip2_file_seek_offset
  10277. *
  10278. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10279. * opened file
  10280. *
  10281. * NOTE: The DUT must be in BL mode for this command to work
  10282. * NOTE: File open/erase command can reset the offset
  10283. *
  10284. * RETURNS:
  10285. * !0 = Error
  10286. * 0 = Success
  10287. *
  10288. * PARAMETERS:
  10289. * *dev - pointer to device structure
  10290. * file_handle - File handle to read from
  10291. * read_offset - read offset of file
  10292. * write_offset - write offset of file
  10293. ******************************************************************************/
  10294. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10295. u32 read_offset, u32 write_offset)
  10296. {
  10297. int ret = 1;
  10298. u16 status;
  10299. u16 actual_read_len;
  10300. u8 data[10];
  10301. u8 read_buf[16];
  10302. data[0] = file_handle;
  10303. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10304. data[2] = 0xff & read_offset;
  10305. data[3] = 0xff & (read_offset >> 8);
  10306. data[4] = 0xff & (read_offset >> 16);
  10307. data[5] = 0xff & (read_offset >> 24);
  10308. data[6] = 0xff & write_offset;
  10309. data[7] = 0xff & (write_offset >> 8);
  10310. data[8] = 0xff & (write_offset >> 16);
  10311. data[9] = 0xff & (write_offset >> 24);
  10312. ret = _pt_request_pip2_send_cmd(dev,
  10313. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10314. data, 10, read_buf, &actual_read_len);
  10315. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10316. if (ret || (status != 0x00)) {
  10317. pt_debug(dev, DL_ERROR,
  10318. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10319. __func__, status, ret, file_handle);
  10320. ret = -EIO;
  10321. }
  10322. return ret;
  10323. }
  10324. /*******************************************************************************
  10325. * FUNCTION: _pt_pip2_file_crc
  10326. *
  10327. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10328. * the file.
  10329. *
  10330. * NOTE: The DUT must be in BL mode for this command to work
  10331. * NOTE: This command only can be used for BL version 1.8 or greater.
  10332. * BL version 1.8 added this change according to PGV-173.
  10333. *
  10334. * RETURNS:
  10335. * !0 = Error
  10336. * 0 = Success
  10337. *
  10338. * PARAMETERS:
  10339. * *dev - pointer to device structure
  10340. * file_handle - File handle to read from
  10341. * offset - start offset for CRC calculation
  10342. * length - number of bytes to calculate CRC over
  10343. * read_buf - pointer to the read buffer
  10344. ******************************************************************************/
  10345. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10346. u32 offset, u32 length, u8 *read_buf)
  10347. {
  10348. int rc = 1;
  10349. u16 actual_read_len;
  10350. u8 data[10];
  10351. data[0] = file_handle;
  10352. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10353. data[2] = 0xff & offset;
  10354. data[3] = 0xff & (offset >> 8);
  10355. data[4] = 0xff & (offset >> 16);
  10356. data[5] = 0xff & (offset >> 24);
  10357. data[6] = 0xff & length;
  10358. data[7] = 0xff & (length >> 8);
  10359. data[8] = 0xff & (length >> 16);
  10360. data[9] = 0xff & (length >> 24);
  10361. rc = _pt_request_pip2_send_cmd(dev,
  10362. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10363. data, 10, read_buf, &actual_read_len);
  10364. if (rc)
  10365. pt_debug(dev, DL_ERROR,
  10366. "%s Return FILE_CRC failure, rc = %d\n",
  10367. __func__, rc);
  10368. return rc;
  10369. }
  10370. /*******************************************************************************
  10371. * FUNCTION: pt_pip2_ping_test
  10372. *
  10373. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10374. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10375. * response payload matches what was sent.
  10376. * The max payload size is 247:
  10377. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10378. *
  10379. * RETURN:
  10380. * 0 = success
  10381. * !0 = failure
  10382. *
  10383. * PARAMETERS:
  10384. * *dev - pointer to device structure
  10385. * *attr - pointer to device attributes
  10386. * *buf - pointer to output buffer
  10387. ******************************************************************************/
  10388. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10389. {
  10390. u16 actual_read_len;
  10391. u8 *read_buf = NULL;
  10392. u8 *data = NULL;
  10393. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10394. int i = 1;
  10395. int j = 0;
  10396. int rc = 0;
  10397. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10398. if (!read_buf)
  10399. goto ping_test_exit;
  10400. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10401. if (!data)
  10402. goto ping_test_exit;
  10403. /* Load data payload with an array of walking 1's */
  10404. for (i = 0; i < 255; i++)
  10405. data[i] = 0x01 << (i % 8);
  10406. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10407. for (i = 0; i <= max_bytes; i++) {
  10408. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10409. PIP2_CMD_ID_PING, data, i, read_buf,
  10410. &actual_read_len);
  10411. if (rc) {
  10412. pt_debug(dev, DL_ERROR,
  10413. "%s: PING failed with %d byte payload\n",
  10414. __func__, i);
  10415. break;
  10416. }
  10417. /* Verify data returned matches data sent */
  10418. for (j = 0; j < i; j++) {
  10419. if (read_buf[data_offset + j] != data[j]) {
  10420. pt_debug(dev, DL_DEBUG,
  10421. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10422. __func__, i, j, data[j], j,
  10423. read_buf[data_offset + j]);
  10424. goto ping_test_exit;
  10425. }
  10426. }
  10427. }
  10428. ping_test_exit:
  10429. *last_packet_size = i - 1;
  10430. kfree(read_buf);
  10431. kfree(data);
  10432. return rc;
  10433. }
  10434. /*******************************************************************************
  10435. * FUNCTION: _pt_ic_parse_input_hex
  10436. *
  10437. * SUMMARY: Parse a char data array as space delimited hex values into
  10438. * an int array.
  10439. *
  10440. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10441. * buffer to store data is "u32". This function is still needed by the
  10442. * "command" sysfs node because the buffer type to store data is "u8".
  10443. *
  10444. * RETURN: Length of parsed data
  10445. *
  10446. * PARAMETERS:
  10447. * *dev - pointer to device structure
  10448. * *buf - pointer to buffer that holds the input array to parse
  10449. * buf_size - size of buf
  10450. * *ic_buf - pointer to array to store parsed data
  10451. * ic_buf_size - max size of ic_buf
  10452. ******************************************************************************/
  10453. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10454. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10455. {
  10456. const char *pbuf = buf;
  10457. unsigned long value;
  10458. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10459. u32 i = 0;
  10460. u32 j;
  10461. int last = 0;
  10462. int ret;
  10463. pt_debug(dev, DL_DEBUG,
  10464. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10465. __func__, pbuf, buf, buf_size, "scan buf size",
  10466. PT_MAX_PIP2_MSG_SIZE, buf);
  10467. while (pbuf <= (buf + buf_size)) {
  10468. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10469. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10470. __func__, "Max cmd size exceeded", i,
  10471. PT_MAX_PIP2_MSG_SIZE);
  10472. return -EINVAL;
  10473. }
  10474. if (i >= ic_buf_size) {
  10475. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10476. __func__, "Buffer size exceeded", i,
  10477. ic_buf_size);
  10478. return -EINVAL;
  10479. }
  10480. while (((*pbuf == ' ') || (*pbuf == ','))
  10481. && (pbuf < (buf + buf_size))) {
  10482. last = *pbuf;
  10483. pbuf++;
  10484. }
  10485. if (pbuf >= (buf + buf_size))
  10486. break;
  10487. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10488. if ((last == ',') && (*pbuf == ',')) {
  10489. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10490. __func__, "Invalid data format.");
  10491. return -EINVAL;
  10492. }
  10493. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10494. && (pbuf < (buf + buf_size))
  10495. && (*pbuf != ' ')
  10496. && (*pbuf != ','); j++) {
  10497. last = *pbuf;
  10498. scan_buf[j] = *pbuf++;
  10499. }
  10500. ret = kstrtoul(scan_buf, 16, &value);
  10501. if (ret < 0) {
  10502. pt_debug(dev, DL_ERROR,
  10503. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10504. "Invalid data format. ", scan_buf,
  10505. "Use \"0xHH,...,0xHH\"", " instead.",
  10506. i, ret);
  10507. return ret;
  10508. }
  10509. ic_buf[i] = value;
  10510. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10511. __func__, i, value);
  10512. i++;
  10513. }
  10514. return i;
  10515. }
  10516. /*******************************************************************************
  10517. * FUNCTION: _pt_ic_parse_input
  10518. *
  10519. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10520. * hex values or dec values into an int array with the following rules:
  10521. * 1) Hex values must have a "0x" prefix for each element or the first element
  10522. * only
  10523. * 2) Dec values do not have any prefix
  10524. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10525. * string
  10526. *
  10527. * RETURN: Number of values parsed
  10528. *
  10529. * PARAMETERS:
  10530. * *dev - pointer to device structure
  10531. * *buf - pointer to buffer that holds the input array to parse
  10532. * buf_size - size of buf
  10533. * *out_buf - pointer to array to store parsed data
  10534. * out_buf_size - max size of buffer to be stored
  10535. ******************************************************************************/
  10536. static int _pt_ic_parse_input(struct device *dev,
  10537. const char *buf, size_t buf_size,
  10538. u32 *out_buf, size_t out_buf_size)
  10539. {
  10540. const char *pbuf = buf;
  10541. unsigned long value;
  10542. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10543. u32 i = 0;
  10544. u32 j;
  10545. int last = 0;
  10546. int ret = 0;
  10547. u8 str_base = 0;
  10548. pt_debug(dev, DL_DEBUG,
  10549. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10550. __func__, buf_size, out_buf_size, "scan buf size",
  10551. PT_MAX_PIP2_MSG_SIZE, buf);
  10552. while (pbuf <= (buf + buf_size)) {
  10553. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10554. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10555. __func__, "Max cmd size exceeded", i,
  10556. PT_MAX_PIP2_MSG_SIZE);
  10557. ret = -EINVAL;
  10558. goto error;
  10559. }
  10560. if (i >= out_buf_size) {
  10561. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10562. __func__, "Buffer size exceeded", i,
  10563. out_buf_size);
  10564. ret = -EINVAL;
  10565. goto error;
  10566. }
  10567. while (((*pbuf == ' ') || (*pbuf == ','))
  10568. && (pbuf < (buf + buf_size))) {
  10569. last = *pbuf;
  10570. pbuf++;
  10571. }
  10572. if (pbuf >= (buf + buf_size))
  10573. break;
  10574. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10575. if ((last == ',') && (*pbuf == ',')) {
  10576. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10577. __func__, "Invalid data format.");
  10578. ret = -EINVAL;
  10579. goto error;
  10580. }
  10581. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10582. && (pbuf < (buf + buf_size))
  10583. && (*pbuf != ' ')
  10584. && (*pbuf != ','); j++) {
  10585. last = *pbuf;
  10586. scan_buf[j] = *pbuf++;
  10587. }
  10588. if (i == 0) {
  10589. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10590. (strncmp(scan_buf, "0X", 2) == 0))
  10591. str_base = 16;
  10592. else
  10593. str_base = 10;
  10594. } else {
  10595. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10596. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10597. (str_base == 10)) {
  10598. pt_debug(dev, DL_ERROR,
  10599. "%s: Decimal and Heximal data mixed\n",
  10600. __func__);
  10601. ret = -EINVAL;
  10602. goto error;
  10603. }
  10604. }
  10605. ret = kstrtoul(scan_buf, str_base, &value);
  10606. if (ret < 0) {
  10607. pt_debug(dev, DL_ERROR,
  10608. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10609. "Invalid data format. ", scan_buf,
  10610. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10611. " instead.", i, ret);
  10612. goto error;
  10613. }
  10614. out_buf[i] = value;
  10615. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10616. __func__, i, value, value);
  10617. i++;
  10618. }
  10619. ret = i;
  10620. error:
  10621. return ret;
  10622. }
  10623. #ifdef TTHE_TUNER_SUPPORT
  10624. /*******************************************************************************
  10625. * FUNCTION: tthe_debugfs_open
  10626. *
  10627. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10628. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10629. * printing a large number of fingers and sensor data without overflowing
  10630. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10631. * even multiple of PAGE_SIZE
  10632. *
  10633. * RETURN:
  10634. * 0 = success
  10635. * !0 = failure
  10636. *
  10637. * PARAMETERS:
  10638. * *inode - file inode number
  10639. * *filp - file pointer to debugfs file
  10640. ******************************************************************************/
  10641. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10642. {
  10643. struct pt_core_data *cd = inode->i_private;
  10644. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10645. filp->private_data = inode->i_private;
  10646. if (cd->tthe_buf)
  10647. return -EBUSY;
  10648. while (buf_size < 4096)
  10649. buf_size = buf_size << 1;
  10650. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10651. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10652. cd->tthe_buf_size = buf_size;
  10653. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10654. if (!cd->tthe_buf)
  10655. return -ENOMEM;
  10656. return 0;
  10657. }
  10658. /*******************************************************************************
  10659. * FUNCTION: tthe_debugfs_close
  10660. *
  10661. * SUMMARY: Close method for tthe_tuner debugfs node.
  10662. *
  10663. * RETURN:
  10664. * 0 = success
  10665. * !0 = failure
  10666. *
  10667. * PARAMETERS:
  10668. * *inode - file inode number
  10669. * *filp - file pointer to debugfs file
  10670. ******************************************************************************/
  10671. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10672. {
  10673. struct pt_core_data *cd = filp->private_data;
  10674. filp->private_data = NULL;
  10675. kfree(cd->tthe_buf);
  10676. cd->tthe_buf = NULL;
  10677. return 0;
  10678. }
  10679. /*******************************************************************************
  10680. * FUNCTION: tthe_debugfs_read
  10681. *
  10682. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10683. * tthe_buf to user buffer.
  10684. *
  10685. * RETURN: Size of debugfs data print
  10686. *
  10687. * PARAMETERS:
  10688. * *filp - file pointer to debugfs file
  10689. * *buf - the user space buffer to read to
  10690. * count - the maximum number of bytes to read
  10691. * *ppos - the current position in the buffer
  10692. ******************************************************************************/
  10693. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10694. size_t count, loff_t *ppos)
  10695. {
  10696. struct pt_core_data *cd = filp->private_data;
  10697. int size;
  10698. int ret;
  10699. static int partial_read;
  10700. wait_event_interruptible(cd->wait_q,
  10701. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10702. mutex_lock(&cd->tthe_lock);
  10703. if (cd->tthe_exit) {
  10704. mutex_unlock(&cd->tthe_lock);
  10705. return 0;
  10706. }
  10707. if (count > cd->tthe_buf_len)
  10708. size = cd->tthe_buf_len;
  10709. else
  10710. size = count;
  10711. if (!size) {
  10712. mutex_unlock(&cd->tthe_lock);
  10713. return 0;
  10714. }
  10715. if (partial_read) {
  10716. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10717. partial_read = 0;
  10718. } else {
  10719. ret = copy_to_user(buf, cd->tthe_buf, size);
  10720. }
  10721. if (size == count)
  10722. partial_read = count;
  10723. if (ret == size)
  10724. return -EFAULT;
  10725. size -= ret;
  10726. cd->tthe_buf_len -= size;
  10727. mutex_unlock(&cd->tthe_lock);
  10728. *ppos += size;
  10729. return size;
  10730. }
  10731. static const struct file_operations tthe_debugfs_fops = {
  10732. .open = tthe_debugfs_open,
  10733. .release = tthe_debugfs_close,
  10734. .read = tthe_debugfs_read,
  10735. };
  10736. #endif
  10737. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  10738. .start_bl = _pt_request_pip_start_bl,
  10739. .suspend_scanning = _pt_request_pip_suspend_scanning,
  10740. .resume_scanning = _pt_request_pip_resume_scanning,
  10741. .get_param = _pt_request_pip_get_param,
  10742. .set_param = _pt_request_pip_set_param,
  10743. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  10744. .get_config_row_size = _pt_request_pip_get_config_row_size,
  10745. .get_data_structure = _pt_request_pip_get_data_structure,
  10746. .run_selftest = _pt_request_pip_run_selftest,
  10747. .get_selftest_result = _pt_request_pip_get_selftest_result,
  10748. .load_self_test_param = _pt_request_pip_load_self_test_param,
  10749. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  10750. .calibrate_ext = _pt_request_pip_calibrate_ext,
  10751. .initialize_baselines = _pt_request_pip_initialize_baselines,
  10752. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  10753. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  10754. .read_data_block = _pt_request_pip_read_data_block,
  10755. .write_data_block = _pt_request_pip_write_data_block,
  10756. .user_cmd = _pt_request_pip_user_cmd,
  10757. .get_bl_info = _pt_request_pip_bl_get_information,
  10758. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  10759. .launch_app = _pt_request_pip_launch_app,
  10760. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  10761. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  10762. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  10763. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  10764. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  10765. .pip2_file_open = _pt_pip2_file_open,
  10766. .pip2_file_close = _pt_pip2_file_close,
  10767. .pip2_file_erase = _pt_pip2_file_erase,
  10768. .read_us_file = _pt_read_us_file,
  10769. .manage_cal_data = _pt_manage_local_cal_data,
  10770. .calc_crc = crc_ccitt_calculate,
  10771. #ifdef TTDL_DIAGNOSTICS
  10772. .pip2_file_read = _pt_pip2_file_read,
  10773. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  10774. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  10775. .pip2_file_crc = _pt_pip2_file_crc,
  10776. #endif
  10777. };
  10778. static struct pt_core_commands _pt_core_commands = {
  10779. .subscribe_attention = _pt_subscribe_attention,
  10780. .unsubscribe_attention = _pt_unsubscribe_attention,
  10781. .request_exclusive = _pt_request_exclusive,
  10782. .release_exclusive = _pt_release_exclusive,
  10783. .request_reset = _pt_request_reset,
  10784. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  10785. .request_enum = _pt_request_enum,
  10786. .request_sysinfo = _pt_request_sysinfo,
  10787. .request_loader_pdata = _pt_request_loader_pdata,
  10788. .request_stop_wd = _pt_request_stop_wd,
  10789. .request_start_wd = _pt_request_start_wd,
  10790. .request_get_mode = _pt_request_get_mode,
  10791. .request_active_pip_prot = _pt_request_active_pip_protocol,
  10792. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  10793. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  10794. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  10795. .request_dut_generation = _pt_request_dut_generation,
  10796. .request_hw_version = _pt_request_hw_version,
  10797. .parse_sysfs_input = _pt_ic_parse_input,
  10798. #ifdef TTHE_TUNER_SUPPORT
  10799. .request_tthe_print = _pt_request_tthe_print,
  10800. #endif
  10801. #ifdef TTDL_DIAGNOSTICS
  10802. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  10803. #endif
  10804. .nonhid_cmd = &_pt_core_nonhid_cmd,
  10805. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  10806. };
  10807. struct pt_core_commands *pt_get_commands(void)
  10808. {
  10809. return &_pt_core_commands;
  10810. }
  10811. EXPORT_SYMBOL_GPL(pt_get_commands);
  10812. static DEFINE_MUTEX(core_list_lock);
  10813. static LIST_HEAD(core_list);
  10814. static DEFINE_MUTEX(module_list_lock);
  10815. static LIST_HEAD(module_list);
  10816. static int core_number;
  10817. /*******************************************************************************
  10818. * FUNCTION: pt_probe_module
  10819. *
  10820. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  10821. *
  10822. * RETURN:
  10823. * 0 = success
  10824. * !0 = failure
  10825. *
  10826. * PARAMETERS:
  10827. * *cd - pointer to core data
  10828. * *module - pointer to module structure
  10829. ******************************************************************************/
  10830. static int pt_probe_module(struct pt_core_data *cd,
  10831. struct pt_module *module)
  10832. {
  10833. struct module_node *module_node;
  10834. int rc = 0;
  10835. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  10836. if (!module_node)
  10837. return -ENOMEM;
  10838. module_node->module = module;
  10839. mutex_lock(&cd->module_list_lock);
  10840. list_add(&module_node->node, &cd->module_list);
  10841. mutex_unlock(&cd->module_list_lock);
  10842. rc = module->probe(cd->dev, &module_node->data);
  10843. if (rc) {
  10844. /*
  10845. * Remove from the list when probe fails
  10846. * in order not to call release
  10847. */
  10848. mutex_lock(&cd->module_list_lock);
  10849. list_del(&module_node->node);
  10850. mutex_unlock(&cd->module_list_lock);
  10851. kfree(module_node);
  10852. goto exit;
  10853. }
  10854. exit:
  10855. return rc;
  10856. }
  10857. /*******************************************************************************
  10858. * FUNCTION: pt_release_module
  10859. *
  10860. * SUMMARY: Call the release pointer and remove the module pointer from
  10861. * module_list.
  10862. *
  10863. * PARAMETERS:
  10864. * *cd - pointer to core data
  10865. * *module - pointer to module structure
  10866. ******************************************************************************/
  10867. static void pt_release_module(struct pt_core_data *cd,
  10868. struct pt_module *module)
  10869. {
  10870. struct module_node *m, *m_n;
  10871. mutex_lock(&cd->module_list_lock);
  10872. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  10873. if (m->module == module) {
  10874. module->release(cd->dev, m->data);
  10875. list_del(&m->node);
  10876. kfree(m);
  10877. break;
  10878. }
  10879. mutex_unlock(&cd->module_list_lock);
  10880. }
  10881. /*******************************************************************************
  10882. * FUNCTION: pt_probe_modules
  10883. *
  10884. * SUMMARY: Iterate module_list and probe each module.
  10885. *
  10886. * PARAMETERS:
  10887. * *cd - pointer to core data
  10888. ******************************************************************************/
  10889. static void pt_probe_modules(struct pt_core_data *cd)
  10890. {
  10891. struct pt_module *m;
  10892. int rc = 0;
  10893. mutex_lock(&module_list_lock);
  10894. list_for_each_entry(m, &module_list, node) {
  10895. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  10896. __func__, m->name);
  10897. rc = pt_probe_module(cd, m);
  10898. if (rc)
  10899. pt_debug(cd->dev, DL_ERROR,
  10900. "%s: Probe fails for module %s\n",
  10901. __func__, m->name);
  10902. }
  10903. mutex_unlock(&module_list_lock);
  10904. }
  10905. /*******************************************************************************
  10906. * FUNCTION: pt_release_modules
  10907. *
  10908. * SUMMARY: Iterate module_list and remove each module.
  10909. *
  10910. * PARAMETERS:
  10911. * *cd - pointer to core data
  10912. ******************************************************************************/
  10913. static void pt_release_modules(struct pt_core_data *cd)
  10914. {
  10915. struct pt_module *m;
  10916. mutex_lock(&module_list_lock);
  10917. list_for_each_entry(m, &module_list, node)
  10918. pt_release_module(cd, m);
  10919. mutex_unlock(&module_list_lock);
  10920. }
  10921. /*******************************************************************************
  10922. * FUNCTION: pt_get_core_data
  10923. *
  10924. * SUMMARY: Iterate core_list and get core data.
  10925. *
  10926. * RETURN:
  10927. * pointer to core data or null pointer if fail
  10928. *
  10929. * PARAMETERS:
  10930. * *id - pointer to core id
  10931. ******************************************************************************/
  10932. struct pt_core_data *pt_get_core_data(char *id)
  10933. {
  10934. struct pt_core_data *d;
  10935. list_for_each_entry(d, &core_list, node)
  10936. if (!strncmp(d->core_id, id, 20))
  10937. return d;
  10938. return NULL;
  10939. }
  10940. EXPORT_SYMBOL_GPL(pt_get_core_data);
  10941. /*******************************************************************************
  10942. * FUNCTION: pt_add_core
  10943. *
  10944. * SUMMARY: Add core data to the core_list.
  10945. *
  10946. * PARAMETERS:
  10947. * *dev - pointer to device structure
  10948. ******************************************************************************/
  10949. static void pt_add_core(struct device *dev)
  10950. {
  10951. struct pt_core_data *d;
  10952. struct pt_core_data *cd = dev_get_drvdata(dev);
  10953. mutex_lock(&core_list_lock);
  10954. list_for_each_entry(d, &core_list, node)
  10955. if (d->dev == dev)
  10956. goto unlock;
  10957. list_add(&cd->node, &core_list);
  10958. unlock:
  10959. mutex_unlock(&core_list_lock);
  10960. }
  10961. /*******************************************************************************
  10962. * FUNCTION: pt_del_core
  10963. *
  10964. * SUMMARY: Remove core data from the core_list.
  10965. *
  10966. * PARAMETERS:
  10967. * *dev - pointer to device structure
  10968. ******************************************************************************/
  10969. static void pt_del_core(struct device *dev)
  10970. {
  10971. struct pt_core_data *d, *d_n;
  10972. mutex_lock(&core_list_lock);
  10973. list_for_each_entry_safe(d, d_n, &core_list, node)
  10974. if (d->dev == dev) {
  10975. list_del(&d->node);
  10976. goto unlock;
  10977. }
  10978. unlock:
  10979. mutex_unlock(&core_list_lock);
  10980. }
  10981. /*******************************************************************************
  10982. * FUNCTION: pt_register_module
  10983. *
  10984. * SUMMARY: Register the module to module_list and probe the module for each
  10985. * core.
  10986. *
  10987. * RETURN:
  10988. * 0 = success
  10989. * !0 = failure
  10990. *
  10991. * PARAMETERS:
  10992. * *module - pointer to module structure
  10993. ******************************************************************************/
  10994. int pt_register_module(struct pt_module *module)
  10995. {
  10996. struct pt_module *m;
  10997. struct pt_core_data *cd;
  10998. int rc = 0;
  10999. if (!module || !module->probe || !module->release)
  11000. return -EINVAL;
  11001. mutex_lock(&module_list_lock);
  11002. list_for_each_entry(m, &module_list, node)
  11003. if (m == module) {
  11004. rc = -EEXIST;
  11005. goto unlock;
  11006. }
  11007. list_add(&module->node, &module_list);
  11008. /* Probe the module for each core */
  11009. mutex_lock(&core_list_lock);
  11010. list_for_each_entry(cd, &core_list, node)
  11011. pt_probe_module(cd, module);
  11012. mutex_unlock(&core_list_lock);
  11013. unlock:
  11014. mutex_unlock(&module_list_lock);
  11015. return rc;
  11016. }
  11017. EXPORT_SYMBOL_GPL(pt_register_module);
  11018. /*******************************************************************************
  11019. * FUNCTION: pt_unregister_module
  11020. *
  11021. * SUMMARY: Release the module for each core and remove the module from
  11022. * module_list.
  11023. *
  11024. * RETURN:
  11025. * 0 = success
  11026. * !0 = failure
  11027. *
  11028. * PARAMETERS:
  11029. * *module - pointer to module structure
  11030. ******************************************************************************/
  11031. void pt_unregister_module(struct pt_module *module)
  11032. {
  11033. struct pt_module *m, *m_n;
  11034. struct pt_core_data *cd;
  11035. if (!module)
  11036. return;
  11037. mutex_lock(&module_list_lock);
  11038. /* Release the module for each core */
  11039. mutex_lock(&core_list_lock);
  11040. list_for_each_entry(cd, &core_list, node)
  11041. pt_release_module(cd, module);
  11042. mutex_unlock(&core_list_lock);
  11043. list_for_each_entry_safe(m, m_n, &module_list, node)
  11044. if (m == module) {
  11045. list_del(&m->node);
  11046. break;
  11047. }
  11048. mutex_unlock(&module_list_lock);
  11049. }
  11050. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11051. /*******************************************************************************
  11052. * FUNCTION: pt_get_module_data
  11053. *
  11054. * SUMMARY: Get module data from module_node by module_list.
  11055. *
  11056. * RETURN:
  11057. * pointer to module data
  11058. *
  11059. * PARAMETERS:
  11060. * *dev - pointer to device structure
  11061. * *module - pointer to module structure
  11062. ******************************************************************************/
  11063. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11064. {
  11065. struct pt_core_data *cd = dev_get_drvdata(dev);
  11066. struct module_node *m;
  11067. void *data = NULL;
  11068. mutex_lock(&cd->module_list_lock);
  11069. list_for_each_entry(m, &cd->module_list, node)
  11070. if (m->module == module) {
  11071. data = m->data;
  11072. break;
  11073. }
  11074. mutex_unlock(&cd->module_list_lock);
  11075. return data;
  11076. }
  11077. EXPORT_SYMBOL(pt_get_module_data);
  11078. #ifdef CONFIG_HAS_EARLYSUSPEND
  11079. /*******************************************************************************
  11080. * FUNCTION: pt_early_suspend
  11081. *
  11082. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11083. * attention list.
  11084. *
  11085. * PARAMETERS:
  11086. * *h - pointer to early_suspend structure
  11087. ******************************************************************************/
  11088. static void pt_early_suspend(struct early_suspend *h)
  11089. {
  11090. struct pt_core_data *cd =
  11091. container_of(h, struct pt_core_data, es);
  11092. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11093. }
  11094. /*******************************************************************************
  11095. * FUNCTION: pt_late_resume
  11096. *
  11097. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11098. * attention list.
  11099. *
  11100. * PARAMETERS:
  11101. * *h - pointer to early_suspend structure
  11102. ******************************************************************************/
  11103. static void pt_late_resume(struct early_suspend *h)
  11104. {
  11105. struct pt_core_data *cd =
  11106. container_of(h, struct pt_core_data, es);
  11107. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11108. }
  11109. /*******************************************************************************
  11110. * FUNCTION: pt_setup_early_suspend
  11111. *
  11112. * SUMMARY: Register early/suspend function to the system.
  11113. *
  11114. * PARAMETERS:
  11115. * *cd - pointer to core data
  11116. ******************************************************************************/
  11117. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11118. {
  11119. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11120. cd->es.suspend = pt_early_suspend;
  11121. cd->es.resume = pt_late_resume;
  11122. register_early_suspend(&cd->es);
  11123. }
  11124. #elif defined(CONFIG_FB)
  11125. /*******************************************************************************
  11126. * FUNCTION: fb_notifier_callback
  11127. *
  11128. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11129. * resume/suspend attention list.
  11130. *
  11131. * RETURN:
  11132. * 0 = success
  11133. *
  11134. * PARAMETERS:
  11135. * *self - pointer to notifier_block structure
  11136. * event - event type of fb notifier
  11137. * *data - pointer to fb_event structure
  11138. ******************************************************************************/
  11139. static int fb_notifier_callback(struct notifier_block *self,
  11140. unsigned long event, void *data)
  11141. {
  11142. struct pt_core_data *cd =
  11143. container_of(self, struct pt_core_data, fb_notifier);
  11144. struct fb_event *evdata = data;
  11145. int *blank;
  11146. if (event != FB_EVENT_BLANK || !evdata)
  11147. goto exit;
  11148. blank = evdata->data;
  11149. if (*blank == FB_BLANK_UNBLANK) {
  11150. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11151. if (cd->fb_state != FB_ON) {
  11152. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11153. #if defined(CONFIG_PM_SLEEP)
  11154. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11155. pt_core_resume_(cd->dev);
  11156. #endif
  11157. cd->fb_state = FB_ON;
  11158. }
  11159. } else if (*blank == FB_BLANK_POWERDOWN) {
  11160. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11161. if (cd->fb_state != FB_OFF) {
  11162. #if defined(CONFIG_PM_SLEEP)
  11163. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11164. pt_core_suspend_(cd->dev);
  11165. #endif
  11166. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11167. cd->fb_state = FB_OFF;
  11168. }
  11169. }
  11170. exit:
  11171. return 0;
  11172. }
  11173. /*******************************************************************************
  11174. * FUNCTION: pt_setup_fb_notifier
  11175. *
  11176. * SUMMARY: Set up call back function into fb notifier.
  11177. *
  11178. * PARAMETERS:
  11179. * *cd - pointer to core data
  11180. ******************************************************************************/
  11181. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11182. {
  11183. int rc = 0;
  11184. cd->fb_state = FB_ON;
  11185. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11186. rc = fb_register_client(&cd->fb_notifier);
  11187. if (rc)
  11188. pt_debug(cd->dev, DL_ERROR,
  11189. "Unable to register fb_notifier: %d\n", rc);
  11190. }
  11191. #endif
  11192. /*******************************************************************************
  11193. * FUNCTION: pt_watchdog_work
  11194. *
  11195. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11196. * sleeping then this function simply returns. If the DUT is awake the
  11197. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11198. * somehow a response is waiting on the DUT that has not been read. If
  11199. * this occurs the message is simply consumed. If or once the IRQ is
  11200. * cleared, a PIP PING message is sent to the DUT and if the response
  11201. * is received the watchdog succeeds and exits, if no response is seen
  11202. * a startup is queued unless the maximum number of startups have already
  11203. * been attempted, in that case a BL is attempted.
  11204. *
  11205. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11206. * work thread
  11207. *
  11208. * RETURN: void
  11209. *
  11210. * PARAMETERS:
  11211. * *work - pointer to a work structure for the watchdog work queue
  11212. ******************************************************************************/
  11213. static void pt_watchdog_work(struct work_struct *work)
  11214. {
  11215. int rc = 0;
  11216. struct pt_core_data *cd = container_of(work,
  11217. struct pt_core_data, watchdog_work);
  11218. /*
  11219. * if found the current sleep_state is SS_SLEEPING
  11220. * then no need to request_exclusive, directly return
  11221. */
  11222. if (cd->sleep_state == SS_SLEEPING)
  11223. return;
  11224. #ifdef TTDL_DIAGNOSTICS
  11225. cd->watchdog_count++;
  11226. #endif /* TTDL_DIAGNOSTICS */
  11227. /*
  11228. * The first WD interval was extended to allow DDI to come up.
  11229. * If the WD interval is not the default then adjust timer to the
  11230. * current setting. The user can override value with drv_debug sysfs.
  11231. */
  11232. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11233. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11234. msecs_to_jiffies(cd->watchdog_interval));
  11235. }
  11236. if (pt_check_irq_asserted(cd)) {
  11237. #ifdef TTDL_DIAGNOSTICS
  11238. cd->watchdog_irq_stuck_count++;
  11239. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11240. #endif /* TTDL_DIAGNOSTICS */
  11241. pt_debug(cd->dev, DL_ERROR,
  11242. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11243. __func__);
  11244. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11245. }
  11246. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11247. if (rc < 0) {
  11248. pt_debug(cd->dev, DL_ERROR,
  11249. "%s: fail get exclusive ex=%p own=%p\n",
  11250. __func__, cd->exclusive_dev, cd->dev);
  11251. goto queue_startup;
  11252. }
  11253. rc = pt_pip_null_(cd);
  11254. if (release_exclusive(cd, cd->dev) < 0)
  11255. pt_debug(cd->dev, DL_ERROR,
  11256. "%s: fail to release exclusive\n", __func__);
  11257. queue_startup:
  11258. if (rc) {
  11259. #ifdef TTDL_DIAGNOSTICS
  11260. cd->watchdog_failed_access_count++;
  11261. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11262. #endif /* TTDL_DIAGNOSTICS */
  11263. pt_debug(cd->dev, DL_ERROR,
  11264. "%s: failed to access device in WD, retry count=%d\n",
  11265. __func__, cd->startup_retry_count);
  11266. /* Already tried FW upgrade because of watchdog but failed */
  11267. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11268. return;
  11269. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11270. /*
  11271. * Any wrapper function that trys to disable the
  11272. * WD killing this worker cannot be called here.
  11273. */
  11274. rc = request_exclusive(cd, cd->dev,
  11275. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11276. if (rc < 0) {
  11277. pt_debug(cd->dev, DL_ERROR,
  11278. "%s: fail get exclusive ex=%p own=%p\n",
  11279. __func__, cd->exclusive_dev, cd->dev);
  11280. goto exit;
  11281. }
  11282. cd->hw_detected = false;
  11283. cd->startup_status = STARTUP_STATUS_START;
  11284. pt_debug(cd->dev, DL_DEBUG,
  11285. "%s: Startup Status Reset\n", __func__);
  11286. rc = pt_dut_reset_and_wait(cd);
  11287. if (release_exclusive(cd, cd->dev) < 0)
  11288. pt_debug(cd->dev, DL_ERROR,
  11289. "%s: fail to release exclusive\n",
  11290. __func__);
  11291. if (!rc) {
  11292. cd->hw_detected = true;
  11293. if (!cd->flashless_dut)
  11294. pt_queue_enum(cd);
  11295. }
  11296. #ifdef TTDL_DIAGNOSTICS
  11297. cd->wd_xres_count++;
  11298. pt_debug(cd->dev, DL_ERROR,
  11299. "%s: Comm Failed - DUT reset [#%d]\n",
  11300. __func__, cd->wd_xres_count);
  11301. #endif /* TTDL_DIAGNOSTICS */
  11302. } else {
  11303. /*
  11304. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11305. * reset the part to regain communications, try to BL
  11306. */
  11307. pt_debug(cd->dev, DL_ERROR,
  11308. "%s: WD DUT access failure, Start FW Upgrade\n",
  11309. __func__);
  11310. #ifdef TTDL_DIAGNOSTICS
  11311. /*
  11312. * When diagnostics is enabled allow TTDL to keep
  11313. * trying to find the DUT. This allows the DUT to be
  11314. * hot swap-able while the host stays running. In
  11315. * production this may not be wanted as a customer
  11316. * may have several touch drivers and any driver
  11317. * that doesn't match the current DUT should give
  11318. * up trying and give up using the bus.
  11319. */
  11320. pt_debug(cd->dev, DL_INFO,
  11321. "%s: Resetting startup_retry_count\n",
  11322. __func__);
  11323. cd->startup_retry_count = 0;
  11324. #endif /* TTDL_DIAGNOSTICS */
  11325. /*
  11326. * Since fw may be broken,reset sysinfo ready flag
  11327. * to let upgrade function work.
  11328. */
  11329. mutex_lock(&cd->system_lock);
  11330. cd->sysinfo.ready = false;
  11331. mutex_unlock(&cd->system_lock);
  11332. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11333. pt_debug(cd->dev, DL_ERROR,
  11334. "%s: Queue Restart\n", __func__);
  11335. pt_queue_restart(cd);
  11336. } else
  11337. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11338. }
  11339. } else {
  11340. cd->hw_detected = true;
  11341. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11342. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11343. pt_debug(cd->dev, DL_ERROR,
  11344. "%s: HW detected but not enumerated\n",
  11345. __func__);
  11346. pt_queue_enum(cd);
  11347. }
  11348. }
  11349. exit:
  11350. pt_start_wd_timer(cd);
  11351. }
  11352. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11353. /*******************************************************************************
  11354. * FUNCTION: pt_watchdog_timer
  11355. *
  11356. * SUMMARY: The function that is called when the WD timer expires. If the
  11357. * watchdog work is not already busy schedule the watchdog work queue.
  11358. *
  11359. * RETURN: void
  11360. *
  11361. * PARAMETERS:
  11362. * handle - Handle to the watchdog timer
  11363. ******************************************************************************/
  11364. static void pt_watchdog_timer(unsigned long handle)
  11365. {
  11366. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11367. if (!cd)
  11368. return;
  11369. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11370. __func__);
  11371. if (!work_pending(&cd->watchdog_work))
  11372. schedule_work(&cd->watchdog_work);
  11373. }
  11374. #else
  11375. /*******************************************************************************
  11376. * FUNCTION: pt_watchdog_timer
  11377. *
  11378. * SUMMARY: The function that is called when the WD timer expires. If the
  11379. * watchdog work is not already busy schedule the watchdog work queue.
  11380. *
  11381. * RETURN: void
  11382. *
  11383. * PARAMETERS:
  11384. * *t - Pointer to timer list
  11385. ******************************************************************************/
  11386. static void pt_watchdog_timer(struct timer_list *t)
  11387. {
  11388. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11389. if (!cd)
  11390. return;
  11391. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11392. __func__);
  11393. if (!work_pending(&cd->watchdog_work))
  11394. schedule_work(&cd->watchdog_work);
  11395. }
  11396. #endif
  11397. /*******************************************************************************
  11398. * Core sysfs show and store functions
  11399. ******************************************************************************/
  11400. /*******************************************************************************
  11401. * FUNCTION: pt_hw_version_show
  11402. *
  11403. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11404. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11405. *
  11406. * RETURN: size of data written to sysfs node
  11407. *
  11408. * PARAMETERS:
  11409. * *dev - pointer to device structure
  11410. * *attr - pointer to device attributes structure
  11411. * *buf - pointer to print output buffer
  11412. ******************************************************************************/
  11413. static ssize_t pt_hw_version_show(struct device *dev,
  11414. struct device_attribute *attr, char *buf)
  11415. {
  11416. struct pt_core_data *cd = dev_get_drvdata(dev);
  11417. _pt_request_hw_version(dev, cd->hw_version);
  11418. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11419. }
  11420. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11421. /*******************************************************************************
  11422. * FUNCTION: pt_drv_version_show
  11423. *
  11424. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11425. * TTDL version information
  11426. *
  11427. * RETURN: Char buffer with printed TTDL version information
  11428. *
  11429. * PARAMETERS:
  11430. * *dev - pointer to device structure
  11431. * *attr - pointer to device attributes
  11432. * *buf - pointer to output buffer
  11433. ******************************************************************************/
  11434. static ssize_t pt_drv_version_show(struct device *dev,
  11435. struct device_attribute *attr, char *buf)
  11436. {
  11437. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11438. "Driver: %s\nVersion: %s\nDate: %s\n",
  11439. pt_driver_core_name, pt_driver_core_version,
  11440. pt_driver_core_date);
  11441. }
  11442. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11443. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11444. /*******************************************************************************
  11445. * FUNCTION: pt_fw_version_show
  11446. *
  11447. * SUMMARY: Show method for the fw_version sysfs node that will
  11448. * show the firmware, bootloader and PIP version information
  11449. *
  11450. * RETURN: Size of printed buffer
  11451. *
  11452. * PARAMETERS:
  11453. * *dev - pointer to device structure
  11454. * *attr - pointer to device attributes
  11455. * *buf - pointer to output buffer
  11456. ******************************************************************************/
  11457. static ssize_t pt_fw_version_show(struct device *dev,
  11458. struct device_attribute *attr, char *buf)
  11459. {
  11460. struct pt_core_data *cd = dev_get_drvdata(dev);
  11461. struct pt_ttdata *ttdata;
  11462. int rc = 0;
  11463. if (cd->mode == PT_MODE_OPERATIONAL)
  11464. rc = pt_hid_output_get_sysinfo_(cd);
  11465. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11466. __func__, cd->mode, cd->sysinfo.ready);
  11467. if (cd->sysinfo.ready)
  11468. ttdata = &cd->sysinfo.ttdata;
  11469. else
  11470. rc = -ENODATA;
  11471. if (!rc) {
  11472. return scnprintf(buf, strlen(buf),
  11473. "Status: %d\n"
  11474. "FW : %d.%d.%d\n"
  11475. "Config: %d\n"
  11476. "BL : %d.%d\n"
  11477. "PIP : %d.%d\n",
  11478. rc,
  11479. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11480. ttdata->revctrl,
  11481. ttdata->fw_ver_conf,
  11482. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11483. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11484. } else {
  11485. return scnprintf(buf, strlen(buf),
  11486. "Status: %d\n"
  11487. "FW : n/a\n"
  11488. "Config: n/a\n"
  11489. "BL : n/a\n"
  11490. "PIP : n/a\n",
  11491. rc);
  11492. }
  11493. }
  11494. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11495. /*******************************************************************************
  11496. * FUNCTION: pt_sysinfo_show
  11497. *
  11498. * SUMMARY: Show method for the sysinfo sysfs node that will
  11499. * show all the information from get system information command.
  11500. *
  11501. * RETURN: Size of printed buffer
  11502. *
  11503. * PARAMETERS:
  11504. * *dev - pointer to device structure
  11505. * *attr - pointer to device attributes
  11506. * *buf - pointer to output buffer
  11507. ******************************************************************************/
  11508. static ssize_t pt_sysinfo_show(struct device *dev,
  11509. struct device_attribute *attr, char *buf)
  11510. {
  11511. struct pt_core_data *cd = dev_get_drvdata(dev);
  11512. struct pt_sysinfo *si;
  11513. struct pt_ttdata *ttdata = NULL;
  11514. struct pt_sensing_conf_data *scd = NULL;
  11515. int rc = 0;
  11516. if (cd->mode == PT_MODE_OPERATIONAL) {
  11517. rc = pt_hid_output_get_sysinfo_(cd);
  11518. if (cd->sysinfo.ready) {
  11519. si = &cd->sysinfo;
  11520. ttdata = &si->ttdata;
  11521. scd = &si->sensing_conf_data;
  11522. } else
  11523. rc = -ENODATA;
  11524. } else
  11525. rc = -EPERM;
  11526. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11527. __func__, cd->mode, cd->sysinfo.ready);
  11528. if (!rc && ttdata && scd) {
  11529. return scnprintf(buf, strlen(buf),
  11530. "Status: %d\n"
  11531. "pip_ver_major: 0x%02X\n"
  11532. "pip_ver_minor: 0x%02X\n"
  11533. "fw_pid : 0x%04X\n"
  11534. "fw_ver_major : 0x%02X\n"
  11535. "fw_ver_minor : 0x%02X\n"
  11536. "revctrl : 0x%08X\n"
  11537. "fw_ver_conf : 0x%04X\n"
  11538. "bl_ver_major : 0x%02X\n"
  11539. "bl_ver_minor : 0x%02X\n"
  11540. "jtag_id_h : 0x%04X\n"
  11541. "jtag_id_l : 0x%04X\n"
  11542. "mfg_id[0] : 0x%02X\n"
  11543. "mfg_id[1] : 0x%02X\n"
  11544. "mfg_id[2] : 0x%02X\n"
  11545. "mfg_id[3] : 0x%02X\n"
  11546. "mfg_id[4] : 0x%02X\n"
  11547. "mfg_id[5] : 0x%02X\n"
  11548. "mfg_id[6] : 0x%02X\n"
  11549. "mfg_id[7] : 0x%02X\n"
  11550. "post_code : 0x%04X\n"
  11551. "electrodes_x : 0x%02X\n"
  11552. "electrodes_y : 0x%02X\n"
  11553. "len_x : 0x%04X\n"
  11554. "len_y : 0x%04X\n"
  11555. "res_x : 0x%04X\n"
  11556. "res_y : 0x%04X\n"
  11557. "max_z : 0x%04X\n"
  11558. "origin_x : 0x%02X\n"
  11559. "origin_y : 0x%02X\n"
  11560. "panel_id : 0x%02X\n"
  11561. "btn : 0x%02X\n"
  11562. "scan_mode : 0x%02X\n"
  11563. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11564. rc,
  11565. ttdata->pip_ver_major,
  11566. ttdata->pip_ver_minor,
  11567. ttdata->fw_pid,
  11568. ttdata->fw_ver_major,
  11569. ttdata->fw_ver_minor,
  11570. ttdata->revctrl,
  11571. ttdata->fw_ver_conf,
  11572. ttdata->bl_ver_major,
  11573. ttdata->bl_ver_minor,
  11574. ttdata->jtag_id_h,
  11575. ttdata->jtag_id_l,
  11576. ttdata->mfg_id[0],
  11577. ttdata->mfg_id[1],
  11578. ttdata->mfg_id[2],
  11579. ttdata->mfg_id[3],
  11580. ttdata->mfg_id[4],
  11581. ttdata->mfg_id[5],
  11582. ttdata->mfg_id[6],
  11583. ttdata->mfg_id[7],
  11584. ttdata->post_code,
  11585. scd->electrodes_x,
  11586. scd->electrodes_y,
  11587. scd->len_x,
  11588. scd->len_y,
  11589. scd->res_x,
  11590. scd->res_y,
  11591. scd->max_z,
  11592. scd->origin_x,
  11593. scd->origin_y,
  11594. scd->panel_id,
  11595. scd->btn,
  11596. scd->scan_mode,
  11597. scd->max_tch);
  11598. } else {
  11599. return scnprintf(buf, strlen(buf),
  11600. "Status: %d\n",
  11601. rc);
  11602. }
  11603. }
  11604. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  11605. /*******************************************************************************
  11606. * FUNCTION: pt_hw_reset_show
  11607. *
  11608. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  11609. * by toggling the XRES line and then calls the startup function to
  11610. * allow TTDL to re-enumerate the DUT.
  11611. * The printed value reflects the status of the full reset/enum.
  11612. *
  11613. * PARAMETERS:
  11614. * *dev - pointer to Device structure
  11615. * *attr - pointer to the device attribute structure
  11616. * *buf - pointer to buffer to print
  11617. ******************************************************************************/
  11618. static ssize_t pt_hw_reset_show(struct device *dev,
  11619. struct device_attribute *attr, char *buf)
  11620. {
  11621. struct pt_core_data *cd = dev_get_drvdata(dev);
  11622. int rc = 0;
  11623. int time = 0;
  11624. u8 reset_status = 0;
  11625. int t;
  11626. struct pt_hid_desc hid_desc;
  11627. memset(&hid_desc, 0, sizeof(hid_desc));
  11628. /* Only allow DUT reset if no active BL in progress */
  11629. mutex_lock(&cd->firmware_class_lock);
  11630. mutex_lock(&cd->system_lock);
  11631. cd->startup_state = STARTUP_NONE;
  11632. mutex_unlock(&(cd->system_lock));
  11633. pt_stop_wd_timer(cd);
  11634. /* ensure no left over exclusive access is still locked */
  11635. release_exclusive(cd, cd->dev);
  11636. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  11637. if (rc) {
  11638. mutex_unlock(&cd->firmware_class_lock);
  11639. pt_debug(cd->dev, DL_ERROR,
  11640. "%s: HW reset failed rc = %d\n", __func__, rc);
  11641. goto exit_hw_reset;
  11642. }
  11643. reset_status |= 0x01 << 0;
  11644. if (cd->flashless_dut) {
  11645. mutex_unlock(&cd->firmware_class_lock);
  11646. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  11647. msecs_to_jiffies(200));
  11648. if (IS_TMO(t)) {
  11649. pt_debug(dev, DL_ERROR,
  11650. "%s: Timeout waiting for FW update",
  11651. __func__);
  11652. rc = -ETIME;
  11653. goto exit_hw_reset;
  11654. } else {
  11655. pt_debug(dev, DL_INFO,
  11656. "%s: ----- Wait FW Loading ----",
  11657. __func__);
  11658. rc = _pt_request_wait_for_enum_state(
  11659. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  11660. if (rc) {
  11661. pt_debug(cd->dev, DL_ERROR,
  11662. "%s: No FW Sentinel detected rc = %d\n",
  11663. __func__, rc);
  11664. goto exit_hw_reset;
  11665. }
  11666. reset_status |= 0x01 << 1;
  11667. }
  11668. } else {
  11669. /* Wait for any sentinel */
  11670. rc = _pt_request_wait_for_enum_state(dev, 150,
  11671. STARTUP_STATUS_BL_RESET_SENTINEL |
  11672. STARTUP_STATUS_FW_RESET_SENTINEL);
  11673. if (rc) {
  11674. mutex_unlock(&cd->firmware_class_lock);
  11675. pt_debug(cd->dev, DL_ERROR,
  11676. "%s: No Sentinel detected rc = %d\n",
  11677. __func__, rc);
  11678. goto exit_hw_reset;
  11679. }
  11680. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  11681. msleep(30);
  11682. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  11683. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  11684. if (rc < 0) {
  11685. pt_debug(cd->dev, DL_ERROR,
  11686. "%s: Error on getting HID descriptor r=%d\n",
  11687. __func__, rc);
  11688. goto exit_hw_reset;
  11689. }
  11690. cd->mode = pt_get_mode(cd, &hid_desc);
  11691. if (cd->mode == PT_MODE_BOOTLOADER)
  11692. rc = pt_hid_output_bl_launch_app_(cd);
  11693. } else {
  11694. if (cd->mode == PT_MODE_BOOTLOADER)
  11695. rc = pt_pip2_launch_app(dev,
  11696. PT_CORE_CMD_UNPROTECTED);
  11697. }
  11698. if (rc) {
  11699. mutex_unlock(&cd->firmware_class_lock);
  11700. pt_debug(cd->dev, DL_ERROR,
  11701. "%s: PIP launch app failed rc = %d\n",
  11702. __func__, rc);
  11703. goto exit_hw_reset;
  11704. }
  11705. mutex_unlock(&cd->firmware_class_lock);
  11706. reset_status |= 0x01 << 1;
  11707. msleep(20);
  11708. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  11709. (cd->mode != PT_MODE_OPERATIONAL))
  11710. pt_queue_restart(cd);
  11711. else
  11712. pt_queue_enum(cd);
  11713. }
  11714. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  11715. msleep(50);
  11716. pt_debug(cd->dev, DL_INFO,
  11717. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  11718. __func__, time, STARTUP_STATUS_COMPLETE,
  11719. cd->startup_status);
  11720. time += 50;
  11721. }
  11722. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  11723. rc = -ETIME;
  11724. goto exit_hw_reset;
  11725. }
  11726. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  11727. __func__, cd->startup_status);
  11728. reset_status |= 0x01 << 2;
  11729. pt_start_wd_timer(cd);
  11730. exit_hw_reset:
  11731. return scnprintf(buf, strlen(buf),
  11732. "Status: %d\n"
  11733. "Reset Status: 0x%02X\n", rc, reset_status);
  11734. }
  11735. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  11736. /*******************************************************************************
  11737. * FUNCTION: pt_pip2_cmd_rsp_store
  11738. *
  11739. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  11740. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  11741. * Command byte order:
  11742. * Byte [0] - PIP2 command ID
  11743. * Byte [1-n] - PIP2 command payload
  11744. *
  11745. * RETURN: Size of passed in buffer
  11746. *
  11747. * PARAMETERS:
  11748. * *dev - pointer to device structure
  11749. * *attr - pointer to device attributes
  11750. * *buf - pointer to buffer that hold the command parameters
  11751. * size - size of buf
  11752. ******************************************************************************/
  11753. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  11754. struct device_attribute *attr, const char *buf, size_t size)
  11755. {
  11756. struct pt_core_data *cd = dev_get_drvdata(dev);
  11757. u16 actual_read_len;
  11758. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11759. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  11760. u8 pip2_cmd_id = 0x00;
  11761. u8 *pip2_cmd_data = NULL;
  11762. int data_len = 0;
  11763. int length;
  11764. int rc = 0;
  11765. /* clear shared data */
  11766. mutex_lock(&cd->sysfs_lock);
  11767. cd->raw_cmd_status = 0;
  11768. cd->cmd_rsp_buf_len = 0;
  11769. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11770. mutex_unlock(&cd->sysfs_lock);
  11771. length = _pt_ic_parse_input_hex(dev, buf, size,
  11772. input_data, PT_MAX_PIP2_MSG_SIZE);
  11773. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11774. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11775. __func__);
  11776. rc = -EINVAL;
  11777. goto exit;
  11778. }
  11779. /* Send PIP2 command if enough data was provided */
  11780. if (length >= 1) {
  11781. pip2_cmd_id = input_data[0];
  11782. pip2_cmd_data = &input_data[1];
  11783. data_len = length - 1;
  11784. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  11785. pip2_cmd_id, pip2_cmd_data, data_len,
  11786. read_buf, &actual_read_len);
  11787. cd->raw_cmd_status = rc;
  11788. if (rc) {
  11789. pt_debug(dev, DL_ERROR,
  11790. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  11791. __func__, pip2_cmd_id, rc);
  11792. goto exit;
  11793. } else {
  11794. cd->cmd_rsp_buf_len = actual_read_len;
  11795. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  11796. pt_debug(dev, DL_ERROR,
  11797. "%s: PIP2 actual_read_len = %d\n",
  11798. __func__, actual_read_len);
  11799. }
  11800. } else {
  11801. rc = -EINVAL;
  11802. pt_debug(dev, DL_ERROR,
  11803. "%s: Insufficient data provided for PIP2 cmd\n",
  11804. __func__);
  11805. }
  11806. exit:
  11807. if (rc)
  11808. return rc;
  11809. return size;
  11810. }
  11811. /*******************************************************************************
  11812. * FUNCTION: pt_pip2_cmd_rsp_show
  11813. *
  11814. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  11815. * response generated after using the store method of the pip2_cmd_rsp
  11816. * sysfs node, are available to be read here.
  11817. *
  11818. * PARAMETERS:
  11819. * *dev - pointer to Device structure
  11820. * *attr - pointer to the device attribute structure
  11821. * *buf - pointer to buffer to print
  11822. ******************************************************************************/
  11823. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  11824. struct device_attribute *attr, char *buf)
  11825. {
  11826. struct pt_core_data *cd = dev_get_drvdata(dev);
  11827. int i;
  11828. ssize_t data_len;
  11829. int index;
  11830. mutex_lock(&cd->sysfs_lock);
  11831. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11832. "Status: %d\n", cd->raw_cmd_status);
  11833. if (cd->raw_cmd_status)
  11834. goto error;
  11835. /* Remove the CRC from the length of the response */
  11836. data_len = cd->cmd_rsp_buf_len - 2;
  11837. /* Start printing from the data payload */
  11838. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  11839. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11840. "%02X ", cd->cmd_rsp_buf[i]);
  11841. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  11842. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11843. "\n(%zd bytes)\n",
  11844. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  11845. } else {
  11846. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11847. "\n(%zd bytes)\n", 0);
  11848. }
  11849. error:
  11850. mutex_unlock(&cd->sysfs_lock);
  11851. return index;
  11852. }
  11853. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  11854. pt_pip2_cmd_rsp_store);
  11855. /*******************************************************************************
  11856. * FUNCTION: pt_command_store
  11857. *
  11858. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  11859. * raw PIP command echo'd to this node will be sent directly to the DUT.
  11860. * TTDL will not parse the command.
  11861. *
  11862. * RETURN: Size of passed in buffer
  11863. *
  11864. * PARAMETERS:
  11865. * *dev - pointer to device structure
  11866. * *attr - pointer to device attributes
  11867. * *buf - pointer to buffer that hold the command parameters
  11868. * size - size of buf
  11869. ******************************************************************************/
  11870. static ssize_t pt_command_store(struct device *dev,
  11871. struct device_attribute *attr, const char *buf, size_t size)
  11872. {
  11873. struct pt_core_data *cd = dev_get_drvdata(dev);
  11874. unsigned short crc;
  11875. u16 actual_read_len;
  11876. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  11877. int length;
  11878. int len_field;
  11879. int rc = 0;
  11880. mutex_lock(&cd->sysfs_lock);
  11881. cd->cmd_rsp_buf_len = 0;
  11882. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  11883. mutex_unlock(&cd->sysfs_lock);
  11884. length = _pt_ic_parse_input_hex(dev, buf, size,
  11885. input_data, PT_MAX_PIP2_MSG_SIZE);
  11886. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  11887. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  11888. __func__);
  11889. rc = -EINVAL;
  11890. goto pt_command_store_exit;
  11891. }
  11892. /* PIP2 messages begin with 01 01 */
  11893. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  11894. cd->pip2_prot_active = 1;
  11895. /* Override next seq tag with what was sent */
  11896. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  11897. /* For PIP2 cmd if length does not include crc, add it */
  11898. len_field = (input_data[3] << 8) | input_data[2];
  11899. if (len_field == length && length <= 254) {
  11900. crc = crc_ccitt_calculate(&input_data[2],
  11901. length - 2);
  11902. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  11903. __func__, length, crc);
  11904. input_data[length] = (crc & 0xFF00) >> 8;
  11905. input_data[length + 1] = crc & 0x00FF;
  11906. length = length + 2;
  11907. }
  11908. }
  11909. /* write PIP command to log */
  11910. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  11911. pm_runtime_get_sync(dev);
  11912. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  11913. length, input_data, &actual_read_len);
  11914. pm_runtime_put(dev);
  11915. mutex_lock(&cd->sysfs_lock);
  11916. cd->raw_cmd_status = rc;
  11917. if (rc) {
  11918. cd->cmd_rsp_buf_len = 0;
  11919. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  11920. __func__, buf);
  11921. } else {
  11922. cd->cmd_rsp_buf_len = actual_read_len;
  11923. }
  11924. cd->pip2_prot_active = 0;
  11925. mutex_unlock(&cd->sysfs_lock);
  11926. pt_command_store_exit:
  11927. if (rc)
  11928. return rc;
  11929. return size;
  11930. }
  11931. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  11932. /*******************************************************************************
  11933. * FUNCTION: pt_response_show
  11934. *
  11935. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  11936. * response generated after using the pt_command_store sysfs node, are
  11937. * available to be read here.
  11938. *
  11939. * PARAMETERS:
  11940. * *dev - pointer to Device structure
  11941. * *attr - pointer to the device attribute structure
  11942. * *buf - pointer to buffer to print
  11943. ******************************************************************************/
  11944. static ssize_t pt_response_show(struct device *dev,
  11945. struct device_attribute *attr, char *buf)
  11946. {
  11947. struct pt_core_data *cd = dev_get_drvdata(dev);
  11948. int i;
  11949. ssize_t num_read;
  11950. int index;
  11951. mutex_lock(&cd->sysfs_lock);
  11952. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11953. "Status: %d\n", cd->raw_cmd_status);
  11954. if (cd->raw_cmd_status)
  11955. goto error;
  11956. num_read = cd->cmd_rsp_buf_len;
  11957. for (i = 0; i < num_read; i++)
  11958. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11959. "0x%02X\n", cd->cmd_rsp_buf[i]);
  11960. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  11961. "(%zd bytes)\n", num_read);
  11962. error:
  11963. mutex_unlock(&cd->sysfs_lock);
  11964. return index;
  11965. }
  11966. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  11967. /*******************************************************************************
  11968. * FUNCTION: pt_dut_debug_show
  11969. *
  11970. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  11971. * are available for the store method.
  11972. *
  11973. * RETURN:
  11974. * 0 = success
  11975. * !0 = failure
  11976. *
  11977. * PARAMETERS:
  11978. * *dev - pointer to device structure
  11979. * *attr - pointer to device attributes
  11980. * *buf - pointer to output buffer
  11981. ******************************************************************************/
  11982. static ssize_t pt_dut_debug_show(struct device *dev,
  11983. struct device_attribute *attr, char *buf)
  11984. {
  11985. ssize_t ret;
  11986. ret = scnprintf(buf, strlen(buf),
  11987. "Status: 0\n"
  11988. "dut_debug sends the following commands to the DUT:\n"
  11989. "%d %s \t- %s\n"
  11990. "%d %s \t- %s\n"
  11991. "%d %s \t- %s\n"
  11992. "%d %s \t- %s\n"
  11993. "%d %s \t- %s\n"
  11994. "%d %s \t- %s\n"
  11995. "%d %s \t- %s\n"
  11996. "%d %s \t- %s\n"
  11997. "%d %s \t- %s\n"
  11998. "%d %s \t- %s\n"
  11999. "%d %s \t- %s\n"
  12000. "%d %s \t- %s\n"
  12001. "%d %s \t- %s\n"
  12002. "%d %s \t- %s\n"
  12003. "%d %s \t- %s\n"
  12004. "%d %s \t- %s\n"
  12005. "%d %s \t- %s\n"
  12006. ,
  12007. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12008. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12009. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12010. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12011. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12012. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12013. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12014. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12015. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12016. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12017. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12018. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12019. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12020. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12021. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12022. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12023. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12024. );
  12025. return ret;
  12026. }
  12027. /*******************************************************************************
  12028. * FUNCTION: pt_drv_debug_show
  12029. *
  12030. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12031. * are available for the store method.
  12032. *
  12033. * RETURN:
  12034. * 0 = success
  12035. * !0 = failure
  12036. *
  12037. * PARAMETERS:
  12038. * *dev - pointer to device structure
  12039. * *attr - pointer to device attributes
  12040. * *buf - pointer to output buffer
  12041. ******************************************************************************/
  12042. static ssize_t pt_drv_debug_show(struct device *dev,
  12043. struct device_attribute *attr, char *buf)
  12044. {
  12045. ssize_t ret;
  12046. ret = scnprintf(buf, strlen(buf),
  12047. "Status: 0\n"
  12048. "drv_debug supports the following values:\n"
  12049. "%d %s \t- %s\n"
  12050. "%d %s \t- %s\n"
  12051. "%d %s \t- %s\n"
  12052. "%d %s \t- %s\n"
  12053. "%d %s \t- %s\n"
  12054. "%d %s \t- %s\n"
  12055. "%d %s \t- %s\n"
  12056. "%d %s \t- %s\n"
  12057. "%d %s \t- %s\n"
  12058. "%d %s - %s\n"
  12059. "%d %s \t- %s\n"
  12060. "%d %s \t- %s\n"
  12061. #ifdef TTDL_DIAGNOSTICS
  12062. "%d %s \t- %s\n"
  12063. "%d %s \t- %s\n"
  12064. "%d %s \t- %s\n"
  12065. #endif /* TTDL_DIAGNOSTICS */
  12066. "%d %s \t- %s\n"
  12067. #ifdef TTDL_DIAGNOSTICS
  12068. "%d %s \t- %s\n"
  12069. "%d %s \t- %s\n"
  12070. "%d %s \t- %s\n"
  12071. "%d %s \t- %s\n"
  12072. "%d %s \t- %s\n"
  12073. "%d %s \t- %s\n"
  12074. "%d %s \t- %s\n"
  12075. "%d %s \t- %s\n"
  12076. "%d %s \t- %s\n"
  12077. #endif /* TTDL_DIAGNOSTICS */
  12078. ,
  12079. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12080. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12081. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12082. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12083. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12084. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12085. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12086. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12087. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12088. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12089. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12090. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12091. #ifdef TTDL_DIAGNOSTICS
  12092. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12093. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12094. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12095. #endif /* TTDL_DIAGNOSTICS */
  12096. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12097. #ifdef TTDL_DIAGNOSTICS
  12098. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12099. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12100. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12101. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12102. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12103. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12104. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12105. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12106. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12107. "TTHE_TUNER HID USB Format"
  12108. #endif /* TTDL_DIAGNOSTICS */
  12109. );
  12110. return ret;
  12111. }
  12112. /*******************************************************************************
  12113. * FUNCTION: pt_drv_debug_store
  12114. *
  12115. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12116. * dut_debug. Drv_debug will contain all functionality that can be run
  12117. * without a DUT preset and is available anytime TTDL is running.
  12118. * Dut_debug requires a DUT to be available and will only be created after
  12119. * a DUT has been detected.
  12120. * This function will eventually be split into two but until the overlap
  12121. * has been depricated this function contains all commands that can be
  12122. * used for TTDL/DUT debugging status and control.
  12123. * All commands require at least one value to be passed in *buf with some
  12124. * requiring two.
  12125. *
  12126. * RETURN: Size of passed in buffer
  12127. *
  12128. * PARAMETERS:
  12129. * *dev - pointer to device structure
  12130. * *attr - pointer to device attributes
  12131. * *buf - pointer to buffer that hold the command parameters
  12132. * size - size of buf
  12133. ******************************************************************************/
  12134. static ssize_t pt_drv_debug_store(struct device *dev,
  12135. struct device_attribute *attr, const char *buf, size_t size)
  12136. {
  12137. struct pt_core_data *cd = dev_get_drvdata(dev);
  12138. unsigned long value;
  12139. int rc = 0;
  12140. u8 return_data[8];
  12141. static u8 wd_disabled;
  12142. u32 input_data[3];
  12143. int length;
  12144. #ifdef TTDL_DIAGNOSTICS
  12145. struct i2c_client *client = to_i2c_client(dev);
  12146. unsigned short crc = 0;
  12147. u16 cal_size;
  12148. #endif
  12149. input_data[0] = 0;
  12150. input_data[1] = 0;
  12151. /* Maximmum input is two elements */
  12152. length = _pt_ic_parse_input(dev, buf, size,
  12153. input_data, ARRAY_SIZE(input_data));
  12154. if (length < 1 || length > 2) {
  12155. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12156. __func__);
  12157. rc = -EINVAL;
  12158. goto pt_drv_debug_store_exit;
  12159. }
  12160. value = input_data[0];
  12161. if (length == 1) {
  12162. pt_debug(dev, DL_DEBUG,
  12163. "%s: Debug Cmd Received (id=%d)\n",
  12164. __func__, input_data[0]);
  12165. } else if (length == 2) {
  12166. pt_debug(dev, DL_DEBUG,
  12167. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12168. __func__, input_data[0], input_data[1]);
  12169. } else {
  12170. pt_debug(dev, DL_DEBUG,
  12171. "%s: Invalid arguments received\n", __func__);
  12172. rc = -EINVAL;
  12173. goto pt_drv_debug_store_exit;
  12174. }
  12175. /* Start watchdog timer command */
  12176. if (value == PT_DRV_DBG_START_WD) {
  12177. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12178. wd_disabled = 0;
  12179. cd->watchdog_force_stop = false;
  12180. pt_start_wd_timer(cd);
  12181. goto pt_drv_debug_store_exit;
  12182. }
  12183. /* Stop watchdog timer temporarily */
  12184. pt_stop_wd_timer(cd);
  12185. if (value == PT_DRV_DBG_STOP_WD) {
  12186. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12187. wd_disabled = 1;
  12188. cd->watchdog_force_stop = true;
  12189. goto pt_drv_debug_store_exit;
  12190. }
  12191. switch (value) {
  12192. case PT_DRV_DBG_SUSPEND: /* 4 */
  12193. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12194. wd_disabled = 1;
  12195. rc = pt_core_sleep(cd);
  12196. if (rc)
  12197. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12198. __func__, rc);
  12199. else
  12200. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12201. __func__);
  12202. break;
  12203. case PT_DRV_DBG_RESUME: /* 5 */
  12204. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12205. rc = pt_core_wake(cd);
  12206. if (rc)
  12207. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12208. __func__, rc);
  12209. else
  12210. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12211. __func__);
  12212. break;
  12213. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12214. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12215. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12216. break;
  12217. case PT_DUT_DBG_HID_RESET: /* 50 */
  12218. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12219. pt_hid_cmd_reset(cd);
  12220. break;
  12221. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12222. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12223. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12224. wd_disabled = 0;
  12225. break;
  12226. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12227. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12228. __func__);
  12229. wd_disabled = 1;
  12230. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12231. break;
  12232. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12233. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12234. __func__);
  12235. wd_disabled = 1;
  12236. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12237. break;
  12238. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12239. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12240. pt_hid_output_bl_get_information(cd, return_data);
  12241. break;
  12242. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12243. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12244. __func__);
  12245. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12246. break;
  12247. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12248. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12249. pt_hid_output_bl_launch_app(cd);
  12250. break;
  12251. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12252. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12253. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12254. break;
  12255. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12256. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12257. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12258. break;
  12259. case PT_DUT_DBG_RESET: /* 98 */
  12260. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12261. rc = pt_hw_hard_reset(cd);
  12262. break;
  12263. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12264. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12265. pt_pip_null(cd);
  12266. break;
  12267. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12268. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12269. rc = pt_pip_start_bootloader(cd);
  12270. if (!rc) {
  12271. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12272. cd->mode = PT_MODE_BOOTLOADER;
  12273. }
  12274. break;
  12275. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12276. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12277. pt_hid_output_get_sysinfo(cd);
  12278. break;
  12279. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12280. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12281. pt_pip_suspend_scanning(cd);
  12282. break;
  12283. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12284. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12285. pt_pip_resume_scanning(cd);
  12286. break;
  12287. #ifdef TTHE_TUNER_SUPPORT
  12288. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12289. cd->tthe_exit = 1;
  12290. wake_up(&cd->wait_q);
  12291. kfree(cd->tthe_buf);
  12292. cd->tthe_buf = NULL;
  12293. cd->tthe_exit = 0;
  12294. break;
  12295. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12296. if (cd->tthe_buf)
  12297. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12298. else
  12299. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12300. __func__);
  12301. break;
  12302. #endif
  12303. #ifdef TTDL_DIAGNOSTICS
  12304. case PT_DUT_DBG_HID_DESC: /* 109 */
  12305. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12306. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12307. break;
  12308. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12309. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12310. __func__);
  12311. pt_erase_parameter_list(cd);
  12312. break;
  12313. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12314. rc = pt_read_input(cd);
  12315. if (!rc)
  12316. pt_parse_input(cd);
  12317. break;
  12318. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12319. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12320. &cal_size, &crc);
  12321. if (rc)
  12322. pt_debug(dev, DL_ERROR,
  12323. "%s: CAL Data clear failed rc=%d\n",
  12324. __func__, rc);
  12325. else
  12326. pt_debug(dev, DL_INFO,
  12327. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12328. __func__, crc, size);
  12329. break;
  12330. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12331. mutex_lock(&cd->system_lock);
  12332. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12333. cd->debug_level = input_data[1];
  12334. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12335. __func__, cd->debug_level);
  12336. } else {
  12337. rc = -EINVAL;
  12338. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12339. __func__, input_data[1]);
  12340. }
  12341. mutex_unlock(&(cd->system_lock));
  12342. break;
  12343. #endif
  12344. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12345. mutex_lock(&cd->system_lock);
  12346. if (input_data[1] > 100) {
  12347. cd->watchdog_interval = input_data[1];
  12348. pt_debug(dev, DL_INFO,
  12349. "%s: Set watchdog_ interval to: %d\n",
  12350. __func__, cd->watchdog_interval);
  12351. pt_start_wd_timer(cd);
  12352. } else {
  12353. rc = -EINVAL;
  12354. pt_debug(dev, DL_ERROR,
  12355. "%s: Invalid watchdog interval: %d\n",
  12356. __func__, input_data[1]);
  12357. }
  12358. mutex_unlock(&(cd->system_lock));
  12359. break;
  12360. #ifdef TTDL_DIAGNOSTICS
  12361. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12362. mutex_lock(&cd->system_lock);
  12363. if (input_data[1] == 0) {
  12364. cd->show_timestamp = 0;
  12365. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12366. __func__);
  12367. } else if (input_data[1] == 1) {
  12368. cd->show_timestamp = 1;
  12369. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12370. __func__);
  12371. } else {
  12372. rc = -EINVAL;
  12373. pt_debug(dev, DL_ERROR,
  12374. "%s: Invalid parameter: %d\n",
  12375. __func__, input_data[1]);
  12376. }
  12377. mutex_unlock(&(cd->system_lock));
  12378. break;
  12379. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12380. if (input_data[1] == 0) {
  12381. cd->cpdata->setup_power(cd->cpdata,
  12382. PT_MT_POWER_OFF, cd->dev);
  12383. pt_debug(dev, DL_INFO,
  12384. "%s: Initiate Power Off\n", __func__);
  12385. } else if (input_data[1] == 1) {
  12386. cd->cpdata->setup_power(cd->cpdata,
  12387. PT_MT_POWER_ON, cd->dev);
  12388. pt_debug(dev, DL_INFO,
  12389. "%s: Initiate Power On\n", __func__);
  12390. } else {
  12391. rc = -EINVAL;
  12392. pt_debug(dev, DL_ERROR,
  12393. "%s: Invalid parameter: %d\n",
  12394. __func__, input_data[1]);
  12395. }
  12396. break;
  12397. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12398. if (input_data[1] == 0) {
  12399. pm_runtime_put(dev);
  12400. pt_debug(dev, DL_ERROR,
  12401. "%s: Force call pm_runtime_put()\n", __func__);
  12402. } else if (input_data[1] == 1) {
  12403. pm_runtime_get_sync(dev);
  12404. pt_debug(dev, DL_ERROR,
  12405. "%s: Force call pm_runtime_get_sync()\n",
  12406. __func__);
  12407. } else {
  12408. rc = -EINVAL;
  12409. pt_debug(dev, DL_ERROR,
  12410. "%s: WARNING: Invalid parameter: %d\n",
  12411. __func__, input_data[1]);
  12412. }
  12413. break;
  12414. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12415. mutex_lock(&cd->system_lock);
  12416. if (input_data[1] == 0) {
  12417. cd->show_tt_data = false;
  12418. pt_debug(dev, DL_INFO,
  12419. "%s: Disable TT_DATA\n", __func__);
  12420. } else if (input_data[1] == 1) {
  12421. cd->show_tt_data = true;
  12422. pt_debug(dev, DL_INFO,
  12423. "%s: Enable TT_DATA\n", __func__);
  12424. } else {
  12425. rc = -EINVAL;
  12426. pt_debug(dev, DL_ERROR,
  12427. "%s: Invalid parameter: %d\n",
  12428. __func__, input_data[1]);
  12429. }
  12430. mutex_unlock(&(cd->system_lock));
  12431. break;
  12432. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12433. if (input_data[1] == cd->active_dut_generation) {
  12434. mutex_lock(&cd->system_lock);
  12435. cd->set_dut_generation = true;
  12436. mutex_unlock(&(cd->system_lock));
  12437. } else {
  12438. mutex_lock(&cd->system_lock);
  12439. if (input_data[1] == 0) {
  12440. cd->active_dut_generation = DUT_UNKNOWN;
  12441. cd->set_dut_generation = false;
  12442. } else if (input_data[1] == 1) {
  12443. cd->active_dut_generation = DUT_PIP1_ONLY;
  12444. cd->set_dut_generation = true;
  12445. } else if (input_data[1] == 2) {
  12446. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12447. cd->set_dut_generation = true;
  12448. } else {
  12449. rc = -EINVAL;
  12450. pt_debug(dev, DL_ERROR,
  12451. "%s: Invalid parameter: %d\n",
  12452. __func__, input_data[1]);
  12453. break;
  12454. }
  12455. cd->startup_status = STARTUP_STATUS_START;
  12456. pt_debug(cd->dev, DL_DEBUG,
  12457. "%s: Startup Status Reset\n", __func__);
  12458. mutex_unlock(&(cd->system_lock));
  12459. pt_debug(dev, DL_INFO,
  12460. "%s: Active DUT Generation Set to: %d\n",
  12461. __func__, cd->active_dut_generation);
  12462. /* Changing DUT generations full restart needed */
  12463. pt_queue_restart(cd);
  12464. }
  12465. break;
  12466. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12467. mutex_lock(&cd->system_lock);
  12468. if (input_data[1] == 0) {
  12469. cd->bridge_mode = false;
  12470. pt_debug(dev, DL_INFO,
  12471. "%s: Disable Bridge Mode\n", __func__);
  12472. } else if (input_data[1] == 1) {
  12473. cd->bridge_mode = true;
  12474. pt_debug(dev, DL_INFO,
  12475. "%s: Enable Bridge Mode\n", __func__);
  12476. } else {
  12477. rc = -EINVAL;
  12478. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12479. __func__, input_data[1]);
  12480. }
  12481. mutex_unlock(&(cd->system_lock));
  12482. break;
  12483. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12484. mutex_lock(&cd->system_lock);
  12485. /* Only a 7bit address is valid */
  12486. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12487. client->addr = input_data[1];
  12488. pt_debug(dev, DL_INFO,
  12489. "%s: Set I2C Address: 0x%2X\n",
  12490. __func__, client->addr);
  12491. } else {
  12492. rc = -EINVAL;
  12493. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12494. __func__, input_data[1]);
  12495. client->addr = 0x24;
  12496. }
  12497. mutex_unlock(&(cd->system_lock));
  12498. break;
  12499. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12500. mutex_lock(&cd->system_lock);
  12501. if (input_data[1] == 0) {
  12502. cd->flashless_dut = 0;
  12503. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12504. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12505. __func__);
  12506. } else if (input_data[1] == 1) {
  12507. cd->flashless_dut = 1;
  12508. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12509. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12510. __func__);
  12511. }
  12512. mutex_unlock(&(cd->system_lock));
  12513. break;
  12514. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12515. mutex_lock(&cd->system_lock);
  12516. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12517. cd->force_pip2_seq = input_data[1];
  12518. cd->pip2_cmd_tag_seq = input_data[1];
  12519. pt_debug(dev, DL_INFO,
  12520. "%s: Force PIP2 Seq to: 0x%02X\n",
  12521. __func__, input_data[1]);
  12522. } else {
  12523. cd->force_pip2_seq = 0;
  12524. pt_debug(dev, DL_INFO,
  12525. "%s: Clear Forced PIP2 Seq\n", __func__);
  12526. }
  12527. mutex_unlock(&(cd->system_lock));
  12528. break;
  12529. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12530. mutex_lock(&cd->system_lock);
  12531. if (input_data[1] == 0) {
  12532. cd->bl_with_no_int = 0;
  12533. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12534. } else if (input_data[1] == 1) {
  12535. cd->bl_with_no_int = 1;
  12536. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12537. __func__);
  12538. } else {
  12539. rc = -EINVAL;
  12540. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12541. __func__, input_data[1]);
  12542. }
  12543. mutex_unlock(&(cd->system_lock));
  12544. break;
  12545. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12546. mutex_lock(&cd->system_lock);
  12547. if (input_data[1] == 0) {
  12548. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12549. pt_debug(dev, DL_INFO,
  12550. "%s: Disable Calibration cache in host\n",
  12551. __func__);
  12552. } else if (input_data[1] == 1) {
  12553. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12554. pt_debug(dev, DL_INFO,
  12555. "%s: Enable Calibration cache in host\n",
  12556. __func__);
  12557. } else {
  12558. rc = -EINVAL;
  12559. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12560. __func__, input_data[1]);
  12561. }
  12562. mutex_unlock(&(cd->system_lock));
  12563. break;
  12564. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12565. mutex_lock(&cd->system_lock);
  12566. if (input_data[1] == 0) {
  12567. cd->multi_chip = PT_FEATURE_DISABLE;
  12568. cd->ttdl_bist_select = 0x07;
  12569. pt_debug(dev, DL_INFO,
  12570. "%s: Disable Multi-chip support\n", __func__);
  12571. } else if (input_data[1] == 1) {
  12572. cd->multi_chip = PT_FEATURE_ENABLE;
  12573. cd->ttdl_bist_select = 0x3F;
  12574. pt_debug(dev, DL_INFO,
  12575. "%s: Enable Multi-chip support\n", __func__);
  12576. } else {
  12577. rc = -EINVAL;
  12578. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12579. __func__, input_data[1]);
  12580. }
  12581. mutex_unlock(&(cd->system_lock));
  12582. break;
  12583. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  12584. mutex_lock(&cd->system_lock);
  12585. if (input_data[1] <= 0x07) {
  12586. cd->panel_id_support = input_data[1];
  12587. pt_debug(dev, DL_INFO,
  12588. "%s: Set panel_id_support to %d\n",
  12589. __func__, cd->panel_id_support);
  12590. } else {
  12591. rc = -EINVAL;
  12592. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12593. __func__, input_data[1]);
  12594. }
  12595. mutex_unlock(&(cd->system_lock));
  12596. break;
  12597. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  12598. mutex_lock(&cd->system_lock);
  12599. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  12600. /*
  12601. * The timeout is changed for some cases so the
  12602. * pip_cmd_timeout_default is used to retore back to
  12603. * what the user requested as the new timeout.
  12604. */
  12605. cd->pip_cmd_timeout_default = input_data[1];
  12606. cd->pip_cmd_timeout = input_data[1];
  12607. pt_debug(dev, DL_INFO,
  12608. "%s: PIP Timeout = %d\n", __func__,
  12609. cd->pip_cmd_timeout_default);
  12610. } else {
  12611. rc = -EINVAL;
  12612. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12613. __func__, input_data[1]);
  12614. }
  12615. mutex_unlock(&(cd->system_lock));
  12616. break;
  12617. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  12618. mutex_lock(&cd->system_lock);
  12619. if (input_data[1] == 0) {
  12620. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  12621. pt_debug(dev, DL_INFO,
  12622. "%s: Disable tthe_tuner HID-USB format\n",
  12623. __func__);
  12624. } else if (input_data[1] == 1) {
  12625. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  12626. pt_debug(dev, DL_INFO,
  12627. "%s: Enable tthe_tuner HID-USB format\n",
  12628. __func__);
  12629. } else {
  12630. rc = -EINVAL;
  12631. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12632. __func__, input_data[1]);
  12633. }
  12634. mutex_unlock(&(cd->system_lock));
  12635. break;
  12636. #endif /* TTDL_DIAGNOSTICS */
  12637. default:
  12638. rc = -EINVAL;
  12639. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  12640. }
  12641. /* Enable watchdog timer */
  12642. if (!wd_disabled)
  12643. pt_start_wd_timer(cd);
  12644. pt_drv_debug_store_exit:
  12645. if (rc)
  12646. return rc;
  12647. return size;
  12648. }
  12649. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  12650. pt_drv_debug_store);
  12651. /*******************************************************************************
  12652. * FUNCTION: pt_sleep_status_show
  12653. *
  12654. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  12655. * sleep status as either ON or OFF
  12656. *
  12657. * RETURN:
  12658. * 0 = success
  12659. * !0 = failure
  12660. *
  12661. * PARAMETERS:
  12662. * *dev - pointer to device structure
  12663. * *attr - pointer to device attributes
  12664. * *buf - pointer to output buffer
  12665. ******************************************************************************/
  12666. static ssize_t pt_sleep_status_show(struct device *dev,
  12667. struct device_attribute *attr, char *buf)
  12668. {
  12669. struct pt_core_data *cd = dev_get_drvdata(dev);
  12670. ssize_t ret;
  12671. mutex_lock(&cd->system_lock);
  12672. if (cd->sleep_state == SS_SLEEP_ON)
  12673. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  12674. else
  12675. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  12676. mutex_unlock(&cd->system_lock);
  12677. return ret;
  12678. }
  12679. /*******************************************************************************
  12680. * FUNCTION: pt_panel_id_show
  12681. *
  12682. * SUMMARY: Show method for the panel_id sysfs node that will show the
  12683. * detected panel ID from the DUT
  12684. *
  12685. * RETURN:
  12686. * 0 = success
  12687. * !0 = failure
  12688. *
  12689. * PARAMETERS:
  12690. * *dev - pointer to device structure
  12691. * *attr - pointer to device attributes
  12692. * *buf - pointer to output buffer
  12693. ******************************************************************************/
  12694. static ssize_t pt_panel_id_show(struct device *dev,
  12695. struct device_attribute *attr, char *buf)
  12696. {
  12697. struct pt_core_data *cd = dev_get_drvdata(dev);
  12698. ssize_t ret;
  12699. u8 pid = PANEL_ID_NOT_ENABLED;
  12700. int rc = 0;
  12701. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12702. /*
  12703. * The DUT should report the same panel ID from both the BL and
  12704. * the FW unless the panel_id feature is set to only
  12705. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  12706. * to retrieve the panel_id.
  12707. */
  12708. if (cd->mode == PT_MODE_BOOTLOADER) {
  12709. /*
  12710. * Return the stored value if PT_PANEL_ID_BY_BL
  12711. * is not supported while other feature exits.
  12712. */
  12713. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12714. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  12715. if (rc) {
  12716. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  12717. "Failed to retrieve Panel ID. ",
  12718. "Using cached value\n",
  12719. __func__);
  12720. }
  12721. }
  12722. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12723. if (cd->panel_id_support &
  12724. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12725. /* For all systems sysinfo has the panel_id */
  12726. rc = pt_hid_output_get_sysinfo(cd);
  12727. if (!rc)
  12728. pid =
  12729. cd->sysinfo.sensing_conf_data.panel_id;
  12730. pt_debug(dev, DL_ERROR,
  12731. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  12732. __func__, rc, pid);
  12733. }
  12734. } else {
  12735. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12736. __func__);
  12737. rc = -EPERM;
  12738. }
  12739. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  12740. if (cd->mode == PT_MODE_BOOTLOADER) {
  12741. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  12742. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  12743. if (rc) {
  12744. pt_debug(dev, DL_ERROR,
  12745. "%s: BL get panel ID failed rc=%d\n",
  12746. __func__, rc);
  12747. }
  12748. }
  12749. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  12750. if (cd->panel_id_support &
  12751. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  12752. rc = pt_hid_output_get_sysinfo(cd);
  12753. if (!rc)
  12754. pid =
  12755. cd->sysinfo.sensing_conf_data.panel_id;
  12756. pt_debug(dev, DL_ERROR,
  12757. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  12758. __func__, rc, pid);
  12759. }
  12760. } else {
  12761. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  12762. __func__);
  12763. rc = -EPERM;
  12764. }
  12765. } else {
  12766. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  12767. __func__);
  12768. rc = -EPERM;
  12769. }
  12770. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  12771. rc, pid);
  12772. return ret;
  12773. }
  12774. /*******************************************************************************
  12775. * FUNCTION: pt_get_param_store
  12776. *
  12777. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  12778. * ID to retrieve with the show method.
  12779. *
  12780. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12781. *
  12782. * RETURN: Size of passed in buffer if successful
  12783. *
  12784. * PARAMETERS:
  12785. * *dev - pointer to device structure
  12786. * *attr - pointer to device attributes
  12787. * *buf - pointer to output buffer
  12788. * size - size of buf
  12789. ******************************************************************************/
  12790. static ssize_t pt_get_param_store(struct device *dev,
  12791. struct device_attribute *attr, const char *buf, size_t size)
  12792. {
  12793. struct pt_core_data *cd = dev_get_drvdata(dev);
  12794. u32 input_data[2];
  12795. int length;
  12796. int rc = 0;
  12797. /* Maximum input of one value */
  12798. length = _pt_ic_parse_input(dev, buf, size, input_data,
  12799. ARRAY_SIZE(input_data));
  12800. if (length != 1) {
  12801. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12802. __func__);
  12803. rc = -EINVAL;
  12804. goto exit;
  12805. }
  12806. mutex_lock(&cd->system_lock);
  12807. cd->get_param_id = input_data[0];
  12808. mutex_unlock(&(cd->system_lock));
  12809. exit:
  12810. if (rc)
  12811. return rc;
  12812. return size;
  12813. }
  12814. /*******************************************************************************
  12815. * FUNCTION: pt_get_param_show
  12816. *
  12817. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  12818. * parameter data from the DUT based on the ID stored in the core
  12819. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  12820. * communicate or some other error occures, an error status is returned
  12821. * with no value following.
  12822. * Output is in the form:
  12823. * Status: x
  12824. * 0xyyyyyyyy
  12825. * The 32bit data will only follow the status code if the status == 0
  12826. *
  12827. * NOTE: This sysfs node is only available after a DUT has been enumerated
  12828. *
  12829. * RETURN:
  12830. * 0 = success
  12831. * !0 = failure
  12832. *
  12833. * PARAMETERS:
  12834. * *dev - pointer to device structure
  12835. * *attr - pointer to device attributes
  12836. * *buf - pointer to output buffer
  12837. ******************************************************************************/
  12838. static ssize_t pt_get_param_show(struct device *dev,
  12839. struct device_attribute *attr, char *buf)
  12840. {
  12841. struct pt_core_data *cd = dev_get_drvdata(dev);
  12842. ssize_t ret = 0;
  12843. int status;
  12844. u32 value = 0;
  12845. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  12846. if (status) {
  12847. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  12848. __func__, "pt_get_param", status);
  12849. ret = scnprintf(buf, strlen(buf),
  12850. "%s %d\n",
  12851. "Status:", status);
  12852. } else {
  12853. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  12854. __func__, cd->get_param_id, value);
  12855. ret = scnprintf(buf, strlen(buf),
  12856. "Status: %d\n"
  12857. "0x%04X\n",
  12858. status, value);
  12859. }
  12860. return ret;
  12861. }
  12862. /*******************************************************************************
  12863. * FUNCTION: pt_ttdl_restart_show
  12864. *
  12865. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  12866. * probed modules, calls startup() and then re-probes modules.
  12867. *
  12868. * RETURN: size of data written to sysfs node
  12869. *
  12870. * PARAMETERS:
  12871. * *dev - pointer to device structure
  12872. * *attr - pointer to device attributes structure
  12873. * *buf - pointer to print output buffer
  12874. ******************************************************************************/
  12875. static ssize_t pt_ttdl_restart_show(struct device *dev,
  12876. struct device_attribute *attr, char *buf)
  12877. {
  12878. struct pt_core_data *cd = dev_get_drvdata(dev);
  12879. int t;
  12880. int rc = 0;
  12881. mutex_lock(&cd->system_lock);
  12882. cd->startup_state = STARTUP_NONE;
  12883. mutex_unlock(&(cd->system_lock));
  12884. /* ensure no left over exclusive access is still locked */
  12885. release_exclusive(cd, cd->dev);
  12886. pt_queue_restart(cd);
  12887. t = wait_event_timeout(cd->wait_q,
  12888. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  12889. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  12890. if (IS_TMO(t)) {
  12891. pt_debug(cd->dev, DL_ERROR,
  12892. "%s: TMO waiting for FW sentinel\n", __func__);
  12893. rc = -ETIME;
  12894. }
  12895. return scnprintf(buf, strlen(buf),
  12896. "Status: %d\n"
  12897. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  12898. }
  12899. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  12900. /*******************************************************************************
  12901. * FUNCTION: pt_pip2_gpio_read_show
  12902. *
  12903. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  12904. * contents of the response to the passed in output buffer.
  12905. *
  12906. * RETURN: size of data written to sysfs node
  12907. *
  12908. * PARAMETERS:
  12909. * *dev - pointer to device structure
  12910. * *attr - pointer to device attributes structure
  12911. * *buf - pointer to print output buffer
  12912. ******************************************************************************/
  12913. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  12914. struct device_attribute *attr, char *buf)
  12915. {
  12916. struct pt_core_data *cd = dev_get_drvdata(dev);
  12917. u8 status = 0;
  12918. u32 gpio_value = 0;
  12919. int rc = 0;
  12920. /* This functionality is only available in the BL */
  12921. if (cd->mode == PT_MODE_BOOTLOADER)
  12922. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  12923. else
  12924. rc = -EPERM;
  12925. if (!rc) {
  12926. if (status == 0)
  12927. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12928. "Status: %d\n"
  12929. "DUT GPIO Reg: 0x%08X\n",
  12930. rc, gpio_value);
  12931. else
  12932. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12933. "Status: %d\n"
  12934. "DUT GPIO Reg: n/a\n",
  12935. status);
  12936. } else
  12937. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12938. "Status: %d\n"
  12939. "DUT GPIO Reg: n/a\n",
  12940. rc);
  12941. }
  12942. /*******************************************************************************
  12943. * FUNCTION: pt_pip2_version_show
  12944. *
  12945. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  12946. * contents of the response to the passed in output buffer.
  12947. *
  12948. * RETURN: size of data written to sysfs node
  12949. *
  12950. * PARAMETERS:
  12951. * *dev - pointer to device structure
  12952. * *attr - pointer to device attributes structure
  12953. * *buf - pointer to print output buffer
  12954. ******************************************************************************/
  12955. static ssize_t pt_pip2_version_show(struct device *dev,
  12956. struct device_attribute *attr, char *buf)
  12957. {
  12958. int rc = 0;
  12959. struct pt_core_data *cd = dev_get_drvdata(dev);
  12960. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  12961. rc = pt_pip2_get_version(cd);
  12962. if (!rc) {
  12963. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12964. "PIP VERSION : %02X.%02X\n"
  12965. "BL VERSION : %02X.%02X\n"
  12966. "FW VERSION : %02X.%02X\n"
  12967. "SILICON ID : %04X.%04X\n"
  12968. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  12969. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  12970. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  12971. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  12972. ttdata->chip_id, ttdata->chip_rev,
  12973. ttdata->uid[0], ttdata->uid[1],
  12974. ttdata->uid[2], ttdata->uid[3],
  12975. ttdata->uid[4], ttdata->uid[5],
  12976. ttdata->uid[6], ttdata->uid[7],
  12977. ttdata->uid[8], ttdata->uid[9],
  12978. ttdata->uid[10], ttdata->uid[11]);
  12979. } else {
  12980. pt_debug(dev, DL_ERROR,
  12981. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  12982. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12983. "PIP VERSION : -\n"
  12984. "BL VERSION : -\n"
  12985. "FW VERSION : -\n"
  12986. "SILICON ID : -\n"
  12987. "UID : -\n");
  12988. }
  12989. }
  12990. #ifdef TTDL_DIAGNOSTICS
  12991. /*******************************************************************************
  12992. * FUNCTION: pt_ttdl_status_show
  12993. *
  12994. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  12995. * variable states and GPIO levels. Additional information printed when
  12996. * TTDL_DIAGNOSTICS is enabled.
  12997. *
  12998. * NOTE: All counters will be reset to 0 when this function is called.
  12999. *
  13000. * RETURN:
  13001. * 0 = success
  13002. * !0 = failure
  13003. *
  13004. * PARAMETERS:
  13005. * *dev - pointer to device structure
  13006. * *attr - pointer to device attributes
  13007. * *buf - pointer to output buffer
  13008. ******************************************************************************/
  13009. static ssize_t pt_ttdl_status_show(struct device *dev,
  13010. struct device_attribute *attr, char *buf)
  13011. {
  13012. struct pt_core_data *cd = dev_get_drvdata(dev);
  13013. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13014. struct i2c_client *client = to_i2c_client(dev);
  13015. ssize_t ret;
  13016. u16 cal_size = 0;
  13017. unsigned short crc = 0;
  13018. if (cd->cal_cache_in_host)
  13019. _pt_manage_local_cal_data(dev,
  13020. PT_CAL_DATA_INFO, &cal_size, &crc);
  13021. ret = scnprintf(buf, strlen(buf),
  13022. "%s: 0x%04X\n"
  13023. "%s: %d\n"
  13024. "%s: %s\n"
  13025. "%s: %s %s\n"
  13026. "%s: %s\n"
  13027. "%s: 0x%02X\n"
  13028. "%s: %s\n"
  13029. "%s: %s\n"
  13030. "%s: %s\n"
  13031. "%s: %s\n"
  13032. "%s: %d\n"
  13033. "%s: %d\n"
  13034. "%s: %s\n"
  13035. "%s: %s\n"
  13036. "%s: %d\n"
  13037. #ifdef TTDL_DIAGNOSTICS
  13038. "%s: %d\n"
  13039. "%s: %d\n"
  13040. "%s: %d\n"
  13041. "%s: %d\n"
  13042. "%s: %d\n"
  13043. "%s: %d\n"
  13044. "%s: %d\n"
  13045. "%s: %d\n"
  13046. "%s: %d\n"
  13047. "%s: %d\n"
  13048. "%s: %s\n"
  13049. "%s: %s\n"
  13050. "%s: %s\n"
  13051. "%s: %d\n"
  13052. "%s: 0x%04X\n"
  13053. "%s: %s\n"
  13054. #endif /* TTDL_DIAGNOSTICS */
  13055. ,
  13056. "Startup Status ", cd->startup_status,
  13057. "TTDL Debug Level ", cd->debug_level,
  13058. "Mode ",
  13059. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13060. "Operational" : "BL") : "Unknown",
  13061. "DUT Generation ",
  13062. cd->active_dut_generation ?
  13063. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13064. "PT TC/TT" : "Gen5/6") : "Unknown",
  13065. cd->active_dut_generation ?
  13066. (cd->set_dut_generation == true ?
  13067. "(Set)" : "(Detected)") : "",
  13068. "HW Detected ",
  13069. cd->hw_detected ? "True" : "False",
  13070. "I2C Address ",
  13071. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13072. "Active Bus Module ",
  13073. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13074. "Flashless Mode ",
  13075. cd->flashless_dut == 1 ? "Yes" : "No",
  13076. "GPIO state - IRQ ",
  13077. cd->cpdata->irq_stat ?
  13078. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13079. "High" : "Low") : "not defined",
  13080. "GPIO state - TP_XRES ",
  13081. pdata->core_pdata->rst_gpio ?
  13082. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13083. "High" : "Low") : "not defined",
  13084. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13085. "Startup Retry Count ", cd->startup_retry_count,
  13086. "WD - Manual Force Stop ",
  13087. cd->watchdog_force_stop ? "True" : "False",
  13088. "WD - Enabled ",
  13089. cd->watchdog_enabled ? "True" : "False",
  13090. "WD - Interval (ms) ", cd->watchdog_interval
  13091. #ifdef TTDL_DIAGNOSTICS
  13092. , "WD - Triggered Count ", cd->watchdog_count,
  13093. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13094. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13095. "WD - XRES Count ", cd->wd_xres_count,
  13096. "IRQ Triggered Count ", cd->irq_count,
  13097. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13098. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13099. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13100. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13101. "Error GPIO trigger type ", cd->err_gpio_type,
  13102. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13103. "Suppress No-Flash Auto BL ",
  13104. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13105. "Yes" : "No",
  13106. "Calibration Cache on host ",
  13107. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13108. "Yes" : "No",
  13109. "Calibration Cache size ", cal_size,
  13110. "Calibration Cache chip ID ", crc,
  13111. "Multi-Chip Support ",
  13112. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13113. #endif /* TTDL_DIAGNOSTICS */
  13114. );
  13115. #ifdef TTDL_DIAGNOSTICS
  13116. /* Reset all diagnostic counters */
  13117. cd->watchdog_count = 0;
  13118. cd->watchdog_irq_stuck_count = 0;
  13119. cd->watchdog_failed_access_count = 0;
  13120. cd->wd_xres_count = 0;
  13121. cd->irq_count = 0;
  13122. cd->bl_retry_packet_count = 0;
  13123. cd->pip2_crc_error_count = 0;
  13124. cd->bus_transmit_error_count = 0;
  13125. #endif
  13126. return ret;
  13127. }
  13128. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13129. /*******************************************************************************
  13130. * FUNCTION: pt_pip2_enter_bl_show
  13131. *
  13132. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13133. * the DUT into the BL and show the success or failure of entering the BL
  13134. *
  13135. * RETURN:
  13136. * 0 = success
  13137. * !0 = failure
  13138. *
  13139. * PARAMETERS:
  13140. * *dev - pointer to device structure
  13141. * *attr - pointer to device attributes
  13142. * *buf - pointer to output buffer
  13143. ******************************************************************************/
  13144. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13145. struct device_attribute *attr, char *buf)
  13146. {
  13147. ssize_t ret = 0;
  13148. int rc = 0;
  13149. int result = 0;
  13150. u8 mode = PT_MODE_UNKNOWN;
  13151. struct pt_core_data *cd = dev_get_drvdata(dev);
  13152. /* Turn off the TTDL WD before enter bootloader */
  13153. pt_stop_wd_timer(cd);
  13154. /* Ensure NO enumeration work is queued or will be queued */
  13155. cancel_work_sync(&cd->enum_work);
  13156. mutex_lock(&cd->system_lock);
  13157. cd->bridge_mode = true;
  13158. mutex_unlock(&cd->system_lock);
  13159. /* set mode to operational to avoid any extra PIP traffic */
  13160. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13161. switch (result) {
  13162. case PT_ENTER_BL_PASS:
  13163. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13164. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13165. break;
  13166. case PT_ENTER_BL_ERROR:
  13167. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13168. rc,
  13169. " Unknown Error");
  13170. break;
  13171. case PT_ENTER_BL_RESET_FAIL:
  13172. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13173. rc,
  13174. " Soft Reset Failed");
  13175. break;
  13176. case PT_ENTER_BL_HID_START_BL_FAIL:
  13177. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13178. rc,
  13179. " PIP Start BL Cmd Failed");
  13180. break;
  13181. case PT_ENTER_BL_CONFIRM_FAIL:
  13182. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13183. rc,
  13184. " Error confirming DUT entered BL");
  13185. break;
  13186. default:
  13187. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13188. rc, " Unknown Error");
  13189. break;
  13190. };
  13191. /* Allow enumeration work to be queued again */
  13192. cd->bridge_mode = false;
  13193. return ret;
  13194. }
  13195. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13196. /*******************************************************************************
  13197. * FUNCTION: pt_pip2_exit_bl_show
  13198. *
  13199. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13200. * launch the APP and put the DUT Application mode
  13201. *
  13202. * RETURN:
  13203. * 0 = success
  13204. * !0 = failure
  13205. *
  13206. * PARAMETERS:
  13207. * *dev - pointer to device structure
  13208. * *attr - pointer to device attributes
  13209. * *buf - pointer to output buffer
  13210. ******************************************************************************/
  13211. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13212. struct device_attribute *attr, char *buf)
  13213. {
  13214. struct pt_core_data *cd = dev_get_drvdata(dev);
  13215. ssize_t ret = 0;
  13216. int rc = 0;
  13217. u8 status_str[PT_STATUS_STR_LEN];
  13218. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13219. /*
  13220. * Perform enum if startup_status doesn't reach to
  13221. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13222. */
  13223. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13224. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13225. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13226. strlcpy(status_str,
  13227. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13228. }
  13229. }
  13230. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13231. rc, status_str);
  13232. return ret;
  13233. }
  13234. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13235. #endif
  13236. #ifdef EASYWAKE_TSG6
  13237. /*******************************************************************************
  13238. * FUNCTION: pt_easy_wakeup_gesture_show
  13239. *
  13240. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13241. * current easy wakeup gesture
  13242. *
  13243. * RETURN:
  13244. * 0 = success
  13245. * !0 = failure
  13246. *
  13247. * PARAMETERS:
  13248. * *dev - pointer to device structure
  13249. * *attr - pointer to device attributes
  13250. * *buf - pointer to output buffer
  13251. ******************************************************************************/
  13252. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13253. struct device_attribute *attr, char *buf)
  13254. {
  13255. struct pt_core_data *cd = dev_get_drvdata(dev);
  13256. ssize_t ret;
  13257. mutex_lock(&cd->system_lock);
  13258. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13259. cd->easy_wakeup_gesture);
  13260. mutex_unlock(&cd->system_lock);
  13261. return ret;
  13262. }
  13263. /*******************************************************************************
  13264. * FUNCTION: pt_easy_wakeup_gesture_store
  13265. *
  13266. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13267. * allows the wake gesture to be set to a custom value.
  13268. *
  13269. * RETURN: Size of passed in buffer is success
  13270. *
  13271. * PARAMETERS:
  13272. * *dev - pointer to device structure
  13273. * *attr - pointer to device attributes
  13274. * *buf - pointer to buffer that hold the command parameters
  13275. * size - size of buf
  13276. ******************************************************************************/
  13277. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13278. struct device_attribute *attr, const char *buf, size_t size)
  13279. {
  13280. struct pt_core_data *cd = dev_get_drvdata(dev);
  13281. u32 input_data[2];
  13282. int length;
  13283. int rc = 0;
  13284. /* Maximum input of one value */
  13285. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13286. ARRAY_SIZE(input_data));
  13287. if (length != 1) {
  13288. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13289. __func__);
  13290. rc = -EINVAL;
  13291. goto exit;
  13292. }
  13293. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13294. __func__, cd->features.easywake);
  13295. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13296. rc = -EINVAL;
  13297. goto exit;
  13298. }
  13299. pm_runtime_get_sync(dev);
  13300. mutex_lock(&cd->system_lock);
  13301. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13302. cd->easy_wakeup_gesture = (u8)input_data[0];
  13303. pt_debug(dev, DL_INFO,
  13304. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13305. __func__, cd->easy_wakeup_gesture);
  13306. } else
  13307. rc = -ENODEV;
  13308. mutex_unlock(&cd->system_lock);
  13309. pm_runtime_put(dev);
  13310. exit:
  13311. if (rc)
  13312. return rc;
  13313. return size;
  13314. }
  13315. /*******************************************************************************
  13316. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13317. *
  13318. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13319. * show the TSG6 easywake gesture ID
  13320. *
  13321. * RETURN:
  13322. * 0 = success
  13323. * !0 = failure
  13324. *
  13325. * PARAMETERS:
  13326. * *dev - pointer to device structure
  13327. * *attr - pointer to device attributes
  13328. * *buf - pointer to output buffer
  13329. ******************************************************************************/
  13330. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13331. struct device_attribute *attr, char *buf)
  13332. {
  13333. struct pt_core_data *cd = dev_get_drvdata(dev);
  13334. ssize_t ret;
  13335. mutex_lock(&cd->system_lock);
  13336. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13337. cd->gesture_id);
  13338. mutex_unlock(&cd->system_lock);
  13339. return ret;
  13340. }
  13341. /*******************************************************************************
  13342. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13343. *
  13344. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13345. * show the TSG6 easywake gesture data in the following format:
  13346. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13347. *
  13348. * RETURN:
  13349. * 0 = success
  13350. * !0 = failure
  13351. *
  13352. * PARAMETERS:
  13353. * *dev - pointer to device structure
  13354. * *attr - pointer to device attributes
  13355. * *buf - pointer to output buffer
  13356. ******************************************************************************/
  13357. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13358. struct device_attribute *attr, char *buf)
  13359. {
  13360. struct pt_core_data *cd = dev_get_drvdata(dev);
  13361. ssize_t ret = 0;
  13362. int i;
  13363. mutex_lock(&cd->system_lock);
  13364. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13365. for (i = 0; i < cd->gesture_data_length; i++)
  13366. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13367. "0x%02X\n", cd->gesture_data[i]);
  13368. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13369. "(%d bytes)\n", cd->gesture_data_length);
  13370. mutex_unlock(&cd->system_lock);
  13371. return ret;
  13372. }
  13373. #endif /* EASYWAKE_TSG6 */
  13374. #ifdef TTDL_DIAGNOSTICS
  13375. /*******************************************************************************
  13376. * FUNCTION: pt_err_gpio_show
  13377. *
  13378. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13379. * setting up the gpio was successful
  13380. *
  13381. * RETURN: Char buffer with printed GPIO creation state
  13382. *
  13383. * PARAMETERS:
  13384. * *dev - pointer to device structure
  13385. * *attr - pointer to device attributes
  13386. * *buf - pointer to output buffer
  13387. ******************************************************************************/
  13388. static ssize_t pt_err_gpio_show(struct device *dev,
  13389. struct device_attribute *attr, char *buf)
  13390. {
  13391. struct pt_core_data *cd = dev_get_drvdata(dev);
  13392. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13393. "Err GPIO (%d) : %s\n"
  13394. "Err GPIO trig type: %d\n",
  13395. cd->err_gpio,
  13396. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13397. "HIGH" : "low") : "not defined"),
  13398. cd->err_gpio_type);
  13399. }
  13400. /*******************************************************************************
  13401. * FUNCTION: pt_err_gpio_store
  13402. *
  13403. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13404. * available host GPIO to be used to trigger when TTDL detects a PIP
  13405. * command/response timeout.
  13406. *
  13407. * RETURN:
  13408. * 0 = success
  13409. * !0 = failure
  13410. *
  13411. * PARAMETERS:
  13412. * *dev - pointer to device structure
  13413. * *attr - pointer to device attributes
  13414. * *buf - pointer to buffer that hold the command parameters
  13415. * size - size of buf
  13416. ******************************************************************************/
  13417. static ssize_t pt_err_gpio_store(struct device *dev,
  13418. struct device_attribute *attr, const char *buf, size_t size)
  13419. {
  13420. struct pt_core_data *cd = dev_get_drvdata(dev);
  13421. unsigned long gpio;
  13422. int rc = 0;
  13423. u32 input_data[3];
  13424. int length;
  13425. u8 err_type;
  13426. input_data[0] = 0;
  13427. input_data[1] = 0;
  13428. /* Maximmum input is two elements */
  13429. length = _pt_ic_parse_input(dev, buf, size,
  13430. input_data, ARRAY_SIZE(input_data));
  13431. if (length < 1 || length > 2) {
  13432. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13433. __func__);
  13434. rc = -EINVAL;
  13435. goto exit;
  13436. }
  13437. gpio = input_data[0];
  13438. err_type = (u8)input_data[1];
  13439. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13440. rc = -EINVAL;
  13441. goto exit;
  13442. }
  13443. mutex_lock(&cd->system_lock);
  13444. gpio_free(gpio);
  13445. rc = gpio_request(gpio, NULL);
  13446. if (rc) {
  13447. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13448. rc = -ENODEV;
  13449. } else {
  13450. cd->err_gpio = gpio;
  13451. cd->err_gpio_type = err_type;
  13452. gpio_direction_output(gpio, 0);
  13453. }
  13454. mutex_unlock(&cd->system_lock);
  13455. exit:
  13456. if (rc)
  13457. return rc;
  13458. return size;
  13459. }
  13460. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13461. pt_err_gpio_store);
  13462. /*******************************************************************************
  13463. * FUNCTION: pt_drv_irq_show
  13464. *
  13465. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13466. * TTDL interrupt is enabled/disabled
  13467. *
  13468. * RETURN:
  13469. * 0 = success
  13470. * !0 = failure
  13471. *
  13472. * PARAMETERS:
  13473. * *dev - pointer to device structure
  13474. * *attr - pointer to device attributes
  13475. * *buf - pointer to output buffer
  13476. ******************************************************************************/
  13477. static ssize_t pt_drv_irq_show(struct device *dev,
  13478. struct device_attribute *attr, char *buf)
  13479. {
  13480. struct pt_core_data *cd = dev_get_drvdata(dev);
  13481. ssize_t ret = 0;
  13482. mutex_lock(&cd->system_lock);
  13483. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13484. if (cd->irq_enabled)
  13485. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13486. "Driver interrupt: ENABLED\n");
  13487. else
  13488. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13489. "Driver interrupt: DISABLED\n");
  13490. mutex_unlock(&cd->system_lock);
  13491. return ret;
  13492. }
  13493. /*******************************************************************************
  13494. * FUNCTION: pt_drv_irq_store
  13495. *
  13496. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13497. * IRQ to be enabled/disabled.
  13498. *
  13499. * RETURN: Size of passed in buffer
  13500. *
  13501. * PARAMETERS:
  13502. * *dev - pointer to device structure
  13503. * *attr - pointer to device attributes
  13504. * *buf - pointer to buffer that hold the command parameters
  13505. * size - size of buf
  13506. ******************************************************************************/
  13507. static ssize_t pt_drv_irq_store(struct device *dev,
  13508. struct device_attribute *attr, const char *buf, size_t size)
  13509. {
  13510. struct pt_core_data *cd = dev_get_drvdata(dev);
  13511. u32 input_data[2];
  13512. int length;
  13513. int rc = 0;
  13514. /* Maximum input of one value */
  13515. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13516. ARRAY_SIZE(input_data));
  13517. if (length != 1) {
  13518. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13519. __func__);
  13520. rc = -EINVAL;
  13521. goto exit;
  13522. }
  13523. mutex_lock(&cd->system_lock);
  13524. switch (input_data[0]) {
  13525. case 0:
  13526. if (cd->irq_enabled) {
  13527. cd->irq_enabled = false;
  13528. /* Disable IRQ has no return value to check */
  13529. disable_irq_nosync(cd->irq);
  13530. pt_debug(dev, DL_INFO,
  13531. "%s: Driver IRQ now disabled\n",
  13532. __func__);
  13533. } else
  13534. pt_debug(dev, DL_INFO,
  13535. "%s: Driver IRQ already disabled\n",
  13536. __func__);
  13537. break;
  13538. case 1:
  13539. if (cd->irq_enabled == false) {
  13540. cd->irq_enabled = true;
  13541. enable_irq(cd->irq);
  13542. pt_debug(dev, DL_INFO,
  13543. "%s: Driver IRQ now enabled\n",
  13544. __func__);
  13545. } else
  13546. pt_debug(dev, DL_INFO,
  13547. "%s: Driver IRQ already enabled\n",
  13548. __func__);
  13549. break;
  13550. default:
  13551. rc = -EINVAL;
  13552. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13553. }
  13554. mutex_unlock(&(cd->system_lock));
  13555. exit:
  13556. if (rc)
  13557. return rc;
  13558. return size;
  13559. }
  13560. /*******************************************************************************
  13561. * FUNCTION: pt_pip2_bin_hdr_show
  13562. *
  13563. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13564. * the bin file header from flash and show each field
  13565. *
  13566. * RETURN:
  13567. * 0 = success
  13568. * !0 = failure
  13569. *
  13570. * PARAMETERS:
  13571. * *dev - pointer to device structure
  13572. * *attr - pointer to device attributes
  13573. * *buf - pointer to output buffer
  13574. ******************************************************************************/
  13575. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13576. struct device_attribute *attr, char *buf)
  13577. {
  13578. ssize_t ret = 0;
  13579. struct pt_bin_file_hdr hdr = {0};
  13580. int rc;
  13581. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  13582. ret = scnprintf(buf, strlen(buf),
  13583. "%s: %d\n"
  13584. "%s: %d\n"
  13585. "%s: 0x%04X\n"
  13586. "%s: %d\n"
  13587. "%s: %d\n"
  13588. "%s: %d\n"
  13589. "%s: 0x%08X\n"
  13590. "%s: 0x%04X\n"
  13591. "%s: 0x%04X\n"
  13592. "%s: %d\n"
  13593. "%s: %d\n",
  13594. "Status", rc,
  13595. "Header Length ", hdr.length,
  13596. "TTPID ", hdr.ttpid,
  13597. "FW Major Ver ", hdr.fw_major,
  13598. "FW Minor Ver ", hdr.fw_minor,
  13599. "FW Rev Control ", hdr.fw_rev_ctrl,
  13600. "FW CRC ", hdr.fw_crc,
  13601. "Silicon Rev ", hdr.si_rev,
  13602. "Silicon ID ", hdr.si_id,
  13603. "Config Ver ", hdr.config_ver,
  13604. "HEX File Size ", hdr.hex_file_size
  13605. );
  13606. return ret;
  13607. }
  13608. /*******************************************************************************
  13609. * FUNCTION: pt_platform_data_show
  13610. *
  13611. * SUMMARY: Show method for the platform_data sysfs node that will show the
  13612. * active platform data including: GPIOs, Vendor and Product IDs,
  13613. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  13614. * HID registers, and Easy wake gesture
  13615. *
  13616. * RETURN: Size of printed data
  13617. *
  13618. * PARAMETERS:
  13619. * *dev - pointer to device structure
  13620. * *attr - pointer to device attributes
  13621. * *buf - pointer to output buffer
  13622. ******************************************************************************/
  13623. static ssize_t pt_platform_data_show(struct device *dev,
  13624. struct device_attribute *attr, char *buf)
  13625. {
  13626. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13627. struct pt_core_data *cd = dev_get_drvdata(dev);
  13628. ssize_t ret;
  13629. ret = scnprintf(buf, strlen(buf),
  13630. "%s: %d\n"
  13631. "%s: %d\n"
  13632. "%s: %d\n"
  13633. "%s: %d\n"
  13634. "%s: %d\n"
  13635. "%s: %d\n"
  13636. "%s: %d\n"
  13637. "%s: %d\n"
  13638. "%s: %d\n"
  13639. "%s: %d\n"
  13640. "%s: %d\n"
  13641. "%s: %d\n"
  13642. "%s: %d\n"
  13643. "%s: %d\n"
  13644. "%s: %d\n"
  13645. "%s: %d\n"
  13646. "%s: %d\n"
  13647. "%s: %d\n"
  13648. "%s: %d\n"
  13649. "%s: %d\n"
  13650. "%s: %d\n"
  13651. "%s: %d\n"
  13652. "%s: %d\n"
  13653. "%s: %d\n"
  13654. "%s: %d\n"
  13655. "%s: %d\n"
  13656. "%s: %d\n"
  13657. "%s: %s\n"
  13658. "%s: %s\n"
  13659. "%s: %d\n",
  13660. "Status", 0,
  13661. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  13662. "Interrupt GPIO Value ",
  13663. pdata->core_pdata->irq_gpio ?
  13664. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  13665. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  13666. "Reset GPIO Value ",
  13667. pdata->core_pdata->rst_gpio ?
  13668. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  13669. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  13670. "DDI Reset GPIO Value ",
  13671. pdata->core_pdata->ddi_rst_gpio ?
  13672. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  13673. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  13674. "VDDI GPIO Value ",
  13675. pdata->core_pdata->vddi_gpio ?
  13676. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  13677. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  13678. "VCC GPIO Value ",
  13679. pdata->core_pdata->vcc_gpio ?
  13680. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  13681. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  13682. "AVDD GPIO Value ",
  13683. pdata->core_pdata->avdd_gpio ?
  13684. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  13685. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  13686. "AVEE GPIO Value ",
  13687. pdata->core_pdata->avee_gpio ?
  13688. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  13689. "Vendor ID ", pdata->core_pdata->vendor_id,
  13690. "Product ID ", pdata->core_pdata->product_id,
  13691. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  13692. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  13693. "Core data flags ", pdata->core_pdata->flags,
  13694. "MT data flags ", pdata->mt_pdata->flags,
  13695. "Loader data flags ", pdata->loader_pdata->flags,
  13696. "Level trigger delay (us) ",
  13697. pdata->core_pdata->level_irq_udelay,
  13698. "HID Descriptor Register ",
  13699. pdata->core_pdata->hid_desc_register,
  13700. "HID Output Register ",
  13701. cd->hid_desc.output_register,
  13702. "HID Command Register ",
  13703. cd->hid_desc.command_register,
  13704. "Easy wakeup gesture ",
  13705. pdata->core_pdata->easy_wakeup_gesture,
  13706. "Config DUT generation ",
  13707. pdata->core_pdata->config_dut_generation ?
  13708. (pdata->core_pdata->config_dut_generation ==
  13709. CONFIG_DUT_PIP2_CAPABLE ?
  13710. "PT TC/TT" : "Gen5/6") : "Auto",
  13711. "Watchdog Force Stop ",
  13712. pdata->core_pdata->watchdog_force_stop ?
  13713. "True" : "False",
  13714. "Panel ID Support ",
  13715. pdata->core_pdata->panel_id_support);
  13716. return ret;
  13717. }
  13718. #define PT_ERR_STR_SIZE 64
  13719. /*******************************************************************************
  13720. * FUNCTION: pt_bist_bus_test
  13721. *
  13722. * SUMMARY: Tests the connectivity of the active bus pins:
  13723. * I2C - SDA and SCL
  13724. * SPI - MOSI, MISO, CLK
  13725. *
  13726. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  13727. * bus. If any data is read we know the I2C/SPI pins are connected
  13728. *
  13729. * RETURN:
  13730. * 0 = Success
  13731. * !0 = Failure
  13732. *
  13733. * PARAMETERS:
  13734. * *dev - pointer to device structure
  13735. * *net_toggled - pointer to where to store if bus toggled
  13736. * *err_str - pointer to error string buffer
  13737. ******************************************************************************/
  13738. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  13739. {
  13740. struct pt_core_data *cd = dev_get_drvdata(dev);
  13741. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  13742. u8 *read_buf = NULL;
  13743. int bytes_read = 0;
  13744. int rc = 0;
  13745. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13746. if (read_buf == NULL) {
  13747. rc = -ENOMEM;
  13748. goto exit;
  13749. }
  13750. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13751. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  13752. disable_irq(cd->irq);
  13753. mutex_lock(&cd->system_lock);
  13754. cd->irq_disabled = true;
  13755. mutex_unlock(&cd->system_lock);
  13756. /*
  13757. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  13758. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  13759. */
  13760. usleep_range(5000, 6000);
  13761. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  13762. __func__, (int)sizeof(ver_cmd));
  13763. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  13764. ">>> User CMD");
  13765. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  13766. if (rc) {
  13767. pt_debug(dev, DL_ERROR,
  13768. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  13769. *net_toggled = 0;
  13770. strlcpy(err_str,
  13771. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  13772. goto exit_enable_irq;
  13773. }
  13774. usleep_range(4000, 5000);
  13775. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  13776. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  13777. __func__, bytes_read);
  13778. if (bytes_read == 0) {
  13779. *net_toggled = 0;
  13780. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  13781. __func__);
  13782. strlcpy(err_str,
  13783. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13784. rc = -EIO;
  13785. goto exit_enable_irq;
  13786. } else {
  13787. if (cd->bus_ops->bustype == BUS_I2C)
  13788. *net_toggled = 1;
  13789. else {
  13790. if ((bytes_read > 3) &&
  13791. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  13792. PIP2_CMD_ID_VERSION)
  13793. *net_toggled = 1;
  13794. else {
  13795. *net_toggled = 0;
  13796. pt_debug(dev, DL_INFO,
  13797. "%s: BUS Read Failed, %d bytes read\n",
  13798. __func__, bytes_read);
  13799. strlcpy(err_str,
  13800. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  13801. }
  13802. }
  13803. }
  13804. exit_enable_irq:
  13805. enable_irq(cd->irq);
  13806. usleep_range(5000, 6000);
  13807. mutex_lock(&cd->system_lock);
  13808. cd->irq_disabled = false;
  13809. mutex_unlock(&cd->system_lock);
  13810. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  13811. exit:
  13812. kfree(read_buf);
  13813. return rc;
  13814. }
  13815. /*******************************************************************************
  13816. * FUNCTION: pt_bist_irq_test
  13817. *
  13818. * SUMMARY: Tests the connectivity of the IRQ net
  13819. *
  13820. * This test will ensure there is a good connection between the host
  13821. * and the DUT on the irq pin. First determine if the IRQ is stuck
  13822. * asserted and if so keep reading messages off of the bus until
  13823. * it de-asserts. Possible outcomes:
  13824. * - IRQ was already de-asserted: Send a PIP command and if an
  13825. * interrupt is generated the test passes.
  13826. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  13827. * test passes.
  13828. * - IRQ stays asserted: After reading the bus multiple times
  13829. * the IRQ stays asserted. Likely open or shorted to GND
  13830. *
  13831. * RETURN:
  13832. * 0 = Success
  13833. * !0 = Failure
  13834. *
  13835. * PARAMETERS:
  13836. * *dev - pointer to device structure
  13837. * *bus_toggled - pointer to where to store if bus toggled
  13838. * *irq_toggled - pointer to where to store if IRQ toggled
  13839. * *xres_toggled - pointer to where to store if XRES toggled
  13840. * *err_str - pointer to error string buffer
  13841. ******************************************************************************/
  13842. static int pt_bist_irq_test(struct device *dev,
  13843. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  13844. {
  13845. struct pt_core_data *cd = dev_get_drvdata(dev);
  13846. u8 *read_buf = NULL;
  13847. u8 mode = PT_MODE_UNKNOWN;
  13848. u16 actual_read_len;
  13849. int bytes_read = 0;
  13850. int count = 0;
  13851. int rc = 0;
  13852. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  13853. if (read_buf == NULL) {
  13854. rc = -ENOMEM;
  13855. goto exit;
  13856. }
  13857. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  13858. cd->irq_count = 0;
  13859. /*
  13860. * Check if IRQ is stuck asserted, if so try a non forced flush of
  13861. * the bus based on the 2 byte initial length read. Try up to 5x.
  13862. */
  13863. while (pt_check_irq_asserted(cd) && count < 5) {
  13864. count++;
  13865. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  13866. }
  13867. if (count > 1 && count < 5 && bytes_read > 0) {
  13868. /*
  13869. * IRQ was stuck but data was successfully read from the
  13870. * bus releasing the IRQ line.
  13871. */
  13872. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  13873. __func__, count, bytes_read);
  13874. *bus_toggled = 1;
  13875. *irq_toggled = 1;
  13876. goto exit;
  13877. }
  13878. if (count == 5 && bytes_read == 0) {
  13879. /*
  13880. * Looped 5x and read nothing off the bus yet the IRQ is still
  13881. * asserted. Possible conditions:
  13882. * - IRQ open circuit
  13883. * - IRQ shorted to GND
  13884. * - I2C/SPI bus is disconnected
  13885. * - FW holding the pin low
  13886. * Try entering the BL to see if communication works there.
  13887. */
  13888. mode = PT_MODE_IGNORE;
  13889. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13890. if (rc) {
  13891. pt_debug(dev, DL_ERROR,
  13892. "%s Failed to enter BL\n", __func__);
  13893. strlcpy(err_str,
  13894. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  13895. *irq_toggled = 0;
  13896. goto exit;
  13897. }
  13898. /*
  13899. * If original mode was operational and we successfully
  13900. * entered the BL, then the XRES net must have toggled
  13901. */
  13902. if (mode == PT_MODE_OPERATIONAL)
  13903. *xres_toggled = 1;
  13904. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13905. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  13906. &actual_read_len);
  13907. if (rc) {
  13908. /*
  13909. * Could not communicate to DUT in BL mode. Save the
  13910. * error string, slim chance but the XRES test below may
  13911. * show the IRQ is actually working.
  13912. */
  13913. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  13914. pt_debug(dev, DL_ERROR,
  13915. "%s: %s, count=%d bytes_read=%d\n",
  13916. __func__, err_str, count, bytes_read);
  13917. *irq_toggled = 0;
  13918. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  13919. goto exit;
  13920. } else {
  13921. *bus_toggled = 1;
  13922. *irq_toggled = 1;
  13923. goto exit;
  13924. }
  13925. }
  13926. if (pt_check_irq_asserted(cd)) {
  13927. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  13928. rc = -EIO;
  13929. *irq_toggled = 0;
  13930. goto exit;
  13931. }
  13932. /* Try sending a PIP command to see if we get a response */
  13933. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13934. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  13935. if (rc) {
  13936. /*
  13937. * Potential IRQ issue, no communication in App mode, attempt
  13938. * the same command in the BL
  13939. */
  13940. mode = PT_MODE_IGNORE;
  13941. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  13942. if (rc) {
  13943. pt_debug(dev, DL_ERROR,
  13944. "%s Failed to enter BL\n", __func__);
  13945. *irq_toggled = 0;
  13946. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13947. goto exit;
  13948. }
  13949. /*
  13950. * If original mode was operational and we successfully
  13951. * entered the BL, this will be useful info for the tp_xres
  13952. * test below.
  13953. */
  13954. if (mode == PT_MODE_OPERATIONAL)
  13955. *xres_toggled = 1;
  13956. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  13957. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  13958. &actual_read_len);
  13959. if (rc) {
  13960. /*
  13961. * Could not communicate in FW mode or BL mode. Save the
  13962. * error string, slim chance but the XRES test below may
  13963. * show the IRQ is actually working.
  13964. */
  13965. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  13966. pt_debug(dev, DL_ERROR,
  13967. "%s: request_active_pip_prot failed\n",
  13968. __func__);
  13969. *irq_toggled = 0;
  13970. goto exit;
  13971. }
  13972. }
  13973. if (cd->irq_count > 0) {
  13974. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  13975. cd->irq_count);
  13976. *bus_toggled = 1;
  13977. *irq_toggled = 1;
  13978. goto exit;
  13979. }
  13980. exit:
  13981. kfree(read_buf);
  13982. return rc;
  13983. }
  13984. /*******************************************************************************
  13985. * FUNCTION: pt_bist_xres_test
  13986. *
  13987. * SUMMARY: Tests the connectivity of the TP_XRES net
  13988. *
  13989. * This test will ensure there is a good connection between the host
  13990. * and the DUT on the tp_xres pin. The pin will be toggled to
  13991. * generate a TP reset which will cause the DUT to output a reset
  13992. * sentinel. If the reset sentinel is seen the test passes. If it is
  13993. * not seen the test will attempt to send a soft reset to simply gain
  13994. * some additional information on the failure:
  13995. * - soft reset fails to send: XRES and IRQ likely open
  13996. * - soft reset passes: XRES likely open or stuck de-asserted
  13997. * - soft reset fails: XRES likely stuck asserted
  13998. *
  13999. * RETURN:
  14000. * 0 = Success
  14001. * !0 = Failure
  14002. *
  14003. * PARAMETERS:
  14004. * *dev - pointer to device structure
  14005. * *bus_toggled - pointer to where to store if bus toggled
  14006. * *irq_toggled - pointer to where to store if IRQ toggled
  14007. * *xres_toggled - pointer to where to store if XRES toggled
  14008. * *err_str - pointer to error string buffer
  14009. ******************************************************************************/
  14010. static int pt_bist_xres_test(struct device *dev,
  14011. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14012. {
  14013. struct pt_core_data *cd = dev_get_drvdata(dev);
  14014. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14015. u8 *read_buf = NULL;
  14016. u8 mode = PT_MODE_UNKNOWN;
  14017. int rc = 0;
  14018. int t = 0;
  14019. int timeout = 300;
  14020. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14021. if (read_buf == NULL) {
  14022. rc = -ENOMEM;
  14023. goto exit;
  14024. }
  14025. /* Clear the startup bit mask, reset and enum will re-populate it */
  14026. cd->startup_status = STARTUP_STATUS_START;
  14027. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14028. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14029. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14030. rc = -ENODEV;
  14031. goto exit;
  14032. }
  14033. /* Ensure we have nothing pending on active bus */
  14034. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14035. /* Perform a hard XRES toggle and wait for reset sentinel */
  14036. mutex_lock(&cd->system_lock);
  14037. cd->hid_reset_cmd_state = 1;
  14038. mutex_unlock(&cd->system_lock);
  14039. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14040. rc = pt_hw_hard_reset(cd);
  14041. /* Set timeout to 1s for the flashless case where a BL could be done */
  14042. if (cd->flashless_dut)
  14043. timeout = 1000;
  14044. /*
  14045. * To avoid the case that next PIP command can be confused by BL/FW
  14046. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14047. * followed by "wakeup event" function can lower the failure rate.
  14048. */
  14049. t = wait_event_timeout(cd->wait_q,
  14050. ((cd->startup_status != STARTUP_STATUS_START)
  14051. && (cd->hid_reset_cmd_state == 0)),
  14052. msecs_to_jiffies(timeout));
  14053. if (IS_TMO(t)) {
  14054. pt_debug(cd->dev, DL_ERROR,
  14055. "%s: TMO waiting for sentinel\n", __func__);
  14056. *xres_toggled = 0;
  14057. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14058. /*
  14059. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14060. * To enter the BL we need to generate an XRES so first try to
  14061. * launch the applicaiton
  14062. */
  14063. if (cd->mode == PT_MODE_BOOTLOADER)
  14064. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14065. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14066. __func__);
  14067. mode = PT_MODE_IGNORE;
  14068. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14069. if (rc) {
  14070. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14071. __func__);
  14072. *xres_toggled = 0;
  14073. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14074. goto exit;
  14075. } else {
  14076. /* Wait for the BL sentinel */
  14077. t = wait_event_timeout(cd->wait_q,
  14078. (cd->startup_status != STARTUP_STATUS_START),
  14079. msecs_to_jiffies(500));
  14080. if (IS_TMO(t)) {
  14081. pt_debug(cd->dev, DL_ERROR,
  14082. "%s: TMO waiting for BL sentinel\n",
  14083. __func__);
  14084. *xres_toggled = 0;
  14085. strlcpy(err_str,
  14086. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14087. rc = -ETIME;
  14088. goto exit;
  14089. }
  14090. }
  14091. }
  14092. mutex_lock(&cd->system_lock);
  14093. cd->hid_reset_cmd_state = 0;
  14094. mutex_unlock(&cd->system_lock);
  14095. /* Look for BL or FW reset sentinels */
  14096. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14097. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14098. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14099. /* If a sentinel was seen, all nets are working */
  14100. *xres_toggled = 1;
  14101. *irq_toggled = 1;
  14102. /*
  14103. * For SPI test, bus read result can be confused as FW sentinel
  14104. * if MISO(slave) is connected to MISO(host).
  14105. */
  14106. if (cd->bus_ops->bustype == BUS_I2C)
  14107. *bus_toggled = 1;
  14108. } else {
  14109. /*
  14110. * Hard reset failed, however some additional information
  14111. * could be determined. Try a soft reset to see if DUT resets
  14112. * with the possible outcomes:
  14113. * - if it resets the line is not stuck asserted
  14114. * - if it does not reset the line could be stuck asserted
  14115. */
  14116. *xres_toggled = 0;
  14117. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14118. msleep(30);
  14119. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14120. __func__, rc);
  14121. if (rc) {
  14122. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14123. pt_debug(dev, DL_ERROR,
  14124. "%s: Hard reset failed, soft reset failed %s\n",
  14125. __func__, err_str);
  14126. goto exit;
  14127. }
  14128. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14129. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14130. strlcpy(err_str,
  14131. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14132. pt_debug(dev, DL_ERROR,
  14133. "%s: Hard reset failed, soft reset passed-%s\n",
  14134. __func__, err_str);
  14135. } else if (cd->startup_status == 0) {
  14136. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14137. pt_debug(dev, DL_ERROR,
  14138. "%s: Hard reset failed, soft reset failed-%s\n",
  14139. __func__, err_str);
  14140. } else {
  14141. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14142. pt_debug(dev, DL_ERROR,
  14143. "%s: Hard and Soft reset failed - %s\n",
  14144. __func__, err_str);
  14145. }
  14146. }
  14147. exit:
  14148. kfree(read_buf);
  14149. return rc;
  14150. }
  14151. /*******************************************************************************
  14152. * FUNCTION: pt_bist_slave_irq_test
  14153. *
  14154. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14155. *
  14156. * This test will ensure there is a good IRQ connection between the master
  14157. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14158. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14159. * saw each slave trigger the IRQ with it's reset sentinel.
  14160. *
  14161. * RETURN:
  14162. * 0 = Success
  14163. * !0 = Failure
  14164. *
  14165. * PARAMETERS:
  14166. * *dev - pointer to device structure
  14167. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14168. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14169. * *err_str - pointer to error string buffer
  14170. * *slave_detect - pointer to slave detect buffer
  14171. * *boot_err - pointer to boot_err buffer
  14172. ******************************************************************************/
  14173. static int pt_bist_slave_irq_test(struct device *dev,
  14174. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14175. u8 *slave_detect, u8 *boot_err)
  14176. {
  14177. struct pt_core_data *cd = dev_get_drvdata(dev);
  14178. u8 mode = PT_MODE_UNKNOWN;
  14179. u8 status;
  14180. u8 boot;
  14181. u8 read_buf[12];
  14182. u8 detected = 0;
  14183. u8 last_err = -1;
  14184. u16 actual_read_len;
  14185. int result = 0;
  14186. int rc = 0;
  14187. /*
  14188. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14189. * detect bits. If the DUT was in FW, entering the BL will cause an
  14190. * XRES signal which will inadvertently test the XRES net as well
  14191. */
  14192. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14193. if (rc) {
  14194. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14195. __func__, rc);
  14196. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14197. goto exit;
  14198. }
  14199. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14200. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14201. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14202. &actual_read_len);
  14203. if (!rc) {
  14204. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14205. "PIP2 STATUS");
  14206. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14207. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14208. /* Slave detect is only valid if status ok and in boot exec */
  14209. if (status == PIP2_RSP_ERR_NONE &&
  14210. boot == PIP2_STATUS_BOOT_EXEC) {
  14211. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14212. SLAVE_DETECT_MASK;
  14213. } else {
  14214. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14215. rc = -EPERM;
  14216. }
  14217. } else {
  14218. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14219. __func__);
  14220. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14221. goto exit;
  14222. }
  14223. /*
  14224. * Retrieve boot error regardless of the state of the slave detect
  14225. * bit because the IRQ could have been stuck high or low.
  14226. */
  14227. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14228. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14229. read_buf, &actual_read_len);
  14230. if (!rc) {
  14231. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14232. "PIP2 GET_LAST_ERRNO");
  14233. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14234. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14235. if (last_err) {
  14236. pt_debug(cd->dev, DL_ERROR,
  14237. "%s: Master Boot Last Err = 0x%02X\n",
  14238. __func__, last_err);
  14239. }
  14240. } else {
  14241. pt_debug(cd->dev, DL_ERROR,
  14242. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14243. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14244. }
  14245. exit:
  14246. pt_debug(cd->dev, DL_INFO,
  14247. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14248. __func__, rc, detected, last_err);
  14249. if (err_str && last_err) {
  14250. if (detected)
  14251. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14252. "- Likely stuck low. Boot Error:",
  14253. last_err);
  14254. else
  14255. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14256. "- Likely stuck high. Boot Error:",
  14257. last_err);
  14258. }
  14259. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14260. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14261. last_err = PIP2_RSP_ERR_NONE;
  14262. if (slave_irq_toggled)
  14263. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14264. if (slave_bus_toggled) {
  14265. /* Leave as UNTEST if slave not detected */
  14266. if (detected)
  14267. *slave_bus_toggled = !last_err ? true : false;
  14268. }
  14269. if (slave_detect)
  14270. *slave_detect = detected;
  14271. if (boot_err)
  14272. *boot_err = last_err;
  14273. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14274. __func__,
  14275. "Detected", detected,
  14276. "slave_irq_toggled", *slave_irq_toggled,
  14277. "slave_bus_toggled", *slave_bus_toggled);
  14278. return rc;
  14279. }
  14280. /*******************************************************************************
  14281. * FUNCTION: pt_bist_slave_xres_test
  14282. *
  14283. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14284. *
  14285. * This test will ensure there is a good TP_XRES connection between the
  14286. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14287. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14288. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14289. * it's reset sentinel.
  14290. *
  14291. * RETURN:
  14292. * 0 = Success
  14293. * !0 = Failure
  14294. *
  14295. * PARAMETERS:
  14296. * *dev - pointer to device structure
  14297. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14298. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14299. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14300. * *err_str - pointer to error string buffer
  14301. ******************************************************************************/
  14302. static int pt_bist_slave_xres_test(struct device *dev,
  14303. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14304. u8 *err_str)
  14305. {
  14306. struct pt_core_data *cd = dev_get_drvdata(dev);
  14307. u8 slave_detect = 0;
  14308. u8 boot_err = 0;
  14309. int rc = 0;
  14310. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14311. mutex_lock(&cd->system_lock);
  14312. cd->hid_reset_cmd_state = 1;
  14313. mutex_unlock(&cd->system_lock);
  14314. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14315. pt_hw_hard_reset(cd);
  14316. msleep(100);
  14317. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14318. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14319. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14320. if (!rc && *slave_irq_toggled == false) {
  14321. /*
  14322. * If the slave IRQ did not toggle, either the slave_detect
  14323. * bit was not set or we had a boot error. If the slave
  14324. * detect was not set the slave did not reset causing a boot
  14325. * error.
  14326. */
  14327. if (!slave_detect)
  14328. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14329. else
  14330. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14331. "- likely open or an IRQ issue. Boot Error:",
  14332. boot_err);
  14333. }
  14334. if (slave_xres_toggled) {
  14335. if (!rc)
  14336. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14337. else
  14338. *slave_xres_toggled = false;
  14339. }
  14340. return rc;
  14341. }
  14342. /*******************************************************************************
  14343. * FUNCTION: pt_bist_slave_bus_test
  14344. *
  14345. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14346. *
  14347. * This test will ensure a good SPI bus connection between the
  14348. * master DUT and the slave DUT(s). This bus connection is ensured by
  14349. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14350. * then there is a master/slave communication issue. Opening file 0 on
  14351. * the master will open it on the slave as well if the slave was detected.
  14352. *
  14353. * RETURN:
  14354. * 0 = Success
  14355. * !0 = Failure
  14356. *
  14357. * PARAMETERS:
  14358. * *dev - pointer to device structure
  14359. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14360. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14361. * *err_str - pointer to error string buffer
  14362. ******************************************************************************/
  14363. static int pt_bist_slave_bus_test(struct device *dev,
  14364. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14365. {
  14366. struct pt_core_data *cd = dev_get_drvdata(dev);
  14367. u8 mode = PT_MODE_UNKNOWN;
  14368. u8 bus_toggled = false;
  14369. u8 file_handle;
  14370. int result = 0;
  14371. int rc = 0;
  14372. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14373. if (rc) {
  14374. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14375. __func__, rc);
  14376. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14377. goto exit;
  14378. }
  14379. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14380. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14381. if (file_handle != PIP2_RAM_FILE) {
  14382. rc = -ENOENT;
  14383. bus_toggled = false;
  14384. pt_debug(dev, DL_ERROR,
  14385. "%s Failed to open bin file\n", __func__);
  14386. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14387. goto exit;
  14388. } else {
  14389. bus_toggled = true;
  14390. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14391. pt_debug(dev, DL_ERROR,
  14392. "%s: File Close failed, file_handle=%d\n",
  14393. __func__, file_handle);
  14394. }
  14395. }
  14396. exit:
  14397. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14398. if (slave_irq_toggled)
  14399. *slave_irq_toggled = bus_toggled;
  14400. if (slave_bus_toggled)
  14401. *slave_bus_toggled = bus_toggled;
  14402. return rc;
  14403. }
  14404. /*******************************************************************************
  14405. * FUNCTION: pt_ttdl_bist_show
  14406. *
  14407. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14408. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14409. *
  14410. * NOTE: This function will reset the DUT and the startup_status bit
  14411. * mask. A pt_enum will be queued after completion.
  14412. *
  14413. * NOTE: The order of the net tests is done to optimize the time it takes
  14414. * to run. The first test is capable of verifying all nets, each subsequent
  14415. * test is only run if the previous was not able to see all nets toggle.
  14416. *
  14417. * RETURN:
  14418. * 0 = success
  14419. * !0 = failure
  14420. *
  14421. * PARAMETERS:
  14422. * *dev - pointer to device structure
  14423. * *attr - pointer to device attributes
  14424. * *buf - pointer to output buffer
  14425. ******************************************************************************/
  14426. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14427. struct device_attribute *attr, char *buf)
  14428. {
  14429. struct pt_core_data *cd = dev_get_drvdata(dev);
  14430. ssize_t ret;
  14431. char *bus_err_str = NULL;
  14432. char *irq_err_str = NULL;
  14433. char *xres_err_str = NULL;
  14434. char *slave_bus_err_str = NULL;
  14435. char *slave_irq_err_str = NULL;
  14436. char *slave_xres_err_str = NULL;
  14437. u8 tests;
  14438. int rc = 0;
  14439. int num_tests = 0;
  14440. int status = 1; /* 0 = Pass, !0 = fail */
  14441. u8 bus_toggled = 0x0F; /* default to untested */
  14442. u8 i2c_toggled = 0x0F; /* default to untested */
  14443. u8 spi_toggled = 0x0F; /* default to untested */
  14444. u8 irq_toggled = 0x0F; /* default to untested */
  14445. u8 xres_toggled = 0x0F; /* default to untested */
  14446. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14447. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14448. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14449. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14450. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14451. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14452. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14453. goto print_results;
  14454. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14455. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14456. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14457. if (cd->multi_chip) {
  14458. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14459. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14460. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14461. if (!slave_bus_err_str ||
  14462. !slave_irq_err_str ||
  14463. !slave_xres_err_str)
  14464. goto print_results;
  14465. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14466. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14467. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14468. }
  14469. /* Turn off the TTDL WD during the test */
  14470. pt_stop_wd_timer(cd);
  14471. /* Shorten default PIP cmd timeout while running BIST */
  14472. cd->pip_cmd_timeout = 200;
  14473. /* Count the number of tests to run */
  14474. tests = cd->ttdl_bist_select;
  14475. while (tests) {
  14476. num_tests += tests & 1;
  14477. tests >>= 1;
  14478. }
  14479. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14480. __func__, cd->ttdl_bist_select, num_tests);
  14481. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14482. if (cd->flashless_dut) {
  14483. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14484. __func__);
  14485. mutex_lock(&cd->system_lock);
  14486. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14487. mutex_unlock(&cd->system_lock);
  14488. }
  14489. /* --------------- TP_XRES BIST TEST --------------- */
  14490. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14491. pt_debug(dev, DL_INFO,
  14492. "%s: ----- Start TP_XRES BIST -----", __func__);
  14493. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14494. &xres_toggled, xres_err_str);
  14495. /* Done if the rest of all nets toggled */
  14496. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14497. goto host_nets_complete;
  14498. }
  14499. /* Flush bus in case a PIP response is waiting from previous test */
  14500. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14501. /* --------------- IRQ BIST TEST --------------- */
  14502. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14503. pt_debug(dev, DL_INFO,
  14504. "%s: ----- Start IRQ BIST -----", __func__);
  14505. bus_toggled = 0xFF;
  14506. irq_toggled = 0xFF;
  14507. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14508. &xres_toggled, irq_err_str);
  14509. /* If this net failed clear results from previous net */
  14510. if (irq_toggled != 1) {
  14511. xres_toggled = 0x0F;
  14512. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14513. }
  14514. if (bus_toggled == 1 && irq_toggled == 1)
  14515. goto host_nets_complete;
  14516. }
  14517. /* Flush bus in case a PIP response is waiting from previous test */
  14518. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14519. /* --------------- BUS BIST TEST --------------- */
  14520. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14521. pt_debug(dev, DL_INFO,
  14522. "%s: ----- Start BUS BIST -----", __func__);
  14523. bus_toggled = 0xFF;
  14524. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14525. /* If this net failed clear results from previous net */
  14526. if (bus_toggled == 0) {
  14527. irq_toggled = 0x0F;
  14528. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14529. }
  14530. }
  14531. host_nets_complete:
  14532. /* --------------- SLAVE XRES BIST TEST --------------- */
  14533. if (cd->multi_chip &&
  14534. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14535. pt_debug(dev, DL_INFO,
  14536. "%s: ----- Start Slave XRES BIST -----", __func__);
  14537. slave_xres_toggled = 0xFF;
  14538. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14539. &slave_bus_toggled, &slave_xres_toggled,
  14540. slave_xres_err_str);
  14541. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14542. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14543. goto print_results;
  14544. }
  14545. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14546. if (cd->multi_chip &&
  14547. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14548. pt_debug(dev, DL_INFO,
  14549. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14550. slave_irq_toggled = 0xFF;
  14551. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14552. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14553. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14554. __func__, slave_irq_toggled);
  14555. if (slave_irq_toggled == 1) {
  14556. slave_bus_toggled = 1;
  14557. goto print_results;
  14558. }
  14559. }
  14560. /* --------------- SLAVE BUS BIST TEST --------------- */
  14561. if (cd->multi_chip &&
  14562. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14563. pt_debug(dev, DL_INFO,
  14564. "%s: ----- Start Slave BUS BIST -----", __func__);
  14565. slave_bus_toggled = 0xFF;
  14566. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14567. &slave_bus_toggled, slave_bus_err_str);
  14568. }
  14569. print_results:
  14570. /* Restore PIP command timeout */
  14571. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14572. /*
  14573. * We're done! - Perform a hard XRES toggle, allowing BL
  14574. * to load FW if there is any in Flash
  14575. */
  14576. mutex_lock(&cd->system_lock);
  14577. cd->hid_reset_cmd_state = 0;
  14578. mutex_unlock(&cd->system_lock);
  14579. pt_debug(dev, DL_INFO,
  14580. "%s: TTDL BIST Complete - Final reset\n", __func__);
  14581. if (cd->flashless_dut) {
  14582. /*
  14583. * For Flashless solution, FW update is triggered after BL is
  14584. * seen that several miliseconds delay is needed.
  14585. */
  14586. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  14587. __func__);
  14588. mutex_lock(&cd->system_lock);
  14589. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14590. mutex_unlock(&cd->system_lock);
  14591. /* Reset DUT and wait 100ms to see if loader started */
  14592. pt_hw_hard_reset(cd);
  14593. msleep(100);
  14594. if (cd->fw_updating) {
  14595. pt_debug(dev, DL_INFO,
  14596. "%s: ----- BIST Wait FW Loading ----",
  14597. __func__);
  14598. rc = _pt_request_wait_for_enum_state(
  14599. dev, 4000, STARTUP_STATUS_COMPLETE);
  14600. }
  14601. } else {
  14602. if (cd->mode == PT_MODE_BOOTLOADER) {
  14603. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14604. if (rc) {
  14605. pt_debug(dev, DL_ERROR,
  14606. "%s Failed to launch app\n", __func__);
  14607. rc = pt_hw_hard_reset(cd);
  14608. }
  14609. }
  14610. /*
  14611. * If FW exists the BL may have just started or will start soon,
  14612. * so the FW sentinel may be on it's way but with no FW it will
  14613. * not arrive, wait for it before deciding if we need to queue
  14614. * an enum.
  14615. */
  14616. rc = _pt_request_wait_for_enum_state(
  14617. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  14618. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  14619. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  14620. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  14621. __func__);
  14622. pt_queue_enum(cd);
  14623. rc = _pt_request_wait_for_enum_state(
  14624. dev, 2000, STARTUP_STATUS_COMPLETE);
  14625. }
  14626. }
  14627. msleep(20);
  14628. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  14629. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  14630. pt_start_wd_timer(cd);
  14631. /* Canned print if any memory allocation issues */
  14632. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  14633. ret = scnprintf(buf, strlen(buf),
  14634. "Status: %d\n"
  14635. "I2C (SDA,SCL): [UNTEST]\n"
  14636. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14637. "IRQ connection: [UNTEST]\n"
  14638. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  14639. if (cd->multi_chip) {
  14640. ret += scnprintf(buf + ret, strlen(buf),
  14641. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14642. "I/P IRQ connection: [UNTEST]\n"
  14643. "I/P TP_XRES connection: [UNTEST]\n");
  14644. }
  14645. } else {
  14646. status = 0;
  14647. if (bus_toggled == 1)
  14648. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14649. if (irq_toggled == 1)
  14650. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14651. if (xres_toggled == 1)
  14652. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14653. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  14654. status += bus_toggled;
  14655. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  14656. status += irq_toggled;
  14657. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  14658. status += xres_toggled;
  14659. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  14660. __func__, status, bus_toggled, irq_toggled,
  14661. xres_toggled);
  14662. if (cd->multi_chip) {
  14663. if (slave_irq_toggled == 1)
  14664. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14665. if (slave_xres_toggled == 1)
  14666. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14667. if (slave_bus_toggled == 1)
  14668. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14669. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  14670. status += slave_bus_toggled;
  14671. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  14672. status += slave_irq_toggled;
  14673. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  14674. status += slave_xres_toggled;
  14675. pt_debug(dev, DL_ERROR,
  14676. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  14677. __func__, status, bus_toggled, irq_toggled,
  14678. xres_toggled, slave_bus_toggled,
  14679. slave_irq_toggled, slave_xres_toggled);
  14680. }
  14681. if (cd->bus_ops->bustype == BUS_I2C)
  14682. i2c_toggled = bus_toggled;
  14683. else
  14684. spi_toggled = bus_toggled;
  14685. ret = scnprintf(buf, strlen(buf),
  14686. "Status: %d\n"
  14687. "I2C (SDA,SCL): %s %s\n"
  14688. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14689. "IRQ connection: %s %s\n"
  14690. "TP_XRES connection: %s %s\n",
  14691. status == num_tests ? 0 : 1,
  14692. i2c_toggled == 0x0F ? "[UNTEST]" :
  14693. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14694. i2c_toggled == 0x0F ? "" : bus_err_str,
  14695. spi_toggled == 0x0F ? "[UNTEST]" :
  14696. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14697. spi_toggled == 0x0F ? "" : bus_err_str,
  14698. irq_toggled == 0x0F ? "[UNTEST]" :
  14699. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14700. irq_err_str,
  14701. xres_toggled == 0x0F ? "[UNTEST]" :
  14702. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  14703. xres_err_str);
  14704. if (cd->multi_chip) {
  14705. ret += scnprintf(buf + ret, strlen(buf),
  14706. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  14707. "I/P IRQ connection: %s %s\n"
  14708. "I/P TP_XRES connection: %s %s\n",
  14709. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  14710. slave_bus_toggled == 1 ? "[ OK ]" :
  14711. "[FAILED]", slave_bus_err_str,
  14712. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  14713. slave_irq_toggled == 1 ? "[ OK ]" :
  14714. "[FAILED]", slave_irq_err_str,
  14715. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  14716. slave_xres_toggled == 1 ? "[ OK ]" :
  14717. "[FAILED]", slave_xres_err_str);
  14718. }
  14719. }
  14720. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  14721. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  14722. __func__, cd->startup_status);
  14723. kfree(bus_err_str);
  14724. kfree(irq_err_str);
  14725. kfree(xres_err_str);
  14726. return ret;
  14727. }
  14728. /*******************************************************************************
  14729. * FUNCTION: pt_ttdl_bist_store
  14730. *
  14731. * SUMMARY: Store method for the ttdl_bist sysfs node.
  14732. *
  14733. * RETURN: Size of passed in buffer if successful
  14734. *
  14735. * PARAMETERS:
  14736. * *dev - pointer to device structure
  14737. * *attr - pointer to device attributes
  14738. * *buf - pointer to command buffer
  14739. * size - size of buf
  14740. ******************************************************************************/
  14741. static ssize_t pt_ttdl_bist_store(struct device *dev,
  14742. struct device_attribute *attr, const char *buf, size_t size)
  14743. {
  14744. struct pt_core_data *cd = dev_get_drvdata(dev);
  14745. u32 input_data[2] = {0};
  14746. int length;
  14747. int rc = 0;
  14748. /* Maximum input of one value */
  14749. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14750. ARRAY_SIZE(input_data));
  14751. if (length != 1) {
  14752. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14753. __func__);
  14754. rc = -EINVAL;
  14755. goto exit;
  14756. } else {
  14757. mutex_lock(&cd->system_lock);
  14758. cd->ttdl_bist_select = input_data[0];
  14759. mutex_unlock(&cd->system_lock);
  14760. }
  14761. exit:
  14762. if (rc)
  14763. return rc;
  14764. return size;
  14765. }
  14766. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  14767. pt_ttdl_bist_store);
  14768. /*******************************************************************************
  14769. * FUNCTION: pt_flush_bus_store
  14770. *
  14771. * SUMMARY: Store method for the flush_bus sysfs node.
  14772. *
  14773. * RETURN: Size of passed in buffer if successful
  14774. *
  14775. * PARAMETERS:
  14776. * *dev - pointer to device structure
  14777. * *attr - pointer to device attributes
  14778. * *buf - pointer to command buffer
  14779. * size - size of buf
  14780. ******************************************************************************/
  14781. static ssize_t pt_flush_bus_store(struct device *dev,
  14782. struct device_attribute *attr, const char *buf, size_t size)
  14783. {
  14784. struct pt_core_data *cd = dev_get_drvdata(dev);
  14785. u32 input_data[2] = {0};
  14786. int length;
  14787. int rc = 0;
  14788. /* Maximum input of one value */
  14789. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14790. ARRAY_SIZE(input_data));
  14791. if (length != 1) {
  14792. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14793. __func__);
  14794. rc = -EINVAL;
  14795. goto exit;
  14796. }
  14797. mutex_lock(&cd->system_lock);
  14798. if (input_data[0] == 0)
  14799. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  14800. else if (input_data[0] == 1)
  14801. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  14802. else
  14803. rc = -EINVAL;
  14804. mutex_unlock(&cd->system_lock);
  14805. exit:
  14806. if (rc)
  14807. return rc;
  14808. return size;
  14809. }
  14810. /*******************************************************************************
  14811. * FUNCTION: pt_flush_bus_show
  14812. *
  14813. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  14814. * based on either the size of the first two bytes or a blind 256 bytes.
  14815. *
  14816. * RETURN:
  14817. * 0 = success
  14818. * !0 = failure
  14819. *
  14820. * PARAMETERS:
  14821. * *dev - pointer to device structure
  14822. * *attr - pointer to device attributes
  14823. * *buf - pointer to output buffer
  14824. ******************************************************************************/
  14825. static ssize_t pt_flush_bus_show(struct device *dev,
  14826. struct device_attribute *attr, char *buf)
  14827. {
  14828. ssize_t ret = 0;
  14829. ssize_t bytes = 0;
  14830. struct pt_core_data *cd = dev_get_drvdata(dev);
  14831. mutex_lock(&cd->system_lock);
  14832. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  14833. ret = scnprintf(buf, strlen(buf),
  14834. "Status: 0\n"
  14835. "%s: %zd\n",
  14836. "Bytes flushed", bytes);
  14837. mutex_unlock(&cd->system_lock);
  14838. return ret;
  14839. }
  14840. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  14841. pt_flush_bus_store);
  14842. /*******************************************************************************
  14843. * FUNCTION: pt_pip2_ping_test_store
  14844. *
  14845. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  14846. *
  14847. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  14848. * someone may want to test sending invalid packet lengths so any values
  14849. * up to 255 are allowed.
  14850. *
  14851. * RETURN: Size of passed in buffer if successful
  14852. *
  14853. * PARAMETERS:
  14854. * *dev - pointer to device structure
  14855. * *attr - pointer to device attributes
  14856. * *buf - pointer to command buffer
  14857. * size - size of buf
  14858. ******************************************************************************/
  14859. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  14860. struct device_attribute *attr, const char *buf, size_t size)
  14861. {
  14862. struct pt_core_data *cd = dev_get_drvdata(dev);
  14863. u32 input_data[2];
  14864. int length;
  14865. int rc = 0;
  14866. /* Maximum input of one value */
  14867. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14868. ARRAY_SIZE(input_data));
  14869. if (length != 1) {
  14870. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14871. __func__);
  14872. rc = -EINVAL;
  14873. goto exit;
  14874. }
  14875. mutex_lock(&cd->system_lock);
  14876. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  14877. cd->ping_test_size = input_data[0];
  14878. else
  14879. rc = -EINVAL;
  14880. mutex_unlock(&cd->system_lock);
  14881. exit:
  14882. if (rc)
  14883. return rc;
  14884. return size;
  14885. }
  14886. /*******************************************************************************
  14887. * FUNCTION: pt_ping_test_show
  14888. *
  14889. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  14890. * PING command and ramps up the optional payload from 0 to
  14891. * ping_test_size.
  14892. * The max payload size is 247:
  14893. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  14894. *
  14895. * RETURN:
  14896. * 0 = success
  14897. * !0 = failure
  14898. *
  14899. * PARAMETERS:
  14900. * *dev - pointer to device structure
  14901. * *attr - pointer to device attributes
  14902. * *buf - pointer to output buffer
  14903. ******************************************************************************/
  14904. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  14905. struct device_attribute *attr, char *buf)
  14906. {
  14907. struct pt_core_data *cd = dev_get_drvdata(dev);
  14908. ssize_t ret;
  14909. int last_packet_size;
  14910. int rc = 0;
  14911. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  14912. if (rc) {
  14913. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  14914. return ret;
  14915. }
  14916. ret = scnprintf(buf, strlen(buf),
  14917. "Status: %d\n"
  14918. "PING payload test passed with packet sizes 0 - %d\n",
  14919. (last_packet_size == cd->ping_test_size ? 0 : 1),
  14920. last_packet_size);
  14921. return ret;
  14922. }
  14923. /*******************************************************************************
  14924. * FUNCTION: pt_t_refresh_store
  14925. *
  14926. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  14927. * in integer as the number of interrupts to count. A timer is started to
  14928. * calculate the total time it takes to see that number of interrupts.
  14929. *
  14930. * RETURN: Size of passed in buffer if successful
  14931. *
  14932. * PARAMETERS:
  14933. * *dev - pointer to device structure
  14934. * *attr - pointer to device attributes
  14935. * *buf - pointer to buffer that hold the command parameters
  14936. * size - size of buf
  14937. ******************************************************************************/
  14938. static ssize_t pt_t_refresh_store(struct device *dev,
  14939. struct device_attribute *attr, const char *buf, size_t size)
  14940. {
  14941. struct pt_core_data *cd = dev_get_drvdata(dev);
  14942. u32 input_data[2];
  14943. int length;
  14944. int rc = 0;
  14945. /* Maximum input of one value */
  14946. length = _pt_ic_parse_input(dev, buf, size, input_data,
  14947. ARRAY_SIZE(input_data));
  14948. if (length != 1) {
  14949. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  14950. __func__);
  14951. rc = -EINVAL;
  14952. goto exit;
  14953. }
  14954. mutex_lock(&cd->system_lock);
  14955. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  14956. input_data[0]);
  14957. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  14958. cd->t_refresh_total = input_data[0];
  14959. cd->t_refresh_count = 0;
  14960. cd->t_refresh_active = 1;
  14961. } else {
  14962. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  14963. rc = -EINVAL;
  14964. }
  14965. mutex_unlock(&cd->system_lock);
  14966. exit:
  14967. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  14968. if (rc)
  14969. return rc;
  14970. return size;
  14971. }
  14972. /*******************************************************************************
  14973. * FUNCTION: pt_t_refresh_show
  14974. *
  14975. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  14976. * of the T-Refresh timer counting the time it takes to see a user defined
  14977. * number of interrupts.
  14978. *
  14979. * RETURN:
  14980. * 0 = success
  14981. * !0 = failure
  14982. *
  14983. * PARAMETERS:
  14984. * *dev - pointer to device structure
  14985. * *attr - pointer to device attributes
  14986. * *buf - pointer to output buffer
  14987. ******************************************************************************/
  14988. static ssize_t pt_t_refresh_show(struct device *dev,
  14989. struct device_attribute *attr, char *buf)
  14990. {
  14991. ssize_t ret = 0;
  14992. struct pt_core_data *cd = dev_get_drvdata(dev);
  14993. u32 whole;
  14994. u16 fraction;
  14995. mutex_lock(&cd->system_lock);
  14996. /* Check if we have counted the number requested */
  14997. if (cd->t_refresh_count != cd->t_refresh_total) {
  14998. ret = scnprintf(buf, strlen(buf),
  14999. "Status: 0\n"
  15000. "%s: %d\n",
  15001. "Still counting... current IRQ count",
  15002. cd->t_refresh_count);
  15003. } else {
  15004. /* Ensure T-Refresh is de-activated */
  15005. cd->t_refresh_active = 0;
  15006. whole = cd->t_refresh_time / cd->t_refresh_count;
  15007. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15008. fraction = fraction * 1000 / cd->t_refresh_count;
  15009. ret = scnprintf(buf, strlen(buf),
  15010. "Status: 0\n"
  15011. "%s: %d\n"
  15012. "%s: %d\n"
  15013. "%s: %d\n"
  15014. "%s: %d.%02d\n",
  15015. "Requested IRQ Count ", cd->t_refresh_total,
  15016. "IRQ Counted ", cd->t_refresh_count,
  15017. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15018. "Average T-Refresh (ms) ", whole, fraction);
  15019. }
  15020. mutex_unlock(&cd->system_lock);
  15021. return ret;
  15022. }
  15023. /*******************************************************************************
  15024. * FUNCTION: pt_dut_status_show
  15025. *
  15026. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15027. * more items such as operation mode,easywake state are added in the future.
  15028. *
  15029. * RETURN: Char buffer with printed scan status information
  15030. *
  15031. * PARAMETERS:
  15032. * *dev - pointer to device structure
  15033. * *attr - pointer to device attributes
  15034. * *buf - pointer to output buffer
  15035. ******************************************************************************/
  15036. static ssize_t pt_dut_status_show(struct device *dev,
  15037. struct device_attribute *attr, char *buf)
  15038. {
  15039. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15040. u8 mode = PT_MODE_UNKNOWN;
  15041. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15042. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15043. struct pt_core_data *cd = dev_get_drvdata(dev);
  15044. ssize_t ret;
  15045. u16 calculated_crc = 0;
  15046. u16 stored_crc = 0;
  15047. u8 status;
  15048. int rc = 0;
  15049. /* In STANDBY the DUT will not repond to any PIP cmd */
  15050. if (cd->fw_sys_mode_in_standby_state) {
  15051. mode = PT_MODE_OPERATIONAL;
  15052. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15053. goto print_limited_results;
  15054. }
  15055. /* Retrieve mode and FW system mode which can only be 0-4 */
  15056. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15057. if (rc || mode == PT_MODE_UNKNOWN) {
  15058. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15059. "%s: %d\n"
  15060. "%s: n/a\n"
  15061. "%s: n/a\n"
  15062. "%s: n/a\n"
  15063. "%s: n/a\n",
  15064. "Status", rc,
  15065. "Active Exec ",
  15066. "FW System Mode ",
  15067. "Stored CRC ",
  15068. "Calculated CRC ");
  15069. return ret;
  15070. } else {
  15071. if (mode == PT_MODE_OPERATIONAL) {
  15072. if (sys_mode > FW_SYS_MODE_MAX)
  15073. sys_mode = FW_SYS_MODE_UNDEFINED;
  15074. if (sys_mode != FW_SYS_MODE_TEST)
  15075. goto print_limited_results;
  15076. rc = pt_pip_verify_config_block_crc_(cd,
  15077. PT_TCH_PARM_EBID, &status,
  15078. &calculated_crc, &stored_crc);
  15079. if (rc)
  15080. goto print_limited_results;
  15081. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15082. "%s: %d\n"
  15083. "%s: %s\n"
  15084. "%s: %s\n"
  15085. "%s: 0x%04X\n"
  15086. "%s: 0x%04X\n",
  15087. "Status", rc,
  15088. "Active Exec ", "FW",
  15089. "FW System Mode ", outputstring[sys_mode],
  15090. "Stored CRC ", stored_crc,
  15091. "Calculated CRC ", calculated_crc);
  15092. return ret;
  15093. } else {
  15094. /* When in BL or unknon mode Active Exec is "n/a" */
  15095. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15096. }
  15097. }
  15098. print_limited_results:
  15099. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15100. "%s: %d\n"
  15101. "%s: %s\n"
  15102. "%s: %s\n"
  15103. "%s: n/a\n"
  15104. "%s: n/a\n",
  15105. "Status", rc,
  15106. "Active Exec ",
  15107. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15108. "FW System Mode ", outputstring[sys_mode],
  15109. "Stored CRC ",
  15110. "Calculated CRC ");
  15111. return ret;
  15112. }
  15113. #endif /* TTDL_DIAGNOSTICS */
  15114. /*******************************************************************************
  15115. * Structures of sysfs attributes for all DUT dependent sysfs node
  15116. ******************************************************************************/
  15117. static struct attribute *early_attrs[] = {
  15118. &dev_attr_hw_version.attr,
  15119. &dev_attr_drv_version.attr,
  15120. &dev_attr_drv_ver.attr,
  15121. &dev_attr_fw_version.attr,
  15122. &dev_attr_sysinfo.attr,
  15123. &dev_attr_pip2_cmd_rsp.attr,
  15124. &dev_attr_command.attr,
  15125. &dev_attr_drv_debug.attr,
  15126. &dev_attr_hw_reset.attr,
  15127. &dev_attr_response.attr,
  15128. &dev_attr_ttdl_restart.attr,
  15129. #ifdef TTDL_DIAGNOSTICS
  15130. &dev_attr_ttdl_status.attr,
  15131. &dev_attr_pip2_enter_bl.attr,
  15132. &dev_attr_pip2_exit_bl.attr,
  15133. &dev_attr_err_gpio.attr,
  15134. &dev_attr_flush_bus.attr,
  15135. &dev_attr_ttdl_bist.attr,
  15136. #endif /* TTDL_DIAGNOSTICS */
  15137. NULL,
  15138. };
  15139. static struct attribute_group early_attr_group = {
  15140. .attrs = early_attrs,
  15141. };
  15142. static struct device_attribute pip2_attributes[] = {
  15143. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15144. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15145. #ifdef TTDL_DIAGNOSTICS
  15146. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15147. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15148. pt_pip2_ping_test_store),
  15149. #endif
  15150. };
  15151. static struct device_attribute attributes[] = {
  15152. __ATTR(dut_debug, 0644,
  15153. pt_dut_debug_show, pt_drv_debug_store),
  15154. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15155. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15156. __ATTR(get_param, 0644,
  15157. pt_get_param_show, pt_get_param_store),
  15158. #ifdef EASYWAKE_TSG6
  15159. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15160. pt_easy_wakeup_gesture_store),
  15161. __ATTR(easy_wakeup_gesture_id, 0444,
  15162. pt_easy_wakeup_gesture_id_show, NULL),
  15163. __ATTR(easy_wakeup_gesture_data, 0444,
  15164. pt_easy_wakeup_gesture_data_show, NULL),
  15165. #endif
  15166. #ifdef TTDL_DIAGNOSTICS
  15167. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15168. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15169. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15170. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15171. #endif /* TTDL_DIAGNOSTICS */
  15172. };
  15173. /*******************************************************************************
  15174. * FUNCTION: add_sysfs_interfaces
  15175. *
  15176. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15177. *
  15178. * RETURN:
  15179. * 0 = success
  15180. * !0 = failure
  15181. *
  15182. * PARAMETERS:
  15183. * *dev - pointer to device structure
  15184. ******************************************************************************/
  15185. static int add_sysfs_interfaces(struct device *dev)
  15186. {
  15187. int i;
  15188. int j = 0;
  15189. struct pt_core_data *cd = dev_get_drvdata(dev);
  15190. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15191. if (device_create_file(dev, attributes + i))
  15192. goto undo;
  15193. }
  15194. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15195. __func__, cd->active_dut_generation);
  15196. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15197. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15198. if (device_create_file(dev, pip2_attributes + j))
  15199. goto undo;
  15200. }
  15201. }
  15202. return 0;
  15203. undo:
  15204. for (i--; i >= 0; i--)
  15205. device_remove_file(dev, attributes + i);
  15206. for (j--; j >= 0; j--)
  15207. device_remove_file(dev, pip2_attributes + j);
  15208. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15209. __func__);
  15210. return -ENODEV;
  15211. }
  15212. /*******************************************************************************
  15213. * FUNCTION: remove_sysfs_interfaces
  15214. *
  15215. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15216. *
  15217. * RETURN: void
  15218. *
  15219. * PARAMETERS:
  15220. * *dev - pointer to device structure
  15221. ******************************************************************************/
  15222. static void remove_sysfs_interfaces(struct device *dev)
  15223. {
  15224. int i;
  15225. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15226. device_remove_file(dev, attributes + i);
  15227. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15228. device_remove_file(dev, pip2_attributes + i);
  15229. }
  15230. /*******************************************************************************
  15231. * FUNCTION: remove_sysfs_and_modules
  15232. *
  15233. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15234. *
  15235. * RETURN: void
  15236. *
  15237. * PARAMETERS:
  15238. * *dev - pointer to device structure
  15239. ******************************************************************************/
  15240. static void remove_sysfs_and_modules(struct device *dev)
  15241. {
  15242. struct pt_core_data *cd = dev_get_drvdata(dev);
  15243. /* Queued work should be removed before to release loader module */
  15244. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15245. pt_release_modules(cd);
  15246. pt_btn_release(dev);
  15247. pt_mt_release(dev);
  15248. remove_sysfs_interfaces(dev);
  15249. }
  15250. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15251. {
  15252. int retval;
  15253. /* Get pinctrl if target uses pinctrl */
  15254. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15255. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15256. retval = PTR_ERR(cd->ts_pinctrl);
  15257. dev_dbg(cd->dev,
  15258. "Target does not use pinctrl %d\n", retval);
  15259. goto err_pinctrl_get;
  15260. }
  15261. cd->pinctrl_state_active
  15262. = pinctrl_lookup_state(cd->ts_pinctrl,
  15263. PINCTRL_STATE_ACTIVE);
  15264. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15265. retval = PTR_ERR(cd->pinctrl_state_active);
  15266. dev_err(cd->dev,
  15267. "Can not lookup %s pinstate %d\n",
  15268. PINCTRL_STATE_ACTIVE, retval);
  15269. goto err_pinctrl_lookup;
  15270. }
  15271. cd->pinctrl_state_suspend
  15272. = pinctrl_lookup_state(cd->ts_pinctrl,
  15273. PINCTRL_STATE_SUSPEND);
  15274. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15275. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15276. dev_err(cd->dev,
  15277. "Can not lookup %s pinstate %d\n",
  15278. PINCTRL_STATE_SUSPEND, retval);
  15279. goto err_pinctrl_lookup;
  15280. }
  15281. cd->pinctrl_state_release
  15282. = pinctrl_lookup_state(cd->ts_pinctrl,
  15283. PINCTRL_STATE_RELEASE);
  15284. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15285. retval = PTR_ERR(cd->pinctrl_state_release);
  15286. dev_dbg(cd->dev,
  15287. "Can not lookup %s pinstate %d\n",
  15288. PINCTRL_STATE_RELEASE, retval);
  15289. }
  15290. return 0;
  15291. err_pinctrl_lookup:
  15292. devm_pinctrl_put(cd->ts_pinctrl);
  15293. err_pinctrl_get:
  15294. cd->ts_pinctrl = NULL;
  15295. return retval;
  15296. }
  15297. /*******************************************************************************
  15298. *******************************************************************************
  15299. * FUNCTION: pt_probe
  15300. *
  15301. * SUMMARY: Probe of the core module.
  15302. *
  15303. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15304. * probe functionality is split into two functions; pt_probe() and
  15305. * pt_probe_complete(). the initial setup is done in this function which
  15306. * then creates a WORK task which runs after the probe timer expires. This
  15307. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15308. *
  15309. * RETURN:
  15310. * 0 = success
  15311. * !0 = failure
  15312. *
  15313. * PARAMETERS:
  15314. * *ops - pointer to the bus
  15315. * *dev - pointer to the device structure
  15316. * irq - IRQ
  15317. * xfer_buf_size - size of the buffer
  15318. ******************************************************************************/
  15319. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15320. u16 irq, size_t xfer_buf_size)
  15321. {
  15322. struct pt_core_data *cd;
  15323. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15324. enum pt_atten_type type;
  15325. struct i2c_client *client = to_i2c_client(dev);
  15326. int rc = 0;
  15327. u8 pip_ver_major;
  15328. u8 pip_ver_minor;
  15329. u32 status = STARTUP_STATUS_START;
  15330. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15331. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15332. __func__);
  15333. rc = -ENODEV;
  15334. goto error_no_pdata;
  15335. }
  15336. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15337. if (!pdata->core_pdata->power) {
  15338. pt_debug(dev, DL_ERROR,
  15339. "%s: Missing platform data function\n",
  15340. __func__);
  15341. rc = -ENODEV;
  15342. goto error_no_pdata;
  15343. }
  15344. }
  15345. /* get context and debug print buffers */
  15346. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15347. if (!cd) {
  15348. rc = -ENOMEM;
  15349. goto error_no_pdata;
  15350. }
  15351. /* Initialize device info */
  15352. cd->dev = dev;
  15353. cd->pdata = pdata;
  15354. cd->cpdata = pdata->core_pdata;
  15355. cd->bus_ops = ops;
  15356. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15357. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15358. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15359. cd->hw_detected = false;
  15360. cd->pip2_prot_active = false;
  15361. cd->pip2_send_user_cmd = false;
  15362. cd->bl_pip_ver_ready = false;
  15363. cd->app_pip_ver_ready = false;
  15364. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15365. cd->get_param_id = 0;
  15366. cd->watchdog_enabled = 0;
  15367. cd->startup_retry_count = 0;
  15368. cd->core_probe_complete = 0;
  15369. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15370. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15371. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15372. cd->flashless_dut = 0;
  15373. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15374. cd->bl_with_no_int = 0;
  15375. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15376. cd->multi_chip = PT_FEATURE_DISABLE;
  15377. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15378. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15379. cd->set_dut_generation = true;
  15380. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15381. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15382. cd->set_dut_generation = true;
  15383. cd->active_dut_generation = DUT_PIP1_ONLY;
  15384. } else {
  15385. cd->set_dut_generation = false;
  15386. cd->active_dut_generation = DUT_UNKNOWN;
  15387. }
  15388. /* Initialize with platform data */
  15389. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15390. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15391. cd->hid_cmd_state = 1;
  15392. cd->fw_updating = false;
  15393. cd->multi_chip = 0;
  15394. #ifdef TTDL_DIAGNOSTICS
  15395. cd->t_refresh_active = 0;
  15396. cd->t_refresh_count = 0;
  15397. cd->pip2_crc_error_count = 0;
  15398. cd->wd_xres_count = 0;
  15399. cd->bl_retry_packet_count = 0;
  15400. cd->file_erase_timeout_count = 0;
  15401. cd->show_tt_data = false;
  15402. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15403. cd->err_gpio = 0;
  15404. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15405. cd->ttdl_bist_select = 0x07;
  15406. cd->force_pip2_seq = 0;
  15407. #endif /* TTDL_DIAGNOSTICS */
  15408. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15409. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15410. sizeof(PT_PIP2_BIN_FILE_PATH));
  15411. pt_init_hid_descriptor(&cd->hid_desc);
  15412. /* Read and store the descriptor lengths */
  15413. cd->hid_core.hid_report_desc_len =
  15414. le16_to_cpu(cd->hid_desc.report_desc_len);
  15415. cd->hid_core.hid_max_input_len =
  15416. le16_to_cpu(cd->hid_desc.max_input_len);
  15417. cd->hid_core.hid_max_output_len =
  15418. le16_to_cpu(cd->hid_desc.max_output_len);
  15419. /* Initialize mutexes and spinlocks */
  15420. mutex_init(&cd->module_list_lock);
  15421. mutex_init(&cd->system_lock);
  15422. mutex_init(&cd->sysfs_lock);
  15423. mutex_init(&cd->ttdl_restart_lock);
  15424. mutex_init(&cd->firmware_class_lock);
  15425. spin_lock_init(&cd->spinlock);
  15426. /* Initialize module list */
  15427. INIT_LIST_HEAD(&cd->module_list);
  15428. /* Initialize attention lists */
  15429. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15430. INIT_LIST_HEAD(&cd->atten_list[type]);
  15431. /* Initialize parameter list */
  15432. INIT_LIST_HEAD(&cd->param_list);
  15433. /* Initialize wait queue */
  15434. init_waitqueue_head(&cd->wait_q);
  15435. rc = pt_ts_pinctrl_init(cd);
  15436. if (!rc && cd->ts_pinctrl) {
  15437. /*
  15438. * Pinctrl handle is optional. If pinctrl handle is found
  15439. * let pins to be configured in active state. If not
  15440. * found continue further without error.
  15441. */
  15442. rc = pinctrl_select_state(cd->ts_pinctrl,
  15443. cd->pinctrl_state_active);
  15444. if (rc < 0)
  15445. dev_err(&client->dev, "failed to select pin to active state\n");
  15446. }
  15447. rc = pt_get_regulator(cd, true);
  15448. if (rc) {
  15449. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15450. goto error_alloc_data;
  15451. }
  15452. rc = pt_enable_regulator(cd, true);
  15453. if (rc) {
  15454. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15455. goto error_get_regulator;
  15456. }
  15457. /* Initialize works */
  15458. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15459. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15460. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15461. /* Initialize HID specific data */
  15462. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15463. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15464. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15465. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15466. cd->hid_core.hid_desc_register =
  15467. cpu_to_le16(cd->cpdata->hid_desc_register);
  15468. /* Set platform easywake value */
  15469. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15470. /* Set platform panel_id value */
  15471. cd->panel_id_support = cd->cpdata->panel_id_support;
  15472. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15473. /* Set Panel ID to default to 0 */
  15474. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15475. else
  15476. /* Set Panel ID to Not Enabled */
  15477. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15478. /* Initialize hw_version default to FFFF.FFFF.FF */
  15479. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15480. dev_set_drvdata(dev, cd);
  15481. /* PtSBC builds will call this function in pt_probe_complete() */
  15482. pt_add_core(dev);
  15483. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15484. if (rc) {
  15485. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15486. __func__);
  15487. goto error_enable_regulator;
  15488. }
  15489. /*
  15490. * Save the pointer to a global value, which will be used
  15491. * in ttdl_restart function
  15492. */
  15493. cd->bus_ops = ops;
  15494. /*
  15495. * When the IRQ GPIO is not direclty accessible and no function is
  15496. * defined to get the IRQ status, the IRQ passed in must be assigned
  15497. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15498. */
  15499. if (!cd->cpdata->irq_stat) {
  15500. cd->irq = irq;
  15501. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15502. __func__, cd->irq);
  15503. }
  15504. /* Call platform init function before setting up the GPIO's */
  15505. if (cd->cpdata->init) {
  15506. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15507. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15508. } else {
  15509. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15510. __func__);
  15511. rc = 0;
  15512. }
  15513. if (rc < 0) {
  15514. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15515. __func__, rc);
  15516. }
  15517. /* Power on any needed regulator(s) */
  15518. if (cd->cpdata->setup_power) {
  15519. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15520. rc = cd->cpdata->setup_power(cd->cpdata,
  15521. PT_MT_POWER_ON, cd->dev);
  15522. } else {
  15523. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15524. __func__);
  15525. rc = 0;
  15526. }
  15527. if (rc < 0)
  15528. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15529. __func__, rc);
  15530. #ifdef TTDL_DIAGNOSTICS
  15531. cd->watchdog_irq_stuck_count = 0;
  15532. cd->bus_transmit_error_count = 0;
  15533. #endif /* TTDL_DIAGNOSTICS */
  15534. if (cd->cpdata->detect) {
  15535. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15536. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15537. pt_platform_detect_read);
  15538. if (!rc) {
  15539. cd->hw_detected = true;
  15540. pt_debug(cd->dev, DL_INFO,
  15541. "%s: HW detected\n", __func__);
  15542. } else {
  15543. cd->hw_detected = false;
  15544. pt_debug(cd->dev, DL_INFO,
  15545. "%s: No HW detected\n", __func__);
  15546. rc = -ENODEV;
  15547. goto error_detect;
  15548. }
  15549. } else {
  15550. pt_debug(dev, DL_ERROR,
  15551. "%s: PARADE No HW detect function pointer\n",
  15552. __func__);
  15553. /*
  15554. * "hw_reset" is not needed in the "if" statement,
  15555. * because "hw_reset" is already included in "hw_detect"
  15556. * function.
  15557. */
  15558. rc = pt_hw_hard_reset(cd);
  15559. if (rc)
  15560. pt_debug(cd->dev, DL_ERROR,
  15561. "%s: FAILED to execute HARD reset\n",
  15562. __func__);
  15563. }
  15564. if (cd->cpdata->setup_irq) {
  15565. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15566. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15567. if (rc) {
  15568. pt_debug(dev, DL_ERROR,
  15569. "%s: Error, couldn't setup IRQ\n", __func__);
  15570. goto error_setup_irq;
  15571. }
  15572. } else {
  15573. pt_debug(dev, DL_ERROR,
  15574. "%s: IRQ function pointer not setup\n",
  15575. __func__);
  15576. goto error_setup_irq;
  15577. }
  15578. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  15579. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  15580. (unsigned long)cd);
  15581. #else
  15582. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  15583. #endif
  15584. pt_stop_wd_timer(cd);
  15585. #ifdef TTHE_TUNER_SUPPORT
  15586. mutex_init(&cd->tthe_lock);
  15587. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  15588. 0644, NULL, cd, &tthe_debugfs_fops);
  15589. #endif
  15590. rc = device_init_wakeup(dev, 1);
  15591. if (rc < 0)
  15592. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  15593. __func__, rc);
  15594. pm_runtime_get_noresume(dev);
  15595. pm_runtime_set_active(dev);
  15596. pm_runtime_enable(dev);
  15597. /* If IRQ asserted, read out all from buffer to release INT pin */
  15598. if (cd->cpdata->irq_stat) {
  15599. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15600. } else {
  15601. /* Force a read in case the reset sentinel already arrived */
  15602. rc = pt_read_input(cd);
  15603. if (!rc)
  15604. pt_parse_input(cd);
  15605. }
  15606. /* Without sleep DUT is not ready and will NAK the first write */
  15607. msleep(150);
  15608. /* Attempt to set the DUT generation if not yet set */
  15609. if (cd->active_dut_generation == DUT_UNKNOWN) {
  15610. if (cd->bl_pip_ver_ready ||
  15611. (cd->app_pip_ver_ready &&
  15612. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  15613. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15614. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  15615. __func__, cd->active_dut_generation);
  15616. } else {
  15617. rc = _pt_detect_dut_generation(cd->dev,
  15618. &status, &cd->active_dut_generation,
  15619. &cd->mode);
  15620. if ((cd->active_dut_generation == DUT_UNKNOWN)
  15621. || rc) {
  15622. pt_debug(cd->dev, DL_ERROR,
  15623. " === DUT Gen unknown, Skip Enum ===\n");
  15624. goto skip_enum;
  15625. }
  15626. }
  15627. }
  15628. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  15629. &pip_ver_major, &pip_ver_minor);
  15630. if (pip_ver_major == 2) {
  15631. cd->bl_pip_ver_ready = true;
  15632. pt_debug(dev, DL_ERROR,
  15633. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  15634. pip_ver_minor);
  15635. cd->hw_detected = true;
  15636. } else if (pip_ver_major == 1) {
  15637. cd->app_pip_ver_ready = true;
  15638. pt_debug(dev, DL_ERROR,
  15639. " === PIP1.%d Detected ===\n", pip_ver_minor);
  15640. cd->hw_detected = true;
  15641. } else {
  15642. cd->sysinfo.ttdata.pip_ver_major = 0;
  15643. cd->sysinfo.ttdata.pip_ver_minor = 0;
  15644. cd->app_pip_ver_ready = false;
  15645. cd->hw_detected = false;
  15646. pt_debug(dev, DL_ERROR,
  15647. " === PIP Version Not Detected, Skip Enum ===\n");
  15648. /* For legacy DUTS proceed, enum will attempt to launch app */
  15649. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  15650. goto skip_enum;
  15651. }
  15652. rc = pt_enum_with_dut(cd, false, &status);
  15653. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  15654. __func__, cd->startup_status, status);
  15655. if (rc == -ENODEV) {
  15656. pt_debug(cd->dev, DL_ERROR,
  15657. "%s: Enumeration Failed r=%d\n", __func__, rc);
  15658. /* For PtSBC don't error out, allow TTDL to stay up */
  15659. goto error_after_startup;
  15660. }
  15661. /* Suspend scanning until probe is complete to avoid asyc touches */
  15662. pt_pip_suspend_scanning_(cd);
  15663. if (cd->hw_detected) {
  15664. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  15665. __func__);
  15666. rc = add_sysfs_interfaces(dev);
  15667. if (rc < 0) {
  15668. pt_debug(dev, DL_ERROR,
  15669. "%s: Error, fail sysfs init\n", __func__);
  15670. goto error_after_startup;
  15671. }
  15672. } else {
  15673. pt_debug(dev, DL_ERROR,
  15674. "%s: No HW detected, sysfs interfaces not added\n",
  15675. __func__);
  15676. }
  15677. skip_enum:
  15678. pm_runtime_put_sync(dev);
  15679. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  15680. rc = pt_mt_probe(dev);
  15681. if (rc < 0) {
  15682. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  15683. __func__);
  15684. goto error_after_sysfs_create;
  15685. }
  15686. rc = pt_btn_probe(dev);
  15687. if (rc < 0) {
  15688. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  15689. __func__);
  15690. goto error_after_startup_mt;
  15691. }
  15692. pt_probe_modules(cd);
  15693. #ifdef CONFIG_HAS_EARLYSUSPEND
  15694. pt_setup_early_suspend(cd);
  15695. #elif defined(CONFIG_FB)
  15696. pt_setup_fb_notifier(cd);
  15697. #endif
  15698. #ifdef NEED_SUSPEND_NOTIFIER
  15699. cd->pm_notifier.notifier_call = pt_pm_notifier;
  15700. register_pm_notifier(&cd->pm_notifier);
  15701. #endif
  15702. pt_pip_resume_scanning_(cd);
  15703. mutex_lock(&cd->system_lock);
  15704. cd->startup_status |= status;
  15705. cd->core_probe_complete = 1;
  15706. mutex_unlock(&cd->system_lock);
  15707. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  15708. __func__);
  15709. return 0;
  15710. error_after_startup_mt:
  15711. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  15712. pt_mt_release(dev);
  15713. error_after_sysfs_create:
  15714. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  15715. pm_runtime_disable(dev);
  15716. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15717. device_wakeup_disable(dev);
  15718. #endif
  15719. device_init_wakeup(dev, 0);
  15720. cancel_work_sync(&cd->enum_work);
  15721. pt_stop_wd_timer(cd);
  15722. pt_free_si_ptrs(cd);
  15723. remove_sysfs_interfaces(dev);
  15724. error_after_startup:
  15725. pr_err("%s PARADE error_after_startup\n", __func__);
  15726. del_timer(&cd->watchdog_timer);
  15727. if (cd->cpdata->setup_irq)
  15728. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15729. error_setup_irq:
  15730. error_detect:
  15731. if (cd->cpdata->init)
  15732. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15733. if (cd->cpdata->setup_power)
  15734. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15735. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15736. kfree(cd);
  15737. error_enable_regulator:
  15738. pt_del_core(dev);
  15739. dev_set_drvdata(dev, NULL);
  15740. pt_enable_regulator(cd, false);
  15741. error_get_regulator:
  15742. pt_get_regulator(cd, false);
  15743. error_alloc_data:
  15744. error_no_pdata:
  15745. pr_err("%s failed.\n", __func__);
  15746. return rc;
  15747. }
  15748. EXPORT_SYMBOL_GPL(pt_probe);
  15749. /*******************************************************************************
  15750. * FUNCTION: pt_release
  15751. *
  15752. * SUMMARY: This function does the following cleanup:
  15753. * - Releases all probed modules
  15754. * - Stops the watchdog
  15755. * - Cancels all pending work tasks
  15756. * - Removes all created sysfs nodes
  15757. * - Removes all created debugfs nodes
  15758. * - Frees the IRQ
  15759. * - Deletes the core
  15760. * - Frees all pointers and HID reports
  15761. *
  15762. * RETURN:
  15763. * 0 = success
  15764. * !0 = failure
  15765. *
  15766. * PARAMETERS:
  15767. * *cd - pointer to the core data structure
  15768. ******************************************************************************/
  15769. int pt_release(struct pt_core_data *cd)
  15770. {
  15771. struct device *dev = cd->dev;
  15772. /*
  15773. * Suspend the device before freeing the startup_work and stopping
  15774. * the watchdog since sleep function restarts watchdog on failure
  15775. */
  15776. pm_runtime_suspend(dev);
  15777. pm_runtime_disable(dev);
  15778. /*
  15779. * Any 'work' that can trigger a new thread should be canceled first.
  15780. * The watchdog is also stopped again because another thread could have
  15781. * restarted it. The irq_work is cancelled last because it is used for
  15782. * all I2C/SPI communication.
  15783. */
  15784. pt_stop_wd_timer(cd);
  15785. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15786. cancel_work_sync(&cd->ttdl_restart_work);
  15787. cancel_work_sync(&cd->enum_work);
  15788. pt_stop_wd_timer(cd);
  15789. pt_release_modules(cd);
  15790. pt_proximity_release(dev);
  15791. pt_btn_release(dev);
  15792. pt_mt_release(dev);
  15793. #ifdef CONFIG_HAS_EARLYSUSPEND
  15794. unregister_early_suspend(&cd->es);
  15795. #elif defined(CONFIG_FB)
  15796. fb_unregister_client(&cd->fb_notifier);
  15797. #endif
  15798. #ifdef NEED_SUSPEND_NOTIFIER
  15799. unregister_pm_notifier(&cd->pm_notifier);
  15800. #endif
  15801. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  15802. device_wakeup_disable(dev);
  15803. #endif
  15804. device_init_wakeup(dev, 0);
  15805. #ifdef TTHE_TUNER_SUPPORT
  15806. mutex_lock(&cd->tthe_lock);
  15807. cd->tthe_exit = 1;
  15808. wake_up(&cd->wait_q);
  15809. mutex_unlock(&cd->tthe_lock);
  15810. debugfs_remove(cd->tthe_debugfs);
  15811. #endif
  15812. sysfs_remove_group(&dev->kobj, &early_attr_group);
  15813. remove_sysfs_interfaces(dev);
  15814. disable_irq_nosync(cd->irq);
  15815. if (cd->cpdata->setup_irq)
  15816. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  15817. if (cd->cpdata->init)
  15818. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  15819. if (cd->cpdata->setup_power)
  15820. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  15821. dev_set_drvdata(dev, NULL);
  15822. pt_del_core(dev);
  15823. pt_enable_regulator(cd, false);
  15824. pt_get_regulator(cd, false);
  15825. pt_free_si_ptrs(cd);
  15826. kfree(cd);
  15827. return 0;
  15828. }
  15829. EXPORT_SYMBOL_GPL(pt_release);
  15830. MODULE_LICENSE("GPL");
  15831. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  15832. MODULE_AUTHOR("Parade Technologies <[email protected]>");