pt_core.c 511 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810
  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. #if defined(CONFIG_DRM)
  43. static struct drm_panel *active_panel;
  44. #endif
  45. MODULE_FIRMWARE(PT_FW_FILE_NAME);
  46. static const char *pt_driver_core_name = PT_CORE_NAME;
  47. static const char *pt_driver_core_version = PT_DRIVER_VERSION;
  48. static const char *pt_driver_core_date = PT_DRIVER_DATE;
  49. struct pt_hid_field {
  50. int report_count;
  51. int report_size;
  52. int size; /* report_count * report_size */
  53. int offset;
  54. int data_type;
  55. int logical_min;
  56. int logical_max;
  57. /* Usage Page (Hi 16 bit) + Usage (Lo 16 bit) */
  58. u32 usage_page;
  59. u32 collection_usage_pages[PT_HID_MAX_COLLECTIONS];
  60. struct pt_hid_report *report;
  61. bool record_field;
  62. };
  63. struct pt_hid_report {
  64. u8 id;
  65. u8 type;
  66. int size;
  67. struct pt_hid_field *fields[PT_HID_MAX_FIELDS];
  68. int num_fields;
  69. int record_field_index;
  70. int header_size;
  71. int record_size;
  72. u32 usage_page;
  73. };
  74. struct atten_node {
  75. struct list_head node;
  76. char *id;
  77. struct device *dev;
  78. int (*func)(struct device *dev);
  79. int mode;
  80. };
  81. struct param_node {
  82. struct list_head node;
  83. u8 id;
  84. u32 value;
  85. u8 size;
  86. };
  87. struct module_node {
  88. struct list_head node;
  89. struct pt_module *module;
  90. void *data;
  91. };
  92. struct pt_hid_cmd {
  93. u8 opcode;
  94. u8 report_type;
  95. union {
  96. u8 report_id;
  97. u8 power_state;
  98. };
  99. u8 has_data_register;
  100. size_t write_length;
  101. u8 *write_buf;
  102. u8 *read_buf;
  103. u8 wait_interrupt;
  104. u8 reset_cmd;
  105. u16 timeout_ms;
  106. };
  107. struct pt_hid_output {
  108. u8 cmd_type;
  109. u16 length;
  110. u8 command_code;
  111. size_t write_length;
  112. u8 *write_buf;
  113. u8 novalidate;
  114. u8 reset_expected;
  115. u16 timeout_ms;
  116. };
  117. #define SET_CMD_OPCODE(byte, opcode) SET_CMD_LOW(byte, opcode)
  118. #define SET_CMD_REPORT_TYPE(byte, type) SET_CMD_HIGH(byte, ((type) << 4))
  119. #define SET_CMD_REPORT_ID(byte, id) SET_CMD_LOW(byte, id)
  120. #define CREATE_PIP1_FW_CMD(command) \
  121. .cmd_type = PIP1_CMD_TYPE_FW, \
  122. .command_code = command
  123. #define CREATE_PIP1_BL_CMD(command) \
  124. .cmd_type = PIP1_CMD_TYPE_BL, \
  125. .command_code = command
  126. #define PT_MAX_PR_BUF_SIZE 2048
  127. /*******************************************************************************
  128. * FUNCTION: pt_pr_buf
  129. *
  130. * SUMMARY: Print out the contents of a buffer to kmsg based on the debug level
  131. *
  132. * RETURN: Void
  133. *
  134. * PARAMETERS:
  135. * *dev - pointer to Device structure
  136. * debug_level - requested debug level to print at
  137. * *buf - pointer to buffer to print
  138. * buf_len - size of buf
  139. * *data_name - Descriptive name of data prefixed to data
  140. ******************************************************************************/
  141. void pt_pr_buf(struct device *dev, u8 debug_level, u8 *buf,
  142. u16 buf_len, const char *data_name)
  143. {
  144. struct pt_core_data *cd = dev_get_drvdata(dev);
  145. int i;
  146. ssize_t pr_buf_index = 0;
  147. int max_size;
  148. /* only proceed if valid debug level and there is data to print */
  149. if (debug_level <= cd->debug_level && buf_len > 0) {
  150. char *pr_buf = kzalloc(PT_MAX_PR_BUF_SIZE, GFP_KERNEL);
  151. if (!pr_buf)
  152. return;
  153. /*
  154. * With a space each printed char takes 3 bytes, subtract
  155. * the length of the data_name prefix as well as 11 bytes
  156. * for the " [0..xxx]: " printed before the data.
  157. */
  158. max_size = (PT_MAX_PR_BUF_SIZE - sizeof(data_name) - 11) / 3;
  159. /* Ensure pr_buf_index stays within the 1018 size */
  160. pr_buf_index += scnprintf(pr_buf, PT_MAX_PR_BUF_SIZE, "%s [0..%d]: ",
  161. data_name);
  162. for (i = 0; i < buf_len && i < max_size; i++)
  163. pr_buf_index += scnprintf(pr_buf + pr_buf_index,
  164. PT_MAX_PR_BUF_SIZE, "%02X ", buf[i]);
  165. pt_debug(dev, debug_level, "%s\n", pr_buf);
  166. kfree(pr_buf);
  167. }
  168. }
  169. EXPORT_SYMBOL_GPL(pt_pr_buf);
  170. #ifdef TTHE_TUNER_SUPPORT
  171. /*******************************************************************************
  172. * FUNCTION: tthe_print
  173. *
  174. * SUMMARY: Format data name and time stamp as the header and format the
  175. * content of input buffer with hex base to "tthe_buf". And then wake up event
  176. * semaphore for tthe debugfs node.
  177. *
  178. * RETURN:
  179. * 0 = success
  180. * !0 = failure
  181. *
  182. * PARAMETERS:
  183. * *cd - pointer to core data
  184. * *buf - pointer to input buffer
  185. * buf_len - size of input buffer
  186. * *data_name - pointer to data name
  187. ******************************************************************************/
  188. static int tthe_print(struct pt_core_data *cd, u8 *buf, int buf_len,
  189. const u8 *data_name)
  190. {
  191. int name_len = strlen(data_name);
  192. int i, n;
  193. u8 *p;
  194. int remain;
  195. u8 data_name_with_time_stamp[100];
  196. /* Prepend timestamp, if requested, to data_name */
  197. if (cd->show_timestamp) {
  198. scnprintf(data_name_with_time_stamp,
  199. sizeof(data_name_with_time_stamp),
  200. "[%u] %s", pt_get_time_stamp(), data_name);
  201. data_name = data_name_with_time_stamp;
  202. name_len = strlen(data_name);
  203. }
  204. mutex_lock(&cd->tthe_lock);
  205. if (!cd->tthe_buf)
  206. goto exit;
  207. /* Add 1 due to the '\n' that is appended at the end */
  208. if (cd->tthe_buf_len + name_len + buf_len + 1 > cd->tthe_buf_size)
  209. goto exit;
  210. if (name_len + buf_len == 0)
  211. goto exit;
  212. remain = cd->tthe_buf_size - cd->tthe_buf_len;
  213. if (remain < name_len)
  214. name_len = remain;
  215. p = cd->tthe_buf + cd->tthe_buf_len;
  216. memcpy(p, data_name, name_len);
  217. cd->tthe_buf_len += name_len;
  218. p += name_len;
  219. remain -= name_len;
  220. *p = 0;
  221. for (i = 0; i < buf_len; i++) {
  222. n = scnprintf(p, remain, "%02X ", buf[i]);
  223. if (n <= 0)
  224. break;
  225. p += n;
  226. remain -= n;
  227. cd->tthe_buf_len += n;
  228. }
  229. n = scnprintf(p, remain, "\n");
  230. cd->tthe_buf_len += n;
  231. exit:
  232. wake_up(&cd->wait_q);
  233. mutex_unlock(&cd->tthe_lock);
  234. return 0;
  235. }
  236. /*******************************************************************************
  237. * FUNCTION: _pt_request_tthe_print
  238. *
  239. * SUMMARY: Function pointer included in core_cmds to allow other modules
  240. * to request to print data to the "tthe_buffer".
  241. *
  242. * RETURN:
  243. * 0 = success
  244. * !0 = failure
  245. *
  246. * PARAMETERS:
  247. * *dev - pointer to device structure
  248. ******************************************************************************/
  249. static int _pt_request_tthe_print(struct device *dev, u8 *buf,
  250. int buf_len, const u8 *data_name)
  251. {
  252. struct pt_core_data *cd = dev_get_drvdata(dev);
  253. return tthe_print(cd, buf, buf_len, data_name);
  254. }
  255. #endif
  256. /*******************************************************************************
  257. * FUNCTION: pt_platform_detect_read
  258. *
  259. * SUMMARY: To be passed to platform dectect function to perform a read
  260. * operation.
  261. *
  262. * RETURN:
  263. * 0 = success
  264. * !0 = failure
  265. *
  266. * PARAMETERS:
  267. * *dev - pointer to Device structure
  268. * *buf - pointer to buffer where the data read will be stored
  269. * size - size to be read
  270. ******************************************************************************/
  271. static int pt_platform_detect_read(struct device *dev, void *buf, int size)
  272. {
  273. struct pt_core_data *cd = dev_get_drvdata(dev);
  274. return pt_adap_read_default(cd, buf, size);
  275. }
  276. /*******************************************************************************
  277. * FUNCTION: pt_add_parameter
  278. *
  279. * SUMMARY: Adds a parameter that has been altered to the parameter linked list.
  280. * On every reset of the DUT this linked list is traversed and all
  281. * parameters in it are restored to the DUT.
  282. *
  283. * RETURN:
  284. * 0 = success
  285. * !0 = failure
  286. *
  287. * PARAMETERS:
  288. * *cd - pointer to core data
  289. * param_id - parameter ID to add
  290. * param_value - Value corresponding to the ID
  291. * param_size - Size of param_value
  292. ******************************************************************************/
  293. static int pt_add_parameter(struct pt_core_data *cd,
  294. u8 param_id, u32 param_value, u8 param_size)
  295. {
  296. struct param_node *param, *param_new;
  297. /* Check if parameter already exists in the list */
  298. spin_lock(&cd->spinlock);
  299. list_for_each_entry(param, &cd->param_list, node) {
  300. if (param->id == param_id) {
  301. /* Update parameter */
  302. param->value = param_value;
  303. pt_debug(cd->dev, DL_INFO,
  304. "%s: Update parameter id:%d value:%d size:%d\n",
  305. __func__, param_id, param_value, param_size);
  306. goto exit_unlock;
  307. }
  308. }
  309. spin_unlock(&cd->spinlock);
  310. param_new = kzalloc(sizeof(*param_new), GFP_KERNEL);
  311. if (!param_new)
  312. return -ENOMEM;
  313. param_new->id = param_id;
  314. param_new->value = param_value;
  315. param_new->size = param_size;
  316. pt_debug(cd->dev, DL_INFO,
  317. "%s: Add parameter id:%d value:%d size:%d\n",
  318. __func__, param_id, param_value, param_size);
  319. spin_lock(&cd->spinlock);
  320. list_add(&param_new->node, &cd->param_list);
  321. exit_unlock:
  322. spin_unlock(&cd->spinlock);
  323. return 0;
  324. }
  325. #ifdef TTDL_DIAGNOSTICS
  326. /*******************************************************************************
  327. * FUNCTION: pt_erase_parameter_list
  328. *
  329. * SUMMARY: Empty out the entire parameter linked list of all parameter/value
  330. * pairs. In some test cases this functionality is needed to ensure DUT
  331. * returns to a virgin state after a reset and no parameters are restored.
  332. *
  333. * RETURN:
  334. * 0 = success
  335. * !0 = failure
  336. *
  337. * PARAMETERS:
  338. * *cd - pointer to core data
  339. ******************************************************************************/
  340. static int pt_erase_parameter_list(struct pt_core_data *cd)
  341. {
  342. struct param_node *pos, *temp;
  343. spin_lock(&cd->spinlock);
  344. list_for_each_entry_safe(pos, temp, &cd->param_list, node) {
  345. pt_debug(cd->dev, DL_INFO,
  346. "%s: Parameter Restore List - remove 0x%02x\n",
  347. __func__, pos->id);
  348. list_del(&pos->node);
  349. kfree(pos);
  350. }
  351. spin_unlock(&cd->spinlock);
  352. return 0;
  353. }
  354. /*******************************************************************************
  355. * FUNCTION: pt_count_parameter_list
  356. *
  357. * SUMMARY: Count the items in the RAM parameter restor list
  358. *
  359. * RETURN:
  360. * 0 = success
  361. * !0 = failure
  362. *
  363. * PARAMETERS:
  364. * *cd - pointer to core data
  365. ******************************************************************************/
  366. static int pt_count_parameter_list(struct pt_core_data *cd)
  367. {
  368. struct param_node *pos, *temp;
  369. int entries = 0;
  370. spin_lock(&cd->spinlock);
  371. list_for_each_entry_safe(pos, temp, &cd->param_list, node)
  372. entries++;
  373. spin_unlock(&cd->spinlock);
  374. return entries;
  375. }
  376. #endif /* TTDL_DIAGNOSTICS */
  377. /*******************************************************************************
  378. * FUNCTION: request_exclusive
  379. *
  380. * SUMMARY: Request exclusive access to the DUT
  381. *
  382. * RETURN:
  383. * 0 = success
  384. * !0 = failure
  385. *
  386. * PARAMETERS:
  387. * *cd - pointer to core data
  388. * *ownptr - pointer to device
  389. * timeout_ms - Timeout value
  390. ******************************************************************************/
  391. int request_exclusive(struct pt_core_data *cd, void *ownptr,
  392. int timeout_ms)
  393. {
  394. int t = msecs_to_jiffies(timeout_ms);
  395. bool with_timeout = (timeout_ms != 0);
  396. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Request EXCLUSIVE t=%d\n",
  397. __func__, timeout_ms);
  398. mutex_lock(&cd->system_lock);
  399. if (!cd->exclusive_dev && cd->exclusive_waits == 0) {
  400. cd->exclusive_dev = ownptr;
  401. goto exit;
  402. }
  403. cd->exclusive_waits++;
  404. wait:
  405. mutex_unlock(&cd->system_lock);
  406. if (with_timeout) {
  407. t = wait_event_timeout(cd->wait_q, !cd->exclusive_dev, t);
  408. if (IS_TMO(t)) {
  409. pt_debug(cd->dev, DL_ERROR,
  410. "%s: tmo waiting exclusive access\n", __func__);
  411. return -ETIME;
  412. }
  413. } else {
  414. wait_event(cd->wait_q, !cd->exclusive_dev);
  415. }
  416. mutex_lock(&cd->system_lock);
  417. if (cd->exclusive_dev)
  418. goto wait;
  419. cd->exclusive_dev = ownptr;
  420. cd->exclusive_waits--;
  421. exit:
  422. mutex_unlock(&cd->system_lock);
  423. pt_debug(cd->dev, DL_DEBUG, "%s: request exclusive ok=%p\n",
  424. __func__, ownptr);
  425. return 0;
  426. }
  427. /*******************************************************************************
  428. * FUNCTION: release_exclusive_
  429. *
  430. * SUMMARY: Release exclusive access to the DUT
  431. *
  432. * RETURN:
  433. * 0 = success
  434. *
  435. * PARAMETERS:
  436. * *cd - pointer to core data
  437. * *ownptr - pointer to device
  438. ******************************************************************************/
  439. static int release_exclusive_(struct pt_core_data *cd, void *ownptr)
  440. {
  441. pt_debug(cd->dev, DL_INFO, "%s: Attempt to Release EXCLUSIVE\n",
  442. __func__);
  443. if (cd->exclusive_dev != ownptr)
  444. return -EINVAL;
  445. pt_debug(cd->dev, DL_DEBUG, "%s: exclusive_dev %p freed\n",
  446. __func__, cd->exclusive_dev);
  447. cd->exclusive_dev = NULL;
  448. wake_up(&cd->wait_q);
  449. return 0;
  450. }
  451. /*******************************************************************************
  452. * FUNCTION: release_exclusive
  453. *
  454. * SUMMARY: Protected wrapper to release_exclusive_()
  455. *
  456. * RETURN:
  457. * 0 = success
  458. * !0 = failure
  459. *
  460. * PARAMETERS:
  461. * *cd - pointer to core data
  462. * *ownptr - pointer to device
  463. ******************************************************************************/
  464. int release_exclusive(struct pt_core_data *cd, void *ownptr)
  465. {
  466. int rc;
  467. mutex_lock(&cd->system_lock);
  468. rc = release_exclusive_(cd, ownptr);
  469. mutex_unlock(&cd->system_lock);
  470. return rc;
  471. }
  472. /*******************************************************************************
  473. * FUNCTION: pt_hid_exec_cmd_
  474. *
  475. * SUMMARY: Send the HID command to the DUT
  476. *
  477. * RETURN:
  478. * 0 = success
  479. * !0 = failure
  480. *
  481. * PARAMETERS:
  482. * *cd - pointer to core data
  483. * *hid_cmd - pointer to the HID command to send
  484. ******************************************************************************/
  485. static int pt_hid_exec_cmd_(struct pt_core_data *cd,
  486. struct pt_hid_cmd *hid_cmd)
  487. {
  488. int rc = 0;
  489. u8 *cmd;
  490. u16 cmd_length;
  491. u8 cmd_offset = 0;
  492. cmd_length = 2 /* command register */
  493. + 2 /* command */
  494. + (hid_cmd->report_id >= 0XF ? 1 : 0) /* Report ID */
  495. + (hid_cmd->has_data_register ? 2 : 0) /* Data register */
  496. + hid_cmd->write_length; /* Data length */
  497. cmd = kzalloc(cmd_length, GFP_KERNEL);
  498. if (!cmd)
  499. return -ENOMEM;
  500. /* Set Command register */
  501. memcpy(&cmd[cmd_offset], &cd->hid_desc.command_register,
  502. sizeof(cd->hid_desc.command_register));
  503. cmd_offset += sizeof(cd->hid_desc.command_register);
  504. /* Set Command */
  505. SET_CMD_REPORT_TYPE(cmd[cmd_offset], hid_cmd->report_type);
  506. if (hid_cmd->report_id >= 0XF)
  507. SET_CMD_REPORT_ID(cmd[cmd_offset], 0xF);
  508. else
  509. SET_CMD_REPORT_ID(cmd[cmd_offset], hid_cmd->report_id);
  510. cmd_offset++;
  511. SET_CMD_OPCODE(cmd[cmd_offset], hid_cmd->opcode);
  512. cmd_offset++;
  513. if (hid_cmd->report_id >= 0XF) {
  514. cmd[cmd_offset] = hid_cmd->report_id;
  515. cmd_offset++;
  516. }
  517. /* Set Data register */
  518. if (hid_cmd->has_data_register) {
  519. memcpy(&cmd[cmd_offset], &cd->hid_desc.data_register,
  520. sizeof(cd->hid_desc.data_register));
  521. cmd_offset += sizeof(cd->hid_desc.data_register);
  522. }
  523. /* Set Data */
  524. if (hid_cmd->write_length && hid_cmd->write_buf) {
  525. memcpy(&cmd[cmd_offset], hid_cmd->write_buf,
  526. hid_cmd->write_length);
  527. cmd_offset += hid_cmd->write_length;
  528. }
  529. pt_debug(cd->dev, DL_INFO,
  530. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  531. __func__, cmd_length, hid_cmd->report_id);
  532. pt_pr_buf(cd->dev, DL_DEBUG, cmd, cmd_length, ">>> CMD");
  533. rc = pt_adap_write_read_specific(cd, cmd_length, cmd,
  534. hid_cmd->read_buf);
  535. if (rc)
  536. pt_debug(cd->dev, DL_ERROR,
  537. "%s: Fail pt_adap_transfer\n", __func__);
  538. kfree(cmd);
  539. return rc;
  540. }
  541. #ifdef TTDL_DIAGNOSTICS
  542. /*******************************************************************************
  543. * FUNCTION: pt_toggle_err_gpio
  544. *
  545. * SUMMARY: Toggles the pre-defined error GPIO
  546. *
  547. * RETURN: n/a
  548. *
  549. * PARAMETERS:
  550. * *cd - pointer to core data
  551. * type - type of err that occurred
  552. ******************************************************************************/
  553. void pt_toggle_err_gpio(struct pt_core_data *cd, u8 type)
  554. {
  555. pt_debug(cd->dev, DL_DEBUG, "%s called with type = %d\n",
  556. __func__, type);
  557. if (cd->err_gpio && type == cd->err_gpio_type) {
  558. pt_debug(cd->dev, DL_WARN, "%s: Toggle ERR GPIO\n", __func__);
  559. gpio_direction_output(cd->err_gpio,
  560. !gpio_get_value(cd->err_gpio));
  561. }
  562. }
  563. /*******************************************************************************
  564. * FUNCTION: _pt_request_toggle_err_gpio
  565. *
  566. * SUMMARY: Function pointer included in core_cmds to allow other modules
  567. * to request to toggle the err_gpio
  568. *
  569. * RETURN: n/a
  570. *
  571. * PARAMETERS:
  572. * *cd - pointer to core data
  573. * type - type of err that occurred
  574. ******************************************************************************/
  575. void _pt_request_toggle_err_gpio(struct device *dev, u8 type)
  576. {
  577. struct pt_core_data *cd = dev_get_drvdata(dev);
  578. pt_toggle_err_gpio(cd, type);
  579. }
  580. #endif /* TTDL_DIAGNOSTICS */
  581. /*******************************************************************************
  582. * FUNCTION: pt_hid_exec_cmd_and_wait_
  583. *
  584. * SUMMARY: Send the HID command to the DUT and wait for the response
  585. *
  586. * RETURN:
  587. * 0 = success
  588. * !0 = failure
  589. *
  590. * PARAMETERS:
  591. * *cd - pointer to core data
  592. * *hid_cmd - pointer to the HID command to send
  593. ******************************************************************************/
  594. static int pt_hid_exec_cmd_and_wait_(struct pt_core_data *cd,
  595. struct pt_hid_cmd *hid_cmd)
  596. {
  597. int rc = 0;
  598. int t;
  599. u16 timeout_ms;
  600. int *cmd_state;
  601. if (hid_cmd->reset_cmd)
  602. cmd_state = &cd->hid_reset_cmd_state;
  603. else
  604. cmd_state = &cd->hid_cmd_state;
  605. if (hid_cmd->wait_interrupt) {
  606. mutex_lock(&cd->system_lock);
  607. *cmd_state = 1;
  608. mutex_unlock(&cd->system_lock);
  609. }
  610. rc = pt_hid_exec_cmd_(cd, hid_cmd);
  611. if (rc) {
  612. if (hid_cmd->wait_interrupt)
  613. goto error;
  614. goto exit;
  615. }
  616. if (!hid_cmd->wait_interrupt)
  617. goto exit;
  618. if (hid_cmd->timeout_ms)
  619. timeout_ms = hid_cmd->timeout_ms;
  620. else
  621. timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT;
  622. t = wait_event_timeout(cd->wait_q, (*cmd_state == 0),
  623. msecs_to_jiffies(timeout_ms));
  624. if (IS_TMO(t)) {
  625. #ifdef TTDL_DIAGNOSTICS
  626. cd->bus_transmit_error_count++;
  627. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  628. #endif /* TTDL_DIAGNOSTICS */
  629. pt_debug(cd->dev, DL_ERROR,
  630. "%s: HID output cmd execution timed out\n",
  631. __func__);
  632. rc = -ETIME;
  633. goto error;
  634. }
  635. goto exit;
  636. error:
  637. mutex_lock(&cd->system_lock);
  638. *cmd_state = 0;
  639. mutex_unlock(&cd->system_lock);
  640. exit:
  641. return rc;
  642. }
  643. /*******************************************************************************
  644. * FUNCTION: pt_hid_cmd_reset_
  645. *
  646. * SUMMARY: Send the HID RESET command to the DUT
  647. *
  648. * RETURN:
  649. * 0 = success
  650. * !0 = failure
  651. *
  652. * PARAMETERS:
  653. * *cd - pointer to core data
  654. ******************************************************************************/
  655. static int pt_hid_cmd_reset_(struct pt_core_data *cd)
  656. {
  657. struct pt_hid_cmd hid_cmd = {
  658. .opcode = HID_CMD_RESET,
  659. .wait_interrupt = 1,
  660. .reset_cmd = 1,
  661. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  662. };
  663. return pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  664. }
  665. /*******************************************************************************
  666. * FUNCTION: pt_hid_cmd_reset
  667. *
  668. * SUMMARY: Wrapper function for pt_hid_cmd_reset_ that guarantees exclusive
  669. * access.
  670. *
  671. * RETURN:
  672. * 0 = success
  673. * !0 = failure
  674. *
  675. * PARAMETERS:
  676. * *cd - pointer to core data
  677. ******************************************************************************/
  678. static int pt_hid_cmd_reset(struct pt_core_data *cd)
  679. {
  680. int rc = 0;
  681. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  682. if (rc < 0) {
  683. pt_debug(cd->dev, DL_ERROR,
  684. "%s: fail get exclusive ex=%p own=%p\n",
  685. __func__, cd->exclusive_dev, cd->dev);
  686. return rc;
  687. }
  688. pt_debug(cd->dev, DL_INFO, "%s: Send HID Reset command\n", __func__);
  689. rc = pt_hid_cmd_reset_(cd);
  690. if (release_exclusive(cd, cd->dev) < 0)
  691. pt_debug(cd->dev, DL_ERROR,
  692. "%s: fail to release exclusive\n", __func__);
  693. return rc;
  694. }
  695. /*******************************************************************************
  696. * FUNCTION: pt_hid_cmd_set_power_
  697. *
  698. * SUMMARY: Send hid cmd to set power state for the DUT and wait for response
  699. *
  700. * RETURN:
  701. * 0 = success
  702. * !0 = failure
  703. *
  704. * PARAMETERS:
  705. * *cd - pointer to core data
  706. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  707. ******************************************************************************/
  708. static int pt_hid_cmd_set_power_(struct pt_core_data *cd,
  709. u8 power_state)
  710. {
  711. int rc = 0;
  712. struct pt_hid_cmd hid_cmd = {
  713. .opcode = HID_CMD_SET_POWER,
  714. .wait_interrupt = 1,
  715. .timeout_ms = PT_HID_CMD_DEFAULT_TIMEOUT,
  716. };
  717. hid_cmd.power_state = power_state;
  718. /* The chip won't give response if goes to Deep Standby */
  719. if (power_state == HID_POWER_STANDBY) {
  720. rc = pt_hid_exec_cmd_(cd, &hid_cmd);
  721. if (rc)
  722. pt_debug(cd->dev, DL_ERROR,
  723. "%s: Failed to set power to state:%d\n",
  724. __func__, power_state);
  725. else
  726. cd->fw_sys_mode_in_standby_state = true;
  727. return rc;
  728. }
  729. cd->fw_sys_mode_in_standby_state = false;
  730. rc = pt_hid_exec_cmd_and_wait_(cd, &hid_cmd);
  731. if (rc) {
  732. pt_debug(cd->dev, DL_ERROR,
  733. "%s: Failed to set power to state:%d\n",
  734. __func__, power_state);
  735. return rc;
  736. }
  737. /* validate */
  738. if ((cd->response_buf[2] != HID_RESPONSE_REPORT_ID)
  739. || ((cd->response_buf[3] & 0x3) != power_state)
  740. || ((cd->response_buf[4] & 0xF) != HID_CMD_SET_POWER))
  741. rc = -EINVAL;
  742. return rc;
  743. }
  744. /*******************************************************************************
  745. * FUNCTION: pt_hid_cmd_set_power
  746. *
  747. * SUMMARY: Wrapper function for pt_hid_cmd_set_power_ that guarantees
  748. * exclusive access.
  749. *
  750. * RETURN:
  751. * 0 = success
  752. * !0 = failure
  753. *
  754. * PARAMETERS:
  755. * *cd - pointer to core data
  756. * power_state - power state to set(HID_POWER_ON/HID_POWER_SLEEP)
  757. ******************************************************************************/
  758. static int pt_hid_cmd_set_power(struct pt_core_data *cd,
  759. u8 power_state)
  760. {
  761. int rc;
  762. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  763. if (rc < 0) {
  764. pt_debug(cd->dev, DL_ERROR,
  765. "%s: fail get exclusive ex=%p own=%p\n",
  766. __func__, cd->exclusive_dev, cd->dev);
  767. return rc;
  768. }
  769. rc = pt_hid_cmd_set_power_(cd, power_state);
  770. if (release_exclusive(cd, cd->dev) < 0)
  771. pt_debug(cd->dev, DL_ERROR,
  772. "%s: fail to release exclusive\n", __func__);
  773. return rc;
  774. }
  775. static const u16 crc_table[16] = {
  776. 0x0000, 0x1021, 0x2042, 0x3063,
  777. 0x4084, 0x50a5, 0x60c6, 0x70e7,
  778. 0x8108, 0x9129, 0xa14a, 0xb16b,
  779. 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  780. };
  781. /*******************************************************************************
  782. * FUNCTION: _pt_compute_crc
  783. *
  784. * SUMMARY: Calculate CRC by CRC table.
  785. *
  786. * RETURN:
  787. * CRC calculation result
  788. *
  789. * PARAMETERS:
  790. * *buf - pointer to the data array to be calculated
  791. * size - size of data array
  792. ******************************************************************************/
  793. static u16 _pt_compute_crc(u8 *buf, u32 size)
  794. {
  795. u16 remainder = 0xFFFF;
  796. u16 xor_mask = 0x0000;
  797. u32 index;
  798. u32 byte_value;
  799. u32 table_index;
  800. u32 crc_bit_width = sizeof(u16) * 8;
  801. /* Divide the message by polynomial, via the table. */
  802. for (index = 0; index < size; index++) {
  803. byte_value = buf[index];
  804. table_index = ((byte_value >> 4) & 0x0F)
  805. ^ (remainder >> (crc_bit_width - 4));
  806. remainder = crc_table[table_index] ^ (remainder << 4);
  807. table_index = (byte_value & 0x0F)
  808. ^ (remainder >> (crc_bit_width - 4));
  809. remainder = crc_table[table_index] ^ (remainder << 4);
  810. }
  811. /* Perform the final remainder CRC. */
  812. return remainder ^ xor_mask;
  813. }
  814. u16 ccitt_Table[] = {
  815. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  816. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  817. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  818. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  819. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  820. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  821. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  822. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  823. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  824. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  825. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  826. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  827. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  828. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  829. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  830. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  831. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  832. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  833. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  834. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  835. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  836. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  837. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  838. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  839. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  840. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  841. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  842. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  843. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  844. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  845. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  846. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  847. };
  848. /*******************************************************************************
  849. * FUNCTION: crc_ccitt_calculate
  850. *
  851. * SUMMARY: Calculate CRC with ccitt standard by CRC table.
  852. *
  853. * RETURN:
  854. * CRC calculation result
  855. *
  856. * PARAMETERS:
  857. * *q - pointer to the data array to be calculated
  858. * len - size of data array
  859. ******************************************************************************/
  860. static unsigned short crc_ccitt_calculate(unsigned char *q, int len)
  861. {
  862. unsigned short crc = 0xffff;
  863. while (len-- > 0)
  864. crc = ccitt_Table[(crc >> 8 ^ *q++) & 0xff] ^ (crc << 8);
  865. return crc;
  866. }
  867. /*******************************************************************************
  868. * FUNCTION: pt_pip2_cmd_calculate_crc
  869. *
  870. * SUMMARY: Calculate the CRC of a command packet
  871. *
  872. * RETURN: void
  873. *
  874. * PARAMETERS:
  875. * *cmd - pointer to command data
  876. * extra_bytes - Extra bytes included in command length
  877. ******************************************************************************/
  878. static void pt_pip2_cmd_calculate_crc(struct pip2_cmd_structure *cmd,
  879. u8 extra_bytes)
  880. {
  881. u8 buf[PT_MAX_PIP2_MSG_SIZE + 1] = {0};
  882. unsigned short crc;
  883. buf[0] = cmd->len & 0xff;
  884. buf[1] = (cmd->len & 0xff00) >> 8;
  885. buf[2] = cmd->seq;
  886. buf[3] = cmd->id;
  887. memcpy(&buf[4], cmd->data, cmd->len - extra_bytes);
  888. /* Calculate the CRC for the first 4 bytes above and the data payload */
  889. crc = crc_ccitt_calculate(buf, 4 + (cmd->len - extra_bytes));
  890. cmd->crc[0] = (crc & 0xff00) >> 8;
  891. cmd->crc[1] = (crc & 0xff);
  892. }
  893. /*******************************************************************************
  894. * FUNCTION: pt_pip2_get_next_cmd_seq
  895. *
  896. * SUMMARY: Gets the next sequence number for a PIP2 command. The sequence
  897. * number is a 3 bit value (bits [0-2]) but because TTDL will always have
  898. * the TAG bit set (bit 3), the counter starts at 0x08 and goes to 0x0F.
  899. * If the "force_pip2_seq" holds a valid seq value (0x08-0x0F) then do not
  900. * increment, just use the forced value.
  901. *
  902. * RETURN: Next command sequence number [0x08-0x0F]
  903. *
  904. * PARAMETERS:
  905. * *cd - pointer to core data
  906. ******************************************************************************/
  907. static u8 pt_pip2_get_next_cmd_seq(struct pt_core_data *cd)
  908. {
  909. #ifdef TTDL_DIAGNOSTICS
  910. if (cd->force_pip2_seq <= 0x07) {
  911. cd->pip2_cmd_tag_seq++;
  912. if (cd->pip2_cmd_tag_seq > 0x0F)
  913. cd->pip2_cmd_tag_seq = 0x08;
  914. } else {
  915. cd->pip2_cmd_tag_seq = cd->force_pip2_seq;
  916. }
  917. #else
  918. cd->pip2_cmd_tag_seq++;
  919. if (cd->pip2_cmd_tag_seq > 0x0F)
  920. cd->pip2_cmd_tag_seq = 0x08;
  921. #endif
  922. return cd->pip2_cmd_tag_seq;
  923. }
  924. /*
  925. * Following macros are to define the response time (the interval between PIP2
  926. * command finishes sending and INT pin falls). The unit is in microsecond.
  927. * It has different time settings between the solution GPIO polling and Bus
  928. * polling due to the considration for system load.
  929. */
  930. #ifdef PT_POLL_RESP_BY_BUS
  931. #define POLL_RETRY_DEFAULT_INTERVAL 50
  932. #define PIP2_RESP_DEFAULT_TIME_MIN 50
  933. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  934. #define PIP2_RESP_FILE_WRITE_TIME_MIN 220
  935. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  936. #else
  937. #define POLL_RETRY_DEFAULT_INTERVAL 20
  938. #define PIP2_RESP_DEFAULT_TIME_MIN 20
  939. #define PIP2_RESP_DEFAULT_TIME_MAX (PT_PIP_CMD_DEFAULT_TIMEOUT * 1000)
  940. #define PIP2_RESP_FILE_WRITE_TIME_MIN 20
  941. #define PIP2_RESP_FILE_IOCTL_TIME_MAX (PT_PIP2_CMD_FILE_ERASE_TIMEOUT * 1000)
  942. #endif
  943. /*
  944. * id: the command id defined in PIP2
  945. * response_len: the (maximum) length of response.
  946. * response_time_min: minimum response time in microsecond
  947. * response_time_max: maximum response time in microsecond
  948. */
  949. static const struct pip2_cmd_response_structure pip2_cmd_response[] = {
  950. {.id = PIP2_CMD_ID_PING,
  951. .response_len = 255,
  952. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  953. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  954. {.id = PIP2_CMD_ID_STATUS,
  955. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  956. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  957. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  958. {.id = PIP2_CMD_ID_CTRL,
  959. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  960. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  961. .response_time_max = PT_PIP2_CMD_FILE_ERASE_TIMEOUT},
  962. {.id = PIP2_CMD_ID_CONFIG,
  963. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  964. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  965. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  966. {.id = PIP2_CMD_ID_CLEAR,
  967. .response_len = PIP2_EXTRA_BYTES_NUM + 0,
  968. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  969. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  970. {.id = PIP2_CMD_ID_RESET,
  971. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  972. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  973. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  974. {.id = PIP2_CMD_ID_VERSION,
  975. .response_len = PIP2_EXTRA_BYTES_NUM + 23,
  976. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  977. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  978. {.id = PIP2_CMD_ID_FILE_OPEN,
  979. .response_len = PIP2_EXTRA_BYTES_NUM + 2,
  980. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  981. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  982. {.id = PIP2_CMD_ID_FILE_CLOSE,
  983. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  984. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  985. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  986. {.id = PIP2_CMD_ID_FILE_READ,
  987. .response_len = 255,
  988. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  989. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  990. {.id = PIP2_CMD_ID_FILE_WRITE,
  991. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  992. .response_time_min = PIP2_RESP_FILE_WRITE_TIME_MIN,
  993. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  994. {.id = PIP2_CMD_ID_FILE_IOCTL,
  995. .response_len = PIP2_EXTRA_BYTES_NUM + 10,
  996. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  997. .response_time_max = PIP2_RESP_FILE_IOCTL_TIME_MAX},
  998. {.id = PIP2_CMD_ID_FLASH_INFO,
  999. .response_len = PIP2_EXTRA_BYTES_NUM + 17,
  1000. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1001. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1002. {.id = PIP2_CMD_ID_EXECUTE,
  1003. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1004. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1005. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1006. {.id = PIP2_CMD_ID_GET_LAST_ERRNO,
  1007. .response_len = PIP2_EXTRA_BYTES_NUM + 3,
  1008. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1009. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1010. {.id = PIP2_CMD_ID_EXIT_HOST_MODE,
  1011. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1012. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1013. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1014. {.id = PIP2_CMD_ID_READ_GPIO,
  1015. .response_len = PIP2_EXTRA_BYTES_NUM + 5,
  1016. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1017. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1018. {.id = PIP2_CMD_EXECUTE_SCAN,
  1019. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1020. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1021. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1022. {.id = PIP2_CMD_SET_PARAMETER,
  1023. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1024. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1025. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1026. {.id = PIP2_CMD_GET_PARAMETER,
  1027. .response_len = PIP2_EXTRA_BYTES_NUM + 7,
  1028. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1029. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1030. {.id = PIP2_CMD_SET_DDI_REG,
  1031. .response_len = PIP2_EXTRA_BYTES_NUM + 1,
  1032. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1033. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1034. {.id = PIP2_CMD_GET_DDI_REG,
  1035. .response_len = PIP2_EXTRA_BYTES_NUM + 249,
  1036. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1037. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX},
  1038. {.id = PIP2_CMD_ID_END,
  1039. .response_len = 255,
  1040. .response_time_min = PIP2_RESP_DEFAULT_TIME_MIN,
  1041. .response_time_max = PIP2_RESP_DEFAULT_TIME_MAX}
  1042. };
  1043. /*******************************************************************************
  1044. * FUNCTION: pt_pip2_get_cmd_response_len
  1045. *
  1046. * SUMMARY: Gets the expected response length based on the command ID
  1047. *
  1048. * RETURN: Expected response length
  1049. *
  1050. * PARAMETERS:
  1051. * id - Command ID (-1 means input ID is not in list of PIP2 command)
  1052. ******************************************************************************/
  1053. static int pt_pip2_get_cmd_response_len(u8 id)
  1054. {
  1055. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1056. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1057. p++;
  1058. if (p->id != PIP2_CMD_ID_END)
  1059. return p->response_len;
  1060. else
  1061. return -EPERM;
  1062. }
  1063. /*******************************************************************************
  1064. * FUNCTION: pt_pip2_get_cmd_resp_time_min
  1065. *
  1066. * SUMMARY: Gets the minimum response time (the interval between PIP2 command
  1067. * finishes sending and INT pin falls) based on the command ID
  1068. *
  1069. * RETURN: Estimated minimum response time in microsecond
  1070. *
  1071. * PARAMETERS:
  1072. * id - Command ID
  1073. ******************************************************************************/
  1074. static u32 pt_pip2_get_cmd_resp_time_min(u8 id)
  1075. {
  1076. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1077. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1078. p++;
  1079. if (p->id != PIP2_CMD_ID_END)
  1080. return p->response_time_min;
  1081. else
  1082. return PIP2_RESP_DEFAULT_TIME_MIN;
  1083. }
  1084. /*******************************************************************************
  1085. * FUNCTION: pt_pip2_get_cmd_resp_time_max
  1086. *
  1087. * SUMMARY: Gets the maximum response time (the interval between PIP2 command
  1088. * finishes sending and INT pin falls) based on the command ID
  1089. *
  1090. * RETURN: Estimated maximum response time in microsecond
  1091. *
  1092. * PARAMETERS:
  1093. * id - Command ID
  1094. ******************************************************************************/
  1095. static u32 pt_pip2_get_cmd_resp_time_max(u8 id)
  1096. {
  1097. const struct pip2_cmd_response_structure *p = pip2_cmd_response;
  1098. while ((p->id != id) && (p->id != PIP2_CMD_ID_END))
  1099. p++;
  1100. if (p->id != PIP2_CMD_ID_END)
  1101. return p->response_time_max;
  1102. else
  1103. return PIP2_RESP_DEFAULT_TIME_MAX;
  1104. }
  1105. /*******************************************************************************
  1106. * FUNCTION: pt_pip2_validate_response
  1107. *
  1108. * SUMMARY: Validate the response of PIP2 command.
  1109. *
  1110. * RETURN:
  1111. * 0 = success
  1112. * !0 = failure
  1113. *
  1114. * PARAMETERS:
  1115. * *cd - pointer to core data
  1116. * *pip2_cmd - pointer to PIP2 command to send
  1117. * *read_buf - pointer to response buffer
  1118. * actual_read_len - actual read length of the response
  1119. ******************************************************************************/
  1120. static int pt_pip2_validate_response(struct pt_core_data *cd,
  1121. struct pip2_cmd_structure *pip2_cmd, u8 *read_buf,
  1122. u16 actual_read_len)
  1123. {
  1124. int rc = 0;
  1125. u8 response_seq = 0;
  1126. u8 reserved_bits = 0;
  1127. u8 cmd_id = 0;
  1128. u8 response_bit = 0;
  1129. unsigned short calc_crc = 0;
  1130. unsigned short resp_crc = 0;
  1131. /* Verify the length of response buffer */
  1132. if (actual_read_len < PT_MIN_PIP2_PACKET_SIZE) {
  1133. pt_debug(cd->dev, DL_ERROR,
  1134. "%s cmd[0x%02X] read length ERR: read_len = %d\n",
  1135. __func__, pip2_cmd->id, actual_read_len);
  1136. rc = -EINVAL;
  1137. goto exit;
  1138. }
  1139. /* Verify the CRC */
  1140. calc_crc = crc_ccitt_calculate(read_buf, actual_read_len - 2);
  1141. resp_crc = read_buf[actual_read_len - 2] << 8;
  1142. resp_crc |= read_buf[actual_read_len - 1];
  1143. if (resp_crc != calc_crc) {
  1144. pt_debug(cd->dev, DL_ERROR,
  1145. "%s: cmd[0x%02X] CRC ERR: calc=0x%04X rsp=0x%04X\n",
  1146. __func__, pip2_cmd->id, calc_crc, resp_crc);
  1147. #ifdef TTDL_DIAGNOSTICS
  1148. cd->pip2_crc_error_count++;
  1149. #endif /* TTDL_DIAGNOSTICS */
  1150. rc = -EINVAL;
  1151. goto exit;
  1152. }
  1153. /* Verify the response bit is set */
  1154. response_bit = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x80;
  1155. if (!response_bit) {
  1156. pt_debug(cd->dev, DL_ERROR,
  1157. "%s cmd[0x%02X] response bit ERR: response_bit = %d\n",
  1158. __func__, pip2_cmd->id, response_bit);
  1159. rc = -EINVAL;
  1160. goto exit;
  1161. }
  1162. /* Verify the command ID matches from command to response */
  1163. cmd_id = read_buf[PIP2_RESP_REPORT_ID_OFFSET] & 0x7F;
  1164. if (cmd_id != pip2_cmd->id) {
  1165. pt_debug(cd->dev, DL_ERROR,
  1166. "%s cmd[0x%02X] command ID ERR: cmd_id = 0x%02X\n",
  1167. __func__, pip2_cmd->id, cmd_id);
  1168. rc = -EINVAL;
  1169. goto exit;
  1170. }
  1171. /* Verify the SEQ number matches from command to response */
  1172. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F;
  1173. if ((pip2_cmd->seq & 0x0F) != response_seq) {
  1174. pt_debug(cd->dev, DL_ERROR,
  1175. "%s cmd[0x%02X] send_seq = 0x%02X, resp_seq = 0x%02X\n",
  1176. __func__, pip2_cmd->id,
  1177. pip2_cmd->seq, response_seq);
  1178. rc = -EINVAL;
  1179. goto exit;
  1180. }
  1181. /* Verify the reserved bits are 0 */
  1182. reserved_bits = read_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0xF0;
  1183. if (reserved_bits)
  1184. pt_debug(cd->dev, DL_WARN,
  1185. "%s cmd[0x%02X] reserved_bits = 0x%02X\n",
  1186. __func__, pip2_cmd->id, reserved_bits);
  1187. exit:
  1188. if (rc)
  1189. pt_pr_buf(cd->dev, DL_WARN, cd->input_buf, actual_read_len,
  1190. "PIP RSP:");
  1191. return rc;
  1192. }
  1193. /*******************************************************************************
  1194. * FUNCTION: pt_hid_output_validate_bl_response
  1195. *
  1196. * SUMMARY: Validate the response of bootloader command.
  1197. *
  1198. * RETURN:
  1199. * 0 = success
  1200. * !0 = failure
  1201. *
  1202. * PARAMETERS:
  1203. * *cd - pointer to core data
  1204. * *hid_output - pointer to hid output data structure
  1205. ******************************************************************************/
  1206. static int pt_hid_output_validate_bl_response(
  1207. struct pt_core_data *cd,
  1208. struct pt_hid_output *hid_output)
  1209. {
  1210. u16 size;
  1211. u16 crc;
  1212. u8 status;
  1213. size = get_unaligned_le16(&cd->response_buf[0]);
  1214. if (hid_output->reset_expected && !size)
  1215. return 0;
  1216. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1217. != PT_PIP_BL_RESPONSE_REPORT_ID) {
  1218. pt_debug(cd->dev, DL_ERROR,
  1219. "%s: BL output response, wrong report_id\n", __func__);
  1220. return -EPROTO;
  1221. }
  1222. if (cd->response_buf[4] != PIP1_BL_SOP) {
  1223. pt_debug(cd->dev, DL_ERROR,
  1224. "%s: BL output response, wrong SOP\n", __func__);
  1225. return -EPROTO;
  1226. }
  1227. if (cd->response_buf[size - 1] != PIP1_BL_EOP) {
  1228. pt_debug(cd->dev, DL_ERROR,
  1229. "%s: BL output response, wrong EOP\n", __func__);
  1230. return -EPROTO;
  1231. }
  1232. crc = _pt_compute_crc(&cd->response_buf[4], size - 7);
  1233. if (cd->response_buf[size - 3] != LOW_BYTE(crc)
  1234. || cd->response_buf[size - 2] != HI_BYTE(crc)) {
  1235. pt_debug(cd->dev, DL_ERROR,
  1236. "%s: BL output response, wrong CRC 0x%X\n",
  1237. __func__, crc);
  1238. return -EPROTO;
  1239. }
  1240. status = cd->response_buf[5];
  1241. if (status) {
  1242. pt_debug(cd->dev, DL_ERROR,
  1243. "%s: BL output response, ERROR:%d\n",
  1244. __func__, status);
  1245. return -EPROTO;
  1246. }
  1247. return 0;
  1248. }
  1249. /*******************************************************************************
  1250. * FUNCTION: pt_hid_output_validate_app_response
  1251. *
  1252. * SUMMARY: Validate the response of application command.
  1253. *
  1254. * RETURN:
  1255. * 0 = success
  1256. * !0 = failure
  1257. *
  1258. * PARAMETERS:
  1259. * *cd - pointer to core data
  1260. * *hid_output - pointer to hid output data structure
  1261. ******************************************************************************/
  1262. static int pt_hid_output_validate_app_response(
  1263. struct pt_core_data *cd,
  1264. struct pt_hid_output *hid_output)
  1265. {
  1266. int command_code;
  1267. u16 size;
  1268. size = get_unaligned_le16(&cd->response_buf[0]);
  1269. if (hid_output->reset_expected && !size)
  1270. return 0;
  1271. if (cd->response_buf[PIP1_RESP_REPORT_ID_OFFSET]
  1272. != PT_PIP_NON_HID_RESPONSE_ID) {
  1273. pt_debug(cd->dev, DL_ERROR,
  1274. "%s: APP output response, wrong report_id\n", __func__);
  1275. return -EPROTO;
  1276. }
  1277. command_code = cd->response_buf[PIP1_RESP_COMMAND_ID_OFFSET]
  1278. & PIP1_RESP_COMMAND_ID_MASK;
  1279. if (command_code != hid_output->command_code) {
  1280. pt_debug(cd->dev, DL_ERROR,
  1281. "%s: APP output response, wrong command_code:%X\n",
  1282. __func__, command_code);
  1283. return -EPROTO;
  1284. }
  1285. return 0;
  1286. }
  1287. /*******************************************************************************
  1288. * FUNCTION: pt_check_set_parameter
  1289. *
  1290. * SUMMARY: Check command input and response for Set Parameter command.And
  1291. * store the parameter to the list for resume work if pass the check.
  1292. *
  1293. * PARAMETERS:
  1294. * *cd - pointer to core data
  1295. * *hid_output - pointer to hid output data structure
  1296. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1297. ******************************************************************************/
  1298. static void pt_check_set_parameter(struct pt_core_data *cd,
  1299. struct pt_hid_output *hid_output, bool raw)
  1300. {
  1301. u8 *param_buf;
  1302. u32 param_value = 0;
  1303. u8 param_size;
  1304. u8 param_id;
  1305. int i = 0;
  1306. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  1307. return;
  1308. /* Check command input for Set Parameter command */
  1309. if (raw && hid_output->length >= 10 && hid_output->length <= 13
  1310. && !memcmp(&hid_output->write_buf[0],
  1311. &cd->hid_desc.output_register,
  1312. sizeof(cd->hid_desc.output_register))
  1313. && hid_output->write_buf[4] ==
  1314. PT_PIP_NON_HID_COMMAND_ID
  1315. && hid_output->write_buf[6] ==
  1316. PIP1_CMD_ID_SET_PARAM)
  1317. param_buf = &hid_output->write_buf[7];
  1318. else if (!raw && hid_output->cmd_type == PIP1_CMD_TYPE_FW
  1319. && hid_output->command_code == PIP1_CMD_ID_SET_PARAM
  1320. && hid_output->write_length >= 3
  1321. && hid_output->write_length <= 6)
  1322. param_buf = &hid_output->write_buf[0];
  1323. else
  1324. return;
  1325. /* Get parameter ID, size and value */
  1326. param_id = param_buf[0];
  1327. param_size = param_buf[1];
  1328. if (param_size > 4) {
  1329. pt_debug(cd->dev, DL_ERROR,
  1330. "%s: Invalid parameter size\n", __func__);
  1331. return;
  1332. }
  1333. param_buf = &param_buf[2];
  1334. while (i < param_size)
  1335. param_value += *(param_buf++) << (8 * i++);
  1336. /* Check command response for Set Parameter command */
  1337. if (cd->response_buf[2] != PT_PIP_NON_HID_RESPONSE_ID
  1338. || (cd->response_buf[4] &
  1339. PIP1_RESP_COMMAND_ID_MASK) !=
  1340. PIP1_CMD_ID_SET_PARAM
  1341. || cd->response_buf[5] != param_id
  1342. || cd->response_buf[6] != param_size) {
  1343. pt_debug(cd->dev, DL_ERROR,
  1344. "%s: Set Parameter command not successful\n",
  1345. __func__);
  1346. return;
  1347. }
  1348. pt_add_parameter(cd, param_id, param_value, param_size);
  1349. }
  1350. /*******************************************************************************
  1351. * FUNCTION: pt_check_command
  1352. *
  1353. * SUMMARY: Check the output command. The function pt_check_set_parameter() is
  1354. * called here to check output command and store parameter to the list.
  1355. *
  1356. * PARAMETERS:
  1357. * *cd - pointer to core data
  1358. * *hid_output - pointer to hid output data structure
  1359. * raw - flag to show if output cmd is user cmd(1:user cmd)
  1360. ******************************************************************************/
  1361. static void pt_check_command(struct pt_core_data *cd,
  1362. struct pt_hid_output *hid_output, bool raw)
  1363. {
  1364. pt_check_set_parameter(cd, hid_output, raw);
  1365. }
  1366. /*******************************************************************************
  1367. * FUNCTION: pt_hid_output_validate_response
  1368. *
  1369. * SUMMARY: Validate the response of application or bootloader command.
  1370. *
  1371. * RETURN:
  1372. * 0 = success
  1373. * !0 = failure
  1374. *
  1375. * PARAMETERS:
  1376. * *cd - pointer to core data
  1377. * *hid_output - pointer to hid output data structure
  1378. ******************************************************************************/
  1379. static int pt_hid_output_validate_response(struct pt_core_data *cd,
  1380. struct pt_hid_output *hid_output)
  1381. {
  1382. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1383. return pt_hid_output_validate_bl_response(cd, hid_output);
  1384. return pt_hid_output_validate_app_response(cd, hid_output);
  1385. }
  1386. /*******************************************************************************
  1387. * FUNCTION: pt_hid_send_output_user_
  1388. *
  1389. * SUMMARY: Blindly send user data to the DUT.
  1390. *
  1391. * RETURN:
  1392. * 0 = success
  1393. * !0 = failure
  1394. *
  1395. * PARAMETERS:
  1396. * *cd - pointer to core data
  1397. * *hid_output - pointer to the command to send
  1398. ******************************************************************************/
  1399. static int pt_hid_send_output_user_(struct pt_core_data *cd,
  1400. struct pt_hid_output *hid_output)
  1401. {
  1402. int rc = 0;
  1403. int cmd;
  1404. if (!hid_output->length || !hid_output->write_buf)
  1405. return -EINVAL;
  1406. if (cd->pip2_prot_active) {
  1407. cmd = hid_output->write_buf[PIP2_CMD_COMMAND_ID_OFFSET];
  1408. cmd &= PIP2_CMD_COMMAND_ID_MASK;
  1409. } else
  1410. cmd = hid_output->write_buf[PIP1_CMD_COMMAND_ID_OFFSET];
  1411. pt_debug(cd->dev, DL_INFO,
  1412. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1413. __func__, hid_output->length, cmd);
  1414. pt_pr_buf(cd->dev, DL_DEBUG, hid_output->write_buf,
  1415. hid_output->length, ">>> User CMD");
  1416. rc = pt_adap_write_read_specific(cd, hid_output->length,
  1417. hid_output->write_buf, NULL);
  1418. if (rc)
  1419. pt_debug(cd->dev, DL_ERROR,
  1420. "%s: Fail pt_adap_transfer\n", __func__);
  1421. return rc;
  1422. }
  1423. /*******************************************************************************
  1424. * FUNCTION: pt_hid_send_output_user_and_wait_
  1425. *
  1426. * SUMMARY: Blindly send user data to the DUT and wait for the response.
  1427. *
  1428. * RETURN:
  1429. * 0 = success
  1430. * !0 = failure
  1431. *
  1432. * PARAMETERS:
  1433. * *cd - pointer to core data
  1434. * *hid_output - pointer to the command to send
  1435. ******************************************************************************/
  1436. static int pt_hid_send_output_user_and_wait_(struct pt_core_data *cd,
  1437. struct pt_hid_output *hid_output)
  1438. {
  1439. int rc = 0;
  1440. int t;
  1441. mutex_lock(&cd->system_lock);
  1442. cd->hid_cmd_state = PIP1_CMD_ID_USER_CMD + 1;
  1443. mutex_unlock(&cd->system_lock);
  1444. rc = pt_hid_send_output_user_(cd, hid_output);
  1445. if (rc)
  1446. goto error;
  1447. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1448. msecs_to_jiffies(cd->pip_cmd_timeout));
  1449. if (IS_TMO(t)) {
  1450. #ifdef TTDL_DIAGNOSTICS
  1451. cd->bus_transmit_error_count++;
  1452. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1453. #endif /* TTDL_DIAGNOSTICS */
  1454. pt_debug(cd->dev, DL_ERROR,
  1455. "%s: HID output cmd execution timed out\n",
  1456. __func__);
  1457. rc = -ETIME;
  1458. goto error;
  1459. }
  1460. pt_check_command(cd, hid_output, true);
  1461. goto exit;
  1462. error:
  1463. mutex_lock(&cd->system_lock);
  1464. cd->hid_cmd_state = 0;
  1465. mutex_unlock(&cd->system_lock);
  1466. exit:
  1467. return rc;
  1468. }
  1469. /*******************************************************************************
  1470. * FUNCTION: pt_check_irq_asserted
  1471. *
  1472. * SUMMARY: Checks if the IRQ GPIO is asserted or not. There are times when
  1473. * the FW can hold the INT line low ~150us after the read is complete.
  1474. * NOTE: if irq_stat is not defined this function will return false
  1475. *
  1476. * RETURN:
  1477. * true = IRQ asserted
  1478. * false = IRQ not asserted
  1479. *
  1480. * PARAMETERS:
  1481. * *cd - pointer to core data
  1482. ******************************************************************************/
  1483. static bool pt_check_irq_asserted(struct pt_core_data *cd)
  1484. {
  1485. #ifdef ENABLE_WORKAROUND_FOR_GLITCH_AFTER_BL_LAUNCH_APP
  1486. /*
  1487. * Workaround for FW defect, CDT165308
  1488. * bl_launch app creates a glitch in IRQ line
  1489. */
  1490. if (cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1
  1491. && cd->cpdata->irq_stat) {
  1492. /*
  1493. * in X1S panel and GC1546 panel, the width for the INT
  1494. * glitch is about 4us,the normal INT width of response
  1495. * will last more than 200us, so use 10us delay
  1496. * for distinguish the glitch the normal INT is enough.
  1497. */
  1498. udelay(10);
  1499. }
  1500. #endif
  1501. if (cd->cpdata->irq_stat) {
  1502. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1503. == PT_IRQ_ASSERTED_VALUE) {
  1504. /* Debounce to allow FW to release INT */
  1505. usleep_range(100, 200);
  1506. }
  1507. if (cd->cpdata->irq_stat(cd->cpdata, cd->dev)
  1508. == PT_IRQ_ASSERTED_VALUE)
  1509. return true;
  1510. else
  1511. return false;
  1512. }
  1513. return true;
  1514. }
  1515. /*******************************************************************************
  1516. * FUNCTION: pt_flush_bus
  1517. *
  1518. * SUMMARY: Force flushing the bus by reading len bytes or forced 255 bytes
  1519. * Used if IRQ is found to be stuck low
  1520. *
  1521. * RETURN: Length of bytes read from bus
  1522. *
  1523. * PARAMETERS:
  1524. * *cd - pointer to core data
  1525. * flush_type - type of flush
  1526. * - PT_FLUSH_BUS_BASED_ON_LEN (two reads)
  1527. * - PT_FLUSH_BUS_FULL_256_READ
  1528. * *read_buf - pointer to store read data
  1529. ******************************************************************************/
  1530. static ssize_t pt_flush_bus(struct pt_core_data *cd,
  1531. u8 flush_type, u8 *read_buf)
  1532. {
  1533. u8 buf[PT_MAX_PIP2_MSG_SIZE];
  1534. u16 pip_len;
  1535. int bytes_read;
  1536. int rc = 0;
  1537. if (flush_type == PT_FLUSH_BUS_BASED_ON_LEN) {
  1538. rc = pt_adap_read_default(cd, buf, 2);
  1539. if (rc) {
  1540. bytes_read = 0;
  1541. goto exit;
  1542. }
  1543. pip_len = get_unaligned_le16(&buf[0]);
  1544. if (pip_len == 2 || pip_len >= PT_PIP_1P7_EMPTY_BUF) {
  1545. #ifdef TTDL_DIAGNOSTICS
  1546. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EMPTY_PACKET);
  1547. #endif
  1548. bytes_read = 2;
  1549. pt_debug(cd->dev, DL_INFO,
  1550. "%s: Empty buf detected - len=0x%04X\n",
  1551. __func__, pip_len);
  1552. } else if (pip_len == 0) {
  1553. bytes_read = 0;
  1554. pt_debug(cd->dev, DL_INFO,
  1555. "%s: Sentinel detected\n", __func__);
  1556. } else if (pip_len > PT_MAX_PIP2_MSG_SIZE) {
  1557. pt_debug(cd->dev, DL_ERROR,
  1558. "%s: Illegal len=0x%04x, force %d byte read\n",
  1559. __func__, pip_len, PT_MAX_PIP2_MSG_SIZE);
  1560. rc = pt_adap_read_default(cd, buf,
  1561. PT_MAX_PIP2_MSG_SIZE);
  1562. if (!rc)
  1563. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1564. else
  1565. bytes_read = 0;
  1566. } else {
  1567. pt_debug(cd->dev, DL_INFO,
  1568. "%s: Flush read of %d bytes...\n",
  1569. __func__, pip_len);
  1570. rc = pt_adap_read_default(cd, buf, pip_len);
  1571. if (!rc)
  1572. bytes_read = pip_len;
  1573. else
  1574. bytes_read = 0;
  1575. }
  1576. } else {
  1577. pt_debug(cd->dev, DL_INFO,
  1578. "%s: Forced flush of max %d bytes...\n",
  1579. __func__, PT_MAX_PIP2_MSG_SIZE);
  1580. rc = pt_adap_read_default(cd, buf, PT_MAX_PIP2_MSG_SIZE);
  1581. if (!rc)
  1582. bytes_read = PT_MAX_PIP2_MSG_SIZE;
  1583. else
  1584. bytes_read = 0;
  1585. }
  1586. if (read_buf && (bytes_read > 3))
  1587. memcpy(read_buf, buf, bytes_read);
  1588. exit:
  1589. return bytes_read;
  1590. }
  1591. /*******************************************************************************
  1592. * FUNCTION: pt_flush_bus_if_irq_asserted
  1593. *
  1594. * SUMMARY: This function will flush the active bus if the INT is found to be
  1595. * asserted.
  1596. *
  1597. * RETURN: bytes cleared from bus
  1598. *
  1599. * PARAMETERS:
  1600. * *cd - pointer the core data structure
  1601. * flush_type - type of flush
  1602. * - PT_FLUSH_BUS_BASED_ON_LEN
  1603. * - PT_FLUSH_BUS_FULL_256_READ
  1604. ******************************************************************************/
  1605. static int pt_flush_bus_if_irq_asserted(struct pt_core_data *cd, u8 flush_type)
  1606. {
  1607. int count = 0;
  1608. int bytes_read = 0;
  1609. while (pt_check_irq_asserted(cd) && count < 5) {
  1610. count++;
  1611. bytes_read = pt_flush_bus(cd, flush_type, NULL);
  1612. if (bytes_read) {
  1613. pt_debug(cd->dev, DL_WARN,
  1614. "%s: Cleared %d bytes off bus\n",
  1615. __func__, bytes_read);
  1616. }
  1617. }
  1618. if (pt_check_irq_asserted(cd)) {
  1619. pt_debug(cd->dev, DL_ERROR,
  1620. "%s: IRQ still asserted, %d bytes read\n",
  1621. __func__, bytes_read);
  1622. } else {
  1623. pt_debug(cd->dev, DL_INFO,
  1624. "%s: IRQ cleared, %d bytes read\n",
  1625. __func__, bytes_read);
  1626. }
  1627. return bytes_read;
  1628. }
  1629. /*******************************************************************************
  1630. * FUNCTION: pt_hid_send_output_
  1631. *
  1632. * SUMMARY: Send a touch application command to the DUT
  1633. *
  1634. * RETURN:
  1635. * 0 = success
  1636. * !0 = failure
  1637. *
  1638. * PARAMETERS:
  1639. * *cd - pointer to core data
  1640. * *hid_output - pointer to the command to send
  1641. ******************************************************************************/
  1642. static int pt_hid_send_output_(struct pt_core_data *cd,
  1643. struct pt_hid_output *hid_output)
  1644. {
  1645. int rc = 0;
  1646. u8 *cmd;
  1647. u16 length;
  1648. u16 crc;
  1649. u8 report_id;
  1650. u8 cmd_offset = 0;
  1651. u8 cmd_allocated = 0;
  1652. switch (hid_output->cmd_type) {
  1653. case PIP1_CMD_TYPE_FW:
  1654. report_id = PT_PIP_NON_HID_COMMAND_ID;
  1655. length = 5;
  1656. break;
  1657. case PIP1_CMD_TYPE_BL:
  1658. report_id = PT_PIP_BL_COMMAND_REPORT_ID;
  1659. length = 11 /* 5 + SOP + LEN(2) + CRC(2) + EOP */;
  1660. break;
  1661. default:
  1662. return -EINVAL;
  1663. }
  1664. length += hid_output->write_length;
  1665. if (length + 2 > PT_PREALLOCATED_CMD_BUFFER) {
  1666. cmd = kzalloc(length + 2, GFP_KERNEL);
  1667. if (!cmd)
  1668. return -ENOMEM;
  1669. cmd_allocated = 1;
  1670. } else {
  1671. cmd = cd->cmd_buf;
  1672. }
  1673. /* Set Output register */
  1674. memcpy(&cmd[cmd_offset], &cd->hid_desc.output_register,
  1675. sizeof(cd->hid_desc.output_register));
  1676. cmd_offset += sizeof(cd->hid_desc.output_register);
  1677. cmd[cmd_offset++] = LOW_BYTE(length);
  1678. cmd[cmd_offset++] = HI_BYTE(length);
  1679. cmd[cmd_offset++] = report_id;
  1680. cmd[cmd_offset++] = 0x0; /* reserved */
  1681. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL)
  1682. cmd[cmd_offset++] = PIP1_BL_SOP;
  1683. cmd[cmd_offset++] = hid_output->command_code;
  1684. /* Set Data Length for bootloader */
  1685. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1686. cmd[cmd_offset++] = LOW_BYTE(hid_output->write_length);
  1687. cmd[cmd_offset++] = HI_BYTE(hid_output->write_length);
  1688. }
  1689. /* Set Data */
  1690. if (hid_output->write_length && hid_output->write_buf) {
  1691. memcpy(&cmd[cmd_offset], hid_output->write_buf,
  1692. hid_output->write_length);
  1693. cmd_offset += hid_output->write_length;
  1694. }
  1695. if (hid_output->cmd_type == PIP1_CMD_TYPE_BL) {
  1696. crc = _pt_compute_crc(&cmd[6],
  1697. hid_output->write_length + 4);
  1698. cmd[cmd_offset++] = LOW_BYTE(crc);
  1699. cmd[cmd_offset++] = HI_BYTE(crc);
  1700. cmd[cmd_offset++] = PIP1_BL_EOP;
  1701. }
  1702. pt_debug(cd->dev, DL_INFO,
  1703. ">>> %s: Write Buffer Size[%d] Cmd[0x%02X]\n",
  1704. __func__, length + 2, hid_output->command_code);
  1705. pt_pr_buf(cd->dev, DL_DEBUG, cmd, length + 2, ">>> CMD");
  1706. rc = pt_adap_write_read_specific(cd, length + 2, cmd, NULL);
  1707. if (rc)
  1708. pt_debug(cd->dev, DL_ERROR,
  1709. "%s: Fail pt_adap_transfer rc=%d\n", __func__, rc);
  1710. if (cmd_allocated)
  1711. kfree(cmd);
  1712. return rc;
  1713. }
  1714. /*******************************************************************************
  1715. * FUNCTION: pt_pip1_send_output_and_wait_
  1716. *
  1717. * SUMMARY: Send valid PIP1 command to the DUT and wait for the response.
  1718. *
  1719. * RETURN:
  1720. * 0 = success
  1721. * !0 = failure
  1722. *
  1723. * PARAMETERS:
  1724. * *cd - pointer to core data
  1725. * *hid_output - pointer to the command to send
  1726. ******************************************************************************/
  1727. static int pt_pip1_send_output_and_wait_(struct pt_core_data *cd,
  1728. struct pt_hid_output *hid_output)
  1729. {
  1730. int rc = 0;
  1731. int t;
  1732. u16 timeout_ms;
  1733. mutex_lock(&cd->system_lock);
  1734. cd->hid_cmd_state = hid_output->command_code + 1;
  1735. mutex_unlock(&cd->system_lock);
  1736. if (hid_output->timeout_ms)
  1737. timeout_ms = hid_output->timeout_ms;
  1738. else
  1739. timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT;
  1740. rc = pt_hid_send_output_(cd, hid_output);
  1741. if (rc)
  1742. goto error;
  1743. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  1744. msecs_to_jiffies(timeout_ms));
  1745. if (IS_TMO(t)) {
  1746. #ifdef TTDL_DIAGNOSTICS
  1747. cd->bus_transmit_error_count++;
  1748. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  1749. #endif /* TTDL_DIAGNOSTICS */
  1750. pt_debug(cd->dev, DL_ERROR,
  1751. "%s: HID output cmd execution timed out (%dms)\n",
  1752. __func__, timeout_ms);
  1753. rc = -ETIME;
  1754. goto error;
  1755. }
  1756. if (!hid_output->novalidate)
  1757. rc = pt_hid_output_validate_response(cd, hid_output);
  1758. pt_check_command(cd, hid_output, false);
  1759. goto exit;
  1760. error:
  1761. mutex_lock(&cd->system_lock);
  1762. cd->hid_cmd_state = 0;
  1763. mutex_unlock(&cd->system_lock);
  1764. exit:
  1765. return rc;
  1766. }
  1767. /*******************************************************************************
  1768. * FUNCTION: pt_hid_output_user_cmd_
  1769. *
  1770. * SUMMARY: Load the write buffer into a HID structure and send it as a HID cmd
  1771. * to the DUT waiting for the response and loading it into the read buffer
  1772. *
  1773. * RETURN:
  1774. * 0 = success
  1775. * !0 = failure
  1776. *
  1777. * PARAMETERS:
  1778. * *cd - pointer to core data
  1779. * read_len - expected read length of the response
  1780. * *read_buf - pointer to where the response will be loaded
  1781. * write_len - length of the write buffer
  1782. * *write_buf - pointer to the write buffer
  1783. * *actual_read_len - pointer to the actual amount of data read back
  1784. ******************************************************************************/
  1785. static int pt_hid_output_user_cmd_(struct pt_core_data *cd,
  1786. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1787. u16 *actual_read_len)
  1788. {
  1789. int rc = 0;
  1790. u16 size;
  1791. struct pt_hid_output hid_output = {
  1792. .length = write_len,
  1793. .write_buf = write_buf,
  1794. };
  1795. #ifdef TTHE_TUNER_SUPPORT
  1796. if (!cd->pip2_send_user_cmd) {
  1797. int command_code = 0;
  1798. int len;
  1799. /* Print up to cmd ID */
  1800. len = PIP1_CMD_COMMAND_ID_OFFSET + 1;
  1801. if (write_len < len)
  1802. len = write_len;
  1803. else
  1804. command_code = write_buf[PIP1_CMD_COMMAND_ID_OFFSET]
  1805. & PIP1_CMD_COMMAND_ID_MASK;
  1806. /* Don't print EXEC_PANEL_SCAN & RETRIEVE_PANEL_SCAN commands */
  1807. if (command_code != PIP1_CMD_ID_EXEC_PANEL_SCAN &&
  1808. command_code != PIP1_CMD_ID_RETRIEVE_PANEL_SCAN)
  1809. tthe_print(cd, write_buf, len, "CMD=");
  1810. }
  1811. #endif
  1812. rc = pt_hid_send_output_user_and_wait_(cd, &hid_output);
  1813. if (rc)
  1814. return rc;
  1815. /* Get the response size from the first 2 bytes in the response */
  1816. size = get_unaligned_le16(&cd->response_buf[0]);
  1817. /* Ensure size is not greater than max buffer size */
  1818. if (size > PT_MAX_PIP2_MSG_SIZE)
  1819. size = PT_MAX_PIP2_MSG_SIZE;
  1820. /* Minimum size to read is the 2 byte len field */
  1821. if (size == 0)
  1822. size = 2;
  1823. if (size > read_len) {
  1824. pt_debug(cd->dev, DL_ERROR,
  1825. "%s: PIP2 len field=%d, requested read_len=%d\n",
  1826. __func__, size, read_len);
  1827. *actual_read_len = 0;
  1828. return -EIO;
  1829. }
  1830. memcpy(read_buf, cd->response_buf, size);
  1831. *actual_read_len = size;
  1832. return 0;
  1833. }
  1834. /*******************************************************************************
  1835. * FUNCTION: pt_hid_output_user_cmd
  1836. *
  1837. * SUMMARY: Protected call to pt_hid_output_user_cmd_ by exclusive access to
  1838. * the DUT.
  1839. *
  1840. * RETURN:
  1841. * 0 = success
  1842. * !0 = failure
  1843. *
  1844. * PARAMETERS:
  1845. * *cd - pointer to core data
  1846. * read_len - length of data to read
  1847. * *read_buf - pointer to store read data
  1848. * write_len - length of data to write
  1849. * *write_buf - pointer to buffer to write
  1850. * *actual_read_len - pointer to store data length actually read
  1851. ******************************************************************************/
  1852. static int pt_hid_output_user_cmd(struct pt_core_data *cd,
  1853. u16 read_len, u8 *read_buf, u16 write_len, u8 *write_buf,
  1854. u16 *actual_read_len)
  1855. {
  1856. int rc;
  1857. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  1858. if (rc < 0) {
  1859. pt_debug(cd->dev, DL_ERROR,
  1860. "%s: fail get exclusive ex=%p own=%p\n",
  1861. __func__, cd->exclusive_dev, cd->dev);
  1862. return rc;
  1863. }
  1864. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1865. write_len, write_buf, actual_read_len);
  1866. if (release_exclusive(cd, cd->dev) < 0)
  1867. pt_debug(cd->dev, DL_ERROR,
  1868. "%s: fail to release exclusive\n", __func__);
  1869. return rc;
  1870. }
  1871. /*******************************************************************************
  1872. * FUNCTION: _pt_request_pip2_send_cmd
  1873. *
  1874. * SUMMARY: Writes a PIP2 command packet to DUT, then waits for the
  1875. * interrupt and reads response data to read_buf
  1876. *
  1877. * RETURN:
  1878. * 0 = success
  1879. * !0 = failure
  1880. *
  1881. * PARAMETERS:
  1882. * *dev - pointer to device structure
  1883. * protect - flag to run in protected mode
  1884. * id - ID of PIP command
  1885. * *data - pointer to PIP data payload
  1886. * report_body_len - report length
  1887. * *read_buf - pointer to response buffer
  1888. * *actual_read_len - pointer to response buffer length
  1889. ******************************************************************************/
  1890. static int _pt_request_pip2_send_cmd(struct device *dev,
  1891. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  1892. u16 *actual_read_len)
  1893. {
  1894. struct pt_core_data *cd = dev_get_drvdata(dev);
  1895. struct pip2_cmd_structure pip2_cmd;
  1896. int rc = 0;
  1897. int i = 0;
  1898. int j = 0;
  1899. u16 write_len;
  1900. u8 *write_buf = NULL;
  1901. u16 read_len;
  1902. u8 extra_bytes;
  1903. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  1904. /* Hard coded register for PIP2.x */
  1905. pip2_cmd.reg[0] = 0x01;
  1906. pip2_cmd.reg[1] = 0x01;
  1907. /*
  1908. * For PIP2.1+ the length field value includes itself:
  1909. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  1910. *
  1911. * The overall write length must include only the register:
  1912. * ADD 2: 2 (Register)
  1913. */
  1914. extra_bytes = 6;
  1915. write_len = 2;
  1916. /* PIP2 the CMD ID is a 7bit field */
  1917. if (id > PIP2_CMD_ID_END) {
  1918. pt_debug(dev, DL_WARN, "%s: Invalid PIP2 CMD ID 0x%02X\n",
  1919. __func__, id);
  1920. rc = -EINVAL;
  1921. goto exit;
  1922. }
  1923. pip2_cmd.len = report_body_len + extra_bytes;
  1924. pip2_cmd.id = id & PIP2_CMD_COMMAND_ID_MASK;
  1925. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  1926. pip2_cmd.data = data;
  1927. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  1928. /* Add the command length to the extra bytes based on PIP version */
  1929. write_len += pip2_cmd.len;
  1930. pt_debug(dev, DL_INFO, "%s Length Field: %d, Write Len: %d",
  1931. __func__, pip2_cmd.len, write_len);
  1932. write_buf = kzalloc(write_len, GFP_KERNEL);
  1933. if (write_buf == NULL) {
  1934. rc = -ENOMEM;
  1935. goto exit;
  1936. }
  1937. write_buf[i++] = pip2_cmd.reg[0];
  1938. write_buf[i++] = pip2_cmd.reg[1];
  1939. write_buf[i++] = pip2_cmd.len & 0xff;
  1940. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  1941. write_buf[i++] = pip2_cmd.seq;
  1942. write_buf[i++] = pip2_cmd.id;
  1943. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  1944. write_buf[j] = pip2_cmd.data[j-i];
  1945. write_buf[j++] = pip2_cmd.crc[0];
  1946. write_buf[j++] = pip2_cmd.crc[1];
  1947. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  1948. if (read_len < 0)
  1949. read_len = 255;
  1950. pt_debug(dev, DL_INFO,
  1951. "%s cmd_id[0x%02X] expected response length:%d ",
  1952. __func__, pip2_cmd.id, read_len);
  1953. /*
  1954. * All PIP2 commands come through this function.
  1955. * Set flag for PIP2.x interface to allow response parsing to know
  1956. * how to decode the protocol header.
  1957. */
  1958. mutex_lock(&cd->system_lock);
  1959. cd->pip2_prot_active = true;
  1960. cd->pip2_send_user_cmd = true;
  1961. mutex_unlock(&cd->system_lock);
  1962. if (protect == PT_CORE_CMD_PROTECTED)
  1963. rc = pt_hid_output_user_cmd(cd, read_len, read_buf,
  1964. write_len, write_buf, actual_read_len);
  1965. else {
  1966. rc = pt_hid_output_user_cmd_(cd, read_len, read_buf,
  1967. write_len, write_buf, actual_read_len);
  1968. }
  1969. if (rc) {
  1970. pt_debug(dev, DL_ERROR,
  1971. "%s: nonhid_cmd->user_cmd() Error = %d\n",
  1972. __func__, rc);
  1973. goto exit;
  1974. }
  1975. rc = pt_pip2_validate_response(cd, &pip2_cmd, read_buf,
  1976. *actual_read_len);
  1977. exit:
  1978. mutex_lock(&cd->system_lock);
  1979. cd->pip2_prot_active = false;
  1980. cd->pip2_send_user_cmd = false;
  1981. mutex_unlock(&cd->system_lock);
  1982. kfree(write_buf);
  1983. return rc;
  1984. }
  1985. /*******************************************************************************
  1986. * FUNCTION: _pt_pip2_send_cmd_no_int
  1987. *
  1988. * SUMMARY: Writes a PIP2 command packet to DUT, then poll the response and
  1989. * reads response data to read_buf if response is available.
  1990. *
  1991. * NOTE:
  1992. * Interrupt MUST be disabled before to call this function.
  1993. *
  1994. * RETURN:
  1995. * 0 = success
  1996. * !0 = failure
  1997. *
  1998. * PARAMETERS:
  1999. * *dev - pointer to device structure
  2000. * protect - flag to run in protected mode
  2001. * id - ID of PIP command
  2002. * *data - pointer to PIP data payload
  2003. * report_body_len - report length
  2004. * *read_buf - pointer to response buffer
  2005. * *actual_read_len - pointer to response buffer length
  2006. ******************************************************************************/
  2007. static int _pt_pip2_send_cmd_no_int(struct device *dev,
  2008. int protect, u8 id, u8 *data, u16 report_body_len, u8 *read_buf,
  2009. u16 *actual_read_len)
  2010. {
  2011. int max_retry = 0;
  2012. int retry = 0;
  2013. int rc = 0;
  2014. int i = 0;
  2015. int j = 0;
  2016. u16 write_len;
  2017. u8 *write_buf = NULL;
  2018. u16 read_len;
  2019. u16 size = 0;
  2020. u8 response_seq = 0;
  2021. u8 extra_bytes;
  2022. u32 retry_interval = 0;
  2023. u32 retry_total_time = 0;
  2024. u32 resp_time_min = pt_pip2_get_cmd_resp_time_min(id);
  2025. u32 resp_time_max = pt_pip2_get_cmd_resp_time_max(id);
  2026. struct pt_core_data *cd = dev_get_drvdata(dev);
  2027. struct pip2_cmd_structure pip2_cmd;
  2028. if (protect == PT_CORE_CMD_PROTECTED) {
  2029. rc = request_exclusive(cd,
  2030. cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2031. if (rc < 0) {
  2032. pt_debug(cd->dev, DL_ERROR,
  2033. "%s: fail get exclusive ex=%p own=%p\n",
  2034. __func__, cd->exclusive_dev, cd->dev);
  2035. return rc;
  2036. }
  2037. }
  2038. memset(&pip2_cmd, 0, sizeof(pip2_cmd));
  2039. /* Hard coded register for PIP2.x */
  2040. pip2_cmd.reg[0] = 0x01;
  2041. pip2_cmd.reg[1] = 0x01;
  2042. /*
  2043. * For PIP2.1+ the length field value includes itself:
  2044. * ADD 6: 2 (LEN) + 1 (SEQ) + 1 (REPORT ID) + 2 (CRC)
  2045. *
  2046. * The overall write length must include only the register:
  2047. * ADD 2: 2 (Register)
  2048. */
  2049. extra_bytes = 6;
  2050. write_len = 2;
  2051. pip2_cmd.len = report_body_len + extra_bytes;
  2052. pip2_cmd.id = id;
  2053. pip2_cmd.seq = pt_pip2_get_next_cmd_seq(cd);
  2054. pip2_cmd.data = data;
  2055. pt_pip2_cmd_calculate_crc(&pip2_cmd, extra_bytes);
  2056. /* Add the command length to the extra bytes based on PIP version */
  2057. write_len += pip2_cmd.len;
  2058. write_buf = kzalloc(write_len, GFP_KERNEL);
  2059. if (write_buf == NULL) {
  2060. rc = -ENOMEM;
  2061. goto exit;
  2062. }
  2063. write_buf[i++] = pip2_cmd.reg[0];
  2064. write_buf[i++] = pip2_cmd.reg[1];
  2065. write_buf[i++] = pip2_cmd.len & 0xff;
  2066. write_buf[i++] = (pip2_cmd.len & 0xff00) >> 8;
  2067. write_buf[i++] = pip2_cmd.seq;
  2068. write_buf[i++] = pip2_cmd.id;
  2069. for (j = i; j < i + pip2_cmd.len - extra_bytes; j++)
  2070. write_buf[j] = pip2_cmd.data[j-i];
  2071. write_buf[j++] = pip2_cmd.crc[0];
  2072. write_buf[j++] = pip2_cmd.crc[1];
  2073. read_len = pt_pip2_get_cmd_response_len(pip2_cmd.id);
  2074. if (read_len < 0)
  2075. read_len = 255;
  2076. pt_debug(dev, DL_INFO,
  2077. "%s: ATM - cmd_id[0x%02X] expected response length:%d ",
  2078. __func__, pip2_cmd.id, read_len);
  2079. pt_pr_buf(cd->dev, DL_DEBUG, write_buf, write_len, ">>> NO_INT CMD");
  2080. rc = pt_adap_write_read_specific(cd, write_len, write_buf, NULL);
  2081. if (rc) {
  2082. pt_debug(dev, DL_ERROR,
  2083. "%s: SPI write Error = %d\n",
  2084. __func__, rc);
  2085. goto exit;
  2086. }
  2087. #ifdef PT_POLL_RESP_BY_BUS
  2088. /*
  2089. * Frequent bus read can increase system load obviously. The expected
  2090. * first bus read should be valid and timely. The tollerance for
  2091. * usleep_range should be limited. The minimum response delay (between
  2092. * command finishes sending and INT pin falls) is less than 50
  2093. * microseconds. So the 10 microseconds should be maximum tollerance
  2094. * with the consideration that the unit to calculate the response delay
  2095. * is 10 microseconds and more precise is not necessary. Every
  2096. * additional 10 microseconds only contribute less than 3 milliseconds
  2097. * for whole BL.
  2098. */
  2099. usleep_range(resp_time_min, resp_time_min+10);
  2100. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2101. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2102. rc = pt_adap_read_default(cd, read_buf, read_len);
  2103. if (rc) {
  2104. pt_debug(dev, DL_ERROR, "%s: SPI read Error = %d\n",
  2105. __func__, rc);
  2106. break;
  2107. }
  2108. response_seq = read_buf[PIP2_RESP_SEQUENCE_OFFSET];
  2109. size = get_unaligned_le16(&read_buf[0]);
  2110. if ((size <= read_len) &&
  2111. (size >= PIP2_EXTRA_BYTES_NUM) &&
  2112. (pip2_cmd.seq & 0x07) == (response_seq & 0x07)) {
  2113. break;
  2114. }
  2115. /*
  2116. * To reduce the bus and system load, increase the sleep
  2117. * step gradually:
  2118. * 1 ~ 19 : step=50 us, sleep_us=[50, 100, 150, 200, ..950]
  2119. * 20 ~ 39 : step=1000 us, sleep_us=[1950, 2950, ...20950]
  2120. * 40 ~ MAX: step=50 ms, sleep_ms=[71, 121, 191,..]
  2121. */
  2122. retry++;
  2123. if (retry < 20) {
  2124. retry_interval += POLL_RETRY_DEFAULT_INTERVAL;
  2125. usleep_range(retry_interval,
  2126. retry_interval + POLL_RETRY_DEFAULT_INTERVAL);
  2127. } else if (retry < 40) {
  2128. retry_interval += 1000;
  2129. usleep_range(retry_interval, retry_interval + 1000);
  2130. } else {
  2131. retry_interval += 50000;
  2132. msleep(retry_interval/1000);
  2133. }
  2134. retry_total_time += retry_interval;
  2135. }
  2136. #else
  2137. /*
  2138. * Frequent GPIO read will not increase CPU/system load heavily if the
  2139. * interval is longer than 10 us, so it is safe to poll GPIO with a
  2140. * fixed interval: 20 us.
  2141. */
  2142. usleep_range(resp_time_min, resp_time_min+10);
  2143. max_retry = resp_time_max / POLL_RETRY_DEFAULT_INTERVAL;
  2144. while ((retry < max_retry) && (retry_total_time < resp_time_max)) {
  2145. if (!gpio_get_value(cd->cpdata->irq_gpio)) {
  2146. rc = pt_adap_read_default(cd, read_buf, read_len);
  2147. size = get_unaligned_le16(&read_buf[0]);
  2148. if (rc)
  2149. pt_debug(dev, DL_ERROR,
  2150. "%s: SPI read Error = %d\n",
  2151. __func__, rc);
  2152. else if (size > read_len) {
  2153. pt_debug(cd->dev, DL_ERROR,
  2154. "%s: PIP2 len field=%d, requested read_len=%d\n",
  2155. __func__, size, read_len);
  2156. rc = -EIO;
  2157. }
  2158. break;
  2159. }
  2160. /*
  2161. * Poll GPIO with fixed interval 20 us, and tollerance is
  2162. * limited to 10 us to speed up the process.
  2163. */
  2164. retry_interval = POLL_RETRY_DEFAULT_INTERVAL;
  2165. usleep_range(retry_interval, retry_interval+10);
  2166. retry_total_time += retry_interval;
  2167. }
  2168. #endif
  2169. *actual_read_len = size;
  2170. if (rc || (retry >= max_retry) || (retry_total_time >= resp_time_max)) {
  2171. pt_debug(dev, DL_ERROR,
  2172. "%s cmd[0x%02X] timed out, send_seq=0x%02X, resp_seq=0x%02X\n",
  2173. __func__, pip2_cmd.id, pip2_cmd.seq, response_seq);
  2174. *actual_read_len = 0;
  2175. rc = -EINVAL;
  2176. }
  2177. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, *actual_read_len,
  2178. "<<< NO_INT Read");
  2179. exit:
  2180. kfree(write_buf);
  2181. if (protect == PT_CORE_CMD_PROTECTED) {
  2182. if (release_exclusive(cd, cd->dev) < 0)
  2183. pt_debug(cd->dev, DL_ERROR,
  2184. "%s: fail to release exclusive\n", __func__);
  2185. }
  2186. return rc;
  2187. }
  2188. /*******************************************************************************
  2189. * FUNCTION: pt_pip_null_
  2190. *
  2191. * SUMMARY: Send the PIP "ping"(0x00) command to the DUT and wait for response.
  2192. * This function is used by watchdog to check if the fw corrupts.
  2193. *
  2194. * RETURN:
  2195. * 0 = success
  2196. * !0 = failure
  2197. *
  2198. * PARAMETERS:
  2199. * *cd - pointer to core data
  2200. ******************************************************************************/
  2201. static int pt_pip_null_(struct pt_core_data *cd)
  2202. {
  2203. struct pt_hid_output hid_output = {
  2204. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_NULL),
  2205. };
  2206. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  2207. }
  2208. /*******************************************************************************
  2209. * FUNCTION: pt_pip_null
  2210. *
  2211. * SUMMARY: Wrapper function for pt_pip_null_ that guarantees exclusive access.
  2212. *
  2213. * RETURN:
  2214. * 0 = success
  2215. * !0 = failure
  2216. *
  2217. * PARAMETERS:
  2218. * *cd - pointer to core data
  2219. ******************************************************************************/
  2220. static int pt_pip_null(struct pt_core_data *cd)
  2221. {
  2222. int rc;
  2223. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2224. if (rc < 0) {
  2225. pt_debug(cd->dev, DL_ERROR,
  2226. "%s: fail get exclusive ex=%p own=%p\n",
  2227. __func__, cd->exclusive_dev, cd->dev);
  2228. return rc;
  2229. }
  2230. rc = pt_pip_null_(cd);
  2231. if (release_exclusive(cd, cd->dev) < 0)
  2232. pt_debug(cd->dev, DL_ERROR,
  2233. "%s: fail to release exclusive\n", __func__);
  2234. return rc;
  2235. }
  2236. static void pt_stop_wd_timer(struct pt_core_data *cd);
  2237. /*******************************************************************************
  2238. * FUNCTION: pt_pip_start_bootloader_
  2239. *
  2240. * SUMMARY: Sends the HID command start_bootloader [PIP cmd 0x01] to the DUT
  2241. *
  2242. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  2243. * the WD active could cause this function to fail!
  2244. *
  2245. * RETURN:
  2246. * 0 = success
  2247. * !0 = failure
  2248. *
  2249. * PARAMETERS:
  2250. * *cd - pointer to core data
  2251. ******************************************************************************/
  2252. static int pt_pip_start_bootloader_(struct pt_core_data *cd)
  2253. {
  2254. int rc = 0;
  2255. struct pt_hid_output hid_output = {
  2256. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_START_BOOTLOADER),
  2257. .timeout_ms = PT_PIP1_START_BOOTLOADER_TIMEOUT,
  2258. .reset_expected = 1,
  2259. };
  2260. if (cd->watchdog_enabled) {
  2261. pt_debug(cd->dev, DL_WARN,
  2262. "%s: watchdog isn't stopped before enter bl\n",
  2263. __func__);
  2264. goto exit;
  2265. }
  2266. /* Reset startup status after entering BL, new DUT enum required */
  2267. cd->startup_status = STARTUP_STATUS_START;
  2268. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  2269. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2270. if (rc) {
  2271. pt_debug(cd->dev, DL_ERROR,
  2272. "%s: Start BL PIP cmd failed. rc = %d\n",
  2273. __func__, rc);
  2274. }
  2275. exit:
  2276. return rc;
  2277. }
  2278. /*******************************************************************************
  2279. * FUNCTION: pt_pip_start_bootloader
  2280. *
  2281. * SUMMARY: Protected function to force DUT to enter the BL
  2282. *
  2283. * RETURN:
  2284. * 0 = success
  2285. * !0 = failure
  2286. *
  2287. * PARAMETERS:
  2288. * *cd - pointer to core data structure
  2289. ******************************************************************************/
  2290. static int pt_pip_start_bootloader(struct pt_core_data *cd)
  2291. {
  2292. int rc;
  2293. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2294. if (rc < 0) {
  2295. pt_debug(cd->dev, DL_ERROR,
  2296. "%s: fail get exclusive ex=%p own=%p\n",
  2297. __func__, cd->exclusive_dev, cd->dev);
  2298. return rc;
  2299. }
  2300. rc = pt_pip_start_bootloader_(cd);
  2301. if (release_exclusive(cd, cd->dev) < 0)
  2302. pt_debug(cd->dev, DL_ERROR,
  2303. "%s: fail to release exclusive\n", __func__);
  2304. return rc;
  2305. }
  2306. /*******************************************************************************
  2307. * FUNCTION: _pt_request_pip_start_bl
  2308. *
  2309. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other
  2310. * modules to request the DUT to enter the BL
  2311. *
  2312. * RETURN:
  2313. * 0 = success
  2314. * !0 = failure
  2315. *
  2316. * PARAMETERS:
  2317. * *dev - pointer to device structure
  2318. * protect - flag to run in protected mode
  2319. ******************************************************************************/
  2320. static int _pt_request_pip_start_bl(struct device *dev, int protect)
  2321. {
  2322. struct pt_core_data *cd = dev_get_drvdata(dev);
  2323. if (protect)
  2324. return pt_pip_start_bootloader(cd);
  2325. return pt_pip_start_bootloader_(cd);
  2326. }
  2327. /*******************************************************************************
  2328. * FUNCTION: pt_pip2_ver_load_ttdata
  2329. *
  2330. * SUMMARY: Function to load the Version information from the PIP2 VERSION
  2331. * command into the core data struct.
  2332. *
  2333. * RETURN: n/a
  2334. *
  2335. * PARAMETERS:
  2336. * *cd - pointer to core data structure
  2337. * len - Length of data in response_buf
  2338. ******************************************************************************/
  2339. static void pt_pip2_ver_load_ttdata(struct pt_core_data *cd, u16 len)
  2340. {
  2341. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2342. struct pt_pip2_version_full *full_ver;
  2343. struct pt_pip2_version *ver;
  2344. /*
  2345. * The PIP2 VERSION command can return different lengths of data.
  2346. * The additional LOT fields are included when the packet
  2347. * size is >= 29 bytes. Older FW sends a reduced packet size.
  2348. * NOTE:
  2349. * - The FW would swap the BL and FW versions when reporting
  2350. * the small packet.
  2351. * - Sub Lot bytes 16 and 17 are reserved.
  2352. */
  2353. if (len >= 0x1D) {
  2354. full_ver = (struct pt_pip2_version_full *)
  2355. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2356. ttdata->pip_ver_major = full_ver->pip2_version_msb;
  2357. ttdata->pip_ver_minor = full_ver->pip2_version_lsb;
  2358. ttdata->bl_ver_major = full_ver->bl_version_msb;
  2359. ttdata->bl_ver_minor = full_ver->bl_version_lsb;
  2360. ttdata->fw_ver_major = full_ver->fw_version_msb;
  2361. ttdata->fw_ver_minor = full_ver->fw_version_lsb;
  2362. /*
  2363. * BL PIP 2.02 and greater the version fields are
  2364. * swapped
  2365. */
  2366. if (ttdata->pip_ver_major >= 2 && ttdata->pip_ver_minor >= 2) {
  2367. ttdata->chip_rev =
  2368. get_unaligned_le16(&full_ver->chip_rev);
  2369. ttdata->chip_id =
  2370. get_unaligned_le16(&full_ver->chip_id);
  2371. } else {
  2372. ttdata->chip_rev =
  2373. get_unaligned_le16(&full_ver->chip_id);
  2374. ttdata->chip_id =
  2375. get_unaligned_le16(&full_ver->chip_rev);
  2376. }
  2377. memcpy(ttdata->uid, full_ver->uid, PT_UID_SIZE);
  2378. pt_pr_buf(cd->dev, DL_INFO, (u8 *)full_ver,
  2379. sizeof(struct pt_pip2_version_full),
  2380. "PIP2 VERSION FULL");
  2381. } else {
  2382. ver = (struct pt_pip2_version *)
  2383. &cd->response_buf[PIP2_RESP_STATUS_OFFSET];
  2384. ttdata->pip_ver_major = ver->pip2_version_msb;
  2385. ttdata->pip_ver_minor = ver->pip2_version_lsb;
  2386. ttdata->bl_ver_major = ver->bl_version_msb;
  2387. ttdata->bl_ver_minor = ver->bl_version_lsb;
  2388. ttdata->fw_ver_major = ver->fw_version_msb;
  2389. ttdata->fw_ver_minor = ver->fw_version_lsb;
  2390. ttdata->chip_rev = get_unaligned_le16(&ver->chip_rev);
  2391. ttdata->chip_id = get_unaligned_le16(&ver->chip_id);
  2392. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ver,
  2393. sizeof(struct pt_pip2_version), "PIP2 VERSION");
  2394. }
  2395. }
  2396. /*******************************************************************************
  2397. * FUNCTION: pt_si_get_ttdata
  2398. *
  2399. * SUMMARY: Function to load the version information from the system information
  2400. * PIP command into the core data struct.
  2401. *
  2402. * RETURN: n/a
  2403. *
  2404. * PARAMETERS:
  2405. * *cd - pointer to core data structure
  2406. ******************************************************************************/
  2407. static void pt_si_get_ttdata(struct pt_core_data *cd)
  2408. {
  2409. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  2410. struct pt_ttdata_dev *ttdata_dev =
  2411. (struct pt_ttdata_dev *)
  2412. &cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  2413. ttdata->pip_ver_major = ttdata_dev->pip_ver_major;
  2414. ttdata->pip_ver_minor = ttdata_dev->pip_ver_minor;
  2415. ttdata->bl_ver_major = ttdata_dev->bl_ver_major;
  2416. ttdata->bl_ver_minor = ttdata_dev->bl_ver_minor;
  2417. ttdata->fw_ver_major = ttdata_dev->fw_ver_major;
  2418. ttdata->fw_ver_minor = ttdata_dev->fw_ver_minor;
  2419. ttdata->fw_pid = get_unaligned_le16(&ttdata_dev->fw_pid);
  2420. ttdata->fw_ver_conf = get_unaligned_le16(&ttdata_dev->fw_ver_conf);
  2421. ttdata->post_code = get_unaligned_le16(&ttdata_dev->post_code);
  2422. ttdata->revctrl = get_unaligned_le32(&ttdata_dev->revctrl);
  2423. ttdata->jtag_id_l = get_unaligned_le16(&ttdata_dev->jtag_si_id_l);
  2424. ttdata->jtag_id_h = get_unaligned_le16(&ttdata_dev->jtag_si_id_h);
  2425. memcpy(ttdata->mfg_id, ttdata_dev->mfg_id, PT_NUM_MFGID);
  2426. pt_pr_buf(cd->dev, DL_INFO, (u8 *)ttdata_dev,
  2427. sizeof(struct pt_ttdata_dev), "sysinfo_ttdata");
  2428. }
  2429. /*******************************************************************************
  2430. * FUNCTION: pt_si_get_sensing_conf_data
  2431. *
  2432. * SUMMARY: Function to load the sensing information from the system information
  2433. * PIP command into the core data struct.
  2434. *
  2435. * RETURN: n/a
  2436. *
  2437. * PARAMETERS:
  2438. * *cd - pointer to core data structure
  2439. ******************************************************************************/
  2440. static void pt_si_get_sensing_conf_data(struct pt_core_data *cd)
  2441. {
  2442. struct pt_sensing_conf_data *scd = &cd->sysinfo.sensing_conf_data;
  2443. struct pt_sensing_conf_data_dev *scd_dev =
  2444. (struct pt_sensing_conf_data_dev *)
  2445. &cd->response_buf[PIP1_SYSINFO_SENSING_OFFSET];
  2446. scd->electrodes_x = scd_dev->electrodes_x;
  2447. scd->electrodes_y = scd_dev->electrodes_y;
  2448. scd->origin_x = scd_dev->origin_x;
  2449. scd->origin_y = scd_dev->origin_y;
  2450. /* PIP 1.4 (001-82649 *Q) add X_IS_TX bit in X_ORG */
  2451. if (scd->origin_x & 0x02) {
  2452. scd->tx_num = scd->electrodes_x;
  2453. scd->rx_num = scd->electrodes_y;
  2454. } else {
  2455. scd->tx_num = scd->electrodes_y;
  2456. scd->rx_num = scd->electrodes_x;
  2457. }
  2458. /*
  2459. * When the Panel ID is coming from an XY pin and not a dedicated
  2460. * GPIO, store the PID in pid_for_loader. This cannot be done for all
  2461. * other DUTs as the loader will use cd->pid_for_loader to generate
  2462. * the bin file name but will ignore it if pid_for_loader is still
  2463. * set to PANEL_ID_NOT_ENABLED
  2464. */
  2465. if (cd->panel_id_support &
  2466. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  2467. mutex_lock(&cd->system_lock);
  2468. cd->pid_for_loader = scd_dev->panel_id;
  2469. mutex_unlock(&cd->system_lock);
  2470. }
  2471. scd->panel_id = scd_dev->panel_id;
  2472. scd->btn = scd_dev->btn;
  2473. scd->scan_mode = scd_dev->scan_mode;
  2474. scd->max_tch = scd_dev->max_num_of_tch_per_refresh_cycle;
  2475. scd->res_x = get_unaligned_le16(&scd_dev->res_x);
  2476. scd->res_y = get_unaligned_le16(&scd_dev->res_y);
  2477. scd->max_z = get_unaligned_le16(&scd_dev->max_z);
  2478. scd->len_x = get_unaligned_le16(&scd_dev->len_x);
  2479. scd->len_y = get_unaligned_le16(&scd_dev->len_y);
  2480. pt_pr_buf(cd->dev, DL_INFO, (u8 *)scd_dev,
  2481. sizeof(struct pt_sensing_conf_data_dev),
  2482. "sensing_conf_data");
  2483. }
  2484. /*******************************************************************************
  2485. * FUNCTION: pt_si_setup
  2486. *
  2487. * SUMMARY: Setup the xy_data and xy_mode by allocating the needed memory
  2488. *
  2489. * RETURN:
  2490. * 0 = success
  2491. * !0 = failure
  2492. *
  2493. * PARAMETERS:
  2494. * *cd - pointer to core data structure
  2495. ******************************************************************************/
  2496. static int pt_si_setup(struct pt_core_data *cd)
  2497. {
  2498. struct pt_sysinfo *si = &cd->sysinfo;
  2499. int max_tch = si->sensing_conf_data.max_tch;
  2500. if (!si->xy_data)
  2501. si->xy_data = kzalloc(max_tch * si->desc.tch_record_size,
  2502. GFP_KERNEL);
  2503. if (!si->xy_data)
  2504. return -ENOMEM;
  2505. if (!si->xy_mode)
  2506. si->xy_mode = kzalloc(si->desc.tch_header_size, GFP_KERNEL);
  2507. if (!si->xy_mode) {
  2508. kfree(si->xy_data);
  2509. return -ENOMEM;
  2510. }
  2511. return 0;
  2512. }
  2513. /*******************************************************************************
  2514. * FUNCTION: pt_si_get_btn_data
  2515. *
  2516. * SUMMARY: Setup the core data button information based on the response of the
  2517. * System Information PIP command.
  2518. *
  2519. * RETURN:
  2520. * 0 = success
  2521. * !0 = failure
  2522. *
  2523. * PARAMETERS:
  2524. * *cd - pointer to core data structure
  2525. ******************************************************************************/
  2526. static int pt_si_get_btn_data(struct pt_core_data *cd)
  2527. {
  2528. struct pt_sysinfo *si = &cd->sysinfo;
  2529. int num_btns = 0;
  2530. int num_defined_keys;
  2531. u16 *key_table;
  2532. int btn;
  2533. int i;
  2534. int rc = 0;
  2535. unsigned int btns = cd->response_buf[PIP1_SYSINFO_BTN_OFFSET]
  2536. & PIP1_SYSINFO_BTN_MASK;
  2537. size_t btn_keys_size;
  2538. pt_debug(cd->dev, DL_INFO, "%s: get btn data\n", __func__);
  2539. for (i = 0; i < PIP1_SYSINFO_MAX_BTN; i++) {
  2540. if (btns & (1 << i))
  2541. num_btns++;
  2542. }
  2543. si->num_btns = num_btns;
  2544. if (num_btns) {
  2545. btn_keys_size = num_btns * sizeof(struct pt_btn);
  2546. if (!si->btn)
  2547. si->btn = kzalloc(btn_keys_size, GFP_KERNEL);
  2548. if (!si->btn)
  2549. return -ENOMEM;
  2550. if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS] == NULL)
  2551. num_defined_keys = 0;
  2552. else if (cd->cpdata->sett[PT_IC_GRPNUM_BTN_KEYS]->data == NULL)
  2553. num_defined_keys = 0;
  2554. else
  2555. num_defined_keys = cd->cpdata->sett
  2556. [PT_IC_GRPNUM_BTN_KEYS]->size;
  2557. for (btn = 0; btn < num_btns && btn < num_defined_keys; btn++) {
  2558. key_table = (u16 *)cd->cpdata->sett
  2559. [PT_IC_GRPNUM_BTN_KEYS]->data;
  2560. si->btn[btn].key_code = key_table[btn];
  2561. si->btn[btn].enabled = true;
  2562. }
  2563. for (; btn < num_btns; btn++) {
  2564. si->btn[btn].key_code = KEY_RESERVED;
  2565. si->btn[btn].enabled = true;
  2566. }
  2567. return rc;
  2568. }
  2569. kfree(si->btn);
  2570. si->btn = NULL;
  2571. return rc;
  2572. }
  2573. /*******************************************************************************
  2574. * FUNCTION: pt_si_put_log_data
  2575. *
  2576. * SUMMARY: Prints all sys info data to kmsg log
  2577. *
  2578. * RETURN: n/a
  2579. *
  2580. * PARAMETERS:
  2581. * *cd - pointer to core data structure
  2582. ******************************************************************************/
  2583. static void pt_si_put_log_data(struct pt_core_data *cd)
  2584. {
  2585. struct pt_sysinfo *si = &cd->sysinfo;
  2586. struct pt_ttdata *ttdata = &si->ttdata;
  2587. struct pt_sensing_conf_data *scd = &si->sensing_conf_data;
  2588. int i;
  2589. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_major = 0x%02X (%d)\n",
  2590. __func__, ttdata->pip_ver_major, ttdata->pip_ver_major);
  2591. pt_debug(cd->dev, DL_DEBUG, "%s: pip_ver_minor = 0x%02X (%d)\n",
  2592. __func__, ttdata->pip_ver_minor, ttdata->pip_ver_minor);
  2593. pt_debug(cd->dev, DL_DEBUG, "%s: fw_pid = 0x%04X (%d)\n",
  2594. __func__, ttdata->fw_pid, ttdata->fw_pid);
  2595. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_major = 0x%02X (%d)\n",
  2596. __func__, ttdata->fw_ver_major, ttdata->fw_ver_major);
  2597. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_minor = 0x%02X (%d)\n",
  2598. __func__, ttdata->fw_ver_minor, ttdata->fw_ver_minor);
  2599. pt_debug(cd->dev, DL_DEBUG, "%s: revctrl = 0x%08X (%d)\n",
  2600. __func__, ttdata->revctrl, ttdata->revctrl);
  2601. pt_debug(cd->dev, DL_DEBUG, "%s: fw_ver_conf = 0x%04X (%d)\n",
  2602. __func__, ttdata->fw_ver_conf, ttdata->fw_ver_conf);
  2603. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_major = 0x%02X (%d)\n",
  2604. __func__, ttdata->bl_ver_major, ttdata->bl_ver_major);
  2605. pt_debug(cd->dev, DL_DEBUG, "%s: bl_ver_minor = 0x%02X (%d)\n",
  2606. __func__, ttdata->bl_ver_minor, ttdata->bl_ver_minor);
  2607. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_h = 0x%04X (%d)\n",
  2608. __func__, ttdata->jtag_id_h, ttdata->jtag_id_h);
  2609. pt_debug(cd->dev, DL_DEBUG, "%s: jtag_id_l = 0x%04X (%d)\n",
  2610. __func__, ttdata->jtag_id_l, ttdata->jtag_id_l);
  2611. for (i = 0; i < PT_NUM_MFGID; i++)
  2612. pt_debug(cd->dev, DL_DEBUG,
  2613. "%s: mfg_id[%d] = 0x%02X (%d)\n",
  2614. __func__, i, ttdata->mfg_id[i],
  2615. ttdata->mfg_id[i]);
  2616. pt_debug(cd->dev, DL_DEBUG, "%s: post_code = 0x%04X (%d)\n",
  2617. __func__, ttdata->post_code, ttdata->post_code);
  2618. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_x = 0x%02X (%d)\n",
  2619. __func__, scd->electrodes_x, scd->electrodes_x);
  2620. pt_debug(cd->dev, DL_DEBUG, "%s: electrodes_y = 0x%02X (%d)\n",
  2621. __func__, scd->electrodes_y, scd->electrodes_y);
  2622. pt_debug(cd->dev, DL_DEBUG, "%s: len_x = 0x%04X (%d)\n",
  2623. __func__, scd->len_x, scd->len_x);
  2624. pt_debug(cd->dev, DL_DEBUG, "%s: len_y = 0x%04X (%d)\n",
  2625. __func__, scd->len_y, scd->len_y);
  2626. pt_debug(cd->dev, DL_DEBUG, "%s: res_x = 0x%04X (%d)\n",
  2627. __func__, scd->res_x, scd->res_x);
  2628. pt_debug(cd->dev, DL_DEBUG, "%s: res_y = 0x%04X (%d)\n",
  2629. __func__, scd->res_y, scd->res_y);
  2630. pt_debug(cd->dev, DL_DEBUG, "%s: max_z = 0x%04X (%d)\n",
  2631. __func__, scd->max_z, scd->max_z);
  2632. pt_debug(cd->dev, DL_DEBUG, "%s: origin_x = 0x%02X (%d)\n",
  2633. __func__, scd->origin_x, scd->origin_x);
  2634. pt_debug(cd->dev, DL_DEBUG, "%s: origin_y = 0x%02X (%d)\n",
  2635. __func__, scd->origin_y, scd->origin_y);
  2636. pt_debug(cd->dev, DL_DEBUG, "%s: panel_id = 0x%02X (%d)\n",
  2637. __func__, scd->panel_id, scd->panel_id);
  2638. pt_debug(cd->dev, DL_DEBUG, "%s: btn =0x%02X (%d)\n",
  2639. __func__, scd->btn, scd->btn);
  2640. pt_debug(cd->dev, DL_DEBUG, "%s: scan_mode = 0x%02X (%d)\n",
  2641. __func__, scd->scan_mode, scd->scan_mode);
  2642. pt_debug(cd->dev, DL_DEBUG,
  2643. "%s: max_num_of_tch_per_refresh_cycle = 0x%02X (%d)\n",
  2644. __func__, scd->max_tch, scd->max_tch);
  2645. pt_debug(cd->dev, DL_DEBUG, "%s: xy_mode = %p\n",
  2646. __func__, si->xy_mode);
  2647. pt_debug(cd->dev, DL_DEBUG, "%s: xy_data = %p\n",
  2648. __func__, si->xy_data);
  2649. }
  2650. /*******************************************************************************
  2651. * FUNCTION: pt_get_sysinfo_regs
  2652. *
  2653. * SUMMARY: Setup all the core data System information based on the response
  2654. * of the System Information PIP command.
  2655. *
  2656. * RETURN:
  2657. * 0 = success
  2658. * !0 = failure
  2659. *
  2660. * PARAMETERS:
  2661. * *cd - pointer to core data structure
  2662. ******************************************************************************/
  2663. static int pt_get_sysinfo_regs(struct pt_core_data *cd)
  2664. {
  2665. struct pt_sysinfo *si = &cd->sysinfo;
  2666. int rc;
  2667. rc = pt_si_get_btn_data(cd);
  2668. if (rc < 0)
  2669. return rc;
  2670. pt_si_get_ttdata(cd);
  2671. pt_si_get_sensing_conf_data(cd);
  2672. pt_si_setup(cd);
  2673. pt_si_put_log_data(cd);
  2674. si->ready = true;
  2675. return rc;
  2676. }
  2677. /*******************************************************************************
  2678. * FUNCTION: pt_free_si_ptrs
  2679. *
  2680. * SUMMARY: Frees all memory associated with the System Information within
  2681. * core data
  2682. *
  2683. * RETURN: n/a
  2684. *
  2685. * PARAMETERS:
  2686. * *cd - pointer to core data structure
  2687. ******************************************************************************/
  2688. static void pt_free_si_ptrs(struct pt_core_data *cd)
  2689. {
  2690. struct pt_sysinfo *si = &cd->sysinfo;
  2691. kfree(si->btn);
  2692. kfree(si->xy_mode);
  2693. kfree(si->xy_data);
  2694. }
  2695. /*******************************************************************************
  2696. * FUNCTION: pt_hid_output_get_sysinfo_
  2697. *
  2698. * SUMMARY: Sends the PIP Get SYS INFO command to the DUT and waits for the
  2699. * response.
  2700. *
  2701. * RETURN::
  2702. * 0 = success
  2703. * !0 = failure
  2704. *
  2705. * PARAMETERS:
  2706. * *cd - pointer to core data structure
  2707. ******************************************************************************/
  2708. static int pt_hid_output_get_sysinfo_(struct pt_core_data *cd)
  2709. {
  2710. int rc = 0;
  2711. struct pt_hid_output hid_output = {
  2712. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  2713. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  2714. };
  2715. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2716. if (rc)
  2717. return rc;
  2718. /* Parse the sysinfo data */
  2719. rc = pt_get_sysinfo_regs(cd);
  2720. if (rc)
  2721. pt_free_si_ptrs(cd);
  2722. return rc;
  2723. }
  2724. /*******************************************************************************
  2725. * FUNCTION: pt_hid_output_get_sysinfo
  2726. *
  2727. * SUMMARY: Protected call to pt_hid_output_get_sysinfo_
  2728. *
  2729. * RETURN::
  2730. * 0 = success
  2731. * !0 = failure
  2732. *
  2733. * PARAMETERS:
  2734. * *cd - pointer to core data structure
  2735. ******************************************************************************/
  2736. static int pt_hid_output_get_sysinfo(struct pt_core_data *cd)
  2737. {
  2738. int rc;
  2739. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2740. if (rc < 0) {
  2741. pt_debug(cd->dev, DL_ERROR,
  2742. "%s: fail get exclusive ex=%p own=%p\n",
  2743. __func__, cd->exclusive_dev, cd->dev);
  2744. return rc;
  2745. }
  2746. rc = pt_hid_output_get_sysinfo_(cd);
  2747. if (release_exclusive(cd, cd->dev) < 0)
  2748. pt_debug(cd->dev, DL_ERROR,
  2749. "%s: fail to release exclusive\n", __func__);
  2750. return rc;
  2751. }
  2752. /*******************************************************************************
  2753. * FUNCTION: pt_pip_suspend_scanning_
  2754. *
  2755. * SUMMARY: Sends the PIP Suspend Scanning command to the DUT
  2756. *
  2757. * RETURN::
  2758. * 0 = success
  2759. * !0 = failure
  2760. *
  2761. * PARAMETERS:
  2762. * *cd - pointer to core data structure
  2763. ******************************************************************************/
  2764. static int pt_pip_suspend_scanning_(struct pt_core_data *cd)
  2765. {
  2766. int rc = 0;
  2767. struct pt_hid_output hid_output = {
  2768. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SUSPEND_SCANNING),
  2769. };
  2770. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2771. if (rc) {
  2772. pt_debug(cd->dev, DL_ERROR,
  2773. "%s: Suspend Scan PIP cmd failed. rc = %d\n",
  2774. __func__, rc);
  2775. }
  2776. return rc;
  2777. }
  2778. /*******************************************************************************
  2779. * FUNCTION: pt_pip_suspend_scanning
  2780. *
  2781. * SUMMARY: Protected wrapper for calling pt_hid_output_suspend_scanning_
  2782. *
  2783. * RETURN::
  2784. * 0 = success
  2785. * !0 = failure
  2786. *
  2787. * PARAMETERS:
  2788. * *cd - pointer to core data structure
  2789. ******************************************************************************/
  2790. static int pt_pip_suspend_scanning(struct pt_core_data *cd)
  2791. {
  2792. int rc;
  2793. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2794. if (rc < 0) {
  2795. pt_debug(cd->dev, DL_ERROR,
  2796. "%s: fail get exclusive ex=%p own=%p\n",
  2797. __func__, cd->exclusive_dev, cd->dev);
  2798. return rc;
  2799. }
  2800. rc = pt_pip_suspend_scanning_(cd);
  2801. if (release_exclusive(cd, cd->dev) < 0)
  2802. pt_debug(cd->dev, DL_ERROR,
  2803. "%s: fail to release exclusive\n", __func__);
  2804. return rc;
  2805. }
  2806. /*******************************************************************************
  2807. * FUNCTION: _pt_request_pip_suspend_scanning
  2808. *
  2809. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2810. * calls to the protected or unprotected call to pt_pip_suspend_scanning
  2811. *
  2812. * RETURN:
  2813. * 0 = success
  2814. * !0 = failure
  2815. *
  2816. * PARAMETERS:
  2817. * *dev - pointer to device structure
  2818. * protect - 0 = call non-protected function
  2819. * 1 = call protected function
  2820. ******************************************************************************/
  2821. static int _pt_request_pip_suspend_scanning(struct device *dev,
  2822. int protect)
  2823. {
  2824. struct pt_core_data *cd = dev_get_drvdata(dev);
  2825. if (protect)
  2826. return pt_pip_suspend_scanning(cd);
  2827. return pt_pip_suspend_scanning_(cd);
  2828. }
  2829. /*******************************************************************************
  2830. * FUNCTION: pt_pip_resume_scanning_
  2831. *
  2832. * SUMMARY: Sends the PIP Resume Scanning command to the DUT
  2833. *
  2834. * RETURN::
  2835. * 0 = success
  2836. * !0 = failure
  2837. *
  2838. * PARAMETERS:
  2839. * *cd - pointer to core data structure
  2840. ******************************************************************************/
  2841. static int pt_pip_resume_scanning_(struct pt_core_data *cd)
  2842. {
  2843. int rc = 0;
  2844. struct pt_hid_output hid_output = {
  2845. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RESUME_SCANNING),
  2846. };
  2847. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2848. if (rc) {
  2849. pt_debug(cd->dev, DL_ERROR,
  2850. "%s: Resume Scan PIP cmd failed. rc = %d\n",
  2851. __func__, rc);
  2852. }
  2853. return rc;
  2854. }
  2855. /*******************************************************************************
  2856. * FUNCTION: pt_pip_resume_scanning
  2857. *
  2858. * SUMMARY: Protected wrapper for calling pt_pip_resume_scanning_
  2859. *
  2860. * RETURN::
  2861. * 0 = success
  2862. * !0 = failure
  2863. *
  2864. * PARAMETERS:
  2865. * *cd - pointer to core data structure
  2866. ******************************************************************************/
  2867. static int pt_pip_resume_scanning(struct pt_core_data *cd)
  2868. {
  2869. int rc;
  2870. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2871. if (rc < 0) {
  2872. pt_debug(cd->dev, DL_ERROR,
  2873. "%s: fail get exclusive ex=%p own=%p\n",
  2874. __func__, cd->exclusive_dev, cd->dev);
  2875. return rc;
  2876. }
  2877. rc = pt_pip_resume_scanning_(cd);
  2878. if (release_exclusive(cd, cd->dev) < 0)
  2879. pt_debug(cd->dev, DL_ERROR,
  2880. "%s: fail to release exclusive\n", __func__);
  2881. return rc;
  2882. }
  2883. /*******************************************************************************
  2884. * FUNCTION: _pt_request_pip_resume_scanning
  2885. *
  2886. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2887. * calls to the protected or unprotected call to pt_pip_resume_scanning
  2888. *
  2889. * RETURN::
  2890. * 0 = success
  2891. * !0 = failure
  2892. *
  2893. * PARAMETERS:
  2894. * *dev - pointer to device structure
  2895. * protect - 0 = call non-protected function
  2896. * 1 = call protected function
  2897. ******************************************************************************/
  2898. static int _pt_request_pip_resume_scanning(struct device *dev,
  2899. int protect)
  2900. {
  2901. struct pt_core_data *cd = dev_get_drvdata(dev);
  2902. if (protect)
  2903. return pt_pip_resume_scanning(cd);
  2904. return pt_pip_resume_scanning_(cd);
  2905. }
  2906. /*******************************************************************************
  2907. * FUNCTION: pt_pip_get_param_
  2908. *
  2909. * SUMMARY: Sends a PIP command 0x05 Get Parameter to the DUT and returns
  2910. * the 32bit parameter value
  2911. *
  2912. * RETURN:
  2913. * 0 = success
  2914. * !0 = failure
  2915. *
  2916. * PARAMETERS:
  2917. * *cd - pointer to core data
  2918. * param_id - parameter ID to retrieve
  2919. * *value - value of DUT parameter
  2920. ******************************************************************************/
  2921. static int pt_pip_get_param_(struct pt_core_data *cd,
  2922. u8 param_id, u32 *value)
  2923. {
  2924. int write_length = 1;
  2925. u8 param[1] = { param_id };
  2926. u8 read_param_id;
  2927. int param_size;
  2928. u8 *ptr;
  2929. int rc = 0;
  2930. int i;
  2931. struct pt_hid_output hid_output = {
  2932. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_PARAM),
  2933. .write_length = write_length,
  2934. .write_buf = param,
  2935. };
  2936. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  2937. if (rc)
  2938. return rc;
  2939. read_param_id = cd->response_buf[5];
  2940. if (read_param_id != param_id)
  2941. return -EPROTO;
  2942. param_size = cd->response_buf[6];
  2943. ptr = &cd->response_buf[7];
  2944. *value = 0;
  2945. for (i = 0; i < param_size; i++)
  2946. *value += ptr[i] << (i * 8);
  2947. return 0;
  2948. }
  2949. /*******************************************************************************
  2950. * FUNCTION: pt_pip_get_param
  2951. *
  2952. * SUMMARY: Protected call to pt_hid_output_get_param_ by a request exclusive
  2953. *
  2954. * RETURN:
  2955. * 0 = success
  2956. * !0 = failure
  2957. *
  2958. * PARAMETERS:
  2959. * *cd - pointer to core data
  2960. * param_id - parameter ID to retrieve
  2961. * *value - value of DUT parameter
  2962. ******************************************************************************/
  2963. static int pt_pip_get_param(struct pt_core_data *cd,
  2964. u8 param_id, u32 *value)
  2965. {
  2966. int rc;
  2967. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  2968. if (rc < 0) {
  2969. pt_debug(cd->dev, DL_ERROR,
  2970. "%s: fail get exclusive ex=%p own=%p\n",
  2971. __func__, cd->exclusive_dev, cd->dev);
  2972. return rc;
  2973. }
  2974. rc = pt_pip_get_param_(cd, param_id, value);
  2975. if (release_exclusive(cd, cd->dev) < 0)
  2976. pt_debug(cd->dev, DL_ERROR,
  2977. "%s: fail to release exclusive\n", __func__);
  2978. return rc;
  2979. }
  2980. /*******************************************************************************
  2981. * FUNCTION: _pt_request_pip_get_param
  2982. *
  2983. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  2984. * calls to the protected or unprotected call to pt_pip_get_param
  2985. *
  2986. * RETURN:
  2987. * 0 = success
  2988. * !0 = failure
  2989. *
  2990. * PARAMETERS:
  2991. * *dev - pointer to device structure
  2992. * protect - flag to call protected or non protected function
  2993. * param_id - parameter ID to retrieve
  2994. * *value - value of DUT parameter
  2995. ******************************************************************************/
  2996. int _pt_request_pip_get_param(struct device *dev,
  2997. int protect, u8 param_id, u32 *value)
  2998. {
  2999. struct pt_core_data *cd = dev_get_drvdata(dev);
  3000. if (protect)
  3001. return pt_pip_get_param(cd, param_id, value);
  3002. return pt_pip_get_param_(cd, param_id, value);
  3003. }
  3004. /*******************************************************************************
  3005. * FUNCTION: pt_pip_set_param_
  3006. *
  3007. * SUMMARY: Sends a PIP command 0x06 Set Parameter to the DUT writing the
  3008. * passed in value to flash
  3009. *
  3010. * RETURN:
  3011. * 0 = success
  3012. * !0 = failure
  3013. *
  3014. * PARAMETERS:
  3015. * *cd - pointer to core data
  3016. * param_id - parameter ID to set
  3017. * value - value to write
  3018. * size - size to write
  3019. ******************************************************************************/
  3020. static int pt_pip_set_param_(struct pt_core_data *cd,
  3021. u8 param_id, u32 value, u8 size)
  3022. {
  3023. u8 write_buf[6];
  3024. u8 *ptr = &write_buf[2];
  3025. int rc = 0;
  3026. int i;
  3027. struct pt_hid_output hid_output = {
  3028. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_SET_PARAM),
  3029. .write_buf = write_buf,
  3030. };
  3031. write_buf[0] = param_id;
  3032. write_buf[1] = size;
  3033. for (i = 0; i < size; i++) {
  3034. ptr[i] = value & 0xFF;
  3035. value = value >> 8;
  3036. }
  3037. hid_output.write_length = 2 + size;
  3038. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3039. if (rc)
  3040. return rc;
  3041. if (param_id != cd->response_buf[5] || size != cd->response_buf[6])
  3042. return -EPROTO;
  3043. return 0;
  3044. }
  3045. /*******************************************************************************
  3046. * FUNCTION: pt_pip_set_param
  3047. *
  3048. * SUMMARY: Protected call to pt_hid_output_set_param_ by a request exclusive
  3049. *
  3050. * RETURN:
  3051. * 0 = success
  3052. * !0 = failure
  3053. *
  3054. * PARAMETERS:
  3055. * *cd - pointer to core data
  3056. * param_id - parameter ID to set
  3057. * value - value to write
  3058. * size - size to write
  3059. ******************************************************************************/
  3060. static int pt_pip_set_param(struct pt_core_data *cd,
  3061. u8 param_id, u32 value, u8 size)
  3062. {
  3063. int rc;
  3064. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3065. if (rc < 0) {
  3066. pt_debug(cd->dev, DL_ERROR,
  3067. "%s: fail get exclusive ex=%p own=%p\n",
  3068. __func__, cd->exclusive_dev, cd->dev);
  3069. return rc;
  3070. }
  3071. rc = pt_pip_set_param_(cd, param_id, value, size);
  3072. if (release_exclusive(cd, cd->dev) < 0)
  3073. pt_debug(cd->dev, DL_ERROR,
  3074. "%s: fail to release exclusive\n", __func__);
  3075. return rc;
  3076. }
  3077. /*******************************************************************************
  3078. * FUNCTION: _pt_request_pip_set_param
  3079. *
  3080. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3081. * calls to the protected or unprotected call to pt_pip_set_param
  3082. *
  3083. * RETURN:
  3084. * 0 = success
  3085. * !0 = failure
  3086. *
  3087. * PARAMETERS:
  3088. * *dev - pointer to device structure
  3089. * protect - flag to call protected or non-protected
  3090. * param_id - parameter ID to set
  3091. * value - value to write
  3092. * size - size to write
  3093. ******************************************************************************/
  3094. int _pt_request_pip_set_param(struct device *dev, int protect,
  3095. u8 param_id, u32 value, u8 size)
  3096. {
  3097. struct pt_core_data *cd = dev_get_drvdata(dev);
  3098. if (protect)
  3099. return pt_pip_set_param(cd, param_id, value, size);
  3100. return pt_pip_set_param_(cd, param_id, value, size);
  3101. }
  3102. /*******************************************************************************
  3103. * FUNCTION: _pt_pip_enter_easywake_state_
  3104. *
  3105. * SUMMARY: Sends a PIP command 0x09 Enter EasyWake State to the DUT
  3106. *
  3107. * RETURN:
  3108. * 0 = success
  3109. * !0 = failure
  3110. *
  3111. * PARAMETERS:
  3112. * *dev - pointer to device structure
  3113. * data - easywake guesture (Only used for PIP1.6 and earlier)
  3114. * *return_data - return status if easywake was entered
  3115. ******************************************************************************/
  3116. static int pt_hid_output_enter_easywake_state_(
  3117. struct pt_core_data *cd, u8 data, u8 *return_data)
  3118. {
  3119. int write_length = 1;
  3120. u8 param[1] = { data };
  3121. int rc = 0;
  3122. struct pt_hid_output hid_output = {
  3123. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_ENTER_EASYWAKE_STATE),
  3124. .write_length = write_length,
  3125. .write_buf = param,
  3126. };
  3127. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3128. if (rc)
  3129. return rc;
  3130. *return_data = cd->response_buf[5];
  3131. return rc;
  3132. }
  3133. /*******************************************************************************
  3134. * FUNCTION: pt_pip_verify_config_block_crc_
  3135. *
  3136. * SUMMARY: Sends the PIP "Verify Data Block CRC" (0x20) command to the DUT
  3137. *
  3138. * RETURN:
  3139. * 0 = success
  3140. * !0 = failure
  3141. *
  3142. * PARAMETERS:
  3143. * *cd - pointer the core data structure
  3144. * ebid - enumerated block ID
  3145. * *status - PIP command status
  3146. * calculated_crc - calculated CRC
  3147. * stored_crc - stored CRC in config area
  3148. ******************************************************************************/
  3149. static int pt_pip_verify_config_block_crc_(
  3150. struct pt_core_data *cd, u8 ebid, u8 *status,
  3151. u16 *calculated_crc, u16 *stored_crc)
  3152. {
  3153. int write_length = 1;
  3154. u8 param[1] = { ebid };
  3155. u8 *ptr;
  3156. int rc = 0;
  3157. struct pt_hid_output hid_output = {
  3158. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  3159. .write_length = write_length,
  3160. .write_buf = param,
  3161. };
  3162. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3163. if (rc)
  3164. return rc;
  3165. ptr = &cd->response_buf[5];
  3166. *status = ptr[0];
  3167. *calculated_crc = get_unaligned_le16(&ptr[1]);
  3168. *stored_crc = get_unaligned_le16(&ptr[3]);
  3169. return 0;
  3170. }
  3171. /*******************************************************************************
  3172. * FUNCTION: pt_pip_verify_config_block_crc
  3173. *
  3174. * SUMMARY: Protected call to pt_hid_output_verify_config_block_crc_() within
  3175. * an exclusive access to the DUT.
  3176. *
  3177. * RETURN:
  3178. * 0 = success
  3179. * !0 = failure
  3180. *
  3181. * PARAMETERS:
  3182. * *cd - pointer the core data structure
  3183. * ebid - enumerated block ID
  3184. * *status - PIP command status
  3185. * calculated_crc - calculated CRC
  3186. * stored_crc - stored CRC in config area
  3187. ******************************************************************************/
  3188. static int pt_pip_verify_config_block_crc(
  3189. struct pt_core_data *cd, u8 ebid, u8 *status,
  3190. u16 *calculated_crc, u16 *stored_crc)
  3191. {
  3192. int rc;
  3193. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3194. if (rc < 0) {
  3195. pt_debug(cd->dev, DL_ERROR,
  3196. "%s: fail get exclusive ex=%p own=%p\n",
  3197. __func__, cd->exclusive_dev, cd->dev);
  3198. return rc;
  3199. }
  3200. rc = pt_pip_verify_config_block_crc_(cd, ebid, status,
  3201. calculated_crc, stored_crc);
  3202. if (release_exclusive(cd, cd->dev) < 0)
  3203. pt_debug(cd->dev, DL_ERROR,
  3204. "%s: fail to release exclusive\n", __func__);
  3205. return rc;
  3206. }
  3207. /*******************************************************************************
  3208. * FUNCTION: _pt_request_pip_verify_config_block_crc
  3209. *
  3210. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3211. * calls to the protected or unprotected call to
  3212. * pt_pip_verify_config_block_crc_
  3213. *
  3214. * RETURN:
  3215. * 0 = success
  3216. * !0 = failure
  3217. *
  3218. * PARAMETERS:
  3219. * *dev - pointer to device structure
  3220. * protect - flag to call protected or non-protected
  3221. * ebid - enumerated block ID
  3222. * *status - PIP command status
  3223. * calculated_crc - calculated CRC
  3224. * stored_crc - stored CRC in config area
  3225. ******************************************************************************/
  3226. static int _pt_request_pip_verify_config_block_crc(
  3227. struct device *dev, int protect, u8 ebid, u8 *status,
  3228. u16 *calculated_crc, u16 *stored_crc)
  3229. {
  3230. struct pt_core_data *cd = dev_get_drvdata(dev);
  3231. if (protect)
  3232. return pt_pip_verify_config_block_crc(cd, ebid,
  3233. status, calculated_crc, stored_crc);
  3234. return pt_pip_verify_config_block_crc_(cd, ebid,
  3235. status, calculated_crc, stored_crc);
  3236. }
  3237. /*******************************************************************************
  3238. * FUNCTION: pt_pip_get_config_row_size_
  3239. *
  3240. * SUMMARY: Sends the PIP "Get Data Row Size" (0x21) command to the DUT
  3241. *
  3242. * RETURN:
  3243. * 0 = success
  3244. * !0 = failure
  3245. *
  3246. * PARAMETERS:
  3247. * *cd - pointer to core data
  3248. * protect - flag to call protected or non-protected
  3249. * *row_size - pointer to store the retrieved row size
  3250. ******************************************************************************/
  3251. static int pt_pip_get_config_row_size_(struct pt_core_data *cd,
  3252. u16 *row_size)
  3253. {
  3254. int rc = 0;
  3255. struct pt_hid_output hid_output = {
  3256. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_CONFIG_ROW_SIZE),
  3257. };
  3258. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3259. if (rc)
  3260. return rc;
  3261. *row_size = get_unaligned_le16(&cd->response_buf[5]);
  3262. return 0;
  3263. }
  3264. /*******************************************************************************
  3265. * FUNCTION: pt_pip_get_config_row_size
  3266. *
  3267. * SUMMARY: Protected call to pt_hid_output_get_config_row_size_ within
  3268. * an exclusive access to the DUT.
  3269. *
  3270. * RETURN:
  3271. * 0 = success
  3272. * !0 = failure
  3273. *
  3274. * PARAMETERS:
  3275. * *cd - pointer to core data
  3276. * protect - flag to call protected or non-protected
  3277. * *row_size - pointer to store the retrieved row size
  3278. ******************************************************************************/
  3279. static int pt_pip_get_config_row_size(struct pt_core_data *cd,
  3280. u16 *row_size)
  3281. {
  3282. int rc;
  3283. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3284. if (rc < 0) {
  3285. pt_debug(cd->dev, DL_ERROR,
  3286. "%s: fail get exclusive ex=%p own=%p\n",
  3287. __func__, cd->exclusive_dev, cd->dev);
  3288. return rc;
  3289. }
  3290. rc = pt_pip_get_config_row_size_(cd, row_size);
  3291. if (release_exclusive(cd, cd->dev) < 0)
  3292. pt_debug(cd->dev, DL_ERROR,
  3293. "%s: fail to release exclusive\n", __func__);
  3294. return rc;
  3295. }
  3296. /*******************************************************************************
  3297. * FUNCTION: _pt_request_pip_get_config_row_size
  3298. *
  3299. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3300. * calls to the protected or unprotected call to
  3301. * pt_pip_get_config_row_size_
  3302. *
  3303. * RETURN:
  3304. * 0 = success
  3305. * !0 = failure
  3306. *
  3307. * PARAMETERS:
  3308. * *dev - pointer to device structure
  3309. * protect - flag to call protected or non-protected
  3310. * *row_size - pointer to store the retrieved row size
  3311. ******************************************************************************/
  3312. static int _pt_request_pip_get_config_row_size(struct device *dev,
  3313. int protect, u16 *row_size)
  3314. {
  3315. struct pt_core_data *cd = dev_get_drvdata(dev);
  3316. if (protect)
  3317. return pt_pip_get_config_row_size(cd, row_size);
  3318. return pt_pip_get_config_row_size_(cd, row_size);
  3319. }
  3320. /*******************************************************************************
  3321. * FUNCTION: pt_pip1_read_data_block_
  3322. *
  3323. * SUMMARY: Sends the PIP "Read Data Block" (0x22) command to the DUT and print
  3324. * output data to the "read_buf" and update "crc".
  3325. *
  3326. * RETURN:
  3327. * 0 = success
  3328. * !0 = failure
  3329. *
  3330. * PARAMETERS:
  3331. * *cd - pointer to core data
  3332. * row_number - row number
  3333. * length - length of data to read
  3334. * ebid - block id
  3335. * *actual_read_len - Actual data length read
  3336. * *read_buf - pointer to the buffer to store read data
  3337. * read_buf_size - size of read_buf
  3338. * *crc - pointer to store CRC of row data
  3339. ******************************************************************************/
  3340. static int pt_pip1_read_data_block_(struct pt_core_data *cd,
  3341. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3342. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3343. {
  3344. int read_ebid;
  3345. int status;
  3346. int rc = 0;
  3347. int write_length = 5;
  3348. u8 write_buf[5];
  3349. u8 cmd_offset = 0;
  3350. u16 calc_crc;
  3351. struct pt_hid_output hid_output = {
  3352. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_READ_DATA_BLOCK),
  3353. .write_length = write_length,
  3354. .write_buf = write_buf,
  3355. };
  3356. write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3357. write_buf[cmd_offset++] = HI_BYTE(row_number);
  3358. write_buf[cmd_offset++] = LOW_BYTE(length);
  3359. write_buf[cmd_offset++] = HI_BYTE(length);
  3360. write_buf[cmd_offset++] = ebid;
  3361. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3362. if (rc)
  3363. return rc;
  3364. status = cd->response_buf[5];
  3365. if (status)
  3366. return status;
  3367. read_ebid = cd->response_buf[6];
  3368. if ((read_ebid != ebid) || (cd->response_buf[9] != 0))
  3369. return -EPROTO;
  3370. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  3371. if (length == 0 || *actual_read_len == 0)
  3372. return 0;
  3373. if (read_buf_size >= *actual_read_len)
  3374. memcpy(read_buf, &cd->response_buf[10], *actual_read_len);
  3375. else
  3376. return -EPROTO;
  3377. *crc = get_unaligned_le16(&cd->response_buf[*actual_read_len + 10]);
  3378. /* Validate Row Data CRC */
  3379. calc_crc = _pt_compute_crc(read_buf, *actual_read_len);
  3380. if (*crc == calc_crc) {
  3381. return 0;
  3382. } else {
  3383. pt_debug(cd->dev, DL_ERROR,
  3384. "%s: CRC Mismatch packet=0x%04X calc=0x%04X\n",
  3385. __func__, *crc, calc_crc);
  3386. return -EPROTO;
  3387. }
  3388. }
  3389. /*******************************************************************************
  3390. * FUNCTION: _pt_request_pip_read_data_block
  3391. *
  3392. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3393. * calls to pt_pip1_read_data_block_
  3394. *
  3395. * RETURN:
  3396. * 0 = success
  3397. * !0 = failure
  3398. *
  3399. * PARAMETERS:
  3400. * *dev - pointer to device structure
  3401. * row_number - row number
  3402. * length - length of data to read
  3403. * ebid - block id
  3404. * *actual_read_len - Actual data length read
  3405. * *read_buf - pointer to the buffer to store read data
  3406. * *crc - pointer to store CRC of row data
  3407. ******************************************************************************/
  3408. static int _pt_request_pip_read_data_block(struct device *dev,
  3409. u16 row_number, u16 length, u8 ebid, u16 *actual_read_len,
  3410. u8 *read_buf, u16 read_buf_size, u16 *crc)
  3411. {
  3412. struct pt_core_data *cd = dev_get_drvdata(dev);
  3413. return pt_pip1_read_data_block_(cd, row_number, length,
  3414. ebid, actual_read_len, read_buf, read_buf_size, crc);
  3415. }
  3416. /*******************************************************************************
  3417. * FUNCTION: pt_pip1_write_data_block_
  3418. *
  3419. * SUMMARY: Sends the PIP "Write Data Block" (0x23) command to the DUT and
  3420. * write data to the data block.
  3421. *
  3422. * RETURN:
  3423. * 0 = success
  3424. * !0 = failure
  3425. *
  3426. * PARAMETERS:
  3427. * *cd - pointer to core data
  3428. * row_number - row in config block to write to
  3429. * write_length - length of data to write
  3430. * ebid - enumerated block ID
  3431. * *write_buf - pointer to buffer to write
  3432. * *security_key - pointer to security key to allow write
  3433. * *actual_write_len - pointer to store data length actually written
  3434. ******************************************************************************/
  3435. static int pt_pip1_write_data_block_(struct pt_core_data *cd,
  3436. u16 row_number, u16 write_length, u8 ebid, u8 *write_buf,
  3437. u8 *security_key, u16 *actual_write_len)
  3438. {
  3439. /* row_number + write_len + ebid + security_key + crc */
  3440. int full_write_length = 2 + 2 + 1 + write_length + 8 + 2;
  3441. u8 *full_write_buf;
  3442. u8 cmd_offset = 0;
  3443. u16 crc;
  3444. int status;
  3445. int rc = 0;
  3446. int read_ebid;
  3447. u8 *data;
  3448. struct pt_hid_output hid_output = {
  3449. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_WRITE_DATA_BLOCK),
  3450. .write_length = full_write_length,
  3451. .timeout_ms = PT_PIP1_CMD_WRITE_CONF_BLOCK_TIMEOUT,
  3452. };
  3453. full_write_buf = kzalloc(full_write_length, GFP_KERNEL);
  3454. if (!full_write_buf)
  3455. return -ENOMEM;
  3456. hid_output.write_buf = full_write_buf;
  3457. full_write_buf[cmd_offset++] = LOW_BYTE(row_number);
  3458. full_write_buf[cmd_offset++] = HI_BYTE(row_number);
  3459. full_write_buf[cmd_offset++] = LOW_BYTE(write_length);
  3460. full_write_buf[cmd_offset++] = HI_BYTE(write_length);
  3461. full_write_buf[cmd_offset++] = ebid;
  3462. data = &full_write_buf[cmd_offset];
  3463. memcpy(data, write_buf, write_length);
  3464. cmd_offset += write_length;
  3465. memcpy(&full_write_buf[cmd_offset], security_key, 8);
  3466. cmd_offset += 8;
  3467. crc = _pt_compute_crc(data, write_length);
  3468. full_write_buf[cmd_offset++] = LOW_BYTE(crc);
  3469. full_write_buf[cmd_offset++] = HI_BYTE(crc);
  3470. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3471. if (rc)
  3472. goto exit;
  3473. status = cd->response_buf[5];
  3474. if (status) {
  3475. rc = -EINVAL;
  3476. goto exit;
  3477. }
  3478. read_ebid = cd->response_buf[6];
  3479. if (read_ebid != ebid) {
  3480. rc = -EPROTO;
  3481. goto exit;
  3482. }
  3483. *actual_write_len = get_unaligned_le16(&cd->response_buf[7]);
  3484. exit:
  3485. kfree(full_write_buf);
  3486. return rc;
  3487. }
  3488. /*******************************************************************************
  3489. * FUNCTION: _pt_request_pip_write_data_block
  3490. *
  3491. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3492. * calls to the protected or unprotected call to pt_pip1_write_data_block_
  3493. *
  3494. * RETURN:
  3495. * 0 = success
  3496. * !0 = failure
  3497. *
  3498. * PARAMETERS:
  3499. * *dev - pointer to device structure
  3500. * protect - flag to call protected or non-protected
  3501. * row_number - row in config block to write to
  3502. * write_length - length of data to write
  3503. * ebid - enumerated block ID
  3504. * *write_buf - pointer to buffer to write
  3505. * *security_key - pointer to security key to allow write
  3506. * *actual_write_len - pointer to store data length actually written
  3507. ******************************************************************************/
  3508. static int _pt_request_pip_write_data_block(struct device *dev,
  3509. u16 row_number, u16 write_length, u8 ebid,
  3510. u8 *write_buf, u8 *security_key, u16 *actual_write_len)
  3511. {
  3512. struct pt_core_data *cd = dev_get_drvdata(dev);
  3513. return pt_pip1_write_data_block_(cd, row_number,
  3514. write_length, ebid, write_buf, security_key,
  3515. actual_write_len);
  3516. }
  3517. /*******************************************************************************
  3518. * FUNCTION: pt_pip_get_data_structure_
  3519. *
  3520. * SUMMARY: Sends the PIP "Retrieve Data Structure" (0x24) command to the DUT
  3521. * returning a structure of data defined by data_id
  3522. *
  3523. * RETURN:
  3524. * 0 = success
  3525. * !0 = failure
  3526. *
  3527. * PARAMETERS:
  3528. * *cd - pointer to core data
  3529. * read_offset - read pointer offset
  3530. * read_length - length of data to read
  3531. * data_id - data ID to read
  3532. * *status - pointer to store the read response status
  3533. * *data_format - pointer to store format of data read
  3534. * *actual_read_len - pointer to store data length actually read
  3535. * *data - pointer to store data read
  3536. ******************************************************************************/
  3537. static int pt_pip_get_data_structure_(
  3538. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3539. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3540. u8 *data)
  3541. {
  3542. int rc = 0;
  3543. u16 total_read_len = 0;
  3544. u16 read_len;
  3545. u16 off_buf = 0;
  3546. u8 write_buf[5];
  3547. u8 read_data_id;
  3548. struct pt_hid_output hid_output = {
  3549. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_DATA_STRUCTURE),
  3550. .write_length = 5,
  3551. .write_buf = write_buf,
  3552. };
  3553. again:
  3554. write_buf[0] = LOW_BYTE(read_offset);
  3555. write_buf[1] = HI_BYTE(read_offset);
  3556. write_buf[2] = LOW_BYTE(read_length);
  3557. write_buf[3] = HI_BYTE(read_length);
  3558. write_buf[4] = data_id;
  3559. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3560. if (rc)
  3561. return rc;
  3562. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  3563. goto set_status;
  3564. read_data_id = cd->response_buf[6];
  3565. if (read_data_id != data_id)
  3566. return -EPROTO;
  3567. read_len = get_unaligned_le16(&cd->response_buf[7]);
  3568. if (read_len && data) {
  3569. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  3570. total_read_len += read_len;
  3571. if (read_len < read_length) {
  3572. read_offset += read_len;
  3573. off_buf += read_len;
  3574. read_length -= read_len;
  3575. goto again;
  3576. }
  3577. }
  3578. if (data_format)
  3579. *data_format = cd->response_buf[9];
  3580. if (actual_read_len)
  3581. *actual_read_len = total_read_len;
  3582. set_status:
  3583. if (status)
  3584. *status = cd->response_buf[5];
  3585. return rc;
  3586. }
  3587. /*******************************************************************************
  3588. * FUNCTION: pt_pip_get_data_structure
  3589. *
  3590. * SUMMARY: Protected call to pt_hid_output_get_data_structure within
  3591. * an exclusive access to the DUT.
  3592. *
  3593. * RETURN:
  3594. * 0 = success
  3595. * !0 = failure
  3596. *
  3597. * PARAMETERS:
  3598. * *cd - pointer to core data
  3599. * read_offset - read pointer offset
  3600. * read_length - length of data to read
  3601. * data_id - data ID to read
  3602. * *status - pointer to store the read response status
  3603. * *data_format - pointer to store format of data read
  3604. * *actual_read_len - pointer to store data length actually read
  3605. * *data - pointer to store data read
  3606. ******************************************************************************/
  3607. static int pt_pip_get_data_structure(
  3608. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3609. u8 data_id, u8 *status, u8 *data_format, u16 *actual_read_len,
  3610. u8 *data)
  3611. {
  3612. int rc;
  3613. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3614. if (rc < 0) {
  3615. pt_debug(cd->dev, DL_ERROR,
  3616. "%s: fail get exclusive ex=%p own=%p\n",
  3617. __func__, cd->exclusive_dev, cd->dev);
  3618. return rc;
  3619. }
  3620. rc = pt_pip_get_data_structure_(cd, read_offset,
  3621. read_length, data_id, status, data_format,
  3622. actual_read_len, data);
  3623. if (release_exclusive(cd, cd->dev) < 0)
  3624. pt_debug(cd->dev, DL_ERROR,
  3625. "%s: fail to release exclusive\n", __func__);
  3626. return rc;
  3627. }
  3628. /*******************************************************************************
  3629. * FUNCTION: _pt_request_pip_get_data_structure
  3630. *
  3631. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3632. * calls to the protected or unprotected call to
  3633. * pt_pip_get_data_structure
  3634. *
  3635. * RETURN:
  3636. * 0 = success
  3637. * !0 = failure
  3638. *
  3639. * PARAMETERS:
  3640. * *dev - pointer to device structure
  3641. * protect - flag to call protected or non-protected
  3642. * read_offset - read pointer offset
  3643. * read_length - length of data to read
  3644. * data_id - data ID to read
  3645. * *status - pointer to store the read response status
  3646. * *data_format - pointer to store format of data read
  3647. * *actual_read_len - pointer to store data length actually read
  3648. * *data - pointer to store data read
  3649. ******************************************************************************/
  3650. static int _pt_request_pip_get_data_structure(struct device *dev,
  3651. int protect, u16 read_offset, u16 read_length, u8 data_id,
  3652. u8 *status, u8 *data_format, u16 *actual_read_len, u8 *data)
  3653. {
  3654. struct pt_core_data *cd = dev_get_drvdata(dev);
  3655. if (protect)
  3656. return pt_pip_get_data_structure(cd,
  3657. read_offset, read_length, data_id, status,
  3658. data_format, actual_read_len, data);
  3659. return pt_pip_get_data_structure_(cd,
  3660. read_offset, read_length, data_id, status,
  3661. data_format, actual_read_len, data);
  3662. }
  3663. /*******************************************************************************
  3664. * FUNCTION: _pt_manage_local_cal_data
  3665. *
  3666. * SUMMARY: This function manages storing or restoring a copy of the Firmware
  3667. * CALIBRATION data. It stores it in a local static array and can be
  3668. * cleared, loaded or used to restore the CAL data back to the running FW.
  3669. * The CAL data is read or restored by use of the PIP1 commands:
  3670. * - READ_DATA_BLOCK (0x22)
  3671. * - WRITE_DATA_BLOCK (0x23)
  3672. *
  3673. * RETURN:
  3674. * 0 = success
  3675. * !0 = failure
  3676. *
  3677. * PARAMETERS:
  3678. * *dev - pointer to device structure
  3679. * action - One of the following actions:
  3680. * - PT_CAL_DATA_SAVE
  3681. * - PT_CAL_DATA_RESTORE
  3682. * - PT_CAL_DATA_CLEAR
  3683. * - PT_CAL_DATA_SIZE
  3684. * *size - pointer to the number of bytes transferred
  3685. * *crc - pointer to Chip ID CRC that the CAL data was retrieved from
  3686. ******************************************************************************/
  3687. static int _pt_manage_local_cal_data(struct device *dev, u8 action, u16 *size,
  3688. unsigned short *crc)
  3689. {
  3690. struct pt_core_data *cd = dev_get_drvdata(dev);
  3691. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  3692. unsigned short calc_id_crc = 0;
  3693. static u8 *cal_cache_data;
  3694. static u16 cal_cache_len;
  3695. static unsigned short cal_cache_chip_id;
  3696. int rc = 0;
  3697. u8 *tmp_data = NULL;
  3698. u8 row_number = 0;
  3699. u8 prefix[20];
  3700. u16 cal_size = 0;
  3701. u16 transfer_size;
  3702. u16 act_trans_len = 0;
  3703. u16 byte_offset = 0;
  3704. u16 cal_blk_size;
  3705. u16 total_rows;
  3706. u16 remain_bytes;
  3707. u16 data_block_crc;
  3708. u16 buf_size = 12;
  3709. pt_debug(dev, DL_INFO, "%s: ATM - CAL Cache action=%d\n",
  3710. __func__, action);
  3711. switch (action) {
  3712. case PT_CAL_DATA_SAVE:
  3713. /* Read the size of the CAL block and calculate # rows */
  3714. tmp_data = kzalloc(buf_size, GFP_KERNEL);
  3715. if (!tmp_data) {
  3716. rc = -ENOMEM;
  3717. goto exit;
  3718. }
  3719. /*
  3720. * Don't check rc as doing a read size will give a false
  3721. * error on the CRC check.
  3722. */
  3723. rc = pt_pip1_read_data_block_(cd, row_number, 0, PT_CAL_EBID,
  3724. &act_trans_len, tmp_data, buf_size, &data_block_crc);
  3725. cal_blk_size = act_trans_len;
  3726. kfree(tmp_data);
  3727. pt_debug(dev, DL_INFO,
  3728. "%s: CAL Cache size=%d FW CAL Size=%d\n",
  3729. __func__, cal_cache_len, cal_blk_size);
  3730. /* Safety net to ensure we didn't read incorrect size */
  3731. if (cal_blk_size > PT_CAL_DATA_MAX_SIZE) {
  3732. pt_debug(dev, DL_ERROR, "%s: Alloc struct Failed\n",
  3733. __func__);
  3734. rc = 1;
  3735. goto exit;
  3736. }
  3737. /* Panels could have diff CAL sizes, Re-allocate the cache */
  3738. if (cal_blk_size != cal_cache_len) {
  3739. kfree(cal_cache_data);
  3740. cal_cache_data = kzalloc(cal_blk_size + 2,
  3741. GFP_KERNEL);
  3742. if (!cal_cache_data) {
  3743. rc = -ENOMEM;
  3744. goto exit;
  3745. }
  3746. pt_debug(dev, DL_INFO, "%s: CAL Cache Allocated\n",
  3747. __func__);
  3748. }
  3749. memset(&cal_cache_data[0], 0, cal_blk_size + 2);
  3750. /* Calculate how many rows [0-n] (PIP Transactions) */
  3751. total_rows = (cal_blk_size / PT_CAL_DATA_ROW_SIZE) - 1;
  3752. remain_bytes = cal_blk_size % PT_CAL_DATA_ROW_SIZE;
  3753. /* Add row if we have a last partial row */
  3754. if (remain_bytes > 0)
  3755. total_rows++;
  3756. pt_debug(dev, DL_INFO,
  3757. "%s: CAL size=%d rows=[0-%d] partial row bytes=%d\n",
  3758. __func__, cal_blk_size, total_rows, remain_bytes);
  3759. /* Read all rows unless an error occurs */
  3760. rc = 0;
  3761. while (rc == 0 && row_number <= total_rows) {
  3762. act_trans_len = 0;
  3763. if (remain_bytes > 0 && row_number == total_rows)
  3764. transfer_size = remain_bytes;
  3765. else
  3766. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3767. rc = pt_pip1_read_data_block_(cd, row_number,
  3768. transfer_size, PT_CAL_EBID,
  3769. &act_trans_len,
  3770. &cal_cache_data[byte_offset], cal_blk_size + 2,
  3771. &data_block_crc);
  3772. if (rc) {
  3773. /* Error occurred, exit loop */
  3774. cal_size = 0;
  3775. break;
  3776. }
  3777. pt_debug(dev, DL_INFO,
  3778. "%s: CAL read rc=%d actual read len=%d\n",
  3779. __func__, rc, act_trans_len);
  3780. byte_offset += act_trans_len;
  3781. cal_size = byte_offset;
  3782. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3783. pt_pr_buf(dev, DL_INFO,
  3784. &cal_cache_data[byte_offset - act_trans_len],
  3785. act_trans_len, prefix);
  3786. row_number++;
  3787. }
  3788. if (cal_size > 0) {
  3789. /* Save a CRC of the chip info the CAL was saved from */
  3790. calc_id_crc = crc_ccitt_calculate(
  3791. (u8 *)&ttdata->chip_rev, 4 + PT_UID_SIZE);
  3792. cal_cache_chip_id = calc_id_crc;
  3793. cal_cache_len = cal_size;
  3794. pt_debug(dev, DL_INFO,
  3795. "%s: CAL Cache: CRC=0x%04X Total Size=%d\n",
  3796. __func__, calc_id_crc, cal_size);
  3797. }
  3798. *size = cal_size;
  3799. *crc = calc_id_crc;
  3800. break;
  3801. case PT_CAL_DATA_RESTORE:
  3802. cal_size = cal_cache_len;
  3803. while ((rc == 0) && (byte_offset < cal_size)) {
  3804. if (cal_size - byte_offset > PT_CAL_DATA_ROW_SIZE)
  3805. transfer_size = PT_CAL_DATA_ROW_SIZE;
  3806. else
  3807. transfer_size = cal_size - byte_offset;
  3808. rc = pt_pip1_write_data_block_(cd, row_number,
  3809. transfer_size, PT_CAL_EBID,
  3810. &cal_cache_data[byte_offset],
  3811. (u8 *)pt_data_block_security_key,
  3812. &act_trans_len);
  3813. byte_offset += act_trans_len;
  3814. pt_debug(dev, DL_INFO, "%s: CAL write byte offset=%d\n",
  3815. __func__, byte_offset);
  3816. scnprintf(prefix, sizeof(prefix), "%s[%d]", "CAL DATA ROW", row_number);
  3817. pt_pr_buf(dev, DL_INFO,
  3818. &cal_cache_data[byte_offset - act_trans_len],
  3819. act_trans_len, prefix);
  3820. if ((byte_offset > cal_size) ||
  3821. (act_trans_len != transfer_size))
  3822. rc = -EIO;
  3823. row_number++;
  3824. }
  3825. *size = byte_offset;
  3826. *crc = cal_cache_chip_id;
  3827. break;
  3828. case PT_CAL_DATA_CLEAR:
  3829. if (cal_cache_data)
  3830. memset(&cal_cache_data[0], 0, cal_cache_len);
  3831. cal_cache_len = 0;
  3832. cal_cache_chip_id = 0;
  3833. *size = 0;
  3834. *crc = 0;
  3835. break;
  3836. case PT_CAL_DATA_INFO:
  3837. default:
  3838. *size = cal_cache_len;
  3839. *crc = cal_cache_chip_id;
  3840. pt_debug(dev, DL_INFO,
  3841. "%s: CAL Cache: CRC=%04X Total Size=%d\n",
  3842. __func__, cal_cache_chip_id,
  3843. cal_cache_len);
  3844. break;
  3845. }
  3846. exit:
  3847. pt_debug(dev, DL_INFO,
  3848. "%s: CAL Cache exit: rc=%d CRC=0x%04X Total Size=%d\n",
  3849. __func__, rc, *crc, *size);
  3850. return rc;
  3851. }
  3852. /*******************************************************************************
  3853. * FUNCTION: pt_pip_run_selftest_
  3854. *
  3855. * SUMMARY: Sends the PIP "Run Self Test" (0x26) command to the DUT
  3856. * to execute a FW built in self test
  3857. *
  3858. * RETURN:
  3859. * 0 = success
  3860. * !0 = failure
  3861. *
  3862. * PARAMETERS:
  3863. * *cd - pointer to core data
  3864. * test_id - enumerated test ID to run
  3865. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3866. * *status - pointer to store the read response status
  3867. * *summary_results - pointer to store the results summary
  3868. * *results_available - pointer to store if results are available
  3869. *****************************************************************************/
  3870. static int pt_pip_run_selftest_(
  3871. struct pt_core_data *cd, u8 test_id,
  3872. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3873. u8 *results_available)
  3874. {
  3875. int rc = 0;
  3876. u8 write_buf[2];
  3877. struct pt_hid_output hid_output = {
  3878. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RUN_SELF_TEST),
  3879. .write_length = 2,
  3880. .write_buf = write_buf,
  3881. .timeout_ms = PT_PIP1_CMD_RUN_SELF_TEST_TIMEOUT,
  3882. };
  3883. write_buf[0] = test_id;
  3884. write_buf[1] = write_idacs_to_flash;
  3885. if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  3886. hid_output.write_length = 1;
  3887. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  3888. if (rc)
  3889. return rc;
  3890. if (status)
  3891. *status = cd->response_buf[5];
  3892. if (summary_result)
  3893. *summary_result = cd->response_buf[6];
  3894. /* results_available only available before PIP 1.03 */
  3895. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 3)) {
  3896. if (results_available)
  3897. *results_available = cd->response_buf[7];
  3898. }
  3899. return rc;
  3900. }
  3901. /*******************************************************************************
  3902. * FUNCTION: pt_pip_run_selftest
  3903. *
  3904. * SUMMARY: Protected call to pt_hid_output_run_selftest within
  3905. * an exclusive access to the DUT.
  3906. *
  3907. * RETURN:
  3908. * 0 = success
  3909. * !0 = failure
  3910. *
  3911. * PARAMETERS:
  3912. * *cd - pointer to core data
  3913. * test_id - enumerated test ID to run
  3914. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3915. * *status - pointer to store the read response status
  3916. * *summary_results - pointer to store the results summary
  3917. * *results_available - pointer to store if results are available
  3918. ******************************************************************************/
  3919. static int pt_pip_run_selftest(
  3920. struct pt_core_data *cd, u8 test_id,
  3921. u8 write_idacs_to_flash, u8 *status, u8 *summary_result,
  3922. u8 *results_available)
  3923. {
  3924. int rc;
  3925. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  3926. if (rc < 0) {
  3927. pt_debug(cd->dev, DL_ERROR,
  3928. "%s: fail get exclusive ex=%p own=%p\n",
  3929. __func__, cd->exclusive_dev, cd->dev);
  3930. return rc;
  3931. }
  3932. rc = pt_pip_run_selftest_(cd, test_id,
  3933. write_idacs_to_flash, status, summary_result,
  3934. results_available);
  3935. if (release_exclusive(cd, cd->dev) < 0)
  3936. pt_debug(cd->dev, DL_ERROR,
  3937. "%s: fail to release exclusive\n", __func__);
  3938. return rc;
  3939. }
  3940. /*******************************************************************************
  3941. * FUNCTION: _pt_request_pip_run_selftest
  3942. *
  3943. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  3944. * calls to the protected or unprotected call to pt_pip_run_selftest
  3945. *
  3946. * RETURN:
  3947. * 0 = success
  3948. * !0 = failure
  3949. *
  3950. * PARAMETERS:
  3951. * *dev - pointer to device structure
  3952. * protect - flag to call protected or non-protected
  3953. * test_id - enumerated test ID to run
  3954. * write_idacs_to_flash - flag whether to write new IDACS to flash
  3955. * *status - pointer to store the read response status
  3956. * *summary_results - pointer to store the results summary
  3957. * *results_available - pointer to store if results are available
  3958. ******************************************************************************/
  3959. static int _pt_request_pip_run_selftest(struct device *dev,
  3960. int protect, u8 test_id, u8 write_idacs_to_flash, u8 *status,
  3961. u8 *summary_result, u8 *results_available)
  3962. {
  3963. struct pt_core_data *cd = dev_get_drvdata(dev);
  3964. if (protect)
  3965. return pt_pip_run_selftest(cd, test_id,
  3966. write_idacs_to_flash, status, summary_result,
  3967. results_available);
  3968. return pt_pip_run_selftest_(cd, test_id,
  3969. write_idacs_to_flash, status, summary_result,
  3970. results_available);
  3971. }
  3972. /*******************************************************************************
  3973. * FUNCTION: _pt_pip_get_selftest_result_
  3974. *
  3975. * SUMMARY: Sends the PIP "Get Self Test Results" (0x27) command to the DUT
  3976. * to retrieve the self test results from the self test already executed
  3977. *
  3978. * RETURN:
  3979. * 0 = success
  3980. * !0 = failure
  3981. *
  3982. * PARAMETERS:
  3983. * *cd - pointer to core data
  3984. * read_offset - read pointer offset
  3985. * read_length - length of data to read
  3986. * test_id - enumerated test ID to read selftest results from
  3987. * *status - pointer to store the read response status
  3988. * *actual_read_len - pointer to store data length actually read
  3989. * *status - pointer to where the cmd response statas is stored
  3990. ******************************************************************************/
  3991. static int pt_pip_get_selftest_result_(
  3992. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  3993. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  3994. {
  3995. int rc = 0;
  3996. u16 total_read_len = 0;
  3997. u16 read_len;
  3998. u16 off_buf = 0;
  3999. u8 write_buf[5];
  4000. u8 read_test_id;
  4001. bool repeat;
  4002. struct pt_hid_output hid_output = {
  4003. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SELF_TEST_RESULT),
  4004. .write_length = 5,
  4005. .write_buf = write_buf,
  4006. };
  4007. /*
  4008. * Do not repeat reading for Auto Shorts test
  4009. * when PIP version < 1.3
  4010. */
  4011. repeat = IS_PIP_VER_GE(&cd->sysinfo, 1, 3)
  4012. || test_id != PT_ST_ID_AUTOSHORTS;
  4013. again:
  4014. write_buf[0] = LOW_BYTE(read_offset);
  4015. write_buf[1] = HI_BYTE(read_offset);
  4016. write_buf[2] = LOW_BYTE(read_length);
  4017. write_buf[3] = HI_BYTE(read_length);
  4018. write_buf[4] = test_id;
  4019. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4020. if (rc)
  4021. return rc;
  4022. if (cd->response_buf[5] != PT_CMD_STATUS_SUCCESS)
  4023. goto set_status;
  4024. read_test_id = cd->response_buf[6];
  4025. if (read_test_id != test_id)
  4026. return -EPROTO;
  4027. read_len = get_unaligned_le16(&cd->response_buf[7]);
  4028. if (read_len && data) {
  4029. memcpy(&data[off_buf], &cd->response_buf[10], read_len);
  4030. total_read_len += read_len;
  4031. if (repeat && read_len < read_length) {
  4032. read_offset += read_len;
  4033. off_buf += read_len;
  4034. read_length -= read_len;
  4035. goto again;
  4036. }
  4037. }
  4038. if (actual_read_len)
  4039. *actual_read_len = total_read_len;
  4040. set_status:
  4041. if (status)
  4042. *status = cd->response_buf[5];
  4043. return rc;
  4044. }
  4045. /*******************************************************************************
  4046. * FUNCTION: _pt_pip_get_selftest_result
  4047. *
  4048. * SUMMARY: Protected call to pt_hid_output_get_selftest_result by exclusive
  4049. * access to the DUT
  4050. *
  4051. * RETURN:
  4052. * 0 = success
  4053. * !0 = failure
  4054. *
  4055. * PARAMETERS:
  4056. * *cd - pointer to core data
  4057. * read_offset - read pointer offset
  4058. * read_length - length of data to read
  4059. * test_id - enumerated test ID to read selftest results from
  4060. * *status - pointer to store the read response status
  4061. * *actual_read_len - pointer to store data length actually read
  4062. * *status - pointer to where the cmd response statas is stored
  4063. ******************************************************************************/
  4064. static int pt_pip_get_selftest_result(
  4065. struct pt_core_data *cd, u16 read_offset, u16 read_length,
  4066. u8 test_id, u8 *status, u16 *actual_read_len, u8 *data)
  4067. {
  4068. int rc;
  4069. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4070. if (rc < 0) {
  4071. pt_debug(cd->dev, DL_ERROR,
  4072. "%s: fail get exclusive ex=%p own=%p\n",
  4073. __func__, cd->exclusive_dev, cd->dev);
  4074. return rc;
  4075. }
  4076. rc = pt_pip_get_selftest_result_(cd, read_offset,
  4077. read_length, test_id, status, actual_read_len, data);
  4078. if (release_exclusive(cd, cd->dev) < 0)
  4079. pt_debug(cd->dev, DL_ERROR,
  4080. "%s: fail to release exclusive\n", __func__);
  4081. return rc;
  4082. }
  4083. /*******************************************************************************
  4084. * FUNCTION: _pt_request_pip_get_selftest_result
  4085. *
  4086. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4087. * calls to the protected or unprotected call to pt_pip_get_selftest_result
  4088. *
  4089. * RETURN:
  4090. * 0 = success
  4091. * !0 = failure
  4092. *
  4093. * PARAMETERS:
  4094. * *dev - pointer to device structure
  4095. * protect - flag to call protected or non-protected
  4096. * read_offset - read pointer offset
  4097. * read_length - length of data to read
  4098. * test_id - enumerated test ID to read selftest results from
  4099. * *status - pointer to store the read response status
  4100. * *actual_read_len - pointer to store data length actually read
  4101. * *data - pointer to where the data read is stored
  4102. ******************************************************************************/
  4103. static int _pt_request_pip_get_selftest_result(struct device *dev,
  4104. int protect, u16 read_offset, u16 read_length, u8 test_id,
  4105. u8 *status, u16 *actual_read_len, u8 *data)
  4106. {
  4107. struct pt_core_data *cd = dev_get_drvdata(dev);
  4108. if (protect)
  4109. return pt_pip_get_selftest_result(cd, read_offset,
  4110. read_length, test_id, status, actual_read_len,
  4111. data);
  4112. return pt_pip_get_selftest_result_(cd, read_offset,
  4113. read_length, test_id, status, actual_read_len,
  4114. data);
  4115. }
  4116. /*******************************************************************************
  4117. * FUNCTION: _pt_pip_load_self_test_param
  4118. *
  4119. * SUMMARY: Sends the PIP "Load Self Test Parameters" (0x25) command to the DUT
  4120. * to load parameters needed by a self test
  4121. *
  4122. * RETURN:
  4123. * 0 = success
  4124. * !0 = failure
  4125. *
  4126. * PARAMETERS:
  4127. * *cd - pointer to core data
  4128. * self_test_id - enumerated test ID for which the parmeters belong
  4129. * load_offset - mem offset to where to load parameters
  4130. * load_length - length of parameter data to load
  4131. * *parameters - pointer to list of parameter data
  4132. * *status - pointer to store the response status
  4133. * *ret_test_id - pointer to returned test id the parameters were stored
  4134. * *act_load_len - pointer to store the actual load length that was writen
  4135. ******************************************************************************/
  4136. static int pt_pip_load_self_test_param_(struct pt_core_data *cd,
  4137. u8 self_test_id, u16 load_offset, u16 load_length,
  4138. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4139. {
  4140. int rc = 0;
  4141. int i;
  4142. u8 write_buf[PT_MAX_PIP1_MSG_SIZE];
  4143. struct pt_hid_output hid_output = {
  4144. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_LOAD_SELF_TEST_PARAM),
  4145. .write_length = 5 + load_length,
  4146. .write_buf = write_buf,
  4147. .timeout_ms = PT_PIP1_CMD_DEFAULT_TIMEOUT,
  4148. };
  4149. write_buf[0] = LOW_BYTE(load_offset);
  4150. write_buf[1] = HI_BYTE(load_offset);
  4151. write_buf[2] = LOW_BYTE(load_length);
  4152. write_buf[3] = HI_BYTE(load_length);
  4153. write_buf[4] = self_test_id;
  4154. for (i = 0; i < load_length; i++)
  4155. write_buf[i + 5] = parameters[i];
  4156. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4157. if (rc)
  4158. return rc;
  4159. if (status)
  4160. *status = cd->response_buf[5];
  4161. if (ret_test_id)
  4162. *ret_test_id = cd->response_buf[6];
  4163. if (act_load_len)
  4164. *act_load_len = get_unaligned_le16(&cd->response_buf[7]);
  4165. return rc;
  4166. }
  4167. /*******************************************************************************
  4168. * FUNCTION: pt_pip_load_self_test_param
  4169. *
  4170. * SUMMARY: Protected call to pt_pip_load_self_test_param_ within an exclusive
  4171. * access to the DUT.
  4172. *
  4173. * RETURN:
  4174. * 0 = success
  4175. * !0 = failure
  4176. *
  4177. * PARAMETERS:
  4178. * *cd - pointer to core data
  4179. * self_test_id - enumerated test ID for which the parmeters belong
  4180. * load_offset - mem offset to where to load parameters
  4181. * load_length - length of parameter data to load
  4182. * *parameters - pointer to list of parameter data
  4183. * *status - pointer to store the response status
  4184. * *ret_test_id - pointer to returned test id the parameters were stored
  4185. * *act_load_len - pointer to store the actual load length that was writen
  4186. ******************************************************************************/
  4187. static int pt_pip_load_self_test_param(struct pt_core_data *cd,
  4188. u8 self_test_id, u16 load_offset, u16 load_length,
  4189. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4190. {
  4191. int rc;
  4192. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4193. if (rc < 0) {
  4194. pt_debug(cd->dev, DL_ERROR,
  4195. "%s: fail get exclusive ex=%p own=%p\n",
  4196. __func__, cd->exclusive_dev, cd->dev);
  4197. return rc;
  4198. }
  4199. rc = pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4200. load_length, parameters, status, ret_test_id, act_load_len);
  4201. if (release_exclusive(cd, cd->dev) < 0)
  4202. pt_debug(cd->dev, DL_ERROR,
  4203. "%s: fail to release exclusive\n", __func__);
  4204. return rc;
  4205. }
  4206. /*******************************************************************************
  4207. * FUNCTION: _pt_request_pip_load_self_test_param
  4208. *
  4209. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4210. * calls to the protected or unprotected call to
  4211. * pt_pip_load_self_test_param
  4212. *
  4213. * RETURN:
  4214. * 0 = success
  4215. * !0 = failure
  4216. *
  4217. * PARAMETERS:
  4218. * *dev - pointer to device structure
  4219. * protect - flag to call protected or non-protected
  4220. * self_test_id - enumerated test ID for which the parmeters belong
  4221. * load_offset - mem offset to where to load parameters
  4222. * load_length - length of parameter data to load
  4223. * *parameters - pointer to list of parameter data
  4224. * *status - pointer to store the response status
  4225. * *ret_test_id - pointer to returned test id the parameters were stored
  4226. * *act_load_len - pointer to store the actual load length that was writen
  4227. ******************************************************************************/
  4228. static int _pt_request_pip_load_self_test_param(struct device *dev,
  4229. int protect, u8 self_test_id, u16 load_offset, u16 load_length,
  4230. u8 *parameters, u8 *status, u8 *ret_test_id, u16 *act_load_len)
  4231. {
  4232. struct pt_core_data *cd = dev_get_drvdata(dev);
  4233. if (protect)
  4234. return pt_pip_load_self_test_param(cd, self_test_id,
  4235. load_offset, load_length, parameters, status, ret_test_id,
  4236. act_load_len);
  4237. return pt_pip_load_self_test_param_(cd, self_test_id, load_offset,
  4238. load_length, parameters, status, ret_test_id, act_load_len);
  4239. }
  4240. /*******************************************************************************
  4241. * FUNCTION: pt_pip_calibrate_ext_
  4242. *
  4243. * SUMMARY: Send the PIP1 Extended Calibrate command (0x30) to the DUT waiting
  4244. * for the response
  4245. *
  4246. * NOTE: This calibrate command requires the DUT to support PIP version >= 1.10
  4247. *
  4248. * RETURN:
  4249. * 0 = success
  4250. * !0 = failure
  4251. *
  4252. * PARAMETERS:
  4253. * *cd - pointer to core data
  4254. * *cal_data - pointer to extended calibration data structure
  4255. * *status - pointer to where the command response status is stored
  4256. ******************************************************************************/
  4257. static int pt_pip_calibrate_ext_(struct pt_core_data *cd,
  4258. struct pt_cal_ext_data *cal_data, u8 *status)
  4259. {
  4260. int rc = 0;
  4261. int write_length = 4;
  4262. u8 write_buf[4];
  4263. u16 size = 0;
  4264. unsigned short crc = 0;
  4265. struct pt_hid_output hid_output = {
  4266. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_DEVICE_EXTENDED),
  4267. .write_length = write_length,
  4268. .write_buf = write_buf,
  4269. .timeout_ms = PT_PIP1_CMD_CALIBRATE_EXT_TIMEOUT,
  4270. };
  4271. if (cal_data == NULL)
  4272. return -EINVAL;
  4273. memcpy(write_buf, cal_data, sizeof(struct pt_cal_ext_data));
  4274. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4275. if (rc)
  4276. return rc;
  4277. if (status)
  4278. *status = cd->response_buf[5];
  4279. /*
  4280. * When doing a calibration on a flashless DUT, save CAL data in
  4281. * the TTDL cache on any successful calibration
  4282. */
  4283. if (*status == 0 && cd->cal_cache_in_host) {
  4284. pt_debug(cd->dev, DL_INFO, "%s: Retrieve and Save CAL\n",
  4285. __func__);
  4286. rc = _pt_manage_local_cal_data(cd->dev, PT_CAL_DATA_SAVE,
  4287. &size, &crc);
  4288. if (rc)
  4289. pt_debug(cd->dev, DL_ERROR,
  4290. "%s: Error Saving CAL rc=%d\n", __func__, rc);
  4291. else
  4292. pt_debug(cd->dev, DL_INFO,
  4293. "%s: Saved CAL: chip ID=0x%04X size=%d\n",
  4294. __func__, crc, size);
  4295. }
  4296. return 0;
  4297. }
  4298. /*******************************************************************************
  4299. * FUNCTION: pt_pip_calibrate_ext
  4300. *
  4301. * SUMMARY: Protected call to pt_pip_calibrate_ext_ by exclusive access to the
  4302. * DUT
  4303. *
  4304. * RETURN:
  4305. * 0 = success
  4306. * !0 = failure
  4307. *
  4308. * PARAMETERS:
  4309. * *cd - pointer to core data
  4310. * *cal_data - pointer to extended calibration data structure
  4311. * *status - pointer to where the command response status is stored
  4312. ******************************************************************************/
  4313. static int pt_pip_calibrate_ext(struct pt_core_data *cd,
  4314. struct pt_cal_ext_data *cal_data, u8 *status)
  4315. {
  4316. int rc;
  4317. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4318. if (rc < 0) {
  4319. pt_debug(cd->dev, DL_ERROR,
  4320. "%s: fail get exclusive ex=%p own=%p\n",
  4321. __func__, cd->exclusive_dev, cd->dev);
  4322. return rc;
  4323. }
  4324. rc = pt_pip_calibrate_ext_(cd, cal_data, status);
  4325. if (release_exclusive(cd, cd->dev) < 0)
  4326. pt_debug(cd->dev, DL_ERROR,
  4327. "%s: fail to release exclusive\n", __func__);
  4328. return rc;
  4329. }
  4330. /*******************************************************************************
  4331. * FUNCTION: _pt_request_pip_calibrate_ext
  4332. *
  4333. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4334. * calls to the protected or unprotected call to pt_pip_calibrate_ext
  4335. *
  4336. * RETURN:
  4337. * 0 = success
  4338. * !0 = failure
  4339. *
  4340. * PARAMETERS:
  4341. * *dev - pointer to device structure
  4342. * protect - flag to call protected or non-protected
  4343. * *cal_data - pointer to extended calibration data structure
  4344. * *status - pointer to where the command response status is stored
  4345. ******************************************************************************/
  4346. static int _pt_request_pip_calibrate_ext(struct device *dev,
  4347. int protect, struct pt_cal_ext_data *cal_data, u8 *status)
  4348. {
  4349. struct pt_core_data *cd = dev_get_drvdata(dev);
  4350. if (protect)
  4351. return pt_pip_calibrate_ext(cd, cal_data, status);
  4352. return pt_pip_calibrate_ext_(cd, cal_data, status);
  4353. }
  4354. /*******************************************************************************
  4355. * FUNCTION: pt_pip_calibrate_idacs_
  4356. *
  4357. * SUMMARY: Send the PIP Calibrate IDACs command (0x28) to the DUT waiting
  4358. * for the response
  4359. *
  4360. * RETURN:
  4361. * 0 = success
  4362. * !0 = failure
  4363. *
  4364. * PARAMETERS:
  4365. * *cd - pointer to core data
  4366. * mode - sense mode to calibrate (0-5)
  4367. * *status - pointer to where the command response status is stored
  4368. ******************************************************************************/
  4369. static int pt_pip_calibrate_idacs_(struct pt_core_data *cd,
  4370. u8 mode, u8 *status)
  4371. {
  4372. int rc = 0;
  4373. int write_length = 1;
  4374. u8 write_buf[1];
  4375. u8 cmd_offset = 0;
  4376. struct pt_hid_output hid_output = {
  4377. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_CALIBRATE_IDACS),
  4378. .write_length = write_length,
  4379. .write_buf = write_buf,
  4380. .timeout_ms = PT_PIP1_CMD_CALIBRATE_IDAC_TIMEOUT,
  4381. };
  4382. write_buf[cmd_offset++] = mode;
  4383. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4384. if (rc)
  4385. return rc;
  4386. *status = cd->response_buf[5];
  4387. if (*status)
  4388. return -EINVAL;
  4389. return 0;
  4390. }
  4391. /*******************************************************************************
  4392. * FUNCTION: pt_pip_calibrate_idacs
  4393. *
  4394. * SUMMARY: Protected call to pt_hid_output_calibrate_idacs_ by exclusive
  4395. * access to the DUT
  4396. *
  4397. * RETURN:
  4398. * 0 = success
  4399. * !0 = failure
  4400. *
  4401. * PARAMETERS:
  4402. * *cd - pointer to core data
  4403. * mode - sense mode to calibrate (0-5)
  4404. * *status - pointer to where the command response status is stored
  4405. ******************************************************************************/
  4406. static int pt_pip_calibrate_idacs(struct pt_core_data *cd,
  4407. u8 mode, u8 *status)
  4408. {
  4409. int rc;
  4410. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4411. if (rc < 0) {
  4412. pt_debug(cd->dev, DL_ERROR,
  4413. "%s: fail get exclusive ex=%p own=%p\n",
  4414. __func__, cd->exclusive_dev, cd->dev);
  4415. return rc;
  4416. }
  4417. rc = pt_pip_calibrate_idacs_(cd, mode, status);
  4418. if (release_exclusive(cd, cd->dev) < 0)
  4419. pt_debug(cd->dev, DL_ERROR,
  4420. "%s: fail to release exclusive\n", __func__);
  4421. return rc;
  4422. }
  4423. /*******************************************************************************
  4424. * FUNCTION: _pt_request_pip_calibrate_idacs
  4425. *
  4426. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4427. * calls to the protected or unprotected call to pt_pip_calibrate_idacs
  4428. *
  4429. * RETURN:
  4430. * 0 = success
  4431. * !0 = failure
  4432. *
  4433. * PARAMETERS:
  4434. * *dev - pointer to device structure
  4435. * protect - flag to call protected or non-protected
  4436. * mode - sense mode to calibrate (0-5)
  4437. * *status - pointer to where the command response status is stored
  4438. ******************************************************************************/
  4439. static int _pt_request_pip_calibrate_idacs(struct device *dev,
  4440. int protect, u8 mode, u8 *status)
  4441. {
  4442. struct pt_core_data *cd = dev_get_drvdata(dev);
  4443. if (protect)
  4444. return pt_pip_calibrate_idacs(cd, mode, status);
  4445. return pt_pip_calibrate_idacs_(cd, mode, status);
  4446. }
  4447. /*******************************************************************************
  4448. * FUNCTION: pt_hid_output_initialize_baselines_
  4449. *
  4450. * SUMMARY: Send the PIP "Initialize Baselines" command (0x29) to the DUT
  4451. * waiting for the response.
  4452. *
  4453. * RETURN:
  4454. * 0 = success
  4455. * !0 = failure
  4456. *
  4457. * PARAMETERS:
  4458. * *cd - pointer to core data
  4459. * test_id - bit type flag to allow initialize baseline MUT,BTN,SELG
  4460. * each or together with a single command.
  4461. * *status - pointer to where the command response status is stored
  4462. ******************************************************************************/
  4463. static int pt_hid_output_initialize_baselines_(
  4464. struct pt_core_data *cd, u8 test_id, u8 *status)
  4465. {
  4466. int rc = 0;
  4467. int write_length = 1;
  4468. u8 write_buf[1];
  4469. u8 cmd_offset = 0;
  4470. struct pt_hid_output hid_output = {
  4471. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_INITIALIZE_BASELINES),
  4472. .write_length = write_length,
  4473. .write_buf = write_buf,
  4474. };
  4475. write_buf[cmd_offset++] = test_id;
  4476. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4477. if (rc)
  4478. return rc;
  4479. *status = cd->response_buf[5];
  4480. if (*status)
  4481. return -EINVAL;
  4482. return rc;
  4483. }
  4484. /*******************************************************************************
  4485. * FUNCTION: pt_hid_output_initialize_baselines
  4486. *
  4487. * SUMMARY: Protected call to pt_hid_output_initialize_baselines_ by exclusive
  4488. * access to the DUT
  4489. *
  4490. * RETURN:
  4491. * 0 = success
  4492. * !0 = failure
  4493. *
  4494. * PARAMETERS:
  4495. * *cd - pointer to core data
  4496. * test_id - enumerated ID against which to initialize the baseline
  4497. * *status - pointer to where the command response status is stored
  4498. ******************************************************************************/
  4499. static int pt_hid_output_initialize_baselines(struct pt_core_data *cd,
  4500. u8 test_id, u8 *status)
  4501. {
  4502. int rc;
  4503. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4504. if (rc < 0) {
  4505. pt_debug(cd->dev, DL_ERROR,
  4506. "%s: fail get exclusive ex=%p own=%p\n",
  4507. __func__, cd->exclusive_dev, cd->dev);
  4508. return rc;
  4509. }
  4510. rc = pt_hid_output_initialize_baselines_(cd, test_id, status);
  4511. if (release_exclusive(cd, cd->dev) < 0)
  4512. pt_debug(cd->dev, DL_ERROR,
  4513. "%s: fail to release exclusive\n", __func__);
  4514. return rc;
  4515. }
  4516. /*******************************************************************************
  4517. * FUNCTION: _pt_request_pip_initialize_baselines
  4518. *
  4519. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4520. * calls to the protected or unprotected call to
  4521. * pt_pip_initialize_baselines
  4522. *
  4523. * RETURN:
  4524. * 0 = success
  4525. * !0 = failure
  4526. *
  4527. * PARAMETERS:
  4528. * *dev - pointer to device structure
  4529. * protect - flag to call protected or non-protected
  4530. * test_id - enumerated ID against which to initialize the baseline
  4531. * *status - pointer to where the command response status is stored
  4532. ******************************************************************************/
  4533. static int _pt_request_pip_initialize_baselines(struct device *dev,
  4534. int protect, u8 test_id, u8 *status)
  4535. {
  4536. struct pt_core_data *cd = dev_get_drvdata(dev);
  4537. if (protect)
  4538. return pt_hid_output_initialize_baselines(cd, test_id,
  4539. status);
  4540. return pt_hid_output_initialize_baselines_(cd, test_id, status);
  4541. }
  4542. /*******************************************************************************
  4543. * FUNCTION: pt_hid_output_exec_panel_scan_
  4544. *
  4545. * SUMMARY: Sends the PIP "Execute Panel Scan" (0x2A) to the DUT and waits for
  4546. * the response
  4547. *
  4548. * RETURN:
  4549. * 0 = success
  4550. * !0 = failure
  4551. *
  4552. * PARAMETERS:
  4553. * *cd - pointer to core data
  4554. ******************************************************************************/
  4555. static int pt_hid_output_exec_panel_scan_(struct pt_core_data *cd)
  4556. {
  4557. struct pt_hid_output hid_output = {
  4558. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_EXEC_PANEL_SCAN),
  4559. };
  4560. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  4561. }
  4562. /*******************************************************************************
  4563. * FUNCTION: pt_hid_output_exec_panel_scan
  4564. *
  4565. * SUMMARY: Protected call to pt_hid_output_exec_panel_scan_ by exclusive
  4566. * access to the DUT.
  4567. *
  4568. * RETURN:
  4569. * 0 = success
  4570. * !0 = failure
  4571. *
  4572. * PARAMETERS:
  4573. * *cd - pointer to core data
  4574. ******************************************************************************/
  4575. static int pt_hid_output_exec_panel_scan(struct pt_core_data *cd)
  4576. {
  4577. int rc;
  4578. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4579. if (rc < 0) {
  4580. pt_debug(cd->dev, DL_ERROR,
  4581. "%s: fail get exclusive ex=%p own=%p\n",
  4582. __func__, cd->exclusive_dev, cd->dev);
  4583. return rc;
  4584. }
  4585. rc = pt_hid_output_exec_panel_scan_(cd);
  4586. if (release_exclusive(cd, cd->dev) < 0)
  4587. pt_debug(cd->dev, DL_ERROR,
  4588. "%s: fail to release exclusive\n", __func__);
  4589. return rc;
  4590. }
  4591. /*******************************************************************************
  4592. * FUNCTION: pt_pip2_exec_panel_scan_
  4593. *
  4594. * SUMMARY: Send the PIP2 "Execute Panel Scan" (0x21) to the DUT and waits for
  4595. * the response
  4596. *
  4597. * RETURN:
  4598. * 0 = success
  4599. * !0 = failure
  4600. *
  4601. * PARAMETERS:
  4602. * *cd - pointer to core data
  4603. * scan_type - type of panel scan to perform (PIP2 only)
  4604. ******************************************************************************/
  4605. static int pt_pip2_exec_panel_scan_(struct pt_core_data *cd, u8 scan_type)
  4606. {
  4607. int rc = 0;
  4608. u8 data[2];
  4609. u8 read_buf[10];
  4610. u16 actual_read_len;
  4611. pt_debug(cd->dev, DL_DEBUG, "%s: PIP2 Execute Scan %d\n",
  4612. __func__, scan_type);
  4613. data[0] = scan_type;
  4614. rc = _pt_request_pip2_send_cmd(cd->dev,
  4615. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_EXECUTE_SCAN,
  4616. data, 1, read_buf, &actual_read_len);
  4617. if (rc) {
  4618. pt_debug(cd->dev, DL_ERROR,
  4619. "%s EXECUTE_SCAN command for type %d failed. rc=%d\n",
  4620. __func__, scan_type, rc);
  4621. }
  4622. return rc;
  4623. }
  4624. /*******************************************************************************
  4625. * FUNCTION: pt_pip2_exec_panel_scan
  4626. *
  4627. * SUMMARY: Protected call to pt_pip2_exec_panel_scan_ by exclusive
  4628. * access to the DUT.
  4629. *
  4630. * RETURN:
  4631. * 0 = success
  4632. * !0 = failure
  4633. *
  4634. * PARAMETERS:
  4635. * *cd - pointer to core data
  4636. * scan_type - type of panel scan to perform (PIP2 only)
  4637. ******************************************************************************/
  4638. static int pt_pip2_exec_panel_scan(struct pt_core_data *cd, u8 scan_type)
  4639. {
  4640. int rc;
  4641. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4642. if (rc < 0) {
  4643. pt_debug(cd->dev, DL_ERROR,
  4644. "%s: fail get exclusive ex=%p own=%p\n",
  4645. __func__, cd->exclusive_dev, cd->dev);
  4646. return rc;
  4647. }
  4648. rc = pt_pip2_exec_panel_scan_(cd, scan_type);
  4649. if (release_exclusive(cd, cd->dev) < 0)
  4650. pt_debug(cd->dev, DL_ERROR,
  4651. "%s: fail to release exclusive\n", __func__);
  4652. return rc;
  4653. }
  4654. /*******************************************************************************
  4655. * FUNCTION: _pt_request_pip_exec_panel_scan
  4656. *
  4657. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4658. * calls to the protected or unprotected call to
  4659. * pt_pip2_exec_panel_scan or pt_hid_output_exec_panel_scan
  4660. *
  4661. * RETURN:
  4662. * 0 = success
  4663. * !0 = failure
  4664. *
  4665. * PARAMETERS:
  4666. * *dev - pointer to device structure
  4667. * protect - flag to call protected or non-protected
  4668. * scan_type - type of panel scan to perform (PIP2 only)
  4669. ******************************************************************************/
  4670. static int _pt_request_pip_exec_panel_scan(struct device *dev,
  4671. int protect, u8 scan_type)
  4672. {
  4673. struct pt_core_data *cd = dev_get_drvdata(dev);
  4674. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 12)) {
  4675. if (protect)
  4676. return pt_pip2_exec_panel_scan(cd, scan_type);
  4677. return pt_pip2_exec_panel_scan_(cd, scan_type);
  4678. }
  4679. if (protect)
  4680. return pt_hid_output_exec_panel_scan(cd);
  4681. return pt_hid_output_exec_panel_scan_(cd);
  4682. }
  4683. /*******************************************************************************
  4684. * FUNCTION: pt_hid_output_retrieve_panel_scan_
  4685. *
  4686. * SUMMARY: Sends the PIP "Retrieve Panel Scan" (0x2B) command to the DUT
  4687. * to retrieve the specified data type for a the last successful Execute
  4688. * Panel Scan command.
  4689. *
  4690. * RETURN:
  4691. * 0 = success
  4692. * !0 = failure
  4693. *
  4694. * PARAMETERS:
  4695. * *dev - pointer to device structure
  4696. * protect - flag to call protected or non-protected
  4697. * read_offset - read pointer offset
  4698. * read_count - length of data to read
  4699. * data_id - enumerated test ID to read selftest results from
  4700. * *response - pointer to store the read response status
  4701. * *config - pointer to store config data
  4702. * *actual_read_len - pointer to store data length actually read
  4703. * *read_buf - pointer to the read buffer
  4704. ******************************************************************************/
  4705. static int pt_hid_output_retrieve_panel_scan_(
  4706. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4707. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4708. u8 *read_buf)
  4709. {
  4710. int status;
  4711. u8 read_data_id;
  4712. int rc = 0;
  4713. int write_length = 5;
  4714. u8 write_buf[5];
  4715. u8 cmd_offset = 0;
  4716. u8 data_elem_size;
  4717. int size;
  4718. int data_size;
  4719. struct pt_hid_output hid_output = {
  4720. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_RETRIEVE_PANEL_SCAN),
  4721. .write_length = write_length,
  4722. .write_buf = write_buf,
  4723. };
  4724. write_buf[cmd_offset++] = LOW_BYTE(read_offset);
  4725. write_buf[cmd_offset++] = HI_BYTE(read_offset);
  4726. write_buf[cmd_offset++] = LOW_BYTE(read_count);
  4727. write_buf[cmd_offset++] = HI_BYTE(read_count);
  4728. write_buf[cmd_offset++] = data_id;
  4729. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4730. if (rc)
  4731. return rc;
  4732. status = cd->response_buf[5];
  4733. if (status)
  4734. return -EINVAL;
  4735. read_data_id = cd->response_buf[6];
  4736. if (read_data_id != data_id)
  4737. return -EPROTO;
  4738. size = get_unaligned_le16(&cd->response_buf[0]);
  4739. *actual_read_len = get_unaligned_le16(&cd->response_buf[7]);
  4740. *config = cd->response_buf[9];
  4741. data_elem_size = *config & 0x07;
  4742. data_size = *actual_read_len * data_elem_size;
  4743. if (read_buf)
  4744. memcpy(read_buf, &cd->response_buf[10], data_size);
  4745. if (response)
  4746. memcpy(response, cd->response_buf, size);
  4747. return rc;
  4748. }
  4749. /*******************************************************************************
  4750. * FUNCTION: pt_hid_output_retrieve_panel_scan
  4751. *
  4752. * SUMMARY: Protected call to pt_hid_output_retrieve_panel_scan_ by exclusive
  4753. * access to the DUT.
  4754. *
  4755. * RETURN:
  4756. * 0 = success
  4757. * !0 = failure
  4758. *
  4759. * PARAMETERS:
  4760. * *dev - pointer to device structure
  4761. * protect - flag to call protected or non-protected
  4762. * read_offset - read pointer offset
  4763. * read_count - length of data to read
  4764. * data_id - enumerated test ID to read selftest results from
  4765. * *response - pointer to store the read response status
  4766. * *config - pointer to store config data
  4767. * *actual_read_len - pointer to store data length actually read
  4768. * *read_buf - pointer to the read buffer
  4769. ******************************************************************************/
  4770. static int pt_hid_output_retrieve_panel_scan(
  4771. struct pt_core_data *cd, u16 read_offset, u16 read_count,
  4772. u8 data_id, u8 *response, u8 *config, u16 *actual_read_len,
  4773. u8 *read_buf)
  4774. {
  4775. int rc;
  4776. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4777. if (rc < 0) {
  4778. pt_debug(cd->dev, DL_ERROR,
  4779. "%s: fail get exclusive ex=%p own=%p\n",
  4780. __func__, cd->exclusive_dev, cd->dev);
  4781. return rc;
  4782. }
  4783. rc = pt_hid_output_retrieve_panel_scan_(cd, read_offset,
  4784. read_count, data_id, response, config,
  4785. actual_read_len, read_buf);
  4786. if (release_exclusive(cd, cd->dev) < 0)
  4787. pt_debug(cd->dev, DL_ERROR,
  4788. "%s: fail to release exclusive\n", __func__);
  4789. return rc;
  4790. }
  4791. /*******************************************************************************
  4792. * FUNCTION: _pt_request_pip_retrieve_panel_scan
  4793. *
  4794. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4795. * calls to the protected or unprotected call to
  4796. * pt_hid_output_retrieve_panel_scan
  4797. *
  4798. * RETURN:
  4799. * 0 = success
  4800. * !0 = failure
  4801. *
  4802. * PARAMETERS:
  4803. * *dev - pointer to device structure
  4804. * protect - flag to call protected or non-protected
  4805. * read_offset - read pointer offset
  4806. * read_count - length of data to read
  4807. * data_id - enumerated test ID to read selftest results from
  4808. * *response - pointer to store the read response status
  4809. * *config - pointer to store config data
  4810. * *actual_read_len - pointer to store data length actually read
  4811. * *read_buf - pointer to the read buffer
  4812. ******************************************************************************/
  4813. static int _pt_request_pip_retrieve_panel_scan(struct device *dev,
  4814. int protect, u16 read_offset, u16 read_count, u8 data_id,
  4815. u8 *response, u8 *config, u16 *actual_read_len, u8 *read_buf)
  4816. {
  4817. struct pt_core_data *cd = dev_get_drvdata(dev);
  4818. if (protect)
  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. return pt_hid_output_retrieve_panel_scan_(cd,
  4823. read_offset, read_count, data_id, response,
  4824. config, actual_read_len, read_buf);
  4825. }
  4826. /*******************************************************************************
  4827. * FUNCTION: _pt_request_pip_user_cmd
  4828. *
  4829. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4830. * calls to the protected or unprotected call to
  4831. * pt_hid_output_user_cmd
  4832. *
  4833. * RETURN:
  4834. * 0 = success
  4835. * !0 = failure
  4836. *
  4837. * PARAMETERS:
  4838. * *dev - pointer to device structure
  4839. * protect - flag to call protected or non-protected
  4840. * read_len - length of data to read
  4841. * *read_buf - pointer to store read data
  4842. * write_len - length of data to write
  4843. * *write_buf - pointer to buffer to write
  4844. * *actual_read_len - pointer to store data length actually read
  4845. ******************************************************************************/
  4846. static int _pt_request_pip_user_cmd(struct device *dev,
  4847. int protect, u16 read_len, u8 *read_buf, u16 write_len,
  4848. u8 *write_buf, u16 *actual_read_len)
  4849. {
  4850. struct pt_core_data *cd = dev_get_drvdata(dev);
  4851. if (protect)
  4852. return pt_hid_output_user_cmd(cd, read_len, read_buf,
  4853. write_len, write_buf, actual_read_len);
  4854. return pt_hid_output_user_cmd_(cd, read_len, read_buf,
  4855. write_len, write_buf, actual_read_len);
  4856. }
  4857. /*******************************************************************************
  4858. * FUNCTION: pt_hid_output_bl_get_information_
  4859. *
  4860. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x38) command to the
  4861. * DUT to retrieve bootloader version and chip identification information.
  4862. *
  4863. * RETURN:
  4864. * 0 = success
  4865. * !0 = failure
  4866. *
  4867. * PARAMETERS:
  4868. * *cd - pointer to core data
  4869. * *return_data - pointer to store the return data
  4870. *****************************************************************************/
  4871. static int pt_hid_output_bl_get_information_(struct pt_core_data *cd,
  4872. u8 *return_data)
  4873. {
  4874. int rc;
  4875. int data_len;
  4876. struct pt_hid_output hid_output = {
  4877. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_INFO),
  4878. };
  4879. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4880. if (rc)
  4881. return rc;
  4882. data_len = get_unaligned_le16(&cd->input_buf[6]);
  4883. if (!data_len)
  4884. return -EPROTO;
  4885. memcpy(return_data, &cd->response_buf[8], data_len);
  4886. return 0;
  4887. }
  4888. /*******************************************************************************
  4889. * FUNCTION: pt_hid_output_bl_get_information
  4890. *
  4891. * SUMMARY: Protected call to pt_hid_output_bl_get_information_ by exclusive
  4892. * access to the DUT.
  4893. *
  4894. * RETURN:
  4895. * 0 = success
  4896. * !0 = failure
  4897. *
  4898. * PARAMETERS:
  4899. * *cd - pointer to core data
  4900. * *return_data - pointer to store the return data
  4901. ******************************************************************************/
  4902. static int pt_hid_output_bl_get_information(struct pt_core_data *cd,
  4903. u8 *return_data)
  4904. {
  4905. int rc;
  4906. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  4907. if (rc < 0) {
  4908. pt_debug(cd->dev, DL_ERROR,
  4909. "%s: fail get exclusive ex=%p own=%p\n",
  4910. __func__, cd->exclusive_dev, cd->dev);
  4911. return rc;
  4912. }
  4913. rc = pt_hid_output_bl_get_information_(cd, return_data);
  4914. if (release_exclusive(cd, cd->dev) < 0)
  4915. pt_debug(cd->dev, DL_ERROR,
  4916. "%s: fail to release exclusive\n", __func__);
  4917. return rc;
  4918. }
  4919. /*******************************************************************************
  4920. * FUNCTION: _pt_request_pip_bl_get_information
  4921. *
  4922. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  4923. * calls to the protected or unprotected call to
  4924. * pt_hid_output_bl_get_information
  4925. *
  4926. * RETURN:
  4927. * 0 = success
  4928. * !0 = failure
  4929. *
  4930. * PARAMETERS:
  4931. * *dev - pointer to device structure
  4932. * protect - flag to call protected or non-protected
  4933. * *return_data - pointer to store bl data
  4934. ******************************************************************************/
  4935. static int _pt_request_pip_bl_get_information(struct device *dev,
  4936. int protect, u8 *return_data)
  4937. {
  4938. struct pt_core_data *cd = dev_get_drvdata(dev);
  4939. if (protect)
  4940. return pt_hid_output_bl_get_information(cd, return_data);
  4941. return pt_hid_output_bl_get_information_(cd, return_data);
  4942. }
  4943. /*******************************************************************************
  4944. * FUNCTION: pt_hid_output_bl_initiate_bl_
  4945. *
  4946. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x48) command to the
  4947. * DUT to erases the entire TrueTouch application, Configuration Data block,
  4948. * and Design Data block in flash and enables the host to execute the Program
  4949. * and Verify Row command to bootload the application image and data.
  4950. *
  4951. * RETURN:
  4952. * 0 = success
  4953. * !0 = failure
  4954. *
  4955. * PARAMETERS:
  4956. * *cd - pointer to core data
  4957. * protect - flag to call protected or non-protected
  4958. * key_size - size of key
  4959. * *key_buf - pointer to key data to allow operation
  4960. * row_size - size of the meta data row
  4961. * *metadata_row_buf - pointer to meta data to write
  4962. ******************************************************************************/
  4963. static int pt_hid_output_bl_initiate_bl_(struct pt_core_data *cd,
  4964. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  4965. {
  4966. u16 write_length = key_size + row_size;
  4967. u8 *write_buf;
  4968. int rc = 0;
  4969. struct pt_hid_output hid_output = {
  4970. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_INITIATE_BL),
  4971. .write_length = write_length,
  4972. .timeout_ms = PT_PIP1_CMD_INITIATE_BL_TIMEOUT,
  4973. };
  4974. write_buf = kzalloc(write_length, GFP_KERNEL);
  4975. if (!write_buf)
  4976. return -ENOMEM;
  4977. hid_output.write_buf = write_buf;
  4978. if (key_size)
  4979. memcpy(write_buf, key_buf, key_size);
  4980. if (row_size)
  4981. memcpy(&write_buf[key_size], metadata_row_buf, row_size);
  4982. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  4983. kfree(write_buf);
  4984. return rc;
  4985. }
  4986. /*******************************************************************************
  4987. * FUNCTION: pt_hid_output_bl_initiate_bl
  4988. *
  4989. * SUMMARY: Protected call to pt_hid_output_bl_initiate_bl_ by exclusive
  4990. * access to the DUT.
  4991. *
  4992. * RETURN:
  4993. * 0 = success
  4994. * !0 = failure
  4995. *
  4996. * PARAMETERS:
  4997. * *cd - pointer to core data
  4998. * key_size - size of key
  4999. * *key_buf - pointer to key data to allow operation
  5000. * row_size - size of the meta data row
  5001. * *metadata_row_buf - pointer to meta data to write
  5002. ******************************************************************************/
  5003. static int pt_hid_output_bl_initiate_bl(struct pt_core_data *cd,
  5004. u16 key_size, u8 *key_buf, u16 row_size, u8 *metadata_row_buf)
  5005. {
  5006. int rc;
  5007. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5008. if (rc < 0) {
  5009. pt_debug(cd->dev, DL_ERROR,
  5010. "%s: fail get exclusive ex=%p own=%p\n",
  5011. __func__, cd->exclusive_dev, cd->dev);
  5012. return rc;
  5013. }
  5014. rc = pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5015. row_size, metadata_row_buf);
  5016. if (release_exclusive(cd, cd->dev) < 0)
  5017. pt_debug(cd->dev, DL_ERROR,
  5018. "%s: fail to release exclusive\n", __func__);
  5019. return rc;
  5020. }
  5021. /*******************************************************************************
  5022. * FUNCTION: _pt_request_pip_bl_initiate_bl
  5023. *
  5024. * SUMMARY: Function pointer included in core_nonhid_cmd struct for external
  5025. * calls to the protected or unprotected call to
  5026. * pt_hid_output_bl_initiate_bl
  5027. *
  5028. * RETURN:
  5029. * 0 = success
  5030. * !0 = failure
  5031. *
  5032. * PARAMETERS:
  5033. * *dev - pointer to device structure
  5034. * protect - flag to call protected or non-protected
  5035. * key_size - size of key
  5036. * *key_buf - pointer to key data to allow operation
  5037. * row_size - size of the meta data row
  5038. * *metadata_row_buf - pointer to meta data to write
  5039. ******************************************************************************/
  5040. static int _pt_request_pip_bl_initiate_bl(struct device *dev,
  5041. int protect, u16 key_size, u8 *key_buf, u16 row_size,
  5042. u8 *metadata_row_buf)
  5043. {
  5044. struct pt_core_data *cd = dev_get_drvdata(dev);
  5045. if (protect)
  5046. return pt_hid_output_bl_initiate_bl(cd, key_size, key_buf,
  5047. row_size, metadata_row_buf);
  5048. return pt_hid_output_bl_initiate_bl_(cd, key_size, key_buf,
  5049. row_size, metadata_row_buf);
  5050. }
  5051. /*******************************************************************************
  5052. * FUNCTION: pt_hid_output_bl_program_and_verify_
  5053. *
  5054. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x39) command to upload
  5055. * and program a 128-byte row into the flash, and then verifies written data.
  5056. *
  5057. * RETURN:
  5058. * 0 = success
  5059. * !0 = failure
  5060. *
  5061. * PARAMETERS:
  5062. * *cd - pointer to core data
  5063. * data_len - length of data_buf
  5064. * *data_buf - firmware image to program
  5065. ******************************************************************************/
  5066. static int pt_hid_output_bl_program_and_verify_(
  5067. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5068. {
  5069. struct pt_hid_output hid_output = {
  5070. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY),
  5071. .write_length = data_len,
  5072. .write_buf = data_buf,
  5073. .timeout_ms = PT_PIP1_CMD_PROGRAM_AND_VERIFY_TIMEOUT,
  5074. };
  5075. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5076. }
  5077. /*******************************************************************************
  5078. * FUNCTION: pt_hid_output_bl_program_and_verify
  5079. *
  5080. * SUMMARY: Protected call to pt_hid_output_bl_program_and_verify_ by exclusive
  5081. * access to the DUT.
  5082. *
  5083. * RETURN:
  5084. * 0 = success
  5085. * !0 = failure
  5086. *
  5087. * PARAMETERS:
  5088. * *cd - pointer to core data
  5089. * data_len - length of data_buf
  5090. * *data_buf - firmware image to program
  5091. ******************************************************************************/
  5092. static int pt_hid_output_bl_program_and_verify(
  5093. struct pt_core_data *cd, u16 data_len, u8 *data_buf)
  5094. {
  5095. int rc;
  5096. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5097. if (rc < 0) {
  5098. pt_debug(cd->dev, DL_ERROR,
  5099. "%s: fail get exclusive ex=%p own=%p\n",
  5100. __func__, cd->exclusive_dev, cd->dev);
  5101. return rc;
  5102. }
  5103. rc = pt_hid_output_bl_program_and_verify_(cd, data_len, data_buf);
  5104. if (release_exclusive(cd, cd->dev) < 0)
  5105. pt_debug(cd->dev, DL_ERROR,
  5106. "%s: fail to release exclusive\n", __func__);
  5107. return rc;
  5108. }
  5109. /*******************************************************************************
  5110. * FUNCTION: _pt_request_pip_bl_program_and_verify
  5111. *
  5112. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5113. * to request to have the BL program and verify a FW image
  5114. *
  5115. * RETURN:
  5116. * 0 = success
  5117. * !0 = failure
  5118. *
  5119. * PARAMETERS:
  5120. * *dev - pointer to device structure
  5121. * protect - boolean to determine to call the protected function
  5122. * data_len - length of data_buf
  5123. * *data_buf - firmware image to program
  5124. ******************************************************************************/
  5125. static int _pt_request_pip_bl_program_and_verify(
  5126. struct device *dev, int protect, u16 data_len, u8 *data_buf)
  5127. {
  5128. struct pt_core_data *cd = dev_get_drvdata(dev);
  5129. if (protect)
  5130. return pt_hid_output_bl_program_and_verify(cd, data_len,
  5131. data_buf);
  5132. return pt_hid_output_bl_program_and_verify_(cd, data_len,
  5133. data_buf);
  5134. }
  5135. /*******************************************************************************
  5136. * FUNCTION: pt_hid_output_bl_verify_app_integrity_
  5137. *
  5138. * SUMMARY: Sends the PIP "Get Bootloader Information" (0x31) command to
  5139. * perform a full verification of the application integrity by calculating the
  5140. * CRC of the image in flash and compare it to the expected CRC stored in the
  5141. * Metadata row.
  5142. *
  5143. * RETURN:
  5144. * 0 = success
  5145. * !0 = failure
  5146. *
  5147. * PARAMETERS:
  5148. * *cd - pointer to core data
  5149. * *result - pointer to store result
  5150. ******************************************************************************/
  5151. static int pt_hid_output_bl_verify_app_integrity_(
  5152. struct pt_core_data *cd, u8 *result)
  5153. {
  5154. int rc;
  5155. struct pt_hid_output hid_output = {
  5156. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY),
  5157. };
  5158. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5159. if (rc) {
  5160. *result = 0;
  5161. return rc;
  5162. }
  5163. *result = cd->response_buf[8];
  5164. return 0;
  5165. }
  5166. /*******************************************************************************
  5167. * FUNCTION: pt_hid_output_bl_verify_app_integrity
  5168. *
  5169. * SUMMARY: Protected call to pt_hid_output_bl_verify_app_integrity_ by
  5170. * exclusive access to the DUT.
  5171. *
  5172. * RETURN:
  5173. * 0 = success
  5174. * !0 = failure
  5175. *
  5176. * PARAMETERS:
  5177. * *cd - pointer to core data
  5178. * *result - pointer to store result
  5179. ******************************************************************************/
  5180. static int pt_hid_output_bl_verify_app_integrity(
  5181. struct pt_core_data *cd, u8 *result)
  5182. {
  5183. int rc;
  5184. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5185. if (rc < 0) {
  5186. pt_debug(cd->dev, DL_ERROR,
  5187. "%s: fail get exclusive ex=%p own=%p\n",
  5188. __func__, cd->exclusive_dev, cd->dev);
  5189. return rc;
  5190. }
  5191. rc = pt_hid_output_bl_verify_app_integrity_(cd, result);
  5192. if (release_exclusive(cd, cd->dev) < 0)
  5193. pt_debug(cd->dev, DL_ERROR,
  5194. "%s: fail to release exclusive\n", __func__);
  5195. return rc;
  5196. }
  5197. /*******************************************************************************
  5198. * FUNCTION: _pt_request_pip_bl_verify_app_integrity
  5199. *
  5200. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5201. * to request to have the BL verify the application integrity (PIP1.x only)
  5202. *
  5203. * RETURN:
  5204. * 0 = success
  5205. * !0 = failure
  5206. *
  5207. * PARAMETERS:
  5208. * *dev - pointer to device structure
  5209. * protect - boolean to determine to call the protected function
  5210. * *result - pointer to store result
  5211. ******************************************************************************/
  5212. static int _pt_request_pip_bl_verify_app_integrity(
  5213. struct device *dev, int protect, u8 *result)
  5214. {
  5215. struct pt_core_data *cd = dev_get_drvdata(dev);
  5216. if (protect)
  5217. return pt_hid_output_bl_verify_app_integrity(cd, result);
  5218. return pt_hid_output_bl_verify_app_integrity_(cd, result);
  5219. }
  5220. /*******************************************************************************
  5221. * FUNCTION: pt_hid_output_bl_launch_app_
  5222. *
  5223. * SUMMARY: Sends the PIP "Launch Application" (0x3B) command to launch the
  5224. * application from bootloader (PIP1.x only).
  5225. *
  5226. * RETURN:
  5227. * 0 = success
  5228. * !0 = failure
  5229. *
  5230. * PARAMETERS:
  5231. * *cd - pointer to core data
  5232. ******************************************************************************/
  5233. static int pt_hid_output_bl_launch_app_(struct pt_core_data *cd)
  5234. {
  5235. struct pt_hid_output hid_output = {
  5236. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_LAUNCH_APP),
  5237. .reset_expected = 1,
  5238. };
  5239. return pt_pip1_send_output_and_wait_(cd, &hid_output);
  5240. }
  5241. /*******************************************************************************
  5242. * FUNCTION: pt_hid_output_bl_launch_app
  5243. *
  5244. * SUMMARY: Protected call to pt_hid_output_bl_launch_app_ by exclusive access
  5245. * to the DUT.
  5246. *
  5247. * RETURN:
  5248. * 0 = success
  5249. * !0 = failure
  5250. *
  5251. * PARAMETERS:
  5252. * *cd - pointer to core data
  5253. ******************************************************************************/
  5254. static int pt_hid_output_bl_launch_app(struct pt_core_data *cd)
  5255. {
  5256. int rc;
  5257. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5258. if (rc < 0) {
  5259. pt_debug(cd->dev, DL_ERROR,
  5260. "%s: fail get exclusive ex=%p own=%p\n",
  5261. __func__, cd->exclusive_dev, cd->dev);
  5262. return rc;
  5263. }
  5264. rc = pt_hid_output_bl_launch_app_(cd);
  5265. if (release_exclusive(cd, cd->dev) < 0)
  5266. pt_debug(cd->dev, DL_ERROR,
  5267. "%s: fail to release exclusive\n", __func__);
  5268. return rc;
  5269. }
  5270. /*******************************************************************************
  5271. * FUNCTION: _pt_request_pip_launch_app
  5272. *
  5273. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5274. * to request to have the BL launch the application. (PIP1.x only)
  5275. *
  5276. * RETURN:
  5277. * 0 = success
  5278. * !0 = failure
  5279. *
  5280. * PARAMETERS:
  5281. * *dev - pointer to device structure
  5282. * protect - boolean to determine to call the protected function
  5283. ******************************************************************************/
  5284. static int _pt_request_pip_launch_app(struct device *dev,
  5285. int protect)
  5286. {
  5287. struct pt_core_data *cd = dev_get_drvdata(dev);
  5288. if (protect)
  5289. return pt_hid_output_bl_launch_app(cd);
  5290. return pt_hid_output_bl_launch_app_(cd);
  5291. }
  5292. /*******************************************************************************
  5293. * FUNCTION: pt_hid_output_bl_get_panel_id_
  5294. *
  5295. * SUMMARY: Sends the PIP "Get Panel ID" (0x3E) command to return the Panel ID
  5296. * value store in the System Information.
  5297. *
  5298. * RETURN:
  5299. * 0 = success
  5300. * !0 = failure
  5301. *
  5302. * PARAMETERS:
  5303. * *cd - pointer to core data
  5304. * *panel_id - pointer to where the panel ID will be stored
  5305. ******************************************************************************/
  5306. static int pt_hid_output_bl_get_panel_id_(
  5307. struct pt_core_data *cd, u8 *panel_id)
  5308. {
  5309. int rc;
  5310. struct pt_hid_output hid_output = {
  5311. CREATE_PIP1_BL_CMD(PIP1_BL_CMD_ID_GET_PANEL_ID),
  5312. };
  5313. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5314. if (rc == -EPROTO && cd->response_buf[5] == ERROR_COMMAND) {
  5315. pt_debug(cd->dev, DL_ERROR,
  5316. "%s: Get Panel ID command not supported\n",
  5317. __func__);
  5318. *panel_id = PANEL_ID_NOT_ENABLED;
  5319. return 0;
  5320. } else if (rc < 0) {
  5321. pt_debug(cd->dev, DL_ERROR,
  5322. "%s: Error on Get Panel ID command\n", __func__);
  5323. return rc;
  5324. }
  5325. *panel_id = cd->response_buf[8];
  5326. return 0;
  5327. }
  5328. /*******************************************************************************
  5329. * FUNCTION: pt_hid_output_bl_get_panel_id
  5330. *
  5331. * SUMMARY: Protected call to pt_hid_output_bl_get_panel_id_ by exclusive access
  5332. * to the DUT.
  5333. *
  5334. * RETURN:
  5335. * 0 = success
  5336. * !0 = failure
  5337. *
  5338. * PARAMETERS:
  5339. * *cd - pointer to core data
  5340. * *panel_id - pointer to where the panel ID will be stored
  5341. ******************************************************************************/
  5342. static int pt_hid_output_bl_get_panel_id(
  5343. struct pt_core_data *cd, u8 *panel_id)
  5344. {
  5345. int rc;
  5346. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5347. if (rc < 0) {
  5348. pt_debug(cd->dev, DL_ERROR,
  5349. "%s: fail get exclusive ex=%p own=%p\n",
  5350. __func__, cd->exclusive_dev, cd->dev);
  5351. return rc;
  5352. }
  5353. rc = pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5354. if (release_exclusive(cd, cd->dev) < 0)
  5355. pt_debug(cd->dev, DL_ERROR,
  5356. "%s: fail to release exclusive\n", __func__);
  5357. return rc;
  5358. }
  5359. /*******************************************************************************
  5360. * FUNCTION: _pt_request_pip_bl_get_panel_id
  5361. *
  5362. * SUMMARY: Function pointer included in core_nonhid_cmds to allow other modules
  5363. * to have the BL retrieve the panel ID
  5364. *
  5365. * RETURN:
  5366. * 0 = success
  5367. * !0 = failure
  5368. *
  5369. * PARAMETERS:
  5370. * *dev - pointer to device structure
  5371. * protect - flag to run in protected mode
  5372. * *panel_id - pointer to where the panel ID will be stored
  5373. ******************************************************************************/
  5374. static int _pt_request_pip_bl_get_panel_id(
  5375. struct device *dev, int protect, u8 *panel_id)
  5376. {
  5377. struct pt_core_data *cd = dev_get_drvdata(dev);
  5378. if (protect)
  5379. return pt_hid_output_bl_get_panel_id(cd, panel_id);
  5380. return pt_hid_output_bl_get_panel_id_(cd, panel_id);
  5381. }
  5382. /*******************************************************************************
  5383. * FUNCTION: pt_pip2_get_mode_sysmode_
  5384. *
  5385. * SUMMARY: Determine the current mode and system mode of the DUT by use of the
  5386. * PIP2 STATUS command.
  5387. *
  5388. * RETURN:
  5389. * 0 = success
  5390. * !0 = failure
  5391. *
  5392. * PARAMETERS:
  5393. * *cd - pointer to core data structure
  5394. * *mode - pointer to store the retrieved mode
  5395. * *sys_mode - pointer to store the FW system mode
  5396. ******************************************************************************/
  5397. static int pt_pip2_get_mode_sysmode_(struct pt_core_data *cd,
  5398. u8 *mode, u8 *sys_mode)
  5399. {
  5400. u16 actual_read_len;
  5401. u8 read_buf[12];
  5402. u8 status, boot;
  5403. int rc = 0;
  5404. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  5405. PIP2_CMD_ID_STATUS, NULL, 0, read_buf, &actual_read_len);
  5406. pt_debug(cd->dev, DL_INFO, "%s: PIP2 STATUS command rc = %d\n",
  5407. __func__, rc);
  5408. if (!rc) {
  5409. pt_pr_buf(cd->dev, DL_DEBUG, read_buf, actual_read_len,
  5410. "PIP2 STATUS");
  5411. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5412. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  5413. if (sys_mode) {
  5414. if (status == PIP2_RSP_ERR_NONE &&
  5415. boot == PIP2_STATUS_APP_EXEC)
  5416. *sys_mode = read_buf[PIP2_RESP_BODY_OFFSET + 1];
  5417. else
  5418. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5419. }
  5420. if (mode) {
  5421. if (status == PIP2_RSP_ERR_NONE &&
  5422. boot == PIP2_STATUS_BOOT_EXEC)
  5423. *mode = PT_MODE_BOOTLOADER;
  5424. else if (status == PIP2_RSP_ERR_NONE &&
  5425. boot == PIP2_STATUS_APP_EXEC)
  5426. *mode = PT_MODE_OPERATIONAL;
  5427. else
  5428. *mode = PT_MODE_UNKNOWN;
  5429. }
  5430. } else {
  5431. if (mode)
  5432. *mode = PT_MODE_UNKNOWN;
  5433. if (sys_mode)
  5434. *sys_mode = FW_SYS_MODE_UNDEFINED;
  5435. pt_debug(cd->dev, DL_WARN,
  5436. "%s: Mode and sys_mode could not be determined\n",
  5437. __func__);
  5438. }
  5439. return rc;
  5440. }
  5441. /*******************************************************************************
  5442. * FUNCTION: pt_pip2_get_mode_sysmode
  5443. *
  5444. * SUMMARY: Protected call to pt_pip2_get_mode_sysmode_ by exclusive
  5445. * access to the DUT.
  5446. *
  5447. * RETURN:
  5448. * 0 = success
  5449. * !0 = failure
  5450. *
  5451. * PARAMETERS:
  5452. * *cd - pointer to core data structure
  5453. * *mode - pointer to store the retrieved mode
  5454. * *sys_mode - pointer to store the FW system mode
  5455. ******************************************************************************/
  5456. static int pt_pip2_get_mode_sysmode(struct pt_core_data *cd,
  5457. u8 *mode, u8 *sys_mode)
  5458. {
  5459. int rc;
  5460. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5461. if (rc < 0) {
  5462. pt_debug(cd->dev, DL_ERROR,
  5463. "%s: fail get exclusive ex=%p own=%p\n",
  5464. __func__, cd->exclusive_dev, cd->dev);
  5465. return rc;
  5466. }
  5467. rc = pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5468. if (release_exclusive(cd, cd->dev) < 0)
  5469. pt_debug(cd->dev, DL_ERROR,
  5470. "%s: fail to release exclusive\n", __func__);
  5471. return rc;
  5472. }
  5473. /*******************************************************************************
  5474. * FUNCTION: _pt_request_pip2_get_mode_sysmode
  5475. *
  5476. * SUMMARY: Function pointer included in core_commands struct for external
  5477. * calls to the protected or unprotected call to
  5478. * pt_pip2_get_mode_sysmode
  5479. *
  5480. * RETURN:
  5481. * 0 = success
  5482. * !0 = failure
  5483. *
  5484. * PARAMETERS:
  5485. * *dev - pointer to device structure
  5486. * *mode - pointer to store the retrieved mode
  5487. * *sys_mode - pointer to store the FW system mode
  5488. ******************************************************************************/
  5489. static int _pt_request_pip2_get_mode_sysmode(struct device *dev,
  5490. int protect, u8 *mode, u8 *sys_mode)
  5491. {
  5492. struct pt_core_data *cd = dev_get_drvdata(dev);
  5493. if (protect)
  5494. return pt_pip2_get_mode_sysmode(cd, mode, sys_mode);
  5495. return pt_pip2_get_mode_sysmode_(cd, mode, sys_mode);
  5496. }
  5497. /*******************************************************************************
  5498. * FUNCTION: _pt_poll_for_fw_exit_boot_mode
  5499. *
  5500. * SUMMARY: Verify and or poll for the FW to exit BOOT mode. During the FW BOOT
  5501. * mode only the following PIP commands will be serviced, any other PIP
  5502. * command the FW will respond with an "Invalid PIP Command" response.
  5503. * - Get HID Descriptor (Register 0x0001, no command ID)
  5504. * - Reset (Register 0x0005, RESET HID request)
  5505. * - Ping (Register 0x0004, Command ID 0x00
  5506. * - Get System Information (Register 0x0004, Command ID 0x02)
  5507. * - PIP2 Status (Register 0x0101, Command ID 0x01)
  5508. * - PIP2 Version (Register 0x0101, Command ID 0x07)
  5509. * This function will loop on the results of the STATUS command until
  5510. * the FW reports it is out of BOOT mode.
  5511. *
  5512. * NOTE:
  5513. * - This function will update cd->fw_system_mode
  5514. * - The STATUS cmd only supports this functionality for PIP 1.11+
  5515. *
  5516. * RETURN:
  5517. * 0 = success
  5518. * !0 = failure
  5519. *
  5520. * PARAMETERS:
  5521. * *cd - pointer to core data
  5522. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5523. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5524. ******************************************************************************/
  5525. static int _pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5526. int *actual_wait)
  5527. {
  5528. int loop = 0;
  5529. u8 sys_mode = cd->fw_system_mode;
  5530. u8 pause = 10; /* in ms */
  5531. int rc = 0;
  5532. int max_loop = (timeout / pause) + 1; /* Add 1 due to int math */
  5533. if (cd->sysinfo.ready && !IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5534. /*
  5535. * For PIP <1.11, no support for polling wait so do a hard
  5536. * coded wait and assume the FW is out of BOOT. Added 1 to
  5537. * timeout to make it clear in kmsg if non polling was done.
  5538. */
  5539. *actual_wait = PT_FW_EXIT_BOOT_MODE_TIMEOUT + 1;
  5540. pt_debug(cd->dev, DL_ERROR,
  5541. "%s: PIP %d.%d no support for ext STATUS, sleep %d\n",
  5542. __func__,
  5543. cd->sysinfo.ttdata.pip_ver_major,
  5544. cd->sysinfo.ttdata.pip_ver_minor, *actual_wait);
  5545. msleep(*actual_wait);
  5546. sys_mode = FW_SYS_MODE_SCANNING;
  5547. }
  5548. if (sys_mode == FW_SYS_MODE_BOOT) {
  5549. while (!rc && loop <= max_loop &&
  5550. (sys_mode == FW_SYS_MODE_BOOT)) {
  5551. loop++;
  5552. usleep_range(9000, pause * 1000);
  5553. rc = pt_pip2_get_mode_sysmode_(cd, NULL, &sys_mode);
  5554. pt_debug(cd->dev, DL_DEBUG,
  5555. "%s: FW in BOOT mode-sleep %dms, sys_mode=%d\n",
  5556. __func__, loop * pause, sys_mode);
  5557. }
  5558. *actual_wait = (int)(loop * pause);
  5559. pt_debug(cd->dev, DL_WARN,
  5560. "%s: FW exited BOOT mode in %dms, sys_mode=%d\n",
  5561. __func__, *actual_wait, sys_mode);
  5562. if (rc)
  5563. sys_mode = FW_SYS_MODE_UNDEFINED;
  5564. else if (sys_mode == FW_SYS_MODE_BOOT ||
  5565. sys_mode == FW_SYS_MODE_UNDEFINED)
  5566. rc = -EBUSY;
  5567. }
  5568. mutex_lock(&cd->system_lock);
  5569. cd->fw_system_mode = sys_mode;
  5570. mutex_unlock(&cd->system_lock);
  5571. return rc;
  5572. }
  5573. /*******************************************************************************
  5574. * FUNCTION: pt_poll_for_fw_exit_boot_mode
  5575. *
  5576. * SUMMARY: Protected call to _pt_poll_for_fw_exit_boot_mode by exclusive
  5577. * access to the DUT.
  5578. *
  5579. * RETURN:
  5580. * 0 = success
  5581. * !0 = failure
  5582. *
  5583. * PARAMETERS:
  5584. * *cd - pointer to core data
  5585. * timeout - max time (ms) to wait for FW to exit BOOT mode
  5586. * actual_wait - pointer to actual time waited for FW to exit BOOT mode
  5587. ******************************************************************************/
  5588. static int pt_poll_for_fw_exit_boot_mode(struct pt_core_data *cd, int timeout,
  5589. int *actual_wait)
  5590. {
  5591. int rc;
  5592. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5593. if (rc < 0) {
  5594. pt_debug(cd->dev, DL_ERROR,
  5595. "%s: fail get exclusive ex=%p own=%p\n",
  5596. __func__, cd->exclusive_dev, cd->dev);
  5597. return rc;
  5598. }
  5599. rc = _pt_poll_for_fw_exit_boot_mode(cd, timeout, actual_wait);
  5600. if (release_exclusive(cd, cd->dev) < 0)
  5601. pt_debug(cd->dev, DL_ERROR,
  5602. "%s: fail to release exclusive\n", __func__);
  5603. return rc;
  5604. }
  5605. /*******************************************************************************
  5606. * FUNCTION: _pt_get_fw_sys_mode
  5607. *
  5608. * SUMMARY: Determine the FW system mode. For PIP 1.11+ the
  5609. * PIP2 STATUS command is used to directly query the FW system mode. For older
  5610. * PIP versions, there is no direct PIP commamnd that will directly provide this
  5611. * information but any PIP command above 0x1F requires scanning to be disabled
  5612. * before it will be operational. If scanning was not disabled before sending
  5613. * these PIP commands the FW will respond with a 6 byte error response. So to
  5614. * safely determine the scanning state, a PIP message that will not affect the
  5615. * operation of the FW was chosen:
  5616. * "Verify Data Block CRC (ID 0x20)" is sent and if a 6 byte error code is
  5617. * received scanning is active.
  5618. *
  5619. * RETURN:
  5620. * 0 = success
  5621. * !0 = failure
  5622. *
  5623. * PARAMETERS:
  5624. * *cd - pointer to core data
  5625. * *sys_mode - pointer to FW System mode
  5626. * *mode - pointer to mode (BL/FW)
  5627. ******************************************************************************/
  5628. static int _pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5629. {
  5630. int write_length = 1;
  5631. int report_length;
  5632. int rc = 0;
  5633. u8 tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5634. u8 tmp_mode = PT_MODE_UNKNOWN;
  5635. u8 param[1] = { PT_TCH_PARM_EBID };
  5636. struct pt_hid_output hid_output = {
  5637. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC),
  5638. .write_length = write_length,
  5639. .write_buf = param,
  5640. .novalidate = true,
  5641. };
  5642. /* AFter PIP1.11 the preferred method is using STATUS cmd */
  5643. if (IS_PIP_VER_GE(&cd->sysinfo, 1, 11)) {
  5644. rc = pt_pip2_get_mode_sysmode_(cd, &tmp_mode, &tmp_sys_mode);
  5645. pt_debug(cd->dev, DL_DEBUG, "%s: tmp_sys_mode=%d tmp_mode=%d\n",
  5646. __func__, tmp_sys_mode, tmp_mode);
  5647. if (!rc) {
  5648. if (tmp_mode != PT_MODE_OPERATIONAL)
  5649. tmp_sys_mode = FW_SYS_MODE_UNDEFINED;
  5650. }
  5651. goto exit;
  5652. }
  5653. /* Older systems use PIP1 CONFIG_BLOCK_CRC to best determine sys_mode */
  5654. if (cd->mode != PT_MODE_OPERATIONAL) {
  5655. tmp_mode = cd->mode;
  5656. goto exit;
  5657. }
  5658. rc = pt_pip1_send_output_and_wait_(cd, &hid_output);
  5659. if (rc)
  5660. goto exit;
  5661. report_length = (cd->response_buf[1] << 8) | (cd->response_buf[0]);
  5662. if ((report_length == 0x06) &&
  5663. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) == 0x00) &&
  5664. (cd->response_buf[5] == PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5665. tmp_mode = PIP2_STATUS_APP_EXEC;
  5666. tmp_sys_mode = FW_SYS_MODE_SCANNING;
  5667. } else if ((report_length == 0x0A) &&
  5668. ((cd->response_buf[4] & PIP1_RESP_COMMAND_ID_MASK) ==
  5669. PIP1_CMD_ID_VERIFY_CONFIG_BLOCK_CRC)) {
  5670. tmp_mode = PIP2_STATUS_APP_EXEC;
  5671. tmp_sys_mode = FW_SYS_MODE_TEST;
  5672. }
  5673. exit:
  5674. if (mode)
  5675. *mode = tmp_mode;
  5676. if (sys_mode)
  5677. *sys_mode = tmp_sys_mode;
  5678. pt_debug(cd->dev, DL_INFO, "%s: Return Mode=%d sys_mode=%d\n",
  5679. __func__, tmp_mode, tmp_sys_mode);
  5680. return rc;
  5681. }
  5682. /*******************************************************************************
  5683. * FUNCTION: pt_get_fw_sys_mode
  5684. *
  5685. * SUMMARY: Protected call to _pt_get_fw_sys_mode() to determine if FW scanning
  5686. * is active or not.
  5687. *
  5688. * RETURN:
  5689. * 0 = success
  5690. * !0 = failure
  5691. *
  5692. * PARAMETERS:
  5693. * *cd - pointer to core data
  5694. * *sys_mode - pointer to fw system mode
  5695. * *mode - pointer to mode
  5696. ******************************************************************************/
  5697. static int pt_get_fw_sys_mode(struct pt_core_data *cd, u8 *sys_mode, u8 *mode)
  5698. {
  5699. int rc;
  5700. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5701. if (rc < 0) {
  5702. pt_debug(cd->dev, DL_ERROR,
  5703. "%s: fail get exclusive ex=%p own=%p\n",
  5704. __func__, cd->exclusive_dev, cd->dev);
  5705. return rc;
  5706. }
  5707. rc = _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5708. if (release_exclusive(cd, cd->dev) < 0)
  5709. pt_debug(cd->dev, DL_ERROR,
  5710. "%s: fail to release exclusive\n", __func__);
  5711. return rc;
  5712. }
  5713. /*******************************************************************************
  5714. * FUNCTION: _pt_request_get_fw_sys_mode
  5715. *
  5716. * SUMMARY: Function pointer included in core_cmds to allow other modules
  5717. * to request to get scan state
  5718. *
  5719. * RETURN:
  5720. * 0 = success
  5721. * !0 = failure
  5722. *
  5723. * PARAMETERS:
  5724. * *dev - pointer to device structure
  5725. * protect - flag to call protected or non-protected
  5726. * *sys_mode - pointer to FW system mode
  5727. * *mode - pointer to mode
  5728. ******************************************************************************/
  5729. static int _pt_request_get_fw_sys_mode(struct device *dev, int protect,
  5730. u8 *sys_mode, u8 *mode)
  5731. {
  5732. struct pt_core_data *cd = dev_get_drvdata(dev);
  5733. if (protect)
  5734. return pt_get_fw_sys_mode(cd, sys_mode, mode);
  5735. return _pt_get_fw_sys_mode(cd, sys_mode, mode);
  5736. }
  5737. /* Default hid descriptor to provide basic register map */
  5738. const struct pt_hid_desc hid_desc_default = {
  5739. 230, /* hid_desc_len */
  5740. HID_APP_REPORT_ID, /* packet_id */
  5741. 0x00, /* reserved_byte */
  5742. 0x0100, /* bcd_version */
  5743. 0x00EC, /* report_desc_len */
  5744. 0x0002, /* report_desc_register */
  5745. 0x0003, /* input_register */
  5746. 0x00FE, /* max_input_len */
  5747. 0x0004, /* output_register */
  5748. 0x00FE, /* max_output_len */
  5749. 0x0005, /* command_register */
  5750. 0x0006, /* data_register */
  5751. 0x04B4, /* vendor_id */
  5752. 0xC101, /* product_id */
  5753. 0x0100, /* version_id */
  5754. {0x00, 0x00, 0x00, 0x00} /* reserved[4] */
  5755. };
  5756. /*******************************************************************************
  5757. * FUNCTION: pt_init_hid_descriptor
  5758. *
  5759. * SUMMARY: Setup default values for HID descriptor structure
  5760. *
  5761. *
  5762. * PARAMETERS:
  5763. * *desc - pointer to the HID descriptor data read back from DUT
  5764. ******************************************************************************/
  5765. static inline void pt_init_hid_descriptor(struct pt_hid_desc *desc)
  5766. {
  5767. memcpy(desc, &hid_desc_default, sizeof(hid_desc_default));
  5768. }
  5769. /*******************************************************************************
  5770. * FUNCTION: pt_get_hid_descriptor_
  5771. *
  5772. * SUMMARY: Send the get HID descriptor command to the DUT and load the response
  5773. * into the HID descriptor structure
  5774. *
  5775. * RETURN:
  5776. * 0 = success
  5777. * !0 = failure
  5778. *
  5779. * PARAMETERS:
  5780. * *cd - pointer to core data
  5781. * *desc - pointer to the HID descriptor data read back from DUT
  5782. ******************************************************************************/
  5783. static int pt_get_hid_descriptor_(struct pt_core_data *cd,
  5784. struct pt_hid_desc *desc)
  5785. {
  5786. struct device *dev = cd->dev;
  5787. int rc = 0;
  5788. int t;
  5789. u8 cmd[2];
  5790. /*
  5791. * During startup the HID descriptor is required for all future
  5792. * processing. If IRQ is already asserted due to an early touch report
  5793. * the report must be cleared before sending command.
  5794. */
  5795. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5796. /* Read HID descriptor length and version */
  5797. mutex_lock(&cd->system_lock);
  5798. cd->hid_cmd_state = 1;
  5799. mutex_unlock(&cd->system_lock);
  5800. /* Set HID descriptor register */
  5801. memcpy(cmd, &cd->hid_core.hid_desc_register,
  5802. sizeof(cd->hid_core.hid_desc_register));
  5803. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer [%zu]",
  5804. __func__, sizeof(cmd));
  5805. pt_pr_buf(cd->dev, DL_DEBUG, cmd, sizeof(cmd), ">>> Get HID Desc");
  5806. rc = pt_adap_write_read_specific(cd, 2, cmd, NULL);
  5807. if (rc) {
  5808. pt_debug(dev, DL_ERROR,
  5809. "%s: failed to get HID descriptor, rc=%d\n",
  5810. __func__, rc);
  5811. goto error;
  5812. }
  5813. t = wait_event_timeout(cd->wait_q, (cd->hid_cmd_state == 0),
  5814. msecs_to_jiffies(PT_GET_HID_DESCRIPTOR_TIMEOUT));
  5815. if (IS_TMO(t)) {
  5816. #ifdef TTDL_DIAGNOSTICS
  5817. cd->bus_transmit_error_count++;
  5818. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  5819. #endif /* TTDL_DIAGNOSTICS */
  5820. pt_debug(cd->dev, DL_ERROR,
  5821. "%s: HID get descriptor timed out\n", __func__);
  5822. rc = -ETIME;
  5823. goto error;
  5824. } else {
  5825. cd->hw_detected = true;
  5826. }
  5827. /* Load the HID descriptor including all registers */
  5828. memcpy((u8 *)desc, cd->response_buf, sizeof(struct pt_hid_desc));
  5829. /* Check HID descriptor length and version */
  5830. pt_debug(dev, DL_INFO, "%s: HID len:%X HID ver:%X\n", __func__,
  5831. le16_to_cpu(desc->hid_desc_len),
  5832. le16_to_cpu(desc->bcd_version));
  5833. if (le16_to_cpu(desc->hid_desc_len) != sizeof(*desc) ||
  5834. le16_to_cpu(desc->bcd_version) != HID_VERSION) {
  5835. pt_debug(dev, DL_ERROR, "%s: Unsupported HID version\n",
  5836. __func__);
  5837. return -ENODEV;
  5838. }
  5839. goto exit;
  5840. error:
  5841. mutex_lock(&cd->system_lock);
  5842. cd->hid_cmd_state = 0;
  5843. mutex_unlock(&cd->system_lock);
  5844. exit:
  5845. return rc;
  5846. }
  5847. /*******************************************************************************
  5848. * FUNCTION: pt_get_hid_descriptor
  5849. *
  5850. * SUMMARY: Protected call to pt_get_hid_descriptor_()
  5851. *
  5852. * RETURN:
  5853. * 0 = success
  5854. * !0 = failure
  5855. *
  5856. * PARAMETERS:
  5857. * *cd - pointer to core data
  5858. * *desc - pointer to the HID descriptor data read back from DUT
  5859. ******************************************************************************/
  5860. static int pt_get_hid_descriptor(struct pt_core_data *cd,
  5861. struct pt_hid_desc *desc)
  5862. {
  5863. int rc;
  5864. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5865. if (rc < 0) {
  5866. pt_debug(cd->dev, DL_ERROR,
  5867. "%s: fail get exclusive ex=%p own=%p\n",
  5868. __func__, cd->exclusive_dev, cd->dev);
  5869. return rc;
  5870. }
  5871. rc = pt_get_hid_descriptor_(cd, desc);
  5872. if (release_exclusive(cd, cd->dev) < 0)
  5873. pt_debug(cd->dev, DL_ERROR,
  5874. "%s: fail to release exclusive\n", __func__);
  5875. return rc;
  5876. }
  5877. /*******************************************************************************
  5878. * FUNCTION: pt_pip2_get_version_
  5879. *
  5880. * SUMMARY: Sends a PIP2 VERSION command to the DUT and stores the data in
  5881. * cd-ttdata
  5882. *
  5883. * RETURN:
  5884. * 0 = success
  5885. * !0 = failure
  5886. *
  5887. * PARAMETERS:
  5888. * *cd - pointer to core data
  5889. ******************************************************************************/
  5890. static int pt_pip2_get_version_(struct pt_core_data *cd)
  5891. {
  5892. int rc = 0;
  5893. int status;
  5894. u8 read_buf[64];
  5895. u16 actual_read_len;
  5896. rc = _pt_request_pip2_send_cmd(cd->dev,
  5897. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  5898. NULL, 0, read_buf, &actual_read_len);
  5899. if (rc) {
  5900. pt_debug(cd->dev, DL_ERROR,
  5901. "%s: Error Sending PIP2 VERSION Cmd rc=%d\n",
  5902. __func__, rc);
  5903. return rc;
  5904. }
  5905. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  5906. if (status == 0) {
  5907. /* Parse the PIP2 VERSION response into ttdata */
  5908. pt_pip2_ver_load_ttdata(cd, actual_read_len);
  5909. } else {
  5910. pt_debug(cd->dev, DL_ERROR,
  5911. "%s: Error in PIP2 VERSION Cmd status=%d\n",
  5912. __func__, status);
  5913. return status;
  5914. }
  5915. return rc;
  5916. }
  5917. /*******************************************************************************
  5918. * FUNCTION: pt_pip2_get_version
  5919. *
  5920. * SUMMARY: Protected call to pt_pip2_get_version_ by exclusive
  5921. * access to the DUT.
  5922. *
  5923. * RETURN:
  5924. * 0 = success
  5925. * !0 = failure
  5926. *
  5927. * PARAMETERS:
  5928. * *cd - pointer to core data structure
  5929. ******************************************************************************/
  5930. static int pt_pip2_get_version(struct pt_core_data *cd)
  5931. {
  5932. int rc;
  5933. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  5934. if (rc < 0) {
  5935. pt_debug(cd->dev, DL_ERROR,
  5936. "%s: fail get exclusive ex=%p own=%p\n",
  5937. __func__, cd->exclusive_dev, cd->dev);
  5938. return rc;
  5939. }
  5940. rc = pt_pip2_get_version_(cd);
  5941. if (release_exclusive(cd, cd->dev) < 0)
  5942. pt_debug(cd->dev, DL_ERROR,
  5943. "%s: fail to release exclusive\n", __func__);
  5944. return rc;
  5945. }
  5946. /*******************************************************************************
  5947. * FUNCTION: _pt_request_active_pip_protocol
  5948. *
  5949. * SUMMARY: Get active PIP protocol version using the PIP2 version command.
  5950. * Function will return PIP version of BL or application based on
  5951. * when it's called.
  5952. *
  5953. * RETURN:
  5954. * 0 = success
  5955. * !0 = failure
  5956. *
  5957. * PARAMETERS:
  5958. * *dev - pointer to device structure
  5959. * protect - flag to run in protected mode
  5960. * *pip_version_major - pointer to store PIP major version
  5961. * *pip_version_minor - pointer to store PIP minor version
  5962. ******************************************************************************/
  5963. int _pt_request_active_pip_protocol(struct device *dev, int protect,
  5964. u8 *pip_version_major, u8 *pip_version_minor)
  5965. {
  5966. struct pt_core_data *cd = dev_get_drvdata(dev);
  5967. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  5968. int rc = 0;
  5969. struct pt_hid_output sys_info = {
  5970. CREATE_PIP1_FW_CMD(PIP1_CMD_ID_GET_SYSINFO),
  5971. .timeout_ms = PT_PIP1_CMD_GET_SYSINFO_TIMEOUT,
  5972. };
  5973. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  5974. /* Skip PIP2 command if DUT generation is confirmed */
  5975. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  5976. goto skip_pip2_command;
  5977. rc = pt_pip2_get_version_(cd);
  5978. if (!rc) {
  5979. *pip_version_major = ttdata->pip_ver_major;
  5980. *pip_version_minor = ttdata->pip_ver_minor;
  5981. pt_debug(dev, DL_INFO,
  5982. "%s: pip_version = %d.%d\n", __func__,
  5983. *pip_version_major, *pip_version_minor);
  5984. } else {
  5985. /*
  5986. * Legacy products do not support the pip2 protocol to get
  5987. * pip version. However, they do support the "get sysinfo"
  5988. * command to get pip version from FW, but the bootloader
  5989. * does not support it. This function will try "get sysinfo"
  5990. * command if the pip2 command failed but this cmd could also
  5991. * fail if DUT is stuck in bootloader mode.
  5992. */
  5993. pt_debug(dev, DL_INFO,
  5994. "%s: PIP2 no response rc = %d, try legacy cmd\n",
  5995. __func__, rc);
  5996. skip_pip2_command:
  5997. rc = pt_pip1_send_output_and_wait_(cd, &sys_info);
  5998. if (!rc) {
  5999. *pip_version_minor =
  6000. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET + 1];
  6001. *pip_version_major =
  6002. cd->response_buf[PIP1_SYSINFO_TTDATA_OFFSET];
  6003. pt_debug(dev, DL_INFO,
  6004. "%s: pip_version = %d.%d\n", __func__,
  6005. *pip_version_major, *pip_version_minor);
  6006. } else {
  6007. *pip_version_major = 0;
  6008. *pip_version_minor = 0;
  6009. pt_debug(dev, DL_ERROR,
  6010. "%s: pip_version Not Detected\n", __func__);
  6011. }
  6012. }
  6013. return rc;
  6014. }
  6015. EXPORT_SYMBOL_GPL(_pt_request_active_pip_protocol);
  6016. /*******************************************************************************
  6017. * FUNCTION: _pt_detect_dut_generation
  6018. *
  6019. * SUMMARY: Determine the generation of device that we are communicating with:
  6020. * DUT_PIP1_ONLY (Gen5 or Gen6)
  6021. * DUT_PIP2_CAPABLE (TC33xx or TT7xxx)
  6022. * The HID_DESC command is supported in Gen5/6 BL and FW as well as
  6023. * TT/TC FW. The packet ID in the descriptor, however, is unique when
  6024. * coming form the BL or the FW:
  6025. * Packet_ID in BL = HID_BL_REPORT_ID (0xFF)
  6026. * Packet_ID in FW = HID_APP_REPORT_ID (0xF7)
  6027. * This function will return a modified status if it detects the DUT
  6028. * is in the BL. In the case of a Gen5/6 BL, which also sends out a FW
  6029. * reset sentinel, the status is "corrected" from a FW to BL sentinel.
  6030. *
  6031. * RETURN:
  6032. * 0 = success
  6033. * !0 = failure
  6034. *
  6035. * PARAMETERS:
  6036. * *dev - pointer to device structure
  6037. * *status - pointer to status bitmask
  6038. * *dut_gen - pointer to store the dut_generation
  6039. * *mode - pointer to store the PT_MODE
  6040. ******************************************************************************/
  6041. static int _pt_detect_dut_generation(struct device *dev,
  6042. u32 *status, u8 *dut_gen, enum pt_mode *mode)
  6043. {
  6044. int rc = 0;
  6045. u8 dut_gen_tmp = DUT_UNKNOWN;
  6046. u8 mode_tmp = PT_MODE_UNKNOWN;
  6047. u8 attempt = 1;
  6048. u32 status_tmp = STARTUP_STATUS_START;
  6049. struct pt_core_data *cd = dev_get_drvdata(dev);
  6050. struct pt_hid_desc hid_desc;
  6051. memset(&hid_desc, 0, sizeof(hid_desc));
  6052. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6053. while (rc && attempt < 3) {
  6054. attempt++;
  6055. usleep_range(2000, 5000);
  6056. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6057. }
  6058. if (!rc && hid_desc.packet_id == HID_BL_REPORT_ID) {
  6059. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 BL */
  6060. mode_tmp = PT_MODE_BOOTLOADER;
  6061. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6062. } else if (!rc && hid_desc.packet_id == HID_APP_REPORT_ID) {
  6063. rc = pt_pip2_get_version_(cd);
  6064. if (!rc)
  6065. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC FW */
  6066. else
  6067. dut_gen_tmp = DUT_PIP1_ONLY; /* Gen5/6 FW */
  6068. mode_tmp = PT_MODE_OPERATIONAL;
  6069. status_tmp = STARTUP_STATUS_FW_RESET_SENTINEL;
  6070. rc = 0; /* To return success instead of error code */
  6071. } else if (rc) {
  6072. rc = pt_pip2_get_version_(cd);
  6073. if (!rc) {
  6074. dut_gen_tmp = DUT_PIP2_CAPABLE; /* TT/TC BL */
  6075. mode_tmp = PT_MODE_BOOTLOADER;
  6076. status_tmp = STARTUP_STATUS_BL_RESET_SENTINEL;
  6077. }
  6078. }
  6079. mutex_lock(&cd->system_lock);
  6080. if (dut_gen)
  6081. *dut_gen = dut_gen_tmp;
  6082. if (mode)
  6083. *mode = mode_tmp;
  6084. if (status)
  6085. *status = status_tmp;
  6086. mutex_unlock(&cd->system_lock);
  6087. #ifdef TTDL_DIAGNOSTICS
  6088. pt_debug(cd->dev, DL_INFO, "%s: Generation=%d Mode=%d\n",
  6089. __func__, dut_gen_tmp, mode_tmp);
  6090. #endif /* TTDL_DIAGNOSTICS */
  6091. return rc;
  6092. }
  6093. /*******************************************************************************
  6094. * FUNCTION: _pt_request_dut_generation
  6095. *
  6096. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6097. * to get current dut generation.
  6098. *
  6099. * NOTE: This function WILL NOT try to determine dut generation.
  6100. *
  6101. * RETURN:
  6102. * The current dut generation.
  6103. *
  6104. * PARAMETERS:
  6105. * *dev - pointer to device structure
  6106. ******************************************************************************/
  6107. static int _pt_request_dut_generation(struct device *dev)
  6108. {
  6109. struct pt_core_data *cd = dev_get_drvdata(dev);
  6110. return cd->active_dut_generation;
  6111. }
  6112. #define HW_VERSION_LEN_MAX 13
  6113. /*******************************************************************************
  6114. * FUNCTION: _legacy_generate_hw_version
  6115. *
  6116. * SUMMARY: Format chip information from struct ttdata (maintained by PIP1
  6117. * SYSINFO command) or struct bl_info (maintained by PIP1 BL INFORMATION
  6118. * command) to the hw_version.
  6119. *
  6120. * RETURN:
  6121. * 0 = success
  6122. * !0 = failure
  6123. *
  6124. * PARAMETERS:
  6125. * *cd - pointer to core data
  6126. * *hw_version - pointer to store the hardware version
  6127. ******************************************************************************/
  6128. static int _legacy_generate_hw_version(struct pt_core_data *cd,
  6129. char *hw_version)
  6130. {
  6131. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6132. if (cd->sysinfo.ready) {
  6133. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6134. ttdata->jtag_id_h, cd->pid_for_loader);
  6135. return 0;
  6136. } else if (cd->bl_info.ready) {
  6137. scnprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.FFFF.%02X",
  6138. cd->bl_info.chip_id, cd->pid_for_loader);
  6139. return 0;
  6140. } else {
  6141. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6142. pt_debug(cd->dev, DL_ERROR,
  6143. "%s: SYSINFO and BL_INFO are not ready\n", __func__);
  6144. return -ENODATA;
  6145. }
  6146. }
  6147. /*******************************************************************************
  6148. * FUNCTION: _pip2_generate_hw_version
  6149. *
  6150. * SUMMARY: Format chip information from struct ttdata (maintained by PIP2
  6151. * VERSION command) to the hw_version.
  6152. *
  6153. * RETURN:
  6154. * 0 = success
  6155. * !0 = failure
  6156. *
  6157. * PARAMETERS:
  6158. * *cd - pointer to core data
  6159. * *hw_version - pointer to store the hardware version
  6160. ******************************************************************************/
  6161. static int _pip2_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6162. {
  6163. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6164. if (cd->app_pip_ver_ready | cd->bl_pip_ver_ready) {
  6165. snprintf(hw_version, HW_VERSION_LEN_MAX, "%04X.%04X.%02X",
  6166. ttdata->chip_id, ttdata->chip_rev, cd->pid_for_loader);
  6167. return 0;
  6168. } else {
  6169. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6170. pt_debug(cd->dev, DL_ERROR,
  6171. "%s: PIP Version are not ready\n", __func__);
  6172. return -ENODATA;
  6173. }
  6174. }
  6175. /*******************************************************************************
  6176. * FUNCTION: pt_generate_hw_version
  6177. *
  6178. * SUMMARY: Wraaper function for both legacy and TT/TC products generate the
  6179. * hw_version from static data.
  6180. *
  6181. * RETURN:
  6182. * 0 = success
  6183. * !0 = failure
  6184. *
  6185. * PARAMETERS:
  6186. * *cd - pointer to core data
  6187. * *hw_version - pointer to store the hardware version
  6188. ******************************************************************************/
  6189. static int pt_generate_hw_version(struct pt_core_data *cd, char *hw_version)
  6190. {
  6191. int rc = 0;
  6192. if (!hw_version)
  6193. return -ENOMEM;
  6194. if (cd->active_dut_generation == DUT_PIP1_ONLY)
  6195. rc = _legacy_generate_hw_version(cd, hw_version);
  6196. else if (cd->active_dut_generation == DUT_PIP2_CAPABLE)
  6197. rc = _pip2_generate_hw_version(cd, hw_version);
  6198. else {
  6199. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6200. rc = -ENODATA;
  6201. }
  6202. return rc;
  6203. }
  6204. /*******************************************************************************
  6205. * SUMMARY: Attempt to retrieve the HW version of the connected DUT
  6206. *
  6207. * NOTE: The calling function must ensure to free *hw_version
  6208. *
  6209. * RETURN:
  6210. * 0 = success
  6211. * !0 = Failure
  6212. *
  6213. * PARAMETERS:
  6214. * *dev - pointer to device structure
  6215. * *hw_version - pointer to where the hw_version string will be stored
  6216. ******************************************************************************/
  6217. static int _pt_request_hw_version(struct device *dev, char *hw_version)
  6218. {
  6219. int rc = 0;
  6220. u16 actual_read_len;
  6221. u16 pip_ver;
  6222. u8 rd_buf[256];
  6223. u8 status;
  6224. u8 index = PIP2_RESP_STATUS_OFFSET;
  6225. u8 return_data[8];
  6226. u8 panel_id;
  6227. struct pt_core_data *cd = dev_get_drvdata(dev);
  6228. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  6229. if (!hw_version)
  6230. return -ENOMEM;
  6231. if (!cd->hw_detected) {
  6232. /* No HW detected */
  6233. rc = -ENODEV;
  6234. pt_debug(dev, DL_ERROR, "%s: no hardware is detected!\n",
  6235. __func__);
  6236. goto exit_error;
  6237. }
  6238. /* For Parade TC or TT parts */
  6239. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  6240. rc = _pt_request_pip2_send_cmd(dev,
  6241. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_VERSION,
  6242. NULL, 0, rd_buf, &actual_read_len);
  6243. if (rc) {
  6244. pt_debug(dev, DL_ERROR,
  6245. "%s: Failed to send PIP2 VERSION cmd\n",
  6246. __func__);
  6247. goto exit_error;
  6248. }
  6249. status = rd_buf[index];
  6250. if (status == 0) {
  6251. pip_ver = 256 * rd_buf[index + 2] + rd_buf[index + 1];
  6252. /*
  6253. * BL PIP 2.02 and greater the version fields are
  6254. * swapped
  6255. */
  6256. if (pip_ver >= 0x0202) {
  6257. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6258. "%02X%02X.%02X%02X.FF",
  6259. rd_buf[index + 10], rd_buf[index + 9],
  6260. rd_buf[index + 8], rd_buf[index + 7]);
  6261. } else {
  6262. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6263. "%02X%02X.%02X%02X.FF",
  6264. rd_buf[index + 8], rd_buf[index + 7],
  6265. rd_buf[index + 10], rd_buf[index + 9]);
  6266. }
  6267. return STATUS_SUCCESS;
  6268. } else {
  6269. rc = status;
  6270. pt_debug(dev, DL_WARN,
  6271. "%s: PIP2 VERSION cmd response error\n",
  6272. __func__);
  6273. }
  6274. } else if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  6275. /*
  6276. * For Parade/Cypress legacy parts the RevID and FamilyID are
  6277. * hard coded to FFFF
  6278. */
  6279. if (cd->mode == PT_MODE_OPERATIONAL) {
  6280. rc = pt_hid_output_get_sysinfo(cd);
  6281. if (!rc) {
  6282. panel_id =
  6283. cd->sysinfo.sensing_conf_data.panel_id;
  6284. } else {
  6285. panel_id = PANEL_ID_NOT_ENABLED;
  6286. }
  6287. /* In FW - simply retrieve from ttdata struct */
  6288. snprintf(hw_version, HW_VERSION_LEN_MAX,
  6289. "%04X.FFFF.%02X",
  6290. ttdata->jtag_id_h,
  6291. panel_id);
  6292. return STATUS_SUCCESS;
  6293. } else {
  6294. /*
  6295. * Return the stored value if PT_PANEL_ID_BY_BL
  6296. * is not supported while other feature is.
  6297. */
  6298. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  6299. rc = pt_hid_output_bl_get_information(
  6300. cd, return_data);
  6301. if (!rc) {
  6302. rc = pt_hid_output_bl_get_panel_id(
  6303. cd, &panel_id);
  6304. }
  6305. } else
  6306. panel_id = cd->pid_for_loader;
  6307. if (!rc) {
  6308. snprintf(hw_version,
  6309. HW_VERSION_LEN_MAX,
  6310. "%02X%02X.FFFF.%02X",
  6311. return_data[3], return_data[2],
  6312. panel_id);
  6313. return STATUS_SUCCESS;
  6314. }
  6315. }
  6316. } else {
  6317. /* Unknown generation */
  6318. rc = -ENODEV;
  6319. pt_debug(dev, DL_ERROR, "%s: generation is unknown!\n",
  6320. __func__);
  6321. }
  6322. exit_error:
  6323. snprintf(hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  6324. return rc;
  6325. }
  6326. /*******************************************************************************
  6327. * FUNCTION: pt_start_wd_timer
  6328. *
  6329. * SUMMARY: Starts the TTDL watchdog timer if the timer interval is > 0
  6330. *
  6331. * RETURN: void
  6332. *
  6333. * PARAMETERS:
  6334. * *cd - pointer to core data
  6335. ******************************************************************************/
  6336. static void pt_start_wd_timer(struct pt_core_data *cd)
  6337. {
  6338. if (cd->watchdog_interval < 100) {
  6339. pt_debug(cd->dev, DL_ERROR,
  6340. "%s: WARNING: Invalid watchdog interval: %d\n",
  6341. __func__, cd->watchdog_interval);
  6342. return;
  6343. }
  6344. if (cd->watchdog_force_stop) {
  6345. pt_debug(cd->dev, DL_INFO,
  6346. "%s: TTDL WD Forced stop\n", __func__);
  6347. return;
  6348. }
  6349. mod_timer(&cd->watchdog_timer, jiffies +
  6350. msecs_to_jiffies(cd->watchdog_interval));
  6351. cd->watchdog_enabled = 1;
  6352. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Started\n", __func__);
  6353. }
  6354. /*******************************************************************************
  6355. * FUNCTION: pt_stop_wd_timer
  6356. *
  6357. * SUMMARY: Stops the TTDL watchdog timer if the timer interval is > 0
  6358. *
  6359. * RETURN: void
  6360. *
  6361. * PARAMETERS:
  6362. * *cd - pointer to core data
  6363. ******************************************************************************/
  6364. static void pt_stop_wd_timer(struct pt_core_data *cd)
  6365. {
  6366. if (!cd->watchdog_interval)
  6367. return;
  6368. /*
  6369. * Ensure we wait until the watchdog timer
  6370. * running on a different CPU finishes
  6371. */
  6372. del_timer_sync(&cd->watchdog_timer);
  6373. cancel_work_sync(&cd->watchdog_work);
  6374. del_timer_sync(&cd->watchdog_timer);
  6375. cd->watchdog_enabled = 0;
  6376. pt_debug(cd->dev, DL_INFO, "%s: TTDL WD Stopped\n", __func__);
  6377. }
  6378. /*******************************************************************************
  6379. * FUNCTION: pt_hw_soft_reset
  6380. *
  6381. * SUMMARY: Sends a PIP reset command to the DUT. Disable/re-enable the
  6382. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6383. * schedule an enum if it fires when the DUT is being reset.
  6384. * This can cause a double reset.
  6385. *
  6386. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6387. * the WD active could cause this function to fail!
  6388. *
  6389. * RETURN:
  6390. * 0 = success
  6391. * !0 = failure
  6392. *
  6393. * PARAMETERS:
  6394. * *cd - pointer to core data struct
  6395. * protect - flag to call protected or non-protected
  6396. ******************************************************************************/
  6397. static int pt_hw_soft_reset(struct pt_core_data *cd, int protect)
  6398. {
  6399. int rc = 0;
  6400. mutex_lock(&cd->system_lock);
  6401. cd->startup_status = STARTUP_STATUS_START;
  6402. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  6403. mutex_unlock(&cd->system_lock);
  6404. if (protect)
  6405. rc = pt_hid_cmd_reset(cd);
  6406. else
  6407. rc = pt_hid_cmd_reset_(cd);
  6408. if (rc < 0) {
  6409. pt_debug(cd->dev, DL_ERROR,
  6410. "%s: FAILED to execute SOFT reset\n", __func__);
  6411. return rc;
  6412. }
  6413. pt_debug(cd->dev, DL_INFO, "%s: SOFT reset successful\n",
  6414. __func__);
  6415. return 0;
  6416. }
  6417. /*******************************************************************************
  6418. * FUNCTION: pt_hw_hard_reset
  6419. *
  6420. * SUMMARY: Calls the platform xres function if it exists to perform a hard
  6421. * reset on the DUT by toggling the XRES gpio. Disable/re-enable the
  6422. * TTDL watchdog around the reset to ensure the WD doesn't happen to
  6423. * schedule an enum if it fires when the DUT is being reset.
  6424. * This can cause a double reset.
  6425. *
  6426. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6427. * the WD active could cause this function to fail!
  6428. *
  6429. * RETURN:
  6430. * 0 = success
  6431. * !0 = failure
  6432. *
  6433. * PARAMETERS:
  6434. * *cd - pointer to core data struct
  6435. ******************************************************************************/
  6436. static int pt_hw_hard_reset(struct pt_core_data *cd)
  6437. {
  6438. if (cd->cpdata->xres) {
  6439. cd->startup_status = STARTUP_STATUS_START;
  6440. pt_debug(cd->dev, DL_DEBUG, "%s: Startup Status Reset\n",
  6441. __func__);
  6442. cd->cpdata->xres(cd->cpdata, cd->dev);
  6443. pt_debug(cd->dev, DL_WARN, "%s: executed HARD reset\n",
  6444. __func__);
  6445. return 0;
  6446. }
  6447. pt_debug(cd->dev, DL_ERROR,
  6448. "%s: FAILED to execute HARD reset\n", __func__);
  6449. return -ENODEV;
  6450. }
  6451. /*******************************************************************************
  6452. * FUNCTION: pt_dut_reset
  6453. *
  6454. * SUMMARY: Attempts to reset the DUT by a hard reset and if that fails a
  6455. * soft reset.
  6456. *
  6457. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  6458. * the WD active could cause this function to fail!
  6459. * NOTE: "protect" flag is only used for soft reset.
  6460. *
  6461. * RETURN:
  6462. * 0 = success
  6463. * !0 = failure
  6464. *
  6465. * PARAMETERS:
  6466. * *cd - pointer to core data structure
  6467. * protect - flag to call protected or non-protected
  6468. ******************************************************************************/
  6469. static int pt_dut_reset(struct pt_core_data *cd, int protect)
  6470. {
  6471. int rc = 0;
  6472. pt_debug(cd->dev, DL_INFO, "%s: reset hw...\n", __func__);
  6473. mutex_lock(&cd->system_lock);
  6474. cd->hid_reset_cmd_state = 1;
  6475. rc = pt_hw_hard_reset(cd);
  6476. mutex_unlock(&cd->system_lock);
  6477. if (rc == -ENODEV) {
  6478. mutex_lock(&cd->system_lock);
  6479. cd->hid_reset_cmd_state = 0;
  6480. mutex_unlock(&cd->system_lock);
  6481. pt_debug(cd->dev, DL_ERROR,
  6482. "%s: Hard reset failed, try soft reset\n", __func__);
  6483. rc = pt_hw_soft_reset(cd, protect);
  6484. }
  6485. if (rc)
  6486. pt_debug(cd->dev, DL_ERROR, "%s: %s dev='%s' r=%d\n",
  6487. __func__, "Fail hw reset", dev_name(cd->dev), rc);
  6488. return rc;
  6489. }
  6490. /*******************************************************************************
  6491. * FUNCTION: pt_dut_reset_and_wait
  6492. *
  6493. * SUMMARY: Wrapper function for pt_dut_reset that waits for the reset to
  6494. * complete
  6495. *
  6496. * RETURN:
  6497. * 0 = success
  6498. * !0 = failure
  6499. *
  6500. * PARAMETERS:
  6501. * *cd - pointer to core data structure
  6502. ******************************************************************************/
  6503. static int pt_dut_reset_and_wait(struct pt_core_data *cd)
  6504. {
  6505. int rc = 0;
  6506. int t;
  6507. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  6508. if (rc < 0)
  6509. goto exit;
  6510. t = wait_event_timeout(cd->wait_q,
  6511. (cd->hid_reset_cmd_state == 0),
  6512. msecs_to_jiffies(PT_HID_CMD_DEFAULT_TIMEOUT));
  6513. if (IS_TMO(t)) {
  6514. #ifdef TTDL_DIAGNOSTICS
  6515. cd->bus_transmit_error_count++;
  6516. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  6517. #endif /* TTDL_DIAGNOSTICS */
  6518. pt_debug(cd->dev, DL_ERROR, "%s: reset timed out\n",
  6519. __func__);
  6520. rc = -ETIME;
  6521. goto exit;
  6522. }
  6523. exit:
  6524. return rc;
  6525. }
  6526. /*
  6527. * touch default parameters (from report descriptor) to resolve protocol for
  6528. * touch report
  6529. */
  6530. const struct pt_tch_abs_params tch_hdr_default[PT_TCH_NUM_HDR] = {
  6531. /* byte offset, size, min, max, bit offset, report */
  6532. {0x00, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* SCAN TIME */
  6533. {0x02, 0x01, 0x00, 0x20, 0x00, 0x01}, /* NUMBER OF RECORDS */
  6534. {0x02, 0x01, 0x00, 0x02, 0x05, 0x01}, /* LARGE OBJECT */
  6535. {0x03, 0x01, 0x00, 0x08, 0x00, 0x01}, /* NOISE EFFECT */
  6536. {0x03, 0x01, 0x00, 0x04, 0x06, 0x01}, /* REPORT_COUNTER */
  6537. };
  6538. /*
  6539. * button default parameters (from report descriptor) to resolve protocol for
  6540. * button report
  6541. */
  6542. const struct pt_tch_abs_params tch_abs_default[PT_TCH_NUM_ABS] = {
  6543. /* byte offset, size, min, max, bit offset, report */
  6544. {0x02, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* X */
  6545. {0x04, 0x02, 0x00, 0x10000, 0x00, 0x01}, /* Y */
  6546. {0x06, 0x01, 0x00, 0x100, 0x00, 0x01}, /* P (Z) */
  6547. {0x01, 0x01, 0x00, 0x20, 0x00, 0x01}, /* TOUCH ID */
  6548. {0x01, 0x01, 0x00, 0x04, 0x05, 0x01}, /* EVENT ID */
  6549. {0x00, 0x01, 0x00, 0x08, 0x00, 0x01}, /* OBJECT ID */
  6550. {0x01, 0x01, 0x00, 0x02, 0x07, 0x01}, /* LIFTOFF */
  6551. {0x07, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MAJOR */
  6552. {0x08, 0x01, 0x00, 0x100, 0x00, 0x01}, /* TOUCH_MINOR */
  6553. {0x09, 0x01, 0x00, 0x100, 0x00, 0x01}, /* ORIENTATION */
  6554. };
  6555. /*******************************************************************************
  6556. * FUNCTION: pt_init_pip_report_fields
  6557. *
  6558. * SUMMARY: Setup default values for touch/button report parsing.
  6559. *
  6560. * PARAMETERS:
  6561. * *cd - pointer to core data structure
  6562. ******************************************************************************/
  6563. static void pt_init_pip_report_fields(struct pt_core_data *cd)
  6564. {
  6565. struct pt_sysinfo *si = &cd->sysinfo;
  6566. memcpy(si->tch_hdr, tch_hdr_default, sizeof(tch_hdr_default));
  6567. memcpy(si->tch_abs, tch_abs_default, sizeof(tch_abs_default));
  6568. si->desc.tch_report_id = PT_PIP_TOUCH_REPORT_ID;
  6569. si->desc.tch_record_size = TOUCH_REPORT_SIZE;
  6570. si->desc.tch_header_size = TOUCH_INPUT_HEADER_SIZE;
  6571. si->desc.btn_report_id = PT_PIP_CAPSENSE_BTN_REPORT_ID;
  6572. cd->features.easywake = 1;
  6573. cd->features.noise_metric = 1;
  6574. cd->features.tracking_heatmap = 1;
  6575. cd->features.sensor_data = 1;
  6576. }
  6577. /*******************************************************************************
  6578. * FUNCTION: pt_get_mode
  6579. *
  6580. * SUMMARY: Determine the current mode from the contents of a HID descriptor
  6581. * message
  6582. *
  6583. * RETURN: Enum of the current mode
  6584. *
  6585. * PARAMETERS:
  6586. * *cd - pointer to the Core Data structure
  6587. * protect - run command in protected mode
  6588. * *mode - pointer to store the retrieved mode
  6589. ******************************************************************************/
  6590. static int pt_get_mode(struct pt_core_data *cd, struct pt_hid_desc *desc)
  6591. {
  6592. if (desc->packet_id == HID_APP_REPORT_ID)
  6593. return PT_MODE_OPERATIONAL;
  6594. else if (desc->packet_id == HID_BL_REPORT_ID)
  6595. return PT_MODE_BOOTLOADER;
  6596. return PT_MODE_UNKNOWN;
  6597. }
  6598. /*******************************************************************************
  6599. * FUNCTION: _pt_request_get_mode
  6600. *
  6601. * SUMMARY: Function pointer included in core_cmds to allow other modules
  6602. * to determine the current mode of the DUT by use of the Get HID
  6603. * Descriptor command.
  6604. *
  6605. * RETURN:
  6606. * 0 = success
  6607. * !0 = failure
  6608. *
  6609. * PARAMETERS:
  6610. * *dev - pointer to device structure
  6611. * protect - run command in protected mode
  6612. * *mode - pointer to store the retrieved mode
  6613. ******************************************************************************/
  6614. static int _pt_request_get_mode(struct device *dev, int protect, u8 *mode)
  6615. {
  6616. struct pt_core_data *cd = dev_get_drvdata(dev);
  6617. struct pt_hid_desc hid_desc;
  6618. int rc = 0;
  6619. memset(&hid_desc, 0, sizeof(hid_desc));
  6620. if (protect)
  6621. rc = pt_get_hid_descriptor(cd, &hid_desc);
  6622. else
  6623. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  6624. if (rc)
  6625. *mode = PT_MODE_UNKNOWN;
  6626. else
  6627. *mode = pt_get_mode(cd, &hid_desc);
  6628. return rc;
  6629. }
  6630. /*******************************************************************************
  6631. * FUNCTION: pt_queue_enum_
  6632. *
  6633. * SUMMARY: Queues a TTDL enum by scheduling work with the pt_enum_with_dut()
  6634. * function. It won't try to add/delete sysfs node or modules.
  6635. *
  6636. * RETURN: void
  6637. *
  6638. * PARAMETERS:
  6639. * *cd - pointer to core data
  6640. ******************************************************************************/
  6641. static void pt_queue_enum_(struct pt_core_data *cd)
  6642. {
  6643. if (cd->startup_state == STARTUP_NONE) {
  6644. cd->startup_state = STARTUP_QUEUED;
  6645. #ifdef TTDL_DIAGNOSTICS
  6646. if (!cd->bridge_mode)
  6647. schedule_work(&cd->enum_work);
  6648. else
  6649. cd->startup_state = STARTUP_NONE;
  6650. #else
  6651. schedule_work(&cd->enum_work);
  6652. #endif
  6653. pt_debug(cd->dev, DL_INFO,
  6654. "%s: enum_work queued\n", __func__);
  6655. } else {
  6656. pt_debug(cd->dev, DL_WARN,
  6657. "%s: Enum not queued - startup_state = %d\n",
  6658. __func__, cd->startup_state);
  6659. }
  6660. }
  6661. /*******************************************************************************
  6662. * FUNCTION: pt_queue_enum
  6663. *
  6664. * SUMMARY: Queues a TTDL enum within a mutex lock
  6665. *
  6666. * RETURN: void
  6667. *
  6668. * PARAMETERS:
  6669. * *cd - pointer to core data
  6670. ******************************************************************************/
  6671. static void pt_queue_enum(struct pt_core_data *cd)
  6672. {
  6673. mutex_lock(&cd->system_lock);
  6674. pt_queue_enum_(cd);
  6675. mutex_unlock(&cd->system_lock);
  6676. }
  6677. static void remove_sysfs_and_modules(struct device *dev);
  6678. /*******************************************************************************
  6679. * FUNCTION: pt_queue_restart
  6680. *
  6681. * SUMMARY: Queues a TTDL restart within a mutex lock
  6682. *
  6683. * RETURN: void
  6684. *
  6685. * PARAMETERS:
  6686. * *cd - pointer to core data
  6687. * remove_sysfs_module - True: remove all DUT relative sysfs nodes and modules
  6688. * False: will not perform remove action
  6689. ******************************************************************************/
  6690. static void pt_queue_restart(struct pt_core_data *cd)
  6691. {
  6692. mutex_lock(&cd->system_lock);
  6693. if (cd->startup_state == STARTUP_NONE) {
  6694. cd->startup_state = STARTUP_QUEUED;
  6695. schedule_work(&cd->ttdl_restart_work);
  6696. pt_debug(cd->dev, DL_INFO,
  6697. "%s: pt_ttdl_restart queued\n", __func__);
  6698. } else {
  6699. pt_debug(cd->dev, DL_INFO, "%s: startup_state = %d\n",
  6700. __func__, cd->startup_state);
  6701. }
  6702. mutex_unlock(&cd->system_lock);
  6703. }
  6704. /*******************************************************************************
  6705. * FUNCTION: call_atten_cb
  6706. *
  6707. * SUMMARY: Iterate over attention list call the function that registered.
  6708. *
  6709. * RETURN: void
  6710. *
  6711. * PARAMETERS:
  6712. * *cd - pointer to core data
  6713. * type - type of attention list
  6714. * mode - condition for execution
  6715. ******************************************************************************/
  6716. static void call_atten_cb(struct pt_core_data *cd,
  6717. enum pt_atten_type type, int mode)
  6718. {
  6719. struct atten_node *atten, *atten_n;
  6720. pt_debug(cd->dev, DL_DEBUG, "%s: check list type=%d mode=%d\n",
  6721. __func__, type, mode);
  6722. spin_lock(&cd->spinlock);
  6723. list_for_each_entry_safe(atten, atten_n,
  6724. &cd->atten_list[type], node) {
  6725. if (!mode || atten->mode & mode) {
  6726. spin_unlock(&cd->spinlock);
  6727. pt_debug(cd->dev, DL_DEBUG,
  6728. "%s: attention for '%s'",
  6729. __func__, dev_name(atten->dev));
  6730. atten->func(atten->dev);
  6731. spin_lock(&cd->spinlock);
  6732. }
  6733. }
  6734. spin_unlock(&cd->spinlock);
  6735. }
  6736. /*******************************************************************************
  6737. * FUNCTION: start_fw_upgrade
  6738. *
  6739. * SUMMARY: Calling "PT_ATTEN_LOADER" attention list that loader registered to
  6740. * start firmware upgrade.
  6741. *
  6742. * RETURN:
  6743. * 0 = success
  6744. *
  6745. * PARAMETERS:
  6746. * *data - pointer to core data
  6747. ******************************************************************************/
  6748. static int start_fw_upgrade(void *data)
  6749. {
  6750. struct pt_core_data *cd = (struct pt_core_data *)data;
  6751. call_atten_cb(cd, PT_ATTEN_LOADER, 0);
  6752. return 0;
  6753. }
  6754. /*******************************************************************************
  6755. * FUNCTION: pt_put_device_into_easy_wakeup_
  6756. *
  6757. * SUMMARY: Call the enter_easywake_state function and set the device into easy
  6758. * wake up state.
  6759. *
  6760. * RETURN:
  6761. * 0 = success
  6762. * !0 = failure
  6763. *
  6764. * PARAMETERS:
  6765. * *cd - pointer to core data
  6766. ******************************************************************************/
  6767. static int pt_put_device_into_easy_wakeup_(struct pt_core_data *cd)
  6768. {
  6769. int rc = 0;
  6770. u8 status = 0;
  6771. mutex_lock(&cd->system_lock);
  6772. cd->wait_until_wake = 0;
  6773. mutex_unlock(&cd->system_lock);
  6774. rc = pt_hid_output_enter_easywake_state_(cd,
  6775. cd->easy_wakeup_gesture, &status);
  6776. if (rc || status == 0)
  6777. return -EBUSY;
  6778. return rc;
  6779. }
  6780. /*******************************************************************************
  6781. * FUNCTION: pt_put_device_into_deep_sleep_
  6782. *
  6783. * SUMMARY: Call the set_power function and set the DUT to deep sleep
  6784. *
  6785. * RETURN:
  6786. * 0 = success
  6787. * !0 = error
  6788. *
  6789. * PARAMETERS:
  6790. * *cd - pointer to core data
  6791. ******************************************************************************/
  6792. static int pt_put_device_into_deep_sleep_(struct pt_core_data *cd)
  6793. {
  6794. int rc = 0;
  6795. rc = pt_hid_cmd_set_power_(cd, HID_POWER_SLEEP);
  6796. if (rc)
  6797. rc = -EBUSY;
  6798. return rc;
  6799. }
  6800. /*******************************************************************************
  6801. * FUNCTION: pt_put_device_into_deep_standby_
  6802. *
  6803. * SUMMARY: Call the set_power function and set the DUT to Deep Standby
  6804. *
  6805. * RETURN:
  6806. * 0 = success
  6807. * !0 = error
  6808. *
  6809. * PARAMETERS:
  6810. * *cd - pointer to core data
  6811. ******************************************************************************/
  6812. static int pt_put_device_into_deep_standby_(struct pt_core_data *cd)
  6813. {
  6814. int rc = 0;
  6815. rc = pt_hid_cmd_set_power_(cd, HID_POWER_STANDBY);
  6816. if (rc)
  6817. rc = -EBUSY;
  6818. return rc;
  6819. }
  6820. /*******************************************************************************
  6821. * FUNCTION: pt_core_poweroff_device_
  6822. *
  6823. * SUMMARY: Disable IRQ and HW power down the device.
  6824. *
  6825. * RETURN:
  6826. * 0 = success
  6827. * !0 = failure
  6828. *
  6829. * PARAMETERS:
  6830. * *cd - pointer to core data
  6831. ******************************************************************************/
  6832. static int pt_core_poweroff_device_(struct pt_core_data *cd)
  6833. {
  6834. int rc;
  6835. if (cd->irq_enabled) {
  6836. cd->irq_enabled = false;
  6837. disable_irq_nosync(cd->irq);
  6838. }
  6839. rc = cd->cpdata->power(cd->cpdata, 0, cd->dev, 0);
  6840. if (rc < 0)
  6841. pt_debug(cd->dev, DL_ERROR, "%s: HW Power down fails r=%d\n",
  6842. __func__, rc);
  6843. return rc;
  6844. }
  6845. /*******************************************************************************
  6846. * FUNCTION: pt_core_sleep_
  6847. *
  6848. * SUMMARY: Suspend the device with power off or deep sleep based on the
  6849. * configuration in the core platform data structure.
  6850. *
  6851. * RETURN:
  6852. * 0 = success
  6853. * !0 = failure
  6854. *
  6855. * PARAMETERS:
  6856. * *cd - pointer to core data
  6857. ******************************************************************************/
  6858. static int pt_core_sleep_(struct pt_core_data *cd)
  6859. {
  6860. int rc = 0;
  6861. mutex_lock(&cd->system_lock);
  6862. pt_debug(cd->dev, DL_INFO, "%s - sleep_state %d\n", __func__, cd->sleep_state);
  6863. if (cd->sleep_state == SS_SLEEP_OFF || cd->sleep_state == SS_SLEEP_NONE) {
  6864. cd->sleep_state = SS_SLEEPING;
  6865. } else {
  6866. mutex_unlock(&cd->system_lock);
  6867. pt_debug(cd->dev, DL_INFO,
  6868. "%s - Skip slee[ state %d\n", __func__, cd->sleep_state);
  6869. return 1;
  6870. }
  6871. mutex_unlock(&cd->system_lock);
  6872. /* Ensure watchdog and startup works stopped */
  6873. pt_stop_wd_timer(cd);
  6874. cancel_work_sync(&cd->enum_work);
  6875. pt_stop_wd_timer(cd);
  6876. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  6877. pt_debug(cd->dev, DL_INFO, "%s: Entering into power off mode:\n", __func__);
  6878. rc = pt_core_poweroff_device_(cd);
  6879. if (rc)
  6880. pr_err("%s: Power off error detected :rc=%d\n", __func__, rc);
  6881. } else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY) {
  6882. pt_debug(cd->dev, DL_INFO,
  6883. "%s: Entering into deep standby mode:\n", __func__);
  6884. rc = pt_put_device_into_deep_standby_(cd);
  6885. if (rc)
  6886. pr_err("%s: Deep standby error detected :rc=%d\n", __func__, rc);
  6887. } else {
  6888. pt_debug(cd->dev, DL_INFO,
  6889. "%s: Entering into deep sleep mode:\n", __func__);
  6890. rc = pt_put_device_into_deep_sleep_(cd);
  6891. if (rc)
  6892. pr_err("%s: Deep sleep error detected :rc=%d\n", __func__, rc);
  6893. }
  6894. mutex_lock(&cd->system_lock);
  6895. cd->sleep_state = SS_SLEEP_ON;
  6896. mutex_unlock(&cd->system_lock);
  6897. return rc;
  6898. }
  6899. /*******************************************************************************
  6900. * FUNCTION: pt_core_easywake_on_
  6901. *
  6902. * SUMMARY: Suspend the device with easy wake on the
  6903. * configuration in the core platform data structure.
  6904. *
  6905. * RETURN:
  6906. * 0 = success
  6907. * !0 = failure
  6908. *
  6909. * PARAMETERS:
  6910. * *cd - pointer to core data
  6911. ******************************************************************************/
  6912. static int pt_core_easywake_on_(struct pt_core_data *cd)
  6913. {
  6914. int rc = 0;
  6915. mutex_lock(&cd->system_lock);
  6916. if (cd->sleep_state == SS_SLEEP_ON) {
  6917. mutex_unlock(&cd->system_lock);
  6918. pt_debug(cd->dev, DL_INFO, "%s - Skip sleep state %d\n",
  6919. __func__, cd->sleep_state);
  6920. return 1;
  6921. }
  6922. mutex_unlock(&cd->system_lock);
  6923. /* Ensure watchdog and startup works stopped */
  6924. pt_stop_wd_timer(cd);
  6925. cancel_work_sync(&cd->enum_work);
  6926. pt_stop_wd_timer(cd);
  6927. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  6928. rc = pt_put_device_into_easy_wakeup_(cd);
  6929. pt_debug(cd->dev, DL_INFO, "%s :Entering into easywakeup: rc=%d\n", __func__, rc);
  6930. if (rc)
  6931. pr_err("%s: Easy wakeup error detected :rc=%d\n", __func__, rc);
  6932. }
  6933. return rc;
  6934. }
  6935. /*******************************************************************************
  6936. * FUNCTION: pt_core_easywake_on
  6937. *
  6938. * SUMMARY: Protected call to pt_core_easywake_on_ by exclusive access to the DUT.
  6939. *
  6940. * RETURN:
  6941. * 0 = success
  6942. * !0 = failure
  6943. *
  6944. * PARAMETERS:
  6945. * *cd - pointer to core data
  6946. ******************************************************************************/
  6947. static int pt_core_easywake_on(struct pt_core_data *cd)
  6948. {
  6949. int rc = 0;
  6950. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6951. if (rc < 0) {
  6952. pt_debug(cd->dev, DL_ERROR,
  6953. "%s: fail get exclusive ex=%p own=%p\n",
  6954. __func__, cd->exclusive_dev, cd->dev);
  6955. return rc;
  6956. }
  6957. rc = pt_core_easywake_on_(cd);
  6958. if (release_exclusive(cd, cd->dev) < 0)
  6959. pt_debug(cd->dev, DL_ERROR,
  6960. "%s: fail to release exclusive\n", __func__);
  6961. else
  6962. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6963. __func__);
  6964. return rc;
  6965. }
  6966. /*******************************************************************************
  6967. * FUNCTION: pt_core_sleep
  6968. *
  6969. * SUMMARY: Protected call to pt_core_sleep_ by exclusive access to the DUT.
  6970. *
  6971. * RETURN:
  6972. * 0 = success
  6973. * !0 = failure
  6974. *
  6975. * PARAMETERS:
  6976. * *cd - pointer to core data
  6977. ******************************************************************************/
  6978. static int pt_core_sleep(struct pt_core_data *cd)
  6979. {
  6980. int rc = 0;
  6981. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  6982. if (rc < 0) {
  6983. pt_debug(cd->dev, DL_ERROR,
  6984. "%s: fail get exclusive ex=%p own=%p\n",
  6985. __func__, cd->exclusive_dev, cd->dev);
  6986. return rc;
  6987. }
  6988. rc = pt_core_sleep_(cd);
  6989. if (release_exclusive(cd, cd->dev) < 0)
  6990. pt_debug(cd->dev, DL_ERROR,
  6991. "%s: fail to release exclusive\n", __func__);
  6992. else
  6993. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  6994. __func__);
  6995. return rc;
  6996. }
  6997. /*******************************************************************************
  6998. * FUNCTION: pt_wakeup_host
  6999. *
  7000. * SUMMARY: Check wake up report and call the PT_ATTEN_WAKE attention list.
  7001. *
  7002. * NOTE: TSG5 EasyWake and TSG6 EasyWake use different protocol.
  7003. *
  7004. * RETURN:
  7005. * 0 = success
  7006. * !0 = failure
  7007. *
  7008. * PARAMETERS:
  7009. * *cd - pointer to core data
  7010. ******************************************************************************/
  7011. static int pt_wakeup_host(struct pt_core_data *cd)
  7012. {
  7013. #ifndef EASYWAKE_TSG6
  7014. /* TSG5 EasyWake */
  7015. int rc = 0;
  7016. int event_id;
  7017. int size = get_unaligned_le16(&cd->input_buf[0]);
  7018. /* Validate report */
  7019. if (size != 4 || cd->input_buf[2] != 4)
  7020. rc = -EINVAL;
  7021. event_id = cd->input_buf[3];
  7022. pt_debug(cd->dev, DL_INFO, "%s: e=%d, rc=%d\n",
  7023. __func__, event_id, rc);
  7024. if (rc) {
  7025. pt_core_sleep_(cd);
  7026. goto exit;
  7027. }
  7028. /* attention WAKE */
  7029. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7030. exit:
  7031. return rc;
  7032. #else
  7033. /* TSG6 FW1.3 EasyWake */
  7034. int rc = 0;
  7035. int i = 0;
  7036. int report_length;
  7037. /* Validate report */
  7038. if (cd->input_buf[2] != PT_PIP_WAKEUP_REPORT_ID) {
  7039. rc = -EINVAL;
  7040. pt_core_sleep_(cd);
  7041. goto exit;
  7042. }
  7043. /* Get gesture id and gesture data length */
  7044. cd->gesture_id = cd->input_buf[3];
  7045. report_length = (cd->input_buf[1] << 8) | (cd->input_buf[0]);
  7046. cd->gesture_data_length = report_length - 4;
  7047. pt_debug(cd->dev, DL_INFO,
  7048. "%s: gesture_id = %d, gesture_data_length = %d\n",
  7049. __func__, cd->gesture_id, cd->gesture_data_length);
  7050. for (i = 0; i < cd->gesture_data_length; i++)
  7051. cd->gesture_data[i] = cd->input_buf[4 + i];
  7052. /* attention WAKE */
  7053. call_atten_cb(cd, PT_ATTEN_WAKE, 0);
  7054. exit:
  7055. return rc;
  7056. #endif
  7057. }
  7058. /*******************************************************************************
  7059. * FUNCTION: pt_get_touch_axis
  7060. *
  7061. * SUMMARY: Function to calculate touch axis
  7062. *
  7063. * PARAMETERS:
  7064. * *cd - pointer to core data structure
  7065. * *axis - pointer to axis calculation result
  7066. * size - size in bytes
  7067. * max - max value of result
  7068. * *xy_data - pointer to input data to be parsed
  7069. * bofs - bit offset
  7070. ******************************************************************************/
  7071. static void pt_get_touch_axis(struct pt_core_data *cd,
  7072. int *axis, int size, int max, u8 *data, int bofs)
  7073. {
  7074. int nbyte;
  7075. int next;
  7076. for (nbyte = 0, *axis = 0, next = 0; nbyte < size; nbyte++) {
  7077. *axis = *axis + ((data[next] >> bofs) << (nbyte * 8));
  7078. next++;
  7079. }
  7080. *axis &= max - 1;
  7081. }
  7082. /*******************************************************************************
  7083. * FUNCTION: move_tracking_heatmap_data
  7084. *
  7085. * SUMMARY: Move the valid tracking heatmap data from the input buffer into the
  7086. * system information structure, xy_mode and xy_data.
  7087. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7088. * the tthe_tuner sysfs node under the label "THM"
  7089. *
  7090. * RETURN:
  7091. * 0 = success
  7092. * !0 = failure
  7093. *
  7094. * PARAMETERS:
  7095. * *cd - pointer to core data
  7096. * *si - pointer to the system information structure
  7097. ******************************************************************************/
  7098. static int move_tracking_heatmap_data(struct pt_core_data *cd,
  7099. struct pt_sysinfo *si)
  7100. {
  7101. #ifdef TTHE_TUNER_SUPPORT
  7102. int size = get_unaligned_le16(&cd->input_buf[0]);
  7103. if (size)
  7104. tthe_print(cd, cd->input_buf, size, "THM=");
  7105. #endif
  7106. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7107. return 0;
  7108. }
  7109. /*******************************************************************************
  7110. * FUNCTION: move_sensor_data
  7111. *
  7112. * SUMMARY: Move the valid sensor data from the input buffer into the system
  7113. * information structure, xy_mode and xy_data.
  7114. * - If TTHE_TUNER_SUPPORT is defined print the raw sensor data into
  7115. * the tthe_tuner sysfs node under the label "sensor_monitor"
  7116. *
  7117. * RETURN:
  7118. * 0 = success
  7119. * !0 = failure
  7120. *
  7121. * PARAMETERS:
  7122. * *cd - pointer to core data
  7123. * *si - pointer to the system information structure
  7124. ******************************************************************************/
  7125. static int move_sensor_data(struct pt_core_data *cd,
  7126. struct pt_sysinfo *si)
  7127. {
  7128. #ifdef TTHE_TUNER_SUPPORT
  7129. int size = get_unaligned_le16(&cd->input_buf[0]);
  7130. if (size)
  7131. tthe_print(cd, cd->input_buf, size, "sensor_monitor=");
  7132. #endif
  7133. memcpy(si->xy_mode, cd->input_buf, SENSOR_HEADER_SIZE);
  7134. return 0;
  7135. }
  7136. /*******************************************************************************
  7137. * FUNCTION: move_button_data
  7138. *
  7139. * SUMMARY: Move the valid button data from the input buffer into the system
  7140. * information structure, xy_mode and xy_data.
  7141. * - If TTHE_TUNER_SUPPORT is defined print the raw button data into
  7142. * the tthe_tuner sysfs node under the label "OpModeData"
  7143. *
  7144. * RETURN:
  7145. * 0 = success
  7146. * !0 = failure
  7147. *
  7148. * PARAMETERS:
  7149. * *cd - pointer to core data
  7150. * *si - pointer to the system information structure
  7151. ******************************************************************************/
  7152. static int move_button_data(struct pt_core_data *cd,
  7153. struct pt_sysinfo *si)
  7154. {
  7155. #ifdef TTHE_TUNER_SUPPORT
  7156. int size = get_unaligned_le16(&cd->input_buf[0]);
  7157. if (size)
  7158. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7159. #endif
  7160. memcpy(si->xy_mode, cd->input_buf, BTN_INPUT_HEADER_SIZE);
  7161. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode, BTN_INPUT_HEADER_SIZE,
  7162. "xy_mode");
  7163. memcpy(si->xy_data, &cd->input_buf[BTN_INPUT_HEADER_SIZE],
  7164. BTN_REPORT_SIZE);
  7165. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, BTN_REPORT_SIZE,
  7166. "xy_data");
  7167. return 0;
  7168. }
  7169. /*******************************************************************************
  7170. * FUNCTION: move_touch_data
  7171. *
  7172. * SUMMARY: Move the valid touch data from the input buffer into the system
  7173. * information structure, xy_mode and xy_data.
  7174. * - If TTHE_TUNER_SUPPORT is defined print the raw touch data into
  7175. * the tthe_tuner sysfs node under the label "OpModeData"
  7176. *
  7177. * RETURN:
  7178. * 0 = success
  7179. * !0 = failure
  7180. *
  7181. * PARAMETERS:
  7182. * *cd - pointer to core data
  7183. * *si - pointer to the system information structure
  7184. ******************************************************************************/
  7185. static int move_touch_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7186. {
  7187. int max_tch = si->sensing_conf_data.max_tch;
  7188. int num_cur_tch;
  7189. int length;
  7190. struct pt_tch_abs_params *tch = &si->tch_hdr[PT_TCH_NUM];
  7191. #ifdef TTHE_TUNER_SUPPORT
  7192. int size = get_unaligned_le16(&cd->input_buf[0]);
  7193. if (size)
  7194. tthe_print(cd, cd->input_buf, size, "OpModeData=");
  7195. #endif
  7196. memcpy(si->xy_mode, cd->input_buf, si->desc.tch_header_size);
  7197. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_mode,
  7198. si->desc.tch_header_size, "xy_mode");
  7199. pt_get_touch_axis(cd, &num_cur_tch, tch->size,
  7200. tch->max, si->xy_mode + 3 + tch->ofs, tch->bofs);
  7201. if (unlikely(num_cur_tch > max_tch))
  7202. num_cur_tch = max_tch;
  7203. length = num_cur_tch * si->desc.tch_record_size;
  7204. memcpy(si->xy_data, &cd->input_buf[si->desc.tch_header_size], length);
  7205. pt_pr_buf(cd->dev, DL_INFO, (u8 *)si->xy_data, length, "xy_data");
  7206. return 0;
  7207. }
  7208. /*******************************************************************************
  7209. * FUNCTION: move_hid_pen_data
  7210. *
  7211. * SUMMARY: TODO Move the valid pen data from the input buffer into the system
  7212. * information structure, xy_mode and xy_data.
  7213. * - If TTHE_TUNER_SUPPORT is defined print the raw pen data into
  7214. * the tthe_tuner sysfs node under the label "HID" starting with the
  7215. * report ID.
  7216. *
  7217. * RETURN:
  7218. * 0 = success
  7219. * !0 = failure
  7220. *
  7221. * PARAMETERS:
  7222. * *cd - pointer to core data
  7223. * *si - pointer to the system information structure
  7224. ******************************************************************************/
  7225. static int move_hid_pen_data(struct pt_core_data *cd, struct pt_sysinfo *si)
  7226. {
  7227. #ifdef TTHE_TUNER_SUPPORT
  7228. int size = get_unaligned_le16(&cd->input_buf[0]);
  7229. if (size) {
  7230. /*
  7231. * HID over USB does not require the two byte length field, so
  7232. * this should print from input_buf[2] but to keep both finger
  7233. * and pen reports the same the length is included
  7234. */
  7235. if (cd->tthe_hid_usb_format == PT_FEATURE_ENABLE)
  7236. tthe_print(cd, &(cd->input_buf[2]), size - 2,
  7237. "HID-USB=");
  7238. else
  7239. tthe_print(cd, &(cd->input_buf[0]), size,
  7240. "HID-I2C=");
  7241. }
  7242. #endif
  7243. pt_pr_buf(cd->dev, DL_INFO, (u8 *)&(cd->input_buf[0]), size, "HID Pen");
  7244. return 0;
  7245. }
  7246. /*******************************************************************************
  7247. * FUNCTION: parse_touch_input
  7248. *
  7249. * SUMMARY: Parse the touch report and take action based on the touch
  7250. * report_id.
  7251. *
  7252. * RETURN:
  7253. * 0 = success
  7254. * !0 = failure
  7255. *
  7256. * PARAMETERS:
  7257. * *cd - pointer to core data
  7258. * size - size of touch record
  7259. ******************************************************************************/
  7260. static int parse_touch_input(struct pt_core_data *cd, int size)
  7261. {
  7262. struct pt_sysinfo *si = &cd->sysinfo;
  7263. int report_id = cd->input_buf[2];
  7264. int rc = -EINVAL;
  7265. pt_debug(cd->dev, DL_DEBUG, "%s: Received touch report\n",
  7266. __func__);
  7267. if (!si->ready) {
  7268. pt_debug(cd->dev, DL_ERROR,
  7269. "%s: Need system information to parse touches\n",
  7270. __func__);
  7271. return 0;
  7272. }
  7273. if (!si->xy_mode || !si->xy_data)
  7274. return rc;
  7275. if (report_id == PT_PIP_TOUCH_REPORT_ID)
  7276. rc = move_touch_data(cd, si);
  7277. else if (report_id == PT_HID_PEN_REPORT_ID)
  7278. rc = move_hid_pen_data(cd, si);
  7279. else if (report_id == PT_PIP_CAPSENSE_BTN_REPORT_ID)
  7280. rc = move_button_data(cd, si);
  7281. else if (report_id == PT_PIP_SENSOR_DATA_REPORT_ID)
  7282. rc = move_sensor_data(cd, si);
  7283. else if (report_id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7284. rc = move_tracking_heatmap_data(cd, si);
  7285. if (rc)
  7286. return rc;
  7287. /* attention IRQ */
  7288. call_atten_cb(cd, PT_ATTEN_IRQ, cd->mode);
  7289. return 0;
  7290. }
  7291. /*******************************************************************************
  7292. * FUNCTION: parse_command_input
  7293. *
  7294. * SUMMARY: Move the response data from the input buffer to the response buffer
  7295. *
  7296. * RETURN:
  7297. * 0 = success
  7298. * !0 = failure
  7299. *
  7300. * PARAMETERS:
  7301. * *cd - pointer to core data
  7302. * size - size of response data
  7303. ******************************************************************************/
  7304. static int parse_command_input(struct pt_core_data *cd, int size)
  7305. {
  7306. pt_debug(cd->dev, DL_DEBUG, "%s: Received cmd interrupt\n",
  7307. __func__);
  7308. memcpy(cd->response_buf, cd->input_buf, size);
  7309. #if defined(TTHE_TUNER_SUPPORT) && defined(TTDL_DIAGNOSTICS)
  7310. if (size && cd->show_tt_data) {
  7311. if (cd->pip2_prot_active)
  7312. tthe_print(cd, cd->input_buf, size, "TT_DATA_PIP2=");
  7313. else
  7314. tthe_print(cd, cd->input_buf, size, "TT_DATA=");
  7315. }
  7316. #endif
  7317. mutex_lock(&cd->system_lock);
  7318. cd->hid_cmd_state = 0;
  7319. mutex_unlock(&cd->system_lock);
  7320. wake_up(&cd->wait_q);
  7321. return 0;
  7322. }
  7323. /*******************************************************************************
  7324. * FUNCTION: pt_allow_enumeration
  7325. *
  7326. * SUMMARY: Determine if an enumeration or fully re-probe should perform when
  7327. * FW sentinel is seen.
  7328. *
  7329. * RETURN:
  7330. * true = allow enumeration or fully re-probe
  7331. * false = skip enumeration and fully re-probe
  7332. *
  7333. * PARAMETERS:
  7334. * *cd - pointer to core data
  7335. ******************************************************************************/
  7336. static inline bool pt_allow_enumeration(struct pt_core_data *cd)
  7337. {
  7338. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7339. (!cd->hid_reset_cmd_state) &&
  7340. (cd->core_probe_complete) &&
  7341. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7342. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7343. (cd->mode == PT_MODE_OPERATIONAL)) {
  7344. return true;
  7345. }
  7346. if ((!cd->hid_reset_cmd_state) &&
  7347. (cd->core_probe_complete) &&
  7348. (cd->hid_cmd_state != PIP1_CMD_ID_START_BOOTLOADER + 1) &&
  7349. (cd->hid_cmd_state != PIP1_BL_CMD_ID_LAUNCH_APP + 1) &&
  7350. (cd->active_dut_generation != DUT_PIP1_ONLY)) {
  7351. return true;
  7352. }
  7353. pt_debug(cd->dev, DL_INFO,
  7354. "%s: Dissallow - %s=%d %s=%d %s=0x%02X %s=%d\n",
  7355. __func__,
  7356. "hid_reset_cmd_state(0)", cd->hid_reset_cmd_state,
  7357. "core_probe_complete(1)", cd->core_probe_complete,
  7358. "hid_cmd_state(Not 0x02 or 0x39)", cd->hid_cmd_state,
  7359. "active_dut_gen(0,2)", cd->active_dut_generation);
  7360. return false;
  7361. }
  7362. /*******************************************************************************
  7363. * FUNCTION: pt_is_touch_report
  7364. *
  7365. * SUMMARY: Determine if a report ID should be treated as a touch report
  7366. *
  7367. * RETURN:
  7368. * true = report ID is a touch report
  7369. * false = report ID is not a touch report
  7370. *
  7371. * PARAMETERS:
  7372. * id - Report ID
  7373. ******************************************************************************/
  7374. static bool pt_is_touch_report(int id)
  7375. {
  7376. if (id == PT_PIP_TOUCH_REPORT_ID ||
  7377. id == PT_HID_PEN_REPORT_ID ||
  7378. id == PT_PIP_CAPSENSE_BTN_REPORT_ID ||
  7379. id == PT_PIP_SENSOR_DATA_REPORT_ID ||
  7380. id == PT_PIP_TRACKING_HEATMAP_REPORT_ID)
  7381. return true;
  7382. else
  7383. return false;
  7384. }
  7385. /*******************************************************************************
  7386. * FUNCTION: pt_parse_input
  7387. *
  7388. * SUMMARY: Parse the input data read from DUT due to IRQ. Handle data based
  7389. * on if its a response to a command or asynchronous touch data / reset
  7390. * sentinel. PIP2.x messages have additional error checking that is
  7391. * parsed (SEQ match from cmd to rsp, CRC valid).
  7392. * Look for special packets based on unique lengths:
  7393. * 0 bytes - APP(FW) reset sentinel or Gen5/6 BL sentinel
  7394. * 2 bytes - Empty buffer (PIP 1.7 and earlier)
  7395. * 11 bytes - possible PIP2.x reset sentinel (TAG and SEQ must = 0)
  7396. * 0xFFXX - Empty buffer (PIP 1.7+)
  7397. * Queue a startup after any asynchronous FW reset sentinel is seen, unless
  7398. * the initial probe has not yet been done.
  7399. *
  7400. * RETURN:
  7401. * 0 = success
  7402. * !0 = failure
  7403. *
  7404. * PARAMETERS:
  7405. * *cd - pointer to core data
  7406. ******************************************************************************/
  7407. static int pt_parse_input(struct pt_core_data *cd)
  7408. {
  7409. int report_id;
  7410. int cmd_id;
  7411. int is_command = 0;
  7412. int size;
  7413. int print_size;
  7414. bool touch_report = true;
  7415. unsigned short calc_crc;
  7416. unsigned short resp_crc;
  7417. cd->fw_sys_mode_in_standby_state = false;
  7418. size = get_unaligned_le16(&cd->input_buf[0]);
  7419. print_size = size;
  7420. pt_debug(cd->dev, DL_DEBUG, "<<< %s: IRQ Triggered, read len [%d]\n",
  7421. __func__, print_size);
  7422. if (print_size <= PT_MAX_INPUT)
  7423. pt_pr_buf(cd->dev, DL_DEBUG, cd->input_buf, print_size,
  7424. "<<< Read buf");
  7425. if (size == 0 ||
  7426. (size == 11 &&
  7427. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] &
  7428. PIP2_RESP_SEQUENCE_MASK) == 0 &&
  7429. (cd->input_buf[PIP2_RESP_REPORT_ID_OFFSET] &
  7430. PIP2_CMD_COMMAND_ID_MASK) ==
  7431. PIP2_CMD_ID_STATUS)) {
  7432. touch_report = false;
  7433. cd->hw_detected = true;
  7434. cd->bl_pip_ver_ready = false;
  7435. cd->app_pip_ver_ready = false;
  7436. if (size == 0) {
  7437. mutex_lock(&cd->system_lock);
  7438. cd->pip2_prot_active = false;
  7439. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  7440. /*
  7441. * For Gen5/6 this sentinel could be from
  7442. * the BL or FW. Attempt to set the correct
  7443. * mode based on the previous PIP command.
  7444. */
  7445. if (cd->hid_cmd_state ==
  7446. PIP1_BL_CMD_ID_LAUNCH_APP + 1) {
  7447. cd->mode = PT_MODE_OPERATIONAL;
  7448. cd->startup_status =
  7449. STARTUP_STATUS_FW_RESET_SENTINEL;
  7450. } else if (cd->hid_cmd_state ==
  7451. PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7452. cd->hid_reset_cmd_state) {
  7453. cd->mode = PT_MODE_BOOTLOADER;
  7454. cd->startup_status =
  7455. STARTUP_STATUS_BL_RESET_SENTINEL;
  7456. } else {
  7457. cd->mode = PT_MODE_UNKNOWN;
  7458. cd->startup_status =
  7459. STARTUP_STATUS_START;
  7460. }
  7461. cd->fw_system_mode = FW_SYS_MODE_UNDEFINED;
  7462. pt_debug(cd->dev, DL_INFO,
  7463. "%s: ATM Gen5/6 %s sentinel received\n",
  7464. __func__,
  7465. (cd->mode == PT_MODE_OPERATIONAL ?
  7466. "FW" :
  7467. (cd->mode == PT_MODE_BOOTLOADER ?
  7468. "BL" : "Unknown")));
  7469. } else {
  7470. cd->mode = PT_MODE_OPERATIONAL;
  7471. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  7472. cd->startup_status =
  7473. STARTUP_STATUS_FW_RESET_SENTINEL;
  7474. pt_debug(cd->dev, DL_INFO,
  7475. "%s: ATM PT/TT FW sentinel received\n",
  7476. __func__);
  7477. }
  7478. mutex_unlock(&cd->system_lock);
  7479. if (pt_allow_enumeration(cd)) {
  7480. if (cd->active_dut_generation == DUT_UNKNOWN) {
  7481. pt_debug(cd->dev, DL_INFO,
  7482. "%s: Queue Restart\n", __func__);
  7483. pt_queue_restart(cd);
  7484. } else {
  7485. pt_debug(cd->dev, DL_INFO,
  7486. "%s: Queue Enum\n", __func__);
  7487. pt_queue_enum(cd);
  7488. }
  7489. } else {
  7490. pt_debug(cd->dev, DL_INFO,
  7491. "%s: Sentinel - No Queued Action\n",
  7492. __func__);
  7493. }
  7494. } else {
  7495. /* Sentinel must be from TT/TC BL */
  7496. mutex_lock(&cd->system_lock);
  7497. cd->pip2_prot_active = true;
  7498. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  7499. cd->mode = PT_MODE_BOOTLOADER;
  7500. cd->sysinfo.ready = false;
  7501. mutex_unlock(&cd->system_lock);
  7502. pt_debug(cd->dev, DL_INFO,
  7503. "%s: BL Reset sentinel received\n", __func__);
  7504. if (cd->flashless_dut &&
  7505. cd->flashless_auto_bl == PT_ALLOW_AUTO_BL) {
  7506. pt_debug(cd->dev, DL_INFO,
  7507. "%s: BL to RAM for flashless DUT\n",
  7508. __func__);
  7509. kthread_run(start_fw_upgrade, cd, "pt_loader");
  7510. }
  7511. }
  7512. mutex_lock(&cd->system_lock);
  7513. memcpy(cd->response_buf, cd->input_buf, 2);
  7514. if (!cd->hid_reset_cmd_state && !cd->hid_cmd_state) {
  7515. mutex_unlock(&cd->system_lock);
  7516. pt_debug(cd->dev, DL_WARN,
  7517. "%s: Device Initiated Reset\n", __func__);
  7518. wake_up(&cd->wait_q);
  7519. return 0;
  7520. }
  7521. cd->hid_reset_cmd_state = 0;
  7522. if (cd->hid_cmd_state == PIP1_CMD_ID_START_BOOTLOADER + 1 ||
  7523. cd->hid_cmd_state == PIP1_BL_CMD_ID_LAUNCH_APP + 1 ||
  7524. cd->hid_cmd_state == PIP1_CMD_ID_USER_CMD + 1)
  7525. cd->hid_cmd_state = 0;
  7526. wake_up(&cd->wait_q);
  7527. mutex_unlock(&cd->system_lock);
  7528. return 0;
  7529. } else if (size == 2 || size >= PT_PIP_1P7_EMPTY_BUF) {
  7530. /*
  7531. * This debug message below is used by PBATS to calculate the
  7532. * time from the last lift off IRQ to when FW exits LFT mode.
  7533. */
  7534. touch_report = false;
  7535. pt_debug(cd->dev, DL_WARN,
  7536. "%s: DUT - Empty buffer detected\n", __func__);
  7537. return 0;
  7538. } else if (size > PT_MAX_INPUT) {
  7539. pt_debug(cd->dev, DL_ERROR,
  7540. "%s: DUT - Unexpected len field in active bus data!\n",
  7541. __func__);
  7542. return -EINVAL;
  7543. }
  7544. if (cd->pip2_prot_active) {
  7545. pt_debug(cd->dev, DL_DEBUG,
  7546. "%s: Decode PIP2.x Response\n", __func__);
  7547. /* PIP2 does not have a report id, hard code it */
  7548. report_id = 0x00;
  7549. cmd_id = cd->input_buf[PIP2_RESP_COMMAND_ID_OFFSET];
  7550. calc_crc = crc_ccitt_calculate(cd->input_buf, size - 2);
  7551. resp_crc = cd->input_buf[size - 2] << 8;
  7552. resp_crc |= cd->input_buf[size - 1];
  7553. if ((cd->pip2_cmd_tag_seq !=
  7554. (cd->input_buf[PIP2_RESP_SEQUENCE_OFFSET] & 0x0F)) &&
  7555. (resp_crc != calc_crc) &&
  7556. ((cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7557. == PT_PIP_TOUCH_REPORT_ID) ||
  7558. (cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET]
  7559. == PT_PIP_CAPSENSE_BTN_REPORT_ID))) {
  7560. pt_debug(cd->dev, DL_WARN,
  7561. "%s: %s %d %s\n",
  7562. __func__,
  7563. "Received PIP1 report id =",
  7564. cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET],
  7565. "when expecting a PIP2 report - IGNORE report");
  7566. return 0;
  7567. }
  7568. is_command = 1;
  7569. touch_report = false;
  7570. } else {
  7571. report_id = cd->input_buf[PIP1_RESP_REPORT_ID_OFFSET];
  7572. cmd_id = cd->input_buf[PIP1_RESP_COMMAND_ID_OFFSET];
  7573. }
  7574. #ifdef TTDL_DIAGNOSTICS
  7575. pt_debug(cd->dev, DL_DEBUG,
  7576. "%s: pip2 = %d report_id: 0x%02X, cmd_code: 0x%02X\n",
  7577. __func__, cd->pip2_prot_active, report_id,
  7578. (cmd_id & PIP2_CMD_COMMAND_ID_MASK));
  7579. #endif /* TTDL_DIAGNOSTICS */
  7580. if (report_id == PT_PIP_WAKEUP_REPORT_ID) {
  7581. pt_wakeup_host(cd);
  7582. #ifdef TTHE_TUNER_SUPPORT
  7583. tthe_print(cd, cd->input_buf, size, "TT_WAKEUP=");
  7584. #endif
  7585. return 0;
  7586. }
  7587. mod_timer_pending(&cd->watchdog_timer, jiffies +
  7588. msecs_to_jiffies(cd->watchdog_interval));
  7589. /*
  7590. * If it is PIP2 response, the report_id has been set to 0,
  7591. * so it will not be parsed as a touch packet.
  7592. */
  7593. if (!pt_is_touch_report(report_id)) {
  7594. is_command = 1;
  7595. touch_report = false;
  7596. }
  7597. if (unlikely(is_command)) {
  7598. parse_command_input(cd, size);
  7599. return 0;
  7600. }
  7601. if (touch_report)
  7602. parse_touch_input(cd, size);
  7603. return 0;
  7604. }
  7605. /*******************************************************************************
  7606. * FUNCTION: pt_read_input
  7607. *
  7608. * SUMMARY: Reads incoming data off of the active bus
  7609. *
  7610. * RETURN:
  7611. * 0 = success
  7612. * !0 = failure
  7613. *
  7614. * PARAMETERS:
  7615. * *cd - pointer to core data
  7616. ******************************************************************************/
  7617. static int pt_read_input(struct pt_core_data *cd)
  7618. {
  7619. struct device *dev = cd->dev;
  7620. int rc = 0;
  7621. int t;
  7622. int retry = PT_BUS_READ_INPUT_RETRY_COUNT;
  7623. /*
  7624. * Workaround for easywake failure
  7625. * Interrupt for easywake, wait for bus controller to wake
  7626. */
  7627. mutex_lock(&cd->system_lock);
  7628. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture)) {
  7629. if (cd->sleep_state == SS_SLEEP_ON) {
  7630. mutex_unlock(&cd->system_lock);
  7631. if (!dev->power.is_suspended)
  7632. goto read;
  7633. t = wait_event_timeout(cd->wait_q,
  7634. (cd->wait_until_wake == 1),
  7635. msecs_to_jiffies(2000));
  7636. #ifdef TTDL_DIAGNOSTICS
  7637. if (IS_TMO(t)) {
  7638. cd->bus_transmit_error_count++;
  7639. pt_toggle_err_gpio(cd, PT_ERR_GPIO_I2C_TRANS);
  7640. pt_debug(dev, DL_ERROR,
  7641. "%s: !!!I2C Transmission Error %d\n",
  7642. __func__,
  7643. cd->bus_transmit_error_count);
  7644. }
  7645. #else
  7646. if (IS_TMO(t))
  7647. pt_queue_enum(cd);
  7648. #endif /* TTDL_DIAGNOSTICS */
  7649. goto read;
  7650. }
  7651. }
  7652. mutex_unlock(&cd->system_lock);
  7653. read:
  7654. /* Try reading up to 'retry' times */
  7655. while (retry-- != 0) {
  7656. rc = pt_adap_read_default_nosize(cd, cd->input_buf,
  7657. PT_MAX_INPUT);
  7658. if (!rc) {
  7659. pt_debug(dev, DL_DEBUG,
  7660. "%s: Read input successfully\n", __func__);
  7661. goto read_exit;
  7662. }
  7663. usleep_range(1000, 2000);
  7664. }
  7665. pt_debug(dev, DL_ERROR,
  7666. "%s: Error getting report, rc=%d\n", __func__, rc);
  7667. read_exit:
  7668. return rc;
  7669. }
  7670. /*******************************************************************************
  7671. * FUNCTION: pt_irq
  7672. *
  7673. * SUMMARY: Process all detected interrupts
  7674. *
  7675. * RETURN:
  7676. * IRQ_HANDLED - Finished processing the interrupt
  7677. *
  7678. * PARAMETERS:
  7679. * irq - IRQ number
  7680. * *handle - pointer to core data struct
  7681. ******************************************************************************/
  7682. irqreturn_t pt_irq(int irq, void *handle)
  7683. {
  7684. struct pt_core_data *cd = handle;
  7685. int rc = 0;
  7686. if (!pt_check_irq_asserted(cd))
  7687. return IRQ_HANDLED;
  7688. rc = pt_read_input(cd);
  7689. #ifdef TTDL_DIAGNOSTICS
  7690. cd->irq_count++;
  7691. /* Used to calculate T-Refresh */
  7692. if (cd->t_refresh_active) {
  7693. if (cd->t_refresh_count == 0) {
  7694. cd->t_refresh_time = jiffies;
  7695. cd->t_refresh_count++;
  7696. } else if (cd->t_refresh_count < cd->t_refresh_total) {
  7697. cd->t_refresh_count++;
  7698. } else {
  7699. cd->t_refresh_active = 0;
  7700. cd->t_refresh_time =
  7701. jiffies_to_msecs(jiffies - cd->t_refresh_time);
  7702. }
  7703. }
  7704. #endif /* TTDL_DIAGNOSTICS */
  7705. if (!rc)
  7706. pt_parse_input(cd);
  7707. return IRQ_HANDLED;
  7708. }
  7709. /*******************************************************************************
  7710. * FUNCTION: _pt_subscribe_attention
  7711. *
  7712. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7713. * to subscribe themselves into the TTDL attention list
  7714. *
  7715. * RETURN:
  7716. * 0 = success
  7717. * !0 = failure
  7718. *
  7719. * PARAMETERS:
  7720. * *dev - pointer to device structure
  7721. * type - attention type enum
  7722. * *id - ID of the module calling this function
  7723. * *func - callback function pointer to be called when notified
  7724. * mode - attention mode
  7725. ******************************************************************************/
  7726. int _pt_subscribe_attention(struct device *dev,
  7727. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7728. int mode)
  7729. {
  7730. struct pt_core_data *cd = dev_get_drvdata(dev);
  7731. struct atten_node *atten, *atten_new;
  7732. atten_new = kzalloc(sizeof(*atten_new), GFP_KERNEL);
  7733. if (!atten_new)
  7734. return -ENOMEM;
  7735. pt_debug(cd->dev, DL_INFO, "%s from '%s'\n", __func__,
  7736. dev_name(cd->dev));
  7737. spin_lock(&cd->spinlock);
  7738. list_for_each_entry(atten, &cd->atten_list[type], node) {
  7739. if (atten->id == id && atten->mode == mode) {
  7740. spin_unlock(&cd->spinlock);
  7741. kfree(atten_new);
  7742. pt_debug(cd->dev, DL_INFO, "%s: %s=%p %s=%d\n",
  7743. __func__,
  7744. "already subscribed attention",
  7745. dev, "mode", mode);
  7746. return 0;
  7747. }
  7748. }
  7749. atten_new->id = id;
  7750. atten_new->dev = dev;
  7751. atten_new->mode = mode;
  7752. atten_new->func = func;
  7753. list_add(&atten_new->node, &cd->atten_list[type]);
  7754. spin_unlock(&cd->spinlock);
  7755. return 0;
  7756. }
  7757. /*******************************************************************************
  7758. * FUNCTION: _pt_unsubscribe_attention
  7759. *
  7760. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7761. * to unsubscribe themselves from the TTDL attention list
  7762. *
  7763. * RETURN:
  7764. * 0 = success
  7765. * !0 = failure
  7766. *
  7767. * PARAMETERS:
  7768. * *dev - pointer to device structure
  7769. * type - attention type enum
  7770. * *id - ID of the module calling this function
  7771. * *func - function pointer
  7772. * mode - attention mode
  7773. ******************************************************************************/
  7774. int _pt_unsubscribe_attention(struct device *dev,
  7775. enum pt_atten_type type, char *id, int (*func)(struct device *),
  7776. int mode)
  7777. {
  7778. struct pt_core_data *cd = dev_get_drvdata(dev);
  7779. struct atten_node *atten, *atten_n;
  7780. spin_lock(&cd->spinlock);
  7781. list_for_each_entry_safe(atten, atten_n, &cd->atten_list[type], node) {
  7782. if (atten->id == id && atten->mode == mode) {
  7783. list_del(&atten->node);
  7784. spin_unlock(&cd->spinlock);
  7785. pt_debug(cd->dev, DL_DEBUG, "%s: %s=%p %s=%d\n",
  7786. __func__,
  7787. "unsub for atten->dev", atten->dev,
  7788. "atten->mode", atten->mode);
  7789. kfree(atten);
  7790. return 0;
  7791. }
  7792. }
  7793. spin_unlock(&cd->spinlock);
  7794. return -ENODEV;
  7795. }
  7796. /*******************************************************************************
  7797. * FUNCTION: _pt_request_exclusive
  7798. *
  7799. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7800. * to request exclusive access
  7801. *
  7802. * RETURN:
  7803. * 0 = success
  7804. * !0 = failure
  7805. *
  7806. * PARAMETERS:
  7807. * *dev - pointer to device structure
  7808. * timeout_ms - timeout to wait for exclusive access
  7809. ******************************************************************************/
  7810. static int _pt_request_exclusive(struct device *dev,
  7811. int timeout_ms)
  7812. {
  7813. struct pt_core_data *cd = dev_get_drvdata(dev);
  7814. return request_exclusive(cd, (void *)dev, timeout_ms);
  7815. }
  7816. /*******************************************************************************
  7817. * FUNCTION: _pt_release_exclusive
  7818. *
  7819. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7820. * to release exclusive access
  7821. *
  7822. * RETURN:
  7823. * 0 = success
  7824. * !0 = failure
  7825. *
  7826. * PARAMETERS:
  7827. * *dev - pointer to device structure
  7828. ******************************************************************************/
  7829. static int _pt_release_exclusive(struct device *dev)
  7830. {
  7831. struct pt_core_data *cd = dev_get_drvdata(dev);
  7832. return release_exclusive(cd, (void *)dev);
  7833. }
  7834. /*******************************************************************************
  7835. * FUNCTION: _pt_request_reset
  7836. *
  7837. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7838. * to request the DUT to be reset. Function returns err if refused or
  7839. * timeout occurs (Note: core uses fixed timeout period).
  7840. *
  7841. * NOTE: Function blocks until ISR occurs.
  7842. *
  7843. * RETURN:
  7844. * 0 = success
  7845. * !0 = failure
  7846. *
  7847. * PARAMETERS:
  7848. * *dev - pointer to device structure
  7849. * protect - flag to call protected or non-protected
  7850. ******************************************************************************/
  7851. static int _pt_request_reset(struct device *dev, int protect)
  7852. {
  7853. struct pt_core_data *cd = dev_get_drvdata(dev);
  7854. int rc;
  7855. rc = pt_dut_reset(cd, protect);
  7856. if (rc < 0) {
  7857. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  7858. __func__, rc);
  7859. }
  7860. return rc;
  7861. }
  7862. /*******************************************************************************
  7863. * FUNCTION: _pt_request_enum
  7864. *
  7865. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7866. * to request TTDL to queue an enum. This function will return err
  7867. * if refused; if no error then enum has completed and system is in
  7868. * normal operation mode.
  7869. *
  7870. * RETURN:
  7871. * 0 = success
  7872. * !0 = failure
  7873. *
  7874. * PARAMETERS:
  7875. * *dev - pointer to device structure
  7876. * wait - boolean to determine if to wait for startup event
  7877. ******************************************************************************/
  7878. static int _pt_request_enum(struct device *dev, bool wait)
  7879. {
  7880. struct pt_core_data *cd = dev_get_drvdata(dev);
  7881. pt_queue_enum(cd);
  7882. if (wait)
  7883. wait_event_timeout(cd->wait_q,
  7884. cd->startup_state == STARTUP_NONE,
  7885. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  7886. return 0;
  7887. }
  7888. /*******************************************************************************
  7889. * FUNCTION: _pt_request_sysinfo
  7890. *
  7891. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7892. * to request the pointer to the system information structure. This
  7893. * function will return NULL if sysinfo has not been acquired from the
  7894. * DUT yet.
  7895. *
  7896. * RETURN: Pointer to the system information struct
  7897. *
  7898. * PARAMETERS:
  7899. * *dev - pointer to device structure
  7900. ******************************************************************************/
  7901. struct pt_sysinfo *_pt_request_sysinfo(struct device *dev)
  7902. {
  7903. struct pt_core_data *cd = dev_get_drvdata(dev);
  7904. int rc = 0;
  7905. /* Attempt to get sysinfo if not ready and panel_id is from XY pin */
  7906. if ((cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO) &&
  7907. !cd->sysinfo.ready) {
  7908. rc = pt_hid_output_get_sysinfo_(cd);
  7909. if (rc) {
  7910. pt_debug(cd->dev, DL_ERROR,
  7911. "%s: Error getting sysinfo rc=%d\n",
  7912. __func__, rc);
  7913. }
  7914. }
  7915. if (cd->sysinfo.ready)
  7916. return &cd->sysinfo;
  7917. return NULL;
  7918. }
  7919. /*******************************************************************************
  7920. * FUNCTION: _pt_request_loader_pdata
  7921. *
  7922. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7923. * to request the pointer to the loader platform data
  7924. *
  7925. * RETURN: Pointer to the loader platform data struct
  7926. *
  7927. * PARAMETERS:
  7928. * *dev - pointer to device structure
  7929. ******************************************************************************/
  7930. static struct pt_loader_platform_data *_pt_request_loader_pdata(
  7931. struct device *dev)
  7932. {
  7933. struct pt_core_data *cd = dev_get_drvdata(dev);
  7934. return cd->pdata->loader_pdata;
  7935. }
  7936. /*******************************************************************************
  7937. * FUNCTION: _pt_request_start_wd
  7938. *
  7939. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7940. * to request to start the TTDL watchdog
  7941. *
  7942. * RETURN:
  7943. * 0 = success
  7944. * !0 = failure
  7945. *
  7946. * PARAMETERS:
  7947. * *dev - pointer to device structure
  7948. ******************************************************************************/
  7949. static int _pt_request_start_wd(struct device *dev)
  7950. {
  7951. struct pt_core_data *cd = dev_get_drvdata(dev);
  7952. pt_start_wd_timer(cd);
  7953. return 0;
  7954. }
  7955. /*******************************************************************************
  7956. * FUNCTION: _pt_request_stop_wd
  7957. *
  7958. * SUMMARY: Function pointer included in core_cmds to allow other modules
  7959. * to request to stop the TTDL watchdog
  7960. *
  7961. * RETURN:
  7962. * 0 = success
  7963. * !0 = failure
  7964. *
  7965. * PARAMETERS:
  7966. * *dev - pointer to device structure
  7967. ******************************************************************************/
  7968. static int _pt_request_stop_wd(struct device *dev)
  7969. {
  7970. struct pt_core_data *cd = dev_get_drvdata(dev);
  7971. pt_stop_wd_timer(cd);
  7972. return 0;
  7973. }
  7974. /*******************************************************************************
  7975. * FUNCTION: pt_pip2_launch_app
  7976. *
  7977. * SUMMARY: Sends the PIP2 EXECUTE command to launch the APP and then wait for
  7978. * the FW reset sentinel to indicate the function succeeded.
  7979. *
  7980. * NOTE: Calling this function when the DUT is in Application mode WILL result
  7981. * in a timeout delay and with the DUT being reset with an XRES.
  7982. *
  7983. * RETURN:
  7984. * 0 = success
  7985. * !0 = failure
  7986. *
  7987. * PARAMETERS:
  7988. * *cd - pointer to core data structure
  7989. * protect - flag to call protected or non-protected
  7990. ******************************************************************************/
  7991. static int pt_pip2_launch_app(struct device *dev, int protect)
  7992. {
  7993. struct pt_core_data *cd = dev_get_drvdata(dev);
  7994. u16 actual_read_len;
  7995. u16 tmp_startup_status = cd->startup_status;
  7996. u8 read_buf[12];
  7997. u8 status;
  7998. int time = 0;
  7999. int rc = 0;
  8000. mutex_lock(&cd->system_lock);
  8001. cd->startup_status = STARTUP_STATUS_START;
  8002. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  8003. mutex_unlock(&cd->system_lock);
  8004. rc = _pt_request_pip2_send_cmd(dev, protect,
  8005. PIP2_CMD_ID_EXECUTE, NULL, 0, read_buf,
  8006. &actual_read_len);
  8007. if (rc) {
  8008. pt_debug(dev, DL_ERROR,
  8009. "%s: PIP2 EXECUTE cmd failed rc = %d\n",
  8010. __func__, rc);
  8011. } else {
  8012. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  8013. /* Test for no or invalid image in FLASH, no point to reset */
  8014. if (status == PIP2_RSP_ERR_INVALID_IMAGE) {
  8015. rc = status;
  8016. goto exit;
  8017. }
  8018. /* Any other boot failure */
  8019. if (status != 0) {
  8020. pt_debug(dev, DL_ERROR,
  8021. "%s: FW did not EXECUTE, status = %d\n",
  8022. __func__, status);
  8023. rc = status;
  8024. }
  8025. }
  8026. if (rc) {
  8027. pt_debug(dev, DL_ERROR,
  8028. "%s: Failed to launch APP, XRES DUT rc = %d\n",
  8029. __func__, rc);
  8030. goto exit;
  8031. }
  8032. while ((cd->startup_status == STARTUP_STATUS_START) && time < 240) {
  8033. msleep(20);
  8034. pt_debug(cd->dev, DL_INFO, "%s: wait for %d for enum=0x%04X\n",
  8035. __func__, time, cd->startup_status);
  8036. time += 20;
  8037. }
  8038. if (cd->startup_status == STARTUP_STATUS_START) {
  8039. pt_debug(cd->dev, DL_WARN,
  8040. "%s: TMO waiting for FW reset sentinel\n", __func__);
  8041. rc = -ETIME;
  8042. }
  8043. exit:
  8044. if (cd->startup_status == STARTUP_STATUS_START) {
  8045. /* Reset to original state because we could be stuck in BL */
  8046. mutex_lock(&cd->system_lock);
  8047. cd->startup_status = tmp_startup_status;
  8048. mutex_unlock(&cd->system_lock);
  8049. }
  8050. return rc;
  8051. }
  8052. /*******************************************************************************
  8053. * FUNCTION: _pt_request_pip2_launch_app
  8054. *
  8055. * SUMMARY: Calls pt_pip2_launch_app() when configured to. A small delay is
  8056. * inserted to ensure the reset has allowed the BL reset sentinel to be
  8057. * consumed.
  8058. *
  8059. * RETURN:
  8060. * 0 = success
  8061. * !0 = failure
  8062. *
  8063. * PARAMETERS:
  8064. * *cd - pointer to core data structure
  8065. ******************************************************************************/
  8066. static int _pt_request_pip2_launch_app(struct device *dev, int protect)
  8067. {
  8068. return pt_pip2_launch_app(dev, protect);
  8069. }
  8070. /*******************************************************************************
  8071. * FUNCTION: _pt_request_wait_for_enum_state
  8072. *
  8073. * SUMMARY: Loops for up to timeout waiting for the startup_status to reach
  8074. * the state passed in or STARTUP_STATUS_COMPLETE whichever comes first
  8075. *
  8076. * RETURN:
  8077. * 0 = success
  8078. * !0 = failure
  8079. *
  8080. * PARAMETERS:
  8081. * *cd - pointer to core data structure
  8082. * timeout - timeout for how long to wait
  8083. * state - enum state to wait for
  8084. ******************************************************************************/
  8085. static int _pt_request_wait_for_enum_state(struct device *dev, int timeout,
  8086. int state)
  8087. {
  8088. int rc = 0;
  8089. int t;
  8090. struct pt_core_data *cd = dev_get_drvdata(dev);
  8091. t = wait_event_timeout(cd->wait_q,
  8092. (cd->startup_status & state) || (cd->startup_status & 0x0100),
  8093. msecs_to_jiffies(timeout));
  8094. if (IS_TMO(t)) {
  8095. pt_debug(cd->dev, DL_ERROR,
  8096. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8097. __func__, state, timeout);
  8098. pt_debug(cd->dev, DL_WARN,
  8099. "%s: enum state reached 0x%04X\n",
  8100. __func__, cd->startup_status);
  8101. rc = -ETIME;
  8102. } else if (cd->startup_status & state) {
  8103. pt_debug(cd->dev, DL_INFO,
  8104. "%s: Enum state reached: enum=0x%04X in %dms\n",
  8105. __func__, cd->startup_status,
  8106. (t == 1) ? timeout : (timeout - jiffies_to_msecs(t)));
  8107. } else {
  8108. if (t == 1) {
  8109. pt_debug(
  8110. cd->dev, DL_ERROR,
  8111. "%s: TMO waiting for enum state 0x%04X in %dms\n",
  8112. __func__, state, timeout);
  8113. rc = -ETIME;
  8114. } else {
  8115. pt_debug(
  8116. cd->dev, DL_ERROR,
  8117. "%s: Enum state 0x%04X not reached in %dms\n",
  8118. __func__, state, timeout - jiffies_to_msecs(t));
  8119. rc = -EINVAL;
  8120. }
  8121. pt_debug(cd->dev, DL_INFO, "%s: enum state reached 0x%04X\n",
  8122. __func__, cd->startup_status);
  8123. }
  8124. return rc;
  8125. }
  8126. /*******************************************************************************
  8127. * FUNCTION: pt_core_wake_device_from_deep_sleep_
  8128. *
  8129. * SUMMARY: Call the set_power function and set the DUT to wake up from
  8130. * deep sleep.
  8131. *
  8132. * RETURN:
  8133. * 0 = success
  8134. * !0 = error
  8135. *
  8136. * PARAMETERS:
  8137. * *cd - pointer to core data
  8138. ******************************************************************************/
  8139. static int pt_core_wake_device_from_deep_sleep_(
  8140. struct pt_core_data *cd)
  8141. {
  8142. int rc;
  8143. rc = pt_hid_cmd_set_power_(cd, HID_POWER_ON);
  8144. if (rc)
  8145. rc = -EAGAIN;
  8146. /* Prevent failure on sequential wake/sleep requests from OS */
  8147. msleep(20);
  8148. return rc;
  8149. }
  8150. /*******************************************************************************
  8151. * FUNCTION: pt_core_wake_device_from_easy_wake_
  8152. *
  8153. * SUMMARY: Wake up device from Easy-Wake state.
  8154. *
  8155. * RETURN:
  8156. * 0 = success
  8157. * !0 = failure
  8158. *
  8159. * PARAMETERS:
  8160. * *cd - pointer to core data
  8161. ******************************************************************************/
  8162. static int pt_core_wake_device_from_easy_wake_(struct pt_core_data *cd)
  8163. {
  8164. int rc = 0;
  8165. mutex_lock(&cd->system_lock);
  8166. cd->wait_until_wake = 1;
  8167. mutex_unlock(&cd->system_lock);
  8168. wake_up(&cd->wait_q);
  8169. msleep(20);
  8170. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8171. return rc;
  8172. }
  8173. /*******************************************************************************
  8174. * FUNCTION: pt_restore_parameters_
  8175. *
  8176. * SUMMARY: This function sends all RAM parameters stored in the linked list
  8177. * back to the DUT
  8178. *
  8179. * RETURN:
  8180. * 0 = success
  8181. * !0 = failure
  8182. *
  8183. * PARAMETERS:
  8184. * *cd - pointer the core data structure
  8185. ******************************************************************************/
  8186. static int pt_restore_parameters_(struct pt_core_data *cd)
  8187. {
  8188. struct param_node *param;
  8189. int rc = 0;
  8190. if (!(cd->cpdata->flags & PT_CORE_FLAG_RESTORE_PARAMETERS))
  8191. goto exit;
  8192. spin_lock(&cd->spinlock);
  8193. list_for_each_entry(param, &cd->param_list, node) {
  8194. spin_unlock(&cd->spinlock);
  8195. pt_debug(cd->dev, DL_INFO, "%s: Parameter id:%d value:%d\n",
  8196. __func__, param->id, param->value);
  8197. rc = pt_pip_set_param_(cd, param->id,
  8198. param->value, param->size);
  8199. if (rc)
  8200. goto exit;
  8201. spin_lock(&cd->spinlock);
  8202. }
  8203. spin_unlock(&cd->spinlock);
  8204. exit:
  8205. return rc;
  8206. }
  8207. /*******************************************************************************
  8208. * FUNCTION: pt_pip2_exit_bl_
  8209. *
  8210. * SUMMARY: Attempt to exit the BL and run the application, taking into account
  8211. * a DUT that may not have flash and will need FW to be loaded into RAM
  8212. *
  8213. * RETURN:
  8214. * 0 = success
  8215. * !0 = failure
  8216. *
  8217. * PARAMETERS:
  8218. * *cd - pointer the core data structure
  8219. * *status_str - pointer to optional status string buffer
  8220. * buf_size - size of status_str buffer
  8221. ******************************************************************************/
  8222. int pt_pip2_exit_bl_(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8223. {
  8224. int rc;
  8225. int wait_time = 0;
  8226. u8 mode = PT_MODE_UNKNOWN;
  8227. bool load_status_str = false;
  8228. /*
  8229. * Below function has protective call to ensure no enum is still on
  8230. * going, while this kind of protection should be applied widely in
  8231. * future (TODO).
  8232. */
  8233. rc = pt_pip2_get_mode_sysmode(cd, &mode, NULL);
  8234. if (status_str && buf_size <= 50)
  8235. load_status_str = true;
  8236. if (mode == PT_MODE_BOOTLOADER) {
  8237. if (cd->flashless_dut == 1) {
  8238. rc = pt_hw_hard_reset(cd);
  8239. } else {
  8240. rc = pt_pip2_launch_app(cd->dev,
  8241. PT_CORE_CMD_UNPROTECTED);
  8242. if (rc == PIP2_RSP_ERR_INVALID_IMAGE) {
  8243. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8244. __func__, "Invalid image in FLASH rc", rc);
  8245. } else if (rc) {
  8246. pt_debug(cd->dev, DL_ERROR, "%s: %s = %d\n",
  8247. __func__, "Failed to launch app rc", rc);
  8248. }
  8249. }
  8250. if (!rc) {
  8251. if (cd->flashless_dut == 1) {
  8252. /* Wait for BL to complete before enum */
  8253. rc = _pt_request_wait_for_enum_state(cd->dev,
  8254. 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  8255. if (rc && load_status_str) {
  8256. strlcpy(status_str, "No FW sentinel after BL",
  8257. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8258. goto exit;
  8259. }
  8260. }
  8261. /*
  8262. * If the host wants to interact with the FW or do a
  8263. * forced calibration, the FW must be out of BOOT mode
  8264. * and the system information must have been retrieved.
  8265. * Reaching the FW_OUT_OF_BOOT state guarantees both.
  8266. * If, however, the enumeration does not reach this
  8267. * point, the DUT may still be in APP mode so test
  8268. * for all conditions.
  8269. */
  8270. rc = _pt_request_wait_for_enum_state(cd->dev, 4500,
  8271. STARTUP_STATUS_FW_OUT_OF_BOOT);
  8272. if (!rc || cd->startup_status >=
  8273. STARTUP_STATUS_FW_RESET_SENTINEL) {
  8274. mutex_lock(&cd->system_lock);
  8275. cd->mode = PT_MODE_OPERATIONAL;
  8276. mutex_unlock(&cd->system_lock);
  8277. }
  8278. if (rc) {
  8279. pt_debug(cd->dev, DL_WARN, "%s: %s: 0x%04X\n",
  8280. __func__, "Failed to enum with DUT",
  8281. cd->startup_status);
  8282. if (load_status_str && !(cd->startup_status &
  8283. STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  8284. strlcpy(status_str, "FW Stuck in Boot mode",
  8285. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8286. goto exit;
  8287. }
  8288. }
  8289. /*
  8290. * The coming FW sentinel could wake up the event
  8291. * queue, which has chance to be taken by next command
  8292. * wrongly. Following delay is a workaround to avoid
  8293. * this issue for most situations.
  8294. */
  8295. msleep(20);
  8296. pt_start_wd_timer(cd);
  8297. }
  8298. if (load_status_str) {
  8299. if (rc == PIP2_RSP_ERR_INVALID_IMAGE)
  8300. strlcpy(status_str, "Failed - Invalid image in FLASH",
  8301. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8302. else if (!rc)
  8303. strlcpy(status_str, "Entered APP from BL mode",
  8304. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8305. else
  8306. strlcpy(status_str, "Failed to enter APP from BL mode",
  8307. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8308. }
  8309. } else if (mode == PT_MODE_OPERATIONAL) {
  8310. mutex_lock(&cd->system_lock);
  8311. cd->mode = mode;
  8312. mutex_unlock(&cd->system_lock);
  8313. rc = pt_poll_for_fw_exit_boot_mode(cd, 1500, &wait_time);
  8314. if (load_status_str) {
  8315. if (!rc)
  8316. strlcpy(status_str, "Already in APP mode",
  8317. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8318. else
  8319. strlcpy(status_str, "Already in APP mode - FW stuck in Boot mode",
  8320. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8321. }
  8322. } else if (rc || mode == PT_MODE_UNKNOWN) {
  8323. mutex_lock(&cd->system_lock);
  8324. cd->mode = mode;
  8325. mutex_unlock(&cd->system_lock);
  8326. if (load_status_str)
  8327. strlcpy(status_str, "Failed to determine active mode",
  8328. sizeof(*status_str)*PT_STATUS_STR_LEN);
  8329. }
  8330. exit:
  8331. if (!rc)
  8332. pt_start_wd_timer(cd);
  8333. return rc;
  8334. }
  8335. /*******************************************************************************
  8336. * FUNCTION: pt_pip2_exit_bl
  8337. *
  8338. * SUMMARY: Wrapper function for _pt_pip2_exit_bl that guarantees exclusive
  8339. * access.
  8340. *
  8341. * RETURN:
  8342. * 0 = success
  8343. * !0 = failure
  8344. *
  8345. * PARAMETERS:
  8346. * *cd - pointer the core data structure
  8347. * *status_str - pointer to optional status string buffer
  8348. * buf_size - size of status_str buffer
  8349. ******************************************************************************/
  8350. int pt_pip2_exit_bl(struct pt_core_data *cd, u8 *status_str, int buf_size)
  8351. {
  8352. int rc;
  8353. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8354. if (rc < 0) {
  8355. pt_debug(cd->dev, DL_ERROR,
  8356. "%s: fail get exclusive ex=%p own=%p\n", __func__,
  8357. cd->exclusive_dev, cd->dev);
  8358. return rc;
  8359. }
  8360. rc = pt_pip2_exit_bl_(cd, status_str, buf_size);
  8361. if (release_exclusive(cd, cd->dev) < 0)
  8362. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8363. __func__);
  8364. return rc;
  8365. }
  8366. /*******************************************************************************
  8367. * FUNCTION: _fast_startup
  8368. *
  8369. * SUMMARY: Perform fast startup after resume device by power on/off stratergy.
  8370. *
  8371. * RETURN:
  8372. * 0 = success
  8373. * !0 = failure
  8374. *
  8375. * PARAMETERS:
  8376. * *cd - pointer the core data structure
  8377. ******************************************************************************/
  8378. static int _fast_startup(struct pt_core_data *cd)
  8379. {
  8380. int retry = PT_CORE_STARTUP_RETRY_COUNT;
  8381. int rc = 0;
  8382. u8 mode = PT_MODE_UNKNOWN;
  8383. struct pt_hid_desc hid_desc;
  8384. int wait_time = 0;
  8385. memset(&hid_desc, 0, sizeof(hid_desc));
  8386. reset:
  8387. if (retry != PT_CORE_STARTUP_RETRY_COUNT)
  8388. pt_debug(cd->dev, DL_INFO, "%s: Retry %d\n", __func__,
  8389. PT_CORE_STARTUP_RETRY_COUNT - retry);
  8390. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8391. pt_debug(cd->dev, DL_INFO, "%s: PIP1 Enumeration start\n",
  8392. __func__);
  8393. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8394. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8395. if (rc < 0) {
  8396. pt_debug(cd->dev, DL_ERROR,
  8397. "%s: Error on getting HID descriptor r=%d\n",
  8398. __func__, rc);
  8399. if (retry--)
  8400. goto reset;
  8401. goto exit;
  8402. }
  8403. cd->mode = pt_get_mode(cd, &hid_desc);
  8404. if (cd->mode == PT_MODE_BOOTLOADER) {
  8405. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8406. __func__);
  8407. rc = pt_hid_output_bl_launch_app_(cd);
  8408. if (rc < 0) {
  8409. pt_debug(cd->dev, DL_ERROR,
  8410. "%s: Error on launch app r=%d\n",
  8411. __func__, rc);
  8412. if (retry--)
  8413. goto reset;
  8414. goto exit;
  8415. }
  8416. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8417. if (rc < 0) {
  8418. pt_debug(cd->dev, DL_ERROR,
  8419. "%s: Error on getting HID descriptor r=%d\n",
  8420. __func__, rc);
  8421. if (retry--)
  8422. goto reset;
  8423. goto exit;
  8424. }
  8425. cd->mode = pt_get_mode(cd, &hid_desc);
  8426. if (cd->mode == PT_MODE_BOOTLOADER) {
  8427. if (retry--)
  8428. goto reset;
  8429. goto exit;
  8430. }
  8431. }
  8432. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8433. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8434. } else {
  8435. pt_debug(cd->dev, DL_INFO, "%s: PIP2 Enumeration start\n",
  8436. __func__);
  8437. if (retry == PT_CORE_STARTUP_RETRY_COUNT) {
  8438. /* Wait for any sentinel before first try */
  8439. rc = _pt_request_wait_for_enum_state(
  8440. cd->dev, 150,
  8441. STARTUP_STATUS_BL_RESET_SENTINEL |
  8442. STARTUP_STATUS_FW_RESET_SENTINEL);
  8443. if (rc)
  8444. pt_debug(cd->dev, DL_ERROR,
  8445. "%s: No Sentinel detected rc = %d\n",
  8446. __func__, rc);
  8447. } else
  8448. pt_flush_bus_if_irq_asserted(cd,
  8449. PT_FLUSH_BUS_BASED_ON_LEN);
  8450. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  8451. if (rc) {
  8452. pt_debug(cd->dev, DL_ERROR,
  8453. "%s: Get mode failed, mode unknown\n",
  8454. __func__);
  8455. mode = PT_MODE_UNKNOWN;
  8456. }
  8457. cd->mode = mode;
  8458. if (cd->mode == PT_MODE_BOOTLOADER) {
  8459. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8460. __func__);
  8461. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  8462. if (rc) {
  8463. pt_debug(cd->dev, DL_ERROR,
  8464. "%s Failed to exit bootloader\n",
  8465. __func__);
  8466. msleep(50);
  8467. rc = -ENODEV;
  8468. if (retry--)
  8469. goto reset;
  8470. goto exit;
  8471. } else {
  8472. pt_debug(cd->dev, DL_INFO,
  8473. "%s: Exit bootloader successfully\n",
  8474. __func__);
  8475. }
  8476. if (cd->mode != PT_MODE_OPERATIONAL) {
  8477. pt_debug(cd->dev, DL_WARN,
  8478. "%s: restore mode failure mode = %d\n",
  8479. __func__, cd->mode);
  8480. if (retry--)
  8481. goto reset;
  8482. goto exit;
  8483. }
  8484. }
  8485. cd->startup_status |= STARTUP_STATUS_GET_DESC;
  8486. }
  8487. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  8488. rc = _pt_poll_for_fw_exit_boot_mode(cd, 500, &wait_time);
  8489. if (!rc) {
  8490. cd->startup_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  8491. pt_debug(cd->dev, DL_WARN,
  8492. "%s: Exit FW BOOT Mode after %dms\n",
  8493. __func__, wait_time);
  8494. } else {
  8495. pt_debug(cd->dev, DL_WARN,
  8496. "%s: FW stuck in BOOT Mode after %dms\n",
  8497. __func__, wait_time);
  8498. goto exit;
  8499. }
  8500. if (!cd->sysinfo.ready) {
  8501. rc = pt_hid_output_get_sysinfo_(cd);
  8502. if (rc) {
  8503. pt_debug(cd->dev, DL_ERROR,
  8504. "%s: Error on getting sysinfo r=%d\n",
  8505. __func__, rc);
  8506. if (retry--)
  8507. goto reset;
  8508. goto exit;
  8509. }
  8510. }
  8511. cd->startup_status |= STARTUP_STATUS_GET_SYS_INFO;
  8512. rc = pt_restore_parameters_(cd);
  8513. if (rc)
  8514. pt_debug(cd->dev, DL_ERROR,
  8515. "%s: failed to restore parameters rc=%d\n",
  8516. __func__, rc);
  8517. else
  8518. cd->startup_status |= STARTUP_STATUS_RESTORE_PARM;
  8519. exit:
  8520. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  8521. return rc;
  8522. }
  8523. /*******************************************************************************
  8524. * FUNCTION: pt_core_poweron_device_
  8525. *
  8526. * SUMMARY: Power on device, enable IRQ, and then perform a fast startup.
  8527. *
  8528. * RETURN:
  8529. * 0 = success
  8530. * !0 = failure
  8531. *
  8532. * PARAMETERS:
  8533. * *cd - pointer to core data
  8534. ******************************************************************************/
  8535. static int pt_core_poweron_device_(struct pt_core_data *cd)
  8536. {
  8537. struct device *dev = cd->dev;
  8538. int rc = 0;
  8539. /*
  8540. * After power on action, the chip can general FW sentinel. It can
  8541. * trigger an enumeration without hid_reset_cmd_state flag. Since the
  8542. * _fast_startup() can perform a quick enumeration too, here doesn't
  8543. * need another enumeration.
  8544. */
  8545. mutex_lock(&cd->system_lock);
  8546. cd->startup_status = STARTUP_STATUS_START;
  8547. cd->hid_reset_cmd_state = 1;
  8548. mutex_unlock(&cd->system_lock);
  8549. rc = cd->cpdata->power(cd->cpdata, 1, dev, 0);
  8550. if (rc < 0) {
  8551. pt_debug(dev, DL_ERROR, "%s: HW Power up fails r=%d\n",
  8552. __func__, rc);
  8553. goto exit;
  8554. }
  8555. if (!cd->irq_enabled) {
  8556. cd->irq_enabled = true;
  8557. enable_irq(cd->irq);
  8558. }
  8559. /* TBD: following function doesn't update startup_status */
  8560. rc = _fast_startup(cd);
  8561. exit:
  8562. return rc;
  8563. }
  8564. /*******************************************************************************
  8565. * FUNCTION: pt_core_wake_device_from_deep_standby_
  8566. *
  8567. * SUMMARY: Reset device, and then trigger a full enumeration.
  8568. *
  8569. * RETURN:
  8570. * 0 = success
  8571. * !0 = failure
  8572. *
  8573. * PARAMETERS:
  8574. * *cd - pointer to core data
  8575. ******************************************************************************/
  8576. static int pt_core_wake_device_from_deep_standby_(struct pt_core_data *cd)
  8577. {
  8578. int rc;
  8579. rc = pt_dut_reset_and_wait(cd);
  8580. if (rc < 0) {
  8581. pt_debug(cd->dev, DL_ERROR, "%s: Error on h/w reset r=%d\n",
  8582. __func__, rc);
  8583. goto exit;
  8584. }
  8585. rc = _fast_startup(cd);
  8586. exit:
  8587. return rc;
  8588. }
  8589. /*******************************************************************************
  8590. * FUNCTION: pt_core_easywake_off_
  8591. *
  8592. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8593. * the configuration in the core platform data structure.
  8594. *
  8595. * RETURN:
  8596. * 0 = success
  8597. * !0 = failure
  8598. *
  8599. * PARAMETERS:
  8600. * *cd - pointer to core data
  8601. ******************************************************************************/
  8602. static int pt_core_easywake_off_(struct pt_core_data *cd)
  8603. {
  8604. int rc = 0;
  8605. mutex_lock(&cd->system_lock);
  8606. if (cd->sleep_state == SS_SLEEP_OFF) {
  8607. mutex_unlock(&cd->system_lock);
  8608. pt_debug(cd->dev, DL_INFO,
  8609. "%s - %d skip wakeoff\n", __func__, cd->sleep_state);
  8610. return 1;
  8611. }
  8612. mutex_unlock(&cd->system_lock);
  8613. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8614. if (IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  8615. rc = pt_core_wake_device_from_easy_wake_(cd);
  8616. if (rc < 0)
  8617. pt_debug(cd->dev, DL_ERROR,
  8618. "%s - %d failed %d\n", __func__, rc);
  8619. }
  8620. pt_start_wd_timer(cd);
  8621. return rc;
  8622. }
  8623. /*******************************************************************************
  8624. * FUNCTION: pt_core_easywake_off
  8625. *
  8626. * SUMMARY: Protected call to pt_core_easywake_off by exclusive access to the DUT.
  8627. *
  8628. * RETURN:
  8629. * 0 = success
  8630. * !0 = failure
  8631. *
  8632. * PARAMETERS:
  8633. * *cd - pointer to core data
  8634. ******************************************************************************/
  8635. static int pt_core_easywake_off(struct pt_core_data *cd)
  8636. {
  8637. int rc;
  8638. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8639. if (rc < 0) {
  8640. pt_debug(cd->dev, DL_ERROR,
  8641. "%s: fail get exclusive ex=%p own=%p\n",
  8642. __func__, cd->exclusive_dev, cd->dev);
  8643. return rc;
  8644. }
  8645. rc = pt_core_easywake_off_(cd);
  8646. if (release_exclusive(cd, cd->dev) < 0)
  8647. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8648. __func__);
  8649. else
  8650. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8651. __func__);
  8652. return rc;
  8653. }
  8654. /*******************************************************************************
  8655. * FUNCTION: pt_core_wake_
  8656. *
  8657. * SUMMARY: Resume the device with a power on or wake from deep sleep based on
  8658. * the configuration in the core platform data structure.
  8659. *
  8660. * RETURN:
  8661. * 0 = success
  8662. * !0 = failure
  8663. *
  8664. * PARAMETERS:
  8665. * *cd - pointer to core data
  8666. ******************************************************************************/
  8667. static int pt_core_wake_(struct pt_core_data *cd)
  8668. {
  8669. int rc = 0;
  8670. mutex_lock(&cd->system_lock);
  8671. if (cd->sleep_state == SS_SLEEP_ON || cd->sleep_state == SS_SLEEP_NONE) {
  8672. cd->sleep_state = SS_WAKING;
  8673. } else {
  8674. mutex_unlock(&cd->system_lock);
  8675. pt_debug(cd->dev, DL_INFO,
  8676. "%s - skip wake sleep state %d\n", __func__, cd->sleep_state);
  8677. return 1;
  8678. }
  8679. mutex_unlock(&cd->system_lock);
  8680. if (!(cd->cpdata->flags & PT_CORE_FLAG_SKIP_RESUME)) {
  8681. if (cd->cpdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  8682. pt_debug(cd->dev, DL_INFO,
  8683. "%s: Entering into poweron mode:\n", __func__);
  8684. rc = pt_core_poweron_device_(cd);
  8685. if (rc < 0)
  8686. pr_err("%s: Poweron error detected: rc=%d\n",
  8687. __func__, rc);
  8688. }
  8689. else if (cd->cpdata->flags & PT_CORE_FLAG_DEEP_STANDBY)
  8690. rc = pt_core_wake_device_from_deep_standby_(cd);
  8691. else /* Default action to exit DeepSleep */
  8692. rc = pt_core_wake_device_from_deep_sleep_(cd);
  8693. }
  8694. mutex_lock(&cd->system_lock);
  8695. cd->sleep_state = SS_SLEEP_OFF;
  8696. mutex_unlock(&cd->system_lock);
  8697. pt_start_wd_timer(cd);
  8698. return rc;
  8699. }
  8700. /*******************************************************************************
  8701. * FUNCTION: pt_core_wake_
  8702. *
  8703. * SUMMARY: Protected call to pt_core_wake_ by exclusive access to the DUT.
  8704. *
  8705. * RETURN:
  8706. * 0 = success
  8707. * !0 = failure
  8708. *
  8709. * PARAMETERS:
  8710. * *cd - pointer to core data
  8711. ******************************************************************************/
  8712. static int pt_core_wake(struct pt_core_data *cd)
  8713. {
  8714. int rc;
  8715. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  8716. if (rc < 0) {
  8717. pt_debug(cd->dev, DL_ERROR,
  8718. "%s: fail get exclusive ex=%p own=%p\n",
  8719. __func__, cd->exclusive_dev, cd->dev);
  8720. return rc;
  8721. }
  8722. rc = pt_core_wake_(cd);
  8723. if (release_exclusive(cd, cd->dev) < 0)
  8724. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  8725. __func__);
  8726. else
  8727. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  8728. __func__);
  8729. return rc;
  8730. }
  8731. /*******************************************************************************
  8732. * FUNCTION: pt_get_ic_crc_
  8733. *
  8734. * SUMMARY: This function retrieves the config block CRC
  8735. *
  8736. * NOTE: The post condition of calling this function will be that the DUT will
  8737. * be in SCANNINING mode if no failures occur
  8738. *
  8739. * RETURN:
  8740. * 0 = success
  8741. * !0 = failure
  8742. *
  8743. * PARAMETERS:
  8744. * *cd - pointer the core data structure
  8745. * ebid - enumerated block ID
  8746. ******************************************************************************/
  8747. static int pt_get_ic_crc_(struct pt_core_data *cd, u8 ebid)
  8748. {
  8749. struct pt_sysinfo *si = &cd->sysinfo;
  8750. int rc = 0;
  8751. u8 status;
  8752. u16 calculated_crc = 0;
  8753. u16 stored_crc = 0;
  8754. rc = pt_pip_suspend_scanning_(cd);
  8755. if (rc)
  8756. goto error;
  8757. rc = pt_pip_verify_config_block_crc_(cd, ebid, &status,
  8758. &calculated_crc, &stored_crc);
  8759. if (rc)
  8760. goto exit;
  8761. if (status) {
  8762. rc = -EINVAL;
  8763. goto exit;
  8764. }
  8765. si->ttconfig.crc = stored_crc;
  8766. exit:
  8767. pt_pip_resume_scanning_(cd);
  8768. error:
  8769. pt_debug(cd->dev, DL_INFO,
  8770. "%s: CRC: ebid:%d, calc:0x%04X, stored:0x%04X, rc=%d\n",
  8771. __func__, ebid, calculated_crc, stored_crc, rc);
  8772. return rc;
  8773. }
  8774. /*******************************************************************************
  8775. * FUNCTION: pt_pip2_read_gpio
  8776. *
  8777. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and stores the 32 gpio
  8778. * bits into the passed in variable
  8779. *
  8780. * NOTE: PIP2 READ_GPIO command is only supported in bootloader
  8781. *
  8782. * RETURN:
  8783. * 0 = success
  8784. * !0 = failure
  8785. *
  8786. * PARAMETERS:
  8787. * *dev - pointer to device structure
  8788. * *status - pointer to where the command response status is stored
  8789. * *gpio - pointer to device attributes structure
  8790. ******************************************************************************/
  8791. static int pt_pip2_read_gpio(struct device *dev, u8 *status, u32 *gpio)
  8792. {
  8793. int rc = 0;
  8794. u16 actual_read_len;
  8795. u8 read_buf[12];
  8796. u8 tmp_status = 0;
  8797. u8 index = PIP2_RESP_STATUS_OFFSET;
  8798. memset(read_buf, 0, ARRAY_SIZE(read_buf));
  8799. rc = _pt_request_pip2_send_cmd(dev,
  8800. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_READ_GPIO,
  8801. NULL, 0, read_buf, &actual_read_len);
  8802. if (rc) {
  8803. pt_debug(dev, DL_ERROR,
  8804. "%s: Failed to send PIP2 READ_GPIO cmd\n", __func__);
  8805. rc = -ECOMM;
  8806. } else {
  8807. tmp_status = read_buf[index];
  8808. }
  8809. if (status)
  8810. *status = tmp_status;
  8811. if (!rc && gpio && (tmp_status == 0)) {
  8812. *gpio = ((read_buf[index + 4] << 24) |
  8813. (read_buf[index + 3] << 16) |
  8814. (read_buf[index + 2] << 8) |
  8815. (read_buf[index + 1]));
  8816. }
  8817. return rc;
  8818. }
  8819. /*******************************************************************************
  8820. * FUNCTION: _pt_pip2_get_panel_id_by_gpio
  8821. *
  8822. * SUMMARY: Wrapper function to call pt_pip2_read_gpio() to get panel ID
  8823. *
  8824. * RETURN:
  8825. * 0 = success
  8826. * !0 = failure
  8827. *
  8828. * PARAMETERS:
  8829. * *cd - pointer the core data structure
  8830. * *pid - pointer to store panel ID
  8831. ******************************************************************************/
  8832. static int _pt_pip2_get_panel_id_by_gpio(struct pt_core_data *cd, u8 *pid)
  8833. {
  8834. u32 gpio_value = 0;
  8835. u8 status = 0;
  8836. u8 panel_id = PANEL_ID_NOT_ENABLED;
  8837. int rc = 0;
  8838. if (!pid)
  8839. return -ENOMEM;
  8840. rc = pt_pip2_read_gpio(cd->dev, &status, &gpio_value);
  8841. if (!rc) {
  8842. if (status == 0) {
  8843. panel_id = (gpio_value & PT_PANEL_ID_BITMASK) >>
  8844. PT_PANEL_ID_SHIFT;
  8845. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X %s=0x%08X\n",
  8846. __func__,
  8847. "BL mode PID", panel_id, "gpio", gpio_value);
  8848. *pid = panel_id;
  8849. } else {
  8850. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8851. __func__,
  8852. "BL read gpio failed status", status);
  8853. }
  8854. } else {
  8855. pt_debug(cd->dev, DL_ERROR, "%s: %s=%d\n",
  8856. __func__,
  8857. "BL read gpio failed status", status);
  8858. }
  8859. return rc;
  8860. }
  8861. /*******************************************************************************
  8862. * FUNCTION: pt_enum_with_dut_
  8863. *
  8864. * SUMMARY: This function does the full enumeration of the DUT with TTDL.
  8865. * The core data (cd) startup_status will store, as a bitmask, each
  8866. * state of the enumeration process. The startup will be attempted
  8867. * PT_CORE_STARTUP_RETRY_COUNT times before giving up.
  8868. *
  8869. * RETURN:
  8870. * 0 = success
  8871. * !0 = failure
  8872. *
  8873. * PARAMETERS:
  8874. * *cd - pointer the core data structure
  8875. * reset - Flag to reset the DUT before attempting to enumerate
  8876. * *status - poionter to store the enum status bitmask flags
  8877. ******************************************************************************/
  8878. static int pt_enum_with_dut_(struct pt_core_data *cd, bool reset,
  8879. u32 *enum_status)
  8880. {
  8881. int try = 1;
  8882. int rc = 0;
  8883. int wait_time = 0;
  8884. bool detected = false;
  8885. u8 return_data[8];
  8886. u8 mode = PT_MODE_UNKNOWN;
  8887. u8 pid = PANEL_ID_NOT_ENABLED;
  8888. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  8889. struct pt_hid_desc hid_desc;
  8890. memset(&hid_desc, 0, sizeof(hid_desc));
  8891. #ifdef TTHE_TUNER_SUPPORT
  8892. tthe_print(cd, NULL, 0, "enter startup");
  8893. #endif
  8894. pt_debug(cd->dev, DL_INFO, "%s: Start enum... 0x%04X, reset=%d\n",
  8895. __func__, cd->startup_status, reset);
  8896. pt_stop_wd_timer(cd);
  8897. reset:
  8898. if (try > 1)
  8899. pt_debug(cd->dev, DL_WARN, "%s: DUT Enum Attempt %d\n",
  8900. __func__, try);
  8901. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  8902. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  8903. pt_debug(cd->dev, DL_INFO,
  8904. "%s: PIP1 Enumeration start\n", __func__);
  8905. /* Only reset the DUT after the first try */
  8906. if (reset || (try > 1)) {
  8907. /*
  8908. * Reset hardware only for Legacy parts. Skip for TT/TC
  8909. * parts because if the FW image was loaded directly
  8910. * to SRAM issueing a reset ill wipe out what was just
  8911. * loaded.
  8912. */
  8913. rc = pt_dut_reset_and_wait(cd);
  8914. if (rc < 0) {
  8915. pt_debug(cd->dev, DL_ERROR,
  8916. "%s: Error on h/w reset r=%d\n",
  8917. __func__, rc);
  8918. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8919. goto reset;
  8920. goto exit;
  8921. }
  8922. /* sleep to allow FW to be launched if available */
  8923. msleep(120);
  8924. }
  8925. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  8926. if (rc < 0) {
  8927. pt_debug(cd->dev, DL_ERROR,
  8928. "%s: Error getting HID Descriptor r=%d\n",
  8929. __func__, rc);
  8930. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8931. goto reset;
  8932. rc = -EIO;
  8933. goto exit;
  8934. }
  8935. detected = true;
  8936. cd->mode = pt_get_mode(cd, &hid_desc);
  8937. /*
  8938. * Most systems do not use an XY pin as the panel_id and so
  8939. * the BL is used to retrieve the panel_id, however for
  8940. * systems that do use an XY pin, the panel_id MUST be
  8941. * retrieved from the system information when running FW
  8942. * (done below) and so this section of code is skipped.
  8943. * Entering the BL here is only needed on XY_PIN systems.
  8944. */
  8945. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  8946. if (cd->mode == PT_MODE_OPERATIONAL) {
  8947. rc = pt_pip_start_bootloader_(cd);
  8948. if (rc < 0) {
  8949. pt_debug(cd->dev, DL_ERROR,
  8950. "%s: Error on start bootloader r=%d\n",
  8951. __func__, rc);
  8952. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8953. goto reset;
  8954. goto exit;
  8955. }
  8956. cd->mode = PT_MODE_BOOTLOADER;
  8957. pt_debug(cd->dev, DL_INFO, "%s: Bootloader mode\n",
  8958. __func__);
  8959. }
  8960. rc = pt_hid_output_bl_get_information_(cd, return_data);
  8961. if (!rc) {
  8962. cd->bl_info.ready = true;
  8963. cd->bl_info.chip_id =
  8964. get_unaligned_le16(&return_data[2]);
  8965. pt_debug(cd->dev, DL_INFO, "%s: chip ID %04X\n",
  8966. __func__, cd->bl_info.chip_id);
  8967. } else {
  8968. pt_debug(cd->dev, DL_ERROR,
  8969. "%s: failed to get chip ID, r=%d\n",
  8970. __func__, rc);
  8971. }
  8972. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  8973. mutex_lock(&cd->system_lock);
  8974. if (!rc) {
  8975. cd->pid_for_loader = pid;
  8976. pt_debug(cd->dev, DL_INFO, "%s: Panel ID: 0x%02X\n",
  8977. __func__, cd->pid_for_loader);
  8978. } else {
  8979. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  8980. pt_debug(cd->dev, DL_WARN,
  8981. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  8982. __func__, cd->pid_for_loader);
  8983. }
  8984. mutex_unlock(&cd->system_lock);
  8985. }
  8986. /* Exit BL due to XY_PIN case or any other cause to be in BL */
  8987. if (cd->mode == PT_MODE_BOOTLOADER) {
  8988. rc = pt_hid_output_bl_launch_app_(cd);
  8989. if (rc < 0) {
  8990. pt_debug(cd->dev, DL_ERROR,
  8991. "%s: Error on launch app r=%d\n",
  8992. __func__, rc);
  8993. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  8994. goto reset;
  8995. rc = -ENODEV;
  8996. goto exit;
  8997. }
  8998. /* sleep to allow FW to be launched if available */
  8999. msleep(120);
  9000. /* Ensure the DUT is now in Application mode */
  9001. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  9002. if (rc < 0) {
  9003. pt_debug(cd->dev, DL_ERROR,
  9004. "%s: Error getting HID Desc r=%d\n",
  9005. __func__, rc);
  9006. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9007. goto reset;
  9008. rc = -EIO;
  9009. goto exit;
  9010. }
  9011. cd->mode = pt_get_mode(cd, &hid_desc);
  9012. if (cd->mode == PT_MODE_BOOTLOADER) {
  9013. pt_debug(cd->dev, DL_WARN,
  9014. "%s: Error confiming exit BL\n",
  9015. __func__);
  9016. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9017. goto reset;
  9018. rc = -ENODEV;
  9019. goto exit;
  9020. }
  9021. }
  9022. pt_debug(cd->dev, DL_INFO, "%s: Operational mode\n", __func__);
  9023. cd->mode = PT_MODE_OPERATIONAL;
  9024. *enum_status |= STARTUP_STATUS_GET_DESC;
  9025. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9026. } else {
  9027. /* Generation is PIP2 Capable */
  9028. pt_debug(cd->dev, DL_INFO,
  9029. "%s: PIP2 Enumeration start\n", __func__);
  9030. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9031. if (rc) {
  9032. pt_debug(cd->dev, DL_ERROR,
  9033. "%s: Get mode failed, mode unknown\n",
  9034. __func__);
  9035. mode = PT_MODE_UNKNOWN;
  9036. } else
  9037. detected = true;
  9038. cd->mode = mode;
  9039. switch (cd->mode) {
  9040. case PT_MODE_OPERATIONAL:
  9041. pt_debug(cd->dev, DL_INFO,
  9042. "%s: Operational mode\n", __func__);
  9043. if (cd->app_pip_ver_ready == false) {
  9044. rc = pt_pip2_get_version_(cd);
  9045. if (!rc)
  9046. cd->app_pip_ver_ready = true;
  9047. else {
  9048. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9049. goto reset;
  9050. goto exit;
  9051. }
  9052. }
  9053. break;
  9054. case PT_MODE_BOOTLOADER:
  9055. pt_debug(cd->dev, DL_INFO,
  9056. "%s: Bootloader mode\n", __func__);
  9057. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  9058. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  9059. mutex_lock(&cd->system_lock);
  9060. if (!rc) {
  9061. cd->pid_for_loader = pid;
  9062. pt_debug(cd->dev, DL_INFO,
  9063. "%s: Panel ID: 0x%02X\n",
  9064. __func__, cd->pid_for_loader);
  9065. } else {
  9066. cd->pid_for_loader =
  9067. PANEL_ID_NOT_ENABLED;
  9068. pt_debug(cd->dev, DL_WARN,
  9069. "%s: Read Failed, disable Panel ID: 0x%02X\n",
  9070. __func__, cd->pid_for_loader);
  9071. }
  9072. mutex_unlock(&cd->system_lock);
  9073. }
  9074. if (cd->bl_pip_ver_ready == false) {
  9075. rc = pt_pip2_get_version_(cd);
  9076. if (!rc)
  9077. cd->bl_pip_ver_ready = true;
  9078. else {
  9079. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9080. goto reset;
  9081. goto exit;
  9082. }
  9083. }
  9084. /*
  9085. * Launch app command will fail in flashless mode.
  9086. * Skip launch app command here to save time for
  9087. * enumeration flow.
  9088. */
  9089. if (cd->flashless_dut)
  9090. goto exit;
  9091. /*
  9092. * pt_pip2_launch_app() is needed here instead of
  9093. * pt_pip2_exit_bl() because exit_bl will cause several
  9094. * hw_resets to occur and the auto BL on a flashless
  9095. * DUT will fail.
  9096. */
  9097. rc = pt_pip2_launch_app(cd->dev,
  9098. PT_CORE_CMD_UNPROTECTED);
  9099. if (rc) {
  9100. pt_debug(cd->dev, DL_ERROR,
  9101. "%s: Error on launch app r=%d\n",
  9102. __func__, rc);
  9103. msleep(50);
  9104. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9105. goto reset;
  9106. goto exit;
  9107. }
  9108. /*
  9109. * IRQ thread can be delayed if the serial log print is
  9110. * enabled. It causes next command to get wrong response
  9111. * Here the delay is to ensure pt_parse_input() to be
  9112. * finished.
  9113. */
  9114. msleep(60);
  9115. /* Check and update the mode */
  9116. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9117. if (rc) {
  9118. pt_debug(cd->dev, DL_ERROR,
  9119. "%s: Get mode failed, mode unknown\n",
  9120. __func__);
  9121. mode = PT_MODE_UNKNOWN;
  9122. }
  9123. cd->mode = mode;
  9124. if (cd->mode == PT_MODE_OPERATIONAL) {
  9125. pt_debug(cd->dev, DL_INFO,
  9126. "%s: Launched to Operational mode\n",
  9127. __func__);
  9128. } else if (cd->mode == PT_MODE_BOOTLOADER) {
  9129. pt_debug(cd->dev, DL_ERROR,
  9130. "%s: Launch failed, Bootloader mode\n",
  9131. __func__);
  9132. goto exit;
  9133. } else if (cd->mode == PT_MODE_UNKNOWN) {
  9134. pt_debug(cd->dev, DL_ERROR,
  9135. "%s: Launch failed, Unknown mode\n",
  9136. __func__);
  9137. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9138. goto reset;
  9139. goto exit;
  9140. }
  9141. if (cd->app_pip_ver_ready == false) {
  9142. rc = pt_pip2_get_version_(cd);
  9143. if (!rc)
  9144. cd->app_pip_ver_ready = true;
  9145. else {
  9146. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9147. goto reset;
  9148. goto exit;
  9149. }
  9150. }
  9151. break;
  9152. default:
  9153. pt_debug(cd->dev, DL_ERROR,
  9154. "%s: Unknown mode\n", __func__);
  9155. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9156. goto reset;
  9157. goto exit;
  9158. }
  9159. *enum_status |= STARTUP_STATUS_GET_DESC;
  9160. }
  9161. pt_init_pip_report_fields(cd);
  9162. *enum_status |= STARTUP_STATUS_GET_RPT_DESC;
  9163. if (!cd->features.easywake)
  9164. cd->easy_wakeup_gesture = PT_CORE_EWG_NONE;
  9165. pt_debug(cd->dev, DL_INFO, "%s: Reading sysinfo\n", __func__);
  9166. rc = pt_hid_output_get_sysinfo_(cd);
  9167. if (rc) {
  9168. pt_debug(cd->dev, DL_ERROR,
  9169. "%s: Error on getting sysinfo r=%d\n", __func__, rc);
  9170. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9171. goto reset;
  9172. goto exit;
  9173. }
  9174. *enum_status |= STARTUP_STATUS_GET_SYS_INFO;
  9175. /* FW cannot handle most PIP cmds when it is still in BOOT mode */
  9176. rc = _pt_poll_for_fw_exit_boot_mode(cd, 10000, &wait_time);
  9177. if (!rc) {
  9178. *enum_status |= STARTUP_STATUS_FW_OUT_OF_BOOT;
  9179. pt_debug(cd->dev, DL_WARN,
  9180. "%s: Exit FW BOOT Mode after %dms\n",
  9181. __func__, wait_time);
  9182. } else {
  9183. pt_debug(cd->dev, DL_WARN,
  9184. "%s: FW stuck in BOOT Mode after %dms\n",
  9185. __func__, wait_time);
  9186. goto exit;
  9187. }
  9188. pt_debug(cd->dev, DL_INFO, "%s pt Prot Version: %d.%d\n",
  9189. __func__,
  9190. cd->sysinfo.ttdata.pip_ver_major,
  9191. cd->sysinfo.ttdata.pip_ver_minor);
  9192. rc = pt_get_ic_crc_(cd, PT_TCH_PARM_EBID);
  9193. if (rc) {
  9194. pt_debug(cd->dev, DL_ERROR,
  9195. "%s: DUT Config block CRC failure rc=%d\n",
  9196. __func__, rc);
  9197. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9198. goto reset;
  9199. } else {
  9200. _pt_get_fw_sys_mode(cd, &sys_mode, NULL);
  9201. if (sys_mode != FW_SYS_MODE_SCANNING) {
  9202. pt_debug(cd->dev, DL_ERROR,
  9203. "%s: scan state: %d, retry: %d\n",
  9204. __func__, sys_mode, try);
  9205. if (try++ < PT_CORE_STARTUP_RETRY_COUNT)
  9206. goto reset;
  9207. } else
  9208. *enum_status |= STARTUP_STATUS_GET_CFG_CRC;
  9209. }
  9210. rc = pt_restore_parameters_(cd);
  9211. if (rc) {
  9212. pt_debug(cd->dev, DL_ERROR,
  9213. "%s: Failed to restore parameters rc=%d\n",
  9214. __func__, rc);
  9215. } else
  9216. *enum_status |= STARTUP_STATUS_RESTORE_PARM;
  9217. call_atten_cb(cd, PT_ATTEN_STARTUP, 0);
  9218. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  9219. cd->startup_retry_count = 0;
  9220. exit:
  9221. /* Generate the HW Version of the connected DUT and store in cd */
  9222. pt_generate_hw_version(cd, cd->hw_version);
  9223. pt_debug(cd->dev, DL_WARN, "%s: HW Version: %s\n", __func__,
  9224. cd->hw_version);
  9225. pt_start_wd_timer(cd);
  9226. if (!detected)
  9227. rc = -ENODEV;
  9228. #ifdef TTHE_TUNER_SUPPORT
  9229. tthe_print(cd, NULL, 0, "exit startup");
  9230. #endif
  9231. pt_debug(cd->dev, DL_WARN,
  9232. "%s: Completed Enumeration rc=%d On Attempt %d\n",
  9233. __func__, rc, try);
  9234. return rc;
  9235. }
  9236. /*******************************************************************************
  9237. * FUNCTION: pt_enum_with_dut
  9238. *
  9239. * SUMMARY: This is the safe function wrapper for pt_enum_with_dut_() by
  9240. * requesting exclusive access around the call.
  9241. *
  9242. * RETURN:
  9243. * 0 = success
  9244. * !0 = failure
  9245. *
  9246. * PARAMETERS:
  9247. * *cd - pointer the core data structure
  9248. * reset - Flag to reset the DUT before attempting to enumerate
  9249. * *status - pointer to store the enum status bitmask flags
  9250. ******************************************************************************/
  9251. static int pt_enum_with_dut(struct pt_core_data *cd, bool reset, u32 *status)
  9252. {
  9253. int rc = 0;
  9254. mutex_lock(&cd->system_lock);
  9255. cd->startup_state = STARTUP_RUNNING;
  9256. mutex_unlock(&cd->system_lock);
  9257. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  9258. if (rc) {
  9259. pt_debug(cd->dev, DL_ERROR,
  9260. "%s: fail get exclusive ex=%p own=%p\n",
  9261. __func__, cd->exclusive_dev, cd->dev);
  9262. goto exit;
  9263. }
  9264. rc = pt_enum_with_dut_(cd, reset, status);
  9265. if (release_exclusive(cd, cd->dev) < 0)
  9266. /* Don't return fail code, mode is already changed. */
  9267. pt_debug(cd->dev, DL_ERROR, "%s: fail to release exclusive\n",
  9268. __func__);
  9269. else
  9270. pt_debug(cd->dev, DL_DEBUG, "%s: pass release exclusive\n",
  9271. __func__);
  9272. exit:
  9273. mutex_lock(&cd->system_lock);
  9274. /* Clear startup state for any tasks waiting for startup completion */
  9275. cd->startup_state = STARTUP_NONE;
  9276. mutex_unlock(&cd->system_lock);
  9277. /* Set STATUS_COMPLETE bit to indicate the status is ready to be read */
  9278. *status |= STARTUP_STATUS_COMPLETE;
  9279. /* Wake the waiters for end of startup */
  9280. wake_up(&cd->wait_q);
  9281. return rc;
  9282. }
  9283. static int add_sysfs_interfaces(struct device *dev);
  9284. static void remove_sysfs_interfaces(struct device *dev);
  9285. static void remove_sysfs_and_modules(struct device *dev);
  9286. static void pt_release_modules(struct pt_core_data *cd);
  9287. static void pt_probe_modules(struct pt_core_data *cd);
  9288. /*******************************************************************************
  9289. * FUNCTION: _pt_ttdl_restart
  9290. *
  9291. * SUMMARY: Restarts TTDL enumeration with the DUT and re-probes all modules
  9292. *
  9293. * NOTE: The function DOSE NOT remove sysfs and modules. Trying to create
  9294. * existing sysfs nodes will produce an error.
  9295. *
  9296. * RETURN:
  9297. * 0 = success
  9298. * !0 = failure
  9299. *
  9300. * PARAMETERS:
  9301. * *dev - pointer to core device
  9302. ******************************************************************************/
  9303. static int _pt_ttdl_restart(struct device *dev)
  9304. {
  9305. int rc = 0;
  9306. struct pt_core_data *cd = dev_get_drvdata(dev);
  9307. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9308. struct i2c_client *client =
  9309. (struct i2c_client *)container_of(dev, struct i2c_client, dev);
  9310. #endif
  9311. /*
  9312. * Make sure the device is awake, pt_mt_release function will
  9313. * cause pm sleep function and lead to deadlock.
  9314. */
  9315. pm_runtime_get_sync(dev);
  9316. /* Use ttdl_restart_lock to avoid reentry */
  9317. mutex_lock(&cd->ttdl_restart_lock);
  9318. remove_sysfs_and_modules(cd->dev);
  9319. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9320. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  9321. pt_debug(dev, DL_ERROR,
  9322. "%s I2C functionality not Supported\n", __func__);
  9323. rc = -EIO;
  9324. goto ttdl_no_error;
  9325. }
  9326. #endif
  9327. if (cd->active_dut_generation == DUT_UNKNOWN) {
  9328. rc = _pt_detect_dut_generation(cd->dev,
  9329. &cd->startup_status, &cd->active_dut_generation,
  9330. &cd->mode);
  9331. if ((cd->active_dut_generation == DUT_UNKNOWN) || (rc)) {
  9332. pt_debug(dev, DL_ERROR,
  9333. "%s: Error, Unknown DUT Generation rc=%d\n",
  9334. __func__, rc);
  9335. }
  9336. }
  9337. rc = add_sysfs_interfaces(cd->dev);
  9338. if (rc < 0)
  9339. pt_debug(cd->dev, DL_ERROR,
  9340. "%s: Error, failed sysfs nodes rc=%d\n",
  9341. __func__, rc);
  9342. if (!(cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL)) {
  9343. pt_debug(dev, DL_INFO, "%s: Call pt_enum_with_dut\n", __func__);
  9344. rc = pt_enum_with_dut(cd, true, &cd->startup_status);
  9345. if (rc < 0)
  9346. pt_debug(dev, DL_ERROR,
  9347. "%s: Error, Failed to Enumerate\n", __func__);
  9348. }
  9349. rc = pt_mt_probe(dev);
  9350. if (rc < 0) {
  9351. pt_debug(dev, DL_ERROR,
  9352. "%s: Error, fail mt probe\n", __func__);
  9353. }
  9354. rc = pt_btn_probe(dev);
  9355. if (rc < 0) {
  9356. pt_debug(dev, DL_ERROR,
  9357. "%s: Error, fail btn probe\n", __func__);
  9358. }
  9359. pt_probe_modules(cd);
  9360. pt_debug(cd->dev, DL_WARN,
  9361. "%s: Well Done! TTDL Restart Completed\n", __func__);
  9362. rc = 0;
  9363. #ifdef CONFIG_TOUCHSCREEN_PARADE_I2C
  9364. ttdl_no_error:
  9365. #endif
  9366. mutex_unlock(&cd->ttdl_restart_lock);
  9367. mutex_lock(&cd->system_lock);
  9368. cd->startup_status |= STARTUP_STATUS_COMPLETE;
  9369. cd->startup_state = STARTUP_NONE;
  9370. mutex_unlock(&cd->system_lock);
  9371. pm_runtime_put(dev);
  9372. return rc;
  9373. }
  9374. /*******************************************************************************
  9375. * FUNCTION: pt_restart_work_function
  9376. *
  9377. * SUMMARY: This is the wrapper function placed in a work queue to call
  9378. * _pt_ttdl_restart()
  9379. *
  9380. * RETURN: none
  9381. *
  9382. * PARAMETERS:
  9383. * *work - pointer to the work_struct
  9384. ******************************************************************************/
  9385. static void pt_restart_work_function(struct work_struct *work)
  9386. {
  9387. struct pt_core_data *cd = container_of(work,
  9388. struct pt_core_data, ttdl_restart_work);
  9389. int rc = 0;
  9390. rc = _pt_ttdl_restart(cd->dev);
  9391. if (rc < 0)
  9392. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9393. __func__, rc);
  9394. }
  9395. /*******************************************************************************
  9396. * FUNCTION: pt_enum_work_function
  9397. *
  9398. * SUMMARY: This is the wrapper function placed in a work queue to call
  9399. * pt_enum_with_dut()
  9400. *
  9401. * RETURN: none
  9402. *
  9403. * PARAMETERS:
  9404. * *work - pointer to the work_struct
  9405. ******************************************************************************/
  9406. static void pt_enum_work_function(struct work_struct *work)
  9407. {
  9408. struct pt_core_data *cd = container_of(work,
  9409. struct pt_core_data, enum_work);
  9410. int rc;
  9411. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  9412. if (rc < 0)
  9413. pt_debug(cd->dev, DL_ERROR, "%s: Fail queued startup r=%d\n",
  9414. __func__, rc);
  9415. }
  9416. static int pt_get_regulator(struct pt_core_data *cd, bool get)
  9417. {
  9418. int rc;
  9419. if (!get) {
  9420. rc = 0;
  9421. goto regulator_put;
  9422. }
  9423. cd->vdd = regulator_get(cd->dev, "vdd");
  9424. if (IS_ERR(cd->vdd)) {
  9425. rc = PTR_ERR(cd->vdd);
  9426. dev_err(cd->dev,
  9427. "Regulator get failed vdd rc=%d\n", rc);
  9428. goto regulator_put;
  9429. }
  9430. cd->vcc_i2c = regulator_get(cd->dev, "vcc_i2c");
  9431. if (IS_ERR(cd->vcc_i2c)) {
  9432. rc = PTR_ERR(cd->vcc_i2c);
  9433. dev_err(cd->dev,
  9434. "Regulator get failed vcc_i2c rc=%d\n", rc);
  9435. goto regulator_put;
  9436. }
  9437. return 0;
  9438. regulator_put:
  9439. if (cd->vdd) {
  9440. regulator_put(cd->vdd);
  9441. cd->vdd = NULL;
  9442. }
  9443. if (cd->vcc_i2c) {
  9444. regulator_put(cd->vcc_i2c);
  9445. cd->vcc_i2c = NULL;
  9446. }
  9447. return rc;
  9448. }
  9449. static int pt_enable_regulator(struct pt_core_data *cd, bool en)
  9450. {
  9451. int rc;
  9452. if (!en) {
  9453. rc = 0;
  9454. goto disable_vcc_i2c_reg;
  9455. }
  9456. if (cd->vdd) {
  9457. if (regulator_count_voltages(cd->vdd) > 0) {
  9458. rc = regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9459. FT_VTG_MAX_UV);
  9460. if (rc) {
  9461. dev_err(cd->dev,
  9462. "Regulator set_vtg failed vdd rc=%d\n", rc);
  9463. goto exit;
  9464. }
  9465. }
  9466. rc = regulator_enable(cd->vdd);
  9467. if (rc) {
  9468. dev_err(cd->dev,
  9469. "Regulator vdd enable failed rc=%d\n", rc);
  9470. goto exit;
  9471. }
  9472. }
  9473. if (cd->vcc_i2c) {
  9474. if (regulator_count_voltages(cd->vcc_i2c) > 0) {
  9475. rc = regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9476. FT_I2C_VTG_MAX_UV);
  9477. if (rc) {
  9478. dev_err(cd->dev,
  9479. "Regulator set_vtg failed vcc_i2c rc=%d\n", rc);
  9480. goto disable_vdd_reg;
  9481. }
  9482. }
  9483. rc = regulator_enable(cd->vcc_i2c);
  9484. if (rc) {
  9485. dev_err(cd->dev,
  9486. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  9487. goto disable_vdd_reg;
  9488. }
  9489. }
  9490. return 0;
  9491. disable_vcc_i2c_reg:
  9492. if (cd->vcc_i2c) {
  9493. if (regulator_count_voltages(cd->vcc_i2c) > 0)
  9494. regulator_set_voltage(cd->vcc_i2c, FT_I2C_VTG_MIN_UV,
  9495. FT_I2C_VTG_MAX_UV);
  9496. regulator_disable(cd->vcc_i2c);
  9497. }
  9498. disable_vdd_reg:
  9499. if (cd->vdd) {
  9500. if (regulator_count_voltages(cd->vdd) > 0)
  9501. regulator_set_voltage(cd->vdd, FT_VTG_MIN_UV,
  9502. FT_VTG_MAX_UV);
  9503. regulator_disable(cd->vdd);
  9504. }
  9505. exit:
  9506. return rc;
  9507. }
  9508. #if (KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE)
  9509. #define KERNEL_VER_GT_3_19
  9510. #endif
  9511. #if defined(CONFIG_PM_RUNTIME) || defined(KERNEL_VER_GT_3_19)
  9512. /* CONFIG_PM_RUNTIME option is removed in 3.19.0 */
  9513. #if defined(CONFIG_PM_SLEEP)
  9514. /*******************************************************************************
  9515. * FUNCTION: pt_core_rt_suspend
  9516. *
  9517. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_sleep.
  9518. *
  9519. * RETURN:
  9520. * 0 = success
  9521. * !0 = failure
  9522. *
  9523. * PARAMETERS:
  9524. * *dev - pointer to core device
  9525. ******************************************************************************/
  9526. static int pt_core_rt_suspend(struct device *dev)
  9527. {
  9528. struct pt_core_data *cd = dev_get_drvdata(dev);
  9529. int rc = 0;
  9530. dev_info(dev, "%s: Entering into runtime suspend mode:\n",
  9531. __func__);
  9532. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9533. return 0;
  9534. rc = pt_core_easywake_on(cd);
  9535. if (rc < 0) {
  9536. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9537. return -EAGAIN;
  9538. }
  9539. return 0;
  9540. }
  9541. /*******************************************************************************
  9542. * FUNCTION: pt_core_rt_resume
  9543. *
  9544. * SUMMARY: Wrapper function with PM Runtime stratergy to call pt_core_wake.
  9545. *
  9546. * RETURN:
  9547. * 0 = success
  9548. * !0 = failure
  9549. *
  9550. * PARAMETERS:
  9551. * *dev - pointer to core device
  9552. ******************************************************************************/
  9553. static int pt_core_rt_resume(struct device *dev)
  9554. {
  9555. struct pt_core_data *cd = dev_get_drvdata(dev);
  9556. int rc = 0;
  9557. dev_info(dev, "%s: Entering into runtime resume mode:\n",
  9558. __func__);
  9559. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  9560. return 0;
  9561. rc = pt_core_easywake_off(cd);
  9562. if (rc < 0) {
  9563. pt_debug(dev, DL_ERROR, "%s: Error on wake\n", __func__);
  9564. return -EAGAIN;
  9565. }
  9566. return 0;
  9567. }
  9568. #endif /* CONFIG_PM_SLEEP */
  9569. #endif /* CONFIG_PM_RUNTIME || LINUX_VERSION_CODE */
  9570. #if defined(CONFIG_PM_SLEEP)
  9571. /*******************************************************************************
  9572. * FUNCTION: pt_core_suspend_
  9573. *
  9574. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9575. * pt_core_sleep. This function may disable IRQ during sleep state.
  9576. *
  9577. * RETURN:
  9578. * 0 = success
  9579. * !0 = failure
  9580. *
  9581. * PARAMETERS:
  9582. * *dev - pointer to core device
  9583. ******************************************************************************/
  9584. static int pt_core_suspend_(struct device *dev)
  9585. {
  9586. int rc;
  9587. struct pt_core_data *cd = dev_get_drvdata(dev);
  9588. pt_debug(dev, DL_INFO, "%s: Entering into suspend mode:\n",
  9589. __func__);
  9590. rc = pt_core_sleep(cd);
  9591. if (rc) {
  9592. pt_debug(dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  9593. return -EAGAIN;
  9594. }
  9595. #ifdef REGULATOR
  9596. rc = pt_enable_regulator(cd, false);
  9597. if (rc)
  9598. dev_err(dev, "%s: Failed to disable regulators: rc=%d\n", __func__, rc);
  9599. #endif
  9600. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9601. return 0;
  9602. /* Required to prevent interrupts before bus awake */
  9603. disable_irq(cd->irq);
  9604. cd->irq_disabled = 1;
  9605. if (device_may_wakeup(dev)) {
  9606. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9607. __func__);
  9608. if (!enable_irq_wake(cd->irq))
  9609. cd->irq_wake = 1;
  9610. } else {
  9611. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9612. __func__);
  9613. }
  9614. return rc;
  9615. }
  9616. /*******************************************************************************
  9617. * FUNCTION: pt_core_suspend
  9618. *
  9619. * SUMMARY: Wrapper function of pt_core_suspend_() to help avoid TP from being
  9620. * woke up or put to sleep based on Kernel power state even when the display
  9621. * is off based on the check of TTDL core platform flag.
  9622. *
  9623. * RETURN:
  9624. * 0 = success
  9625. * !0 = failure
  9626. *
  9627. * PARAMETERS:
  9628. * *dev - pointer to core device
  9629. ******************************************************************************/
  9630. static int pt_core_suspend(struct device *dev)
  9631. {
  9632. struct pt_core_data *cd = dev_get_drvdata(dev);
  9633. int rc = 0;
  9634. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9635. return 0;
  9636. rc = pt_core_suspend_(dev);
  9637. pt_debug(dev, DL_WARN, "%s Exit - rc = %d\n", __func__, rc);
  9638. return rc;
  9639. }
  9640. /*******************************************************************************
  9641. * FUNCTION: pt_core_resume_
  9642. *
  9643. * SUMMARY: Wrapper function with device suspend/resume stratergy to call
  9644. * pt_core_wake. This function may enable IRQ before wake up.
  9645. *
  9646. * RETURN:
  9647. * 0 = success
  9648. * !0 = failure
  9649. *
  9650. * PARAMETERS:
  9651. * *dev - pointer to core device
  9652. ******************************************************************************/
  9653. static int pt_core_resume_(struct device *dev)
  9654. {
  9655. int rc = 0;
  9656. struct pt_core_data *cd = dev_get_drvdata(dev);
  9657. dev_info(dev, "%s: Entering into resume mode:\n",
  9658. __func__);
  9659. #ifdef REGULATOR
  9660. rc = pt_enable_regulator(cd, true);
  9661. if (rc < 0) {
  9662. dev_err(dev, "%s: Failed to enable regulators: rc=%d\n",
  9663. __func__, rc);
  9664. }
  9665. #endif
  9666. dev_info(dev, "%s: Voltage regulator enabled: rc=%d\n",
  9667. __func__, rc);
  9668. if (!IS_EASY_WAKE_CONFIGURED(cd->easy_wakeup_gesture))
  9669. goto exit;
  9670. /*
  9671. * Bus pm does not call suspend if device runtime suspended
  9672. * This flag is covers that case
  9673. */
  9674. if (cd->irq_disabled) {
  9675. enable_irq(cd->irq);
  9676. cd->irq_disabled = 0;
  9677. }
  9678. if (device_may_wakeup(dev)) {
  9679. pt_debug(dev, DL_WARN, "%s Device MAY wakeup\n",
  9680. __func__);
  9681. if (cd->irq_wake) {
  9682. disable_irq_wake(cd->irq);
  9683. cd->irq_wake = 0;
  9684. }
  9685. } else {
  9686. pt_debug(dev, DL_WARN, "%s Device MAY NOT wakeup\n",
  9687. __func__);
  9688. }
  9689. exit:
  9690. rc = pt_core_wake(cd);
  9691. if (rc) {
  9692. dev_err(dev, "%s: Failed to wake up: rc=%d\n",
  9693. __func__, rc);
  9694. return -EAGAIN;
  9695. }
  9696. return 0;
  9697. }
  9698. /*******************************************************************************
  9699. * FUNCTION: pt_core_resume
  9700. *
  9701. * SUMMARY: Wrapper function of pt_core_resume_() to avoid TP to be waken/slept
  9702. * along with kernel power state even the display is off based on the check of
  9703. * TTDL core platform flag.
  9704. *
  9705. * RETURN:
  9706. * 0 = success
  9707. * !0 = failure
  9708. *
  9709. * PARAMETERS:
  9710. * *dev - pointer to core device
  9711. ******************************************************************************/
  9712. static int pt_core_resume(struct device *dev)
  9713. {
  9714. struct pt_core_data *cd = dev_get_drvdata(dev);
  9715. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_SYS_SLEEP)
  9716. return 0;
  9717. return pt_core_resume_(dev);
  9718. }
  9719. #endif
  9720. #ifdef NEED_SUSPEND_NOTIFIER
  9721. /*******************************************************************************
  9722. * FUNCTION: pt_pm_notifier
  9723. *
  9724. * SUMMARY: This function is registered to notifier chain and will perform
  9725. * suspend operation if match event PM_SUSPEND_PREPARE.
  9726. *
  9727. * RETURN:
  9728. * 0 = success
  9729. * !0 = failure
  9730. *
  9731. * PARAMETERS:
  9732. * *nb - pointer to notifier_block structure
  9733. * action - notifier event type
  9734. * *data - void pointer
  9735. ******************************************************************************/
  9736. static int pt_pm_notifier(struct notifier_block *nb,
  9737. unsigned long action, void *data)
  9738. {
  9739. struct pt_core_data *cd = container_of(nb,
  9740. struct pt_core_data, pm_notifier);
  9741. if (action == PM_SUSPEND_PREPARE) {
  9742. pt_debug(cd->dev, DL_INFO, "%s: Suspend prepare\n",
  9743. __func__);
  9744. /*
  9745. * If PM runtime is not suspended, either call runtime
  9746. * PM suspend callback or wait until it finishes
  9747. */
  9748. if (!pm_runtime_suspended(cd->dev))
  9749. pm_runtime_suspend(cd->dev);
  9750. (void) pt_core_suspend(cd->dev);
  9751. }
  9752. return NOTIFY_DONE;
  9753. }
  9754. #endif
  9755. const struct dev_pm_ops pt_pm_ops = {
  9756. SET_SYSTEM_SLEEP_PM_OPS(pt_core_suspend, pt_core_resume)
  9757. SET_RUNTIME_PM_OPS(pt_core_rt_suspend, pt_core_rt_resume,
  9758. NULL)
  9759. };
  9760. EXPORT_SYMBOL_GPL(pt_pm_ops);
  9761. /*******************************************************************************
  9762. * FUNCTION: _pt_request_pip2_enter_bl
  9763. *
  9764. * SUMMARY: Force the DUT to enter the BL by resetting the DUT by use of the
  9765. * XRES pin or a soft reset.
  9766. *
  9767. * NOTE: The WD MUST be stopped/restarted by the calling Function. Having
  9768. * the WD active could cause this function to fail!
  9769. * NOTE: If start_mode is passed in as PT_MODE_IGNORE, this function
  9770. * will not try to determine the current mode but will proceed with
  9771. * resetting the DUT and entering the BL.
  9772. *
  9773. * NOTE: The definition of result code:
  9774. * PT_ENTER_BL_PASS (0)
  9775. * PT_ENTER_BL_ERROR (1)
  9776. * PT_ENTER_BL_RESET_FAIL (2)
  9777. * PT_ENTER_BL_HID_START_BL_FAIL (3)
  9778. * PT_ENTER_BL_CONFIRM_FAIL (4)
  9779. * PT_ENTER_BL_GET_FLASH_INFO_FAIL (5)
  9780. *
  9781. * RETURNS:
  9782. * 0 = success
  9783. * !0 = failure
  9784. *
  9785. *
  9786. * PARAMETERS:
  9787. * *dev - pointer to device structure
  9788. * *start_mode - pointer to the mode the DUT was in when this function
  9789. * starts
  9790. * *result - pointer to store the result when to enter BL
  9791. ******************************************************************************/
  9792. int _pt_request_pip2_enter_bl(struct device *dev, u8 *start_mode, int *result)
  9793. {
  9794. int rc = 0;
  9795. int t;
  9796. int tmp_result = PT_ENTER_BL_ERROR;
  9797. int flash_info_retry = 2;
  9798. u8 mode = PT_MODE_UNKNOWN;
  9799. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  9800. u8 read_buf[32];
  9801. u16 actual_read_len;
  9802. struct pt_core_data *cd = dev_get_drvdata(dev);
  9803. u8 host_mode_cmd[4] = {0xA5, 0xA5, 0xA5, 0xA5};
  9804. u8 time = 0;
  9805. u8 saved_flashless_auto_bl_mode = cd->flashless_auto_bl;
  9806. if (cd->watchdog_enabled) {
  9807. pt_debug(dev, DL_WARN,
  9808. "%s: Watchdog must be stopped before entering BL\n",
  9809. __func__);
  9810. goto exit;
  9811. }
  9812. cancel_work_sync(&cd->enum_work);
  9813. cancel_work_sync(&cd->watchdog_work);
  9814. /* if undefined assume operational/test to bypass all checks */
  9815. if (*start_mode == PT_MODE_IGNORE) {
  9816. mode = PT_MODE_OPERATIONAL;
  9817. sys_mode = FW_SYS_MODE_TEST;
  9818. pt_debug(dev, DL_INFO, "%s: Assume Mode = %d", __func__, mode);
  9819. } else if (*start_mode == PT_MODE_UNKNOWN) {
  9820. rc = pt_pip2_get_mode_sysmode_(cd, &mode, &sys_mode);
  9821. if (rc) {
  9822. pt_debug(dev, DL_ERROR,
  9823. "%s: Get mode failed, mode unknown\n",
  9824. __func__);
  9825. }
  9826. *start_mode = mode;
  9827. pt_debug(dev, DL_INFO, "%s: Get Mode = %d", __func__, mode);
  9828. } else if (*start_mode == PT_MODE_OPERATIONAL) {
  9829. /* Assume SCANNIING mode to avoid doing an extra get_mode */
  9830. sys_mode = FW_SYS_MODE_SCANNING;
  9831. }
  9832. _retry:
  9833. /* For Flashless DUTs - Suppress auto BL on next BL sentinel */
  9834. pt_debug(dev, DL_INFO, "%s: Flashless Auto_BL - SUPPRESS\n", __func__);
  9835. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  9836. switch (mode) {
  9837. case PT_MODE_UNKNOWN:
  9838. /*
  9839. * When the mode could not be determined the DUT could be
  9840. * in App mode running corrupted FW or FW that is not
  9841. * responding to the mode request, assume no communication
  9842. * and do a hard reset
  9843. */
  9844. mutex_lock(&cd->system_lock);
  9845. cd->startup_status = STARTUP_STATUS_START;
  9846. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9847. mutex_unlock(&cd->system_lock);
  9848. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9849. if (rc) {
  9850. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9851. goto exit;
  9852. }
  9853. break;
  9854. case PT_MODE_OPERATIONAL:
  9855. if (sys_mode == FW_SYS_MODE_SCANNING) {
  9856. pt_debug(dev, DL_INFO, "%s: Suspend Scanning\n",
  9857. __func__);
  9858. rc = pt_pip_suspend_scanning_(cd);
  9859. if (rc) {
  9860. /*
  9861. * Print to log but don't exit, the FW could be
  9862. * running but be hung or fail to respond to
  9863. * this request
  9864. */
  9865. pt_debug(dev, DL_ERROR,
  9866. "%s Suspend Scan Failed\n", __func__);
  9867. }
  9868. /* sleep to allow the suspend scan to be processed */
  9869. usleep_range(1000, 2000);
  9870. }
  9871. mutex_lock(&cd->system_lock);
  9872. cd->startup_status = STARTUP_STATUS_START;
  9873. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  9874. mutex_unlock(&cd->system_lock);
  9875. /* Reset device to enter the BL */
  9876. rc = pt_dut_reset(cd, PT_CORE_CMD_UNPROTECTED);
  9877. if (rc) {
  9878. tmp_result = PT_ENTER_BL_RESET_FAIL;
  9879. goto exit;
  9880. }
  9881. break;
  9882. case PT_MODE_BOOTLOADER:
  9883. /* Do nothing as we are already in the BL */
  9884. tmp_result = PT_ENTER_BL_PASS;
  9885. goto exit;
  9886. default:
  9887. /* Should NEVER get here */
  9888. tmp_result = PT_ENTER_BL_ERROR;
  9889. pt_debug(dev, DL_ERROR, "%s: Unknown mode code\n", __func__);
  9890. goto exit;
  9891. }
  9892. if (!cd->flashless_dut &&
  9893. (mode == PT_MODE_UNKNOWN || mode == PT_MODE_OPERATIONAL)) {
  9894. /*
  9895. * Sending the special "Host Mode" command will instruct the
  9896. * BL to not execute the FW it has loaded into RAM.
  9897. * The command must be sent within a 40ms window from releasing
  9898. * the XRES pin. If the messages is sent too early it will NAK,
  9899. * so keep sending it every 2ms until it is accepted by the BL.
  9900. * A no-flash DUT does not require this command as there is no
  9901. * FW for the BL to load and execute.
  9902. */
  9903. usleep_range(4000, 6000);
  9904. pt_debug(cd->dev, DL_INFO,
  9905. ">>> %s: Write Buffer Size[%d] Stay in BL\n",
  9906. __func__, (int)sizeof(host_mode_cmd));
  9907. pt_pr_buf(cd->dev, DL_DEBUG, host_mode_cmd,
  9908. (int)sizeof(host_mode_cmd), ">>> User CMD");
  9909. rc = 1;
  9910. while (rc && time < 34) {
  9911. rc = pt_adap_write_read_specific(cd, 4,
  9912. host_mode_cmd, NULL);
  9913. usleep_range(1800, 2000);
  9914. time += 2;
  9915. }
  9916. /* Sleep to allow the BL to come up */
  9917. usleep_range(1000, 2000);
  9918. }
  9919. /*
  9920. * To avoid the case that next PIP command can be confused by BL/FW
  9921. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  9922. * followed by "wakeup event" function can lower the failure rate.
  9923. */
  9924. t = wait_event_timeout(cd->wait_q,
  9925. ((cd->startup_status != STARTUP_STATUS_START)
  9926. && (cd->hid_reset_cmd_state == 0)),
  9927. msecs_to_jiffies(300));
  9928. if (IS_TMO(t)) {
  9929. pt_debug(cd->dev, DL_ERROR,
  9930. "%s: TMO waiting for BL sentinel\n", __func__);
  9931. }
  9932. /* Check if device is now in BL mode */
  9933. rc = pt_pip2_get_mode_sysmode_(cd, &mode, NULL);
  9934. pt_debug(dev, DL_INFO, "%s: Mode=%d, Status=0x%04X\n", __func__, mode,
  9935. cd->startup_status);
  9936. if (!rc && mode == PT_MODE_BOOTLOADER) {
  9937. pt_debug(dev, DL_INFO, "%s In bootloader mode now\n", __func__);
  9938. mutex_lock(&cd->system_lock);
  9939. cd->pip2_prot_active = true;
  9940. cd->mode = PT_MODE_BOOTLOADER;
  9941. mutex_unlock(&cd->system_lock);
  9942. tmp_result = PT_ENTER_BL_PASS;
  9943. } else {
  9944. /*
  9945. * If the device doesn't enter BL mode as expected and rc is
  9946. * tested pass by above function pt_pip2_get_mode_sysmode_(),
  9947. * the function should return an error code to indicate this
  9948. * failure PT_ENTER_BL_CONFIRM_FAIL.
  9949. */
  9950. if (!rc)
  9951. rc = -EINVAL;
  9952. tmp_result = PT_ENTER_BL_CONFIRM_FAIL;
  9953. mutex_lock(&cd->system_lock);
  9954. cd->mode = mode;
  9955. mutex_unlock(&cd->system_lock);
  9956. pt_debug(dev, DL_ERROR,
  9957. "%s ERROR: Not in BL as expected", __func__);
  9958. }
  9959. exit:
  9960. if (!cd->flashless_dut && (tmp_result == PT_ENTER_BL_PASS)) {
  9961. /* Check to get (buffered) flash information */
  9962. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  9963. PIP2_CMD_ID_FLASH_INFO, NULL, 0,
  9964. read_buf, &actual_read_len);
  9965. if (!rc) {
  9966. if (read_buf[PIP2_RESP_BODY_OFFSET] == 0) {
  9967. pt_debug(
  9968. dev, DL_WARN,
  9969. "%s Unavailable Manufacturer ID: 0x%02x\n",
  9970. __func__,
  9971. read_buf[PIP2_RESP_BODY_OFFSET]);
  9972. /*
  9973. * If the BL was unable to cache the correct
  9974. * values when entering the first time due to
  9975. * the Flash part not having been powered up
  9976. * long enough, re-enter the BL to trigger the
  9977. * BL to re-attempt to cache the values.
  9978. */
  9979. if (flash_info_retry-- > 0) {
  9980. mode = PT_MODE_UNKNOWN;
  9981. pt_debug(dev, DL_WARN,
  9982. "%s Assume mode to UNKNOWN to enter BL again, retry=%d\n",
  9983. __func__, flash_info_retry);
  9984. goto _retry;
  9985. } else {
  9986. pt_debug(dev, DL_WARN,
  9987. "%s Manufacturer ID Unknown\n",
  9988. __func__);
  9989. tmp_result = PT_ENTER_BL_PASS;
  9990. }
  9991. }
  9992. } else {
  9993. tmp_result = PT_ENTER_BL_GET_FLASH_INFO_FAIL;
  9994. pt_debug(
  9995. dev, DL_ERROR,
  9996. "%s: Failed to send PIP2 READ_FLASH_INFO cmd\n",
  9997. __func__);
  9998. }
  9999. }
  10000. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - %s\n", __func__,
  10001. saved_flashless_auto_bl_mode == PT_ALLOW_AUTO_BL ? "ALLOW" :
  10002. "SUPPRESS");
  10003. cd->flashless_auto_bl = saved_flashless_auto_bl_mode;
  10004. if (result)
  10005. *result = tmp_result;
  10006. return rc;
  10007. }
  10008. /*******************************************************************************
  10009. * FUNCTION: _pt_pip2_file_open
  10010. *
  10011. * SUMMARY: Using the BL PIP2 commands open a file and return the file handle
  10012. *
  10013. * NOTE: The DUT must be in BL mode for this command to work
  10014. *
  10015. * RETURNS:
  10016. * <0 = Error
  10017. * >0 = file handle opened
  10018. *
  10019. * PARAMETERS:
  10020. * *dev - pointer to device structure
  10021. * file_no - PIP2 file number to open
  10022. ******************************************************************************/
  10023. int _pt_pip2_file_open(struct device *dev, u8 file_no)
  10024. {
  10025. int ret = 0;
  10026. u16 status;
  10027. u16 actual_read_len;
  10028. u8 file_handle;
  10029. u8 data[2];
  10030. u8 read_buf[10];
  10031. pt_debug(dev, DL_DEBUG, "%s: OPEN file %d\n", __func__, file_no);
  10032. data[0] = file_no;
  10033. ret = _pt_request_pip2_send_cmd(dev,
  10034. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_OPEN,
  10035. data, 1, read_buf, &actual_read_len);
  10036. if (ret) {
  10037. pt_debug(dev, DL_ERROR,
  10038. "%s ROM BL FILE_OPEN timeout for file = %d\n",
  10039. __func__, file_no);
  10040. return -PIP2_RSP_ERR_NOT_OPEN;
  10041. }
  10042. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10043. file_handle = read_buf[PIP2_RESP_BODY_OFFSET];
  10044. if (ret || ((status != 0x00) && (status != 0x03)) ||
  10045. (file_handle != file_no)) {
  10046. pt_debug(dev, DL_ERROR,
  10047. "%s ROM BL FILE_OPEN failure: 0x%02X for file = %d\n",
  10048. __func__, status, file_handle);
  10049. return -status;
  10050. }
  10051. return file_handle;
  10052. }
  10053. /*******************************************************************************
  10054. * FUNCTION: _pt_pip2_file_close
  10055. *
  10056. * SUMMARY: Using the BL PIP2 commands close a file
  10057. *
  10058. * NOTE: The DUT must be in BL mode for this command to work
  10059. *
  10060. * RETURNS:
  10061. * <0 = Error
  10062. * >0 = file handle closed
  10063. *
  10064. * PARAMETERS:
  10065. * *dev - pointer to device structure
  10066. * file_handle - handle to the file to be closed
  10067. ******************************************************************************/
  10068. int _pt_pip2_file_close(struct device *dev, u8 file_handle)
  10069. {
  10070. int ret = 0;
  10071. u16 status;
  10072. u16 actual_read_len;
  10073. u8 data[2];
  10074. u8 read_buf[32];
  10075. pt_debug(dev, DL_DEBUG, "%s: CLOSE file %d\n", __func__, file_handle);
  10076. data[0] = file_handle;
  10077. ret = _pt_request_pip2_send_cmd(dev,
  10078. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_CLOSE,
  10079. data, 1, read_buf, &actual_read_len);
  10080. if (ret) {
  10081. pt_debug(dev, DL_ERROR,
  10082. "%s ROM BL FILE_CLOSE timeout for file = %d\n",
  10083. __func__, file_handle);
  10084. return -ETIME;
  10085. }
  10086. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10087. if (status != 0x00) {
  10088. pt_debug(dev, DL_ERROR,
  10089. "%s ROM BL FILE_CLOSE failure: 0x%02X for file = %d\n",
  10090. __func__, status, file_handle);
  10091. return -status;
  10092. }
  10093. return file_handle;
  10094. }
  10095. /*******************************************************************************
  10096. * FUNCTION: _pt_pip2_file_erase
  10097. *
  10098. * SUMMARY: Using the BL PIP2 commands erase a file
  10099. *
  10100. * NOTE: The DUT must be in BL mode for this command to work
  10101. * NOTE: Some FLASH parts can time out while erasing one or more sectors,
  10102. * one retry is attempted for each sector in a file.
  10103. *
  10104. * RETURNS:
  10105. * <0 = Error
  10106. * >0 = file handle closed
  10107. *
  10108. * PARAMETERS:
  10109. * *dev - pointer to device structure
  10110. * file_handle - handle to the file to be erased
  10111. * *status - PIP2 erase status code
  10112. ******************************************************************************/
  10113. static int _pt_pip2_file_erase(struct device *dev, u8 file_handle, int *status)
  10114. {
  10115. int ret = 0;
  10116. int max_retry = PT_PIP2_MAX_FILE_SIZE/PT_PIP2_FILE_SECTOR_SIZE;
  10117. int retry = 1;
  10118. u16 actual_read_len;
  10119. u8 data[2];
  10120. u8 read_buf[10];
  10121. struct pt_core_data *cd = dev_get_drvdata(dev);
  10122. pt_debug(dev, DL_DEBUG, "%s: ERASE file %d\n", __func__, file_handle);
  10123. data[0] = file_handle;
  10124. data[1] = PIP2_FILE_IOCTL_CODE_ERASE_FILE;
  10125. *status = PIP2_RSP_ERR_TIMEOUT;
  10126. /* Increase waiting time for large file erase */
  10127. mutex_lock(&cd->system_lock);
  10128. cd->pip_cmd_timeout = PT_PIP2_CMD_FILE_ERASE_TIMEOUT;
  10129. mutex_unlock(&cd->system_lock);
  10130. while (*status == PIP2_RSP_ERR_TIMEOUT && retry < max_retry) {
  10131. ret = _pt_request_pip2_send_cmd(dev,
  10132. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10133. data, 2, read_buf, &actual_read_len);
  10134. if (ret)
  10135. break;
  10136. *status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10137. if (*status == PIP2_RSP_ERR_TIMEOUT) {
  10138. #ifdef TTDL_DIAGNOSTICS
  10139. cd->file_erase_timeout_count++;
  10140. #endif
  10141. pt_debug(dev, DL_WARN,
  10142. "%s: ERASE timeout %d for file = %d\n",
  10143. __func__, retry, file_handle);
  10144. }
  10145. retry++;
  10146. }
  10147. mutex_lock(&cd->system_lock);
  10148. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  10149. mutex_unlock(&cd->system_lock);
  10150. if (ret) {
  10151. pt_debug(dev, DL_ERROR,
  10152. "%s ROM FILE_ERASE cmd failure: %d for file = %d\n",
  10153. __func__, ret, file_handle);
  10154. return -EIO;
  10155. }
  10156. if (*status != 0x00) {
  10157. pt_debug(dev, DL_ERROR,
  10158. "%s ROM FILE_ERASE failure: 0x%02X for file = %d\n",
  10159. __func__, *status, file_handle);
  10160. return -EIO;
  10161. }
  10162. return file_handle;
  10163. }
  10164. /*******************************************************************************
  10165. * FUNCTION: _pt_pip2_file_read
  10166. *
  10167. * SUMMARY: Using the BL PIP2 commands read n bytes from a already opened file
  10168. *
  10169. * NOTE: The DUT must be in BL mode for this command to work
  10170. *
  10171. * RETURNS:
  10172. * <0 = Error
  10173. * >0 = number of bytes read
  10174. *
  10175. * PARAMETERS:
  10176. * *dev - pointer to device structure
  10177. * file_handle - File handle to read from
  10178. * num_bytes - number of bytes to read
  10179. ******************************************************************************/
  10180. int _pt_pip2_file_read(struct device *dev, u8 file_handle, u16 num_bytes,
  10181. u8 *read_buf)
  10182. {
  10183. int ret = 0;
  10184. u16 status;
  10185. u16 actual_read_len;
  10186. u8 data[3];
  10187. data[0] = file_handle;
  10188. data[1] = (num_bytes & 0x00FF);
  10189. data[2] = (num_bytes & 0xFF00) >> 8;
  10190. ret = _pt_request_pip2_send_cmd(dev,
  10191. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_READ,
  10192. data, 3, read_buf, &actual_read_len);
  10193. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10194. if (ret || ((status != 0x00) && (status != 0x03))) {
  10195. pt_debug(dev, DL_ERROR,
  10196. "%s File open failure with error code = %d\n",
  10197. __func__, status);
  10198. return -EPERM;
  10199. }
  10200. ret = num_bytes;
  10201. return ret;
  10202. }
  10203. /*******************************************************************************
  10204. * FUNCTION: _pt_read_us_file
  10205. *
  10206. * SUMMARY: Open a user space file and read 'size' bytes into buf. If size = 0
  10207. * then read the entire file.
  10208. * NOTE: The file size must be less than PT_PIP2_MAX_FILE_SIZE
  10209. *
  10210. * RETURN:
  10211. * 0 = success
  10212. * !0 = failure
  10213. *
  10214. * PARAMETERS:
  10215. * *file_path - pointer to the file path
  10216. * *buf - pointer to the buffer to store the file contents
  10217. * *size - pointer to the size of the file
  10218. ******************************************************************************/
  10219. int _pt_read_us_file(struct device *dev, u8 *file_path, u8 *buf, int *size)
  10220. {
  10221. struct file *filp = NULL;
  10222. struct inode *inode = NULL;
  10223. unsigned int file_len = 0;
  10224. unsigned int read_len = 0;
  10225. mm_segment_t oldfs;
  10226. int rc = 0;
  10227. if (file_path == NULL || buf == NULL) {
  10228. pt_debug(dev, DL_ERROR, "%s: path || buf is NULL.\n", __func__);
  10229. return -EINVAL;
  10230. }
  10231. pt_debug(dev, DL_WARN, "%s: path = %s\n", __func__, file_path);
  10232. oldfs = get_fs();
  10233. set_fs(KERNEL_DS);
  10234. filp = filp_open(file_path, O_RDONLY, 0400);
  10235. if (IS_ERR(filp)) {
  10236. pt_debug(dev, DL_ERROR, "%s: Failed to open %s\n", __func__,
  10237. file_path);
  10238. rc = -ENOENT;
  10239. goto err;
  10240. }
  10241. if (filp->f_op == NULL) {
  10242. pt_debug(dev, DL_ERROR, "%s: File Operation Method Error\n",
  10243. __func__);
  10244. rc = -EINVAL;
  10245. goto exit;
  10246. }
  10247. inode = filp->f_path.dentry->d_inode;
  10248. if (inode == NULL) {
  10249. pt_debug(dev, DL_ERROR, "%s: Get inode from filp failed\n",
  10250. __func__);
  10251. rc = -EINVAL;
  10252. goto exit;
  10253. }
  10254. file_len = i_size_read(inode->i_mapping->host);
  10255. if (file_len == 0) {
  10256. pt_debug(dev, DL_ERROR, "%s: file size error,file_len = %d\n",
  10257. __func__, file_len);
  10258. rc = -EINVAL;
  10259. goto exit;
  10260. }
  10261. if (*size == 0)
  10262. read_len = file_len;
  10263. else
  10264. read_len = *size;
  10265. if (read_len > PT_PIP2_MAX_FILE_SIZE) {
  10266. pt_debug(dev, DL_ERROR, "%s: file size ( %d ) exception.\n",
  10267. __func__, read_len);
  10268. rc = -EINVAL;
  10269. goto exit;
  10270. }
  10271. filp->private_data = inode->i_private;
  10272. if (vfs_read(filp, buf, read_len, &(filp->f_pos)) != read_len) {
  10273. pt_debug(dev, DL_ERROR, "%s: file read error.\n", __func__);
  10274. rc = -EINVAL;
  10275. goto exit;
  10276. }
  10277. *size = read_len;
  10278. exit:
  10279. if (filp_close(filp, NULL) != 0)
  10280. pt_debug(dev, DL_ERROR, "%s: file close error.\n", __func__);
  10281. err:
  10282. set_fs(oldfs);
  10283. return rc;
  10284. }
  10285. /*******************************************************************************
  10286. * FUNCTION: _pt_request_pip2_bin_hdr
  10287. *
  10288. * SUMMARY: Read the stored bin file header from Flash or the User Space file
  10289. * in the case of a flashless DUT, and parse the contents
  10290. *
  10291. * RETURNS:
  10292. * 0 = Success
  10293. * !0 = Error condition
  10294. *
  10295. * PARAMETERS:
  10296. * *dev - pointer to device structure
  10297. ******************************************************************************/
  10298. int _pt_request_pip2_bin_hdr(struct device *dev, struct pt_bin_file_hdr *hdr)
  10299. {
  10300. struct pt_core_data *cd = dev_get_drvdata(dev);
  10301. u8 file_handle;
  10302. u8 read_buf[255];
  10303. u8 hdr_len = 0;
  10304. u8 i;
  10305. int bytes_read;
  10306. int read_size;
  10307. int ret = 0;
  10308. int rc = 0;
  10309. bool load_hdr_struct = false;
  10310. if (cd->flashless_dut) {
  10311. read_size = sizeof(read_buf);
  10312. rc = _pt_read_us_file(dev, cd->pip2_us_file_path,
  10313. read_buf, &read_size);
  10314. if (rc) {
  10315. ret = rc;
  10316. pt_debug(dev, DL_ERROR,
  10317. "%s Failed to read fw image from US, rc=%d\n",
  10318. __func__, rc);
  10319. goto exit;
  10320. }
  10321. load_hdr_struct = true;
  10322. hdr_len = read_buf[0];
  10323. i = 0;
  10324. } else {
  10325. if (cd->mode != PT_MODE_BOOTLOADER) {
  10326. ret = -EPERM;
  10327. goto exit;
  10328. }
  10329. /* Open the bin file in Flash */
  10330. pt_debug(dev, DL_INFO, "%s Open File 1\n", __func__);
  10331. file_handle = _pt_pip2_file_open(dev, PIP2_FW_FILE);
  10332. if (file_handle != PIP2_FW_FILE) {
  10333. ret = -ENOENT;
  10334. pt_debug(dev, DL_ERROR,
  10335. "%s Failed to open bin file\n", __func__);
  10336. goto exit;
  10337. }
  10338. /* Read the header length from the file */
  10339. pt_debug(dev, DL_INFO, "%s Read length of header\n", __func__);
  10340. read_size = 1;
  10341. bytes_read = _pt_pip2_file_read(dev, file_handle, read_size,
  10342. read_buf);
  10343. if (bytes_read != read_size) {
  10344. ret = -EX_ERR_FREAD;
  10345. pt_debug(dev, DL_ERROR,
  10346. "%s Failed to bin file header len\n", __func__);
  10347. goto exit_close_file;
  10348. }
  10349. hdr_len = read_buf[PIP2_RESP_BODY_OFFSET];
  10350. if (hdr_len == 0xFF) {
  10351. ret = -EX_ERR_FLEN;
  10352. pt_debug(dev, DL_ERROR,
  10353. "%s Bin header len is invalid\n", __func__);
  10354. goto exit_close_file;
  10355. }
  10356. /* Read the rest of the header from the bin file */
  10357. pt_debug(dev, DL_INFO, "%s Read bin file header\n", __func__);
  10358. memset(read_buf, 0, sizeof(read_buf));
  10359. bytes_read = _pt_pip2_file_read(dev, file_handle, hdr_len,
  10360. read_buf);
  10361. if (bytes_read != hdr_len) {
  10362. ret = -EX_ERR_FREAD;
  10363. pt_debug(dev, DL_ERROR,
  10364. "%s Failed to read bin file\n", __func__);
  10365. goto exit_close_file;
  10366. }
  10367. load_hdr_struct = true;
  10368. exit_close_file:
  10369. /* Close the file */
  10370. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  10371. ret = -EX_ERR_FCLOSE;
  10372. pt_debug(dev, DL_ERROR,
  10373. "%s Failed to close bin file\n", __func__);
  10374. }
  10375. /*
  10376. * The length was already read so subtract 1 to make the rest of
  10377. * the offsets match the spec
  10378. */
  10379. i = PIP2_RESP_BODY_OFFSET - 1;
  10380. }
  10381. if (load_hdr_struct) {
  10382. hdr->length = hdr_len;
  10383. hdr->ttpid = (read_buf[i+1] << 8) | read_buf[i+2];
  10384. hdr->fw_major = (read_buf[i+3]);
  10385. hdr->fw_minor = (read_buf[i+4]);
  10386. hdr->fw_crc = (read_buf[i+5] << 24) |
  10387. (read_buf[i+6] << 16) |
  10388. (read_buf[i+7] << 8) |
  10389. (read_buf[i+8]);
  10390. hdr->fw_rev_ctrl = (read_buf[i+9] << 24) |
  10391. (read_buf[i+10] << 16) |
  10392. (read_buf[i+11] << 8) |
  10393. (read_buf[i+12]);
  10394. hdr->si_rev = (read_buf[i+14] << 8) | (read_buf[i+13]);
  10395. hdr->si_id = (read_buf[i+16] << 8) | (read_buf[i+15]);
  10396. hdr->config_ver = (read_buf[i+17] << 8) | (read_buf[i+18]);
  10397. if (hdr_len >= 22) {
  10398. hdr->hex_file_size = (read_buf[i+19] << 24) |
  10399. (read_buf[i+20] << 16) |
  10400. (read_buf[i+21] << 8) |
  10401. (read_buf[i+22]);
  10402. } else {
  10403. hdr->hex_file_size = 0;
  10404. }
  10405. }
  10406. exit:
  10407. return ret;
  10408. }
  10409. /*******************************************************************************
  10410. * FUNCTION: _pt_pip2_file_get_stats
  10411. *
  10412. * SUMMARY: Using the BL PIP2 commands get file information from an already
  10413. * opened file
  10414. *
  10415. * NOTE: The DUT must be in BL mode for this command to work
  10416. *
  10417. * RETURNS:
  10418. * !0 = Error
  10419. * 0 = Success
  10420. *
  10421. * PARAMETERS:
  10422. * *dev - pointer to device structure
  10423. * file_handle - File handle to read from
  10424. * *address - pointer to store address of file
  10425. * *file_size _ pointer to store size of file
  10426. ******************************************************************************/
  10427. int _pt_pip2_file_get_stats(struct device *dev, u8 file_handle, u32 *address,
  10428. u32 *file_size)
  10429. {
  10430. int ret = 1;
  10431. u16 status;
  10432. u16 actual_read_len;
  10433. u8 data[2];
  10434. u8 read_buf[16];
  10435. data[0] = file_handle;
  10436. data[1] = PIP2_FILE_IOCTL_CODE_FILE_STATS;
  10437. ret = _pt_request_pip2_send_cmd(dev,
  10438. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10439. data, 2, read_buf, &actual_read_len);
  10440. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10441. if (ret || (status != 0x00)) {
  10442. pt_debug(dev, DL_ERROR,
  10443. "%s ROM FILE_STATS failure: 0x%02X for file = %d, ret = %d\n",
  10444. __func__, status, file_handle, ret);
  10445. ret = -EIO;
  10446. goto exit;
  10447. }
  10448. pt_debug(dev, DL_DEBUG,
  10449. "%s --- FILE %d Information ---\n", __func__, file_handle);
  10450. if (address) {
  10451. *address = read_buf[PIP2_RESP_BODY_OFFSET] +
  10452. (read_buf[PIP2_RESP_BODY_OFFSET + 1] << 8) +
  10453. (read_buf[PIP2_RESP_BODY_OFFSET + 2] << 16) +
  10454. (read_buf[PIP2_RESP_BODY_OFFSET + 3] << 24);
  10455. pt_debug(dev, DL_DEBUG, "%s Address: 0x%08x\n",
  10456. __func__, *address);
  10457. }
  10458. if (file_size) {
  10459. *file_size = read_buf[PIP2_RESP_BODY_OFFSET + 4] +
  10460. (read_buf[PIP2_RESP_BODY_OFFSET + 5] << 8) +
  10461. (read_buf[PIP2_RESP_BODY_OFFSET + 6] << 16) +
  10462. (read_buf[PIP2_RESP_BODY_OFFSET + 7] << 24);
  10463. pt_debug(dev, DL_DEBUG, "%s Size : 0x%08x\n",
  10464. __func__, *file_size);
  10465. }
  10466. exit:
  10467. return ret;
  10468. }
  10469. /*******************************************************************************
  10470. * FUNCTION: _pt_pip2_file_seek_offset
  10471. *
  10472. * SUMMARY: Using the BL PIP2 commands seek read/write offset for an already
  10473. * opened file
  10474. *
  10475. * NOTE: The DUT must be in BL mode for this command to work
  10476. * NOTE: File open/erase command can reset the offset
  10477. *
  10478. * RETURNS:
  10479. * !0 = Error
  10480. * 0 = Success
  10481. *
  10482. * PARAMETERS:
  10483. * *dev - pointer to device structure
  10484. * file_handle - File handle to read from
  10485. * read_offset - read offset of file
  10486. * write_offset - write offset of file
  10487. ******************************************************************************/
  10488. int _pt_pip2_file_seek_offset(struct device *dev, u8 file_handle,
  10489. u32 read_offset, u32 write_offset)
  10490. {
  10491. int ret = 1;
  10492. u16 status;
  10493. u16 actual_read_len;
  10494. u8 data[10];
  10495. u8 read_buf[16];
  10496. data[0] = file_handle;
  10497. data[1] = PIP2_FILE_IOCTL_CODE_SEEK_POINTER;
  10498. data[2] = 0xff & read_offset;
  10499. data[3] = 0xff & (read_offset >> 8);
  10500. data[4] = 0xff & (read_offset >> 16);
  10501. data[5] = 0xff & (read_offset >> 24);
  10502. data[6] = 0xff & write_offset;
  10503. data[7] = 0xff & (write_offset >> 8);
  10504. data[8] = 0xff & (write_offset >> 16);
  10505. data[9] = 0xff & (write_offset >> 24);
  10506. ret = _pt_request_pip2_send_cmd(dev,
  10507. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10508. data, 10, read_buf, &actual_read_len);
  10509. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  10510. if (ret || (status != 0x00)) {
  10511. pt_debug(dev, DL_ERROR,
  10512. "%s ROM FILE_SEEK failure: 0x%02X for file = %d, ret = %d\n",
  10513. __func__, status, ret, file_handle);
  10514. ret = -EIO;
  10515. }
  10516. return ret;
  10517. }
  10518. /*******************************************************************************
  10519. * FUNCTION: _pt_pip2_file_crc
  10520. *
  10521. * SUMMARY: Using the BL PIP2 commands to calculate CRC for a file or portion of
  10522. * the file.
  10523. *
  10524. * NOTE: The DUT must be in BL mode for this command to work
  10525. * NOTE: This command only can be used for BL version 1.8 or greater.
  10526. * BL version 1.8 added this change according to PGV-173.
  10527. *
  10528. * RETURNS:
  10529. * !0 = Error
  10530. * 0 = Success
  10531. *
  10532. * PARAMETERS:
  10533. * *dev - pointer to device structure
  10534. * file_handle - File handle to read from
  10535. * offset - start offset for CRC calculation
  10536. * length - number of bytes to calculate CRC over
  10537. * read_buf - pointer to the read buffer
  10538. ******************************************************************************/
  10539. int _pt_pip2_file_crc(struct device *dev, u8 file_handle,
  10540. u32 offset, u32 length, u8 *read_buf)
  10541. {
  10542. int rc = 1;
  10543. u16 actual_read_len;
  10544. u8 data[10];
  10545. data[0] = file_handle;
  10546. data[1] = PIP2_FILE_IOCTL_CODE_FILE_CRC;
  10547. data[2] = 0xff & offset;
  10548. data[3] = 0xff & (offset >> 8);
  10549. data[4] = 0xff & (offset >> 16);
  10550. data[5] = 0xff & (offset >> 24);
  10551. data[6] = 0xff & length;
  10552. data[7] = 0xff & (length >> 8);
  10553. data[8] = 0xff & (length >> 16);
  10554. data[9] = 0xff & (length >> 24);
  10555. rc = _pt_request_pip2_send_cmd(dev,
  10556. PT_CORE_CMD_UNPROTECTED, PIP2_CMD_ID_FILE_IOCTL,
  10557. data, 10, read_buf, &actual_read_len);
  10558. if (rc)
  10559. pt_debug(dev, DL_ERROR,
  10560. "%s Return FILE_CRC failure, rc = %d\n",
  10561. __func__, rc);
  10562. return rc;
  10563. }
  10564. /*******************************************************************************
  10565. * FUNCTION: pt_pip2_ping_test
  10566. *
  10567. * SUMMARY: BIST type test that uses the PIP2 PING command and ramps up the
  10568. * optional payload from 0 bytes to max_bytes and ensures the PIP2
  10569. * response payload matches what was sent.
  10570. * The max payload size is 247:
  10571. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  10572. *
  10573. * RETURN:
  10574. * 0 = success
  10575. * !0 = failure
  10576. *
  10577. * PARAMETERS:
  10578. * *dev - pointer to device structure
  10579. * *attr - pointer to device attributes
  10580. * *buf - pointer to output buffer
  10581. ******************************************************************************/
  10582. int pt_pip2_ping_test(struct device *dev, int max_bytes, int *last_packet_size)
  10583. {
  10584. u16 actual_read_len;
  10585. u8 *read_buf = NULL;
  10586. u8 *data = NULL;
  10587. int data_offset = PIP2_RESP_STATUS_OFFSET;
  10588. int i = 1;
  10589. int j = 0;
  10590. int rc = 0;
  10591. read_buf = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10592. if (!read_buf)
  10593. goto ping_test_exit;
  10594. data = kzalloc(PT_MAX_PIP2_MSG_SIZE, GFP_KERNEL);
  10595. if (!data)
  10596. goto ping_test_exit;
  10597. /* Load data payload with an array of walking 1's */
  10598. for (i = 0; i < 255; i++)
  10599. data[i] = 0x01 << (i % 8);
  10600. /* Send 'max_bytes' PING cmds using 'i' bytes as payload for each */
  10601. for (i = 0; i <= max_bytes; i++) {
  10602. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_UNPROTECTED,
  10603. PIP2_CMD_ID_PING, data, i, read_buf,
  10604. &actual_read_len);
  10605. if (rc) {
  10606. pt_debug(dev, DL_ERROR,
  10607. "%s: PING failed with %d byte payload\n",
  10608. __func__, i);
  10609. break;
  10610. }
  10611. /* Verify data returned matches data sent */
  10612. for (j = 0; j < i; j++) {
  10613. if (read_buf[data_offset + j] != data[j]) {
  10614. pt_debug(dev, DL_DEBUG,
  10615. "%s: PING packet size %d: sent[%d]=0x%02X recv[%d]=0x%02X\n",
  10616. __func__, i, j, data[j], j,
  10617. read_buf[data_offset + j]);
  10618. goto ping_test_exit;
  10619. }
  10620. }
  10621. }
  10622. ping_test_exit:
  10623. *last_packet_size = i - 1;
  10624. kfree(read_buf);
  10625. kfree(data);
  10626. return rc;
  10627. }
  10628. /*******************************************************************************
  10629. * FUNCTION: _pt_ic_parse_input_hex
  10630. *
  10631. * SUMMARY: Parse a char data array as space delimited hex values into
  10632. * an int array.
  10633. *
  10634. * NOTE: _pt_ic_parse_input() function may have similar work while the type of
  10635. * buffer to store data is "u32". This function is still needed by the
  10636. * "command" sysfs node because the buffer type to store data is "u8".
  10637. *
  10638. * RETURN: Length of parsed data
  10639. *
  10640. * PARAMETERS:
  10641. * *dev - pointer to device structure
  10642. * *buf - pointer to buffer that holds the input array to parse
  10643. * buf_size - size of buf
  10644. * *ic_buf - pointer to array to store parsed data
  10645. * ic_buf_size - max size of ic_buf
  10646. ******************************************************************************/
  10647. static int _pt_ic_parse_input_hex(struct device *dev, const char *buf,
  10648. size_t buf_size, u8 *ic_buf, size_t ic_buf_size)
  10649. {
  10650. const char *pbuf = buf;
  10651. unsigned long value;
  10652. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10653. u32 i = 0;
  10654. u32 j;
  10655. int last = 0;
  10656. int ret;
  10657. pt_debug(dev, DL_DEBUG,
  10658. "%s: pbuf=%p buf=%p size=%zu %s=%d buf=%s\n",
  10659. __func__, pbuf, buf, buf_size, "scan buf size",
  10660. PT_MAX_PIP2_MSG_SIZE, buf);
  10661. while (pbuf <= (buf + buf_size)) {
  10662. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10663. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10664. __func__, "Max cmd size exceeded", i,
  10665. PT_MAX_PIP2_MSG_SIZE);
  10666. return -EINVAL;
  10667. }
  10668. if (i >= ic_buf_size) {
  10669. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10670. __func__, "Buffer size exceeded", i,
  10671. ic_buf_size);
  10672. return -EINVAL;
  10673. }
  10674. while (((*pbuf == ' ') || (*pbuf == ','))
  10675. && (pbuf < (buf + buf_size))) {
  10676. last = *pbuf;
  10677. pbuf++;
  10678. }
  10679. if (pbuf >= (buf + buf_size))
  10680. break;
  10681. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10682. if ((last == ',') && (*pbuf == ',')) {
  10683. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10684. __func__, "Invalid data format.");
  10685. return -EINVAL;
  10686. }
  10687. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10688. && (pbuf < (buf + buf_size))
  10689. && (*pbuf != ' ')
  10690. && (*pbuf != ','); j++) {
  10691. last = *pbuf;
  10692. scan_buf[j] = *pbuf++;
  10693. }
  10694. ret = kstrtoul(scan_buf, 16, &value);
  10695. if (ret < 0) {
  10696. pt_debug(dev, DL_ERROR,
  10697. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10698. "Invalid data format. ", scan_buf,
  10699. "Use \"0xHH,...,0xHH\"", " instead.",
  10700. i, ret);
  10701. return ret;
  10702. }
  10703. ic_buf[i] = value;
  10704. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx",
  10705. __func__, i, value);
  10706. i++;
  10707. }
  10708. return i;
  10709. }
  10710. /*******************************************************************************
  10711. * FUNCTION: _pt_ic_parse_input
  10712. *
  10713. * SUMMARY: Parse user sysfs input data as a space or comma delimited list of
  10714. * hex values or dec values into an int array with the following rules:
  10715. * 1) Hex values must have a "0x" prefix for each element or the first element
  10716. * only
  10717. * 2) Dec values do not have any prefix
  10718. * 3) It is not allowed to have a mix of dec and hex values in the user input
  10719. * string
  10720. *
  10721. * RETURN: Number of values parsed
  10722. *
  10723. * PARAMETERS:
  10724. * *dev - pointer to device structure
  10725. * *buf - pointer to buffer that holds the input array to parse
  10726. * buf_size - size of buf
  10727. * *out_buf - pointer to array to store parsed data
  10728. * out_buf_size - max size of buffer to be stored
  10729. ******************************************************************************/
  10730. static int _pt_ic_parse_input(struct device *dev,
  10731. const char *buf, size_t buf_size,
  10732. u32 *out_buf, size_t out_buf_size)
  10733. {
  10734. const char *pbuf = buf;
  10735. unsigned long value;
  10736. char scan_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  10737. u32 i = 0;
  10738. u32 j;
  10739. int last = 0;
  10740. int ret = 0;
  10741. u8 str_base = 0;
  10742. pt_debug(dev, DL_DEBUG,
  10743. "%s: in_buf_size=%zu out_buf_size=%zu %s=%d buf=%s\n",
  10744. __func__, buf_size, out_buf_size, "scan buf size",
  10745. PT_MAX_PIP2_MSG_SIZE, buf);
  10746. while (pbuf <= (buf + buf_size)) {
  10747. if (i >= PT_MAX_PIP2_MSG_SIZE) {
  10748. pt_debug(dev, DL_ERROR, "%s: %s size=%d max=%d\n",
  10749. __func__, "Max cmd size exceeded", i,
  10750. PT_MAX_PIP2_MSG_SIZE);
  10751. ret = -EINVAL;
  10752. goto error;
  10753. }
  10754. if (i >= out_buf_size) {
  10755. pt_debug(dev, DL_ERROR, "%s: %s size=%d buf_size=%zu\n",
  10756. __func__, "Buffer size exceeded", i,
  10757. out_buf_size);
  10758. ret = -EINVAL;
  10759. goto error;
  10760. }
  10761. while (((*pbuf == ' ') || (*pbuf == ','))
  10762. && (pbuf < (buf + buf_size))) {
  10763. last = *pbuf;
  10764. pbuf++;
  10765. }
  10766. if (pbuf >= (buf + buf_size))
  10767. break;
  10768. memset(scan_buf, 0, PT_MAX_PIP2_MSG_SIZE);
  10769. if ((last == ',') && (*pbuf == ',')) {
  10770. pt_debug(dev, DL_ERROR, "%s: %s \",,\" not allowed.\n",
  10771. __func__, "Invalid data format.");
  10772. ret = -EINVAL;
  10773. goto error;
  10774. }
  10775. for (j = 0; j < (PT_MAX_PIP2_MSG_SIZE - 1)
  10776. && (pbuf < (buf + buf_size))
  10777. && (*pbuf != ' ')
  10778. && (*pbuf != ','); j++) {
  10779. last = *pbuf;
  10780. scan_buf[j] = *pbuf++;
  10781. }
  10782. if (i == 0) {
  10783. if ((strncmp(scan_buf, "0x", 2) == 0) ||
  10784. (strncmp(scan_buf, "0X", 2) == 0))
  10785. str_base = 16;
  10786. else
  10787. str_base = 10;
  10788. } else {
  10789. if (((strncmp(scan_buf, "0x", 2) == 0) ||
  10790. (strncmp(scan_buf, "0X", 2) == 0)) &&
  10791. (str_base == 10)) {
  10792. pt_debug(dev, DL_ERROR,
  10793. "%s: Decimal and Heximal data mixed\n",
  10794. __func__);
  10795. ret = -EINVAL;
  10796. goto error;
  10797. }
  10798. }
  10799. ret = kstrtoul(scan_buf, str_base, &value);
  10800. if (ret < 0) {
  10801. pt_debug(dev, DL_ERROR,
  10802. "%s: %s '%s' %s%s i=%d r=%d\n", __func__,
  10803. "Invalid data format. ", scan_buf,
  10804. "Use \"0xHH,...,0xHH\" or \"DD DD DD ... DD\"",
  10805. " instead.", i, ret);
  10806. goto error;
  10807. }
  10808. out_buf[i] = value;
  10809. pt_debug(dev, DL_DEBUG, "%s: item = %d, value = 0x%02lx(%lu)",
  10810. __func__, i, value, value);
  10811. i++;
  10812. }
  10813. ret = i;
  10814. error:
  10815. return ret;
  10816. }
  10817. #ifdef TTHE_TUNER_SUPPORT
  10818. /*******************************************************************************
  10819. * FUNCTION: tthe_debugfs_open
  10820. *
  10821. * SUMMARY: Open method for tthe_tuner debugfs node. On some hosts the size of
  10822. * PT_MAX_PRBUF_SIZE (equal to PAGE_SIZE) is not large enough to handle
  10823. * printing a large number of fingers and sensor data without overflowing
  10824. * the buffer. tthe_tuner needs ~4K and so the buffer is sized to some
  10825. * even multiple of PAGE_SIZE
  10826. *
  10827. * RETURN:
  10828. * 0 = success
  10829. * !0 = failure
  10830. *
  10831. * PARAMETERS:
  10832. * *inode - file inode number
  10833. * *filp - file pointer to debugfs file
  10834. ******************************************************************************/
  10835. static int tthe_debugfs_open(struct inode *inode, struct file *filp)
  10836. {
  10837. struct pt_core_data *cd = inode->i_private;
  10838. u32 buf_size = PT_MAX_PRBUF_SIZE;
  10839. filp->private_data = inode->i_private;
  10840. if (cd->tthe_buf)
  10841. return -EBUSY;
  10842. while (buf_size < 4096)
  10843. buf_size = buf_size << 1;
  10844. pt_debug(cd->dev, DL_INFO, "%s:PT_MAX_BRBUF_SIZE=%d buf_size=%d\n",
  10845. __func__, (int)PT_MAX_PRBUF_SIZE, (int)buf_size);
  10846. cd->tthe_buf_size = buf_size;
  10847. cd->tthe_buf = kzalloc(cd->tthe_buf_size, GFP_KERNEL);
  10848. if (!cd->tthe_buf)
  10849. return -ENOMEM;
  10850. return 0;
  10851. }
  10852. /*******************************************************************************
  10853. * FUNCTION: tthe_debugfs_close
  10854. *
  10855. * SUMMARY: Close method for tthe_tuner debugfs node.
  10856. *
  10857. * RETURN:
  10858. * 0 = success
  10859. * !0 = failure
  10860. *
  10861. * PARAMETERS:
  10862. * *inode - file inode number
  10863. * *filp - file pointer to debugfs file
  10864. ******************************************************************************/
  10865. static int tthe_debugfs_close(struct inode *inode, struct file *filp)
  10866. {
  10867. struct pt_core_data *cd = filp->private_data;
  10868. filp->private_data = NULL;
  10869. kfree(cd->tthe_buf);
  10870. cd->tthe_buf = NULL;
  10871. return 0;
  10872. }
  10873. /*******************************************************************************
  10874. * FUNCTION: tthe_debugfs_read
  10875. *
  10876. * SUMMARY: Read method for tthe_tuner debugfs node. This function prints
  10877. * tthe_buf to user buffer.
  10878. *
  10879. * RETURN: Size of debugfs data print
  10880. *
  10881. * PARAMETERS:
  10882. * *filp - file pointer to debugfs file
  10883. * *buf - the user space buffer to read to
  10884. * count - the maximum number of bytes to read
  10885. * *ppos - the current position in the buffer
  10886. ******************************************************************************/
  10887. static ssize_t tthe_debugfs_read(struct file *filp, char __user *buf,
  10888. size_t count, loff_t *ppos)
  10889. {
  10890. struct pt_core_data *cd = filp->private_data;
  10891. int size;
  10892. int ret;
  10893. static int partial_read;
  10894. wait_event_interruptible(cd->wait_q,
  10895. cd->tthe_buf_len != 0 || cd->tthe_exit);
  10896. mutex_lock(&cd->tthe_lock);
  10897. if (cd->tthe_exit) {
  10898. mutex_unlock(&cd->tthe_lock);
  10899. return 0;
  10900. }
  10901. if (count > cd->tthe_buf_len)
  10902. size = cd->tthe_buf_len;
  10903. else
  10904. size = count;
  10905. if (!size) {
  10906. mutex_unlock(&cd->tthe_lock);
  10907. return 0;
  10908. }
  10909. if (partial_read) {
  10910. ret = copy_to_user(buf, cd->tthe_buf + partial_read, size);
  10911. partial_read = 0;
  10912. } else {
  10913. ret = copy_to_user(buf, cd->tthe_buf, size);
  10914. }
  10915. if (size == count)
  10916. partial_read = count;
  10917. if (ret == size)
  10918. return -EFAULT;
  10919. size -= ret;
  10920. cd->tthe_buf_len -= size;
  10921. mutex_unlock(&cd->tthe_lock);
  10922. *ppos += size;
  10923. return size;
  10924. }
  10925. static const struct file_operations tthe_debugfs_fops = {
  10926. .open = tthe_debugfs_open,
  10927. .release = tthe_debugfs_close,
  10928. .read = tthe_debugfs_read,
  10929. };
  10930. #endif
  10931. static struct pt_core_nonhid_cmd _pt_core_nonhid_cmd = {
  10932. .start_bl = _pt_request_pip_start_bl,
  10933. .suspend_scanning = _pt_request_pip_suspend_scanning,
  10934. .resume_scanning = _pt_request_pip_resume_scanning,
  10935. .get_param = _pt_request_pip_get_param,
  10936. .set_param = _pt_request_pip_set_param,
  10937. .verify_cfg_block_crc = _pt_request_pip_verify_config_block_crc,
  10938. .get_config_row_size = _pt_request_pip_get_config_row_size,
  10939. .get_data_structure = _pt_request_pip_get_data_structure,
  10940. .run_selftest = _pt_request_pip_run_selftest,
  10941. .get_selftest_result = _pt_request_pip_get_selftest_result,
  10942. .load_self_test_param = _pt_request_pip_load_self_test_param,
  10943. .calibrate_idacs = _pt_request_pip_calibrate_idacs,
  10944. .calibrate_ext = _pt_request_pip_calibrate_ext,
  10945. .initialize_baselines = _pt_request_pip_initialize_baselines,
  10946. .exec_panel_scan = _pt_request_pip_exec_panel_scan,
  10947. .retrieve_panel_scan = _pt_request_pip_retrieve_panel_scan,
  10948. .read_data_block = _pt_request_pip_read_data_block,
  10949. .write_data_block = _pt_request_pip_write_data_block,
  10950. .user_cmd = _pt_request_pip_user_cmd,
  10951. .get_bl_info = _pt_request_pip_bl_get_information,
  10952. .initiate_bl = _pt_request_pip_bl_initiate_bl,
  10953. .launch_app = _pt_request_pip_launch_app,
  10954. .prog_and_verify = _pt_request_pip_bl_program_and_verify,
  10955. .verify_app_integrity = _pt_request_pip_bl_verify_app_integrity,
  10956. .get_panel_id = _pt_request_pip_bl_get_panel_id,
  10957. .pip2_send_cmd = _pt_request_pip2_send_cmd,
  10958. .pip2_send_cmd_no_int = _pt_pip2_send_cmd_no_int,
  10959. .pip2_file_open = _pt_pip2_file_open,
  10960. .pip2_file_close = _pt_pip2_file_close,
  10961. .pip2_file_erase = _pt_pip2_file_erase,
  10962. .read_us_file = _pt_read_us_file,
  10963. .manage_cal_data = _pt_manage_local_cal_data,
  10964. .calc_crc = crc_ccitt_calculate,
  10965. #ifdef TTDL_DIAGNOSTICS
  10966. .pip2_file_read = _pt_pip2_file_read,
  10967. .pip2_file_seek_offset = _pt_pip2_file_seek_offset,
  10968. .pip2_file_get_stats = _pt_pip2_file_get_stats,
  10969. .pip2_file_crc = _pt_pip2_file_crc,
  10970. #endif
  10971. };
  10972. static struct pt_core_commands _pt_core_commands = {
  10973. .subscribe_attention = _pt_subscribe_attention,
  10974. .unsubscribe_attention = _pt_unsubscribe_attention,
  10975. .request_exclusive = _pt_request_exclusive,
  10976. .release_exclusive = _pt_release_exclusive,
  10977. .request_reset = _pt_request_reset,
  10978. .request_pip2_launch_app = _pt_request_pip2_launch_app,
  10979. .request_enum = _pt_request_enum,
  10980. .request_sysinfo = _pt_request_sysinfo,
  10981. .request_loader_pdata = _pt_request_loader_pdata,
  10982. .request_stop_wd = _pt_request_stop_wd,
  10983. .request_start_wd = _pt_request_start_wd,
  10984. .request_get_mode = _pt_request_get_mode,
  10985. .request_active_pip_prot = _pt_request_active_pip_protocol,
  10986. .request_pip2_get_mode_sysmode = _pt_request_pip2_get_mode_sysmode,
  10987. .request_pip2_enter_bl = _pt_request_pip2_enter_bl,
  10988. .request_pip2_bin_hdr = _pt_request_pip2_bin_hdr,
  10989. .request_dut_generation = _pt_request_dut_generation,
  10990. .request_hw_version = _pt_request_hw_version,
  10991. .parse_sysfs_input = _pt_ic_parse_input,
  10992. #ifdef TTHE_TUNER_SUPPORT
  10993. .request_tthe_print = _pt_request_tthe_print,
  10994. #endif
  10995. #ifdef TTDL_DIAGNOSTICS
  10996. .request_toggle_err_gpio = _pt_request_toggle_err_gpio,
  10997. #endif
  10998. .nonhid_cmd = &_pt_core_nonhid_cmd,
  10999. .request_get_fw_mode = _pt_request_get_fw_sys_mode,
  11000. };
  11001. struct pt_core_commands *pt_get_commands(void)
  11002. {
  11003. return &_pt_core_commands;
  11004. }
  11005. EXPORT_SYMBOL_GPL(pt_get_commands);
  11006. static DEFINE_MUTEX(core_list_lock);
  11007. static LIST_HEAD(core_list);
  11008. static DEFINE_MUTEX(module_list_lock);
  11009. static LIST_HEAD(module_list);
  11010. static int core_number;
  11011. /*******************************************************************************
  11012. * FUNCTION: pt_probe_module
  11013. *
  11014. * SUMMARY: Add the module pointer to module_node and call the probe pointer.
  11015. *
  11016. * RETURN:
  11017. * 0 = success
  11018. * !0 = failure
  11019. *
  11020. * PARAMETERS:
  11021. * *cd - pointer to core data
  11022. * *module - pointer to module structure
  11023. ******************************************************************************/
  11024. static int pt_probe_module(struct pt_core_data *cd,
  11025. struct pt_module *module)
  11026. {
  11027. struct module_node *module_node;
  11028. int rc = 0;
  11029. module_node = kzalloc(sizeof(*module_node), GFP_KERNEL);
  11030. if (!module_node)
  11031. return -ENOMEM;
  11032. module_node->module = module;
  11033. mutex_lock(&cd->module_list_lock);
  11034. list_add(&module_node->node, &cd->module_list);
  11035. mutex_unlock(&cd->module_list_lock);
  11036. rc = module->probe(cd->dev, &module_node->data);
  11037. if (rc) {
  11038. /*
  11039. * Remove from the list when probe fails
  11040. * in order not to call release
  11041. */
  11042. mutex_lock(&cd->module_list_lock);
  11043. list_del(&module_node->node);
  11044. mutex_unlock(&cd->module_list_lock);
  11045. kfree(module_node);
  11046. goto exit;
  11047. }
  11048. exit:
  11049. return rc;
  11050. }
  11051. /*******************************************************************************
  11052. * FUNCTION: pt_release_module
  11053. *
  11054. * SUMMARY: Call the release pointer and remove the module pointer from
  11055. * module_list.
  11056. *
  11057. * PARAMETERS:
  11058. * *cd - pointer to core data
  11059. * *module - pointer to module structure
  11060. ******************************************************************************/
  11061. static void pt_release_module(struct pt_core_data *cd,
  11062. struct pt_module *module)
  11063. {
  11064. struct module_node *m, *m_n;
  11065. mutex_lock(&cd->module_list_lock);
  11066. list_for_each_entry_safe(m, m_n, &cd->module_list, node)
  11067. if (m->module == module) {
  11068. module->release(cd->dev, m->data);
  11069. list_del(&m->node);
  11070. kfree(m);
  11071. break;
  11072. }
  11073. mutex_unlock(&cd->module_list_lock);
  11074. }
  11075. /*******************************************************************************
  11076. * FUNCTION: pt_probe_modules
  11077. *
  11078. * SUMMARY: Iterate module_list and probe each module.
  11079. *
  11080. * PARAMETERS:
  11081. * *cd - pointer to core data
  11082. ******************************************************************************/
  11083. static void pt_probe_modules(struct pt_core_data *cd)
  11084. {
  11085. struct pt_module *m;
  11086. int rc = 0;
  11087. mutex_lock(&module_list_lock);
  11088. list_for_each_entry(m, &module_list, node) {
  11089. pt_debug(cd->dev, DL_ERROR, "%s: Probe module %s\n",
  11090. __func__, m->name);
  11091. rc = pt_probe_module(cd, m);
  11092. if (rc)
  11093. pt_debug(cd->dev, DL_ERROR,
  11094. "%s: Probe fails for module %s\n",
  11095. __func__, m->name);
  11096. }
  11097. mutex_unlock(&module_list_lock);
  11098. }
  11099. /*******************************************************************************
  11100. * FUNCTION: pt_release_modules
  11101. *
  11102. * SUMMARY: Iterate module_list and remove each module.
  11103. *
  11104. * PARAMETERS:
  11105. * *cd - pointer to core data
  11106. ******************************************************************************/
  11107. static void pt_release_modules(struct pt_core_data *cd)
  11108. {
  11109. struct pt_module *m;
  11110. mutex_lock(&module_list_lock);
  11111. list_for_each_entry(m, &module_list, node)
  11112. pt_release_module(cd, m);
  11113. mutex_unlock(&module_list_lock);
  11114. }
  11115. /*******************************************************************************
  11116. * FUNCTION: pt_get_core_data
  11117. *
  11118. * SUMMARY: Iterate core_list and get core data.
  11119. *
  11120. * RETURN:
  11121. * pointer to core data or null pointer if fail
  11122. *
  11123. * PARAMETERS:
  11124. * *id - pointer to core id
  11125. ******************************************************************************/
  11126. struct pt_core_data *pt_get_core_data(char *id)
  11127. {
  11128. struct pt_core_data *d;
  11129. list_for_each_entry(d, &core_list, node)
  11130. if (!strncmp(d->core_id, id, 20))
  11131. return d;
  11132. return NULL;
  11133. }
  11134. EXPORT_SYMBOL_GPL(pt_get_core_data);
  11135. /*******************************************************************************
  11136. * FUNCTION: pt_add_core
  11137. *
  11138. * SUMMARY: Add core data to the core_list.
  11139. *
  11140. * PARAMETERS:
  11141. * *dev - pointer to device structure
  11142. ******************************************************************************/
  11143. static void pt_add_core(struct device *dev)
  11144. {
  11145. struct pt_core_data *d;
  11146. struct pt_core_data *cd = dev_get_drvdata(dev);
  11147. mutex_lock(&core_list_lock);
  11148. list_for_each_entry(d, &core_list, node)
  11149. if (d->dev == dev)
  11150. goto unlock;
  11151. list_add(&cd->node, &core_list);
  11152. unlock:
  11153. mutex_unlock(&core_list_lock);
  11154. }
  11155. /*******************************************************************************
  11156. * FUNCTION: pt_del_core
  11157. *
  11158. * SUMMARY: Remove core data from the core_list.
  11159. *
  11160. * PARAMETERS:
  11161. * *dev - pointer to device structure
  11162. ******************************************************************************/
  11163. static void pt_del_core(struct device *dev)
  11164. {
  11165. struct pt_core_data *d, *d_n;
  11166. mutex_lock(&core_list_lock);
  11167. list_for_each_entry_safe(d, d_n, &core_list, node)
  11168. if (d->dev == dev) {
  11169. list_del(&d->node);
  11170. goto unlock;
  11171. }
  11172. unlock:
  11173. mutex_unlock(&core_list_lock);
  11174. }
  11175. /*******************************************************************************
  11176. * FUNCTION: pt_register_module
  11177. *
  11178. * SUMMARY: Register the module to module_list and probe the module for each
  11179. * core.
  11180. *
  11181. * RETURN:
  11182. * 0 = success
  11183. * !0 = failure
  11184. *
  11185. * PARAMETERS:
  11186. * *module - pointer to module structure
  11187. ******************************************************************************/
  11188. int pt_register_module(struct pt_module *module)
  11189. {
  11190. struct pt_module *m;
  11191. struct pt_core_data *cd;
  11192. int rc = 0;
  11193. if (!module || !module->probe || !module->release)
  11194. return -EINVAL;
  11195. mutex_lock(&module_list_lock);
  11196. list_for_each_entry(m, &module_list, node)
  11197. if (m == module) {
  11198. rc = -EEXIST;
  11199. goto unlock;
  11200. }
  11201. list_add(&module->node, &module_list);
  11202. /* Probe the module for each core */
  11203. mutex_lock(&core_list_lock);
  11204. list_for_each_entry(cd, &core_list, node)
  11205. pt_probe_module(cd, module);
  11206. mutex_unlock(&core_list_lock);
  11207. unlock:
  11208. mutex_unlock(&module_list_lock);
  11209. return rc;
  11210. }
  11211. EXPORT_SYMBOL_GPL(pt_register_module);
  11212. /*******************************************************************************
  11213. * FUNCTION: pt_unregister_module
  11214. *
  11215. * SUMMARY: Release the module for each core and remove the module from
  11216. * module_list.
  11217. *
  11218. * RETURN:
  11219. * 0 = success
  11220. * !0 = failure
  11221. *
  11222. * PARAMETERS:
  11223. * *module - pointer to module structure
  11224. ******************************************************************************/
  11225. void pt_unregister_module(struct pt_module *module)
  11226. {
  11227. struct pt_module *m, *m_n;
  11228. struct pt_core_data *cd;
  11229. if (!module)
  11230. return;
  11231. mutex_lock(&module_list_lock);
  11232. /* Release the module for each core */
  11233. mutex_lock(&core_list_lock);
  11234. list_for_each_entry(cd, &core_list, node)
  11235. pt_release_module(cd, module);
  11236. mutex_unlock(&core_list_lock);
  11237. list_for_each_entry_safe(m, m_n, &module_list, node)
  11238. if (m == module) {
  11239. list_del(&m->node);
  11240. break;
  11241. }
  11242. mutex_unlock(&module_list_lock);
  11243. }
  11244. EXPORT_SYMBOL_GPL(pt_unregister_module);
  11245. /*******************************************************************************
  11246. * FUNCTION: pt_get_module_data
  11247. *
  11248. * SUMMARY: Get module data from module_node by module_list.
  11249. *
  11250. * RETURN:
  11251. * pointer to module data
  11252. *
  11253. * PARAMETERS:
  11254. * *dev - pointer to device structure
  11255. * *module - pointer to module structure
  11256. ******************************************************************************/
  11257. void *pt_get_module_data(struct device *dev, struct pt_module *module)
  11258. {
  11259. struct pt_core_data *cd = dev_get_drvdata(dev);
  11260. struct module_node *m;
  11261. void *data = NULL;
  11262. mutex_lock(&cd->module_list_lock);
  11263. list_for_each_entry(m, &cd->module_list, node)
  11264. if (m->module == module) {
  11265. data = m->data;
  11266. break;
  11267. }
  11268. mutex_unlock(&cd->module_list_lock);
  11269. return data;
  11270. }
  11271. EXPORT_SYMBOL(pt_get_module_data);
  11272. #ifdef CONFIG_HAS_EARLYSUSPEND
  11273. /*******************************************************************************
  11274. * FUNCTION: pt_early_suspend
  11275. *
  11276. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_SUSPEND"
  11277. * attention list.
  11278. *
  11279. * PARAMETERS:
  11280. * *h - pointer to early_suspend structure
  11281. ******************************************************************************/
  11282. static void pt_early_suspend(struct early_suspend *h)
  11283. {
  11284. struct pt_core_data *cd =
  11285. container_of(h, struct pt_core_data, es);
  11286. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11287. }
  11288. /*******************************************************************************
  11289. * FUNCTION: pt_late_resume
  11290. *
  11291. * SUMMARY: Android PM architecture function that will call "PT_ATTEN_RESUME"
  11292. * attention list.
  11293. *
  11294. * PARAMETERS:
  11295. * *h - pointer to early_suspend structure
  11296. ******************************************************************************/
  11297. static void pt_late_resume(struct early_suspend *h)
  11298. {
  11299. struct pt_core_data *cd =
  11300. container_of(h, struct pt_core_data, es);
  11301. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11302. }
  11303. /*******************************************************************************
  11304. * FUNCTION: pt_setup_early_suspend
  11305. *
  11306. * SUMMARY: Register early/suspend function to the system.
  11307. *
  11308. * PARAMETERS:
  11309. * *cd - pointer to core data
  11310. ******************************************************************************/
  11311. static void pt_setup_early_suspend(struct pt_core_data *cd)
  11312. {
  11313. cd->es.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  11314. cd->es.suspend = pt_early_suspend;
  11315. cd->es.resume = pt_late_resume;
  11316. register_early_suspend(&cd->es);
  11317. }
  11318. #elif defined(CONFIG_DRM)
  11319. static void pt_resume_work(struct work_struct *work)
  11320. {
  11321. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11322. resume_work);
  11323. int rc = 0;
  11324. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11325. return;
  11326. rc = pt_core_easywake_off(pt_data);
  11327. if (rc < 0) {
  11328. pt_debug(pt_data->dev, DL_ERROR,
  11329. "%s: Error on wake\n", __func__);
  11330. return;
  11331. }
  11332. }
  11333. static void pt_suspend_work(struct work_struct *work)
  11334. {
  11335. struct pt_core_data *pt_data = container_of(work, struct pt_core_data,
  11336. suspend_work);
  11337. int rc = 0;
  11338. pt_debug(pt_data->dev, DL_INFO, "%s Start\n", __func__);
  11339. if (pt_data->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11340. return;
  11341. rc = pt_core_easywake_on(pt_data);
  11342. if (rc < 0) {
  11343. pt_debug(pt_data->dev, DL_ERROR, "%s: Error on sleep\n", __func__);
  11344. return;
  11345. }
  11346. pt_debug(pt_data->dev, DL_INFO, "%s Exit\n", __func__);
  11347. }
  11348. /*******************************************************************************
  11349. * FUNCTION: drm_notifier_callback
  11350. *
  11351. * SUMMARY: Call back function for DRM notifier to allow to call
  11352. * resume/suspend attention list.
  11353. *
  11354. * RETURN:
  11355. * 0 = success
  11356. *
  11357. * PARAMETERS:
  11358. * *self - pointer to notifier_block structure
  11359. * event - event type of fb notifier
  11360. * *data - pointer to fb_event structure
  11361. ******************************************************************************/
  11362. static int drm_notifier_callback(struct notifier_block *self,
  11363. unsigned long event, void *data)
  11364. {
  11365. struct pt_core_data *cd =
  11366. container_of(self, struct pt_core_data, fb_notifier);
  11367. struct drm_panel_notifier *evdata = data;
  11368. int *blank;
  11369. pt_debug(cd->dev, DL_INFO, "%s: DRM notifier called!\n", __func__);
  11370. if (!evdata)
  11371. goto exit;
  11372. if (!(event == DRM_PANEL_EARLY_EVENT_BLANK ||
  11373. event == DRM_PANEL_EVENT_BLANK)) {
  11374. pt_debug(cd->dev, DL_INFO, "%s: Event(%lu) do not need process\n",
  11375. __func__, event);
  11376. goto exit;
  11377. }
  11378. blank = evdata->data;
  11379. pt_debug(cd->dev, DL_INFO, "%s: DRM event:%lu,blank:%d fb_state %d sleep state %d ",
  11380. __func__, event, *blank, cd->fb_state, cd->sleep_state);
  11381. pt_debug(cd->dev, DL_INFO, "%s: DRM Power - %s - FB state %d ",
  11382. __func__, (*blank == DRM_PANEL_BLANK_UNBLANK)?"UP":"DOWN", cd->fb_state);
  11383. if (*blank == DRM_PANEL_BLANK_UNBLANK) {
  11384. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11385. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11386. pt_debug(cd->dev, DL_INFO, "%s: resume: event = %lu, not care\n",
  11387. __func__, event);
  11388. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11389. if (cd->fb_state != FB_ON) {
  11390. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11391. __func__);
  11392. queue_work(cd->pt_workqueue, &cd->resume_work);
  11393. #if defined(CONFIG_PM_SLEEP)
  11394. pt_debug(cd->dev, DL_INFO, "%s: Resume notifier called!\n",
  11395. __func__);
  11396. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11397. pt_core_resume_(cd->dev);
  11398. #endif
  11399. cd->fb_state = FB_ON;
  11400. pt_debug(cd->dev, DL_INFO, "%s: Resume notified!\n", __func__);
  11401. }
  11402. }
  11403. } else if (*blank == DRM_PANEL_BLANK_LP) {
  11404. pt_debug(cd->dev, DL_INFO, "%s: LOWPOWER!\n", __func__);
  11405. if (event == DRM_PANEL_EARLY_EVENT_BLANK) {
  11406. if (cd->fb_state != FB_OFF) {
  11407. #if defined(CONFIG_PM_SLEEP)
  11408. pt_debug(cd->dev, DL_INFO, "%s: Suspend notifier called!\n",
  11409. __func__);
  11410. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11411. pt_core_suspend_(cd->dev);
  11412. #endif
  11413. cancel_work_sync(&cd->resume_work);
  11414. queue_work(cd->pt_workqueue, &cd->suspend_work);
  11415. cd->fb_state = FB_OFF;
  11416. pt_debug(cd->dev, DL_INFO, "%s: Suspend notified!\n", __func__);
  11417. }
  11418. } else if (event == DRM_PANEL_EVENT_BLANK) {
  11419. pt_debug(cd->dev, DL_INFO, "%s: suspend: event = %lu, not care\n",
  11420. __func__, event);
  11421. }
  11422. } else {
  11423. pt_debug(cd->dev, DL_INFO, "%s: DRM BLANK(%d) do not need process\n",
  11424. __func__, *blank);
  11425. }
  11426. exit:
  11427. return 0;
  11428. }
  11429. /*******************************************************************************
  11430. * FUNCTION: pt_setup_drm_notifier
  11431. *
  11432. * SUMMARY: Set up call back function into drm notifier.
  11433. *
  11434. * PARAMETERS:
  11435. * *cd - pointer to core data
  11436. ******************************************************************************/
  11437. static void pt_setup_drm_notifier(struct pt_core_data *cd)
  11438. {
  11439. cd->fb_state = FB_NONE;
  11440. cd->fb_notifier.notifier_call = drm_notifier_callback;
  11441. pt_debug(cd->dev, DL_INFO, "%s: Setting up drm notifier\n", __func__);
  11442. if (!active_panel)
  11443. pt_debug(cd->dev, DL_ERROR,
  11444. "%s: Active panel not registered!\n", __func__);
  11445. cd->pt_workqueue = create_singlethread_workqueue("ts_wq");
  11446. if (!cd->pt_workqueue) {
  11447. pt_debug(cd->dev, DL_ERROR,
  11448. "%s: worker thread creation failed !\n", __func__);
  11449. }
  11450. if (cd->pt_workqueue) {
  11451. INIT_WORK(&cd->resume_work, pt_resume_work);
  11452. INIT_WORK(&cd->suspend_work, pt_suspend_work);
  11453. }
  11454. if (active_panel &&
  11455. drm_panel_notifier_register(active_panel,
  11456. &cd->fb_notifier) < 0)
  11457. pt_debug(cd->dev, DL_ERROR,
  11458. "%s: Register notifier failed!\n", __func__);
  11459. }
  11460. #elif defined(CONFIG_FB)
  11461. /*******************************************************************************
  11462. * FUNCTION: fb_notifier_callback
  11463. *
  11464. * SUMMARY: Call back function for FrameBuffer notifier to allow to call
  11465. * resume/suspend attention list.
  11466. *
  11467. * RETURN:
  11468. * 0 = success
  11469. *
  11470. * PARAMETERS:
  11471. * *self - pointer to notifier_block structure
  11472. * event - event type of fb notifier
  11473. * *data - pointer to fb_event structure
  11474. ******************************************************************************/
  11475. static int fb_notifier_callback(struct notifier_block *self,
  11476. unsigned long event, void *data)
  11477. {
  11478. struct pt_core_data *cd =
  11479. container_of(self, struct pt_core_data, fb_notifier);
  11480. struct fb_event *evdata = data;
  11481. int *blank;
  11482. if (event != FB_EVENT_BLANK || !evdata)
  11483. goto exit;
  11484. blank = evdata->data;
  11485. if (*blank == FB_BLANK_UNBLANK) {
  11486. pt_debug(cd->dev, DL_INFO, "%s: UNBLANK!\n", __func__);
  11487. if (cd->fb_state != FB_ON) {
  11488. call_atten_cb(cd, PT_ATTEN_RESUME, 0);
  11489. #if defined(CONFIG_PM_SLEEP)
  11490. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11491. pt_core_resume_(cd->dev);
  11492. #endif
  11493. cd->fb_state = FB_ON;
  11494. }
  11495. } else if (*blank == FB_BLANK_POWERDOWN) {
  11496. pt_debug(cd->dev, DL_INFO, "%s: POWERDOWN!\n", __func__);
  11497. if (cd->fb_state != FB_OFF) {
  11498. #if defined(CONFIG_PM_SLEEP)
  11499. if (cd->cpdata->flags & PT_CORE_FLAG_SKIP_RUNTIME)
  11500. pt_core_suspend_(cd->dev);
  11501. #endif
  11502. call_atten_cb(cd, PT_ATTEN_SUSPEND, 0);
  11503. cd->fb_state = FB_OFF;
  11504. }
  11505. }
  11506. exit:
  11507. return 0;
  11508. }
  11509. /*******************************************************************************
  11510. * FUNCTION: pt_setup_fb_notifier
  11511. *
  11512. * SUMMARY: Set up call back function into fb notifier.
  11513. *
  11514. * PARAMETERS:
  11515. * *cd - pointer to core data
  11516. ******************************************************************************/
  11517. static void pt_setup_fb_notifier(struct pt_core_data *cd)
  11518. {
  11519. int rc = 0;
  11520. cd->fb_state = FB_ON;
  11521. cd->fb_notifier.notifier_call = fb_notifier_callback;
  11522. rc = fb_register_client(&cd->fb_notifier);
  11523. if (rc)
  11524. pt_debug(cd->dev, DL_ERROR,
  11525. "Unable to register fb_notifier: %d\n", rc);
  11526. }
  11527. #endif
  11528. /*******************************************************************************
  11529. * FUNCTION: pt_watchdog_work
  11530. *
  11531. * SUMMARY: This is where the watchdog work is done except if the DUT is
  11532. * sleeping then this function simply returns. If the DUT is awake the
  11533. * first thing is to ensure the IRQ is not stuck asserted meaning that
  11534. * somehow a response is waiting on the DUT that has not been read. If
  11535. * this occurs the message is simply consumed. If or once the IRQ is
  11536. * cleared, a PIP PING message is sent to the DUT and if the response
  11537. * is received the watchdog succeeds and exits, if no response is seen
  11538. * a startup is queued unless the maximum number of startups have already
  11539. * been attempted, in that case a BL is attempted.
  11540. *
  11541. * NOTE: pt_stop_wd_timer() cannot be called within the context of this
  11542. * work thread
  11543. *
  11544. * RETURN: void
  11545. *
  11546. * PARAMETERS:
  11547. * *work - pointer to a work structure for the watchdog work queue
  11548. ******************************************************************************/
  11549. static void pt_watchdog_work(struct work_struct *work)
  11550. {
  11551. int rc = 0;
  11552. struct pt_core_data *cd = container_of(work,
  11553. struct pt_core_data, watchdog_work);
  11554. /*
  11555. * if found the current sleep_state is SS_SLEEPING
  11556. * then no need to request_exclusive, directly return
  11557. */
  11558. if (cd->sleep_state == SS_SLEEPING)
  11559. return;
  11560. #ifdef TTDL_DIAGNOSTICS
  11561. cd->watchdog_count++;
  11562. #endif /* TTDL_DIAGNOSTICS */
  11563. /*
  11564. * The first WD interval was extended to allow DDI to come up.
  11565. * If the WD interval is not the default then adjust timer to the
  11566. * current setting. The user can override value with drv_debug sysfs.
  11567. */
  11568. if (cd->watchdog_interval != PT_WATCHDOG_TIMEOUT) {
  11569. mod_timer_pending(&cd->watchdog_timer, jiffies +
  11570. msecs_to_jiffies(cd->watchdog_interval));
  11571. }
  11572. if (pt_check_irq_asserted(cd)) {
  11573. #ifdef TTDL_DIAGNOSTICS
  11574. cd->watchdog_irq_stuck_count++;
  11575. pt_toggle_err_gpio(cd, PT_ERR_GPIO_IRQ_STUCK);
  11576. #endif /* TTDL_DIAGNOSTICS */
  11577. pt_debug(cd->dev, DL_ERROR,
  11578. "%s: TTDL WD found IRQ asserted, attempt to clear\n",
  11579. __func__);
  11580. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  11581. }
  11582. rc = request_exclusive(cd, cd->dev, PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11583. if (rc < 0) {
  11584. pt_debug(cd->dev, DL_ERROR,
  11585. "%s: fail get exclusive ex=%p own=%p\n",
  11586. __func__, cd->exclusive_dev, cd->dev);
  11587. goto queue_startup;
  11588. }
  11589. rc = pt_pip_null_(cd);
  11590. if (release_exclusive(cd, cd->dev) < 0)
  11591. pt_debug(cd->dev, DL_ERROR,
  11592. "%s: fail to release exclusive\n", __func__);
  11593. queue_startup:
  11594. if (rc) {
  11595. #ifdef TTDL_DIAGNOSTICS
  11596. cd->watchdog_failed_access_count++;
  11597. pt_toggle_err_gpio(cd, PT_ERR_GPIO_EXCLUSIVE_ACCESS);
  11598. #endif /* TTDL_DIAGNOSTICS */
  11599. pt_debug(cd->dev, DL_ERROR,
  11600. "%s: failed to access device in WD, retry count=%d\n",
  11601. __func__, cd->startup_retry_count);
  11602. /* Already tried FW upgrade because of watchdog but failed */
  11603. if (cd->startup_retry_count > PT_WATCHDOG_RETRY_COUNT)
  11604. return;
  11605. if (cd->startup_retry_count++ < PT_WATCHDOG_RETRY_COUNT) {
  11606. /*
  11607. * Any wrapper function that trys to disable the
  11608. * WD killing this worker cannot be called here.
  11609. */
  11610. rc = request_exclusive(cd, cd->dev,
  11611. PT_REQUEST_EXCLUSIVE_TIMEOUT);
  11612. if (rc < 0) {
  11613. pt_debug(cd->dev, DL_ERROR,
  11614. "%s: fail get exclusive ex=%p own=%p\n",
  11615. __func__, cd->exclusive_dev, cd->dev);
  11616. goto exit;
  11617. }
  11618. cd->hw_detected = false;
  11619. cd->startup_status = STARTUP_STATUS_START;
  11620. pt_debug(cd->dev, DL_DEBUG,
  11621. "%s: Startup Status Reset\n", __func__);
  11622. rc = pt_dut_reset_and_wait(cd);
  11623. if (release_exclusive(cd, cd->dev) < 0)
  11624. pt_debug(cd->dev, DL_ERROR,
  11625. "%s: fail to release exclusive\n",
  11626. __func__);
  11627. if (!rc) {
  11628. cd->hw_detected = true;
  11629. if (!cd->flashless_dut)
  11630. pt_queue_enum(cd);
  11631. }
  11632. #ifdef TTDL_DIAGNOSTICS
  11633. cd->wd_xres_count++;
  11634. pt_debug(cd->dev, DL_ERROR,
  11635. "%s: Comm Failed - DUT reset [#%d]\n",
  11636. __func__, cd->wd_xres_count);
  11637. #endif /* TTDL_DIAGNOSTICS */
  11638. } else {
  11639. /*
  11640. * After trying PT_WATCHDOG_RETRY_COUNT times to
  11641. * reset the part to regain communications, try to BL
  11642. */
  11643. pt_debug(cd->dev, DL_ERROR,
  11644. "%s: WD DUT access failure, Start FW Upgrade\n",
  11645. __func__);
  11646. #ifdef TTDL_DIAGNOSTICS
  11647. /*
  11648. * When diagnostics is enabled allow TTDL to keep
  11649. * trying to find the DUT. This allows the DUT to be
  11650. * hot swap-able while the host stays running. In
  11651. * production this may not be wanted as a customer
  11652. * may have several touch drivers and any driver
  11653. * that doesn't match the current DUT should give
  11654. * up trying and give up using the bus.
  11655. */
  11656. pt_debug(cd->dev, DL_INFO,
  11657. "%s: Resetting startup_retry_count\n",
  11658. __func__);
  11659. cd->startup_retry_count = 0;
  11660. #endif /* TTDL_DIAGNOSTICS */
  11661. /*
  11662. * Since fw may be broken,reset sysinfo ready flag
  11663. * to let upgrade function work.
  11664. */
  11665. mutex_lock(&cd->system_lock);
  11666. cd->sysinfo.ready = false;
  11667. mutex_unlock(&cd->system_lock);
  11668. if (cd->active_dut_generation == DUT_UNKNOWN) {
  11669. pt_debug(cd->dev, DL_ERROR,
  11670. "%s: Queue Restart\n", __func__);
  11671. pt_queue_restart(cd);
  11672. } else
  11673. kthread_run(start_fw_upgrade, cd, "pt_loader");
  11674. }
  11675. } else {
  11676. cd->hw_detected = true;
  11677. if (cd->startup_status <= (STARTUP_STATUS_FW_RESET_SENTINEL |
  11678. STARTUP_STATUS_BL_RESET_SENTINEL)) {
  11679. pt_debug(cd->dev, DL_ERROR,
  11680. "%s: HW detected but not enumerated\n",
  11681. __func__);
  11682. pt_queue_enum(cd);
  11683. }
  11684. }
  11685. exit:
  11686. pt_start_wd_timer(cd);
  11687. }
  11688. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  11689. /*******************************************************************************
  11690. * FUNCTION: pt_watchdog_timer
  11691. *
  11692. * SUMMARY: The function that is called when the WD timer expires. If the
  11693. * watchdog work is not already busy schedule the watchdog work queue.
  11694. *
  11695. * RETURN: void
  11696. *
  11697. * PARAMETERS:
  11698. * handle - Handle to the watchdog timer
  11699. ******************************************************************************/
  11700. static void pt_watchdog_timer(unsigned long handle)
  11701. {
  11702. struct pt_core_data *cd = (struct pt_core_data *)handle;
  11703. if (!cd)
  11704. return;
  11705. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11706. __func__);
  11707. if (!work_pending(&cd->watchdog_work))
  11708. schedule_work(&cd->watchdog_work);
  11709. }
  11710. #else
  11711. /*******************************************************************************
  11712. * FUNCTION: pt_watchdog_timer
  11713. *
  11714. * SUMMARY: The function that is called when the WD timer expires. If the
  11715. * watchdog work is not already busy schedule the watchdog work queue.
  11716. *
  11717. * RETURN: void
  11718. *
  11719. * PARAMETERS:
  11720. * *t - Pointer to timer list
  11721. ******************************************************************************/
  11722. static void pt_watchdog_timer(struct timer_list *t)
  11723. {
  11724. struct pt_core_data *cd = from_timer(cd, t, watchdog_timer);
  11725. if (!cd)
  11726. return;
  11727. pt_debug(cd->dev, DL_DEBUG, "%s: Watchdog timer triggered\n",
  11728. __func__);
  11729. if (!work_pending(&cd->watchdog_work))
  11730. schedule_work(&cd->watchdog_work);
  11731. }
  11732. #endif
  11733. /*******************************************************************************
  11734. * Core sysfs show and store functions
  11735. ******************************************************************************/
  11736. /*******************************************************************************
  11737. * FUNCTION: pt_hw_version_show
  11738. *
  11739. * SUMMARY: Gets the HW version for either PIP1.x or PIP2.x DUTS
  11740. * Output data format: [SiliconID].[RevID FamilyID].[PanelID]
  11741. *
  11742. * RETURN: size of data written to sysfs node
  11743. *
  11744. * PARAMETERS:
  11745. * *dev - pointer to device structure
  11746. * *attr - pointer to device attributes structure
  11747. * *buf - pointer to print output buffer
  11748. ******************************************************************************/
  11749. static ssize_t pt_hw_version_show(struct device *dev,
  11750. struct device_attribute *attr, char *buf)
  11751. {
  11752. struct pt_core_data *cd = dev_get_drvdata(dev);
  11753. _pt_request_hw_version(dev, cd->hw_version);
  11754. return scnprintf(buf, PT_MAX_PRBUF_SIZE, "%s\n", cd->hw_version);
  11755. }
  11756. static DEVICE_ATTR(hw_version, 0444, pt_hw_version_show, NULL);
  11757. /*******************************************************************************
  11758. * FUNCTION: pt_drv_version_show
  11759. *
  11760. * SUMMARY: Show method for the drv_version sysfs node that will show the
  11761. * TTDL version information
  11762. *
  11763. * RETURN: Char buffer with printed TTDL version information
  11764. *
  11765. * PARAMETERS:
  11766. * *dev - pointer to device structure
  11767. * *attr - pointer to device attributes
  11768. * *buf - pointer to output buffer
  11769. ******************************************************************************/
  11770. static ssize_t pt_drv_version_show(struct device *dev,
  11771. struct device_attribute *attr, char *buf)
  11772. {
  11773. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  11774. "Driver: %s\nVersion: %s\nDate: %s\n",
  11775. pt_driver_core_name, pt_driver_core_version,
  11776. pt_driver_core_date);
  11777. }
  11778. static DEVICE_ATTR(drv_version, 0444, pt_drv_version_show, NULL);
  11779. static DEVICE_ATTR(drv_ver, 0444, pt_drv_version_show, NULL);
  11780. /*******************************************************************************
  11781. * FUNCTION: pt_fw_version_show
  11782. *
  11783. * SUMMARY: Show method for the fw_version sysfs node that will
  11784. * show the firmware, bootloader and PIP version information
  11785. *
  11786. * RETURN: Size of printed buffer
  11787. *
  11788. * PARAMETERS:
  11789. * *dev - pointer to device structure
  11790. * *attr - pointer to device attributes
  11791. * *buf - pointer to output buffer
  11792. ******************************************************************************/
  11793. static ssize_t pt_fw_version_show(struct device *dev,
  11794. struct device_attribute *attr, char *buf)
  11795. {
  11796. struct pt_core_data *cd = dev_get_drvdata(dev);
  11797. struct pt_ttdata *ttdata;
  11798. int rc = 0;
  11799. if (cd->mode == PT_MODE_OPERATIONAL)
  11800. rc = pt_hid_output_get_sysinfo_(cd);
  11801. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11802. __func__, cd->mode, cd->sysinfo.ready);
  11803. if (cd->sysinfo.ready)
  11804. ttdata = &cd->sysinfo.ttdata;
  11805. else
  11806. rc = -ENODATA;
  11807. if (!rc) {
  11808. return scnprintf(buf, strlen(buf),
  11809. "Status: %d\n"
  11810. "FW : %d.%d.%d\n"
  11811. "Config: %d\n"
  11812. "BL : %d.%d\n"
  11813. "PIP : %d.%d\n",
  11814. rc,
  11815. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  11816. ttdata->revctrl,
  11817. ttdata->fw_ver_conf,
  11818. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  11819. ttdata->pip_ver_major, ttdata->pip_ver_minor);
  11820. } else {
  11821. return scnprintf(buf, strlen(buf),
  11822. "Status: %d\n"
  11823. "FW : n/a\n"
  11824. "Config: n/a\n"
  11825. "BL : n/a\n"
  11826. "PIP : n/a\n",
  11827. rc);
  11828. }
  11829. }
  11830. static DEVICE_ATTR(fw_version, 0444, pt_fw_version_show, NULL);
  11831. /*******************************************************************************
  11832. * FUNCTION: pt_sysinfo_show
  11833. *
  11834. * SUMMARY: Show method for the sysinfo sysfs node that will
  11835. * show all the information from get system information command.
  11836. *
  11837. * RETURN: Size of printed buffer
  11838. *
  11839. * PARAMETERS:
  11840. * *dev - pointer to device structure
  11841. * *attr - pointer to device attributes
  11842. * *buf - pointer to output buffer
  11843. ******************************************************************************/
  11844. static ssize_t pt_sysinfo_show(struct device *dev,
  11845. struct device_attribute *attr, char *buf)
  11846. {
  11847. struct pt_core_data *cd = dev_get_drvdata(dev);
  11848. struct pt_sysinfo *si;
  11849. struct pt_ttdata *ttdata = NULL;
  11850. struct pt_sensing_conf_data *scd = NULL;
  11851. int rc = 0;
  11852. if (cd->mode == PT_MODE_OPERATIONAL) {
  11853. rc = pt_hid_output_get_sysinfo_(cd);
  11854. if (cd->sysinfo.ready) {
  11855. si = &cd->sysinfo;
  11856. ttdata = &si->ttdata;
  11857. scd = &si->sensing_conf_data;
  11858. } else
  11859. rc = -ENODATA;
  11860. } else
  11861. rc = -EPERM;
  11862. pt_debug(cd->dev, DL_INFO, "%s: mode = %d sysinfo.ready = %d\n",
  11863. __func__, cd->mode, cd->sysinfo.ready);
  11864. if (!rc && ttdata && scd) {
  11865. return scnprintf(buf, strlen(buf),
  11866. "Status: %d\n"
  11867. "pip_ver_major: 0x%02X\n"
  11868. "pip_ver_minor: 0x%02X\n"
  11869. "fw_pid : 0x%04X\n"
  11870. "fw_ver_major : 0x%02X\n"
  11871. "fw_ver_minor : 0x%02X\n"
  11872. "revctrl : 0x%08X\n"
  11873. "fw_ver_conf : 0x%04X\n"
  11874. "bl_ver_major : 0x%02X\n"
  11875. "bl_ver_minor : 0x%02X\n"
  11876. "jtag_id_h : 0x%04X\n"
  11877. "jtag_id_l : 0x%04X\n"
  11878. "mfg_id[0] : 0x%02X\n"
  11879. "mfg_id[1] : 0x%02X\n"
  11880. "mfg_id[2] : 0x%02X\n"
  11881. "mfg_id[3] : 0x%02X\n"
  11882. "mfg_id[4] : 0x%02X\n"
  11883. "mfg_id[5] : 0x%02X\n"
  11884. "mfg_id[6] : 0x%02X\n"
  11885. "mfg_id[7] : 0x%02X\n"
  11886. "post_code : 0x%04X\n"
  11887. "electrodes_x : 0x%02X\n"
  11888. "electrodes_y : 0x%02X\n"
  11889. "len_x : 0x%04X\n"
  11890. "len_y : 0x%04X\n"
  11891. "res_x : 0x%04X\n"
  11892. "res_y : 0x%04X\n"
  11893. "max_z : 0x%04X\n"
  11894. "origin_x : 0x%02X\n"
  11895. "origin_y : 0x%02X\n"
  11896. "panel_id : 0x%02X\n"
  11897. "btn : 0x%02X\n"
  11898. "scan_mode : 0x%02X\n"
  11899. "max_num_of_tch_per_refresh_cycle: 0x%02X\n",
  11900. rc,
  11901. ttdata->pip_ver_major,
  11902. ttdata->pip_ver_minor,
  11903. ttdata->fw_pid,
  11904. ttdata->fw_ver_major,
  11905. ttdata->fw_ver_minor,
  11906. ttdata->revctrl,
  11907. ttdata->fw_ver_conf,
  11908. ttdata->bl_ver_major,
  11909. ttdata->bl_ver_minor,
  11910. ttdata->jtag_id_h,
  11911. ttdata->jtag_id_l,
  11912. ttdata->mfg_id[0],
  11913. ttdata->mfg_id[1],
  11914. ttdata->mfg_id[2],
  11915. ttdata->mfg_id[3],
  11916. ttdata->mfg_id[4],
  11917. ttdata->mfg_id[5],
  11918. ttdata->mfg_id[6],
  11919. ttdata->mfg_id[7],
  11920. ttdata->post_code,
  11921. scd->electrodes_x,
  11922. scd->electrodes_y,
  11923. scd->len_x,
  11924. scd->len_y,
  11925. scd->res_x,
  11926. scd->res_y,
  11927. scd->max_z,
  11928. scd->origin_x,
  11929. scd->origin_y,
  11930. scd->panel_id,
  11931. scd->btn,
  11932. scd->scan_mode,
  11933. scd->max_tch);
  11934. } else {
  11935. return scnprintf(buf, strlen(buf),
  11936. "Status: %d\n",
  11937. rc);
  11938. }
  11939. }
  11940. static DEVICE_ATTR(sysinfo, 0444, pt_sysinfo_show, NULL);
  11941. /*******************************************************************************
  11942. * FUNCTION: pt_hw_reset_show
  11943. *
  11944. * SUMMARY: The show method for the hw_reset sysfs node that does a hw reset
  11945. * by toggling the XRES line and then calls the startup function to
  11946. * allow TTDL to re-enumerate the DUT.
  11947. * The printed value reflects the status of the full reset/enum.
  11948. *
  11949. * PARAMETERS:
  11950. * *dev - pointer to Device structure
  11951. * *attr - pointer to the device attribute structure
  11952. * *buf - pointer to buffer to print
  11953. ******************************************************************************/
  11954. static ssize_t pt_hw_reset_show(struct device *dev,
  11955. struct device_attribute *attr, char *buf)
  11956. {
  11957. struct pt_core_data *cd = dev_get_drvdata(dev);
  11958. int rc = 0;
  11959. int time = 0;
  11960. u8 reset_status = 0;
  11961. int t;
  11962. struct pt_hid_desc hid_desc;
  11963. memset(&hid_desc, 0, sizeof(hid_desc));
  11964. /* Only allow DUT reset if no active BL in progress */
  11965. mutex_lock(&cd->firmware_class_lock);
  11966. mutex_lock(&cd->system_lock);
  11967. cd->startup_state = STARTUP_NONE;
  11968. mutex_unlock(&(cd->system_lock));
  11969. pt_stop_wd_timer(cd);
  11970. /* ensure no left over exclusive access is still locked */
  11971. release_exclusive(cd, cd->dev);
  11972. rc = pt_dut_reset(cd, PT_CORE_CMD_PROTECTED);
  11973. if (rc) {
  11974. mutex_unlock(&cd->firmware_class_lock);
  11975. pt_debug(cd->dev, DL_ERROR,
  11976. "%s: HW reset failed rc = %d\n", __func__, rc);
  11977. goto exit_hw_reset;
  11978. }
  11979. reset_status |= 0x01 << 0;
  11980. if (cd->flashless_dut) {
  11981. mutex_unlock(&cd->firmware_class_lock);
  11982. t = wait_event_timeout(cd->wait_q, (cd->fw_updating == true),
  11983. msecs_to_jiffies(200));
  11984. if (IS_TMO(t)) {
  11985. pt_debug(dev, DL_ERROR,
  11986. "%s: Timeout waiting for FW update",
  11987. __func__);
  11988. rc = -ETIME;
  11989. goto exit_hw_reset;
  11990. } else {
  11991. pt_debug(dev, DL_INFO,
  11992. "%s: ----- Wait FW Loading ----",
  11993. __func__);
  11994. rc = _pt_request_wait_for_enum_state(
  11995. dev, 4000, STARTUP_STATUS_FW_RESET_SENTINEL);
  11996. if (rc) {
  11997. pt_debug(cd->dev, DL_ERROR,
  11998. "%s: No FW Sentinel detected rc = %d\n",
  11999. __func__, rc);
  12000. goto exit_hw_reset;
  12001. }
  12002. reset_status |= 0x01 << 1;
  12003. }
  12004. } else {
  12005. /* Wait for any sentinel */
  12006. rc = _pt_request_wait_for_enum_state(dev, 150,
  12007. STARTUP_STATUS_BL_RESET_SENTINEL |
  12008. STARTUP_STATUS_FW_RESET_SENTINEL);
  12009. if (rc) {
  12010. mutex_unlock(&cd->firmware_class_lock);
  12011. pt_debug(cd->dev, DL_ERROR,
  12012. "%s: No Sentinel detected rc = %d\n",
  12013. __func__, rc);
  12014. goto exit_hw_reset;
  12015. }
  12016. /* sleep needed to ensure no cmd is sent while DUT will NAK */
  12017. msleep(30);
  12018. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  12019. rc = pt_get_hid_descriptor_(cd, &hid_desc);
  12020. if (rc < 0) {
  12021. pt_debug(cd->dev, DL_ERROR,
  12022. "%s: Error on getting HID descriptor r=%d\n",
  12023. __func__, rc);
  12024. goto exit_hw_reset;
  12025. }
  12026. cd->mode = pt_get_mode(cd, &hid_desc);
  12027. if (cd->mode == PT_MODE_BOOTLOADER)
  12028. rc = pt_hid_output_bl_launch_app_(cd);
  12029. } else {
  12030. if (cd->mode == PT_MODE_BOOTLOADER)
  12031. rc = pt_pip2_launch_app(dev,
  12032. PT_CORE_CMD_UNPROTECTED);
  12033. }
  12034. if (rc) {
  12035. mutex_unlock(&cd->firmware_class_lock);
  12036. pt_debug(cd->dev, DL_ERROR,
  12037. "%s: PIP launch app failed rc = %d\n",
  12038. __func__, rc);
  12039. goto exit_hw_reset;
  12040. }
  12041. mutex_unlock(&cd->firmware_class_lock);
  12042. reset_status |= 0x01 << 1;
  12043. msleep(20);
  12044. if ((cd->active_dut_generation == DUT_UNKNOWN) ||
  12045. (cd->mode != PT_MODE_OPERATIONAL))
  12046. pt_queue_restart(cd);
  12047. else
  12048. pt_queue_enum(cd);
  12049. }
  12050. while (!(cd->startup_status & STARTUP_STATUS_COMPLETE) && time < 2000) {
  12051. msleep(50);
  12052. pt_debug(cd->dev, DL_INFO,
  12053. "%s: wait %dms for 0x%04X, current enum=0x%04X\n",
  12054. __func__, time, STARTUP_STATUS_COMPLETE,
  12055. cd->startup_status);
  12056. time += 50;
  12057. }
  12058. if (!(cd->startup_status & STARTUP_STATUS_COMPLETE)) {
  12059. rc = -ETIME;
  12060. goto exit_hw_reset;
  12061. }
  12062. pt_debug(cd->dev, DL_INFO, "%s: HW Reset complete. enum=0x%04X\n",
  12063. __func__, cd->startup_status);
  12064. reset_status |= 0x01 << 2;
  12065. pt_start_wd_timer(cd);
  12066. exit_hw_reset:
  12067. return scnprintf(buf, strlen(buf),
  12068. "Status: %d\n"
  12069. "Reset Status: 0x%02X\n", rc, reset_status);
  12070. }
  12071. static DEVICE_ATTR(hw_reset, 0444, pt_hw_reset_show, NULL);
  12072. /*******************************************************************************
  12073. * FUNCTION: pt_pip2_cmd_rsp_store
  12074. *
  12075. * SUMMARY: This is the store method for the raw PIP2 cmd/rsp sysfs node. Any
  12076. * raw PIP2 command echo'd to this node will be sent directly to the DUT.
  12077. * Command byte order:
  12078. * Byte [0] - PIP2 command ID
  12079. * Byte [1-n] - PIP2 command payload
  12080. *
  12081. * RETURN: Size of passed in buffer
  12082. *
  12083. * PARAMETERS:
  12084. * *dev - pointer to device structure
  12085. * *attr - pointer to device attributes
  12086. * *buf - pointer to buffer that hold the command parameters
  12087. * size - size of buf
  12088. ******************************************************************************/
  12089. static ssize_t pt_pip2_cmd_rsp_store(struct device *dev,
  12090. struct device_attribute *attr, const char *buf, size_t size)
  12091. {
  12092. struct pt_core_data *cd = dev_get_drvdata(dev);
  12093. u16 actual_read_len;
  12094. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12095. u8 read_buf[PT_MAX_PIP2_MSG_SIZE + 1];
  12096. u8 pip2_cmd_id = 0x00;
  12097. u8 *pip2_cmd_data = NULL;
  12098. int data_len = 0;
  12099. int length;
  12100. int rc = 0;
  12101. /* clear shared data */
  12102. mutex_lock(&cd->sysfs_lock);
  12103. cd->raw_cmd_status = 0;
  12104. cd->cmd_rsp_buf_len = 0;
  12105. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12106. mutex_unlock(&cd->sysfs_lock);
  12107. length = _pt_ic_parse_input_hex(dev, buf, size,
  12108. input_data, PT_MAX_PIP2_MSG_SIZE);
  12109. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12110. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12111. __func__);
  12112. rc = -EINVAL;
  12113. goto exit;
  12114. }
  12115. /* Send PIP2 command if enough data was provided */
  12116. if (length >= 1) {
  12117. pip2_cmd_id = input_data[0];
  12118. pip2_cmd_data = &input_data[1];
  12119. data_len = length - 1;
  12120. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  12121. pip2_cmd_id, pip2_cmd_data, data_len,
  12122. read_buf, &actual_read_len);
  12123. cd->raw_cmd_status = rc;
  12124. if (rc) {
  12125. pt_debug(dev, DL_ERROR,
  12126. "%s: PIP2 cmd 0x%02x failed rc = %d\n",
  12127. __func__, pip2_cmd_id, rc);
  12128. goto exit;
  12129. } else {
  12130. cd->cmd_rsp_buf_len = actual_read_len;
  12131. memcpy(cd->cmd_rsp_buf, read_buf, actual_read_len);
  12132. pt_debug(dev, DL_ERROR,
  12133. "%s: PIP2 actual_read_len = %d\n",
  12134. __func__, actual_read_len);
  12135. }
  12136. } else {
  12137. rc = -EINVAL;
  12138. pt_debug(dev, DL_ERROR,
  12139. "%s: Insufficient data provided for PIP2 cmd\n",
  12140. __func__);
  12141. }
  12142. exit:
  12143. if (rc)
  12144. return rc;
  12145. return size;
  12146. }
  12147. /*******************************************************************************
  12148. * FUNCTION: pt_pip2_cmd_rsp_show
  12149. *
  12150. * SUMMARY: The show method for the raw pip2_cmd_rsp sysfs node. Any PIP2
  12151. * response generated after using the store method of the pip2_cmd_rsp
  12152. * sysfs node, are available to be read here.
  12153. *
  12154. * PARAMETERS:
  12155. * *dev - pointer to Device structure
  12156. * *attr - pointer to the device attribute structure
  12157. * *buf - pointer to buffer to print
  12158. ******************************************************************************/
  12159. static ssize_t pt_pip2_cmd_rsp_show(struct device *dev,
  12160. struct device_attribute *attr, char *buf)
  12161. {
  12162. struct pt_core_data *cd = dev_get_drvdata(dev);
  12163. int i;
  12164. ssize_t data_len;
  12165. int index;
  12166. mutex_lock(&cd->sysfs_lock);
  12167. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12168. "Status: %d\n", cd->raw_cmd_status);
  12169. if (cd->raw_cmd_status)
  12170. goto error;
  12171. /* Remove the CRC from the length of the response */
  12172. data_len = cd->cmd_rsp_buf_len - 2;
  12173. /* Start printing from the data payload */
  12174. for (i = PIP1_RESP_COMMAND_ID_OFFSET; i < data_len; i++)
  12175. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12176. "%02X ", cd->cmd_rsp_buf[i]);
  12177. if (data_len >= PIP1_RESP_COMMAND_ID_OFFSET) {
  12178. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12179. "\n(%zd bytes)\n",
  12180. data_len - PIP1_RESP_COMMAND_ID_OFFSET);
  12181. } else {
  12182. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12183. "\n(%zd bytes)\n", 0);
  12184. }
  12185. error:
  12186. mutex_unlock(&cd->sysfs_lock);
  12187. return index;
  12188. }
  12189. static DEVICE_ATTR(pip2_cmd_rsp, 0644, pt_pip2_cmd_rsp_show,
  12190. pt_pip2_cmd_rsp_store);
  12191. /*******************************************************************************
  12192. * FUNCTION: pt_command_store
  12193. *
  12194. * SUMMARY: This is the store method for the raw PIP command sysfs node. Any
  12195. * raw PIP command echo'd to this node will be sent directly to the DUT.
  12196. * TTDL will not parse the command.
  12197. *
  12198. * RETURN: Size of passed in buffer
  12199. *
  12200. * PARAMETERS:
  12201. * *dev - pointer to device structure
  12202. * *attr - pointer to device attributes
  12203. * *buf - pointer to buffer that hold the command parameters
  12204. * size - size of buf
  12205. ******************************************************************************/
  12206. static ssize_t pt_command_store(struct device *dev,
  12207. struct device_attribute *attr, const char *buf, size_t size)
  12208. {
  12209. struct pt_core_data *cd = dev_get_drvdata(dev);
  12210. unsigned short crc;
  12211. u16 actual_read_len;
  12212. u8 input_data[PT_MAX_PIP2_MSG_SIZE + 1];
  12213. int length;
  12214. int len_field;
  12215. int rc = 0;
  12216. mutex_lock(&cd->sysfs_lock);
  12217. cd->cmd_rsp_buf_len = 0;
  12218. memset(cd->cmd_rsp_buf, 0, sizeof(cd->cmd_rsp_buf));
  12219. mutex_unlock(&cd->sysfs_lock);
  12220. length = _pt_ic_parse_input_hex(dev, buf, size,
  12221. input_data, PT_MAX_PIP2_MSG_SIZE);
  12222. if (length <= 0 || length > PT_MAX_PIP2_MSG_SIZE) {
  12223. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12224. __func__);
  12225. rc = -EINVAL;
  12226. goto pt_command_store_exit;
  12227. }
  12228. /* PIP2 messages begin with 01 01 */
  12229. if (length >= 2 && input_data[0] == 0x01 && input_data[1] == 0x01) {
  12230. cd->pip2_prot_active = 1;
  12231. /* Override next seq tag with what was sent */
  12232. cd->pip2_cmd_tag_seq = input_data[4] & 0x0F;
  12233. /* For PIP2 cmd if length does not include crc, add it */
  12234. len_field = (input_data[3] << 8) | input_data[2];
  12235. if (len_field == length && length <= 254) {
  12236. crc = crc_ccitt_calculate(&input_data[2],
  12237. length - 2);
  12238. pt_debug(dev, DL_ERROR, "%s: len=%d crc=0x%02X\n",
  12239. __func__, length, crc);
  12240. input_data[length] = (crc & 0xFF00) >> 8;
  12241. input_data[length + 1] = crc & 0x00FF;
  12242. length = length + 2;
  12243. }
  12244. }
  12245. /* write PIP command to log */
  12246. pt_pr_buf(dev, DL_INFO, input_data, length, "command_buf");
  12247. pm_runtime_get_sync(dev);
  12248. rc = pt_hid_output_user_cmd(cd, PT_MAX_INPUT, cd->cmd_rsp_buf,
  12249. length, input_data, &actual_read_len);
  12250. pm_runtime_put(dev);
  12251. mutex_lock(&cd->sysfs_lock);
  12252. cd->raw_cmd_status = rc;
  12253. if (rc) {
  12254. cd->cmd_rsp_buf_len = 0;
  12255. pt_debug(dev, DL_ERROR, "%s: Failed to send command: %s\n",
  12256. __func__, buf);
  12257. } else {
  12258. cd->cmd_rsp_buf_len = actual_read_len;
  12259. }
  12260. cd->pip2_prot_active = 0;
  12261. mutex_unlock(&cd->sysfs_lock);
  12262. pt_command_store_exit:
  12263. if (rc)
  12264. return rc;
  12265. return size;
  12266. }
  12267. static DEVICE_ATTR(command, 0220, NULL, pt_command_store);
  12268. /*******************************************************************************
  12269. * FUNCTION: pt_response_show
  12270. *
  12271. * SUMMARY: The show method for the raw PIP response sysfs node. Any PIP
  12272. * response generated after using the pt_command_store sysfs node, are
  12273. * available to be read here.
  12274. *
  12275. * PARAMETERS:
  12276. * *dev - pointer to Device structure
  12277. * *attr - pointer to the device attribute structure
  12278. * *buf - pointer to buffer to print
  12279. ******************************************************************************/
  12280. static ssize_t pt_response_show(struct device *dev,
  12281. struct device_attribute *attr, char *buf)
  12282. {
  12283. struct pt_core_data *cd = dev_get_drvdata(dev);
  12284. int i;
  12285. ssize_t num_read;
  12286. int index;
  12287. mutex_lock(&cd->sysfs_lock);
  12288. index = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  12289. "Status: %d\n", cd->raw_cmd_status);
  12290. if (cd->raw_cmd_status)
  12291. goto error;
  12292. num_read = cd->cmd_rsp_buf_len;
  12293. for (i = 0; i < num_read; i++)
  12294. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12295. "0x%02X\n", cd->cmd_rsp_buf[i]);
  12296. index += scnprintf(buf + index, PT_MAX_PRBUF_SIZE - index,
  12297. "(%zd bytes)\n", num_read);
  12298. error:
  12299. mutex_unlock(&cd->sysfs_lock);
  12300. return index;
  12301. }
  12302. static DEVICE_ATTR(response, 0444, pt_response_show, NULL);
  12303. /*******************************************************************************
  12304. * FUNCTION: pt_dut_debug_show
  12305. *
  12306. * SUMMARY: Show method for the dut_debug sysfs node. Shows what parameters
  12307. * are available for the store method.
  12308. *
  12309. * RETURN:
  12310. * 0 = success
  12311. * !0 = failure
  12312. *
  12313. * PARAMETERS:
  12314. * *dev - pointer to device structure
  12315. * *attr - pointer to device attributes
  12316. * *buf - pointer to output buffer
  12317. ******************************************************************************/
  12318. static ssize_t pt_dut_debug_show(struct device *dev,
  12319. struct device_attribute *attr, char *buf)
  12320. {
  12321. ssize_t ret;
  12322. ret = scnprintf(buf, strlen(buf),
  12323. "Status: 0\n"
  12324. "dut_debug sends the following commands to the DUT:\n"
  12325. "%d %s \t- %s\n"
  12326. "%d %s \t- %s\n"
  12327. "%d %s \t- %s\n"
  12328. "%d %s \t- %s\n"
  12329. "%d %s \t- %s\n"
  12330. "%d %s \t- %s\n"
  12331. "%d %s \t- %s\n"
  12332. "%d %s \t- %s\n"
  12333. "%d %s \t- %s\n"
  12334. "%d %s \t- %s\n"
  12335. "%d %s \t- %s\n"
  12336. "%d %s \t- %s\n"
  12337. "%d %s \t- %s\n"
  12338. "%d %s \t- %s\n"
  12339. "%d %s \t- %s\n"
  12340. "%d %s \t- %s\n"
  12341. "%d %s \t- %s\n"
  12342. ,
  12343. PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY, "", "BL Verify APP",
  12344. PT_DUT_DBG_HID_RESET, "", "HID Reset",
  12345. PT_DUT_DBG_HID_SET_POWER_ON, "", "HID SET_POWER ON",
  12346. PT_DUT_DBG_HID_SET_POWER_SLEEP, "", "HID SET_POWER SLEEP",
  12347. PT_DUT_DBG_HID_SET_POWER_STANDBY, "", "HID SET_POWER STANDBY",
  12348. PIP1_BL_CMD_ID_GET_INFO, "", "BL Get Info",
  12349. PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY, "", "BL Program & Verify",
  12350. PIP1_BL_CMD_ID_LAUNCH_APP, "", "BL Launch APP",
  12351. PIP1_BL_CMD_ID_INITIATE_BL, "", "BL Initiate BL",
  12352. PT_DUT_DBG_PIP_SOFT_RESET, "", "PIP Soft Reset",
  12353. PT_DUT_DBG_RESET, "", "Toggle the TP_XRES GPIO",
  12354. PT_DUT_DBG_PIP_NULL, "", "PIP NULL (PING)",
  12355. PT_DUT_DBG_PIP_ENTER_BL, "", "PIP enter BL",
  12356. PT_DUT_DBG_HID_SYSINFO, "", "HID system info",
  12357. PT_DUT_DBG_PIP_SUSPEND_SCAN, "", "Suspend Scan",
  12358. PT_DUT_DBG_PIP_RESUME_SCAN, "", "Resume Scan",
  12359. PT_DUT_DBG_HID_DESC, "", "Get HID Desc"
  12360. );
  12361. return ret;
  12362. }
  12363. /*******************************************************************************
  12364. * FUNCTION: pt_drv_debug_show
  12365. *
  12366. * SUMMARY: Show method for the drv_debug sysfs node. Shows what parameters
  12367. * are available for the store method.
  12368. *
  12369. * RETURN:
  12370. * 0 = success
  12371. * !0 = failure
  12372. *
  12373. * PARAMETERS:
  12374. * *dev - pointer to device structure
  12375. * *attr - pointer to device attributes
  12376. * *buf - pointer to output buffer
  12377. ******************************************************************************/
  12378. static ssize_t pt_drv_debug_show(struct device *dev,
  12379. struct device_attribute *attr, char *buf)
  12380. {
  12381. ssize_t ret;
  12382. ret = scnprintf(buf, strlen(buf),
  12383. "Status: 0\n"
  12384. "drv_debug supports the following values:\n"
  12385. "%d %s \t- %s\n"
  12386. "%d %s \t- %s\n"
  12387. "%d %s \t- %s\n"
  12388. "%d %s \t- %s\n"
  12389. "%d %s \t- %s\n"
  12390. "%d %s \t- %s\n"
  12391. "%d %s \t- %s\n"
  12392. "%d %s \t- %s\n"
  12393. "%d %s \t- %s\n"
  12394. "%d %s - %s\n"
  12395. "%d %s \t- %s\n"
  12396. "%d %s \t- %s\n"
  12397. #ifdef TTDL_DIAGNOSTICS
  12398. "%d %s \t- %s\n"
  12399. "%d %s \t- %s\n"
  12400. "%d %s \t- %s\n"
  12401. #endif /* TTDL_DIAGNOSTICS */
  12402. "%d %s \t- %s\n"
  12403. #ifdef TTDL_DIAGNOSTICS
  12404. "%d %s \t- %s\n"
  12405. "%d %s \t- %s\n"
  12406. "%d %s \t- %s\n"
  12407. "%d %s \t- %s\n"
  12408. "%d %s \t- %s\n"
  12409. "%d %s \t- %s\n"
  12410. "%d %s \t- %s\n"
  12411. "%d %s \t- %s\n"
  12412. "%d %s \t- %s\n"
  12413. #endif /* TTDL_DIAGNOSTICS */
  12414. ,
  12415. PT_DRV_DBG_SUSPEND, " ", "Suspend TTDL responding to INT",
  12416. PT_DRV_DBG_RESUME, " ", "Resume TTDL responding to INT",
  12417. PT_DRV_DBG_STOP_WD, " ", "Stop TTDL WD",
  12418. PT_DRV_DBG_START_WD, " ", "Start TTDL WD",
  12419. PT_DRV_DBG_TTHE_TUNER_EXIT, " ", "Exit TTHE Tuner Logging",
  12420. PT_DRV_DBG_TTHE_BUF_CLEAN, " ", "Clear TTHE Tuner buffer",
  12421. PT_DRV_DBG_CLEAR_PARM_LIST, " ", "Clear RAM Param list",
  12422. PT_DRV_DBG_FORCE_BUS_READ, " ", "Force bus read",
  12423. PT_DRV_DBG_CLEAR_CAL_DATA, " ", "Clear CAL Cache",
  12424. PT_DRV_DBG_REPORT_LEVEL, "[0|1|2|3|4]", "Set TTDL Debug Level",
  12425. PT_DRV_DBG_WATCHDOG_INTERVAL, "[n] ", "TTDL WD Interval in ms",
  12426. PT_DRV_DBG_SHOW_TIMESTAMP, "[0|1]", "Show Timestamps"
  12427. #ifdef TTDL_DIAGNOSTICS
  12428. , PT_DRV_DBG_SETUP_PWR, "[0|1]", "Power DUT up/down",
  12429. PT_DRV_DBG_GET_PUT_SYNC, "[0|1]", "Get/Put Linux Sleep",
  12430. PT_DRV_DBG_SET_TT_DATA, "[0|1]", "Display TT_DATA"
  12431. #endif /* TTDL_DIAGNOSTICS */
  12432. , PT_DRV_DBG_SET_GENERATION, "[0|1|2]", "Set DUT generation"
  12433. #ifdef TTDL_DIAGNOSTICS
  12434. , PT_DRV_DBG_SET_BRIDGE_MODE, "[0|1]", "On/Off Bridge Mode",
  12435. PT_DRV_DBG_SET_I2C_ADDRESS, "[0-127]", "I2C DUT Address",
  12436. PT_DRV_DBG_SET_FLASHLESS_DUT, "[0|1]", "Flashless DUT yes/no",
  12437. PT_DRV_DBG_SET_FORCE_SEQ, "[8-15]", "Force PIP2 Sequence #",
  12438. PT_DRV_DBG_BL_WITH_NO_INT, "[0|1]", "BL with no INT",
  12439. PT_DRV_DBG_CAL_CACHE_IN_HOST, "[0|1]", "CAL Cache in host",
  12440. PT_DRV_DBG_MULTI_CHIP, "[0|1]", "Multi Chip Support",
  12441. PT_DRV_DBG_PIP_TIMEOUT, "[100-7000]", "PIP Resp Timeout (ms)",
  12442. PT_DRV_DBG_TTHE_HID_USB_FORMAT, "[0|1]",
  12443. "TTHE_TUNER HID USB Format"
  12444. #endif /* TTDL_DIAGNOSTICS */
  12445. );
  12446. return ret;
  12447. }
  12448. /*******************************************************************************
  12449. * FUNCTION: pt_drv_debug_store
  12450. *
  12451. * SUMMARY: Currently the store method for both sysfs nodes: drv_debug and
  12452. * dut_debug. Drv_debug will contain all functionality that can be run
  12453. * without a DUT preset and is available anytime TTDL is running.
  12454. * Dut_debug requires a DUT to be available and will only be created after
  12455. * a DUT has been detected.
  12456. * This function will eventually be split into two but until the overlap
  12457. * has been depricated this function contains all commands that can be
  12458. * used for TTDL/DUT debugging status and control.
  12459. * All commands require at least one value to be passed in *buf with some
  12460. * requiring two.
  12461. *
  12462. * RETURN: Size of passed in buffer
  12463. *
  12464. * PARAMETERS:
  12465. * *dev - pointer to device structure
  12466. * *attr - pointer to device attributes
  12467. * *buf - pointer to buffer that hold the command parameters
  12468. * size - size of buf
  12469. ******************************************************************************/
  12470. static ssize_t pt_drv_debug_store(struct device *dev,
  12471. struct device_attribute *attr, const char *buf, size_t size)
  12472. {
  12473. struct pt_core_data *cd = dev_get_drvdata(dev);
  12474. unsigned long value;
  12475. int rc = 0;
  12476. u8 return_data[8];
  12477. static u8 wd_disabled;
  12478. u32 input_data[3];
  12479. int length;
  12480. #ifdef TTDL_DIAGNOSTICS
  12481. struct i2c_client *client = to_i2c_client(dev);
  12482. unsigned short crc = 0;
  12483. u16 cal_size;
  12484. #endif
  12485. input_data[0] = 0;
  12486. input_data[1] = 0;
  12487. /* Maximmum input is two elements */
  12488. length = _pt_ic_parse_input(dev, buf, size,
  12489. input_data, ARRAY_SIZE(input_data));
  12490. if (length < 1 || length > 2) {
  12491. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  12492. __func__);
  12493. rc = -EINVAL;
  12494. goto pt_drv_debug_store_exit;
  12495. }
  12496. value = input_data[0];
  12497. if (length == 1) {
  12498. pt_debug(dev, DL_DEBUG,
  12499. "%s: Debug Cmd Received (id=%d)\n",
  12500. __func__, input_data[0]);
  12501. } else if (length == 2) {
  12502. pt_debug(dev, DL_DEBUG,
  12503. "%s: Debug Cmd Received (id=%d, data=%d)\n",
  12504. __func__, input_data[0], input_data[1]);
  12505. } else {
  12506. pt_debug(dev, DL_DEBUG,
  12507. "%s: Invalid arguments received\n", __func__);
  12508. rc = -EINVAL;
  12509. goto pt_drv_debug_store_exit;
  12510. }
  12511. /* Start watchdog timer command */
  12512. if (value == PT_DRV_DBG_START_WD) {
  12513. pt_debug(dev, DL_INFO, "%s: Cmd: Start Watchdog\n", __func__);
  12514. wd_disabled = 0;
  12515. cd->watchdog_force_stop = false;
  12516. pt_start_wd_timer(cd);
  12517. goto pt_drv_debug_store_exit;
  12518. }
  12519. /* Stop watchdog timer temporarily */
  12520. pt_stop_wd_timer(cd);
  12521. if (value == PT_DRV_DBG_STOP_WD) {
  12522. pt_debug(dev, DL_INFO, "%s: Cmd: Stop Watchdog\n", __func__);
  12523. wd_disabled = 1;
  12524. cd->watchdog_force_stop = true;
  12525. goto pt_drv_debug_store_exit;
  12526. }
  12527. switch (value) {
  12528. case PT_DRV_DBG_SUSPEND: /* 4 */
  12529. pt_debug(dev, DL_INFO, "%s: TTDL: Core Sleep\n", __func__);
  12530. wd_disabled = 1;
  12531. rc = pt_core_sleep(cd);
  12532. if (rc)
  12533. pt_debug(dev, DL_ERROR, "%s: Suspend failed rc=%d\n",
  12534. __func__, rc);
  12535. else
  12536. pt_debug(dev, DL_INFO, "%s: Suspend succeeded\n",
  12537. __func__);
  12538. break;
  12539. case PT_DRV_DBG_RESUME: /* 5 */
  12540. pt_debug(dev, DL_INFO, "%s: TTDL: Wake\n", __func__);
  12541. rc = pt_core_wake(cd);
  12542. if (rc)
  12543. pt_debug(dev, DL_ERROR, "%s: Resume failed rc=%d\n",
  12544. __func__, rc);
  12545. else
  12546. pt_debug(dev, DL_INFO, "%s: Resume succeeded\n",
  12547. __func__);
  12548. break;
  12549. case PIP1_BL_CMD_ID_VERIFY_APP_INTEGRITY: /* BL - 49 */
  12550. pt_debug(dev, DL_INFO, "%s: Cmd: verify app integ\n", __func__);
  12551. pt_hid_output_bl_verify_app_integrity(cd, &return_data[0]);
  12552. break;
  12553. case PT_DUT_DBG_HID_RESET: /* 50 */
  12554. pt_debug(dev, DL_INFO, "%s: Cmd: hid_reset\n", __func__);
  12555. pt_hid_cmd_reset(cd);
  12556. break;
  12557. case PT_DUT_DBG_HID_SET_POWER_ON: /* 53 */
  12558. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_on\n", __func__);
  12559. pt_hid_cmd_set_power(cd, HID_POWER_ON);
  12560. wd_disabled = 0;
  12561. break;
  12562. case PT_DUT_DBG_HID_SET_POWER_SLEEP: /* 54 */
  12563. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_off\n",
  12564. __func__);
  12565. wd_disabled = 1;
  12566. pt_hid_cmd_set_power(cd, HID_POWER_SLEEP);
  12567. break;
  12568. case PT_DUT_DBG_HID_SET_POWER_STANDBY: /* 55 */
  12569. pt_debug(dev, DL_INFO, "%s: Cmd: hid_set_power_standby\n",
  12570. __func__);
  12571. wd_disabled = 1;
  12572. pt_hid_cmd_set_power(cd, HID_POWER_STANDBY);
  12573. break;
  12574. case PIP1_BL_CMD_ID_GET_INFO: /* BL - 56 */
  12575. pt_debug(dev, DL_INFO, "%s: Cmd: bl get info\n", __func__);
  12576. pt_hid_output_bl_get_information(cd, return_data);
  12577. break;
  12578. case PIP1_BL_CMD_ID_PROGRAM_AND_VERIFY: /* BL - 57 */
  12579. pt_debug(dev, DL_INFO, "%s: Cmd: program and verify\n",
  12580. __func__);
  12581. pt_hid_output_bl_program_and_verify(cd, 0, NULL);
  12582. break;
  12583. case PIP1_BL_CMD_ID_LAUNCH_APP: /* BL - 59 */
  12584. pt_debug(dev, DL_INFO, "%s: Cmd: launch app\n", __func__);
  12585. pt_hid_output_bl_launch_app(cd);
  12586. break;
  12587. case PIP1_BL_CMD_ID_INITIATE_BL: /* BL - 72 */
  12588. pt_debug(dev, DL_INFO, "%s: Cmd: initiate bl\n", __func__);
  12589. pt_hid_output_bl_initiate_bl(cd, 0, NULL, 0, NULL);
  12590. break;
  12591. case PT_DUT_DBG_PIP_SOFT_RESET: /* 97 */
  12592. pt_debug(dev, DL_INFO, "%s: Cmd: Soft Reset\n", __func__);
  12593. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  12594. break;
  12595. case PT_DUT_DBG_RESET: /* 98 */
  12596. pt_debug(dev, DL_INFO, "%s: Cmd: Hard Reset\n", __func__);
  12597. rc = pt_hw_hard_reset(cd);
  12598. break;
  12599. case PT_DUT_DBG_PIP_NULL: /* 100 */
  12600. pt_debug(dev, DL_INFO, "%s: Cmd: Ping (null)\n", __func__);
  12601. pt_pip_null(cd);
  12602. break;
  12603. case PT_DUT_DBG_PIP_ENTER_BL: /* 101 */
  12604. pt_debug(dev, DL_INFO, "%s: Cmd: start_bootloader\n", __func__);
  12605. rc = pt_pip_start_bootloader(cd);
  12606. if (!rc) {
  12607. cd->startup_status = STARTUP_STATUS_BL_RESET_SENTINEL;
  12608. cd->mode = PT_MODE_BOOTLOADER;
  12609. }
  12610. break;
  12611. case PT_DUT_DBG_HID_SYSINFO: /* 102 */
  12612. pt_debug(dev, DL_INFO, "%s: Cmd: get_sysinfo\n", __func__);
  12613. pt_hid_output_get_sysinfo(cd);
  12614. break;
  12615. case PT_DUT_DBG_PIP_SUSPEND_SCAN: /* 103 */
  12616. pt_debug(dev, DL_INFO, "%s: Cmd: suspend_scanning\n", __func__);
  12617. pt_pip_suspend_scanning(cd);
  12618. break;
  12619. case PT_DUT_DBG_PIP_RESUME_SCAN: /* 104 */
  12620. pt_debug(dev, DL_INFO, "%s: Cmd: resume_scanning\n", __func__);
  12621. pt_pip_resume_scanning(cd);
  12622. break;
  12623. #ifdef TTHE_TUNER_SUPPORT
  12624. case PT_DRV_DBG_TTHE_TUNER_EXIT: /* 107 */
  12625. cd->tthe_exit = 1;
  12626. wake_up(&cd->wait_q);
  12627. kfree(cd->tthe_buf);
  12628. cd->tthe_buf = NULL;
  12629. cd->tthe_exit = 0;
  12630. break;
  12631. case PT_DRV_DBG_TTHE_BUF_CLEAN: /* 108 */
  12632. if (cd->tthe_buf)
  12633. memset(cd->tthe_buf, 0, PT_MAX_PRBUF_SIZE);
  12634. else
  12635. pt_debug(dev, DL_INFO, "%s : tthe_buf not existed\n",
  12636. __func__);
  12637. break;
  12638. #endif
  12639. #ifdef TTDL_DIAGNOSTICS
  12640. case PT_DUT_DBG_HID_DESC: /* 109 */
  12641. pt_debug(dev, DL_INFO, "%s: Cmd: get_hid_desc\n", __func__);
  12642. pt_get_hid_descriptor(cd, &cd->hid_desc);
  12643. break;
  12644. case PT_DRV_DBG_CLEAR_PARM_LIST: /* 110 */
  12645. pt_debug(dev, DL_INFO, "%s: TTDL: Clear Parameter List\n",
  12646. __func__);
  12647. pt_erase_parameter_list(cd);
  12648. break;
  12649. case PT_DRV_DBG_FORCE_BUS_READ: /* 111 */
  12650. rc = pt_read_input(cd);
  12651. if (!rc)
  12652. pt_parse_input(cd);
  12653. break;
  12654. case PT_DRV_DBG_CLEAR_CAL_DATA: /* 112 */
  12655. rc = _pt_manage_local_cal_data(dev, PT_CAL_DATA_CLEAR,
  12656. &cal_size, &crc);
  12657. if (rc)
  12658. pt_debug(dev, DL_ERROR,
  12659. "%s: CAL Data clear failed rc=%d\n",
  12660. __func__, rc);
  12661. else
  12662. pt_debug(dev, DL_INFO,
  12663. "%s: CAL Cleared, Chip ID=0x%04X size=%d\n",
  12664. __func__, crc, size);
  12665. break;
  12666. case PT_DRV_DBG_REPORT_LEVEL: /* 200 */
  12667. mutex_lock(&cd->system_lock);
  12668. if (input_data[1] >= 0 && input_data[1] < DL_MAX) {
  12669. cd->debug_level = input_data[1];
  12670. pt_debug(dev, DL_INFO, "%s: Set debug_level: %d\n",
  12671. __func__, cd->debug_level);
  12672. } else {
  12673. rc = -EINVAL;
  12674. pt_debug(dev, DL_ERROR, "%s: Invalid debug_level: %d\n",
  12675. __func__, input_data[1]);
  12676. }
  12677. mutex_unlock(&(cd->system_lock));
  12678. break;
  12679. #endif
  12680. case PT_DRV_DBG_WATCHDOG_INTERVAL: /* 201 */
  12681. mutex_lock(&cd->system_lock);
  12682. if (input_data[1] > 100) {
  12683. cd->watchdog_interval = input_data[1];
  12684. pt_debug(dev, DL_INFO,
  12685. "%s: Set watchdog_ interval to: %d\n",
  12686. __func__, cd->watchdog_interval);
  12687. pt_start_wd_timer(cd);
  12688. } else {
  12689. rc = -EINVAL;
  12690. pt_debug(dev, DL_ERROR,
  12691. "%s: Invalid watchdog interval: %d\n",
  12692. __func__, input_data[1]);
  12693. }
  12694. mutex_unlock(&(cd->system_lock));
  12695. break;
  12696. #ifdef TTDL_DIAGNOSTICS
  12697. case PT_DRV_DBG_SHOW_TIMESTAMP: /* 202 */
  12698. mutex_lock(&cd->system_lock);
  12699. if (input_data[1] == 0) {
  12700. cd->show_timestamp = 0;
  12701. pt_debug(dev, DL_INFO, "%s: Disable show_timestamp\n",
  12702. __func__);
  12703. } else if (input_data[1] == 1) {
  12704. cd->show_timestamp = 1;
  12705. pt_debug(dev, DL_INFO, "%s: Enable show_timestamp\n",
  12706. __func__);
  12707. } else {
  12708. rc = -EINVAL;
  12709. pt_debug(dev, DL_ERROR,
  12710. "%s: Invalid parameter: %d\n",
  12711. __func__, input_data[1]);
  12712. }
  12713. mutex_unlock(&(cd->system_lock));
  12714. break;
  12715. case PT_DRV_DBG_SETUP_PWR: /* 205 */
  12716. if (input_data[1] == 0) {
  12717. cd->cpdata->setup_power(cd->cpdata,
  12718. PT_MT_POWER_OFF, cd->dev);
  12719. pt_debug(dev, DL_INFO,
  12720. "%s: Initiate Power Off\n", __func__);
  12721. } else if (input_data[1] == 1) {
  12722. cd->cpdata->setup_power(cd->cpdata,
  12723. PT_MT_POWER_ON, cd->dev);
  12724. pt_debug(dev, DL_INFO,
  12725. "%s: Initiate Power On\n", __func__);
  12726. } else {
  12727. rc = -EINVAL;
  12728. pt_debug(dev, DL_ERROR,
  12729. "%s: Invalid parameter: %d\n",
  12730. __func__, input_data[1]);
  12731. }
  12732. break;
  12733. case PT_DRV_DBG_GET_PUT_SYNC: /* 206 */
  12734. if (input_data[1] == 0) {
  12735. pm_runtime_put(dev);
  12736. pt_debug(dev, DL_ERROR,
  12737. "%s: Force call pm_runtime_put()\n", __func__);
  12738. } else if (input_data[1] == 1) {
  12739. pm_runtime_get_sync(dev);
  12740. pt_debug(dev, DL_ERROR,
  12741. "%s: Force call pm_runtime_get_sync()\n",
  12742. __func__);
  12743. } else {
  12744. rc = -EINVAL;
  12745. pt_debug(dev, DL_ERROR,
  12746. "%s: WARNING: Invalid parameter: %d\n",
  12747. __func__, input_data[1]);
  12748. }
  12749. break;
  12750. case PT_DRV_DBG_SET_TT_DATA: /* 208 */
  12751. mutex_lock(&cd->system_lock);
  12752. if (input_data[1] == 0) {
  12753. cd->show_tt_data = false;
  12754. pt_debug(dev, DL_INFO,
  12755. "%s: Disable TT_DATA\n", __func__);
  12756. } else if (input_data[1] == 1) {
  12757. cd->show_tt_data = true;
  12758. pt_debug(dev, DL_INFO,
  12759. "%s: Enable TT_DATA\n", __func__);
  12760. } else {
  12761. rc = -EINVAL;
  12762. pt_debug(dev, DL_ERROR,
  12763. "%s: Invalid parameter: %d\n",
  12764. __func__, input_data[1]);
  12765. }
  12766. mutex_unlock(&(cd->system_lock));
  12767. break;
  12768. case PT_DRV_DBG_SET_GENERATION: /* 210 */
  12769. if (input_data[1] == cd->active_dut_generation) {
  12770. mutex_lock(&cd->system_lock);
  12771. cd->set_dut_generation = true;
  12772. mutex_unlock(&(cd->system_lock));
  12773. } else {
  12774. mutex_lock(&cd->system_lock);
  12775. if (input_data[1] == 0) {
  12776. cd->active_dut_generation = DUT_UNKNOWN;
  12777. cd->set_dut_generation = false;
  12778. } else if (input_data[1] == 1) {
  12779. cd->active_dut_generation = DUT_PIP1_ONLY;
  12780. cd->set_dut_generation = true;
  12781. } else if (input_data[1] == 2) {
  12782. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  12783. cd->set_dut_generation = true;
  12784. } else {
  12785. rc = -EINVAL;
  12786. pt_debug(dev, DL_ERROR,
  12787. "%s: Invalid parameter: %d\n",
  12788. __func__, input_data[1]);
  12789. break;
  12790. }
  12791. cd->startup_status = STARTUP_STATUS_START;
  12792. pt_debug(cd->dev, DL_DEBUG,
  12793. "%s: Startup Status Reset\n", __func__);
  12794. mutex_unlock(&(cd->system_lock));
  12795. pt_debug(dev, DL_INFO,
  12796. "%s: Active DUT Generation Set to: %d\n",
  12797. __func__, cd->active_dut_generation);
  12798. /* Changing DUT generations full restart needed */
  12799. pt_queue_restart(cd);
  12800. }
  12801. break;
  12802. case PT_DRV_DBG_SET_BRIDGE_MODE: /* 211 */
  12803. mutex_lock(&cd->system_lock);
  12804. if (input_data[1] == 0) {
  12805. cd->bridge_mode = false;
  12806. pt_debug(dev, DL_INFO,
  12807. "%s: Disable Bridge Mode\n", __func__);
  12808. } else if (input_data[1] == 1) {
  12809. cd->bridge_mode = true;
  12810. pt_debug(dev, DL_INFO,
  12811. "%s: Enable Bridge Mode\n", __func__);
  12812. } else {
  12813. rc = -EINVAL;
  12814. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12815. __func__, input_data[1]);
  12816. }
  12817. mutex_unlock(&(cd->system_lock));
  12818. break;
  12819. case PT_DRV_DBG_SET_I2C_ADDRESS: /* 212 */
  12820. mutex_lock(&cd->system_lock);
  12821. /* Only a 7bit address is valid */
  12822. if (input_data[1] >= 0 && input_data[1] <= 0x7F) {
  12823. client->addr = input_data[1];
  12824. pt_debug(dev, DL_INFO,
  12825. "%s: Set I2C Address: 0x%2X\n",
  12826. __func__, client->addr);
  12827. } else {
  12828. rc = -EINVAL;
  12829. pt_debug(dev, DL_ERROR, "%s: Invalid I2C Address %d\n",
  12830. __func__, input_data[1]);
  12831. client->addr = 0x24;
  12832. }
  12833. mutex_unlock(&(cd->system_lock));
  12834. break;
  12835. case PT_DRV_DBG_SET_FLASHLESS_DUT: /* 213 */
  12836. mutex_lock(&cd->system_lock);
  12837. if (input_data[1] == 0) {
  12838. cd->flashless_dut = 0;
  12839. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  12840. pt_debug(dev, DL_INFO, "%s: Disable FLAHLESS DUT\n",
  12841. __func__);
  12842. } else if (input_data[1] == 1) {
  12843. cd->flashless_dut = 1;
  12844. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  12845. pt_debug(dev, DL_INFO, "%s: Enable FLAHLESS DUT\n",
  12846. __func__);
  12847. }
  12848. mutex_unlock(&(cd->system_lock));
  12849. break;
  12850. case PT_DRV_DBG_SET_FORCE_SEQ: /* 214 */
  12851. mutex_lock(&cd->system_lock);
  12852. if (input_data[1] >= 0x8 && input_data[1] <= 0xF) {
  12853. cd->force_pip2_seq = input_data[1];
  12854. cd->pip2_cmd_tag_seq = input_data[1];
  12855. pt_debug(dev, DL_INFO,
  12856. "%s: Force PIP2 Seq to: 0x%02X\n",
  12857. __func__, input_data[1]);
  12858. } else {
  12859. cd->force_pip2_seq = 0;
  12860. pt_debug(dev, DL_INFO,
  12861. "%s: Clear Forced PIP2 Seq\n", __func__);
  12862. }
  12863. mutex_unlock(&(cd->system_lock));
  12864. break;
  12865. case PT_DRV_DBG_BL_WITH_NO_INT: /* 215 */
  12866. mutex_lock(&cd->system_lock);
  12867. if (input_data[1] == 0) {
  12868. cd->bl_with_no_int = 0;
  12869. pt_debug(dev, DL_INFO, "%s: BL using IRQ\n", __func__);
  12870. } else if (input_data[1] == 1) {
  12871. cd->bl_with_no_int = 1;
  12872. pt_debug(dev, DL_INFO, "%s: BL using Polling\n",
  12873. __func__);
  12874. } else {
  12875. rc = -EINVAL;
  12876. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12877. __func__, input_data[1]);
  12878. }
  12879. mutex_unlock(&(cd->system_lock));
  12880. break;
  12881. case PT_DRV_DBG_CAL_CACHE_IN_HOST: /* 216 */
  12882. mutex_lock(&cd->system_lock);
  12883. if (input_data[1] == 0) {
  12884. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  12885. pt_debug(dev, DL_INFO,
  12886. "%s: Disable Calibration cache in host\n",
  12887. __func__);
  12888. } else if (input_data[1] == 1) {
  12889. cd->cal_cache_in_host = PT_FEATURE_ENABLE;
  12890. pt_debug(dev, DL_INFO,
  12891. "%s: Enable Calibration cache in host\n",
  12892. __func__);
  12893. } else {
  12894. rc = -EINVAL;
  12895. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12896. __func__, input_data[1]);
  12897. }
  12898. mutex_unlock(&(cd->system_lock));
  12899. break;
  12900. case PT_DRV_DBG_MULTI_CHIP: /* 217 */
  12901. mutex_lock(&cd->system_lock);
  12902. if (input_data[1] == 0) {
  12903. cd->multi_chip = PT_FEATURE_DISABLE;
  12904. cd->ttdl_bist_select = 0x07;
  12905. pt_debug(dev, DL_INFO,
  12906. "%s: Disable Multi-chip support\n", __func__);
  12907. } else if (input_data[1] == 1) {
  12908. cd->multi_chip = PT_FEATURE_ENABLE;
  12909. cd->ttdl_bist_select = 0x3F;
  12910. pt_debug(dev, DL_INFO,
  12911. "%s: Enable Multi-chip support\n", __func__);
  12912. } else {
  12913. rc = -EINVAL;
  12914. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12915. __func__, input_data[1]);
  12916. }
  12917. mutex_unlock(&(cd->system_lock));
  12918. break;
  12919. case PT_DRV_DBG_SET_PANEL_ID_TYPE: /* 218 */
  12920. mutex_lock(&cd->system_lock);
  12921. if (input_data[1] <= 0x07) {
  12922. cd->panel_id_support = input_data[1];
  12923. pt_debug(dev, DL_INFO,
  12924. "%s: Set panel_id_support to %d\n",
  12925. __func__, cd->panel_id_support);
  12926. } else {
  12927. rc = -EINVAL;
  12928. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12929. __func__, input_data[1]);
  12930. }
  12931. mutex_unlock(&(cd->system_lock));
  12932. break;
  12933. case PT_DRV_DBG_PIP_TIMEOUT: /* 219 */
  12934. mutex_lock(&cd->system_lock);
  12935. if (input_data[1] >= 100 && input_data[1] <= 7000) {
  12936. /*
  12937. * The timeout is changed for some cases so the
  12938. * pip_cmd_timeout_default is used to retore back to
  12939. * what the user requested as the new timeout.
  12940. */
  12941. cd->pip_cmd_timeout_default = input_data[1];
  12942. cd->pip_cmd_timeout = input_data[1];
  12943. pt_debug(dev, DL_INFO,
  12944. "%s: PIP Timeout = %d\n", __func__,
  12945. cd->pip_cmd_timeout_default);
  12946. } else {
  12947. rc = -EINVAL;
  12948. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12949. __func__, input_data[1]);
  12950. }
  12951. mutex_unlock(&(cd->system_lock));
  12952. break;
  12953. case PT_DRV_DBG_TTHE_HID_USB_FORMAT: /* 220 */
  12954. mutex_lock(&cd->system_lock);
  12955. if (input_data[1] == 0) {
  12956. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  12957. pt_debug(dev, DL_INFO,
  12958. "%s: Disable tthe_tuner HID-USB format\n",
  12959. __func__);
  12960. } else if (input_data[1] == 1) {
  12961. cd->tthe_hid_usb_format = PT_FEATURE_ENABLE;
  12962. pt_debug(dev, DL_INFO,
  12963. "%s: Enable tthe_tuner HID-USB format\n",
  12964. __func__);
  12965. } else {
  12966. rc = -EINVAL;
  12967. pt_debug(dev, DL_ERROR, "%s: Invalid parameter: %d\n",
  12968. __func__, input_data[1]);
  12969. }
  12970. mutex_unlock(&(cd->system_lock));
  12971. break;
  12972. #endif /* TTDL_DIAGNOSTICS */
  12973. default:
  12974. rc = -EINVAL;
  12975. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  12976. }
  12977. /* Enable watchdog timer */
  12978. if (!wd_disabled)
  12979. pt_start_wd_timer(cd);
  12980. pt_drv_debug_store_exit:
  12981. if (rc)
  12982. return rc;
  12983. return size;
  12984. }
  12985. static DEVICE_ATTR(drv_debug, 0644, pt_drv_debug_show,
  12986. pt_drv_debug_store);
  12987. /*******************************************************************************
  12988. * FUNCTION: pt_sleep_status_show
  12989. *
  12990. * SUMMARY: Show method for the sleep_status sysfs node that will show the
  12991. * sleep status as either ON or OFF
  12992. *
  12993. * RETURN:
  12994. * 0 = success
  12995. * !0 = failure
  12996. *
  12997. * PARAMETERS:
  12998. * *dev - pointer to device structure
  12999. * *attr - pointer to device attributes
  13000. * *buf - pointer to output buffer
  13001. ******************************************************************************/
  13002. static ssize_t pt_sleep_status_show(struct device *dev,
  13003. struct device_attribute *attr, char *buf)
  13004. {
  13005. struct pt_core_data *cd = dev_get_drvdata(dev);
  13006. ssize_t ret;
  13007. mutex_lock(&cd->system_lock);
  13008. if (cd->sleep_state == SS_SLEEP_ON)
  13009. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "off\n");
  13010. else
  13011. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "on\n");
  13012. mutex_unlock(&cd->system_lock);
  13013. return ret;
  13014. }
  13015. /*******************************************************************************
  13016. * FUNCTION: pt_panel_id_show
  13017. *
  13018. * SUMMARY: Show method for the panel_id sysfs node that will show the
  13019. * detected panel ID from the DUT
  13020. *
  13021. * RETURN:
  13022. * 0 = success
  13023. * !0 = failure
  13024. *
  13025. * PARAMETERS:
  13026. * *dev - pointer to device structure
  13027. * *attr - pointer to device attributes
  13028. * *buf - pointer to output buffer
  13029. ******************************************************************************/
  13030. static ssize_t pt_panel_id_show(struct device *dev,
  13031. struct device_attribute *attr, char *buf)
  13032. {
  13033. struct pt_core_data *cd = dev_get_drvdata(dev);
  13034. ssize_t ret;
  13035. u8 pid = PANEL_ID_NOT_ENABLED;
  13036. int rc = 0;
  13037. if (cd->active_dut_generation == DUT_PIP1_ONLY) {
  13038. /*
  13039. * The DUT should report the same panel ID from both the BL and
  13040. * the FW unless the panel_id feature is set to only
  13041. * PT_PANEL_ID_BY_SYS_INFO, in which case the BL is not able
  13042. * to retrieve the panel_id.
  13043. */
  13044. if (cd->mode == PT_MODE_BOOTLOADER) {
  13045. /*
  13046. * Return the stored value if PT_PANEL_ID_BY_BL
  13047. * is not supported while other feature exits.
  13048. */
  13049. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13050. rc = pt_hid_output_bl_get_panel_id_(cd, &pid);
  13051. if (rc) {
  13052. pt_debug(dev, DL_ERROR, "%s: %s %s\n",
  13053. "Failed to retrieve Panel ID. ",
  13054. "Using cached value\n",
  13055. __func__);
  13056. }
  13057. }
  13058. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13059. if (cd->panel_id_support &
  13060. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13061. /* For all systems sysinfo has the panel_id */
  13062. rc = pt_hid_output_get_sysinfo(cd);
  13063. if (!rc)
  13064. pid =
  13065. cd->sysinfo.sensing_conf_data.panel_id;
  13066. pt_debug(dev, DL_ERROR,
  13067. "%s: Gen6 FW mode rc=%d PID=0x%02X\n",
  13068. __func__, rc, pid);
  13069. }
  13070. } else {
  13071. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13072. __func__);
  13073. rc = -EPERM;
  13074. }
  13075. } else if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  13076. if (cd->mode == PT_MODE_BOOTLOADER) {
  13077. if (cd->panel_id_support & PT_PANEL_ID_BY_BL) {
  13078. rc = _pt_pip2_get_panel_id_by_gpio(cd, &pid);
  13079. if (rc) {
  13080. pt_debug(dev, DL_ERROR,
  13081. "%s: BL get panel ID failed rc=%d\n",
  13082. __func__, rc);
  13083. }
  13084. }
  13085. } else if (cd->mode == PT_MODE_OPERATIONAL) {
  13086. if (cd->panel_id_support &
  13087. (PT_PANEL_ID_BY_BL | PT_PANEL_ID_BY_SYS_INFO)) {
  13088. rc = pt_hid_output_get_sysinfo(cd);
  13089. if (!rc)
  13090. pid =
  13091. cd->sysinfo.sensing_conf_data.panel_id;
  13092. pt_debug(dev, DL_ERROR,
  13093. "%s: TT/TC FW mode rc=%d PID=0x%02X\n",
  13094. __func__, rc, pid);
  13095. }
  13096. } else {
  13097. pt_debug(dev, DL_ERROR, "%s: Active mode unknown\n",
  13098. __func__);
  13099. rc = -EPERM;
  13100. }
  13101. } else {
  13102. pt_debug(dev, DL_ERROR, "%s: Dut generation is unknown\n",
  13103. __func__);
  13104. rc = -EPERM;
  13105. }
  13106. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n0x%02X\n",
  13107. rc, pid);
  13108. return ret;
  13109. }
  13110. /*******************************************************************************
  13111. * FUNCTION: pt_get_param_store
  13112. *
  13113. * SUMMARY: Store method for the get_param sysfs node. Stores what parameter
  13114. * ID to retrieve with the show method.
  13115. *
  13116. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13117. *
  13118. * RETURN: Size of passed in buffer if successful
  13119. *
  13120. * PARAMETERS:
  13121. * *dev - pointer to device structure
  13122. * *attr - pointer to device attributes
  13123. * *buf - pointer to output buffer
  13124. * size - size of buf
  13125. ******************************************************************************/
  13126. static ssize_t pt_get_param_store(struct device *dev,
  13127. struct device_attribute *attr, const char *buf, size_t size)
  13128. {
  13129. struct pt_core_data *cd = dev_get_drvdata(dev);
  13130. u32 input_data[2];
  13131. int length;
  13132. int rc = 0;
  13133. /* Maximum input of one value */
  13134. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13135. ARRAY_SIZE(input_data));
  13136. if (length != 1) {
  13137. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13138. __func__);
  13139. rc = -EINVAL;
  13140. goto exit;
  13141. }
  13142. mutex_lock(&cd->system_lock);
  13143. cd->get_param_id = input_data[0];
  13144. mutex_unlock(&(cd->system_lock));
  13145. exit:
  13146. if (rc)
  13147. return rc;
  13148. return size;
  13149. }
  13150. /*******************************************************************************
  13151. * FUNCTION: pt_get_param_show
  13152. *
  13153. * SUMMARY: Show method for the get_param sysfs node. Retrieves the
  13154. * parameter data from the DUT based on the ID stored in the core
  13155. * data variable "get_param_id". If the ID is invalid, the DUT cannot
  13156. * communicate or some other error occures, an error status is returned
  13157. * with no value following.
  13158. * Output is in the form:
  13159. * Status: x
  13160. * 0xyyyyyyyy
  13161. * The 32bit data will only follow the status code if the status == 0
  13162. *
  13163. * NOTE: This sysfs node is only available after a DUT has been enumerated
  13164. *
  13165. * RETURN:
  13166. * 0 = success
  13167. * !0 = failure
  13168. *
  13169. * PARAMETERS:
  13170. * *dev - pointer to device structure
  13171. * *attr - pointer to device attributes
  13172. * *buf - pointer to output buffer
  13173. ******************************************************************************/
  13174. static ssize_t pt_get_param_show(struct device *dev,
  13175. struct device_attribute *attr, char *buf)
  13176. {
  13177. struct pt_core_data *cd = dev_get_drvdata(dev);
  13178. ssize_t ret = 0;
  13179. int status;
  13180. u32 value = 0;
  13181. status = pt_pip_get_param(cd, cd->get_param_id, &value);
  13182. if (status) {
  13183. pt_debug(dev, DL_ERROR, "%s: %s Failed, status = %d\n",
  13184. __func__, "pt_get_param", status);
  13185. ret = scnprintf(buf, strlen(buf),
  13186. "%s %d\n",
  13187. "Status:", status);
  13188. } else {
  13189. pt_debug(dev, DL_DEBUG, "%s: Param [%d] = 0x%04X\n",
  13190. __func__, cd->get_param_id, value);
  13191. ret = scnprintf(buf, strlen(buf),
  13192. "Status: %d\n"
  13193. "0x%04X\n",
  13194. status, value);
  13195. }
  13196. return ret;
  13197. }
  13198. /*******************************************************************************
  13199. * FUNCTION: pt_ttdl_restart_show
  13200. *
  13201. * SUMMARY: Show method for ttdl_restart sysfs node. This node releases all
  13202. * probed modules, calls startup() and then re-probes modules.
  13203. *
  13204. * RETURN: size of data written to sysfs node
  13205. *
  13206. * PARAMETERS:
  13207. * *dev - pointer to device structure
  13208. * *attr - pointer to device attributes structure
  13209. * *buf - pointer to print output buffer
  13210. ******************************************************************************/
  13211. static ssize_t pt_ttdl_restart_show(struct device *dev,
  13212. struct device_attribute *attr, char *buf)
  13213. {
  13214. struct pt_core_data *cd = dev_get_drvdata(dev);
  13215. int t;
  13216. int rc = 0;
  13217. mutex_lock(&cd->system_lock);
  13218. cd->startup_state = STARTUP_NONE;
  13219. mutex_unlock(&(cd->system_lock));
  13220. /* ensure no left over exclusive access is still locked */
  13221. release_exclusive(cd, cd->dev);
  13222. pt_queue_restart(cd);
  13223. t = wait_event_timeout(cd->wait_q,
  13224. (cd->startup_status >= STARTUP_STATUS_COMPLETE),
  13225. msecs_to_jiffies(PT_REQUEST_ENUM_TIMEOUT));
  13226. if (IS_TMO(t)) {
  13227. pt_debug(cd->dev, DL_ERROR,
  13228. "%s: TMO waiting for FW sentinel\n", __func__);
  13229. rc = -ETIME;
  13230. }
  13231. return scnprintf(buf, strlen(buf),
  13232. "Status: %d\n"
  13233. "Enum Status: 0x%04X\n", rc, cd->startup_status);
  13234. }
  13235. static DEVICE_ATTR(ttdl_restart, 0444, pt_ttdl_restart_show, NULL);
  13236. /*******************************************************************************
  13237. * FUNCTION: pt_pip2_gpio_read_show
  13238. *
  13239. * SUMMARY: Sends a PIP2 READ_GPIO command to the DUT and prints the
  13240. * contents of the response to the passed in output buffer.
  13241. *
  13242. * RETURN: size of data written to sysfs node
  13243. *
  13244. * PARAMETERS:
  13245. * *dev - pointer to device structure
  13246. * *attr - pointer to device attributes structure
  13247. * *buf - pointer to print output buffer
  13248. ******************************************************************************/
  13249. static ssize_t pt_pip2_gpio_read_show(struct device *dev,
  13250. struct device_attribute *attr, char *buf)
  13251. {
  13252. struct pt_core_data *cd = dev_get_drvdata(dev);
  13253. u8 status = 0;
  13254. u32 gpio_value = 0;
  13255. int rc = 0;
  13256. /* This functionality is only available in the BL */
  13257. if (cd->mode == PT_MODE_BOOTLOADER)
  13258. rc = pt_pip2_read_gpio(dev, &status, &gpio_value);
  13259. else
  13260. rc = -EPERM;
  13261. if (!rc) {
  13262. if (status == 0)
  13263. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13264. "Status: %d\n"
  13265. "DUT GPIO Reg: 0x%08X\n",
  13266. rc, gpio_value);
  13267. else
  13268. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13269. "Status: %d\n"
  13270. "DUT GPIO Reg: n/a\n",
  13271. status);
  13272. } else
  13273. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13274. "Status: %d\n"
  13275. "DUT GPIO Reg: n/a\n",
  13276. rc);
  13277. }
  13278. /*******************************************************************************
  13279. * FUNCTION: pt_pip2_version_show
  13280. *
  13281. * SUMMARY: Sends a PIP2 VERSION command to the DUT and prints the
  13282. * contents of the response to the passed in output buffer.
  13283. *
  13284. * RETURN: size of data written to sysfs node
  13285. *
  13286. * PARAMETERS:
  13287. * *dev - pointer to device structure
  13288. * *attr - pointer to device attributes structure
  13289. * *buf - pointer to print output buffer
  13290. ******************************************************************************/
  13291. static ssize_t pt_pip2_version_show(struct device *dev,
  13292. struct device_attribute *attr, char *buf)
  13293. {
  13294. int rc = 0;
  13295. struct pt_core_data *cd = dev_get_drvdata(dev);
  13296. struct pt_ttdata *ttdata = &cd->sysinfo.ttdata;
  13297. rc = pt_pip2_get_version(cd);
  13298. if (!rc) {
  13299. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13300. "PIP VERSION : %02X.%02X\n"
  13301. "BL VERSION : %02X.%02X\n"
  13302. "FW VERSION : %02X.%02X\n"
  13303. "SILICON ID : %04X.%04X\n"
  13304. "UID : 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
  13305. ttdata->pip_ver_major, ttdata->pip_ver_minor,
  13306. ttdata->bl_ver_major, ttdata->bl_ver_minor,
  13307. ttdata->fw_ver_major, ttdata->fw_ver_minor,
  13308. ttdata->chip_id, ttdata->chip_rev,
  13309. ttdata->uid[0], ttdata->uid[1],
  13310. ttdata->uid[2], ttdata->uid[3],
  13311. ttdata->uid[4], ttdata->uid[5],
  13312. ttdata->uid[6], ttdata->uid[7],
  13313. ttdata->uid[8], ttdata->uid[9],
  13314. ttdata->uid[10], ttdata->uid[11]);
  13315. } else {
  13316. pt_debug(dev, DL_ERROR,
  13317. "%s: Failed to retriev PIP2 VERSION data\n", __func__);
  13318. return scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13319. "PIP VERSION : -\n"
  13320. "BL VERSION : -\n"
  13321. "FW VERSION : -\n"
  13322. "SILICON ID : -\n"
  13323. "UID : -\n");
  13324. }
  13325. }
  13326. #ifdef TTDL_DIAGNOSTICS
  13327. /*******************************************************************************
  13328. * FUNCTION: pt_ttdl_status_show
  13329. *
  13330. * SUMMARY: Show method for the ttdl_status sysfs node. Displays TTDL internal
  13331. * variable states and GPIO levels. Additional information printed when
  13332. * TTDL_DIAGNOSTICS is enabled.
  13333. *
  13334. * NOTE: All counters will be reset to 0 when this function is called.
  13335. *
  13336. * RETURN:
  13337. * 0 = success
  13338. * !0 = failure
  13339. *
  13340. * PARAMETERS:
  13341. * *dev - pointer to device structure
  13342. * *attr - pointer to device attributes
  13343. * *buf - pointer to output buffer
  13344. ******************************************************************************/
  13345. static ssize_t pt_ttdl_status_show(struct device *dev,
  13346. struct device_attribute *attr, char *buf)
  13347. {
  13348. struct pt_core_data *cd = dev_get_drvdata(dev);
  13349. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13350. struct i2c_client *client = to_i2c_client(dev);
  13351. ssize_t ret;
  13352. u16 cal_size = 0;
  13353. unsigned short crc = 0;
  13354. if (cd->cal_cache_in_host)
  13355. _pt_manage_local_cal_data(dev,
  13356. PT_CAL_DATA_INFO, &cal_size, &crc);
  13357. ret = scnprintf(buf, strlen(buf),
  13358. "%s: 0x%04X\n"
  13359. "%s: %d\n"
  13360. "%s: %s\n"
  13361. "%s: %s %s\n"
  13362. "%s: %s\n"
  13363. "%s: 0x%02X\n"
  13364. "%s: %s\n"
  13365. "%s: %s\n"
  13366. "%s: %s\n"
  13367. "%s: %s\n"
  13368. "%s: %d\n"
  13369. "%s: %d\n"
  13370. "%s: %s\n"
  13371. "%s: %s\n"
  13372. "%s: %d\n"
  13373. #ifdef TTDL_DIAGNOSTICS
  13374. "%s: %d\n"
  13375. "%s: %d\n"
  13376. "%s: %d\n"
  13377. "%s: %d\n"
  13378. "%s: %d\n"
  13379. "%s: %d\n"
  13380. "%s: %d\n"
  13381. "%s: %d\n"
  13382. "%s: %d\n"
  13383. "%s: %d\n"
  13384. "%s: %s\n"
  13385. "%s: %s\n"
  13386. "%s: %s\n"
  13387. "%s: %d\n"
  13388. "%s: 0x%04X\n"
  13389. "%s: %s\n"
  13390. #endif /* TTDL_DIAGNOSTICS */
  13391. ,
  13392. "Startup Status ", cd->startup_status,
  13393. "TTDL Debug Level ", cd->debug_level,
  13394. "Mode ",
  13395. cd->mode ? (cd->mode == PT_MODE_OPERATIONAL ?
  13396. "Operational" : "BL") : "Unknown",
  13397. "DUT Generation ",
  13398. cd->active_dut_generation ?
  13399. (cd->active_dut_generation == DUT_PIP2_CAPABLE ?
  13400. "PT TC/TT" : "Gen5/6") : "Unknown",
  13401. cd->active_dut_generation ?
  13402. (cd->set_dut_generation == true ?
  13403. "(Set)" : "(Detected)") : "",
  13404. "HW Detected ",
  13405. cd->hw_detected ? "True" : "False",
  13406. "I2C Address ",
  13407. cd->bus_ops->bustype == BUS_I2C ? client->addr : 0,
  13408. "Active Bus Module ",
  13409. cd->bus_ops->bustype == BUS_I2C ? "I2C" : "SPI",
  13410. "Flashless Mode ",
  13411. cd->flashless_dut == 1 ? "Yes" : "No",
  13412. "GPIO state - IRQ ",
  13413. cd->cpdata->irq_stat ?
  13414. (cd->cpdata->irq_stat(cd->cpdata, dev) ?
  13415. "High" : "Low") : "not defined",
  13416. "GPIO state - TP_XRES ",
  13417. pdata->core_pdata->rst_gpio ?
  13418. (gpio_get_value(pdata->core_pdata->rst_gpio) ?
  13419. "High" : "Low") : "not defined",
  13420. "RAM Parm restore list ", pt_count_parameter_list(cd),
  13421. "Startup Retry Count ", cd->startup_retry_count,
  13422. "WD - Manual Force Stop ",
  13423. cd->watchdog_force_stop ? "True" : "False",
  13424. "WD - Enabled ",
  13425. cd->watchdog_enabled ? "True" : "False",
  13426. "WD - Interval (ms) ", cd->watchdog_interval
  13427. #ifdef TTDL_DIAGNOSTICS
  13428. , "WD - Triggered Count ", cd->watchdog_count,
  13429. "WD - IRQ Stuck low count ", cd->watchdog_irq_stuck_count,
  13430. "WD - Device Access Errors ", cd->watchdog_failed_access_count,
  13431. "WD - XRES Count ", cd->wd_xres_count,
  13432. "IRQ Triggered Count ", cd->irq_count,
  13433. "BL Packet Retry Count ", cd->bl_retry_packet_count,
  13434. "PIP2 CRC Error Count ", cd->pip2_crc_error_count,
  13435. "Bus Transmit Error Count ", cd->bus_transmit_error_count,
  13436. "File Erase Timeout Count ", cd->file_erase_timeout_count,
  13437. "Error GPIO trigger type ", cd->err_gpio_type,
  13438. "Exclusive Access Lock ", cd->exclusive_dev ? "Set":"Free",
  13439. "Suppress No-Flash Auto BL ",
  13440. cd->flashless_auto_bl == PT_SUPPRESS_AUTO_BL ?
  13441. "Yes" : "No",
  13442. "Calibration Cache on host ",
  13443. cd->cal_cache_in_host == PT_FEATURE_ENABLE ?
  13444. "Yes" : "No",
  13445. "Calibration Cache size ", cal_size,
  13446. "Calibration Cache chip ID ", crc,
  13447. "Multi-Chip Support ",
  13448. cd->multi_chip == PT_FEATURE_ENABLE ? "Yes" : "No"
  13449. #endif /* TTDL_DIAGNOSTICS */
  13450. );
  13451. #ifdef TTDL_DIAGNOSTICS
  13452. /* Reset all diagnostic counters */
  13453. cd->watchdog_count = 0;
  13454. cd->watchdog_irq_stuck_count = 0;
  13455. cd->watchdog_failed_access_count = 0;
  13456. cd->wd_xres_count = 0;
  13457. cd->irq_count = 0;
  13458. cd->bl_retry_packet_count = 0;
  13459. cd->pip2_crc_error_count = 0;
  13460. cd->bus_transmit_error_count = 0;
  13461. #endif
  13462. return ret;
  13463. }
  13464. static DEVICE_ATTR(ttdl_status, 0444, pt_ttdl_status_show, NULL);
  13465. /*******************************************************************************
  13466. * FUNCTION: pt_pip2_enter_bl_show
  13467. *
  13468. * SUMMARY: Show method for the pip2_enter_bl sysfs node that will force
  13469. * the DUT into the BL and show the success or failure of entering the BL
  13470. *
  13471. * RETURN:
  13472. * 0 = success
  13473. * !0 = failure
  13474. *
  13475. * PARAMETERS:
  13476. * *dev - pointer to device structure
  13477. * *attr - pointer to device attributes
  13478. * *buf - pointer to output buffer
  13479. ******************************************************************************/
  13480. static ssize_t pt_pip2_enter_bl_show(struct device *dev,
  13481. struct device_attribute *attr, char *buf)
  13482. {
  13483. ssize_t ret = 0;
  13484. int rc = 0;
  13485. int result = 0;
  13486. u8 mode = PT_MODE_UNKNOWN;
  13487. struct pt_core_data *cd = dev_get_drvdata(dev);
  13488. /* Turn off the TTDL WD before enter bootloader */
  13489. pt_stop_wd_timer(cd);
  13490. /* Ensure NO enumeration work is queued or will be queued */
  13491. cancel_work_sync(&cd->enum_work);
  13492. mutex_lock(&cd->system_lock);
  13493. cd->bridge_mode = true;
  13494. mutex_unlock(&cd->system_lock);
  13495. /* set mode to operational to avoid any extra PIP traffic */
  13496. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  13497. switch (result) {
  13498. case PT_ENTER_BL_PASS:
  13499. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  13500. "Status: %d\nEntered BL\n", PT_ENTER_BL_PASS);
  13501. break;
  13502. case PT_ENTER_BL_ERROR:
  13503. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13504. rc,
  13505. " Unknown Error");
  13506. break;
  13507. case PT_ENTER_BL_RESET_FAIL:
  13508. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13509. rc,
  13510. " Soft Reset Failed");
  13511. break;
  13512. case PT_ENTER_BL_HID_START_BL_FAIL:
  13513. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13514. rc,
  13515. " PIP Start BL Cmd Failed");
  13516. break;
  13517. case PT_ENTER_BL_CONFIRM_FAIL:
  13518. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13519. rc,
  13520. " Error confirming DUT entered BL");
  13521. break;
  13522. default:
  13523. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13524. rc, " Unknown Error");
  13525. break;
  13526. };
  13527. /* Allow enumeration work to be queued again */
  13528. cd->bridge_mode = false;
  13529. return ret;
  13530. }
  13531. static DEVICE_ATTR(pip2_enter_bl, 0444, pt_pip2_enter_bl_show, NULL);
  13532. /*******************************************************************************
  13533. * FUNCTION: pt_pip2_exit_bl_show
  13534. *
  13535. * SUMMARY: Show method for the pip2_exit_bl sysfs node that will attempt to
  13536. * launch the APP and put the DUT Application mode
  13537. *
  13538. * RETURN:
  13539. * 0 = success
  13540. * !0 = failure
  13541. *
  13542. * PARAMETERS:
  13543. * *dev - pointer to device structure
  13544. * *attr - pointer to device attributes
  13545. * *buf - pointer to output buffer
  13546. ******************************************************************************/
  13547. static ssize_t pt_pip2_exit_bl_show(struct device *dev,
  13548. struct device_attribute *attr, char *buf)
  13549. {
  13550. struct pt_core_data *cd = dev_get_drvdata(dev);
  13551. ssize_t ret = 0;
  13552. int rc = 0;
  13553. u8 status_str[PT_STATUS_STR_LEN];
  13554. rc = pt_pip2_exit_bl_(cd, status_str, PT_STATUS_STR_LEN);
  13555. /*
  13556. * Perform enum if startup_status doesn't reach to
  13557. * STARTUP_STATUS_FW_OUT_OF_BOOT.
  13558. */
  13559. if (!rc && (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT))) {
  13560. rc = pt_enum_with_dut(cd, false, &cd->startup_status);
  13561. if (!(cd->startup_status & STARTUP_STATUS_FW_OUT_OF_BOOT)) {
  13562. strlcpy(status_str,
  13563. "Already in APP mode - FW stuck in Boot mode", sizeof(status_str));
  13564. }
  13565. }
  13566. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n%s\n",
  13567. rc, status_str);
  13568. return ret;
  13569. }
  13570. static DEVICE_ATTR(pip2_exit_bl, 0444, pt_pip2_exit_bl_show, NULL);
  13571. #endif
  13572. #ifdef EASYWAKE_TSG6
  13573. /*******************************************************************************
  13574. * FUNCTION: pt_easy_wakeup_gesture_show
  13575. *
  13576. * SUMMARY: Show method for the easy_wakeup_gesture sysfs node that will show
  13577. * current easy wakeup gesture
  13578. *
  13579. * RETURN:
  13580. * 0 = success
  13581. * !0 = failure
  13582. *
  13583. * PARAMETERS:
  13584. * *dev - pointer to device structure
  13585. * *attr - pointer to device attributes
  13586. * *buf - pointer to output buffer
  13587. ******************************************************************************/
  13588. static ssize_t pt_easy_wakeup_gesture_show(struct device *dev,
  13589. struct device_attribute *attr, char *buf)
  13590. {
  13591. struct pt_core_data *cd = dev_get_drvdata(dev);
  13592. ssize_t ret;
  13593. mutex_lock(&cd->system_lock);
  13594. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "0x%02X\n",
  13595. cd->easy_wakeup_gesture);
  13596. mutex_unlock(&cd->system_lock);
  13597. return ret;
  13598. }
  13599. /*******************************************************************************
  13600. * FUNCTION: pt_easy_wakeup_gesture_store
  13601. *
  13602. * SUMMARY: The store method for the easy_wakeup_gesture sysfs node that
  13603. * allows the wake gesture to be set to a custom value.
  13604. *
  13605. * RETURN: Size of passed in buffer is success
  13606. *
  13607. * PARAMETERS:
  13608. * *dev - pointer to device structure
  13609. * *attr - pointer to device attributes
  13610. * *buf - pointer to buffer that hold the command parameters
  13611. * size - size of buf
  13612. ******************************************************************************/
  13613. static ssize_t pt_easy_wakeup_gesture_store(struct device *dev,
  13614. struct device_attribute *attr, const char *buf, size_t size)
  13615. {
  13616. struct pt_core_data *cd = dev_get_drvdata(dev);
  13617. u32 input_data[2];
  13618. int length;
  13619. int rc = 0;
  13620. /* Maximum input of one value */
  13621. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13622. ARRAY_SIZE(input_data));
  13623. if (length != 1) {
  13624. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13625. __func__);
  13626. rc = -EINVAL;
  13627. goto exit;
  13628. }
  13629. pt_debug(dev, DL_INFO, "%s: features.easywake = 0x%02X\n",
  13630. __func__, cd->features.easywake);
  13631. if (!cd->features.easywake || input_data[0] > 0xFF) {
  13632. rc = -EINVAL;
  13633. goto exit;
  13634. }
  13635. pm_runtime_get_sync(dev);
  13636. mutex_lock(&cd->system_lock);
  13637. if (cd->sysinfo.ready && IS_PIP_VER_GE(&cd->sysinfo, 1, 2)) {
  13638. cd->easy_wakeup_gesture = (u8)input_data[0];
  13639. pt_debug(dev, DL_INFO,
  13640. "%s: Updated easy_wakeup_gesture = 0x%02X\n",
  13641. __func__, cd->easy_wakeup_gesture);
  13642. } else
  13643. rc = -ENODEV;
  13644. mutex_unlock(&cd->system_lock);
  13645. pm_runtime_put(dev);
  13646. exit:
  13647. if (rc)
  13648. return rc;
  13649. return size;
  13650. }
  13651. /*******************************************************************************
  13652. * FUNCTION: pt_easy_wakeup_gesture_id_show
  13653. *
  13654. * SUMMARY: Show method for the easy_wakeup_gesture_id sysfs node that will
  13655. * show the TSG6 easywake gesture ID
  13656. *
  13657. * RETURN:
  13658. * 0 = success
  13659. * !0 = failure
  13660. *
  13661. * PARAMETERS:
  13662. * *dev - pointer to device structure
  13663. * *attr - pointer to device attributes
  13664. * *buf - pointer to output buffer
  13665. ******************************************************************************/
  13666. static ssize_t pt_easy_wakeup_gesture_id_show(struct device *dev,
  13667. struct device_attribute *attr, char *buf)
  13668. {
  13669. struct pt_core_data *cd = dev_get_drvdata(dev);
  13670. ssize_t ret;
  13671. mutex_lock(&cd->system_lock);
  13672. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: 0\n0x%02X\n",
  13673. cd->gesture_id);
  13674. mutex_unlock(&cd->system_lock);
  13675. return ret;
  13676. }
  13677. /*******************************************************************************
  13678. * FUNCTION: pt_easy_wakeup_gesture_data_show
  13679. *
  13680. * SUMMARY: Show method for the easy_wakeup_gesture_data sysfs node that will
  13681. * show the TSG6 easywake gesture data in the following format:
  13682. * x1(LSB),x1(MSB), y1(LSB),y1(MSB), x2(LSB),x2(MSB), y2(LSB),y2(MSB),...
  13683. *
  13684. * RETURN:
  13685. * 0 = success
  13686. * !0 = failure
  13687. *
  13688. * PARAMETERS:
  13689. * *dev - pointer to device structure
  13690. * *attr - pointer to device attributes
  13691. * *buf - pointer to output buffer
  13692. ******************************************************************************/
  13693. static ssize_t pt_easy_wakeup_gesture_data_show(struct device *dev,
  13694. struct device_attribute *attr, char *buf)
  13695. {
  13696. struct pt_core_data *cd = dev_get_drvdata(dev);
  13697. ssize_t ret = 0;
  13698. int i;
  13699. mutex_lock(&cd->system_lock);
  13700. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret, "Status: %d\n", 0);
  13701. for (i = 0; i < cd->gesture_data_length; i++)
  13702. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13703. "0x%02X\n", cd->gesture_data[i]);
  13704. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13705. "(%d bytes)\n", cd->gesture_data_length);
  13706. mutex_unlock(&cd->system_lock);
  13707. return ret;
  13708. }
  13709. #endif /* EASYWAKE_TSG6 */
  13710. #ifdef TTDL_DIAGNOSTICS
  13711. /*******************************************************************************
  13712. * FUNCTION: pt_err_gpio_show
  13713. *
  13714. * SUMMARY: Show method for the err_gpio sysfs node that will show if
  13715. * setting up the gpio was successful
  13716. *
  13717. * RETURN: Char buffer with printed GPIO creation state
  13718. *
  13719. * PARAMETERS:
  13720. * *dev - pointer to device structure
  13721. * *attr - pointer to device attributes
  13722. * *buf - pointer to output buffer
  13723. ******************************************************************************/
  13724. static ssize_t pt_err_gpio_show(struct device *dev,
  13725. struct device_attribute *attr, char *buf)
  13726. {
  13727. struct pt_core_data *cd = dev_get_drvdata(dev);
  13728. return scnprintf(buf, strlen(buf), "Status: 0\n"
  13729. "Err GPIO (%d) : %s\n"
  13730. "Err GPIO trig type: %d\n",
  13731. cd->err_gpio,
  13732. (cd->err_gpio ? (gpio_get_value(cd->err_gpio) ?
  13733. "HIGH" : "low") : "not defined"),
  13734. cd->err_gpio_type);
  13735. }
  13736. /*******************************************************************************
  13737. * FUNCTION: pt_err_gpio_store
  13738. *
  13739. * SUMMARY: The store method for the err_gpio sysfs node that allows any
  13740. * available host GPIO to be used to trigger when TTDL detects a PIP
  13741. * command/response timeout.
  13742. *
  13743. * RETURN:
  13744. * 0 = success
  13745. * !0 = failure
  13746. *
  13747. * PARAMETERS:
  13748. * *dev - pointer to device structure
  13749. * *attr - pointer to device attributes
  13750. * *buf - pointer to buffer that hold the command parameters
  13751. * size - size of buf
  13752. ******************************************************************************/
  13753. static ssize_t pt_err_gpio_store(struct device *dev,
  13754. struct device_attribute *attr, const char *buf, size_t size)
  13755. {
  13756. struct pt_core_data *cd = dev_get_drvdata(dev);
  13757. unsigned long gpio;
  13758. int rc = 0;
  13759. u32 input_data[3];
  13760. int length;
  13761. u8 err_type;
  13762. input_data[0] = 0;
  13763. input_data[1] = 0;
  13764. /* Maximmum input is two elements */
  13765. length = _pt_ic_parse_input(dev, buf, size,
  13766. input_data, ARRAY_SIZE(input_data));
  13767. if (length < 1 || length > 2) {
  13768. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13769. __func__);
  13770. rc = -EINVAL;
  13771. goto exit;
  13772. }
  13773. gpio = input_data[0];
  13774. err_type = (u8)input_data[1];
  13775. if (err_type < 0 || err_type > PT_ERR_GPIO_MAX_TYPE) {
  13776. rc = -EINVAL;
  13777. goto exit;
  13778. }
  13779. mutex_lock(&cd->system_lock);
  13780. gpio_free(gpio);
  13781. rc = gpio_request(gpio, NULL);
  13782. if (rc) {
  13783. pt_debug(dev, DL_ERROR, "error requesting gpio %lu\n", gpio);
  13784. rc = -ENODEV;
  13785. } else {
  13786. cd->err_gpio = gpio;
  13787. cd->err_gpio_type = err_type;
  13788. gpio_direction_output(gpio, 0);
  13789. }
  13790. mutex_unlock(&cd->system_lock);
  13791. exit:
  13792. if (rc)
  13793. return rc;
  13794. return size;
  13795. }
  13796. static DEVICE_ATTR(err_gpio, 0644, pt_err_gpio_show,
  13797. pt_err_gpio_store);
  13798. /*******************************************************************************
  13799. * FUNCTION: pt_drv_irq_show
  13800. *
  13801. * SUMMARY: Show method for the drv_irq sysfs node that will show if the
  13802. * TTDL interrupt is enabled/disabled
  13803. *
  13804. * RETURN:
  13805. * 0 = success
  13806. * !0 = failure
  13807. *
  13808. * PARAMETERS:
  13809. * *dev - pointer to device structure
  13810. * *attr - pointer to device attributes
  13811. * *buf - pointer to output buffer
  13812. ******************************************************************************/
  13813. static ssize_t pt_drv_irq_show(struct device *dev,
  13814. struct device_attribute *attr, char *buf)
  13815. {
  13816. struct pt_core_data *cd = dev_get_drvdata(dev);
  13817. ssize_t ret = 0;
  13818. mutex_lock(&cd->system_lock);
  13819. ret += scnprintf(buf, PT_MAX_PRBUF_SIZE, "Status: %d\n", 0);
  13820. if (cd->irq_enabled)
  13821. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13822. "Driver interrupt: ENABLED\n");
  13823. else
  13824. ret += scnprintf(buf + ret, PT_MAX_PRBUF_SIZE - ret,
  13825. "Driver interrupt: DISABLED\n");
  13826. mutex_unlock(&cd->system_lock);
  13827. return ret;
  13828. }
  13829. /*******************************************************************************
  13830. * FUNCTION: pt_drv_irq_store
  13831. *
  13832. * SUMMARY: The store method for the drv_irq sysfs node that allows the TTDL
  13833. * IRQ to be enabled/disabled.
  13834. *
  13835. * RETURN: Size of passed in buffer
  13836. *
  13837. * PARAMETERS:
  13838. * *dev - pointer to device structure
  13839. * *attr - pointer to device attributes
  13840. * *buf - pointer to buffer that hold the command parameters
  13841. * size - size of buf
  13842. ******************************************************************************/
  13843. static ssize_t pt_drv_irq_store(struct device *dev,
  13844. struct device_attribute *attr, const char *buf, size_t size)
  13845. {
  13846. struct pt_core_data *cd = dev_get_drvdata(dev);
  13847. u32 input_data[2];
  13848. int length;
  13849. int rc = 0;
  13850. /* Maximum input of one value */
  13851. length = _pt_ic_parse_input(dev, buf, size, input_data,
  13852. ARRAY_SIZE(input_data));
  13853. if (length != 1) {
  13854. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  13855. __func__);
  13856. rc = -EINVAL;
  13857. goto exit;
  13858. }
  13859. mutex_lock(&cd->system_lock);
  13860. switch (input_data[0]) {
  13861. case 0:
  13862. if (cd->irq_enabled) {
  13863. cd->irq_enabled = false;
  13864. /* Disable IRQ has no return value to check */
  13865. disable_irq_nosync(cd->irq);
  13866. pt_debug(dev, DL_INFO,
  13867. "%s: Driver IRQ now disabled\n",
  13868. __func__);
  13869. } else
  13870. pt_debug(dev, DL_INFO,
  13871. "%s: Driver IRQ already disabled\n",
  13872. __func__);
  13873. break;
  13874. case 1:
  13875. if (cd->irq_enabled == false) {
  13876. cd->irq_enabled = true;
  13877. enable_irq(cd->irq);
  13878. pt_debug(dev, DL_INFO,
  13879. "%s: Driver IRQ now enabled\n",
  13880. __func__);
  13881. } else
  13882. pt_debug(dev, DL_INFO,
  13883. "%s: Driver IRQ already enabled\n",
  13884. __func__);
  13885. break;
  13886. default:
  13887. rc = -EINVAL;
  13888. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  13889. }
  13890. mutex_unlock(&(cd->system_lock));
  13891. exit:
  13892. if (rc)
  13893. return rc;
  13894. return size;
  13895. }
  13896. /*******************************************************************************
  13897. * FUNCTION: pt_pip2_bin_hdr_show
  13898. *
  13899. * SUMMARY: Show method for the pip2_bin_hdr sysfs node that will read
  13900. * the bin file header from flash and show each field
  13901. *
  13902. * RETURN:
  13903. * 0 = success
  13904. * !0 = failure
  13905. *
  13906. * PARAMETERS:
  13907. * *dev - pointer to device structure
  13908. * *attr - pointer to device attributes
  13909. * *buf - pointer to output buffer
  13910. ******************************************************************************/
  13911. static ssize_t pt_pip2_bin_hdr_show(struct device *dev,
  13912. struct device_attribute *attr, char *buf)
  13913. {
  13914. ssize_t ret = 0;
  13915. struct pt_bin_file_hdr hdr = {0};
  13916. int rc;
  13917. rc = _pt_request_pip2_bin_hdr(dev, &hdr);
  13918. ret = scnprintf(buf, strlen(buf),
  13919. "%s: %d\n"
  13920. "%s: %d\n"
  13921. "%s: 0x%04X\n"
  13922. "%s: %d\n"
  13923. "%s: %d\n"
  13924. "%s: %d\n"
  13925. "%s: 0x%08X\n"
  13926. "%s: 0x%04X\n"
  13927. "%s: 0x%04X\n"
  13928. "%s: %d\n"
  13929. "%s: %d\n",
  13930. "Status", rc,
  13931. "Header Length ", hdr.length,
  13932. "TTPID ", hdr.ttpid,
  13933. "FW Major Ver ", hdr.fw_major,
  13934. "FW Minor Ver ", hdr.fw_minor,
  13935. "FW Rev Control ", hdr.fw_rev_ctrl,
  13936. "FW CRC ", hdr.fw_crc,
  13937. "Silicon Rev ", hdr.si_rev,
  13938. "Silicon ID ", hdr.si_id,
  13939. "Config Ver ", hdr.config_ver,
  13940. "HEX File Size ", hdr.hex_file_size
  13941. );
  13942. return ret;
  13943. }
  13944. /*******************************************************************************
  13945. * FUNCTION: pt_platform_data_show
  13946. *
  13947. * SUMMARY: Show method for the platform_data sysfs node that will show the
  13948. * active platform data including: GPIOs, Vendor and Product IDs,
  13949. * Virtual Key coordinates, Core/MT/Loader flags, Level trigger delay,
  13950. * HID registers, and Easy wake gesture
  13951. *
  13952. * RETURN: Size of printed data
  13953. *
  13954. * PARAMETERS:
  13955. * *dev - pointer to device structure
  13956. * *attr - pointer to device attributes
  13957. * *buf - pointer to output buffer
  13958. ******************************************************************************/
  13959. static ssize_t pt_platform_data_show(struct device *dev,
  13960. struct device_attribute *attr, char *buf)
  13961. {
  13962. struct pt_platform_data *pdata = dev_get_platdata(dev);
  13963. struct pt_core_data *cd = dev_get_drvdata(dev);
  13964. ssize_t ret;
  13965. ret = scnprintf(buf, strlen(buf),
  13966. "%s: %d\n"
  13967. "%s: %d\n"
  13968. "%s: %d\n"
  13969. "%s: %d\n"
  13970. "%s: %d\n"
  13971. "%s: %d\n"
  13972. "%s: %d\n"
  13973. "%s: %d\n"
  13974. "%s: %d\n"
  13975. "%s: %d\n"
  13976. "%s: %d\n"
  13977. "%s: %d\n"
  13978. "%s: %d\n"
  13979. "%s: %d\n"
  13980. "%s: %d\n"
  13981. "%s: %d\n"
  13982. "%s: %d\n"
  13983. "%s: %d\n"
  13984. "%s: %d\n"
  13985. "%s: %d\n"
  13986. "%s: %d\n"
  13987. "%s: %d\n"
  13988. "%s: %d\n"
  13989. "%s: %d\n"
  13990. "%s: %d\n"
  13991. "%s: %d\n"
  13992. "%s: %d\n"
  13993. "%s: %s\n"
  13994. "%s: %s\n"
  13995. "%s: %d\n",
  13996. "Status", 0,
  13997. "Interrupt GPIO ", pdata->core_pdata->irq_gpio,
  13998. "Interrupt GPIO Value ",
  13999. pdata->core_pdata->irq_gpio ?
  14000. gpio_get_value(pdata->core_pdata->irq_gpio) : 0,
  14001. "Reset GPIO ", pdata->core_pdata->rst_gpio,
  14002. "Reset GPIO Value ",
  14003. pdata->core_pdata->rst_gpio ?
  14004. gpio_get_value(pdata->core_pdata->rst_gpio) : 0,
  14005. "DDI Reset GPIO ", pdata->core_pdata->ddi_rst_gpio,
  14006. "DDI Reset GPIO Value ",
  14007. pdata->core_pdata->ddi_rst_gpio ?
  14008. gpio_get_value(pdata->core_pdata->ddi_rst_gpio) : 0,
  14009. "VDDI GPIO ", pdata->core_pdata->vddi_gpio,
  14010. "VDDI GPIO Value ",
  14011. pdata->core_pdata->vddi_gpio ?
  14012. gpio_get_value(pdata->core_pdata->vddi_gpio) : 0,
  14013. "VCC GPIO ", pdata->core_pdata->vcc_gpio,
  14014. "VCC GPIO Value ",
  14015. pdata->core_pdata->vcc_gpio ?
  14016. gpio_get_value(pdata->core_pdata->vcc_gpio) : 0,
  14017. "AVDD GPIO ", pdata->core_pdata->avdd_gpio,
  14018. "AVDD GPIO Value ",
  14019. pdata->core_pdata->avdd_gpio ?
  14020. gpio_get_value(pdata->core_pdata->avdd_gpio) : 0,
  14021. "AVEE GPIO ", pdata->core_pdata->avee_gpio,
  14022. "AVEE GPIO Value ",
  14023. pdata->core_pdata->avee_gpio ?
  14024. gpio_get_value(pdata->core_pdata->avee_gpio) : 0,
  14025. "Vendor ID ", pdata->core_pdata->vendor_id,
  14026. "Product ID ", pdata->core_pdata->product_id,
  14027. "Vkeys x ", pdata->mt_pdata->vkeys_x,
  14028. "Vkeys y ", pdata->mt_pdata->vkeys_y,
  14029. "Core data flags ", pdata->core_pdata->flags,
  14030. "MT data flags ", pdata->mt_pdata->flags,
  14031. "Loader data flags ", pdata->loader_pdata->flags,
  14032. "Level trigger delay (us) ",
  14033. pdata->core_pdata->level_irq_udelay,
  14034. "HID Descriptor Register ",
  14035. pdata->core_pdata->hid_desc_register,
  14036. "HID Output Register ",
  14037. cd->hid_desc.output_register,
  14038. "HID Command Register ",
  14039. cd->hid_desc.command_register,
  14040. "Easy wakeup gesture ",
  14041. pdata->core_pdata->easy_wakeup_gesture,
  14042. "Config DUT generation ",
  14043. pdata->core_pdata->config_dut_generation ?
  14044. (pdata->core_pdata->config_dut_generation ==
  14045. CONFIG_DUT_PIP2_CAPABLE ?
  14046. "PT TC/TT" : "Gen5/6") : "Auto",
  14047. "Watchdog Force Stop ",
  14048. pdata->core_pdata->watchdog_force_stop ?
  14049. "True" : "False",
  14050. "Panel ID Support ",
  14051. pdata->core_pdata->panel_id_support);
  14052. return ret;
  14053. }
  14054. #define PT_ERR_STR_SIZE 64
  14055. /*******************************************************************************
  14056. * FUNCTION: pt_bist_bus_test
  14057. *
  14058. * SUMMARY: Tests the connectivity of the active bus pins:
  14059. * I2C - SDA and SCL
  14060. * SPI - MOSI, MISO, CLK
  14061. *
  14062. * Disable TTDL interrupts, send a PIP cmd and then manually read the
  14063. * bus. If any data is read we know the I2C/SPI pins are connected
  14064. *
  14065. * RETURN:
  14066. * 0 = Success
  14067. * !0 = Failure
  14068. *
  14069. * PARAMETERS:
  14070. * *dev - pointer to device structure
  14071. * *net_toggled - pointer to where to store if bus toggled
  14072. * *err_str - pointer to error string buffer
  14073. ******************************************************************************/
  14074. static int pt_bist_bus_test(struct device *dev, u8 *net_toggled, u8 *err_str)
  14075. {
  14076. struct pt_core_data *cd = dev_get_drvdata(dev);
  14077. u8 ver_cmd[8] = {0x01, 0x01, 0x06, 0x00, 0x0E, 0x07, 0xF0, 0xB1};
  14078. u8 *read_buf = NULL;
  14079. int bytes_read = 0;
  14080. int rc = 0;
  14081. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14082. if (read_buf == NULL) {
  14083. rc = -ENOMEM;
  14084. goto exit;
  14085. }
  14086. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14087. pt_debug(dev, DL_INFO, "%s: TTDL Core Suspend\n", __func__);
  14088. disable_irq(cd->irq);
  14089. mutex_lock(&cd->system_lock);
  14090. cd->irq_disabled = true;
  14091. mutex_unlock(&cd->system_lock);
  14092. /*
  14093. * Sleep >4ms to allow any pending TTDL IRQ to finish. Without this
  14094. * the disable_irq_nosync() could cause the IRQ to get stuck asserted
  14095. */
  14096. usleep_range(5000, 6000);
  14097. pt_debug(cd->dev, DL_INFO, ">>> %s: Write Buffer Size[%d] VERSION\n",
  14098. __func__, (int)sizeof(ver_cmd));
  14099. pt_pr_buf(cd->dev, DL_DEBUG, ver_cmd, (int)sizeof(ver_cmd),
  14100. ">>> User CMD");
  14101. rc = pt_adap_write_read_specific(cd, sizeof(ver_cmd), ver_cmd, NULL);
  14102. if (rc) {
  14103. pt_debug(dev, DL_ERROR,
  14104. "%s: BUS Test - Failed to send VER cmd\n", __func__);
  14105. *net_toggled = 0;
  14106. strlcpy(err_str,
  14107. "- Write failed, bus open or shorted or DUT in reset", PT_ERR_STR_SIZE);
  14108. goto exit_enable_irq;
  14109. }
  14110. usleep_range(4000, 5000);
  14111. bytes_read = pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, read_buf);
  14112. pt_debug(dev, DL_INFO, "%s: BUS Test - %d bytes forced read\n",
  14113. __func__, bytes_read);
  14114. if (bytes_read == 0) {
  14115. *net_toggled = 0;
  14116. pt_debug(dev, DL_INFO, "%s: BUS Read Failed, 0 bytes read\n",
  14117. __func__);
  14118. strlcpy(err_str,
  14119. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14120. rc = -EIO;
  14121. goto exit_enable_irq;
  14122. } else {
  14123. if (cd->bus_ops->bustype == BUS_I2C)
  14124. *net_toggled = 1;
  14125. else {
  14126. if ((bytes_read > 3) &&
  14127. (read_buf[3] & PIP2_CMD_COMMAND_ID_MASK) ==
  14128. PIP2_CMD_ID_VERSION)
  14129. *net_toggled = 1;
  14130. else {
  14131. *net_toggled = 0;
  14132. pt_debug(dev, DL_INFO,
  14133. "%s: BUS Read Failed, %d bytes read\n",
  14134. __func__, bytes_read);
  14135. strlcpy(err_str,
  14136. "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14137. }
  14138. }
  14139. }
  14140. exit_enable_irq:
  14141. enable_irq(cd->irq);
  14142. usleep_range(5000, 6000);
  14143. mutex_lock(&cd->system_lock);
  14144. cd->irq_disabled = false;
  14145. mutex_unlock(&cd->system_lock);
  14146. pt_debug(dev, DL_INFO, "%s: TTDL Core Resumed\n", __func__);
  14147. exit:
  14148. kfree(read_buf);
  14149. return rc;
  14150. }
  14151. /*******************************************************************************
  14152. * FUNCTION: pt_bist_irq_test
  14153. *
  14154. * SUMMARY: Tests the connectivity of the IRQ net
  14155. *
  14156. * This test will ensure there is a good connection between the host
  14157. * and the DUT on the irq pin. First determine if the IRQ is stuck
  14158. * asserted and if so keep reading messages off of the bus until
  14159. * it de-asserts. Possible outcomes:
  14160. * - IRQ was already de-asserted: Send a PIP command and if an
  14161. * interrupt is generated the test passes.
  14162. * - IRQ was asserted: Reading off the bus de-assertes the IRQ,
  14163. * test passes.
  14164. * - IRQ stays asserted: After reading the bus multiple times
  14165. * the IRQ stays asserted. Likely open or shorted to GND
  14166. *
  14167. * RETURN:
  14168. * 0 = Success
  14169. * !0 = Failure
  14170. *
  14171. * PARAMETERS:
  14172. * *dev - pointer to device structure
  14173. * *bus_toggled - pointer to where to store if bus toggled
  14174. * *irq_toggled - pointer to where to store if IRQ toggled
  14175. * *xres_toggled - pointer to where to store if XRES toggled
  14176. * *err_str - pointer to error string buffer
  14177. ******************************************************************************/
  14178. static int pt_bist_irq_test(struct device *dev,
  14179. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14180. {
  14181. struct pt_core_data *cd = dev_get_drvdata(dev);
  14182. u8 *read_buf = NULL;
  14183. u8 mode = PT_MODE_UNKNOWN;
  14184. u16 actual_read_len;
  14185. int bytes_read = 0;
  14186. int count = 0;
  14187. int rc = 0;
  14188. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14189. if (read_buf == NULL) {
  14190. rc = -ENOMEM;
  14191. goto exit;
  14192. }
  14193. /* Clear IRQ triggered count, and re-evaluate at the end of test */
  14194. cd->irq_count = 0;
  14195. /*
  14196. * Check if IRQ is stuck asserted, if so try a non forced flush of
  14197. * the bus based on the 2 byte initial length read. Try up to 5x.
  14198. */
  14199. while (pt_check_irq_asserted(cd) && count < 5) {
  14200. count++;
  14201. bytes_read += pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14202. }
  14203. if (count > 1 && count < 5 && bytes_read > 0) {
  14204. /*
  14205. * IRQ was stuck but data was successfully read from the
  14206. * bus releasing the IRQ line.
  14207. */
  14208. pt_debug(dev, DL_INFO, "%s: count=%d bytes_read=%d\n",
  14209. __func__, count, bytes_read);
  14210. *bus_toggled = 1;
  14211. *irq_toggled = 1;
  14212. goto exit;
  14213. }
  14214. if (count == 5 && bytes_read == 0) {
  14215. /*
  14216. * Looped 5x and read nothing off the bus yet the IRQ is still
  14217. * asserted. Possible conditions:
  14218. * - IRQ open circuit
  14219. * - IRQ shorted to GND
  14220. * - I2C/SPI bus is disconnected
  14221. * - FW holding the pin low
  14222. * Try entering the BL to see if communication works there.
  14223. */
  14224. mode = PT_MODE_IGNORE;
  14225. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14226. if (rc) {
  14227. pt_debug(dev, DL_ERROR,
  14228. "%s Failed to enter BL\n", __func__);
  14229. strlcpy(err_str,
  14230. "- likely shorted to GND or FW holding it.", PT_ERR_STR_SIZE);
  14231. *irq_toggled = 0;
  14232. goto exit;
  14233. }
  14234. /*
  14235. * If original mode was operational and we successfully
  14236. * entered the BL, then the XRES net must have toggled
  14237. */
  14238. if (mode == PT_MODE_OPERATIONAL)
  14239. *xres_toggled = 1;
  14240. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14241. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14242. &actual_read_len);
  14243. if (rc) {
  14244. /*
  14245. * Could not communicate to DUT in BL mode. Save the
  14246. * error string, slim chance but the XRES test below may
  14247. * show the IRQ is actually working.
  14248. */
  14249. strlcpy(err_str, "- likely shorted to GND.", PT_ERR_STR_SIZE);
  14250. pt_debug(dev, DL_ERROR,
  14251. "%s: %s, count=%d bytes_read=%d\n",
  14252. __func__, err_str, count, bytes_read);
  14253. *irq_toggled = 0;
  14254. rc = pt_pip2_exit_bl_(cd, NULL, 0);
  14255. goto exit;
  14256. } else {
  14257. *bus_toggled = 1;
  14258. *irq_toggled = 1;
  14259. goto exit;
  14260. }
  14261. }
  14262. if (pt_check_irq_asserted(cd)) {
  14263. strlcpy(err_str, "- likely shorted to GND", PT_ERR_STR_SIZE);
  14264. rc = -EIO;
  14265. *irq_toggled = 0;
  14266. goto exit;
  14267. }
  14268. /* Try sending a PIP command to see if we get a response */
  14269. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14270. PIP2_CMD_ID_VERSION, NULL, 0, read_buf, &actual_read_len);
  14271. if (rc) {
  14272. /*
  14273. * Potential IRQ issue, no communication in App mode, attempt
  14274. * the same command in the BL
  14275. */
  14276. mode = PT_MODE_IGNORE;
  14277. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14278. if (rc) {
  14279. pt_debug(dev, DL_ERROR,
  14280. "%s Failed to enter BL\n", __func__);
  14281. *irq_toggled = 0;
  14282. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14283. goto exit;
  14284. }
  14285. /*
  14286. * If original mode was operational and we successfully
  14287. * entered the BL, this will be useful info for the tp_xres
  14288. * test below.
  14289. */
  14290. if (mode == PT_MODE_OPERATIONAL)
  14291. *xres_toggled = 1;
  14292. rc = _pt_request_pip2_send_cmd(dev, PT_CORE_CMD_PROTECTED,
  14293. PIP2_CMD_ID_VERSION, NULL, 0, read_buf,
  14294. &actual_read_len);
  14295. if (rc) {
  14296. /*
  14297. * Could not communicate in FW mode or BL mode. Save the
  14298. * error string, slim chance but the XRES test below may
  14299. * show the IRQ is actually working.
  14300. */
  14301. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14302. pt_debug(dev, DL_ERROR,
  14303. "%s: request_active_pip_prot failed\n",
  14304. __func__);
  14305. *irq_toggled = 0;
  14306. goto exit;
  14307. }
  14308. }
  14309. if (cd->irq_count > 0) {
  14310. pt_debug(dev, DL_INFO, "%s: irq_count=%d\n", __func__,
  14311. cd->irq_count);
  14312. *bus_toggled = 1;
  14313. *irq_toggled = 1;
  14314. goto exit;
  14315. }
  14316. exit:
  14317. kfree(read_buf);
  14318. return rc;
  14319. }
  14320. /*******************************************************************************
  14321. * FUNCTION: pt_bist_xres_test
  14322. *
  14323. * SUMMARY: Tests the connectivity of the TP_XRES net
  14324. *
  14325. * This test will ensure there is a good connection between the host
  14326. * and the DUT on the tp_xres pin. The pin will be toggled to
  14327. * generate a TP reset which will cause the DUT to output a reset
  14328. * sentinel. If the reset sentinel is seen the test passes. If it is
  14329. * not seen the test will attempt to send a soft reset to simply gain
  14330. * some additional information on the failure:
  14331. * - soft reset fails to send: XRES and IRQ likely open
  14332. * - soft reset passes: XRES likely open or stuck de-asserted
  14333. * - soft reset fails: XRES likely stuck asserted
  14334. *
  14335. * RETURN:
  14336. * 0 = Success
  14337. * !0 = Failure
  14338. *
  14339. * PARAMETERS:
  14340. * *dev - pointer to device structure
  14341. * *bus_toggled - pointer to where to store if bus toggled
  14342. * *irq_toggled - pointer to where to store if IRQ toggled
  14343. * *xres_toggled - pointer to where to store if XRES toggled
  14344. * *err_str - pointer to error string buffer
  14345. ******************************************************************************/
  14346. static int pt_bist_xres_test(struct device *dev,
  14347. u8 *bus_toggled, u8 *irq_toggled, u8 *xres_toggled, u8 *err_str)
  14348. {
  14349. struct pt_core_data *cd = dev_get_drvdata(dev);
  14350. struct pt_platform_data *pdata = dev_get_platdata(dev);
  14351. u8 *read_buf = NULL;
  14352. u8 mode = PT_MODE_UNKNOWN;
  14353. int rc = 0;
  14354. int t = 0;
  14355. int timeout = 300;
  14356. read_buf = kzalloc(PT_MAX_PIP1_MSG_SIZE, GFP_KERNEL);
  14357. if (read_buf == NULL) {
  14358. rc = -ENOMEM;
  14359. goto exit;
  14360. }
  14361. /* Clear the startup bit mask, reset and enum will re-populate it */
  14362. cd->startup_status = STARTUP_STATUS_START;
  14363. pt_debug(dev, DL_DEBUG, "%s: Startup Status Reset\n", __func__);
  14364. if ((!pdata->core_pdata->rst_gpio) || (!pdata->core_pdata->xres)) {
  14365. strlcpy(err_str, "- Net not configured or available", PT_ERR_STR_SIZE);
  14366. rc = -ENODEV;
  14367. goto exit;
  14368. }
  14369. /* Ensure we have nothing pending on active bus */
  14370. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  14371. /* Perform a hard XRES toggle and wait for reset sentinel */
  14372. mutex_lock(&cd->system_lock);
  14373. cd->hid_reset_cmd_state = 1;
  14374. mutex_unlock(&cd->system_lock);
  14375. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14376. rc = pt_hw_hard_reset(cd);
  14377. /* Set timeout to 1s for the flashless case where a BL could be done */
  14378. if (cd->flashless_dut)
  14379. timeout = 1000;
  14380. /*
  14381. * To avoid the case that next PIP command can be confused by BL/FW
  14382. * sentinel's "wakeup" event, chekcing hid_reset_cmd_state which is
  14383. * followed by "wakeup event" function can lower the failure rate.
  14384. */
  14385. t = wait_event_timeout(cd->wait_q,
  14386. ((cd->startup_status != STARTUP_STATUS_START)
  14387. && (cd->hid_reset_cmd_state == 0)),
  14388. msecs_to_jiffies(timeout));
  14389. if (IS_TMO(t)) {
  14390. pt_debug(cd->dev, DL_ERROR,
  14391. "%s: TMO waiting for sentinel\n", __func__);
  14392. *xres_toggled = 0;
  14393. strlcpy(err_str, "- likely open. (No Reset Sentinel)", PT_ERR_STR_SIZE);
  14394. /*
  14395. * Possibly bad FW, Try entering BL and wait for reset sentinel.
  14396. * To enter the BL we need to generate an XRES so first try to
  14397. * launch the applicaiton
  14398. */
  14399. if (cd->mode == PT_MODE_BOOTLOADER)
  14400. pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14401. pt_debug(dev, DL_INFO, "%s: Enter BL with a hard reset\n",
  14402. __func__);
  14403. mode = PT_MODE_IGNORE;
  14404. rc = _pt_request_pip2_enter_bl(dev, &mode, NULL);
  14405. if (rc) {
  14406. pt_debug(dev, DL_ERROR, "%s Failed to enter BL\n",
  14407. __func__);
  14408. *xres_toggled = 0;
  14409. strlcpy(err_str, "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14410. goto exit;
  14411. } else {
  14412. /* Wait for the BL sentinel */
  14413. t = wait_event_timeout(cd->wait_q,
  14414. (cd->startup_status != STARTUP_STATUS_START),
  14415. msecs_to_jiffies(500));
  14416. if (IS_TMO(t)) {
  14417. pt_debug(cd->dev, DL_ERROR,
  14418. "%s: TMO waiting for BL sentinel\n",
  14419. __func__);
  14420. *xres_toggled = 0;
  14421. strlcpy(err_str,
  14422. "- likely open or shorted to VDDI.", PT_ERR_STR_SIZE);
  14423. rc = -ETIME;
  14424. goto exit;
  14425. }
  14426. }
  14427. }
  14428. mutex_lock(&cd->system_lock);
  14429. cd->hid_reset_cmd_state = 0;
  14430. mutex_unlock(&cd->system_lock);
  14431. /* Look for BL or FW reset sentinels */
  14432. if (!rc && ((cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL) ||
  14433. (cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL))) {
  14434. pt_debug(dev, DL_INFO, "%s: hard XRES pass\n", __func__);
  14435. /* If a sentinel was seen, all nets are working */
  14436. *xres_toggled = 1;
  14437. *irq_toggled = 1;
  14438. /*
  14439. * For SPI test, bus read result can be confused as FW sentinel
  14440. * if MISO(slave) is connected to MISO(host).
  14441. */
  14442. if (cd->bus_ops->bustype == BUS_I2C)
  14443. *bus_toggled = 1;
  14444. } else {
  14445. /*
  14446. * Hard reset failed, however some additional information
  14447. * could be determined. Try a soft reset to see if DUT resets
  14448. * with the possible outcomes:
  14449. * - if it resets the line is not stuck asserted
  14450. * - if it does not reset the line could be stuck asserted
  14451. */
  14452. *xres_toggled = 0;
  14453. rc = pt_hw_soft_reset(cd, PT_CORE_CMD_PROTECTED);
  14454. msleep(30);
  14455. pt_debug(dev, DL_INFO, "%s: TP_XRES BIST soft reset rc=%d",
  14456. __func__, rc);
  14457. if (rc) {
  14458. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14459. pt_debug(dev, DL_ERROR,
  14460. "%s: Hard reset failed, soft reset failed %s\n",
  14461. __func__, err_str);
  14462. goto exit;
  14463. }
  14464. if (cd->startup_status & STARTUP_STATUS_BL_RESET_SENTINEL ||
  14465. cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) {
  14466. strlcpy(err_str,
  14467. "- likely open or stuck high, soft reset OK", PT_ERR_STR_SIZE);
  14468. pt_debug(dev, DL_ERROR,
  14469. "%s: Hard reset failed, soft reset passed-%s\n",
  14470. __func__, err_str);
  14471. } else if (cd->startup_status == 0) {
  14472. strlcpy(err_str, "- likely stuck high.", PT_ERR_STR_SIZE);
  14473. pt_debug(dev, DL_ERROR,
  14474. "%s: Hard reset failed, soft reset failed-%s\n",
  14475. __func__, err_str);
  14476. } else {
  14477. strlcpy(err_str, "- open or stuck.", PT_ERR_STR_SIZE);
  14478. pt_debug(dev, DL_ERROR,
  14479. "%s: Hard and Soft reset failed - %s\n",
  14480. __func__, err_str);
  14481. }
  14482. }
  14483. exit:
  14484. kfree(read_buf);
  14485. return rc;
  14486. }
  14487. /*******************************************************************************
  14488. * FUNCTION: pt_bist_slave_irq_test
  14489. *
  14490. * SUMMARY: Tests the connectivity of the Master/Slave IRQ net
  14491. *
  14492. * This test will ensure there is a good IRQ connection between the master
  14493. * DUT and the slave DUT(s). After power up the STATUS command is sent
  14494. * and the 'Slave Detect' bits are verified to ensure the master DUT
  14495. * saw each slave trigger the IRQ with it's reset sentinel.
  14496. *
  14497. * RETURN:
  14498. * 0 = Success
  14499. * !0 = Failure
  14500. *
  14501. * PARAMETERS:
  14502. * *dev - pointer to device structure
  14503. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14504. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14505. * *err_str - pointer to error string buffer
  14506. * *slave_detect - pointer to slave detect buffer
  14507. * *boot_err - pointer to boot_err buffer
  14508. ******************************************************************************/
  14509. static int pt_bist_slave_irq_test(struct device *dev,
  14510. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str,
  14511. u8 *slave_detect, u8 *boot_err)
  14512. {
  14513. struct pt_core_data *cd = dev_get_drvdata(dev);
  14514. u8 mode = PT_MODE_UNKNOWN;
  14515. u8 status;
  14516. u8 boot;
  14517. u8 read_buf[12];
  14518. u8 detected = 0;
  14519. u8 last_err = -1;
  14520. u16 actual_read_len;
  14521. int result = 0;
  14522. int rc = 0;
  14523. /*
  14524. * Ensure DUT is in the BL where the STATUS cmd will report the slave
  14525. * detect bits. If the DUT was in FW, entering the BL will cause an
  14526. * XRES signal which will inadvertently test the XRES net as well
  14527. */
  14528. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14529. if (rc) {
  14530. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14531. __func__, rc);
  14532. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14533. goto exit;
  14534. }
  14535. /* Use the STATUS command to retrieve the slave detect bit(s) */
  14536. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14537. PIP2_CMD_ID_STATUS, NULL, 0, read_buf,
  14538. &actual_read_len);
  14539. if (!rc) {
  14540. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14541. "PIP2 STATUS");
  14542. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14543. boot = read_buf[PIP2_RESP_BODY_OFFSET] & 0x01;
  14544. /* Slave detect is only valid if status ok and in boot exec */
  14545. if (status == PIP2_RSP_ERR_NONE &&
  14546. boot == PIP2_STATUS_BOOT_EXEC) {
  14547. detected = read_buf[PIP2_RESP_BODY_OFFSET + 2] &
  14548. SLAVE_DETECT_MASK;
  14549. } else {
  14550. strlcpy(err_str, "- State could not be determined", PT_ERR_STR_SIZE);
  14551. rc = -EPERM;
  14552. }
  14553. } else {
  14554. pt_debug(cd->dev, DL_ERROR, "%s: STATUS cmd failure\n",
  14555. __func__);
  14556. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14557. goto exit;
  14558. }
  14559. /*
  14560. * Retrieve boot error regardless of the state of the slave detect
  14561. * bit because the IRQ could have been stuck high or low.
  14562. */
  14563. rc = _pt_request_pip2_send_cmd(cd->dev, PT_CORE_CMD_UNPROTECTED,
  14564. PIP2_CMD_ID_GET_LAST_ERRNO, NULL, 0,
  14565. read_buf, &actual_read_len);
  14566. if (!rc) {
  14567. pt_pr_buf(cd->dev, DL_INFO, read_buf, actual_read_len,
  14568. "PIP2 GET_LAST_ERRNO");
  14569. status = read_buf[PIP2_RESP_STATUS_OFFSET];
  14570. last_err = read_buf[PIP2_RESP_BODY_OFFSET];
  14571. if (last_err) {
  14572. pt_debug(cd->dev, DL_ERROR,
  14573. "%s: Master Boot Last Err = 0x%02X\n",
  14574. __func__, last_err);
  14575. }
  14576. } else {
  14577. pt_debug(cd->dev, DL_ERROR,
  14578. "%s: GET_LAST_ERRNO cmd failure\n", __func__);
  14579. strlcpy(err_str, "- stuck, likely shorted to GND.", PT_ERR_STR_SIZE);
  14580. }
  14581. exit:
  14582. pt_debug(cd->dev, DL_INFO,
  14583. "%s: rc=%d detected=0x%02X boot_err=0x%02X\n",
  14584. __func__, rc, detected, last_err);
  14585. if (err_str && last_err) {
  14586. if (detected)
  14587. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14588. "- Likely stuck low. Boot Error:",
  14589. last_err);
  14590. else
  14591. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14592. "- Likely stuck high. Boot Error:",
  14593. last_err);
  14594. }
  14595. /* Ignore an invalid image error as BIST doesn't need valid FW */
  14596. if (last_err == PIP2_RSP_ERR_INVALID_IMAGE)
  14597. last_err = PIP2_RSP_ERR_NONE;
  14598. if (slave_irq_toggled)
  14599. *slave_irq_toggled = (detected && !last_err) ? true : false;
  14600. if (slave_bus_toggled) {
  14601. /* Leave as UNTEST if slave not detected */
  14602. if (detected)
  14603. *slave_bus_toggled = !last_err ? true : false;
  14604. }
  14605. if (slave_detect)
  14606. *slave_detect = detected;
  14607. if (boot_err)
  14608. *boot_err = last_err;
  14609. pt_debug(cd->dev, DL_INFO, "%s: %s=0x%02X, %s=0x%02X, %s=0x%02X\n",
  14610. __func__,
  14611. "Detected", detected,
  14612. "slave_irq_toggled", *slave_irq_toggled,
  14613. "slave_bus_toggled", *slave_bus_toggled);
  14614. return rc;
  14615. }
  14616. /*******************************************************************************
  14617. * FUNCTION: pt_bist_slave_xres_test
  14618. *
  14619. * SUMMARY: Tests the connectivity of the Master/Slave TP_XRES net
  14620. *
  14621. * This test will ensure there is a good TP_XRES connection between the
  14622. * master DUT and the slave DUT(s). After toggling the XRES pin to the
  14623. * master, the STATUS command is sent and the 'Slave Detect' bits are
  14624. * verified to ensure the master DUT saw each slave trigger the IRQ with
  14625. * it's reset sentinel.
  14626. *
  14627. * RETURN:
  14628. * 0 = Success
  14629. * !0 = Failure
  14630. *
  14631. * PARAMETERS:
  14632. * *dev - pointer to device structure
  14633. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14634. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14635. * *slave_xres_toggled - pointer to where to store if slave XRES toggled
  14636. * *err_str - pointer to error string buffer
  14637. ******************************************************************************/
  14638. static int pt_bist_slave_xres_test(struct device *dev,
  14639. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *slave_xres_toggled,
  14640. u8 *err_str)
  14641. {
  14642. struct pt_core_data *cd = dev_get_drvdata(dev);
  14643. u8 slave_detect = 0;
  14644. u8 boot_err = 0;
  14645. int rc = 0;
  14646. /* Force a reset to force the 'slave detect' bits to be re-acquired */
  14647. mutex_lock(&cd->system_lock);
  14648. cd->hid_reset_cmd_state = 1;
  14649. mutex_unlock(&cd->system_lock);
  14650. pt_debug(dev, DL_INFO, "%s: Perform a hard reset\n", __func__);
  14651. pt_hw_hard_reset(cd);
  14652. msleep(100);
  14653. rc = pt_bist_slave_irq_test(dev, slave_irq_toggled,
  14654. slave_bus_toggled, err_str, &slave_detect, &boot_err);
  14655. pt_debug(dev, DL_INFO, "%s: IRQ test rc = %d\n", __func__, rc);
  14656. if (!rc && *slave_irq_toggled == false) {
  14657. /*
  14658. * If the slave IRQ did not toggle, either the slave_detect
  14659. * bit was not set or we had a boot error. If the slave
  14660. * detect was not set the slave did not reset causing a boot
  14661. * error.
  14662. */
  14663. if (!slave_detect)
  14664. strlcpy(err_str, "- likely open.", PT_ERR_STR_SIZE);
  14665. else
  14666. scnprintf(err_str, PT_ERR_STR_SIZE, "%s 0x%02X",
  14667. "- likely open or an IRQ issue. Boot Error:",
  14668. boot_err);
  14669. }
  14670. if (slave_xres_toggled) {
  14671. if (!rc)
  14672. *slave_xres_toggled = *slave_irq_toggled ? true : false;
  14673. else
  14674. *slave_xres_toggled = false;
  14675. }
  14676. return rc;
  14677. }
  14678. /*******************************************************************************
  14679. * FUNCTION: pt_bist_slave_bus_test
  14680. *
  14681. * SUMMARY: Tests the connectivity of the Master/Slave SPI bus net
  14682. *
  14683. * This test will ensure a good SPI bus connection between the
  14684. * master DUT and the slave DUT(s). This bus connection is ensured by
  14685. * opening file 0 (SRAM loader). If there is a slave and the open fails
  14686. * then there is a master/slave communication issue. Opening file 0 on
  14687. * the master will open it on the slave as well if the slave was detected.
  14688. *
  14689. * RETURN:
  14690. * 0 = Success
  14691. * !0 = Failure
  14692. *
  14693. * PARAMETERS:
  14694. * *dev - pointer to device structure
  14695. * *slave_irq_toggled - pointer to where to store if slave IRQ toggled
  14696. * *slave_bus_toggled - pointer to where to store if slave Bus toggled
  14697. * *err_str - pointer to error string buffer
  14698. ******************************************************************************/
  14699. static int pt_bist_slave_bus_test(struct device *dev,
  14700. u8 *slave_irq_toggled, u8 *slave_bus_toggled, u8 *err_str)
  14701. {
  14702. struct pt_core_data *cd = dev_get_drvdata(dev);
  14703. u8 mode = PT_MODE_UNKNOWN;
  14704. u8 bus_toggled = false;
  14705. u8 file_handle;
  14706. int result = 0;
  14707. int rc = 0;
  14708. rc = _pt_request_pip2_enter_bl(dev, &mode, &result);
  14709. if (rc) {
  14710. pt_debug(cd->dev, DL_ERROR, "%s: Error entering BL rc=%d\n",
  14711. __func__, rc);
  14712. strlcpy(err_str, "- State could not be determined.", PT_ERR_STR_SIZE);
  14713. goto exit;
  14714. }
  14715. pt_debug(dev, DL_INFO, "%s Attempt open file 0\n", __func__);
  14716. file_handle = _pt_pip2_file_open(dev, PIP2_RAM_FILE);
  14717. if (file_handle != PIP2_RAM_FILE) {
  14718. rc = -ENOENT;
  14719. bus_toggled = false;
  14720. pt_debug(dev, DL_ERROR,
  14721. "%s Failed to open bin file\n", __func__);
  14722. strlcpy(err_str, "- Bus open, shorted or DUT in reset", PT_ERR_STR_SIZE);
  14723. goto exit;
  14724. } else {
  14725. bus_toggled = true;
  14726. if (file_handle != _pt_pip2_file_close(dev, file_handle)) {
  14727. pt_debug(dev, DL_ERROR,
  14728. "%s: File Close failed, file_handle=%d\n",
  14729. __func__, file_handle);
  14730. }
  14731. }
  14732. exit:
  14733. /* If the master was able to send/recv a PIP msg, the IRQ must be ok */
  14734. if (slave_irq_toggled)
  14735. *slave_irq_toggled = bus_toggled;
  14736. if (slave_bus_toggled)
  14737. *slave_bus_toggled = bus_toggled;
  14738. return rc;
  14739. }
  14740. /*******************************************************************************
  14741. * FUNCTION: pt_ttdl_bist_show
  14742. *
  14743. * SUMMARY: Show method for the ttdl_bist sysfs node. This built in self test
  14744. * will test that I2C/SPI, IRQ and TP_XRES pins are operational.
  14745. *
  14746. * NOTE: This function will reset the DUT and the startup_status bit
  14747. * mask. A pt_enum will be queued after completion.
  14748. *
  14749. * NOTE: The order of the net tests is done to optimize the time it takes
  14750. * to run. The first test is capable of verifying all nets, each subsequent
  14751. * test is only run if the previous was not able to see all nets toggle.
  14752. *
  14753. * RETURN:
  14754. * 0 = success
  14755. * !0 = failure
  14756. *
  14757. * PARAMETERS:
  14758. * *dev - pointer to device structure
  14759. * *attr - pointer to device attributes
  14760. * *buf - pointer to output buffer
  14761. ******************************************************************************/
  14762. static ssize_t pt_ttdl_bist_show(struct device *dev,
  14763. struct device_attribute *attr, char *buf)
  14764. {
  14765. struct pt_core_data *cd = dev_get_drvdata(dev);
  14766. ssize_t ret;
  14767. char *bus_err_str = NULL;
  14768. char *irq_err_str = NULL;
  14769. char *xres_err_str = NULL;
  14770. char *slave_bus_err_str = NULL;
  14771. char *slave_irq_err_str = NULL;
  14772. char *slave_xres_err_str = NULL;
  14773. u8 tests;
  14774. int rc = 0;
  14775. int num_tests = 0;
  14776. int status = 1; /* 0 = Pass, !0 = fail */
  14777. u8 bus_toggled = 0x0F; /* default to untested */
  14778. u8 i2c_toggled = 0x0F; /* default to untested */
  14779. u8 spi_toggled = 0x0F; /* default to untested */
  14780. u8 irq_toggled = 0x0F; /* default to untested */
  14781. u8 xres_toggled = 0x0F; /* default to untested */
  14782. u8 slave_bus_toggled = 0x0F; /* default to untested */
  14783. u8 slave_irq_toggled = 0x0F; /* default to untested */
  14784. u8 slave_xres_toggled = 0x0F; /* default to untested */
  14785. bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14786. irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14787. xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14788. if (!bus_err_str || !irq_err_str || !xres_err_str)
  14789. goto print_results;
  14790. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14791. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14792. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14793. if (cd->multi_chip) {
  14794. slave_bus_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14795. slave_irq_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14796. slave_xres_err_str = kzalloc(PT_ERR_STR_SIZE, GFP_KERNEL);
  14797. if (!slave_bus_err_str ||
  14798. !slave_irq_err_str ||
  14799. !slave_xres_err_str)
  14800. goto print_results;
  14801. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  14802. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  14803. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  14804. }
  14805. /* Turn off the TTDL WD during the test */
  14806. pt_stop_wd_timer(cd);
  14807. /* Shorten default PIP cmd timeout while running BIST */
  14808. cd->pip_cmd_timeout = 200;
  14809. /* Count the number of tests to run */
  14810. tests = cd->ttdl_bist_select;
  14811. while (tests) {
  14812. num_tests += tests & 1;
  14813. tests >>= 1;
  14814. }
  14815. pt_debug(dev, DL_ERROR, "%s: BIST select = 0x%02X, run %d tests\n",
  14816. __func__, cd->ttdl_bist_select, num_tests);
  14817. /* Suppress auto BL to avoid loader thread sending PIP during xres */
  14818. if (cd->flashless_dut) {
  14819. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - SUPPRESS\n",
  14820. __func__);
  14821. mutex_lock(&cd->system_lock);
  14822. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  14823. mutex_unlock(&cd->system_lock);
  14824. }
  14825. /* --------------- TP_XRES BIST TEST --------------- */
  14826. if ((cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST) != 0) {
  14827. pt_debug(dev, DL_INFO,
  14828. "%s: ----- Start TP_XRES BIST -----", __func__);
  14829. rc = pt_bist_xres_test(dev, &bus_toggled, &irq_toggled,
  14830. &xres_toggled, xres_err_str);
  14831. /* Done if the rest of all nets toggled */
  14832. if (bus_toggled == 1 && irq_toggled == 1 && xres_toggled == 1)
  14833. goto host_nets_complete;
  14834. }
  14835. /* Flush bus in case a PIP response is waiting from previous test */
  14836. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14837. /* --------------- IRQ BIST TEST --------------- */
  14838. if ((cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST) != 0) {
  14839. pt_debug(dev, DL_INFO,
  14840. "%s: ----- Start IRQ BIST -----", __func__);
  14841. bus_toggled = 0xFF;
  14842. irq_toggled = 0xFF;
  14843. rc = pt_bist_irq_test(dev, &bus_toggled, &irq_toggled,
  14844. &xres_toggled, irq_err_str);
  14845. /* If this net failed clear results from previous net */
  14846. if (irq_toggled != 1) {
  14847. xres_toggled = 0x0F;
  14848. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14849. }
  14850. if (bus_toggled == 1 && irq_toggled == 1)
  14851. goto host_nets_complete;
  14852. }
  14853. /* Flush bus in case a PIP response is waiting from previous test */
  14854. pt_flush_bus(cd, PT_FLUSH_BUS_BASED_ON_LEN, NULL);
  14855. /* --------------- BUS BIST TEST --------------- */
  14856. if ((cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST) != 0) {
  14857. pt_debug(dev, DL_INFO,
  14858. "%s: ----- Start BUS BIST -----", __func__);
  14859. bus_toggled = 0xFF;
  14860. rc = pt_bist_bus_test(dev, &bus_toggled, bus_err_str);
  14861. /* If this net failed clear results from previous net */
  14862. if (bus_toggled == 0) {
  14863. irq_toggled = 0x0F;
  14864. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14865. }
  14866. }
  14867. host_nets_complete:
  14868. /* --------------- SLAVE XRES BIST TEST --------------- */
  14869. if (cd->multi_chip &&
  14870. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST) != 0) {
  14871. pt_debug(dev, DL_INFO,
  14872. "%s: ----- Start Slave XRES BIST -----", __func__);
  14873. slave_xres_toggled = 0xFF;
  14874. rc = pt_bist_slave_xres_test(dev, &slave_irq_toggled,
  14875. &slave_bus_toggled, &slave_xres_toggled,
  14876. slave_xres_err_str);
  14877. if ((slave_bus_toggled == 1 && slave_irq_toggled == 1 &&
  14878. slave_xres_toggled == 1) || slave_xres_toggled == 0)
  14879. goto print_results;
  14880. }
  14881. /* --------------- SLAVE IRQ BIST TEST --------------- */
  14882. if (cd->multi_chip &&
  14883. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST) != 0) {
  14884. pt_debug(dev, DL_INFO,
  14885. "%s: ----- Start Slave IRQ BIST -----", __func__);
  14886. slave_irq_toggled = 0xFF;
  14887. rc = pt_bist_slave_irq_test(dev, &slave_irq_toggled,
  14888. &slave_bus_toggled, slave_irq_err_str, NULL, NULL);
  14889. pt_debug(dev, DL_INFO, "%s: slave_irq_toggled = 0x%02X\n",
  14890. __func__, slave_irq_toggled);
  14891. if (slave_irq_toggled == 1) {
  14892. slave_bus_toggled = 1;
  14893. goto print_results;
  14894. }
  14895. }
  14896. /* --------------- SLAVE BUS BIST TEST --------------- */
  14897. if (cd->multi_chip &&
  14898. (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST) != 0) {
  14899. pt_debug(dev, DL_INFO,
  14900. "%s: ----- Start Slave BUS BIST -----", __func__);
  14901. slave_bus_toggled = 0xFF;
  14902. rc = pt_bist_slave_bus_test(dev, &slave_irq_toggled,
  14903. &slave_bus_toggled, slave_bus_err_str);
  14904. }
  14905. print_results:
  14906. /* Restore PIP command timeout */
  14907. cd->pip_cmd_timeout = cd->pip_cmd_timeout_default;
  14908. /*
  14909. * We're done! - Perform a hard XRES toggle, allowing BL
  14910. * to load FW if there is any in Flash
  14911. */
  14912. mutex_lock(&cd->system_lock);
  14913. cd->hid_reset_cmd_state = 0;
  14914. mutex_unlock(&cd->system_lock);
  14915. pt_debug(dev, DL_INFO,
  14916. "%s: TTDL BIST Complete - Final reset\n", __func__);
  14917. if (cd->flashless_dut) {
  14918. /*
  14919. * For Flashless solution, FW update is triggered after BL is
  14920. * seen that several miliseconds delay is needed.
  14921. */
  14922. pt_debug(dev, DL_INFO, "%s Flashless Auto_BL - ALLOW\n",
  14923. __func__);
  14924. mutex_lock(&cd->system_lock);
  14925. cd->flashless_auto_bl = PT_ALLOW_AUTO_BL;
  14926. mutex_unlock(&cd->system_lock);
  14927. /* Reset DUT and wait 100ms to see if loader started */
  14928. pt_hw_hard_reset(cd);
  14929. msleep(100);
  14930. if (cd->fw_updating) {
  14931. pt_debug(dev, DL_INFO,
  14932. "%s: ----- BIST Wait FW Loading ----",
  14933. __func__);
  14934. rc = _pt_request_wait_for_enum_state(
  14935. dev, 4000, STARTUP_STATUS_COMPLETE);
  14936. }
  14937. } else {
  14938. if (cd->mode == PT_MODE_BOOTLOADER) {
  14939. rc = pt_pip2_launch_app(dev, PT_CORE_CMD_PROTECTED);
  14940. if (rc) {
  14941. pt_debug(dev, DL_ERROR,
  14942. "%s Failed to launch app\n", __func__);
  14943. rc = pt_hw_hard_reset(cd);
  14944. }
  14945. }
  14946. /*
  14947. * If FW exists the BL may have just started or will start soon,
  14948. * so the FW sentinel may be on it's way but with no FW it will
  14949. * not arrive, wait for it before deciding if we need to queue
  14950. * an enum.
  14951. */
  14952. rc = _pt_request_wait_for_enum_state(
  14953. dev, 400, STARTUP_STATUS_FW_RESET_SENTINEL);
  14954. if ((cd->startup_status & STARTUP_STATUS_FW_RESET_SENTINEL) &&
  14955. (cd->startup_status & STARTUP_STATUS_COMPLETE) == 0) {
  14956. pt_debug(dev, DL_INFO, "%s: ----- BIST Enum ----",
  14957. __func__);
  14958. pt_queue_enum(cd);
  14959. rc = _pt_request_wait_for_enum_state(
  14960. dev, 2000, STARTUP_STATUS_COMPLETE);
  14961. }
  14962. }
  14963. msleep(20);
  14964. /* --------------- PRINT OUT BIST RESULTS ---------------*/
  14965. pt_debug(dev, DL_INFO, "%s: ----- BIST Print Results ----", __func__);
  14966. pt_start_wd_timer(cd);
  14967. /* Canned print if any memory allocation issues */
  14968. if (!bus_err_str || !irq_err_str || !xres_err_str) {
  14969. ret = scnprintf(buf, strlen(buf),
  14970. "Status: %d\n"
  14971. "I2C (SDA,SCL): [UNTEST]\n"
  14972. "SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14973. "IRQ connection: [UNTEST]\n"
  14974. "TP_XRES connection: [UNTEST]\n", -ENOMEM);
  14975. if (cd->multi_chip) {
  14976. ret += scnprintf(buf + ret, strlen(buf),
  14977. "I/P SPI (MISO,MOSI,CS,CLK): [UNTEST]\n"
  14978. "I/P IRQ connection: [UNTEST]\n"
  14979. "I/P TP_XRES connection: [UNTEST]\n");
  14980. }
  14981. } else {
  14982. status = 0;
  14983. if (bus_toggled == 1)
  14984. memset(bus_err_str, 0, PT_ERR_STR_SIZE);
  14985. if (irq_toggled == 1)
  14986. memset(irq_err_str, 0, PT_ERR_STR_SIZE);
  14987. if (xres_toggled == 1)
  14988. memset(xres_err_str, 0, PT_ERR_STR_SIZE);
  14989. if (cd->ttdl_bist_select & PT_TTDL_BIST_BUS_TEST)
  14990. status += bus_toggled;
  14991. if (cd->ttdl_bist_select & PT_TTDL_BIST_IRQ_TEST)
  14992. status += irq_toggled;
  14993. if (cd->ttdl_bist_select & PT_TTDL_BIST_TP_XRES_TEST)
  14994. status += xres_toggled;
  14995. pt_debug(dev, DL_ERROR, "%s: status = %d (%d,%d,%d)\n",
  14996. __func__, status, bus_toggled, irq_toggled,
  14997. xres_toggled);
  14998. if (cd->multi_chip) {
  14999. if (slave_irq_toggled == 1)
  15000. memset(slave_irq_err_str, 0, PT_ERR_STR_SIZE);
  15001. if (slave_xres_toggled == 1)
  15002. memset(slave_xres_err_str, 0, PT_ERR_STR_SIZE);
  15003. if (slave_bus_toggled == 1)
  15004. memset(slave_bus_err_str, 0, PT_ERR_STR_SIZE);
  15005. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_BUS_TEST)
  15006. status += slave_bus_toggled;
  15007. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_IRQ_TEST)
  15008. status += slave_irq_toggled;
  15009. if (cd->ttdl_bist_select & PT_TTDL_BIST_SLAVE_XRES_TEST)
  15010. status += slave_xres_toggled;
  15011. pt_debug(dev, DL_ERROR,
  15012. "%s: status = %d (%d,%d,%d,%d,%d,%d)\n",
  15013. __func__, status, bus_toggled, irq_toggled,
  15014. xres_toggled, slave_bus_toggled,
  15015. slave_irq_toggled, slave_xres_toggled);
  15016. }
  15017. if (cd->bus_ops->bustype == BUS_I2C)
  15018. i2c_toggled = bus_toggled;
  15019. else
  15020. spi_toggled = bus_toggled;
  15021. ret = scnprintf(buf, strlen(buf),
  15022. "Status: %d\n"
  15023. "I2C (SDA,SCL): %s %s\n"
  15024. "SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15025. "IRQ connection: %s %s\n"
  15026. "TP_XRES connection: %s %s\n",
  15027. status == num_tests ? 0 : 1,
  15028. i2c_toggled == 0x0F ? "[UNTEST]" :
  15029. i2c_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15030. i2c_toggled == 0x0F ? "" : bus_err_str,
  15031. spi_toggled == 0x0F ? "[UNTEST]" :
  15032. spi_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15033. spi_toggled == 0x0F ? "" : bus_err_str,
  15034. irq_toggled == 0x0F ? "[UNTEST]" :
  15035. irq_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15036. irq_err_str,
  15037. xres_toggled == 0x0F ? "[UNTEST]" :
  15038. xres_toggled == 1 ? "[ OK ]" : "[FAILED]",
  15039. xres_err_str);
  15040. if (cd->multi_chip) {
  15041. ret += scnprintf(buf + ret, strlen(buf),
  15042. "I/P SPI (MISO,MOSI,CS,CLK): %s %s\n"
  15043. "I/P IRQ connection: %s %s\n"
  15044. "I/P TP_XRES connection: %s %s\n",
  15045. slave_bus_toggled == 0x0F ? "[UNTEST]" :
  15046. slave_bus_toggled == 1 ? "[ OK ]" :
  15047. "[FAILED]", slave_bus_err_str,
  15048. slave_irq_toggled == 0x0F ? "[UNTEST]" :
  15049. slave_irq_toggled == 1 ? "[ OK ]" :
  15050. "[FAILED]", slave_irq_err_str,
  15051. slave_xres_toggled == 0x0F ? "[UNTEST]" :
  15052. slave_xres_toggled == 1 ? "[ OK ]" :
  15053. "[FAILED]", slave_xres_err_str);
  15054. }
  15055. }
  15056. /* Put TTDL back into a known state, issue a ttdl enum if needed */
  15057. pt_debug(dev, DL_INFO, "%s: Startup_status = 0x%04X\n",
  15058. __func__, cd->startup_status);
  15059. kfree(bus_err_str);
  15060. kfree(irq_err_str);
  15061. kfree(xres_err_str);
  15062. return ret;
  15063. }
  15064. /*******************************************************************************
  15065. * FUNCTION: pt_ttdl_bist_store
  15066. *
  15067. * SUMMARY: Store method for the ttdl_bist sysfs node.
  15068. *
  15069. * RETURN: Size of passed in buffer if successful
  15070. *
  15071. * PARAMETERS:
  15072. * *dev - pointer to device structure
  15073. * *attr - pointer to device attributes
  15074. * *buf - pointer to command buffer
  15075. * size - size of buf
  15076. ******************************************************************************/
  15077. static ssize_t pt_ttdl_bist_store(struct device *dev,
  15078. struct device_attribute *attr, const char *buf, size_t size)
  15079. {
  15080. struct pt_core_data *cd = dev_get_drvdata(dev);
  15081. u32 input_data[2] = {0};
  15082. int length;
  15083. int rc = 0;
  15084. /* Maximum input of one value */
  15085. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15086. ARRAY_SIZE(input_data));
  15087. if (length != 1) {
  15088. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15089. __func__);
  15090. rc = -EINVAL;
  15091. goto exit;
  15092. } else {
  15093. mutex_lock(&cd->system_lock);
  15094. cd->ttdl_bist_select = input_data[0];
  15095. mutex_unlock(&cd->system_lock);
  15096. }
  15097. exit:
  15098. if (rc)
  15099. return rc;
  15100. return size;
  15101. }
  15102. static DEVICE_ATTR(ttdl_bist, 0644, pt_ttdl_bist_show,
  15103. pt_ttdl_bist_store);
  15104. /*******************************************************************************
  15105. * FUNCTION: pt_flush_bus_store
  15106. *
  15107. * SUMMARY: Store method for the flush_bus sysfs node.
  15108. *
  15109. * RETURN: Size of passed in buffer if successful
  15110. *
  15111. * PARAMETERS:
  15112. * *dev - pointer to device structure
  15113. * *attr - pointer to device attributes
  15114. * *buf - pointer to command buffer
  15115. * size - size of buf
  15116. ******************************************************************************/
  15117. static ssize_t pt_flush_bus_store(struct device *dev,
  15118. struct device_attribute *attr, const char *buf, size_t size)
  15119. {
  15120. struct pt_core_data *cd = dev_get_drvdata(dev);
  15121. u32 input_data[2] = {0};
  15122. int length;
  15123. int rc = 0;
  15124. /* Maximum input of one value */
  15125. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15126. ARRAY_SIZE(input_data));
  15127. if (length != 1) {
  15128. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15129. __func__);
  15130. rc = -EINVAL;
  15131. goto exit;
  15132. }
  15133. mutex_lock(&cd->system_lock);
  15134. if (input_data[0] == 0)
  15135. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15136. else if (input_data[0] == 1)
  15137. cd->flush_bus_type = PT_FLUSH_BUS_FULL_256_READ;
  15138. else
  15139. rc = -EINVAL;
  15140. mutex_unlock(&cd->system_lock);
  15141. exit:
  15142. if (rc)
  15143. return rc;
  15144. return size;
  15145. }
  15146. /*******************************************************************************
  15147. * FUNCTION: pt_flush_bus_show
  15148. *
  15149. * SUMMARY: Show method for the flush_bus sysfs node that flushes the active bus
  15150. * based on either the size of the first two bytes or a blind 256 bytes.
  15151. *
  15152. * RETURN:
  15153. * 0 = success
  15154. * !0 = failure
  15155. *
  15156. * PARAMETERS:
  15157. * *dev - pointer to device structure
  15158. * *attr - pointer to device attributes
  15159. * *buf - pointer to output buffer
  15160. ******************************************************************************/
  15161. static ssize_t pt_flush_bus_show(struct device *dev,
  15162. struct device_attribute *attr, char *buf)
  15163. {
  15164. ssize_t ret = 0;
  15165. ssize_t bytes = 0;
  15166. struct pt_core_data *cd = dev_get_drvdata(dev);
  15167. mutex_lock(&cd->system_lock);
  15168. bytes = pt_flush_bus(cd, cd->flush_bus_type, NULL);
  15169. ret = scnprintf(buf, strlen(buf),
  15170. "Status: 0\n"
  15171. "%s: %zd\n",
  15172. "Bytes flushed", bytes);
  15173. mutex_unlock(&cd->system_lock);
  15174. return ret;
  15175. }
  15176. static DEVICE_ATTR(flush_bus, 0644, pt_flush_bus_show,
  15177. pt_flush_bus_store);
  15178. /*******************************************************************************
  15179. * FUNCTION: pt_pip2_ping_test_store
  15180. *
  15181. * SUMMARY: Store method for the pip2_ping_test sysfs node.
  15182. *
  15183. * NOTE: The max PIP2 packet size is 255 (payload for PING 247) however
  15184. * someone may want to test sending invalid packet lengths so any values
  15185. * up to 255 are allowed.
  15186. *
  15187. * RETURN: Size of passed in buffer if successful
  15188. *
  15189. * PARAMETERS:
  15190. * *dev - pointer to device structure
  15191. * *attr - pointer to device attributes
  15192. * *buf - pointer to command buffer
  15193. * size - size of buf
  15194. ******************************************************************************/
  15195. static ssize_t pt_pip2_ping_test_store(struct device *dev,
  15196. struct device_attribute *attr, const char *buf, size_t size)
  15197. {
  15198. struct pt_core_data *cd = dev_get_drvdata(dev);
  15199. u32 input_data[2];
  15200. int length;
  15201. int rc = 0;
  15202. /* Maximum input of one value */
  15203. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15204. ARRAY_SIZE(input_data));
  15205. if (length != 1) {
  15206. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15207. __func__);
  15208. rc = -EINVAL;
  15209. goto exit;
  15210. }
  15211. mutex_lock(&cd->system_lock);
  15212. if (input_data[1] >= 0 && input_data[0] <= PT_MAX_PIP2_MSG_SIZE)
  15213. cd->ping_test_size = input_data[0];
  15214. else
  15215. rc = -EINVAL;
  15216. mutex_unlock(&cd->system_lock);
  15217. exit:
  15218. if (rc)
  15219. return rc;
  15220. return size;
  15221. }
  15222. /*******************************************************************************
  15223. * FUNCTION: pt_ping_test_show
  15224. *
  15225. * SUMMARY: Show method for the ping_test_show sysfs node that sends the PIP2
  15226. * PING command and ramps up the optional payload from 0 to
  15227. * ping_test_size.
  15228. * The max payload size is 247:
  15229. * (255 - 2 byte reg address - 4 byte header - 2 byte CRC)
  15230. *
  15231. * RETURN:
  15232. * 0 = success
  15233. * !0 = failure
  15234. *
  15235. * PARAMETERS:
  15236. * *dev - pointer to device structure
  15237. * *attr - pointer to device attributes
  15238. * *buf - pointer to output buffer
  15239. ******************************************************************************/
  15240. static ssize_t pt_pip2_ping_test_show(struct device *dev,
  15241. struct device_attribute *attr, char *buf)
  15242. {
  15243. struct pt_core_data *cd = dev_get_drvdata(dev);
  15244. ssize_t ret;
  15245. int last_packet_size;
  15246. int rc = 0;
  15247. rc = pt_pip2_ping_test(dev, cd->ping_test_size, &last_packet_size);
  15248. if (rc) {
  15249. ret = scnprintf(buf, strlen(buf), "Status: %d\n", rc);
  15250. return ret;
  15251. }
  15252. ret = scnprintf(buf, strlen(buf),
  15253. "Status: %d\n"
  15254. "PING payload test passed with packet sizes 0 - %d\n",
  15255. (last_packet_size == cd->ping_test_size ? 0 : 1),
  15256. last_packet_size);
  15257. return ret;
  15258. }
  15259. /*******************************************************************************
  15260. * FUNCTION: pt_t_refresh_store
  15261. *
  15262. * SUMMARY: Store method for the t-refresh sysfs node that will takes a passed
  15263. * in integer as the number of interrupts to count. A timer is started to
  15264. * calculate the total time it takes to see that number of interrupts.
  15265. *
  15266. * RETURN: Size of passed in buffer if successful
  15267. *
  15268. * PARAMETERS:
  15269. * *dev - pointer to device structure
  15270. * *attr - pointer to device attributes
  15271. * *buf - pointer to buffer that hold the command parameters
  15272. * size - size of buf
  15273. ******************************************************************************/
  15274. static ssize_t pt_t_refresh_store(struct device *dev,
  15275. struct device_attribute *attr, const char *buf, size_t size)
  15276. {
  15277. struct pt_core_data *cd = dev_get_drvdata(dev);
  15278. u32 input_data[2];
  15279. int length;
  15280. int rc = 0;
  15281. /* Maximum input of one value */
  15282. length = _pt_ic_parse_input(dev, buf, size, input_data,
  15283. ARRAY_SIZE(input_data));
  15284. if (length != 1) {
  15285. pt_debug(dev, DL_ERROR, "%s: Invalid number of arguments\n",
  15286. __func__);
  15287. rc = -EINVAL;
  15288. goto exit;
  15289. }
  15290. mutex_lock(&cd->system_lock);
  15291. pt_debug(dev, DL_INFO, "%s: Input value: %d\n", __func__,
  15292. input_data[0]);
  15293. if (input_data[0] >= 0 && input_data[0] <= 1000) {
  15294. cd->t_refresh_total = input_data[0];
  15295. cd->t_refresh_count = 0;
  15296. cd->t_refresh_active = 1;
  15297. } else {
  15298. pt_debug(dev, DL_ERROR, "%s: Invalid value\n", __func__);
  15299. rc = -EINVAL;
  15300. }
  15301. mutex_unlock(&cd->system_lock);
  15302. exit:
  15303. pt_debug(dev, DL_ERROR, "%s: rc = %d\n", __func__, rc);
  15304. if (rc)
  15305. return rc;
  15306. return size;
  15307. }
  15308. /*******************************************************************************
  15309. * FUNCTION: pt_t_refresh_show
  15310. *
  15311. * SUMMARY: Show method for the t-refresh sysfs node that will show the results
  15312. * of the T-Refresh timer counting the time it takes to see a user defined
  15313. * number of interrupts.
  15314. *
  15315. * RETURN:
  15316. * 0 = success
  15317. * !0 = failure
  15318. *
  15319. * PARAMETERS:
  15320. * *dev - pointer to device structure
  15321. * *attr - pointer to device attributes
  15322. * *buf - pointer to output buffer
  15323. ******************************************************************************/
  15324. static ssize_t pt_t_refresh_show(struct device *dev,
  15325. struct device_attribute *attr, char *buf)
  15326. {
  15327. ssize_t ret = 0;
  15328. struct pt_core_data *cd = dev_get_drvdata(dev);
  15329. u32 whole;
  15330. u16 fraction;
  15331. mutex_lock(&cd->system_lock);
  15332. /* Check if we have counted the number requested */
  15333. if (cd->t_refresh_count != cd->t_refresh_total) {
  15334. ret = scnprintf(buf, strlen(buf),
  15335. "Status: 0\n"
  15336. "%s: %d\n",
  15337. "Still counting... current IRQ count",
  15338. cd->t_refresh_count);
  15339. } else {
  15340. /* Ensure T-Refresh is de-activated */
  15341. cd->t_refresh_active = 0;
  15342. whole = cd->t_refresh_time / cd->t_refresh_count;
  15343. fraction = cd->t_refresh_time % cd->t_refresh_count;
  15344. fraction = fraction * 1000 / cd->t_refresh_count;
  15345. ret = scnprintf(buf, strlen(buf),
  15346. "Status: 0\n"
  15347. "%s: %d\n"
  15348. "%s: %d\n"
  15349. "%s: %d\n"
  15350. "%s: %d.%02d\n",
  15351. "Requested IRQ Count ", cd->t_refresh_total,
  15352. "IRQ Counted ", cd->t_refresh_count,
  15353. "Total Time Elapsed (ms) ", (int)cd->t_refresh_time,
  15354. "Average T-Refresh (ms) ", whole, fraction);
  15355. }
  15356. mutex_unlock(&cd->system_lock);
  15357. return ret;
  15358. }
  15359. /*******************************************************************************
  15360. * FUNCTION: pt_dut_status_show
  15361. *
  15362. * SUMMARY: Show method for DUT status sysfs node. Display DUT's scan state, and
  15363. * more items such as operation mode,easywake state are added in the future.
  15364. *
  15365. * RETURN: Char buffer with printed scan status information
  15366. *
  15367. * PARAMETERS:
  15368. * *dev - pointer to device structure
  15369. * *attr - pointer to device attributes
  15370. * *buf - pointer to output buffer
  15371. ******************************************************************************/
  15372. static ssize_t pt_dut_status_show(struct device *dev,
  15373. struct device_attribute *attr, char *buf)
  15374. {
  15375. u8 sys_mode = FW_SYS_MODE_UNDEFINED;
  15376. u8 mode = PT_MODE_UNKNOWN;
  15377. char *outputstring[7] = {"BOOT", "SCANNING", "DEEP_SLEEP",
  15378. "TEST", "DEEP_STANDBY", "UNDEFINED", "n/a"};
  15379. struct pt_core_data *cd = dev_get_drvdata(dev);
  15380. ssize_t ret;
  15381. u16 calculated_crc = 0;
  15382. u16 stored_crc = 0;
  15383. u8 status;
  15384. int rc = 0;
  15385. /* In STANDBY the DUT will not repond to any PIP cmd */
  15386. if (cd->fw_sys_mode_in_standby_state) {
  15387. mode = PT_MODE_OPERATIONAL;
  15388. sys_mode = FW_SYS_MODE_DEEP_STANDBY;
  15389. goto print_limited_results;
  15390. }
  15391. /* Retrieve mode and FW system mode which can only be 0-4 */
  15392. rc = pt_get_fw_sys_mode(cd, &sys_mode, &mode);
  15393. if (rc || mode == PT_MODE_UNKNOWN) {
  15394. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15395. "%s: %d\n"
  15396. "%s: n/a\n"
  15397. "%s: n/a\n"
  15398. "%s: n/a\n"
  15399. "%s: n/a\n",
  15400. "Status", rc,
  15401. "Active Exec ",
  15402. "FW System Mode ",
  15403. "Stored CRC ",
  15404. "Calculated CRC ");
  15405. return ret;
  15406. } else {
  15407. if (mode == PT_MODE_OPERATIONAL) {
  15408. if (sys_mode > FW_SYS_MODE_MAX)
  15409. sys_mode = FW_SYS_MODE_UNDEFINED;
  15410. if (sys_mode != FW_SYS_MODE_TEST)
  15411. goto print_limited_results;
  15412. rc = pt_pip_verify_config_block_crc_(cd,
  15413. PT_TCH_PARM_EBID, &status,
  15414. &calculated_crc, &stored_crc);
  15415. if (rc)
  15416. goto print_limited_results;
  15417. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15418. "%s: %d\n"
  15419. "%s: %s\n"
  15420. "%s: %s\n"
  15421. "%s: 0x%04X\n"
  15422. "%s: 0x%04X\n",
  15423. "Status", rc,
  15424. "Active Exec ", "FW",
  15425. "FW System Mode ", outputstring[sys_mode],
  15426. "Stored CRC ", stored_crc,
  15427. "Calculated CRC ", calculated_crc);
  15428. return ret;
  15429. } else {
  15430. /* When in BL or unknon mode Active Exec is "n/a" */
  15431. sys_mode = FW_SYS_MODE_UNDEFINED + 1;
  15432. }
  15433. }
  15434. print_limited_results:
  15435. ret = scnprintf(buf, PT_MAX_PRBUF_SIZE,
  15436. "%s: %d\n"
  15437. "%s: %s\n"
  15438. "%s: %s\n"
  15439. "%s: n/a\n"
  15440. "%s: n/a\n",
  15441. "Status", rc,
  15442. "Active Exec ",
  15443. mode == PT_MODE_OPERATIONAL ? "FW" : "BL",
  15444. "FW System Mode ", outputstring[sys_mode],
  15445. "Stored CRC ",
  15446. "Calculated CRC ");
  15447. return ret;
  15448. }
  15449. #endif /* TTDL_DIAGNOSTICS */
  15450. /*******************************************************************************
  15451. * Structures of sysfs attributes for all DUT dependent sysfs node
  15452. ******************************************************************************/
  15453. static struct attribute *early_attrs[] = {
  15454. &dev_attr_hw_version.attr,
  15455. &dev_attr_drv_version.attr,
  15456. &dev_attr_drv_ver.attr,
  15457. &dev_attr_fw_version.attr,
  15458. &dev_attr_sysinfo.attr,
  15459. &dev_attr_pip2_cmd_rsp.attr,
  15460. &dev_attr_command.attr,
  15461. &dev_attr_drv_debug.attr,
  15462. &dev_attr_hw_reset.attr,
  15463. &dev_attr_response.attr,
  15464. &dev_attr_ttdl_restart.attr,
  15465. #ifdef TTDL_DIAGNOSTICS
  15466. &dev_attr_ttdl_status.attr,
  15467. &dev_attr_pip2_enter_bl.attr,
  15468. &dev_attr_pip2_exit_bl.attr,
  15469. &dev_attr_err_gpio.attr,
  15470. &dev_attr_flush_bus.attr,
  15471. &dev_attr_ttdl_bist.attr,
  15472. #endif /* TTDL_DIAGNOSTICS */
  15473. NULL,
  15474. };
  15475. static struct attribute_group early_attr_group = {
  15476. .attrs = early_attrs,
  15477. };
  15478. static struct device_attribute pip2_attributes[] = {
  15479. __ATTR(pip2_version, 0444, pt_pip2_version_show, NULL),
  15480. __ATTR(pip2_gpio_read, 0444, pt_pip2_gpio_read_show, NULL),
  15481. #ifdef TTDL_DIAGNOSTICS
  15482. __ATTR(pip2_bin_hdr, 0444, pt_pip2_bin_hdr_show, NULL),
  15483. __ATTR(pip2_ping_test, 0644, pt_pip2_ping_test_show,
  15484. pt_pip2_ping_test_store),
  15485. #endif
  15486. };
  15487. static struct device_attribute attributes[] = {
  15488. __ATTR(dut_debug, 0644,
  15489. pt_dut_debug_show, pt_drv_debug_store),
  15490. __ATTR(sleep_status, 0444, pt_sleep_status_show, NULL),
  15491. __ATTR(panel_id, 0444, pt_panel_id_show, NULL),
  15492. __ATTR(get_param, 0644,
  15493. pt_get_param_show, pt_get_param_store),
  15494. #ifdef EASYWAKE_TSG6
  15495. __ATTR(easy_wakeup_gesture, 0644, pt_easy_wakeup_gesture_show,
  15496. pt_easy_wakeup_gesture_store),
  15497. __ATTR(easy_wakeup_gesture_id, 0444,
  15498. pt_easy_wakeup_gesture_id_show, NULL),
  15499. __ATTR(easy_wakeup_gesture_data, 0444,
  15500. pt_easy_wakeup_gesture_data_show, NULL),
  15501. #endif
  15502. #ifdef TTDL_DIAGNOSTICS
  15503. __ATTR(platform_data, 0444, pt_platform_data_show, NULL),
  15504. __ATTR(drv_irq, 0644, pt_drv_irq_show, pt_drv_irq_store),
  15505. __ATTR(dut_status, 0444, pt_dut_status_show, NULL),
  15506. __ATTR(t_refresh, 0644, pt_t_refresh_show, pt_t_refresh_store),
  15507. #endif /* TTDL_DIAGNOSTICS */
  15508. };
  15509. /*******************************************************************************
  15510. * FUNCTION: add_sysfs_interfaces
  15511. *
  15512. * SUMMARY: Creates all DUT dependent sysfs nodes owned by the core
  15513. *
  15514. * RETURN:
  15515. * 0 = success
  15516. * !0 = failure
  15517. *
  15518. * PARAMETERS:
  15519. * *dev - pointer to device structure
  15520. ******************************************************************************/
  15521. static int add_sysfs_interfaces(struct device *dev)
  15522. {
  15523. int i;
  15524. int j = 0;
  15525. struct pt_core_data *cd = dev_get_drvdata(dev);
  15526. for (i = 0; i < ARRAY_SIZE(attributes); i++) {
  15527. if (device_create_file(dev, attributes + i))
  15528. goto undo;
  15529. }
  15530. pt_debug(dev, DL_INFO, "%s: Active DUT Generation: %d",
  15531. __func__, cd->active_dut_generation);
  15532. if (cd->active_dut_generation == DUT_PIP2_CAPABLE) {
  15533. for (j = 0; j < ARRAY_SIZE(pip2_attributes); j++) {
  15534. if (device_create_file(dev, pip2_attributes + j))
  15535. goto undo;
  15536. }
  15537. }
  15538. return 0;
  15539. undo:
  15540. for (i--; i >= 0; i--)
  15541. device_remove_file(dev, attributes + i);
  15542. for (j--; j >= 0; j--)
  15543. device_remove_file(dev, pip2_attributes + j);
  15544. pt_debug(dev, DL_ERROR, "%s: failed to create sysfs interface\n",
  15545. __func__);
  15546. return -ENODEV;
  15547. }
  15548. /*******************************************************************************
  15549. * FUNCTION: remove_sysfs_interfaces
  15550. *
  15551. * SUMMARY: Removes all DUT dependent sysfs nodes owned by the core
  15552. *
  15553. * RETURN: void
  15554. *
  15555. * PARAMETERS:
  15556. * *dev - pointer to device structure
  15557. ******************************************************************************/
  15558. static void remove_sysfs_interfaces(struct device *dev)
  15559. {
  15560. int i;
  15561. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  15562. device_remove_file(dev, attributes + i);
  15563. for (i = 0; i < ARRAY_SIZE(pip2_attributes); i++)
  15564. device_remove_file(dev, pip2_attributes + i);
  15565. }
  15566. /*******************************************************************************
  15567. * FUNCTION: remove_sysfs_and_modules
  15568. *
  15569. * SUMMARY: Removes all DUT dependent sysfs nodes and modules
  15570. *
  15571. * RETURN: void
  15572. *
  15573. * PARAMETERS:
  15574. * *dev - pointer to device structure
  15575. ******************************************************************************/
  15576. static void remove_sysfs_and_modules(struct device *dev)
  15577. {
  15578. struct pt_core_data *cd = dev_get_drvdata(dev);
  15579. /* Queued work should be removed before to release loader module */
  15580. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  15581. pt_release_modules(cd);
  15582. pt_btn_release(dev);
  15583. pt_mt_release(dev);
  15584. remove_sysfs_interfaces(dev);
  15585. }
  15586. static int pt_ts_pinctrl_init(struct pt_core_data *cd)
  15587. {
  15588. int retval;
  15589. /* Get pinctrl if target uses pinctrl */
  15590. cd->ts_pinctrl = devm_pinctrl_get(cd->dev);
  15591. if (IS_ERR_OR_NULL(cd->ts_pinctrl)) {
  15592. retval = PTR_ERR(cd->ts_pinctrl);
  15593. dev_dbg(cd->dev,
  15594. "Target does not use pinctrl %d\n", retval);
  15595. goto err_pinctrl_get;
  15596. }
  15597. cd->pinctrl_state_active
  15598. = pinctrl_lookup_state(cd->ts_pinctrl,
  15599. PINCTRL_STATE_ACTIVE);
  15600. if (IS_ERR_OR_NULL(cd->pinctrl_state_active)) {
  15601. retval = PTR_ERR(cd->pinctrl_state_active);
  15602. dev_err(cd->dev,
  15603. "Can not lookup %s pinstate %d\n",
  15604. PINCTRL_STATE_ACTIVE, retval);
  15605. goto err_pinctrl_lookup;
  15606. }
  15607. cd->pinctrl_state_suspend
  15608. = pinctrl_lookup_state(cd->ts_pinctrl,
  15609. PINCTRL_STATE_SUSPEND);
  15610. if (IS_ERR_OR_NULL(cd->pinctrl_state_suspend)) {
  15611. retval = PTR_ERR(cd->pinctrl_state_suspend);
  15612. dev_err(cd->dev,
  15613. "Can not lookup %s pinstate %d\n",
  15614. PINCTRL_STATE_SUSPEND, retval);
  15615. goto err_pinctrl_lookup;
  15616. }
  15617. cd->pinctrl_state_release
  15618. = pinctrl_lookup_state(cd->ts_pinctrl,
  15619. PINCTRL_STATE_RELEASE);
  15620. if (IS_ERR_OR_NULL(cd->pinctrl_state_release)) {
  15621. retval = PTR_ERR(cd->pinctrl_state_release);
  15622. dev_dbg(cd->dev,
  15623. "Can not lookup %s pinstate %d\n",
  15624. PINCTRL_STATE_RELEASE, retval);
  15625. }
  15626. return 0;
  15627. err_pinctrl_lookup:
  15628. devm_pinctrl_put(cd->ts_pinctrl);
  15629. err_pinctrl_get:
  15630. cd->ts_pinctrl = NULL;
  15631. return retval;
  15632. }
  15633. /*******************************************************************************
  15634. *******************************************************************************
  15635. * FUNCTION: pt_probe
  15636. *
  15637. * SUMMARY: Probe of the core module.
  15638. *
  15639. * NOTE: For the Parade Technologies development platform (PtSBC) the
  15640. * probe functionality is split into two functions; pt_probe() and
  15641. * pt_probe_complete(). the initial setup is done in this function which
  15642. * then creates a WORK task which runs after the probe timer expires. This
  15643. * ensures the I2C/SPI is up on the PtSBC in time for TTDL.
  15644. *
  15645. * RETURN:
  15646. * 0 = success
  15647. * !0 = failure
  15648. *
  15649. * PARAMETERS:
  15650. * *ops - pointer to the bus
  15651. * *dev - pointer to the device structure
  15652. * irq - IRQ
  15653. * xfer_buf_size - size of the buffer
  15654. ******************************************************************************/
  15655. int pt_probe(const struct pt_bus_ops *ops, struct device *dev,
  15656. u16 irq, size_t xfer_buf_size)
  15657. {
  15658. struct pt_core_data *cd;
  15659. struct pt_platform_data *pdata = dev_get_platdata(dev);
  15660. enum pt_atten_type type;
  15661. struct i2c_client *client = to_i2c_client(dev);
  15662. int rc = 0;
  15663. u8 pip_ver_major;
  15664. u8 pip_ver_minor;
  15665. u32 status = STARTUP_STATUS_START;
  15666. if (!pdata || !pdata->core_pdata || !pdata->mt_pdata) {
  15667. pt_debug(dev, DL_ERROR, "%s: Missing platform data\n",
  15668. __func__);
  15669. rc = -ENODEV;
  15670. goto error_no_pdata;
  15671. }
  15672. if (pdata->core_pdata->flags & PT_CORE_FLAG_POWEROFF_ON_SLEEP) {
  15673. if (!pdata->core_pdata->power) {
  15674. pt_debug(dev, DL_ERROR,
  15675. "%s: Missing platform data function\n",
  15676. __func__);
  15677. rc = -ENODEV;
  15678. goto error_no_pdata;
  15679. }
  15680. }
  15681. /* get context and debug print buffers */
  15682. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  15683. if (!cd) {
  15684. rc = -ENOMEM;
  15685. goto error_no_pdata;
  15686. }
  15687. /* Initialize device info */
  15688. cd->dev = dev;
  15689. cd->pdata = pdata;
  15690. cd->cpdata = pdata->core_pdata;
  15691. cd->bus_ops = ops;
  15692. cd->debug_level = PT_INITIAL_DEBUG_LEVEL;
  15693. cd->show_timestamp = PT_INITIAL_SHOW_TIME_STAMP;
  15694. scnprintf(cd->core_id, 20, "%s%d", PT_CORE_NAME, core_number++);
  15695. cd->hw_detected = false;
  15696. cd->pip2_prot_active = false;
  15697. cd->pip2_send_user_cmd = false;
  15698. cd->bl_pip_ver_ready = false;
  15699. cd->app_pip_ver_ready = false;
  15700. cd->pip2_cmd_tag_seq = 0x08; /* PIP2 TAG=1 and 3 bit SEQ=0 */
  15701. cd->get_param_id = 0;
  15702. cd->watchdog_enabled = 0;
  15703. cd->startup_retry_count = 0;
  15704. cd->core_probe_complete = 0;
  15705. cd->fw_system_mode = FW_SYS_MODE_BOOT;
  15706. cd->pip_cmd_timeout = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15707. cd->pip_cmd_timeout_default = PT_PIP_CMD_DEFAULT_TIMEOUT;
  15708. cd->flashless_dut = 0;
  15709. cd->flashless_auto_bl = PT_SUPPRESS_AUTO_BL;
  15710. cd->bl_with_no_int = 0;
  15711. cd->cal_cache_in_host = PT_FEATURE_DISABLE;
  15712. cd->multi_chip = PT_FEATURE_DISABLE;
  15713. cd->tthe_hid_usb_format = PT_FEATURE_DISABLE;
  15714. cd->sleep_state = SS_SLEEP_NONE;
  15715. if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP2_CAPABLE) {
  15716. cd->set_dut_generation = true;
  15717. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15718. } else if (cd->cpdata->config_dut_generation == CONFIG_DUT_PIP1_ONLY) {
  15719. cd->set_dut_generation = true;
  15720. cd->active_dut_generation = DUT_PIP1_ONLY;
  15721. } else {
  15722. cd->set_dut_generation = false;
  15723. cd->active_dut_generation = DUT_UNKNOWN;
  15724. }
  15725. /* Initialize with platform data */
  15726. cd->watchdog_force_stop = cd->cpdata->watchdog_force_stop;
  15727. cd->watchdog_interval = PT_WATCHDOG_TIMEOUT;
  15728. cd->hid_cmd_state = 1;
  15729. cd->fw_updating = false;
  15730. cd->multi_chip = 0;
  15731. #ifdef TTDL_DIAGNOSTICS
  15732. cd->t_refresh_active = 0;
  15733. cd->t_refresh_count = 0;
  15734. cd->pip2_crc_error_count = 0;
  15735. cd->wd_xres_count = 0;
  15736. cd->bl_retry_packet_count = 0;
  15737. cd->file_erase_timeout_count = 0;
  15738. cd->show_tt_data = false;
  15739. cd->flush_bus_type = PT_FLUSH_BUS_BASED_ON_LEN;
  15740. cd->err_gpio = 0;
  15741. cd->err_gpio_type = PT_ERR_GPIO_NONE;
  15742. cd->ttdl_bist_select = 0x07;
  15743. cd->force_pip2_seq = 0;
  15744. #endif /* TTDL_DIAGNOSTICS */
  15745. memset(cd->pip2_us_file_path, 0, PT_MAX_PATH_SIZE);
  15746. memcpy(cd->pip2_us_file_path, PT_PIP2_BIN_FILE_PATH,
  15747. sizeof(PT_PIP2_BIN_FILE_PATH));
  15748. pt_init_hid_descriptor(&cd->hid_desc);
  15749. /* Read and store the descriptor lengths */
  15750. cd->hid_core.hid_report_desc_len =
  15751. le16_to_cpu(cd->hid_desc.report_desc_len);
  15752. cd->hid_core.hid_max_input_len =
  15753. le16_to_cpu(cd->hid_desc.max_input_len);
  15754. cd->hid_core.hid_max_output_len =
  15755. le16_to_cpu(cd->hid_desc.max_output_len);
  15756. /* Initialize mutexes and spinlocks */
  15757. mutex_init(&cd->module_list_lock);
  15758. mutex_init(&cd->system_lock);
  15759. mutex_init(&cd->sysfs_lock);
  15760. mutex_init(&cd->ttdl_restart_lock);
  15761. mutex_init(&cd->firmware_class_lock);
  15762. spin_lock_init(&cd->spinlock);
  15763. /* Initialize module list */
  15764. INIT_LIST_HEAD(&cd->module_list);
  15765. /* Initialize attention lists */
  15766. for (type = 0; type < PT_ATTEN_NUM_ATTEN; type++)
  15767. INIT_LIST_HEAD(&cd->atten_list[type]);
  15768. /* Initialize parameter list */
  15769. INIT_LIST_HEAD(&cd->param_list);
  15770. /* Initialize wait queue */
  15771. init_waitqueue_head(&cd->wait_q);
  15772. rc = pt_ts_pinctrl_init(cd);
  15773. if (!rc && cd->ts_pinctrl) {
  15774. /*
  15775. * Pinctrl handle is optional. If pinctrl handle is found
  15776. * let pins to be configured in active state. If not
  15777. * found continue further without error.
  15778. */
  15779. rc = pinctrl_select_state(cd->ts_pinctrl,
  15780. cd->pinctrl_state_active);
  15781. if (rc < 0)
  15782. dev_err(&client->dev, "failed to select pin to active state\n");
  15783. }
  15784. rc = pt_get_regulator(cd, true);
  15785. if (rc) {
  15786. dev_err(&client->dev, "Failed to get voltage regulators\n");
  15787. goto error_alloc_data;
  15788. }
  15789. rc = pt_enable_regulator(cd, true);
  15790. if (rc) {
  15791. dev_err(dev, "Failed to enable regulators: rc=%d\n", rc);
  15792. goto error_get_regulator;
  15793. }
  15794. /* Initialize works */
  15795. INIT_WORK(&cd->enum_work, pt_enum_work_function);
  15796. INIT_WORK(&cd->ttdl_restart_work, pt_restart_work_function);
  15797. INIT_WORK(&cd->watchdog_work, pt_watchdog_work);
  15798. /* Initialize HID specific data */
  15799. cd->hid_core.hid_vendor_id = (cd->cpdata->vendor_id) ?
  15800. cd->cpdata->vendor_id : HID_VENDOR_ID;
  15801. cd->hid_core.hid_product_id = (cd->cpdata->product_id) ?
  15802. cd->cpdata->product_id : HID_APP_PRODUCT_ID;
  15803. cd->hid_core.hid_desc_register =
  15804. cpu_to_le16(cd->cpdata->hid_desc_register);
  15805. /* Set platform easywake value */
  15806. cd->easy_wakeup_gesture = cd->cpdata->easy_wakeup_gesture;
  15807. #ifdef CONFIG_DRM
  15808. /* Setup active dsi panel */
  15809. active_panel = cd->cpdata->active_panel;
  15810. #endif
  15811. /* Set platform panel_id value */
  15812. cd->panel_id_support = cd->cpdata->panel_id_support;
  15813. if (cd->panel_id_support & PT_PANEL_ID_BY_SYS_INFO)
  15814. /* Set Panel ID to default to 0 */
  15815. cd->pid_for_loader = PT_PANEL_ID_DEFAULT;
  15816. else
  15817. /* Set Panel ID to Not Enabled */
  15818. cd->pid_for_loader = PANEL_ID_NOT_ENABLED;
  15819. /* Initialize hw_version default to FFFF.FFFF.FF */
  15820. snprintf(cd->hw_version, HW_VERSION_LEN_MAX, "FFFF.FFFF.FF");
  15821. dev_set_drvdata(dev, cd);
  15822. /* PtSBC builds will call this function in pt_probe_complete() */
  15823. pt_add_core(dev);
  15824. rc = sysfs_create_group(&dev->kobj, &early_attr_group);
  15825. if (rc) {
  15826. pt_debug(cd->dev, DL_ERROR, "%s:create early attrs failed\n",
  15827. __func__);
  15828. goto error_enable_regulator;
  15829. }
  15830. /*
  15831. * Save the pointer to a global value, which will be used
  15832. * in ttdl_restart function
  15833. */
  15834. cd->bus_ops = ops;
  15835. /*
  15836. * When the IRQ GPIO is not direclty accessible and no function is
  15837. * defined to get the IRQ status, the IRQ passed in must be assigned
  15838. * directly as the gpio_to_irq will not work. e.g. CHROMEOS
  15839. */
  15840. if (!cd->cpdata->irq_stat) {
  15841. cd->irq = irq;
  15842. pt_debug(cd->dev, DL_ERROR, "%s:No irq_stat, Set cd->irq = %d\n",
  15843. __func__, cd->irq);
  15844. }
  15845. /* Call platform init function before setting up the GPIO's */
  15846. if (cd->cpdata->init) {
  15847. pt_debug(cd->dev, DL_INFO, "%s: Init HW\n", __func__);
  15848. rc = cd->cpdata->init(cd->cpdata, PT_MT_POWER_ON, cd->dev);
  15849. } else {
  15850. pt_debug(cd->dev, DL_ERROR, "%s: No HW INIT function\n",
  15851. __func__);
  15852. rc = 0;
  15853. }
  15854. if (rc < 0) {
  15855. pt_debug(cd->dev, DL_ERROR, "%s: HW Init fail r=%d\n",
  15856. __func__, rc);
  15857. }
  15858. /* Power on any needed regulator(s) */
  15859. if (cd->cpdata->setup_power) {
  15860. pt_debug(cd->dev, DL_INFO, "%s: Device power on!\n", __func__);
  15861. rc = cd->cpdata->setup_power(cd->cpdata,
  15862. PT_MT_POWER_ON, cd->dev);
  15863. } else {
  15864. pt_debug(cd->dev, DL_ERROR, "%s: No setup power function\n",
  15865. __func__);
  15866. rc = 0;
  15867. }
  15868. if (rc < 0)
  15869. pt_debug(cd->dev, DL_ERROR, "%s: Setup power on fail r=%d\n",
  15870. __func__, rc);
  15871. #ifdef TTDL_DIAGNOSTICS
  15872. cd->watchdog_irq_stuck_count = 0;
  15873. cd->bus_transmit_error_count = 0;
  15874. #endif /* TTDL_DIAGNOSTICS */
  15875. if (cd->cpdata->detect) {
  15876. pt_debug(cd->dev, DL_INFO, "%s: Detect HW\n", __func__);
  15877. rc = cd->cpdata->detect(cd->cpdata, cd->dev,
  15878. pt_platform_detect_read);
  15879. if (!rc) {
  15880. cd->hw_detected = true;
  15881. pt_debug(cd->dev, DL_INFO,
  15882. "%s: HW detected\n", __func__);
  15883. } else {
  15884. cd->hw_detected = false;
  15885. pt_debug(cd->dev, DL_INFO,
  15886. "%s: No HW detected\n", __func__);
  15887. rc = -ENODEV;
  15888. goto error_detect;
  15889. }
  15890. } else {
  15891. pt_debug(dev, DL_ERROR,
  15892. "%s: PARADE No HW detect function pointer\n",
  15893. __func__);
  15894. /*
  15895. * "hw_reset" is not needed in the "if" statement,
  15896. * because "hw_reset" is already included in "hw_detect"
  15897. * function.
  15898. */
  15899. rc = pt_hw_hard_reset(cd);
  15900. if (rc)
  15901. pt_debug(cd->dev, DL_ERROR,
  15902. "%s: FAILED to execute HARD reset\n",
  15903. __func__);
  15904. }
  15905. if (cd->cpdata->setup_irq) {
  15906. pt_debug(cd->dev, DL_INFO, "%s: setup IRQ\n", __func__);
  15907. rc = cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_REG, cd->dev);
  15908. if (rc) {
  15909. pt_debug(dev, DL_ERROR,
  15910. "%s: Error, couldn't setup IRQ\n", __func__);
  15911. goto error_setup_irq;
  15912. }
  15913. } else {
  15914. pt_debug(dev, DL_ERROR,
  15915. "%s: IRQ function pointer not setup\n",
  15916. __func__);
  15917. goto error_setup_irq;
  15918. }
  15919. #if (KERNEL_VERSION(4, 14, 0) > LINUX_VERSION_CODE)
  15920. setup_timer(&cd->watchdog_timer, pt_watchdog_timer,
  15921. (unsigned long)cd);
  15922. #else
  15923. timer_setup(&cd->watchdog_timer, pt_watchdog_timer, 0);
  15924. #endif
  15925. pt_stop_wd_timer(cd);
  15926. #ifdef TTHE_TUNER_SUPPORT
  15927. mutex_init(&cd->tthe_lock);
  15928. cd->tthe_debugfs = debugfs_create_file(PT_TTHE_TUNER_FILE_NAME,
  15929. 0644, NULL, cd, &tthe_debugfs_fops);
  15930. #endif
  15931. rc = device_init_wakeup(dev, 1);
  15932. if (rc < 0)
  15933. pt_debug(dev, DL_ERROR, "%s: Error, device_init_wakeup rc:%d\n",
  15934. __func__, rc);
  15935. pm_runtime_get_noresume(dev);
  15936. pm_runtime_set_active(dev);
  15937. pm_runtime_enable(dev);
  15938. /* If IRQ asserted, read out all from buffer to release INT pin */
  15939. if (cd->cpdata->irq_stat) {
  15940. pt_flush_bus_if_irq_asserted(cd, PT_FLUSH_BUS_BASED_ON_LEN);
  15941. } else {
  15942. /* Force a read in case the reset sentinel already arrived */
  15943. rc = pt_read_input(cd);
  15944. if (!rc)
  15945. pt_parse_input(cd);
  15946. }
  15947. /* Without sleep DUT is not ready and will NAK the first write */
  15948. msleep(150);
  15949. /* Attempt to set the DUT generation if not yet set */
  15950. if (cd->active_dut_generation == DUT_UNKNOWN) {
  15951. if (cd->bl_pip_ver_ready ||
  15952. (cd->app_pip_ver_ready &&
  15953. IS_PIP_VER_GE(&cd->sysinfo, 1, 12))) {
  15954. cd->active_dut_generation = DUT_PIP2_CAPABLE;
  15955. pt_debug(dev, DL_INFO, "%s: dut generation is %d\n",
  15956. __func__, cd->active_dut_generation);
  15957. } else {
  15958. rc = _pt_detect_dut_generation(cd->dev,
  15959. &status, &cd->active_dut_generation,
  15960. &cd->mode);
  15961. if ((cd->active_dut_generation == DUT_UNKNOWN)
  15962. || rc) {
  15963. pt_debug(cd->dev, DL_ERROR,
  15964. " === DUT Gen unknown, Skip Enum ===\n");
  15965. goto skip_enum;
  15966. }
  15967. }
  15968. }
  15969. _pt_request_active_pip_protocol(cd->dev, PT_CORE_CMD_PROTECTED,
  15970. &pip_ver_major, &pip_ver_minor);
  15971. if (pip_ver_major == 2) {
  15972. cd->bl_pip_ver_ready = true;
  15973. pt_debug(dev, DL_ERROR,
  15974. " === PIP2.%d Detected, Attempt to launch APP ===\n",
  15975. pip_ver_minor);
  15976. cd->hw_detected = true;
  15977. } else if (pip_ver_major == 1) {
  15978. cd->app_pip_ver_ready = true;
  15979. pt_debug(dev, DL_ERROR,
  15980. " === PIP1.%d Detected ===\n", pip_ver_minor);
  15981. cd->hw_detected = true;
  15982. } else {
  15983. cd->sysinfo.ttdata.pip_ver_major = 0;
  15984. cd->sysinfo.ttdata.pip_ver_minor = 0;
  15985. cd->app_pip_ver_ready = false;
  15986. cd->hw_detected = false;
  15987. pt_debug(dev, DL_ERROR,
  15988. " === PIP Version Not Detected, Skip Enum ===\n");
  15989. /* For legacy DUTS proceed, enum will attempt to launch app */
  15990. if (cd->active_dut_generation != DUT_PIP1_ONLY)
  15991. goto skip_enum;
  15992. }
  15993. rc = pt_enum_with_dut(cd, false, &status);
  15994. pt_debug(dev, DL_ERROR, "%s: cd->startup_status=0x%04X status=0x%04X\n",
  15995. __func__, cd->startup_status, status);
  15996. if (rc == -ENODEV) {
  15997. pt_debug(cd->dev, DL_ERROR,
  15998. "%s: Enumeration Failed r=%d\n", __func__, rc);
  15999. /* For PtSBC don't error out, allow TTDL to stay up */
  16000. goto error_after_startup;
  16001. }
  16002. /* Suspend scanning until probe is complete to avoid asyc touches */
  16003. pt_pip_suspend_scanning_(cd);
  16004. if (cd->hw_detected) {
  16005. pt_debug(dev, DL_INFO, "%s: Add sysfs interfaces\n",
  16006. __func__);
  16007. rc = add_sysfs_interfaces(dev);
  16008. if (rc < 0) {
  16009. pt_debug(dev, DL_ERROR,
  16010. "%s: Error, fail sysfs init\n", __func__);
  16011. goto error_after_startup;
  16012. }
  16013. } else {
  16014. pt_debug(dev, DL_ERROR,
  16015. "%s: No HW detected, sysfs interfaces not added\n",
  16016. __func__);
  16017. }
  16018. skip_enum:
  16019. pm_runtime_put_sync(dev);
  16020. pt_debug(dev, DL_INFO, "%s: Probe: MT, BTN\n", __func__);
  16021. rc = pt_mt_probe(dev);
  16022. if (rc < 0) {
  16023. pt_debug(dev, DL_ERROR, "%s: Error, fail mt probe\n",
  16024. __func__);
  16025. goto error_after_sysfs_create;
  16026. }
  16027. rc = pt_btn_probe(dev);
  16028. if (rc < 0) {
  16029. pt_debug(dev, DL_ERROR, "%s: Error, fail btn probe\n",
  16030. __func__);
  16031. goto error_after_startup_mt;
  16032. }
  16033. pt_probe_modules(cd);
  16034. #ifdef CONFIG_HAS_EARLYSUSPEND
  16035. pt_setup_early_suspend(cd);
  16036. #elif defined(CONFIG_DRM)
  16037. pt_debug(dev, DL_ERROR, "%s: Probe: Setup drm notifier\n", __func__);
  16038. pt_setup_drm_notifier(cd);
  16039. #elif defined(CONFIG_FB)
  16040. pt_setup_fb_notifier(cd);
  16041. #endif
  16042. #ifdef NEED_SUSPEND_NOTIFIER
  16043. cd->pm_notifier.notifier_call = pt_pm_notifier;
  16044. register_pm_notifier(&cd->pm_notifier);
  16045. #endif
  16046. pt_pip_resume_scanning_(cd);
  16047. mutex_lock(&cd->system_lock);
  16048. cd->startup_status |= status;
  16049. cd->core_probe_complete = 1;
  16050. mutex_unlock(&cd->system_lock);
  16051. pt_debug(dev, DL_ERROR, "%s: TTDL Core Probe Completed Successfully\n",
  16052. __func__);
  16053. return 0;
  16054. error_after_startup_mt:
  16055. pr_err("%s PARADE error_after_startup_mt\n", __func__);
  16056. pt_mt_release(dev);
  16057. error_after_sysfs_create:
  16058. pr_err("%s PARADE error_after_sysfs_create\n", __func__);
  16059. pm_runtime_disable(dev);
  16060. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16061. device_wakeup_disable(dev);
  16062. #endif
  16063. device_init_wakeup(dev, 0);
  16064. cancel_work_sync(&cd->enum_work);
  16065. pt_stop_wd_timer(cd);
  16066. pt_free_si_ptrs(cd);
  16067. remove_sysfs_interfaces(dev);
  16068. error_after_startup:
  16069. pr_err("%s PARADE error_after_startup\n", __func__);
  16070. del_timer(&cd->watchdog_timer);
  16071. if (cd->cpdata->setup_irq)
  16072. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16073. error_setup_irq:
  16074. error_detect:
  16075. if (cd->cpdata->init)
  16076. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16077. if (cd->cpdata->setup_power)
  16078. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16079. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16080. error_enable_regulator:
  16081. pt_del_core(dev);
  16082. dev_set_drvdata(dev, NULL);
  16083. pt_enable_regulator(cd, false);
  16084. error_get_regulator:
  16085. pt_get_regulator(cd, false);
  16086. error_alloc_data:
  16087. kfree(cd);
  16088. error_no_pdata:
  16089. pr_err("%s failed.\n", __func__);
  16090. return rc;
  16091. }
  16092. EXPORT_SYMBOL_GPL(pt_probe);
  16093. /*******************************************************************************
  16094. * FUNCTION: pt_release
  16095. *
  16096. * SUMMARY: This function does the following cleanup:
  16097. * - Releases all probed modules
  16098. * - Stops the watchdog
  16099. * - Cancels all pending work tasks
  16100. * - Removes all created sysfs nodes
  16101. * - Removes all created debugfs nodes
  16102. * - Frees the IRQ
  16103. * - Deletes the core
  16104. * - Frees all pointers and HID reports
  16105. *
  16106. * RETURN:
  16107. * 0 = success
  16108. * !0 = failure
  16109. *
  16110. * PARAMETERS:
  16111. * *cd - pointer to the core data structure
  16112. ******************************************************************************/
  16113. int pt_release(struct pt_core_data *cd)
  16114. {
  16115. struct device *dev = cd->dev;
  16116. /*
  16117. * Suspend the device before freeing the startup_work and stopping
  16118. * the watchdog since sleep function restarts watchdog on failure
  16119. */
  16120. pm_runtime_suspend(dev);
  16121. pm_runtime_disable(dev);
  16122. /*
  16123. * Any 'work' that can trigger a new thread should be canceled first.
  16124. * The watchdog is also stopped again because another thread could have
  16125. * restarted it. The irq_work is cancelled last because it is used for
  16126. * all I2C/SPI communication.
  16127. */
  16128. pt_stop_wd_timer(cd);
  16129. call_atten_cb(cd, PT_ATTEN_CANCEL_LOADER, 0);
  16130. cancel_work_sync(&cd->ttdl_restart_work);
  16131. cancel_work_sync(&cd->enum_work);
  16132. pt_stop_wd_timer(cd);
  16133. pt_release_modules(cd);
  16134. pt_proximity_release(dev);
  16135. pt_btn_release(dev);
  16136. pt_mt_release(dev);
  16137. #ifdef CONFIG_HAS_EARLYSUSPEND
  16138. unregister_early_suspend(&cd->es);
  16139. #elif defined(CONFIG_DRM)
  16140. if (active_panel)
  16141. drm_panel_notifier_unregister(active_panel, &cd->fb_notifier);
  16142. #elif defined(CONFIG_FB)
  16143. fb_unregister_client(&cd->fb_notifier);
  16144. #endif
  16145. #ifdef NEED_SUSPEND_NOTIFIER
  16146. unregister_pm_notifier(&cd->pm_notifier);
  16147. #endif
  16148. #if (KERNEL_VERSION(3, 16, 0) > LINUX_VERSION_CODE)
  16149. device_wakeup_disable(dev);
  16150. #endif
  16151. device_init_wakeup(dev, 0);
  16152. #ifdef TTHE_TUNER_SUPPORT
  16153. mutex_lock(&cd->tthe_lock);
  16154. cd->tthe_exit = 1;
  16155. wake_up(&cd->wait_q);
  16156. mutex_unlock(&cd->tthe_lock);
  16157. debugfs_remove(cd->tthe_debugfs);
  16158. #endif
  16159. sysfs_remove_group(&dev->kobj, &early_attr_group);
  16160. remove_sysfs_interfaces(dev);
  16161. disable_irq_nosync(cd->irq);
  16162. if (cd->cpdata->setup_irq)
  16163. cd->cpdata->setup_irq(cd->cpdata, PT_MT_IRQ_FREE, dev);
  16164. if (cd->cpdata->init)
  16165. cd->cpdata->init(cd->cpdata, PT_MT_POWER_OFF, dev);
  16166. if (cd->cpdata->setup_power)
  16167. cd->cpdata->setup_power(cd->cpdata, PT_MT_POWER_OFF, dev);
  16168. dev_set_drvdata(dev, NULL);
  16169. pt_del_core(dev);
  16170. pt_enable_regulator(cd, false);
  16171. pt_get_regulator(cd, false);
  16172. pt_free_si_ptrs(cd);
  16173. kfree(cd);
  16174. return 0;
  16175. }
  16176. EXPORT_SYMBOL_GPL(pt_release);
  16177. MODULE_LICENSE("GPL");
  16178. MODULE_DESCRIPTION("Parade TrueTouch(R) Standard Product Core Driver");
  16179. MODULE_AUTHOR("Parade Technologies <[email protected]>");